Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
var attachmentError;
req.docs.forEach(function (doc) {
if (doc._attachments) {
Object.keys(doc._attachments).forEach(function (name) {
attachmentError = attachmentError || attachmentNameError(name);
if (!doc._attachments[name].content_type) {
guardedConsole('warn', 'Attachment', name, 'on document', doc._id, 'is missing content_type');
}
});
}
});
if (attachmentError) {
return callback(createError(BAD_REQUEST, attachmentError));
}
if (!('new_edits' in opts)) {
if ('new_edits' in req) {
opts.new_edits = req.new_edits;
} else {
opts.new_edits = true;
}
}
var adapter = this;
if (!opts.new_edits && !isRemote(adapter)) {
// ensure revisions of the same doc are sorted, so that
// the local adapter processes them correctly (#2935)
req.docs.sort(compareByIdThenRev);
}
stores.docStore.get(id, function (err, metadata) {
if (err || !metadata) {
return callback(createError(MISSING_DOC, 'missing'))
}
var rev = getWinningRev(metadata)
var deleted = getIsDeleted(metadata, rev)
if (deleted && !opts.rev) {
return callback(createError(MISSING_DOC, 'deleted'))
}
rev = opts.rev ? opts.rev : rev
var seq = metadata.rev_map[rev]
stores.bySeqStore.get(formatSeq(seq), function (err, doc) {
if (err) {
return callback(createError(MISSING_DOC, 'error'))
}
if (!doc) {
return callback(createError(MISSING_DOC))
}
/* istanbul ignore if */
if ('_id' in doc && doc._id !== metadata.id) {
// this failing implies something very wrong
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;
writeDoc(docInfo, winningRev, deleted, deleted, false,
delta, resultsIdx, callback);
}
txn.get(stores.localStore, doc._id, function (err, resp) {
if (err) {
/* istanbul ignore if */
if (err.name !== 'NotFoundError') {
return callback(err);
} else {
return callback(createError(MISSING_DOC));
}
}
if (resp._rev !== doc._rev) {
return callback(createError(REV_CONFLICT));
}
txn.batch([{
prefix: stores.localStore,
type: 'del',
key: doc._id
}]);
var ret = {ok: true, id: doc._id, rev: '0-0'};
if (opts.ctx) {
// don't execute immediately
return callback(null, ret);
}
txn.execute(db, function (err) {
/* istanbul ignore if */
if (err) {
return callback(err);
}
function parseRevisionInfo(rev) {
if (!/^\d+-/.test(rev)) {
return createError(INVALID_REV);
}
var idx = rev.indexOf('-');
var left = rev.substring(0, idx);
var right = rev.substring(idx + 1);
return {
prefix: parseInt(left, 10),
id: right
};
}
db.storage.get(forDocument(id), (error, doc) => {
if (error) {
return callback(
createError(MISSING_DOC, error.message || 'missing-read-error')
)
}
if (!doc) {
return callback(createError(MISSING_DOC, 'missing-rev-tree'))
}
callback(null, doc.rev_tree)
})
}
stores.docStore.get(docId, function (err, metadata) {
if (err) {
callback(createError(MISSING_DOC));
} else {
callback(null, metadata.rev_tree);
}
});
};
db.storage.get(keys.forDocument(id), (error, doc) => {
if (error) {
return callback(createError(
MISSING_DOC, error.message || 'missing-read-error'))
}
if (!doc) {
return callback(createError(MISSING_DOC, 'missing'))
}
callback(null, doc.rev_tree)
})
}
db.storage.get(forDocument(id), (error, meta) => {
if (error) {
return callback(
createError(MISSING_DOC, error.message || 'missing-read-error')
)
}
if (meta === null) {
return callback(createError(MISSING_DOC, 'missing-no-meta-found'))
}
const rev = opts.rev || winningRev(meta)
if (!meta || (meta.deleted && !opts.rev) || !(rev in meta.rev_map)) {
return callback(createError(MISSING_DOC, 'missing-rev-check'))
}
db.storage.get(forSequence(meta.rev_map[rev]), (error, doc) => {
if (error) {
return callback(
createError(MISSING_DOC, error.message || 'missing-read-error')
)
}
return callback(null, { doc, metadata: meta })
})
db.storage.get(attachmentKey, (error, data) => {
if (error) return reject(createError(MISSING_STUB, error.message))
if (!data)
return reject(
createError(MISSING_STUB, 'can not find attachment')
)
return resolve({
attachment,
dbAttachment: [attachmentKey, data]
})
})
})