-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathchannel.js
More file actions
724 lines (634 loc) · 20.5 KB
/
channel.js
File metadata and controls
724 lines (634 loc) · 20.5 KB
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/*eslint no-shadow: 0*/
import { format, inherits } from 'util'
import { EventEmitter } from 'events'
import { parseMessage, parseVersionMessage, change as change_util } from './util'
import { v4 as uuid } from 'uuid'
import NetworkQueue from './queues/network-queue';
import LocalQueue from './queues/local-queue';
const UNKNOWN_CV = '?';
const CODE_INVALID_VERSION = 405;
const CODE_EMPTY_RESPONSE = 412;
const CODE_INVALID_DIFF = 440;
const operation = {
MODIFY: 'M',
REMOVE: '-'
};
// internal methods used as instance methods on a Channel instance
const internal = {};
/**
* Updates the currently known synced `cv`.
*
* @param {String} cv - the change version synced
* @returns {Promise<String>} the saved `cv`
*/
internal.updateChangeVersion = function( cv ) {
return this.store.setChangeVersion( cv ).then( () => {
// A unit test currently relies on this event, otherwise we can remove it
this.emit( 'change-version', cv );
return cv;
} );
};
/**
* Called when receive a change from the network. Attempt to apply the change
* to the ghost object and notify.
*
* @param {String} id - id of the object changed
* @param {Object} change - the change to apply to the object
*/
internal.changeObject = function( id, change ) {
// pull out the object from the store and apply the change delta
var applyChange = internal.performChange.bind( this, change );
this.networkQueue.queueFor( id ).add( function( done ) {
return applyChange().then( done, done );
} );
};
/**
* Creates a change operation for the object of `id` that changes
* from the date stored in the `ghost` into the data of `object`.
*
* Queues the change for syncing.
*
* @param {String} id - object id
* @param {Object} object - object literal of the data that the change should produce
* @param {Object} ghost - the ghost version used to produce the change object
*/
internal.buildModifyChange = function( id, object, ghost ) {
var payload = change_util.buildChange( change_util.type.MODIFY, id, object, ghost ),
empty = true,
key;
for ( key in payload.v ) {
if ( key ) {
empty = false;
break;
}
}
if ( empty ) {
this.emit( 'unmodified', id, object, ghost );
return;
}
// if the change v is an empty object, do not send, notify?
this.localQueue.queue( payload );
};
/**
* Creates a change object that deletes an object from a bucket.
*
* Queues the change for syncing.
*
* @param {String} id - object to remove
* @param {Object} ghost - current ghost object for the given id
*/
internal.buildRemoveChange = function( id, ghost ) {
var payload = change_util.buildChange( change_util.type.REMOVE, id, {}, ghost );
this.localQueue.queue( payload );
};
internal.diffAndSend = function( id, object ) {
var modify = internal.buildModifyChange.bind( this, id, object );
return this.store.get( id ).then( modify );
};
internal.removeAndSend = function( id ) {
var remove = internal.buildRemoveChange.bind( this, id );
return this.store.get( id ).then( remove );
};
// We've receive a full object from the network. Update the local instance and
// notify of the new object version
internal.updateObjectVersion = function( id, version, data, original, patch, acknowledged ) {
var notify,
changes,
change,
patch,
localModifications,
remoteModifications,
transformed,
update;
// If it's not an ack, it's a change initiated on a different client
// we need to provide a way for the current client to respond to
// a potential conflict if it has modifications that have not been synced
if ( !acknowledged ) {
changes = this.localQueue.dequeueChangesFor( id );
localModifications = change_util.compressChanges( changes, original );
remoteModifications = patch;
transformed = change_util.transform( localModifications, remoteModifications, original );
update = data;
// apply the transformed patch and emit the update
if ( transformed ) {
patch = transformed;
update = change_util.apply( transformed, data );
// queue up the new change
change = change_util.modify( id, version, patch );
this.localQueue.queue( change );
}
notify = this.emit.bind( this, 'update', id, update, original, patch, this.isIndexing );
} else {
notify = internal.updateAcknowledged.bind( this, acknowledged );
}
return this.store.put( id, version, data ).then( notify );
};
internal.removeObject = function( id, acknowledged ) {
var notify;
if ( !acknowledged ) {
notify = this.emit.bind( this, 'remove', id );
} else {
notify = internal.updateAcknowledged.bind( this, acknowledged );
}
return this.store.remove( id ).then( notify );
};
internal.updateAcknowledged = function( change ) {
var id = change.id;
if ( this.localQueue.sent[id] === change ) {
this.localQueue.acknowledge( change );
this.emit( 'acknowledge', id, change );
}
};
internal.performChange = function( change ) {
var success = internal.applyChange.bind( this, change );
return this.store.get( change.id ).then( success );
};
internal.findAcknowledgedChange = function( change ) {
var possibleChange = this.localQueue.sent[change.id];
if ( possibleChange ) {
if ( ( change.ccids || [] ).indexOf( possibleChange.ccid ) > -1 ) {
return possibleChange;
}
}
};
internal.requestObjectVersion = function( id, version ) {
return new Promise( resolve => {
this.once( `version.${ id }.${ version }`, data => {
resolve( data );
} );
this.send( `e:${ id }.${ version }` );
} );
};
internal.applyChange = function( change, ghost ) {
const acknowledged = internal.findAcknowledgedChange.bind( this )( change ),
updateChangeVersion = internal.updateChangeVersion.bind( this, change.cv );
let error,
original,
patch,
modified;
// attempt to apply the change
// TODO: Handle errors as specified in
// 0:c:[{"ccids": ["0435edf4-3f07-4cc6-bf86-f68e6db8779c"], "id": "9e9a9616-8174-42
// { ccids: [ '0435edf4-3f07-4cc6-bf86-f68e6db8779c' ],
// id: '9e9a9616-8174-425a-a1b0-9ed5410f1edc',
// clientid: 'node-b9776e96-c068-42ae-893a-03f50833bddb',
// error: 400 }
if ( change.error ) {
error = new Error( `${change.error} - Could not apply change to: ${ghost.key}` );
error.code = change.error;
error.change = change;
error.ghost = ghost;
internal.handleChangeError.call( this, error, change, acknowledged );
return;
}
if ( change.o === operation.MODIFY ) {
if ( ghost && ( ghost.version !== change.sv ) ) {
internal.requestObjectVersion.call( this, change.id, change.sv ).then( data => {
internal.applyChange.call( this, change, { version: change.sv, data } )
} );
return;
}
original = ghost.data;
patch = change.v;
modified = change_util.apply( patch, original );
return internal.updateObjectVersion.call( this, change.id, change.ev, modified, original, patch, acknowledged )
.then( updateChangeVersion );
} else if ( change.o === operation.REMOVE ) {
return internal.removeObject.bind( this )( change.id, acknowledged ).then( updateChangeVersion );
}
}
internal.handleChangeError = function( err, change, acknowledged ) {
switch ( err.code ) {
case CODE_INVALID_VERSION:
case CODE_INVALID_DIFF: // Invalid version or diff, send full object back to server
if ( ! change.hasSentFullObject ) {
this.store.get( change.id ).then( object => {
change.d = object;
change.hasSentFullObject = true;
this.localQueue.queue( change );
} );
} else {
this.localQueue.dequeueChangesFor( change.id );
}
break;
case CODE_EMPTY_RESPONSE: // Change causes no change, just acknowledge it
internal.updateAcknowledged.call( this, acknowledged );
break;
default:
this.emit( 'error', err, change );
}
}
internal.indexingComplete = function() {
// Indexing has finished
this.setIsIndexing( false );
internal.updateChangeVersion.call( this, this.index_cv )
.then( () => {
this.localQueue.start();
} );
this.emit( 'index', this.index_cv );
this.index_last_id = null;
this.index_cv = null;
this.emit( 'ready' )
}
/**
* Maintains syncing state for a Simperium bucket.
*
* A bucket uses a channel to listen for updates that come from simperium while
* sending updates that are made on the client.
*
* The channel can handle incoming simperium commands via `handleMessage`. These
* messages are stripped of their channel number that separates bucket operations.
* The `Client` maintains which commands should be routed to which channel.
*
* The channel is responsible for creating all change operations and downloading
* bucket data.
*
* @param {String} appid - Simperium app id, used for authenticating
* @param {String} access_token - Simperium user access token
* @param {GhostStore} store - data storage for ghost objects
* @param {String} name - the name of the bucket on Simperium.com
*/
export default function Channel( appid, access_token, store, name ) {
// Uses an event emitter to handle different Simperium commands
const message = this.message = new EventEmitter();
this.name = name;
this.isIndexing = false;
this.appid = appid;
this.store = store;
this.access_token = access_token;
this.session_id = 'node-' + uuid();
// These are the simperium bucket commands the channel knows how to handle
message.on( 'auth', this.onAuth.bind( this ) );
message.on( 'i', this.onIndex.bind( this ) );
message.on( 'c', this.onChanges.bind( this ) );
message.on( 'e', this.onVersion.bind( this ) );
message.on( 'cv', this.onChangeVersion.bind( this ) );
message.on( 'o', function() {} );
// Maintain a queue of operations that come from simperium commands
// so that the can be applied to the ghost data.
this.networkQueue = new NetworkQueue();
// Maintain a queue of operations that originate from this client
// to track their status.
this.localQueue = new LocalQueue( this.store );
// When a local queue has indicatie that it should send a change operation
// emit a simperium command. The Client instance will know how to route that
// command correctly to simperium
this.localQueue.on( 'send', ( data ) => {
this.emit( 'send', `c:${ JSON.stringify( data ) }` );
} );
// Handle change errors caused by changes originating from this client
this.localQueue.on( 'error', internal.handleChangeError.bind( this ) );
}
inherits( Channel, EventEmitter );
/**
* Called by a bucket when a bucket object has been updated.
*
* The channel uses this method to initiate change operations when objects are updated.
*
* It also uses this method during indexing to track which objects have been successfully
* downloaded.
*
* @param {BucketObject} object - the bucket object
* @param {Boolean} [sync=true] - if the object should be synced
*/
Channel.prototype.update = function( object, sync = true ) {
this.onBucketUpdate( object.id );
if ( sync === true ) {
internal.diffAndSend.call( this, object.id, object.data );
}
};
/**
* Tracks indexing state and emits `indexingStateChange`
*
* @private
* @param {Boolean} isIndexing - updates indexing state to this value
*/
Channel.prototype.setIsIndexing = function( isIndexing ) {
this.isIndexing = isIndexing;
this.emit( 'indexingStateChange', this.isIndexing );
}
/**
* Removes an object from Simperium. Called by a bucket when an object is deleted.
*
* @param {String} id - the id of the object to remove
*/
Channel.prototype.remove = function( id ) {
internal.removeAndSend.call( this, id )
}
/**
* Retrieves revisions for a given object from Simperium.
*
* @typedef {Object} BucketObjectRevision
* @property {String} id - bucket object id
* @property {Number} version - revision version
* @property {Object} data - object literal data at given version
*
* @param {String} id - the bucket object id
* @returns {Promise<Array<BucketObjectRevision>>} list of known object versions
*/
Channel.prototype.getRevisions = function( id ) {
return new Promise( ( resolve, reject ) => {
collectionRevisions( this, id, ( error, revisions ) => {
if ( error ) {
reject( error );
return;
}
resolve( revisions );
} );
} );
}
/**
* Checks if there are unsynced changes.
*
* @returns {Promise<Boolean>} true if there are still changes to sync
*/
Channel.prototype.hasLocalChanges = function() {
return Promise.resolve( this.localQueue.hasChanges() );
}
/**
* Retrieves the currently stored version number for a given object
*
* @param {String} id - object id to get the version for
* @returns {Promise<Number>} version number for the object
*/
Channel.prototype.getVersion = function( id ) {
return this.store.get( id ).then( ( ghost ) => {
if ( ghost && ghost.version ) {
return ghost.version;
}
return 0;
} );
}
/**
* Receives incoming messages from Simperium
*
* Called by a client that strips the channel number prefix before
* seding to a specific channel.
*
* @param {String} data - the message from Simperium
*/
Channel.prototype.handleMessage = function( data ) {
var message = parseMessage( data );
this.message.emit( message.command, message.data );
};
/**
* Used to send a message from this channel to Simperium
* The client listens for `send` events and correctly sends them to Simperium
*
* @emits Channel#send
* @private
* @param {String} data - the message to send
*/
Channel.prototype.send = function( data ) {
/**
* Send event
*
* @event Channel#send
* @type {String} - the message to send to Simperium
*/
this.emit( 'send', data );
};
/**
* Restores a buckets data to what is currently stored in the ghost data.
*/
Channel.prototype.reload = function() {
this.store.eachGhost( ghost => {
this.emit( 'update', ghost.key, ghost.data );
} );
};
/**
* Called after a bucket updates an object.
*
* Wile indexing keeps track of which objects have been retrieved.
*
* @param {String} id - object that was updated
*/
Channel.prototype.onBucketUpdate = function( id ) {
if ( ! this.isIndexing ) {
return;
}
if ( this.index_last_id == null || this.index_cv == null ) {
return;
} else if ( this.index_last_id === id ) {
internal.indexingComplete.call( this );
}
};
Channel.prototype.onAuth = function( data ) {
var auth;
var init;
try {
auth = JSON.parse( data );
this.emit( 'unauthorized', auth );
return;
} catch ( error ) {
// request cv and then send method
this.once( 'ready', () => {
this.localQueue.resendSentChanges();
} )
init = ( cv ) => {
if ( cv ) {
this.localQueue.start();
this.sendChangeVersionRequest( cv );
} else {
this.startIndexing();
}
};
this.store.getChangeVersion().then( init );
return;
}
};
/**
* Re-downloads all Simperium bucket data
*/
Channel.prototype.startIndexing = function() {
this.localQueue.pause();
this.setIsIndexing( true );
this.sendIndexRequest();
};
/**
* Called when a channel's socket has been connected
*/
Channel.prototype.onConnect = function() {
var init = {
name: this.name,
clientid: this.session_id,
api: '1.1',
token: this.access_token,
app_id: this.appid,
library: 'node-simperium',
version: '0.0.1'
};
this.send( format( 'init:%s', JSON.stringify( init ) ) );
};
Channel.prototype.onIndex = function( data ) {
const page = JSON.parse( data ),
objects = page.index,
mark = page.mark,
cv = page.current,
update = internal.updateObjectVersion.bind( this );
let objectId;
objects.forEach( function( object ) {
objectId = object.id;
update( object.id, object.v, object.d );
} );
if ( !mark ) {
if ( objectId ) {
this.index_last_id = objectId;
}
if ( !this.index_last_id ) {
internal.indexingComplete.call( this )
}
this.index_cv = cv;
} else {
this.sendIndexRequest( mark );
}
};
Channel.prototype.sendIndexRequest = function( mark ) {
this.send( format( 'i:1:%s::10', mark ? mark : '' ) );
};
Channel.prototype.sendChangeVersionRequest = function( cv ) {
this.send( format( 'cv:%s', cv ) );
};
Channel.prototype.onChanges = function( data ) {
var changes = JSON.parse( data ),
onChange = internal.changeObject.bind( this );
changes.forEach( function( change ) {
onChange( change.id, change );
} );
// emit ready after all server changes have been applied
this.emit( 'ready' );
};
Channel.prototype.onChangeVersion = function( data ) {
if ( data === UNKNOWN_CV ) {
this.store.setChangeVersion( null )
.then( () => this.startIndexing() );
}
};
Channel.prototype.onVersion = function( data ) {
// invalid version, give up without emitting
if ( data.slice( -2 ) === '\n?' ) {
return;
}
const ghost = parseVersionMessage( data );
this.emit( 'version', ghost.id, ghost.version, ghost.data );
this.emit( 'version.' + ghost.id, ghost.id, ghost.version, ghost.data );
this.emit( 'version.' + ghost.id + '.' + ghost.version, ghost.data );
};
/**
* Since revision data is basically immutable we can prevent the
* need to refetch it after it has been loaded once.
*
* E.g. key could be `${ entityId }.${ versionNumber }`
*
* @type {Map<String,Object>} stores specific revisions as a cache
*/
export const revisionCache = new Map();
/**
* Attempts to fetch an entity's revisions
*
* By default, a bucket stores two kinds of history:
* - revisions: the most-recent changes to an entity (60 of these)
* - archive: a "snapshot" of every ten revisions (100 of these)
*
* Together the revisions and archive span changes over the
* 1,060 most-recent changes to an entity, but of course once
* we hit the archive we lose save granularity.
*
* Individual buckets can override the defaults as well and also
* completely eliminate them.
*
* We don't have a listing of which revisions exist for a given entity.
*
* @param {Object} channel used to send messages to the Simperium server
* @param {String} id entity id for which to fetch revisions
* @param {Function} callback called on error or when finished
*/
function collectionRevisions( channel, id, callback ) {
/** @type {Number} ms delay arbitrarily chosen to give up on fetch */
const TIMEOUT = 200;
/** @type {Set} tracks requested revisions */
const requestedVersions = new Set();
/** @type {Array<Object>} contains the revisions and associated data */
const versions = [];
/** @type {Number} remembers newest version of an entity */
let latestVersion;
/** @type {Number} handle for "start finishing" timeout */
let timeout;
/**
* Receive a version update from the server and
* dispatch the next fetch or finish the fetching
*
* @param {String} id entity id
* @param {Number} version version of returned entity
* @param {Object} data value of entity at revision
*/
function onVersion( id, version, data ) {
revisionCache.set( `${ id }.${ version }`, data );
versions.push( { id, version, data } );
// if we have every possible revision already, finish it!
// this bypasses any mandatory delay
if ( versions.length === latestVersion ) {
finish();
return;
}
fetchNextVersion( version );
// defer the final response to the application
clearTimeout( timeout );
timeout = setTimeout( finish, TIMEOUT );
}
/**
* Stop listening for versions and stop fetching them
* and pass accumulated data back to application
*/
function finish() {
clearTimeout( timeout );
channel.removeListener( `version.${ id }`, onVersion );
// sort newest first
callback( null, versions.sort( ( a, b ) => b.version - a.version ) );
}
/**
* Find the next version which isn't around and issue
* a fetch if possible
*
* @param {Number} prevVersion starting point for finding next version
*/
function fetchNextVersion( prevVersion ) {
let version = prevVersion;
// find the next version to request
// some could have come back already
// or been requested already
while ( version > 0 && requestedVersions.has( version ) ) {
version -= 1;
}
// we have them all
if ( ! version ) {
return;
}
requestedVersions.add( version );
// fetch from server or local cache
if ( revisionCache.has( `${ id }.${ version }` ) ) {
onVersion( id, version, revisionCache.get( `${ id }.${ version }` ) );
} else {
channel.send( `e:${ id }.${ version }` );
}
}
// start listening for the responses
channel.on( `version.${ id }`, onVersion );
// request the first revision and start the sequence
// pre-emptively fetch as many as could exist by default
channel.store.get( id ).then( ( { version } ) => {
latestVersion = version;
// grab latest change revisions
for ( let i = 0; i < 60 && ( version - i ) > 0; i++ ) {
fetchNextVersion( version - i );
}
// grab archive revisions
// these are like 1, 11, 21, 31, …, 41, normal revisions [42, 43, 44, 45, …]
const firstArchive = Math.round( ( version - 60 ) / 10 ) * 10 + 1; // 127 -> 67 -> 6 -> 60 -> 61
for ( let i = 0; i < 100 && ( firstArchive - 10 * i ) > 0; i++ ) {
fetchNextVersion( firstArchive - 10 * i );
}
}, callback );
// and set an initial timeout for failed connections
timeout = setTimeout( finish, TIMEOUT * 4 );
}