Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions packages/node_modules/pouchdb-adapter-indexeddb/src/attachments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

import {
atob,
binaryStringToBlobOrBuffer as binStringToBlobOrBuffer,
blobOrBufferToBase64 as blufferToBase64,
} from 'pouchdb-binary-utils';

import { createError, BAD_ARG } from 'pouchdb-errors';
import { binaryMd5 as md5 } from 'pouchdb-md5';

// Convert an attachment supplied by the user into the format this adapter
// stores: a blob or base64 string, with digest and length filled in. Stub
// attachments are passed through untouched; the caller resolves them against
// the attachments already stored for the document.
export default function preProcessAttachment(attachment, attachmentFormat) {
if (attachment.stub) {
return Promise.resolve(attachment);
}

let binData;
if (typeof attachment.data === 'string') {
try {
binData = atob(attachment.data);
} catch (e) {
return Promise.reject(createError(BAD_ARG, 'Attachment is not a valid base64 string'));
}
if (attachmentFormat === 'binary') {
attachment.data = binStringToBlobOrBuffer(binData, attachment.content_type);
}
} else {
binData = attachment.data;
if (attachmentFormat === 'base64') {
// TODO could run these in parallel, if we cared
return new Promise(resolve => {
blufferToBase64(attachment.data, function (b64) {
attachment.data = b64;
md5(binData, function (result) {
attachment.digest = 'md5-' + result;
attachment.length = binData.size || binData.length || 0;
resolve(attachment);
});
});
});
}
}

return new Promise(function (resolve) {
md5(binData, function (result) {
attachment.digest = 'md5-' + result;
attachment.length = binData.size || binData.length || 0;
resolve(attachment);
});
});
}
112 changes: 55 additions & 57 deletions packages/node_modules/pouchdb-adapter-indexeddb/src/bulkDocs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,14 @@ import {
REV_CONFLICT,
MISSING_DOC,
MISSING_STUB,
BAD_ARG,
UNKNOWN_ERROR
} from 'pouchdb-errors';

import {
atob,
binaryStringToBlobOrBuffer as binStringToBlobOrBuffer,
blobOrBufferToBase64 as blufferToBase64,
} from 'pouchdb-binary-utils';

import { isLocalId, parseDoc } from 'pouchdb-adapter-utils';
import { binaryMd5 as md5 } from 'pouchdb-md5';
import { winningRev as calculateWinningRev, merge, compactTree } from 'pouchdb-merge';

import preProcessAttachment from './attachments';
import { putLocal, removeLocal, preProcessLocalAttachments } from './localDocs';
import { DOC_STORE, META_LOCAL_STORE, idbError } from './util';

import { rewrite, sanitise } from './rewrite';
Expand All @@ -32,6 +26,7 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
let error;
const results = [];
const docs = [];
const localDocs = [];
let lastWriteIndex;

