This repository has been archived by the owner on Jul 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
544 lines (481 loc) · 16.7 KB
/
index.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/*
Copyright 2014-2015, Marten de Vries
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
"use strict";
var nodify = require("promise-nodify");
exports.installStaticWrapperMethods = function (PouchDB, handlers) {
//set an 'alternative constructor' so the constructor can be easily
//wrapped, since wrapping 'real' constructors is hard.
PouchDB.new = PouchDB.new || function (name, options, callback) {
return new PouchDB(name, options, callback);
};
PouchDB.destroy = PouchDB.destroy || function (name, options, callback) {
var args = parseBaseArgs(PouchDB, this, options, callback);
var db = new PouchDB(name, args.options);
var promise = db.destroy();
nodify(promise, args.callback);
return promise;
};
installWrappers(PouchDB, handlers, exports.createStaticWrapperMethod);
};
exports.installWrapperMethods = function (db, handlers) {
installWrappers(db, handlers, exports.createWrapperMethod);
};
function installWrappers(base, handlers, createWrapperMethod) {
for (var name in handlers) {
if (!handlers.hasOwnProperty(name)) {
continue;
}
var info = getBaseAndName(base, name);
var original = info.base[info.name];
if (!original) {
//no method to wrap
continue;
}
if (original.hasOwnProperty("_handlers")) {
if (original._handlers.indexOf(handlers[name]) !== -1) {
throw new Error("Wrapper method for '" + name + "' already installed: " + handlers[name]);
}
original._handlers.push(handlers[name]);
} else {
info.base[info.name] = createWrapperMethod(name, original, handlers[name], base);
}
}
}
function getBaseAndName(base, name) {
name = name.split(".");
while (name.length > 1) {
base = base[name.shift(0)];
}
return {
base: base,
name: name[0]
};
}
exports.createStaticWrapperMethod = function (name, original, handler, PouchDB) {
//PouchDB is optional
return createWrapper(name, original, handler, staticWrapperBuilders, PouchDB);
};
exports.createWrapperMethod = function (name, original, handler, db) {
//db is optional
return createWrapper(name, original, handler, wrapperBuilders, db);
};
function createWrapper(name, original, handler, theWrapperBuilders, thisVal) {
//thisVal is optional
var buildWrapper = theWrapperBuilders[name];
if (typeof buildWrapper === "undefined") {
throw new Error("No known wrapper for method name: " + name); //coverage: ignore
}
var handlers = [handler];
var wrapper = buildWrapper(thisVal, original, handlers);
wrapper._original = original;
wrapper._handlers = handlers;
return wrapper;
}
var wrapperBuilders = {};
wrapperBuilders.destroy = function (db, destroy, handlers) {
return function (options, callback) {
var args = parseBaseArgs(db, this, options, callback);
return callHandlers(handlers, args, makeCall(destroy));
};
};
wrapperBuilders.put = function (db, put, handlers) {
return function (/*doc, docId, docRev, options, callback*/) {
var args = {};
args.base = db || this;
var argsList = Array.prototype.slice.call(arguments);
//parsing code borrowed from PouchDB (adapted).
args.doc = argsList.shift();
var id = '_id' in args.doc;
do {
var temp = argsList.shift();
var temptype = typeof temp;
if (temptype === "string" && !id) {
args.doc._id = temp;
id = true;
} else if (temptype === "string" && id && !('_rev' in args.doc)) {
args.doc._rev = temp;
} else if (temptype === "object") {
args.options = temp;
} else if (temptype === "function") {
args.callback = temp;
}
} while (argsList.length);
args.options = args.options || {};
return callHandlers(handlers, args, function () {
return put.call(this, args.doc, args.options);
});
};
};
wrapperBuilders.post = function (db, post, handlers) {
return function (doc, options, callback) {
var args = parseBaseArgs(db, this, options, callback);
args.doc = doc;
return callHandlers(handlers, args, function () {
return post.call(this, args.doc, args.options);
});
};
};
wrapperBuilders.get = function (db, get, handlers) {
return function (docId, options, callback) {
var args = parseBaseArgs(db, this, options, callback);
args.docId = docId;
return callHandlers(handlers, args, function () {
return get.call(this, args.docId, args.options);
});
};
};
wrapperBuilders.remove = function (db, remove, handlers) {
return function (docOrId, optsOrRev, opts, callback) {
var args;
//originally borrowed from PouchDB
if (typeof optsOrRev === 'string') {
// id, rev, opts, callback style
args = parseBaseArgs(db, this, opts, callback);
args.doc = {
_id: docOrId,
_rev: optsOrRev
};
} else {
// doc, opts, callback style
args = parseBaseArgs(db, this, optsOrRev, opts);
args.doc = docOrId;
}
return callHandlers(handlers, args, function () {
return remove.call(this, args.doc, args.options);
});
};
};
wrapperBuilders.bulkDocs = function (db, bulkDocs, handlers) {
return function (docs, options, callback) {
var args = parseBaseArgs(db, this, options, callback);
//support the deprecated signature.
if (typeof(docs) === 'object' && 'new_edits' in docs) {
args.options.new_edits = docs.new_edits;
}
args.docs = docs.docs || docs;
return callHandlers(handlers, args, function () {
return bulkDocs.call(this, args.docs, args.options);
});
};
};
wrapperBuilders.allDocs = function (db, allDocs, handlers) {
return function (options, callback) {
var args = parseBaseArgs(db, this, options, callback);
return callHandlers(handlers, args, makeCallWithOptions(allDocs, args));
};
};
wrapperBuilders.bulkGet = wrapperBuilders.allDocs;
wrapperBuilders.changes = function (db, changes, handlers) {
return function (options, callback) {
//the callback argument is no longer documented. (And deprecated?)
var args = parseBaseArgs(db, this, options, callback);
return callHandlers(handlers, args, makeCallWithOptions(changes, args));
};
};
wrapperBuilders.sync = function (db, replicate, handlers) {
return function (url, options, callback) {
var args = parseBaseArgs(db, this, options, callback);
args.url = url;
return callHandlers(handlers, args, function () {
return replicate.call(this, args.url, args.options);
});
};
};
wrapperBuilders["replicate.from"] = wrapperBuilders.sync;
wrapperBuilders["replicate.to"] = wrapperBuilders.sync;
wrapperBuilders.putAttachment = function (db, putAttachment, handlers) {
return function (docId, attachmentId, rev, doc, type, options, callback) {
//options is not an 'official' argument. But some plug-ins need it
//and maybe (?) also the http adapter.
//valid calls:
//- "id", "aid", "rev", new Blob(), "text/plain", {}, function () {}
//- "id", "aid", new Blob(), "text/plain", {}, function () {}
//- "id", "aid", new Blob(), "text/plain"
var args;
if (typeof type === "string") {
//rev is specified
args = parseBaseArgs(db, this, options, callback);
args.rev = rev;
args.doc = doc;
args.type = type;
} else {
//rev is unspecified
args = parseBaseArgs(db, this, type, options);
args.rev = null;
args.doc = rev;
args.type = doc;
}
//fixed arguments
args.docId = docId;
args.attachmentId = attachmentId;
return callHandlers(handlers, args, function () {
return putAttachment.call(this, args.docId, args.attachmentId, args.rev, args.doc, args.type);
});
};
};
wrapperBuilders.getAttachment = function (db, getAttachment, handlers) {
return function (docId, attachmentId, options, callback) {
var args = parseBaseArgs(db, this, options, callback);
args.docId = docId;
args.attachmentId = attachmentId;
return callHandlers(handlers, args, function () {
return getAttachment.call(this, args.docId, args.attachmentId, args.options);
});
};
};
wrapperBuilders.removeAttachment = function (db, removeAttachment, handlers) {
return function (docId, attachmentId, rev, options, callback) {
//see note on the options argument at putAttachment.
var args = parseBaseArgs(db, this, options, callback);
args.docId = docId;
args.attachmentId = attachmentId;
args.rev = rev;
return callHandlers(handlers, args, function () {
return removeAttachment.call(this, args.docId, args.attachmentId, args.rev);
});
};
};
wrapperBuilders.query = function (db, query, handlers) {
return function (fun, options, callback) {
var args = parseBaseArgs(db, this, options, callback);
args.fun = fun;
return callHandlers(handlers, args, function () {
return query.call(this, args.fun, args.options);
});
};
};
wrapperBuilders.viewCleanup = function (db, viewCleanup, handlers) {
return function (options, callback) {
var args = parseBaseArgs(db, this, options, callback);
return callHandlers(handlers, args, makeCallWithOptions(viewCleanup, args));
};
};
wrapperBuilders.createIndex = function (db, createIndex, handlers) {
return function (index, options, callback) {
var args = parseBaseArgs(db, this, options, callback);
args.index = index;
return callHandlers(handlers, args, function () {
return createIndex.call(this, args.index);
});
};
};
wrapperBuilders.deleteIndex = wrapperBuilders.createIndex;
wrapperBuilders.find = function (db, find, handlers) {
return function (request, options, callback) {
var args = parseBaseArgs(db, this, options, callback);
args.request = request;
return callHandlers(handlers, args, function () {
return find.call(this, args.request);
});
};
};
wrapperBuilders.explain = wrapperBuilders.find;
wrapperBuilders.info = function (db, info, handlers) {
return function (options, callback) {
//see note on the options argument at putAttachment.
var args = parseBaseArgs(db, this, options, callback);
return callHandlers(handlers, args, makeCall(info));
};
};
wrapperBuilders.getIndexes = wrapperBuilders.info;
wrapperBuilders.compact = function (db, compact, handlers) {
return function (options, callback) {
var args = parseBaseArgs(db, this, options, callback);
return callHandlers(handlers, args, makeCallWithOptions(compact, args));
};
};
wrapperBuilders.revsDiff = function (db, revsDiff, handlers) {
return function (diff, options, callback) {
//see note on the options argument at putAttachment.
var args = parseBaseArgs(db, this, options, callback);
args.diff = diff;
return callHandlers(handlers, args, function () {
return revsDiff.call(this, args.diff);
});
};
};
//Plug-in wrapperBuilders; only of the plug-ins for which a wrapper
//has been necessary.
wrapperBuilders.list = function (db, orig, handlers) {
return function (path, options, callback) {
var args = parseBaseArgs(db, this, options, callback);
args.path = path;
return callHandlers(handlers, args, function () {
return orig.call(this, args.path, args.options);
});
};
};
wrapperBuilders.rewriteResultRequestObject = wrapperBuilders.list;
wrapperBuilders.show = wrapperBuilders.list;
wrapperBuilders.update = wrapperBuilders.list;
wrapperBuilders.getSecurity = function (db, getSecurity, handlers) {
return function (options, callback) {
var args = parseBaseArgs(db, this, options, callback);
return callHandlers(handlers, args, makeCallWithOptions(getSecurity, args));
};
};
wrapperBuilders.putSecurity = function (db, putSecurity, handlers) {
return function (secObj, options, callback) {
//see note on the options argument at putAttachment.
var args = parseBaseArgs(db, this, options, callback);
args.secObj = secObj;
return callHandlers(handlers, args, function () {
return putSecurity.call(this, args.secObj);
});
};
};
//static
var staticWrapperBuilders = {};
staticWrapperBuilders.new = function (PouchDB, construct, handlers) {
return function (name, options, callback) {
var args;
if (typeof name === "object") {
args = parseBaseArgs(PouchDB, this, name, options);
} else {
args = parseBaseArgs(PouchDB, this, options, callback);
args.options.name = name;
}
return callHandlers(handlers, args, function () {
return construct.call(this, args.options);
});
};
};
staticWrapperBuilders.destroy = function (PouchDB, destroy, handlers) {
return function (name, options, callback) {
var args;
if (typeof name === "object") {
args = parseBaseArgs(PouchDB, this, name, options);
} else {
args = parseBaseArgs(PouchDB, this, options, callback);
args.options.name = name;
}
if (args.options.internal) {
return destroy.apply(PouchDB, arguments);
}
return callHandlers(handlers, args, function () {
var name = args.options.name;
delete args.options.name;
return destroy.call(this, name, args.options);
});
};
};
staticWrapperBuilders.replicate = function (PouchDB, replicate, handlers) {
return function (source, target, options, callback) {
//no callback
var args = parseBaseArgs(PouchDB, this, options, callback);
args.source = source;
args.target = target;
return callHandlers(handlers, args, function () {
return replicate.call(this, args.source, args.target, args.options);
});
};
};
staticWrapperBuilders.allDbs = function (PouchDB, allDbs, handlers) {
return function (options, callback) {
var args = parseBaseArgs(PouchDB, this, options, callback);
return callHandlers(handlers, args, makeCall(allDbs));
};
};
//Wrap .plugin()? .on()? .defaults()? No use case yet, but it's
//possible...
function parseBaseArgs(thisVal1, thisVal2, options, callback) {
if (typeof options === "function") {
callback = options;
options = {};
}
return {
base: thisVal1 || thisVal2,
options: options || {},
callback: callback
};
}
function callHandlers(handlers, args, method) {
var callback = args.callback;
delete args.callback;
//build a chain of handlers: the bottom handler calls the 'real'
//method, the other handlers call other handlers.
method = method.bind(args.base);
for (var i = handlers.length - 1; i >= 0; i -= 1) {
method = handlers[i].bind(null, method, args);
}
//make sure the handler chain is not started, before the corresponding
//pouchdb instance is fully initialized. Otherwise it might result
//in a double calling the augmented methods
var promise;
if (!args.base.taskqueue.isReady) {
promise = new Promise(function (resolve, reject) {
args.base.taskqueue.addTask(function (failed) {
if (failed) {
reject(failed);
} else {
resolve();
}
});
}).then(function () {
return method();
});
} else {
promise = method();
}
//start running the chain.
nodify(promise, callback);
return promise;
}
function makeCall(func) {
return function () {
return func.call(this);
};
}
function makeCallWithOptions(func, args) {
return function () {
return func.call(this, args.options);
};
}
exports.uninstallWrapperMethods = function (db, handlers) {
uninstallWrappers(db, handlers);
};
exports.uninstallStaticWrapperMethods = function (PouchDB, handlers) {
uninstallWrappers(PouchDB, handlers);
};
function uninstallWrappers(base, handlers) {
for (var name in handlers) {
if (!handlers.hasOwnProperty(name)) {
continue;
}
var info = getBaseAndName(base, name);
var wrapper = info.base[info.name];
if (typeof wrapper === "undefined") {
//method doesn't exist, so was never wrapped in the first place.
continue;
}
var idx;
try {
idx = wrapper._handlers.indexOf(handlers[name]);
} catch (err) {
idx = -1;
}
if (idx === -1) {
throw new Error("Wrapper method for '" + name + "' not installed: " + handlers[name]);
}
wrapper._handlers.splice(idx, 1);
if (!wrapper._handlers.length) {
//fall back to the original on the prototype.
delete info.base[info.name];
if (info.base[info.name] !== wrapper._original) {
//nothing or something unexpected was on the prototype. (E.g.
//replicate.to). Reset the original manually.
info.base[info.name] = wrapper._original;
}
}
}
}