-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
495 lines (439 loc) · 17.5 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
var deepCopy = require('racer-util/object').deepCopy;
exports = module.exports = plugin;
function plugin (racer, options) {
var Store = racer.Store;
function setupPreValidate (shareClient) {
shareClient._validatorsCreate = [];
shareClient._validatorsDel = [];
shareClient._validatorsUpdate = [];
shareClient.use('submit', function (shareRequest, next) {
var useragent = this;
var opData = shareRequest.opData;
opData.collection = shareRequest.collection;
opData.docName = shareRequest.docName;
opData.connectSession = shareRequest.agent.connectSession;
opData.origin = (shareRequest.agent.stream.isServer) ?
'server' :
'browser';
next();
});
/**
* @param {Object} opData
* @param {Array} opData.op
* @param {Number} opData.v
* @param {String} opData.src
* @param {Number} opData.seq
* @param {Object} data is the current snapshot
* @return {Error|undefined}
*/
shareClient.preValidate = function (opData, data) {
var collection = opData.collection;
var docName = opData.docName;
var connectSession = opData.connectSession;
var origin = opData.origin;
var validators;
if (opData.create) {
validators = shareClient._validatorsCreate;
} else if (opData.del) {
validators = shareClient._validatorsDel;
} else {
validators = shareClient._validatorsUpdate;
}
var err;
for (var i = 0, l = validators.length; i < l; i++) {
err = validators[i](collection, docName, opData, data.data, connectSession);
if (err) break;
}
if (err) return err;
}
}
racer.on('store', setupStore);
function setupStore (store) {
if (store._didInitAccessControl) return;
/**
* Assign the connect session to ShareJS's useragent (there is 1 useragent per
* browser tab or window that is connected to our server via browserchannel).
* We'll probably soon move this into racer core, so developers won't need to
* remember to have this code here.
*/
store._didInitAccessControl = true;
store.shareClient.use('connect', function (shareRequest, next) {
var req = shareRequest.req;
if (req && req.session) shareRequest.agent.connectSession = req.session;
next();
});
setupPreValidate(store.shareClient);
}
Store.prototype.allow = function (type, pattern, callback) {
setupStore(this);
this['_allow_' + type](pattern, callback);
};
/**
* A convenience method for declaring access control on queries. This may be
* moved into racer core. We'll want to experiment to see if this particular
* interface is sufficient, before committing this convenience method to
* core.
*/
Store.prototype._allow_query = function (collectionName, callback) {
this.shareClient.use('query', function (shareRequest, next) {
if ((collectionName !== '**') && (collectionName !== shareRequest.collection)) {
return next();
}
var session = shareRequest.agent.connectSession;
shareRequest.query = deepCopy(shareRequest.query);
var origin = (shareRequest.agent.stream.isServer) ?
'server' :
'browser';
if (collectionName === '**') {
callback(shareRequest.collection, shareRequest.query, session, origin, next);
} else {
callback(shareRequest.query, session, origin, next);
}
});
};
/**
* A convenience method for declaring access control on reading individual
* documents. This may be moved into racer core. We'll want to experiment to
* see if this particular interface is sufficient, before committing this
* convenience method to core.
* @param {String} collectionName
* @param {Function} callback(docId, doc, connectSession)
*/
Store.prototype._allow_doc = function (collectionName, callback) {
this.shareClient.filter( function (collection, docId, snapshot, next) {
if (! docId) docId = snapshot.docName;
if ((collectionName !== '**') && (collectionName !== collection)) {
return next();
}
var useragent = this;
var doc = snapshot.data;
var origin = (useragent.stream.isServer) ?
'server' :
'browser';
if (collectionName === '**') {
return callback(collection, docId, doc, useragent.connectSession, origin, next);
} else {
return callback(docId, doc, useragent.connectSession, origin, next);
}
});
};
Store.prototype._allow_change = function (pattern, validate) {
this.shareClient._validatorsUpdate.push(
function (collection, docName, opData, snapshotData, connectSession) {
if (! collectionMatchesPattern(collection, pattern)) return;
var racerMethod = opToRacerMethod(opData.op);
if (-1 === ['change', 'stringRemove', 'stringInsert', 'increment'].indexOf(racerMethod)) {
return;
}
var relativeSegments = segmentsFor(racerMethod, opData);
var isRelevantPath = relevantPath(pattern, relativeSegments);
if (! isRelevantPath) return;
var changeTo = calcChangeTo(racerMethod, opData, relativeSegments, snapshotData);
if (isRelevantPath.length > 1) {
var matches = isRelevantPath;
return validate.apply(null, [docName].concat(matches.slice(1)).concat(changeTo, snapshotData, connectSession));
} else {
return validate(docName, changeTo, snapshotData, connectSession);
}
}
);
var indexOfDot = pattern.indexOf('.');
if ((indexOfDot !== -1) && (indexOfDot + 2 === pattern.length) && pattern.charAt(pattern.length-1) === '*') {
this.shareClient._validatorsCreate.push(
function (collection, docName, opData, snapshotData, connectSession) {
if (! collectionMatchesPattern(collection, pattern)) return;
if (collection !== opData.collection) return;
var newDoc = opData.create.data;
return validate(docName, newDoc, connectSession);
}
);
}
};
/**
* A convenience method for declaring access control on creates, based on the
* value of the document before the operation is applied to it.
* @param {String} collectionName
* @param {Function} callback(docId, opData, doc, connectSession, next)
* where next(docId, opData, docBeforeOp, session, next)
*/
Store.prototype._allow_create = function (pattern, validate) {
this.shareClient._validatorsCreate.push(
function (collection, docName, opData, snapshotData, connectSession) {
if (! collectionMatchesPattern(collection, pattern)) return;
var newDoc = opData.create.data;
return validate(docName, newDoc, connectSession);
}
);
};
/**
* A convenience method for declaring access control on document and
* attribute deletes, based on the value of the document before the operation
* is applied to it.
* @param {String} collectionName
* @param {Function} callback(docId, opData, doc, connectSession, next)
* where next(docId, opData, docBeforeOp, session, next)
*/
Store.prototype._allow_del = function (pattern, validate) {
// If the pattern is just the collection name, then access control is being
// set up for document deletion.
if (pattern.indexOf('.') === -1 && pattern.indexOf('*') === -1) {
this.shareClient._validatorsDel.push(
function (collection, docName, opData, snapshotData, connectSession) {
if (! collectionMatchesPattern(collection, pattern)) return;
var docToRemove = snapshotData;
return validate(docName, docToRemove, connectSession);
}
);
} else {
this.shareClient._validatorsUpdate.push(
function (collection, docName, opData, snapshotData, connectSession) {
if (! collectionMatchesPattern(collection, pattern)) return;
var item = opData.op[0];
// Ignore replaces (i.e., op.oi and op.od are both present);
if (item.oi) return;
var valueToDelete = item.od;
if (valueToDelete === void 0) return;
return validate(docName, valueToDelete, snapshotData, connectSession);
}
);
}
};
Store.prototype._allow_remove = function (pattern, validate) {
this.shareClient._validatorsUpdate.push(
function (collection, docName, opData, snapshotData, connectSession) {
if (! collectionMatchesPattern(collection, pattern)) return;
var racerMethod = opToRacerMethod(opData.op);
if (racerMethod !== 'remove') return;
var relativeSegments = segmentsFor(racerMethod, opData);
var isRelevantPath = relevantPath(pattern, relativeSegments);
if (! isRelevantPath) return;
var changeTo = calcChangeTo(racerMethod, opData, relativeSegments, snapshotData);
var segments = opData.op[0].p;
var index = segments[segments.length-1];
var howMany = 1;
return validate(docName, index, howMany, snapshotData, connectSession);
}
);
};
Store.prototype._allow_insert = function (pattern, validate) {
this.shareClient._validatorsUpdate.push(
function (collection, docName, opData, snapshotData, connectSession) {
if (! collectionMatchesPattern(collection, pattern)) return;
var racerMethod = opToRacerMethod(opData.op);
if (racerMethod !== 'insert') return;
var relativeSegments = segmentsFor(racerMethod, opData);
var isRelevantPath = relevantPath(pattern, relativeSegments);
if (! isRelevantPath) return;
var segments = opData.op[0].p;
var index = segments[segments.length-1];
var toInsert = [opData.op[0].li];
return validate(docName, index, toInsert, snapshotData, connectSession);
}
);
};
Store.prototype._allow_move = function (pattern, validate) {
this.shareClient._validatorsUpdate.push(
function (collection, docName, opData, snapshotData, connectSession) {
if (! collectionMatchesPattern(collection, pattern)) return;
var racerMethod = opToRacerMethod(opData.op);
if (racerMethod !== 'move') return;
var relativeSegments = segmentsFor(racerMethod, opData);
var isRelevantPath = relevantPath(pattern, relativeSegments);
if (! isRelevantPath) return;
var segments = opData.op[0].p;
var from = segments[segments.length-1];
var to = opData.op[0].lm - 1;
var howMany = 1;
return validate(docName, from, to, howMany, snapshotData, connectSession);
}
);
};
Store.prototype._allow_all = function (pattern, validate) {
this.shareClient._validatorsUpdate.push(
function (collection, docName, opData, snapshotData, connectSession) {
if (! collectionMatchesPattern(collection, pattern)) return;
if (pattern === '**') {
var racerMethod = opToRacerMethod(opData.op);
var relativeSegments = segmentsFor(racerMethod, opData);
return validate(docName, relativeSegments.join('.'), opData, snapshotData, connectSession);
} else if (pattern === collection + '**') {
var racerMethod = opToRacerMethod(opData.op);
var relativeSegments = segmentsFor(racerMethod, opData);
return validate(docName, relativeSegments.join('.'), opData, snapshotData, connectSession);
} else {
var racerMethod = opToRacerMethod(opData.op);
var relativeSegments = segmentsFor(racerMethod, opData);
var isRelevantPath = relevantPath(pattern, relativeSegments);
if (! isRelevantPath) return;
return validate(docName, relativeSegments.join('.'), opData, snapshotData, connectSession);
}
}
);
this.shareClient._validatorsCreate.push(
function (collection, docName, opData, snapshotData, connectSession) {
if (! collectionMatchesPattern(collection, pattern)) return;
if ((pattern === '**') || (pattern === collection + '**')) {
var relPath = '';
return validate(docName, relPath, opData, snapshotData, connectSession);
}
}
);
this.shareClient._validatorsDel.push(
function (collection, docName, opData, snapshotData, connectSession) {
if (! collectionMatchesPattern(collection, pattern)) return;
if ((pattern === '**') || (pattern === collection + '**')) {
if (opData.op) {
var racerMethod = opToRacerMethod(opData.op);
var relativeSegments = segmentsFor(racerMethod, opData);
var relPath = relativeSegments.join('.');
return validate(docName, relPath, opData, snapshotData, connectSession);
} else { // else deleting entire document
var relPath = '';
return validate(docName, relPath, opData, snapshotData, connectSession);
}
} else if (! opData.del) {
var racerMethod = opToRacerMethod(opData.op);
var relativeSegments = segmentsFor(racerMethod, opData);
var isRelevantPath = relevantPath(pattern, relativeSegments);
if (! isRelevantPath) return;
if (isRelevantPath.length > 1) {
return validate.apply(null, [docName].concat(isRelevantPath.slice(1)).concat(relativeSegments.join('.'), opData, snapshotData, connectSession));
} else {
return validate(docName, relativeSegments.join('.'), opData, snapshotData, connectSession);
}
}
}
);
};
}
/**
* Express middleware for exposing the user to the model (accessible by the
* server and browser only to the user, via model.get('_session.user').
*/
exports.rememberUser = function (req, res, next) {
var model = req.getModel();
var userId = req.session.userId;
if (! userId) return next();
var $me = model.at('users.' + userId);
$me.fetch( function (err) {
model.ref('_session.user', $me.path());
next();
});
};
function collectionMatchesPattern (collection, pattern) {
if (pattern === '**') return true;
return collection === pattern.slice(0, collection.length);
}
function opToRacerMethod (op) {
var item = op[0];
// object replace, object insert, or object delete
if ((item.oi !== void 0) || (item.od !== void 0)) {
return 'change';
// list replace
} else if (item.li !== undefined && item.ld !== undefined) {
return 'change';
// List insert
} else if (item.li !== undefined) {
return 'insert';
// List remove
} else if (item.ld !== undefined) {
return 'remove';
// List move
} else if (item.lm !== undefined) {
return 'move';
// String insert
} else if (item.si !== undefined) {
return 'stringInsert';
// String remove
} else if (item.sd !== undefined) {
return 'stringRemove';
// Increment
} else if (item.na !== undefined) {
return 'increment';
}
}
function segmentsFor (racerEvent, opData) {
var item = opData.op[0];
// segments relative to doc root
var relativeSegments = item.p;
switch (racerEvent) {
case 'change':
case 'increment':
return relativeSegments;
case 'insert':
case 'remove':
case 'move':
case 'stringInsert':
case 'stringRemove':
return relativeSegments.slice(0, -1);
}
}
function relevantPath (pattern, relativeSegments) {
if (pattern === '**') return [null].concat(relativeSegments);
// Check for patterns "collection**" or e.g., "collection.*.x.y.z**"
if (pattern.slice(pattern.length-2, pattern.length) === '**') {
} else {
// Handle e.g., pattern = "collection.*.x.y.z"
var patternSegments = pattern.split('.');
if (patternSegments[1] !== '*') {
console.warn('Unexpected pattern', pattern);
}
var patternRelativeSegments = patternSegments.slice(2);
// Pass to next middleware if pattern does not match the mutated path
if (relativeSegments.length !== patternRelativeSegments.length) {
return false;
}
if (-1 === patternRelativeSegments.indexOf('*')) {
return relativeSegments.join('.') === patternRelativeSegments.join('.');
}
var regExp = patternToRegExp(patternRelativeSegments.join('.'));
var matches = regExp.exec(relativeSegments.join('.'));
if (matches) {
for (var i = 1, l = matches.length; i < l; i++) {
if (/^\d+$/.test(matches[i])) {
matches[i] = parseInt(matches[i], 10);
}
}
}
return matches;
}
}
function calcChangeTo (racerMethod, opData, relativeSegments, snapshotData) {
var item = opData.op[0];
if (racerMethod === 'change') {
return item.oi || // object replace, insert, or delete
item.li; // list replace
} else if (racerMethod === 'stringInsert') {
var index = relativeSegments[relativeSegments.length-1];
var text = item.si;
var currText = lookupSegments(relativeSegments, snapshotData);
return currText.slice(0, index) +
text +
currText.slice(index);
} else if (racerMethod === 'stringRemove') {
var index = relativeSegments[relativeSegments.length-1];
var text = item.sd;
var currText = lookupSegments(relativeSegments, snapshotData);
return currText.slice(0, index) + currText.slice(index + text.length);
} else if (racerMethod === 'increment') {
var incrBy = item.na;
return lookupSegments(relativeSegments, snapshotData) + incrBy;
}
}
function patternToRegExp (pattern) {
var regExpString = pattern
.replace(/\./g, "\\.")
.replace(/\*/g, "([^.]+)");
return new RegExp(regExpString);
}
function lookupSegments (segments, object) {
var curr = object;
for (var i = 0, l = segments.length; i < l; i++) {
var segment = segments[i];
if (/^\d+$/.test(segment)) {
segment = parseInt(segment, 10);
}
curr = curr[segment];
}
return curr;
}