Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function preprocessString(att, blobType, callback) {
var asBinary = parseBase64(att.data);
if (asBinary.error) {
return callback(asBinary.error);
}
att.length = asBinary.length;
if (blobType === 'blob') {
att.data = binStringToBlobOrBuffer(asBinary, att.content_type);
} else if (blobType === 'base64') {
att.data = btoa(asBinary);
} else { // binary
att.data = asBinary;
}
binaryMd5(asBinary, function (result) {
att.digest = 'md5-' + result;
callback();
});
}
tx.executeSql(sql, [digest], function (tx, result) {
// websql has a bug where \u0000 causes early truncation in strings
// and blobs. to work around this, we used to use the hex() function,
// but that's not performant. after migration 6, we remove \u0000
// and add it back in afterwards
var item = result.rows.item(0);
var data = item.escaped ? unescapeBlob(item.body) :
parseHexString(item.body, encoding);
if (opts.binary) {
res = binStringToBlob(data, type);
} else {
res = btoa(data);
}
callback(null, res);
});
};
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) {
data = readAsBluffer(buffer, type)
} else {
data = buffer.toString('base64')
}
}
delete att.stub
delete att.length
att.data = data
resolve()
})
})
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) {
data = readAsBluffer(buffer, type);
} else {
data = buffer.toString('base64');
}
}
delete att.stub;
delete att.length;
att.data = data;
resolve();
});
});
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 = pouchdbBinaryUtils.binaryStringToBlobOrBuffer('', type);
}
}
} else { // non-empty
if (opts.binary) {
data = readAsBlobOrBuffer(buffer, type);
} else {
data = buffer.toString('base64');
}
}
delete att.stub;
delete att.length;
att.data = data;
resolve();
});
});
var id = encodeDocId(docId) + '/' + encodeAttachmentId(attachmentId);
var url = genDBUrl(host, id);
if (rev) {
url += '?rev=' + rev;
}
if (typeof blob === 'string') {
// input is assumed to be a base64 string
var binary;
try {
binary = atob(blob);
} catch (err) {
return callback(createError(BAD_ARG,
'Attachment is not a valid base64 string'));
}
blob = binary ? binStringToBluffer(binary, type) : '';
}
// Add the attachment
fetchJSON(url, {
headers: new Headers({'Content-Type': type}),
method: 'PUT',
body: blob
}, callback).catch(callback);
});
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) {
data = readAsBluffer(buffer, type);
} else {
data = buffer.toString('base64');
}
}
delete att.stub;
delete att.length;
att.data = data;
resolve();
});
});
function preProcessAttachment(attachment) {
if (attachment.stub) {
return Promise.resolve(attachment);
}
var binData;
if (typeof attachment.data === 'string') {
binData = parseBase64(attachment.data);
if (binData.error) {
return Promise.reject(binData.error);
}
attachment.data = binStringToBlobOrBuffer(binData, attachment.content_type);
} else {
binData = attachment.data;
}
return new Promise(function (resolve) {
md5(binData, function (result) {
attachment.digest = 'md5-' + result;
attachment.length = binData.size || binData.length || 0;
resolve(attachment);
});
});
}