/
getDocs.js
180 lines (151 loc) · 4.92 KB
/
getDocs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
'use strict';
var utils = require('pouchdb-utils');
var clone = utils.clone;
var flatten = utils.flatten;
var Promise = require('pouchdb-promise');
function isGenOne(rev) {
return /^1-/.test(rev);
}
function fileHasChanged(localDoc, remoteDoc, filename) {
return !localDoc._attachments ||
!localDoc._attachments[filename] ||
localDoc._attachments[filename].digest !==
remoteDoc._attachments[filename].digest;
}
function getDocAttachments(db, doc) {
var filenames = Object.keys(doc._attachments);
return Promise.all(filenames.map(function (filename) {
return db.getAttachment(doc._id, filename, {rev: doc._rev});
}));
}
function getDocAttachmentsFromTargetOrSource(target, src, doc) {
var doCheckForLocalAttachments = src.type() === 'http' && target.type() !== 'http';
var filenames = Object.keys(doc._attachments);
if (!doCheckForLocalAttachments) {
return getDocAttachments(src, doc);
}
return target.get(doc._id).then(function (localDoc) {
return Promise.all(filenames.map(function (filename) {
if (fileHasChanged(localDoc, doc, filename)) {
return src.getAttachment(doc._id, filename);
}
return target.getAttachment(localDoc._id, filename);
}));
}).catch(function (error) {
/* istanbul ignore if */
if (error.status !== 404) {
throw error;
}
return getDocAttachments(src, doc);
});
}
function createBulkGetOpts(diffs) {
var requests = [];
Object.keys(diffs).forEach(function (id) {
var missingRevs = diffs[id].missing;
missingRevs.forEach(function (missingRev) {
requests.push({
id: id,
rev: missingRev
});
});
});
return {
docs: requests,
revs: true
};
}
//
// Fetch all the documents from the src as described in the "diffs",
// which is a mapping of docs IDs to revisions. If the state ever
// changes to "cancelled", then the returned promise will be rejected.
// Else it will be resolved with a list of fetched documents.
//
function getDocs(src, target, diffs, state) {
diffs = clone(diffs); // we do not need to modify this
var resultDocs = [],
ok = true;
function getAllDocs() {
var bulkGetOpts = createBulkGetOpts(diffs);
if (!bulkGetOpts.docs.length) { // optimization: skip empty requests
return;
}
return src.bulkGet(bulkGetOpts).then(function (bulkGetResponse) {
/* istanbul ignore if */
if (state.cancelled) {
throw new Error('cancelled');
}
return Promise.all(bulkGetResponse.results.map(function (bulkGetInfo) {
return Promise.all(bulkGetInfo.docs.map(function (doc) {
var remoteDoc = doc.ok;
if (doc.error) {
// when AUTO_COMPACTION is set, docs can be returned which look
// like this: {"missing":"1-7c3ac256b693c462af8442f992b83696"}
ok = false;
}
if (!remoteDoc || !remoteDoc._attachments) {
return remoteDoc;
}
return getDocAttachmentsFromTargetOrSource(target, src,
remoteDoc).then(function (attachments) {
var filenames = Object.keys(remoteDoc._attachments);
attachments.forEach(function (attachment, i) {
var att = remoteDoc._attachments[filenames[i]];
delete att.stub;
delete att.length;
att.data = attachment;
});
return remoteDoc;
});
}));
}))
.then(function (results) {
resultDocs = resultDocs.concat(flatten(results).filter(Boolean));
});
});
}
function hasAttachments(doc) {
return doc._attachments && Object.keys(doc._attachments).length > 0;
}
function fetchRevisionOneDocs(ids) {
// Optimization: fetch gen-1 docs and attachments in
// a single request using _all_docs
return src.allDocs({
keys: ids,
include_docs: true
}).then(function (res) {
if (state.cancelled) {
throw new Error('cancelled');
}
res.rows.forEach(function (row) {
if (row.deleted || !row.doc || !isGenOne(row.value.rev) ||
hasAttachments(row.doc)) {
// if any of these conditions apply, we need to fetch using get()
return;
}
// the doc we got back from allDocs() is sufficient
resultDocs.push(row.doc);
delete diffs[row.id];
});
});
}
function getRevisionOneDocs() {
// filter out the generation 1 docs and get them
// leaving the non-generation one docs to be got otherwise
var ids = Object.keys(diffs).filter(function (id) {
var missing = diffs[id].missing;
return missing.length === 1 && isGenOne(missing[0]);
});
if (ids.length > 0) {
return fetchRevisionOneDocs(ids);
}
}
function returnResult() {
return { ok:ok, docs:resultDocs };
}
return Promise.resolve()
.then(getRevisionOneDocs)
.then(getAllDocs)
.then(returnResult);
}
module.exports = getDocs;