Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
args = clone(args);
var self = this;
var tempCB =
(typeof args[args.length - 1] === 'function') ? args.pop() : false;
// if the last argument is a function, assume its a callback
var usedCB;
if (tempCB) {
// if it was a callback, create a new callback which calls it,
// but do so async so we don't trap any errors
usedCB = function (err, resp) {
process.nextTick(function () {
tempCB(err, resp);
});
};
}
var promise = new Promise(function (fulfill, reject) {
var resp;
try {
var callback = once(function (err, mesg) {
if (err) {
reject(err);
} else {
fulfill(mesg);
}
});
// create a callback for this invocation
// apply the function in the orig context
args.push(callback);
resp = func.apply(self, args);
if (resp && typeof resp.then === 'function') {
fulfill(resp);
}
function fetchAtt () {
return new Promise(function (resolve, reject) {
txn.get(stores.attachmentStore, digest, function (err, oldAtt) {
/* istanbul ignore if */
if (err && err.name !== 'NotFoundError') {
return reject(err)
}
resolve(oldAtt)
})
})
}
var newAtt = {};
if (oldAtt) {
if (oldAtt.refs) {
// only update references if this attachment already has them
// since we cannot migrate old style attachments here without
// doing a full db scan for references
newAtt.refs = oldAtt.refs;
newAtt.refs[ref] = true;
}
} else {
newAtt.refs = {};
newAtt.refs[ref] = true;
}
return new Promise(function (resolve) {
txn.batch([{
type: 'put',
prefix: stores.attachmentStore,
key: digest,
value: newAtt
}]);
resolve(!oldAtt);
});
}
function fetchAttachment (att, stores, opts) {
var type = att.content_type
return new Promise(function (resolve, reject) {
stores.binaryStore.get(att.digest, function (err, buffer) {
var data
if (err) {
/* istanbul ignore if */
if (err.name !== 'NotFoundError') {
return reject(err)
} else {
// empty
if (!opts.binary) {
data = ''
} else {
data = binStringToBluffer('', type)
}
}
} else { // non-empty
if (opts.binary) {
var newAtt = {}
if (oldAtt) {
if (oldAtt.refs) {
// only update references if this attachment already has them
// since we cannot migrate old style attachments here without
// doing a full db scan for references
newAtt.refs = oldAtt.refs
newAtt.refs[ref] = true
}
} else {
newAtt.refs = {}
newAtt.refs[ref] = true
}
return new Promise(function (resolve) {
txn.batch([{
type: 'put',
prefix: stores.attachmentStore,
key: digest,
value: newAtt
}])
resolve(!oldAtt)
})
}
function fetchAtt() {
return new Promise(function (resolve, reject) {
txn.get(stores.attachmentStore, digest, function (err, oldAtt) {
/* istanbul ignore if */
if (err && err.name !== 'NotFoundError') {
return reject(err);
}
resolve(oldAtt);
});
});
}
var ajax = function (opts) {
return new Promise(function (resolve, reject) {
ajaxCore(opts, wrapError(function (err, res) {
if (err) {
return reject(err);
}
resolve(res);
}));
});
};
var updateUser = function (user, opts) {