pouchdb-generate-replication-id
Advanced tools
Comparing version 6.0.2 to 6.0.3
149
lib/index.js
@@ -5,8 +5,141 @@ 'use strict'; | ||
var Promise = _interopDefault(require('pouchdb-promise')); | ||
var pouchdbMd5 = require('pouchdb-md5'); | ||
var pouchdbCollate = require('pouchdb-collate'); | ||
var lie = _interopDefault(require('lie')); | ||
var crypto = _interopDefault(require('crypto')); | ||
/* istanbul ignore next */ | ||
var PouchPromise = typeof Promise === 'function' ? Promise : lie; | ||
function binaryMd5(data, callback) { | ||
var base64 = crypto.createHash('md5').update(data, 'binary').digest('base64'); | ||
callback(base64); | ||
} | ||
// set to '_' for easier debugging | ||
function collate(a, b) { | ||
if (a === b) { | ||
return 0; | ||
} | ||
a = normalizeKey(a); | ||
b = normalizeKey(b); | ||
var ai = collationIndex(a); | ||
var bi = collationIndex(b); | ||
if ((ai - bi) !== 0) { | ||
return ai - bi; | ||
} | ||
if (a === null) { | ||
return 0; | ||
} | ||
switch (typeof a) { | ||
case 'number': | ||
return a - b; | ||
case 'boolean': | ||
return a === b ? 0 : (a < b ? -1 : 1); | ||
case 'string': | ||
return stringCollate(a, b); | ||
} | ||
return Array.isArray(a) ? arrayCollate(a, b) : objectCollate(a, b); | ||
} | ||
// couch considers null/NaN/Infinity/-Infinity === undefined, | ||
// for the purposes of mapreduce indexes. also, dates get stringified. | ||
function normalizeKey(key) { | ||
switch (typeof key) { | ||
case 'undefined': | ||
return null; | ||
case 'number': | ||
if (key === Infinity || key === -Infinity || isNaN(key)) { | ||
return null; | ||
} | ||
return key; | ||
case 'object': | ||
var origKey = key; | ||
if (Array.isArray(key)) { | ||
var len = key.length; | ||
key = new Array(len); | ||
for (var i = 0; i < len; i++) { | ||
key[i] = normalizeKey(origKey[i]); | ||
} | ||
/* istanbul ignore next */ | ||
} else if (key instanceof Date) { | ||
return key.toJSON(); | ||
} else if (key !== null) { // generic object | ||
key = {}; | ||
for (var k in origKey) { | ||
if (origKey.hasOwnProperty(k)) { | ||
var val = origKey[k]; | ||
if (typeof val !== 'undefined') { | ||
key[k] = normalizeKey(val); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return key; | ||
} | ||
function arrayCollate(a, b) { | ||
var len = Math.min(a.length, b.length); | ||
for (var i = 0; i < len; i++) { | ||
var sort = collate(a[i], b[i]); | ||
if (sort !== 0) { | ||
return sort; | ||
} | ||
} | ||
return (a.length === b.length) ? 0 : | ||
(a.length > b.length) ? 1 : -1; | ||
} | ||
function stringCollate(a, b) { | ||
// See: https://github.com/daleharvey/pouchdb/issues/40 | ||
// This is incompatible with the CouchDB implementation, but its the | ||
// best we can do for now | ||
return (a === b) ? 0 : ((a > b) ? 1 : -1); | ||
} | ||
function objectCollate(a, b) { | ||
var ak = Object.keys(a), bk = Object.keys(b); | ||
var len = Math.min(ak.length, bk.length); | ||
for (var i = 0; i < len; i++) { | ||
// First sort the keys | ||
var sort = collate(ak[i], bk[i]); | ||
if (sort !== 0) { | ||
return sort; | ||
} | ||
// if the keys are equal sort the values | ||
sort = collate(a[ak[i]], b[bk[i]]); | ||
if (sort !== 0) { | ||
return sort; | ||
} | ||
} | ||
return (ak.length === bk.length) ? 0 : | ||
(ak.length > bk.length) ? 1 : -1; | ||
} | ||
// The collation is defined by erlangs ordered terms | ||
// the atoms null, true, false come first, then numbers, strings, | ||
// arrays, then objects | ||
// null/undefined/NaN/Infinity/-Infinity are all considered null | ||
function collationIndex(x) { | ||
var id = ['boolean', 'number', 'string', 'object']; | ||
var idx = id.indexOf(typeof x); | ||
//false if -1 otherwise true, but fast!!!!1 | ||
if (~idx) { | ||
if (x === null) { | ||
return 1; | ||
} | ||
if (Array.isArray(x)) { | ||
return 5; | ||
} | ||
return idx < 3 ? (idx + 2) : (idx + 3); | ||
} | ||
/* istanbul ignore next */ | ||
if (Array.isArray(x)) { | ||
return 5; | ||
} | ||
} | ||
function sortObjectPropertiesByKey(queryParams) { | ||
return Object.keys(queryParams).sort(pouchdbCollate.collate).reduce(function (result, key) { | ||
return Object.keys(queryParams).sort(collate).reduce(function (result, key) { | ||
result[key] = queryParams[key]; | ||
@@ -20,3 +153,3 @@ return result; | ||
function generateReplicationId(src, target, opts) { | ||
var docIds = opts.doc_ids ? opts.doc_ids.sort(pouchdbCollate.collate) : ''; | ||
var docIds = opts.doc_ids ? opts.doc_ids.sort(collate) : ''; | ||
var filterFun = opts.filter ? opts.filter.toString() : ''; | ||
@@ -34,7 +167,7 @@ var queryParams = ''; | ||
return Promise.all([src.id(), target.id()]).then(function (res) { | ||
return PouchPromise.all([src.id(), target.id()]).then(function (res) { | ||
var queryData = res[0] + res[1] + filterFun + filterViewName + | ||
queryParams + docIds; | ||
return new Promise(function (resolve) { | ||
pouchdbMd5.binaryMd5(queryData, resolve); | ||
return new PouchPromise(function (resolve) { | ||
binaryMd5(queryData, resolve); | ||
}); | ||
@@ -41,0 +174,0 @@ }).then(function (md5sum) { |
{ | ||
"name": "pouchdb-generate-replication-id", | ||
"version": "6.0.2", | ||
"version": "6.0.3", | ||
"description": "PouchDB function to generate a replication ID to mark progress during replications.", | ||
@@ -16,6 +16,6 @@ "main": "./lib/index.js", | ||
"dependencies": { | ||
"pouchdb-collate": "6.0.2", | ||
"pouchdb-md5": "6.0.2", | ||
"pouchdb-promise": "6.0.2" | ||
"pouchdb-collate": "6.0.3", | ||
"pouchdb-md5": "6.0.3", | ||
"pouchdb-promise": "6.0.3" | ||
} | ||
} |
19170
198
+ Addedpouchdb-binary-utils@6.0.3(transitive)
+ Addedpouchdb-collate@6.0.3(transitive)
+ Addedpouchdb-collections@6.0.3(transitive)
+ Addedpouchdb-errors@6.0.3(transitive)
+ Addedpouchdb-md5@6.0.3(transitive)
+ Addedpouchdb-promise@6.0.3(transitive)
+ Addedpouchdb-utils@6.0.3(transitive)
- Removedpouchdb-binary-utils@6.0.2(transitive)
- Removedpouchdb-collate@6.0.2(transitive)
- Removedpouchdb-collections@6.0.2(transitive)
- Removedpouchdb-errors@6.0.2(transitive)
- Removedpouchdb-md5@6.0.2(transitive)
- Removedpouchdb-promise@6.0.2(transitive)
- Removedpouchdb-utils@6.0.2(transitive)
Updatedpouchdb-collate@6.0.3
Updatedpouchdb-md5@6.0.3
Updatedpouchdb-promise@6.0.3