Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// CouchDB doesn't always return the right content-type for JSON data, so
// we check for ^{ and }$ (ignoring leading/trailing whitespace)
if (!options.binary && (options.json || !options.processData) &&
typeof data !== 'object' &&
(/json/.test(content_type) ||
(/^[\s]*\{/.test(data) && /\}[\s]*$/.test(data)))) {
try {
data = JSON.parse(data.toString());
} catch (e) {}
}
if (response.statusCode >= 200 && response.statusCode < 300) {
onSuccess(data, response, callback);
} else {
error = generateErrorFromResponse(data);
error.status = response.statusCode;
callback(error);
}
});
}
return request(options, function (err, response, body) {
if (err) {
return callback(generateErrorFromResponse(err));
}
var error;
var content_type = response.headers && response.headers['content-type'];
var data = body || defaultBody();
// CouchDB doesn't always return the right content-type for JSON data, so
// we check for ^{ and }$ (ignoring leading/trailing whitespace)
if (!options.binary && (options.json || !options.processData) &&
typeof data !== 'object' &&
(/json/.test(content_type) ||
(/^[\s]*\{/.test(data) && /\}[\s]*$/.test(data)))) {
try {
data = JSON.parse(data.toString());
} catch (e) {}
}
}
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);
}
var att = docInfo.data._attachments[key];
if (att.stub) {
// still need to update the refs mapping
var id = docInfo.data._id;
var rev = docInfo.data._rev;
saveAttachmentRefs(id, rev, att.digest, attachmentSaved);
continue;
}
var data;
if (typeof att.data === 'string') {
// input is assumed to be a base64 string
try {
data = pouchdbBinaryUtils.atob(att.data);
} catch (e) {
callback(pouchdbErrors.createError(pouchdbErrors.BAD_ARG,
'Attachment is not a valid base64 string'));
return;
}
doMD5(docInfo, key, attachmentSaved)(data);
} else {
prepareAttachmentForStorage(att.data,
doMD5(docInfo, key, attachmentSaved));
}
}
function finish() {
var seq = docInfo.metadata.rev_map[docInfo.metadata.rev];
/* istanbul ignore if */
if (seq) {
// check that there aren't any existing revisions with the same
// revision id, else we shouldn't do anything
var att = docInfo.data._attachments[key];
if (att.stub) {
// still need to update the refs mapping
var id = docInfo.data._id;
var rev = docInfo.data._rev;
saveAttachmentRefs(id, rev, att.digest, attachmentSaved);
continue;
}
var data;
if (typeof att.data === 'string') {
// input is assumed to be a base64 string
try {
data = pouchdbBinaryUtils.atob(att.data);
} catch (e) {
callback(pouchdbErrors.createError(pouchdbErrors.BAD_ARG,
'Attachment is not a valid base64 string'));
return;
}
doMD5(docInfo, key, attachmentSaved)(data);
} else {
prepareAttachmentForStorage(att.data,
doMD5(docInfo, key, attachmentSaved));
}
}
function finish() {
var seq = docInfo.metadata.rev_map[docInfo.metadata.rev];
/* istanbul ignore if */
if (seq) {
// check that there aren't any existing revisions with the same
// revision id, else we shouldn't do anything
stores.bySeqStore.get(formatSeq(seq), function (err, doc) {
if (!doc) {
return callback(pouchdbErrors.createError(pouchdbErrors.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 */
if (doc._rev !== rev) {
// this failing implies something very wrong
return callback(new Error('wrong doc returned'));
}
} else {
// we didn't always store this
doc._rev = rev;
stores.bySeqStore.get(formatSeq(seq), function (err, doc) {
if (!doc) {
return callback(pouchdbErrors.createError(pouchdbErrors.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 */
if (doc._rev !== rev) {
// this failing implies something very wrong
return callback(new Error('wrong doc returned'));
}
} else {
// we didn't always store this
doc._rev = rev;