const revsLimit = dbOpts.revs_limit || 1000;
Expand All @@ -52,19 +47,26 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
function fetchExistingDocs(txn, docs) {
let fetched = 0;
const oldDocs = {};
const docsToFetch = docs.filter(Boolean);

function readDone(e) {
if (e.target.result) {
oldDocs[e.target.result.id] = e.target.result;
}
if (++fetched === docs.length) {
if (++fetched === docsToFetch.length) {
processDocs(txn, docs, oldDocs);
processLocalDocs(txn, localDocs);
}
}

docs.forEach(function (doc) {
const docStore = isLocalId(doc.id) ? META_LOCAL_STORE : DOC_STORE;
txn.objectStore(docStore).get(doc.id).onsuccess = readDone;
if (!docsToFetch.length) {
processDocs(txn, docs, oldDocs);
processLocalDocs(txn, localDocs);
return;
}

docsToFetch.forEach(function (doc) {
txn.objectStore(DOC_STORE).get(doc.id).onsuccess = readDone;
});
}

Expand All @@ -79,6 +81,10 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
function processDocs(txn, docs, oldDocs) {

docs.forEach(function (doc, i) {
if (!doc) {
return;
}

let newDoc;

// The first document write cannot be a deletion
Expand Down Expand Up @@ -121,6 +127,27 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
});
}

// Local documents are written through their own 0-N revision path and keep
// no revision history.
function processLocalDocs(txn, localDocs) {
localDocs.forEach(function (entry) {
const doc = entry.doc;
const i = entry.index;
lastWriteIndex = i;

const done = function (err, res) {
results[i] = err || res;
updateSeq(i);
};

if (doc._deleted) {
removeLocal(txn, doc, opts, done);
} else {
putLocal(txn, doc, opts, done);
}
});
}

// Converts from the format returned by parseDoc into the new format
// we use to store
function convertDocFormat(doc) {
Expand Down Expand Up @@ -308,56 +335,15 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
}
}

function preProcessAttachment(attachment) {
if (attachment.stub) {
return Promise.resolve(attachment);
}

let binData;
if (typeof attachment.data === 'string') {
try {
binData = atob(attachment.data);
} catch (e) {
return Promise.reject(createError(BAD_ARG, 'Attachment is not a valid base64 string'));
}
if (metadata.idb_attachment_format === 'binary') {
attachment.data = binStringToBlobOrBuffer(binData, attachment.content_type);
}
} else {
binData = attachment.data;
if (metadata.idb_attachment_format === 'base64') {
// TODO could run these in parallel, if we cared
return new Promise(resolve => {
blufferToBase64(attachment.data, function (b64) {
attachment.data = b64;
md5(binData, function (result) {
attachment.digest = 'md5-' + result;
attachment.length = binData.size || binData.length || 0;
resolve(attachment);
});
});
});
}
}

return new Promise(function (resolve) {
md5(binData, function (result) {
attachment.digest = 'md5-' + result;
attachment.length = binData.size || binData.length || 0;
resolve(attachment);
});
});
}

function preProcessAttachments() {
const promises = docs.map(function (doc) {
const promises = docs.filter(Boolean).map(function (doc) {
const data = doc.revs[doc.rev].data;
if (!data._attachments) {
return Promise.resolve(data);
}
const attachments = Object.keys(data._attachments).map(function (k) {
data._attachments[k].name = k;
return preProcessAttachment(data._attachments[k]);
return preProcessAttachment(data._attachments[k], metadata.idb_attachment_format);
});

return Promise.all(attachments).then(function (newAttachments) {
Expand All @@ -374,11 +360,21 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
}

for (let i = 0, len = req.docs.length; i < len; i++) {
const userDoc = req.docs[i];

if (userDoc && isLocalId(userDoc._id)) {
// Local documents use their own 0-N revision scheme and are stored in
// META_LOCAL_STORE, so they never go through parseDoc.
localDocs.push({doc: userDoc, index: i});
docs.push(null);
continue;
}

let result;
// TODO: We should get rid of throwing for invalid docs, also not sure
// why this is needed in idb-next and not idb
try {
result = parseDoc(req.docs[i], opts.new_edits, dbOpts);
result = parseDoc(userDoc, opts.new_edits, dbOpts);
} catch (err) {
result = err;
}
Expand All @@ -391,7 +387,9 @@ export default function (api, req, opts, metadata, dbOpts, idbChanges, callback)
docs.push(convertDocFormat(result));
}

preProcessAttachments().then(function () {
preProcessLocalAttachments(localDocs.map(function (entry) {
return entry.doc;
}), metadata.idb_attachment_format).then(preProcessAttachments).then(function () {
// We _could_ check doc ids here, and skip opening DOC_STORE if all docs are local.
// This may marginally slow things down for local docs. It seems pragmatic to keep
// the code simple and optimise for calls to bulkDocs() which include non-local docs.
Expand Down
27 changes: 27 additions & 0 deletions packages/node_modules/pouchdb-adapter-indexeddb/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,33 @@ function IndexeddbPouch(dbOpts, callback) {
bulkDocs(api, req, opts, metadata, dbOpts, idbChanges, callback);
});

// Local documents use 0-N revisions with no history. Without these, core
// falls through to the ordinary revision handling, which starts at 1-<hash>.
api._putLocal = function (doc, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
api._bulkDocs({docs: [doc]}, opts, function (err, results) {
if (err) {
return callback(err);
}
const result = results[0];
if (result && result.error) {
return callback(result);
}
callback(null, result);
});
};

api._removeLocal = function (doc, opts, callback) {
if (typeof opts === 'function') {
callback = opts;
opts = {};
}
api._putLocal(Object.assign({}, doc, {_deleted: true}), opts, callback);
};

api._allDocs = $t(function (txn, opts, cb) {
allDocs(txn, metadata, opts, cb);
}, [DOC_STORE]);
Expand Down
Loading