Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
txn.objectStore(DOC_STORE).get(id).onsuccess = function (e) {
metadata = decodeMetadata(e.target.result);
// we can determine the result here if:
// 1. there is no such document
// 2. the document is deleted and we don't ask about specific rev
// When we ask with opts.rev we expect the answer to be either
// doc (possibly with _deleted=true) or missing error
if (!metadata) {
err = createError(MISSING_DOC, 'missing');
return finish();
}
var rev;
if (!opts.rev) {
rev = metadata.winningRev;
var deleted = isDeleted(metadata);
if (deleted) {
err = createError(MISSING_DOC, "deleted");
return finish();
}
} else {
rev = opts.latest ? getLatest(opts.rev, metadata) : opts.rev;
}
var objectStore = txn.objectStore(BY_SEQ_STORE);
var key = metadata.id + '::' + rev;
objectStore.index('_doc_id_rev').get(key).onsuccess = function (e) {
doc = e.target.result;
if (doc) {
doc = decodeDoc(doc);
}
function insertDoc(docInfo, resultsIdx, callback) {
// Cant insert new deleted documents
var winningRev = calculateWinningRev(docInfo.metadata);
var deleted = isDeleted(docInfo.metadata, winningRev);
if ('was_delete' in opts && deleted) {
results[resultsIdx] = createError(MISSING_DOC, 'deleted');
return callback();
}
// 4712 - detect whether a new document was inserted with a _rev
var inConflict = newEdits && rootIsMissing(docInfo);
if (inConflict) {
var err = createError(REV_CONFLICT);
results[resultsIdx] = err;
return callback();
}
var delta = deleted ? 0 : 1;
if (err) {
return cb(err);
}
var doc = result.doc;
var metadata = result.metadata;
var ctx = result.ctx;
if (opts.conflicts) {
var conflicts = collectConflicts(metadata);
if (conflicts.length) {
doc._conflicts = conflicts;
}
}
if (isDeleted(metadata, doc._rev)) {
doc._deleted = true;
}
if (opts.revs || opts.revs_info) {
var splittedRev = doc._rev.split('-');
var revNo = parseInt(splittedRev[0], 10);
var revHash = splittedRev[1];
var paths = rootToLeaf(metadata.rev_tree);
var path = null;
for (var i = 0; i < paths.length; i++) {
var currentPath = paths[i];
var hashIndex = currentPath.ids.map(function (x) { return x.id; })
.indexOf(revHash);
var hashFoundAtRevPos = hashIndex === (revNo - 1);
err.docId = id;
return cb(err);
}
var doc = result.doc;
var metadata = result.metadata;
var ctx = result.ctx;
if (opts.conflicts) {
var conflicts = collectConflicts(metadata);
if (conflicts.length) {
doc._conflicts = conflicts;
}
}
if (isDeleted(metadata, doc._rev)) {
doc._deleted = true;
}
if (opts.revs || opts.revs_info) {
var splittedRev = doc._rev.split('-');
var revNo = parseInt(splittedRev[0], 10);
var revHash = splittedRev[1];
var paths = rootToLeaf(metadata.rev_tree);
var path = null;
for (var i = 0; i < paths.length; i++) {
var currentPath = paths[i];
var hashIndex = currentPath.ids.map(function (x) { return x.id; })
.indexOf(revHash);
var hashFoundAtRevPos = hashIndex === (revNo - 1);
docStore.openCursor().onsuccess = function (event) {
var cursor = event.target.result;
if (cursor) {
var metadata = cursor.value;
var deleted = isDeleted(metadata);
metadata.deletedOrLocal = deleted ? "1" : "0";
docStore.put(metadata);
cursor.continue();
} else {
callback();
}
};
}
function processChange(doc, metadata, opts) {
var changeList = [{rev: doc._rev}];
if (opts.style === 'all_docs') {
changeList = collectLeaves(metadata.rev_tree)
.map(function (x) { return {rev: x.rev}; });
}
var change = {
id: metadata.id,
changes: changeList,
doc: doc
};
if (isDeleted(metadata, doc._rev)) {
change.deleted = true;
}
if (opts.conflicts) {
change.doc._conflicts = collectConflicts(metadata);
if (!change.doc._conflicts.length) {
delete change.doc._conflicts;
}
}
return change;
}