Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var register = require('promise-worker/register')
var Promise = require('pouchdb-promise')
var resolved = Promise.resolve()
function runTest (func) {
return resolved.then(() => {
/* eslint-disable no-new-func */
return new Function(func)()
}).catch(err => {
if (typeof console !== 'undefined' && typeof console.log === 'function') {
console.log(err)
}
return false // failed
})
}
register(message => {
return runTest(message.test)
})
function compact (revsMap, callback) {
var promise = Promise.resolve()
revsMap.forEach(function (revs, docId) {
// TODO: parallelize, for now need to be sequential to
// pass orphaned attachment tests
promise = promise.then(function () {
return new Promise(function (resolve, reject) {
api._doCompactionNoLock(docId, revs, {ctx: txn}, function (err) {
/* istanbul ignore if */
if (err) {
return reject(err)
}
resolve()
})
})
})
})
}
return new Promise(function (resolve) {
txn.batch([{
type: 'put',
prefix: stores.attachmentStore,
key: digest,
value: newAtt
}]);
resolve(!oldAtt);
});
}
// put attachments in a per-digest queue, to avoid two docs with the same
// attachment overwriting each other
var queue = attachmentQueues[digest] || Promise.resolve();
attachmentQueues[digest] = queue.then(function () {
return fetchAtt().then(saveAtt).then(function (isNewAttachment) {
callback(null, isNewAttachment);
}, callback);
});
}
function httpQuery(db, fun, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
opts = extend(true, {}, opts);
if (typeof fun === 'function') {
fun = {map : fun};
}
var promise = Promise.resolve().then(function () {
return httpQueryPromised(db, fun, opts);
});
promisedCallback(promise, callback);
return promise;
}
app.post('/:db/:id(*)', utils.jsonParser, function (req, res) {
if (!/^multipart\/form-data/.test(req.headers['content-type'])) {
return utils.sendJSON(res, 400, {
error: "bad_request",
reason: "only_multipart_accepted"
});
}
var opts = utils.makeOpts(req, req.query);
var promise = Promise.resolve();
var attachments = {};
var form = new multiparty.Form();
var doc;
form.on('error', function (err) {
promise = promise.then(function () {
throw err;
});
}).on('field', function (name, field) {
if (name !== '_rev') {
return;
}
promise = promise.then(function () {
return req.db.get(req.params.id, {rev: field});
}).then(function (theDoc) {
doc = theDoc;
});
function modifyDoc(db, doc) {
if (!(typeof doc.password == 'undefined' || doc.password === null)) {
doc.iterations = utils.dbDataFor(db).iterations;
doc.password_scheme = 'pbkdf2';
doc.salt = utils.generateSecret();
return utils.hashPassword(doc.password, doc.salt, doc.iterations).then(function (hash) {
delete doc.password;
doc.derived_key = hash;
});
}
return Promise.resolve();
}
app.setPouchDB = function (newPouchDB) {
var oldPouchDB = currentPouchDB;
currentPouchDB = newPouchDB;
var stoppingDone = Promise.resolve();
if (oldPouchDB) {
stoppingDone = app.daemonManager.stop(oldPouchDB);
}
return stoppingDone.then(function () {
return app.daemonManager.start(newPouchDB);
});
};
}, function (next) {
Promise.resolve().then(function () {
if (opts.include_docs && opts.attachments) {
return fetchAttachments(results, stores, opts)
}
}).then(function () {
callback(null, {
total_rows: docCount,
offset: opts.skip,
rows: results
})
}, callback)
next()
}).on('unpipe', function () {
throughStream.end()
function initCheckpointer() {
if (checkpointer) {
return Promise.resolve();
}
return generateReplicationId(src, target, opts).then(function (res) {
repId = res;
checkpointer = new Checkpointer(src, target, repId, returnValue);
});
}
exports.hashPassword = function (password, salt, iterations) {
var derived_key = crypto.pbkdf2(password, salt, iterations, 20);
return Promise.resolve(derived_key.toString('hex'));
};