pouchdb-replication
Advanced tools
Comparing version 5.4.5 to 6.0.0
@@ -233,3 +233,2 @@ 'use strict'; | ||
var checkpointer; | ||
var allErrors = []; | ||
var changedDocs = []; | ||
@@ -268,3 +267,4 @@ // Like couchdb, every replication gets a unique session id | ||
var docs = currentBatch.docs; | ||
return target.bulkDocs({docs: docs, new_edits: false}).then(function (res) { | ||
var bulkOpts = {timeout: opts.timeout}; | ||
return target.bulkDocs({docs: docs, new_edits: false}, bulkOpts).then(function (res) { | ||
/* istanbul ignore if */ | ||
@@ -275,21 +275,25 @@ if (returnValue.cancelled) { | ||
} | ||
var errors = []; | ||
var errorsById = {}; | ||
// `res` doesn't include full documents (which live in `docs`), so we create a map of | ||
// (id -> error), and check for errors while iterating over `docs` | ||
var errorsById = Object.create(null); | ||
res.forEach(function (res) { | ||
if (res.error) { | ||
result.doc_write_failures++; | ||
errors.push(res); | ||
errorsById[res.id] = res; | ||
} | ||
}); | ||
allErrors = allErrors.concat(errors); | ||
result.docs_written += currentBatch.docs.length - errors.length; | ||
var non403s = errors.filter(function (error) { | ||
return error.name !== 'unauthorized' && error.name !== 'forbidden'; | ||
}); | ||
var errorsNo = Object.keys(errorsById).length; | ||
result.doc_write_failures += errorsNo; | ||
result.docs_written += docs.length - errorsNo; | ||
docs.forEach(function (doc) { | ||
var error = errorsById[doc._id]; | ||
if (error) { | ||
returnValue.emit('denied', pouchdbUtils.clone(error)); | ||
result.errors.push(error); | ||
if (error.name === 'unauthorized' || error.name === 'forbidden') { | ||
returnValue.emit('denied', pouchdbUtils.clone(error)); | ||
} else { | ||
throw error; | ||
} | ||
} else { | ||
@@ -300,8 +304,2 @@ changedDocs.push(doc); | ||
if (non403s.length > 0) { | ||
var error = new Error('bulkDocs error'); | ||
error.other_errors = errors; | ||
abortReplication('target.bulkDocs failed to write docs', error); | ||
throw new Error('bulkWrite partial failure'); | ||
} | ||
}, function (err) { | ||
@@ -334,3 +332,6 @@ result.doc_write_failures += docs.length; | ||
getChanges(); | ||
}).catch(onCheckpointError); | ||
}).catch(function (err) { | ||
onCheckpointError(err); | ||
throw err; | ||
}); | ||
} | ||
@@ -434,4 +435,2 @@ | ||
result.status = 'aborting'; | ||
result.errors.push(err); | ||
allErrors = allErrors.concat(err); | ||
batches = []; | ||
@@ -443,7 +442,7 @@ pendingBatch = { | ||
}; | ||
completeReplication(); | ||
completeReplication(err); | ||
} | ||
function completeReplication() { | ||
function completeReplication(fatalError) { | ||
if (replicationCompleted) { | ||
@@ -463,16 +462,15 @@ return; | ||
replicationCompleted = true; | ||
var non403s = allErrors.filter(function (error) { | ||
return error.name !== 'unauthorized' && error.name !== 'forbidden'; | ||
}); | ||
if (non403s.length > 0) { | ||
var error = allErrors.pop(); | ||
if (allErrors.length > 0) { | ||
error.other_errors = allErrors; | ||
if (fatalError) { | ||
fatalError.result = result; | ||
if (fatalError.name === 'unauthorized' || fatalError.name === 'forbidden') { | ||
returnValue.emit('error', fatalError); | ||
returnValue.removeAllListeners(); | ||
} else { | ||
backOff(opts, returnValue, fatalError, function () { | ||
replicate$1(src, target, opts, returnValue); | ||
}); | ||
} | ||
error.result = result; | ||
backOff(opts, returnValue, error, function () { | ||
replicate$1(src, target, opts, returnValue); | ||
}); | ||
} else { | ||
result.errors = allErrors; | ||
returnValue.emit('complete', result); | ||
@@ -635,3 +633,2 @@ returnValue.removeAllListeners(); | ||
abortReplication('writeCheckpoint completed with error', err); | ||
throw err; | ||
} | ||
@@ -967,4 +964,22 @@ | ||
PouchDB.sync = sync; | ||
Object.defineProperty(PouchDB.prototype, 'replicate', { | ||
get: function () { | ||
var self = this; | ||
return { | ||
from: function (other, opts, callback) { | ||
return self.constructor.replicate(other, self, opts, callback); | ||
}, | ||
to: function (other, opts, callback) { | ||
return self.constructor.replicate(self, other, opts, callback); | ||
} | ||
}; | ||
} | ||
}); | ||
PouchDB.prototype.sync = function (dbName, opts, callback) { | ||
return this.constructor.sync(this, dbName, opts, callback); | ||
}; | ||
} | ||
module.exports = replication; |
{ | ||
"name": "pouchdb-replication", | ||
"version": "5.4.5", | ||
"version": "6.0.0", | ||
"description": "PouchDB's replication and sync algorithm, as a plugin.", | ||
@@ -18,9 +18,12 @@ "main": "./lib/index.js", | ||
"js-extend": "1.0.1", | ||
"pouchdb-collate": "1.2.0", | ||
"pouchdb-checkpointer": "5.4.5", | ||
"pouchdb-errors": "5.4.5", | ||
"pouchdb-generate-replication-id": "5.4.5", | ||
"pouchdb-promise": "5.4.5", | ||
"pouchdb-utils": "5.4.5" | ||
"pouchdb-collate": "6.0.0", | ||
"pouchdb-checkpointer": "6.0.0", | ||
"pouchdb-errors": "6.0.0", | ||
"pouchdb-generate-replication-id": "6.0.0", | ||
"pouchdb-promise": "6.0.0", | ||
"pouchdb-utils": "6.0.0" | ||
}, | ||
"peerDependencies": { | ||
"pouchdb-core": "6.0.0" | ||
} | ||
} |
@@ -7,4 +7,22 @@ import { replicate } from './replicateWrapper'; | ||
PouchDB.sync = sync; | ||
Object.defineProperty(PouchDB.prototype, 'replicate', { | ||
get: function () { | ||
var self = this; | ||
return { | ||
from: function (other, opts, callback) { | ||
return self.constructor.replicate(other, self, opts, callback); | ||
}, | ||
to: function (other, opts, callback) { | ||
return self.constructor.replicate(self, other, opts, callback); | ||
} | ||
}; | ||
} | ||
}); | ||
PouchDB.prototype.sync = function (dbName, opts, callback) { | ||
return this.constructor.sync(this, dbName, opts, callback); | ||
}; | ||
} | ||
export default replication; |
@@ -27,3 +27,2 @@ import { clone, filterChange, uuid } from 'pouchdb-utils'; | ||
var checkpointer; | ||
var allErrors = []; | ||
var changedDocs = []; | ||
@@ -62,3 +61,4 @@ // Like couchdb, every replication gets a unique session id | ||
var docs = currentBatch.docs; | ||
return target.bulkDocs({docs: docs, new_edits: false}).then(function (res) { | ||
var bulkOpts = {timeout: opts.timeout}; | ||
return target.bulkDocs({docs: docs, new_edits: false}, bulkOpts).then(function (res) { | ||
/* istanbul ignore if */ | ||
@@ -69,21 +69,25 @@ if (returnValue.cancelled) { | ||
} | ||
var errors = []; | ||
var errorsById = {}; | ||
// `res` doesn't include full documents (which live in `docs`), so we create a map of | ||
// (id -> error), and check for errors while iterating over `docs` | ||
var errorsById = Object.create(null); | ||
res.forEach(function (res) { | ||
if (res.error) { | ||
result.doc_write_failures++; | ||
errors.push(res); | ||
errorsById[res.id] = res; | ||
} | ||
}); | ||
allErrors = allErrors.concat(errors); | ||
result.docs_written += currentBatch.docs.length - errors.length; | ||
var non403s = errors.filter(function (error) { | ||
return error.name !== 'unauthorized' && error.name !== 'forbidden'; | ||
}); | ||
var errorsNo = Object.keys(errorsById).length; | ||
result.doc_write_failures += errorsNo; | ||
result.docs_written += docs.length - errorsNo; | ||
docs.forEach(function (doc) { | ||
var error = errorsById[doc._id]; | ||
if (error) { | ||
returnValue.emit('denied', clone(error)); | ||
result.errors.push(error); | ||
if (error.name === 'unauthorized' || error.name === 'forbidden') { | ||
returnValue.emit('denied', clone(error)); | ||
} else { | ||
throw error; | ||
} | ||
} else { | ||
@@ -94,8 +98,2 @@ changedDocs.push(doc); | ||
if (non403s.length > 0) { | ||
var error = new Error('bulkDocs error'); | ||
error.other_errors = errors; | ||
abortReplication('target.bulkDocs failed to write docs', error); | ||
throw new Error('bulkWrite partial failure'); | ||
} | ||
}, function (err) { | ||
@@ -128,3 +126,6 @@ result.doc_write_failures += docs.length; | ||
getChanges(); | ||
}).catch(onCheckpointError); | ||
}).catch(function (err) { | ||
onCheckpointError(err); | ||
throw err; | ||
}); | ||
} | ||
@@ -228,4 +229,2 @@ | ||
result.status = 'aborting'; | ||
result.errors.push(err); | ||
allErrors = allErrors.concat(err); | ||
batches = []; | ||
@@ -237,7 +236,7 @@ pendingBatch = { | ||
}; | ||
completeReplication(); | ||
completeReplication(err); | ||
} | ||
function completeReplication() { | ||
function completeReplication(fatalError) { | ||
if (replicationCompleted) { | ||
@@ -257,16 +256,15 @@ return; | ||
replicationCompleted = true; | ||
var non403s = allErrors.filter(function (error) { | ||
return error.name !== 'unauthorized' && error.name !== 'forbidden'; | ||
}); | ||
if (non403s.length > 0) { | ||
var error = allErrors.pop(); | ||
if (allErrors.length > 0) { | ||
error.other_errors = allErrors; | ||
if (fatalError) { | ||
fatalError.result = result; | ||
if (fatalError.name === 'unauthorized' || fatalError.name === 'forbidden') { | ||
returnValue.emit('error', fatalError); | ||
returnValue.removeAllListeners(); | ||
} else { | ||
backOff(opts, returnValue, fatalError, function () { | ||
replicate(src, target, opts, returnValue); | ||
}); | ||
} | ||
error.result = result; | ||
backOff(opts, returnValue, error, function () { | ||
replicate(src, target, opts, returnValue); | ||
}); | ||
} else { | ||
result.errors = allErrors; | ||
returnValue.emit('complete', result); | ||
@@ -429,3 +427,2 @@ returnValue.removeAllListeners(); | ||
abortReplication('writeCheckpoint completed with error', err); | ||
throw err; | ||
} | ||
@@ -432,0 +429,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67264
1762
9
+ Addeddouble-ended-queue@2.0.0-0(transitive)
+ Addedlie@3.1.0(transitive)
+ Addedpouchdb-binary-utils@6.0.0(transitive)
+ Addedpouchdb-checkpointer@6.0.0(transitive)
+ Addedpouchdb-collate@6.0.0(transitive)
+ Addedpouchdb-collections@6.0.0(transitive)
+ Addedpouchdb-core@6.0.0(transitive)
+ Addedpouchdb-errors@6.0.0(transitive)
+ Addedpouchdb-generate-replication-id@6.0.0(transitive)
+ Addedpouchdb-md5@6.0.0(transitive)
+ Addedpouchdb-merge@6.0.0(transitive)
+ Addedpouchdb-promise@6.0.0(transitive)
+ Addedpouchdb-utils@6.0.0(transitive)
+ Addedscope-eval@0.0.3(transitive)
- Removedacorn@1.2.25.7.4(transitive)
- Removedamdefine@1.0.1(transitive)
- Removedast-types@0.8.150.9.6(transitive)
- Removedbalanced-match@1.0.2(transitive)
- Removedbase62@1.2.8(transitive)
- Removedbrace-expansion@1.1.11(transitive)
- Removedcommander@2.20.3(transitive)
- Removedcommoner@0.10.8(transitive)
- Removedconcat-map@0.0.1(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removeddefined@1.0.1(transitive)
- Removeddetective@4.7.1(transitive)
- Removedes3ify@0.2.2(transitive)
- Removedesmangle-evaluator@1.0.1(transitive)
- Removedesprima@2.7.33.1.3(transitive)
- Removedesprima-fb@15001.1.0-dev-harmony-fb15001.1001.0-dev-harmony-fb(transitive)
- Removedfalafel@1.2.0(transitive)
- Removedforeach@2.0.6(transitive)
- Removedglob@5.0.15(transitive)
- Removedgraceful-fs@4.2.11(transitive)
- Removediconv-lite@0.4.24(transitive)
- Removedinflight@1.0.6(transitive)
- Removedinline-process-browser@1.0.0(transitive)
- Removedisarray@0.0.1(transitive)
- Removedjstransform@11.0.3(transitive)
- Removedlie@3.0.4(transitive)
- Removedminimatch@3.1.2(transitive)
- Removedminimist@1.2.8(transitive)
- Removedmkdirp@0.5.6(transitive)
- Removedobject-assign@2.1.1(transitive)
- Removedobject-keys@1.1.1(transitive)
- Removedonce@1.4.0(transitive)
- Removedpath-is-absolute@1.0.1(transitive)
- Removedpouchdb-binary-utils@5.4.5(transitive)
- Removedpouchdb-checkpointer@5.4.5(transitive)
- Removedpouchdb-collate@1.2.0(transitive)
- Removedpouchdb-collections@1.0.1(transitive)
- Removedpouchdb-errors@5.4.5(transitive)
- Removedpouchdb-generate-replication-id@5.4.5(transitive)
- Removedpouchdb-md5@5.4.5(transitive)
- Removedpouchdb-promise@5.4.5(transitive)
- Removedpouchdb-utils@5.4.5(transitive)
- Removedprivate@0.1.8(transitive)
- Removedq@1.5.1(transitive)
- Removedreadable-stream@1.0.34(transitive)
- Removedrecast@0.10.430.11.23(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsource-map@0.4.40.5.7(transitive)
- Removedstring_decoder@0.10.31(transitive)
- Removedthrough@2.3.8(transitive)
- Removedthrough2@0.6.5(transitive)
- Removedunreachable-branch-transform@0.3.0(transitive)
- Removedwrappy@1.0.2(transitive)
- Removedxtend@4.0.2(transitive)
Updatedpouchdb-checkpointer@6.0.0
Updatedpouchdb-collate@6.0.0
Updatedpouchdb-errors@6.0.0
Updatedpouchdb-promise@6.0.0
Updatedpouchdb-utils@6.0.0