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);
}
rootIsMissing(doc)) {
newDoc = createError(REV_CONFLICT);
// Update the existing document
} else if (oldDocs.hasOwnProperty(doc.id)) {
newDoc = update(txn, doc, oldDocs[doc.id]);
// The update can be rejected if it is an update to an existing
// revision, if so skip it
if (newDoc == false) {
return;
}
// New document
} else {
// Ensure new documents are also stemmed
var merged = merge([], doc.rev_tree[0], docsRevsLimit(doc));
doc.rev_tree = merged.tree;
doc.stemmedRevs = merged.stemmedRevs;
newDoc = doc;
newDoc.isNewDoc = true;
newDoc.wasDeleted = doc.revs[doc.rev].deleted ? 1 : 0;
}
if (newDoc.error) {
results[i] = newDoc;
} else {
oldDocs[newDoc.id] = newDoc;
lastWriteIndex = i;
write(txn, newDoc, i);
}
});
}
// Ignore updates to existing revisions
if ((doc.rev in oldDoc.revs) && !opts.new_edits) {
return false;
}
var isRoot = /^1-/.test(doc.rev);
// Reattach first writes after a deletion to last deleted tree
if (oldDoc.deleted && !doc.deleted && opts.new_edits && isRoot) {
var tmp = doc.revs[doc.rev].data;
tmp._rev = oldDoc.rev;
tmp._id = oldDoc.id;
doc = convertDocFormat(parseDoc(tmp, opts.new_edits, dbOpts));
}
var merged = merge(oldDoc.rev_tree, doc.rev_tree[0], docsRevsLimit(doc));
doc.stemmedRevs = merged.stemmedRevs;
doc.rev_tree = merged.tree;
// Merge the old and new rev data
var revs = oldDoc.revs;
revs[doc.rev] = doc.revs[doc.rev];
doc.revs = revs;
doc.attachments = oldDoc.attachments;
var inConflict = opts.new_edits && (((oldDoc.deleted && doc.deleted) ||
(!oldDoc.deleted && merged.conflicts !== 'new_leaf') ||
(oldDoc.deleted && !doc.deleted && merged.conflicts === 'new_branch') ||
(oldDoc.rev === doc.rev)));
if (inConflict) {
stores.docStore.get(id, function (err, metadata) {
if (err || !metadata) {
return callback(createError(MISSING_DOC, 'missing'));
}
var rev;
if (!opts.rev) {
rev = getWinningRev(metadata);
var deleted = getIsDeleted(metadata, rev);
if (deleted) {
return callback(createError(MISSING_DOC, "deleted"));
}
} else {
rev = opts.latest ? getLatest(opts.rev, metadata) : opts.rev;
}
var seq = metadata.rev_map[rev];
stores.bySeqStore.get(formatSeq(seq), function (err, doc) {
if (!doc) {
return callback(createError(MISSING_DOC));
}
/* istanbul ignore if */
if ('_id' in doc && doc._id !== metadata.id) {
// this failing implies something very wrong
return callback(new Error('wrong doc returned'));
}
doc._id = metadata.id;
if ('_rev' in doc) {
/* istanbul ignore if */
// 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);
}
if (!doc) {
err = createError(MISSING_DOC, 'missing');
return finish();
}
finish();
};
txn.txn.objectStore(DOC_STORE).get(id).onsuccess = function (e) {
var doc = e.target.result;
var rev;
if (!opts.rev) {
rev = (doc && doc.rev);
} else {
rev = opts.latest ? getLatest(opts.rev, doc) : opts.rev;
}
if (!doc || (doc.deleted && !opts.rev) || !(rev in doc.revs)) {
callback(createError(MISSING_DOC, 'missing'));
return;
}
var result = doc.revs[rev].data;
result._id = doc.id;
result._rev = rev;
// WARNING: expecting possible old format
// TODO: why are we passing the transaction in the context?
// It's not clear we ever thread these txns usefully
callback(null, {
doc: result,
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);
if (hashFoundAtRevPos || (!path && hashIndex !== -1)) {
path = currentPath;
}
}
/* istanbul ignore if */
if (!path) {
err = new Error('invalid rev tree');
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;
cursor.onsuccess = function (e) {
var cursor = e.target.result;
if (!cursor) {
return; // done
}
var metadata = decodeMetadataCompat(cursor.value);
metadata.winningRev = metadata.winningRev ||
calculateWinningRev(metadata);
function fetchMetadataSeq() {
// metadata.seq was added post-3.2.0, so if it's missing,
// we need to fetch it manually
var start = metadata.id + '::';
var end = metadata.id + '::\uffff';
var req = bySeqStore.index('_doc_id_rev').openCursor(
IDBKeyRange.bound(start, end));
var metadataSeq = 0;
req.onsuccess = function (e) {
var cursor = e.target.result;
if (!cursor) {
metadata.seq = metadataSeq;
return onGetMetadataSeq();
}
function allDocsInner(data) {
var doc = {
id: metadata.id,
key: metadata.id,
value: {
rev: winningRev
}
};
if (opts.include_docs) {
doc.doc = data;
doc.doc._rev = doc.value.rev;
if (opts.conflicts) {
doc.doc._conflicts = collectConflicts(metadata);
}
for (var att in doc.doc._attachments) {
if (doc.doc._attachments.hasOwnProperty(att)) {
doc.doc._attachments[att].stub = true;
}
}
}
if (opts.inclusive_end === false && metadata.id === opts.endkey) {
return next();
} else if (deleted) {
if (opts.deleted === 'ok') {
doc.value.deleted = true;
doc.doc = null;
} else {
/* istanbul ignore next */
return next();