forked from colinskow/pouch-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
249 lines (222 loc) · 7.15 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
var PouchDB = require('pouchdb');
var BPromise = require('bluebird');
var memdown = require('memdown');
var listener = require('./listener');
var shutdown = false;
var pouchMirror = module.exports = function(dbname, remoteURL, options) {
var self = this;
this.remoteDB = new PouchDB(remoteURL);
this.localDB = new PouchDB(dbname, {'db': memdown});
// Until the memory database is in sync, we will read from the remote DB
this.readDB = this.remoteDB;
this.DBSynced = false;
// Set default options
if(!options) {
options = {};
}
options.initialTimeout = options.timeout || 1000;
options.backoff = options.backoff || 2;
options.maxTimeout = options.maxTimeout || 600000; // ten minutes
options.noRetry = options.noRetry || false;
var timeout = options.initialTimeout;
// Start buffering changes as they come in
self.listener = new listener(self.localDB);
// Continuous replication with exponential back-off retry
function startLiveReplication() {
self.replicator = self.localDB.replicate.from(remoteURL, {live: true})
.on('change', function () {
timeout = options.initialTimeout; // reset
})
.on('uptodate', function() {
if(!self.DBSynced) {
self.DBSynced = true;
self.readDB = self.localDB;
console.log('Initial sync of ' + dbname + ' complete.');
}
})
.on('error', function (err) {
self.DBSynced = false;
self.readDB = self.remoteDB;
if(shutdown || options.noRetry) return;
console.warn('Error: Live replication failed. Retrying in ' + timeout / 1000 + 'seconds.');
console.warn(err);
setTimeout(function () {
timeout *= options.backoff;
if(timeout > options.maxTimeout) {
timeout = options.maxTimeout;
}
startLiveReplication();
}, timeout);
});
}
this.remoteDB
.then(function() {
startLiveReplication();
});
};
pouchMirror.prototype.get = function() {
var args = processArgs(arguments);
var promise = this.readDB.get.apply(this.readDB, args.args);
if(args.cb) promise.nodeify(args.cb);
return promise;
};
pouchMirror.prototype.allDocs = function() {
var args = processArgs(arguments);
var promise = this.readDB.allDocs.apply(this.readDB, args.args);
if(args.cb) promise.nodeify(args.cb);
return promise;
};
pouchMirror.prototype.put = function() {
var args = processArgs(arguments);
var self = this;
var output;
var promise = self.remoteDB.put.apply(self.remoteDB, args.args)
.then(function(response) {
output = response;
return self.listener.waitForChange(response.rev);
})
.then(function() {
return BPromise.resolve(output);
});
if(args.cb) promise.nodeify(args.cb);
return promise;
};
pouchMirror.prototype.post = function() {
var args = processArgs(arguments);
var self = this;
var output;
var promise = self.remoteDB.post.apply(self.remoteDB, args.args)
.then(function(response) {
output = response;
return self.listener.waitForChange(response.rev);
})
.then(function() {
return BPromise.resolve(output);
});
if(args.cb) promise.nodeify(args.cb);
return promise;
};
pouchMirror.prototype.bulkDocs = function() {
var args = processArgs(arguments);
var self = this;
var output;
var promise = self.remoteDB.bulkDocs.apply(self.remoteDB, args.args)
.then(function(results) {
output = results;
var promises = [];
results.forEach(function(row){
if(row.ok === true) {
promises.push(self.listener.waitForChange(row.rev));
}
});
return BPromise.all(promises);
})
.then(function(){
return BPromise.resolve(output);
});
if(args.cb) promise.nodeify(args.cb);
return promise;
};
pouchMirror.prototype.remove = function() {
var args = processArgs(arguments);
var self = this;
var output;
var promise = self.remoteDB.remove.apply(self.remoteDB, args.args)
.then(function(result) {
output = result;
return self.listener.waitForChange(result.rev);
})
.then(function() {
return BPromise.resolve(output);
});
if(args.cb) promise.nodeify(args.cb);
return promise;
};
pouchMirror.prototype.changes = function() {
var args = processArgs(arguments);
var promise = this.localDB.changes.apply(this.localDB, args.args);
if(args.cb) promise.nodeify(args.cb);
return promise;
};
pouchMirror.prototype.replicate = function() {
var args = processArgs(arguments);
var promise = this.localDB.replicate.apply(this.localDB, args.args);
if(args.cb) promise.nodeify(args.cb);
return promise;
};
pouchMirror.prototype.sync = function() {
return this.localDB.sync.apply(this.localDB, arguments);
};
pouchMirror.prototype.putAttachment = function() {
var args = processArgs(arguments);
var self = this;
var output;
var promise = self.remoteDB.putAttachment.apply(self.remoteDB, args.args)
.then(function(response) {
output = response;
return self.listener.waitForChange(response.rev);
})
.then(function() {
return BPromise.resolve(output);
});
if(args.cb) promise.nodeify(args.cb);
return promise;
};
pouchMirror.prototype.getAttachment = function() {
var args = processArgs(arguments);
var promise = this.readDB.getAttachment.apply(this.readDB, args.args);
if(args.cb) promise.nodeify(args.cb);
return promise;
};
pouchMirror.prototype.removeAttachment = function() {
var args = processArgs(arguments);
var self = this;
var output;
var promise = self.remoteDB.removeAttachment.apply(self.remoteDB, args.args)
.then(function(response) {
output = response;
return self.listener.waitForChange(response.rev);
})
.then(function() {
return BPromise.resolve(output);
});
if(args.cb) promise.nodeify(args.cb);
return promise;
};
pouchMirror.prototype.query = function() {
var args = processArgs(arguments);
var promise = this.readDB.query.apply(this.readDB, args.args);
if(args.cb) promise.nodeify(args.cb);
return promise;
};
pouchMirror.prototype.info = function() {
var args = processArgs(arguments);
var self = this;
var theinfo = {};
var promise = new BPromise(function(resolve, reject) {
BPromise.all([self.remoteDB.info(), self.localDB.info()])
.then(function(results) {
theinfo.remote = results[0];
theinfo.local = results[1];
return resolve(theinfo);
}, function(err) {
return reject(err);
});
});
if(args.cb) promise.nodeify(args.cb);
return promise;
};
pouchMirror.prototype.cancelSync = function() {
shutdown = true;
this.replicator.cancel();
};
// Creates an object that separates the callback from the rest of the arguments
var processArgs = function(args) {
args = Array.prototype.slice.call(args);
if(typeof args[args.length-1] === 'function') {
var callback = args.pop();
return {args: args, cb: callback};
} else {
return {args: args, cb: null};
}
};