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) {}
}
}).then(function (json) {
if (!ok) {
json.status = status;
var err = generateErrorFromResponse(json);
callback(err);
} else {
callback(null, json);
}
}).catch(callback);
}
}).then(function (json) {
result.data = json;
if (!result.ok) {
result.data.status = result.status;
var err = generateErrorFromResponse(result.data);
if (callback) {
return callback(err);
} else {
throw err;
}
}
if (Array.isArray(result.data)) {
result.data = result.data.map(function (v) {
if (v.error || v.missing) {
return generateErrorFromResponse(v);
} else {
return v;
}
});
}
result.data = result.data.map(function (v) {
if (v.error || v.missing) {
return generateErrorFromResponse(v);
} else {
return v;
}
});
}
processAllAttachments(newDoc.data)
.then(attachments => {
const change = oldDoc ? getUpdate() : getInsert()
if (change.error) return reject(change.error)
if (attachments) change.attachments = attachments
resolve(change)
})
.catch(reject)
})
}
let newDocs
try {
newDocs = req.docs.map(mapRequestDoc)
} catch (error) {
return callback(generateErrorFromResponse(error))
}
const docIds = newDocs.map(doc => forDocument(doc.id))
db.storage.multiGet(docIds, (error, oldDocs) => {
if (error) return callback(generateErrorFromResponse(error))
const oldDocsObj = oldDocs.reduce((result, doc) => {
if (doc && doc.id) result[doc.id] = doc
return result
}, {})
const promises = newDocs.map(newDoc => {
let oldDoc = oldDocsObj[newDoc.id]
oldDoc = typeof oldDoc === 'function' ? undefined : oldDoc
return getChange(oldDoc, newDoc)
(error, docs) => {
if (error) return callback(generateErrorFromResponse(error))
let rows = docs.map(docToRow)
callback(null, {
total_rows: db.meta.doc_count,
offset: skip,
rows
})
}
)
obj = obj.map(function (v) {
if (v.error || v.missing) {
return generateErrorFromResponse(v);
} else {
return v;
}
});
}
}).then(function (result) {
if (!ok) {
result.status = status;
throw generateErrorFromResponse(result);
}
return result;
}).then(postprocessAttachments(opts));
}