-
Notifications
You must be signed in to change notification settings - Fork 2
/
syncModel.js
465 lines (426 loc) · 12.3 KB
/
syncModel.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
/*
SyncModel creates an in-memory representation of documents flowing
through channels. It also mediates any server access so that UI
components are abtracted from network interactions and the API is
mostly synchronous queries from the UI triggered by SyncModel events.
*/
/* jshint -W061 */
/* global syncFun */
/* global compiledFunction */
var events = require('events'),
coax = require("coax"),
util = require("util");
var dbStateSingletons = {};
exports.SyncModelForDatabase = function(dbURL) {
var state = dbStateSingletons[dbURL]
if (!state) {
state = new SyncModel(dbURL)
state.setMaxListeners(100)
dbStateSingletons[dbURL] = state
}
return state
}
exports.allDBs = function(host, cb){
coax([host, "_all_dbs"], cb);
}
exports.createDB = function(host, db, config, cb){
coax.put([host, db, ""], config, cb);
}
exports.deleteDB = function(host, db, cb){
coax.del([host, db, ""], cb);
}
function SyncModel(db) {
// setup on / emit / etc
events.EventEmitter.call(this);
// private state
var previewFun, self=this, client = coax(db),
dbConfig = {}, previewChannels = {}, previewDocs = {};
// public state
this.db = db;
this.client = client;
this.workingSet = 1000
this.pageSize = 10
// pubic methods
this.setSyncFunction = function(funCode) {
var oldCode = previewFun && previewFun.code
if (funCode == oldCode) {
return;
}
previewChannels = {};
previewDocs = {};
totalChanges = 0
previewFun = compileSyncFunction(funCode)
previewFun.code = funCode
loadChangesHistory()
if (this.deployedSyncFunction() == funCode) {
this.emit("syncReset", "deployed")
} else {
this.emit("syncReset", "preview")
}
}
this.getSyncFunction = function() {
return previewFun.code;
}
this.channelNames = function() {
return Object.keys(previewChannels);
}
this.deployedSyncFunction = function(){
return dbConfig.sync || "function(doc){\n channel(doc.channels)\n}";
}
this.deploySyncFunction = function(code, done) {
var newConfig = {}
for (var k in dbConfig) {
if (dbConfig[k]) {
newConfig[k] = dbConfig[k]
}
}
newConfig.sync = code;
client.del([""]/*[""] to force trailing slash*/,function(err){
if (err && err.constructor !== SyntaxError) {
return done(err);
}
client.put([""]/*[""] to force trailing slash*/,newConfig, function(err, ok){
if (err && err.constructor !== SyntaxError) {
return done(err);
}
self.setSyncFunction(code)
done(false, ok)
})
})
}
this.channel = function(name) {
var changes = [], revs ={}, chan = previewChannels[name];
if (!chan) return {name:name, changes:[]};
var docs = chan.docs;
for (var id in docs) revs[docs[id]] = id
var rs = Object.keys(revs).sort(function(a, b){
return parseInt(a, 10) - parseInt(b, 10);
})
for (var i = rs.length - 1; i >= 0; i--) {
var docid = revs[rs[i]]
changes.push({id:docid, seq:parseInt(rs[i], 10), isAccess : chan.access[docid]})
}
var result = {
name : name,
changes : changes
}
var accessIds = Object.keys(chan.access);
if (accessIds.length) {
result.access = chan.access
result.hiddenAccessIds = [];
for (i = accessIds.length - 1; i >= 0; i--) {
if (!docs[accessIds[i]]) {
result.hiddenAccessIds.push(accessIds[i])
}
}
}
return result
}
this.randomAccessDocID = function() {
var chs = this.channelNames()
chs = shuffleArray(chs);
var ch = chs.pop();
while (ch) {
var chInfo = this.channel(ch)
if (chInfo.access) {
var ids = Object.keys(chInfo.access)
return ids[Math.floor(Math.random()*ids.length)]
}
ch = chs.pop()
}
}
this.randomDocID = function(){
var chs = this.channelNames()
var ch = chs[Math.floor(Math.random()*chs.length)]
var chInfo = this.channel(ch);
var rIds = chInfo.changes.map(function(c){return c.id})
return rIds[Math.floor(Math.random()*rIds.length)]
}
this.getDoc = function(id, cb){
client.get(["_raw", id], function(err, raw) {
if (err) {return cb(err);}
var deployed = raw._sync;
delete raw._sync;
raw._rev = deployed.rev;
raw._id = id;
var previewSet = {}
var preview = runSyncFunction(previewSet, id, raw, 0)
cb(err, raw, transformDeployed(id, deployed), transformPreview(id, preview))
});
}
this.saveDoc = function(doc, cb) {
client.put(doc._id, doc, cb)
}
this.allDocs = function(cb) {
client.get("_all_docs", function(err, data) {
var rows = data.rows.map(function(r){
return {id : r.id, access : previewDocs[r.id]}
})
cb(err, rows)
})
}
this.allUsers = function(cb) {
client.get(["_view", "principals", {state:false}], cb);
}
this.userInfo = function(id, cb) {
client.get(["_user", id], cb)
}
// private implementation
function transformDeployed(id, deployed){
var access = {};
for (var user in deployed.access) {
var chans = Object.keys(deployed.access[user])
for (var i = chans.length - 1; i >= 0; i--) {
var ch = chans[i]
access[ch] = access[ch] || []
access[ch].push(user)
access[ch] = access[ch].sort()
}
}
return {
access : access,
channels : deployed.channels ? Object.keys(deployed.channels) : []
}
}
function transformPreview(id, preview) {
// console.log("preview", preview)
var channelSet = {}
preview.access.forEach(function(acc) {
acc.channels.forEach(function(ch) {
channelSet[ch] = channelSet[ch] || [];
channelSet[ch] =
mergeUsers(channelSet[ch], acc.users);
})
})
// console.log("preview.access", channelSet)
return {
access : channelSet,
channels : preview.channels,
reject : preview.reject
};
}
function runSyncFunction(channelSet, id, doc, seq) {
// console.log('previewFun', doc)
doc._id = id
var sync = previewFun(doc, false, null)
// console.log('previewFun', doc._id, doc, sync)
if (sync.reject) {
console.error("update rejected by sync function", doc, sync)
return;
}
var changed = {};
previewDocs[id] = false;
sync.channels.forEach(function(ch) {
channelSet[ch] = channelSet[ch] || {docs : {}, access:{}};
channelSet[ch].docs[id] = seq;
changed[ch]=true;
})
sync.access.forEach(function(acc) {
previewDocs[id] = true;
acc.channels.forEach(function(ch){
changed[ch]=true;
channelSet[ch] = channelSet[ch] || {docs : {}, access:{}};
channelSet[ch].access[id] =
mergeUsers(channelSet[ch].access[id], acc.users);
})
})
sync.changed = changed;
return sync;
}
function mergeUsers(existing, more) {
var keys = {};
existing = existing || [];
for (var i = existing.length - 1; i >= 0; i--) {
keys[existing[i]] = true;
}
for (i = more.length - 1; i >= 0; i--) {
keys[more[i]] = true;
}
return Object.keys(keys).sort()
}
// function disconnect() {
// if (changesRequest) {
// changesRequest.abort();
// }
// }
this.shutdown = function() {
if (changesRequest) {
changesRequest.destroy();
}
}
var changesRequest;
var oldBefore;
function moreHistory(before) {
// console.log("totalChanges", totalChanges, self.workingSet)
if (oldBefore !== before && totalChanges < self.workingSet) {
oldBefore = before;
client.get(["_changes", {since : before, include_docs : true,
limit : self.pageSize}], function(err, data) {
// console.log("history", data)
data.results.forEach(onChange)
self.emit("batch")
moreHistory(data.last_seq)
})
}
}
function loadChangesHistory(){
// get first page
// console.log("loadChangesHistory")
client.get(["_changes", {limit : self.pageSize, include_docs : true}], function(err, data) {
// console.log("history once", data)
data.results.forEach(onChange)
self.emit("batch")
moreHistory(data.last_seq)
changesRequest = client.changes({since : data.last_seq, include_docs : true}, function(err, data){
// console.log("change", err, data);
if (!err)
onChange(data)
})
})
}
self.once("batch", function() {
self.connected = true;
self.emit("connected")
})
self.on("newListener", function(name, fun){
if (name == "connected" && self.connected) {
fun()
}
})
var totalChanges = 0
function onChangeWDoc(ch, doc) {
// console.log("onChangeWDoc", ch, doc)
var seq;
if ("number" !== typeof ch.seq) {
seq = parseInt(ch.seq.split(":")[1], 10)
} else {
seq = ch.seq
}
ch.doc = doc
totalChanges++;
var sync = runSyncFunction(previewChannels, ch.id, ch.doc, seq)
self.emit("change", ch)
Object.keys(sync.changed).forEach(function(channel) {
self.emit("ch:"+channel);
})
}
function onChange(ch) {
if (ch.doc) {
onChangeWDoc(ch, ch.doc)
} else if (ch.id == "_user/") {
// ignore, this is an access control change sequence not a data sequence
} else {
console.log("doc async", ch.id)
// return;
client.get(ch.id, function(err, doc) {
if (err) {
console.error("doc missing", ch.id)
} else {
onChangeWDoc(ch, doc)
}
})
}
}
client.get("_config", function(err, config) {
if (err) throw(err);
dbConfig = config;
self.setSyncFunction(config.sync || "function(doc){\n channel(doc.channels)\n}");
})
}
util.inherits(SyncModel, events.EventEmitter);
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
var syncWrapper = function(newDoc, oldDoc, realUserCtx) {
"syncCodeStringHere";
var _ = require("underscore"),
log = console.log.bind(console);
function makeArray(maybeArray) {
if (Array.isArray(maybeArray)) {
return maybeArray;
} else {
return [maybeArray];
}
}
function inArray(string, array) {
return array.indexOf(string) != -1;
}
function anyInArray(any, array) {
for (var i = 0; i < any.length; ++i) {
if (inArray(any[i], array))
return true;
}
return false;
}
// Proxy userCtx that allows queries but not direct access to user/roles:
var shouldValidate = (realUserCtx !== null && realUserCtx.name !== null);
function requireUser(names) {
if (!shouldValidate) return;
names = makeArray(names);
if (!inArray(realUserCtx.name, names))
throw({forbidden: "wrong user"});
}
function requireRole(roles) {
if (!shouldValidate) return;
roles = makeArray(roles);
if (!anyInArray(realUserCtx.roles, roles))
throw({forbidden: "missing role"});
}
function requireAccess(channels) {
if (!shouldValidate) return;
channels = makeArray(channels);
if (!anyInArray(realUserCtx.channels, channels))
throw({forbidden: "missing channel access"});
}
var results = {
channels : [],
access : [],
roles : [],
reject : false
};
function channel(){
var args = Array.prototype.slice.apply(arguments);
results.channels = Array.prototype.concat.apply(results.channels, args);
}
function access(users, channels){
results.access.push({
users : makeArray(users),
channels : makeArray(channels)
})
}
function role(users, channels){
results.roles.push({
users : makeArray(users),
channels : makeArray(channels)
})
}
function reject(code, message) {
results.reject = [code, message];
}
try {
// console.log("syncFun", newDoc)
syncFun(newDoc, oldDoc);
} catch(x) {
if (x.forbidden)
reject(403, x.forbidden);
else if (x.unauthorized)
reject(401, x.unauthorized);
else
throw(x);
}
return results;
}.toString();
function compileSyncFunction(syncCode) {
var codeString = "var syncFun = ("+ syncCode+")",
wrappedCode = syncWrapper.replace('"syncCodeStringHere"', function() { return codeString; }),
evalString = "compiledFunction = ("+ wrappedCode+")",
compiledFunction;
eval(evalString);
return compiledFunction;
}