forked from amazon-archives/amazon-cognito-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCognitoSyncDataset.js
592 lines (403 loc) · 20.4 KB
/
CognitoSyncDataset.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
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
/**
* Copyright 2014 Amazon.com,
* Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Amazon Software License (the "License").
* You may not use this file except in compliance with the
* License. A copy of the License is located at
*
* http://aws.amazon.com/asl/
*
* or in the "license" file accompanying this file. This file is
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, express or implied. See the License
* for the specific language governing permissions and
* limitations under the License.
*/
AWS = AWS || {};
AWS.CognitoSyncManager = AWS.CognitoSyncManager || {};
AWS.CognitoSyncManager.Dataset = (function() {
/**
* Constructs a new dataset class.
* @param {string} datasetName
* @param provider
* @param {AWS.CognitoSyncLocalStorage} local
* @param {AWS.CognitoSyncRemoteStorage} remote
* @param {function} logger
* @constructor
*/
var CognitoSyncDataset = function(datasetName, provider, local, remote, logger) {
this.MAX_RETRY = 3;
this.datasetName = datasetName;
this.provider = provider;
this.local = local;
this.remote = remote;
this.logger = logger || function(){};
};
/**
* Validates the record key to ensure it is not empty and shorter than 128 characters.
* @param {String} key The key to validate.
* @returns {boolean}
*/
CognitoSyncDataset.prototype.validateKey = function(key) {
var namePattern = new RegExp('^[a-zA-Z0-9_.:-]{1,128}$');
return namePattern.test(key);
};
/**
* Writes a record to local storage.
* @param {string} key
* @param {string} value
* @param {function} callback Callback(Error, Record)
*/
CognitoSyncDataset.prototype.put = function(key, value, callback) {
var valueType = typeof value;
if (!this.validateKey(key)) { return callback(new Error('Invalid key.')); }
if (valueType !== 'string') {
return callback(new Error('The value type must be a string but was ' + valueType + '.'));
}
this.local.putValue(this.getIdentityId(), this.datasetName, key, value, callback);
};
/**
* Writes a record as null, to be cleaned up on the next synchronization.
* @param {string} key The key to remove.
* @param {function} callback Callback(Error, Record)
*/
CognitoSyncDataset.prototype.remove = function(key, callback) {
if (!this.validateKey(key)) { return callback(new Error('Invalid key.')); }
this.local.putValue(this.getIdentityId(), this.datasetName, key, null, callback);
};
/**
* Gets a record's value from local storage.
* @param {string} key
* @param {function} callback Callback(Error, Value)
*/
CognitoSyncDataset.prototype.get = function(key, callback) {
if (!this.validateKey(key)) { return callback(new Error('Invalid key.')); }
this.local.getValue(this.getIdentityId(), this.datasetName, key, callback);
};
/**
* Gets all records in a dataset from local storage.
* @param {function} callback Callback(Error, Records)
*/
CognitoSyncDataset.prototype.getAllRecords = function(callback) {
this.local.getRecords(this.getIdentityId(), this.datasetName, callback);
};
/**
* Returns the amount of data stored on the server.
* @param callback
*/
CognitoSyncDataset.prototype.getDataStorage = function(callback) {
this.getDatasetMetadata(function(err, meta) {
if (err) { return callback(err); }
if (!meta) { return callback(null, 0); }
return callback(null, meta.getDataStorage());
});
};
/**
* Returns if a specific record has changed.
* @param key
* @param callback
*/
CognitoSyncDataset.prototype.isChanged = function(key, callback) {
if (!this.validateKey(key)) { return callback(new Error('Invalid key.')); }
this.local.getRecord(this.getIdentityId(), this.datasetName, key, function(err, record) {
callback(null, (record && record.isModified()));
});
};
/**
* Returns the dataset metadata.
* @param callback
*/
CognitoSyncDataset.prototype.getDatasetMetadata = function(callback) {
this.local.getDatasetMetadata(this.getIdentityId(), this.datasetName, callback);
};
/**
* Resolves conflicts using the records provided.
* @param resolvedRecords
* @param callback
*/
CognitoSyncDataset.prototype.resolve = function(resolvedRecords, callback) {
this.local.putRecords(this.getIdentityId(), this.datasetName, resolvedRecords, callback);
};
/**
* Puts all values into the dataset.
* @param values
* @param callback
* @returns {*}
*/
CognitoSyncDataset.prototype.putAll = function(values, callback) {
var isValid = true;
for (var key in values) {
if (values.hasOwnProperty(key)) {
if (!this.validateKey(key)) { isValid = false; }
}
}
if (!isValid) { return callback(new Error('Object contains invalid keys.')); }
this.local.putAllValues(this.getIdentityId(), this.datasetName, values, callback);
};
/**
* Returns all records from a dataset.
* @param callback
*/
CognitoSyncDataset.prototype.getAll = function(callback) {
var map = {};
var record;
this.local.getRecords(this.getIdentityId(), this.datasetName, function(err, records) {
if (err) { return callback(err); }
for (var r in records) {
if (records.hasOwnProperty(r)) {
record = records[r];
if (!record.isDeleted()) { map[record.getKey()] = record.getValue(); }
}
}
callback(null, map);
});
};
/**
* Returns the current user's identity id.
* @returns {string};
*/
CognitoSyncDataset.prototype.getIdentityId = function() {
return this.provider.identityId;
};
/**
* Returns the records that have been modified.
* @param callback
*/
CognitoSyncDataset.prototype.getModifiedRecords = function(callback) {
this.local.getModifiedRecords(this.getIdentityId(), this.datasetName, callback);
};
/**
* Returns a list of datasets that have been merged.
* @param callback
*/
CognitoSyncDataset.prototype.getLocalMergedDatasets = function(callback) {
var mergedDatasets = [];
var prefix = this.datasetName + '.';
var dataset;
this.local.getDatasets(this.getIdentityId(), function(err, datasets) {
for (var d in datasets) {
if (datasets.hasOwnProperty(d)) {
dataset = datasets[d];
if (dataset.getDatasetName().indexOf(prefix) === 0) {
mergedDatasets.push(dataset.getDatasetName());
}
}
}
callback(null, mergedDatasets);
});
};
/**
* Starts the synchronization process.
* @param callback
* @param retry
*/
CognitoSyncDataset.prototype.synchronize = function(callback, retry) {
var root = this;
// Validate callback object.
callback = callback || {};
callback.onSuccess = callback.onSuccess || function(dataset, updates) {};
callback.onFailure = callback.onFailure || function(err) {};
callback.onConflict = callback.onConflict || function(dataset, conflicts, callback) { return callback(false); };
callback.onDatasetDeleted = callback.onDatasetDeleted || function(dataset, deletedDataset, callback) { return callback(false); };
callback.onDatasetsMerged = callback.onDatasetsMerged || function(dataset, merges, callback) { return callback(false); };
// Validate/initialize retry count.
if (retry === undefined) { retry = this.MAX_RETRY; }
root.logger('Starting synchronization... (retries: ' + retry + ')');
if (retry < 0) {
return callback.onFailure(new Error('Synchronize failed: exceeded maximum retry count.'));
}
// First check if any datasets have been merged locally.
this.getLocalMergedDatasets(function(err, mergedDatasets) {
if (err) { callback.onFailure(err); }
root.logger('Checking for locally merged datasets... found ' + mergedDatasets.length + '.');
// Detect if merged datasets.
if (mergedDatasets.length > 0) {
root.logger('Deferring to .onDatasetsMerged.');
return callback.onDatasetsMerged(root, mergedDatasets, function(isContinue) {
if (!isContinue) {
// Merges were not handled by callback. Cancel sync.
return callback.onFailure(new Error('Synchronization cancelled by onDatasetsMerged() callback returning false.'));
} else {
// Merges are handled within callback. Restart sync.
return root.synchronize(callback, --retry);
}
});
} else {
// Get the last sync count so we can tell the server what to diff.
root.local.getLastSyncCount(root.getIdentityId(), root.datasetName, function(err, syncCount) {
if (err) { return callback.onFailure(err); }
root.logger('Detecting last sync count... ' + syncCount);
if (syncCount == -1) {
// Dataset has been deleted locally
root.remote.deleteDataset(root.datasetName, function(err, data) {
if (err) { return callback.onFailure(err); }
root.local.purgeDataset(root.getIdentityId(), root.datasetName, function(err) {
if (err) { return callback.onFailure(err); }
return callback.onSuccess(root);
});
});
} else {
// Get all the remote records that have changed since the latest sync count.
root.remote.listUpdates(root.datasetName, syncCount, function(err, remoteRecords) {
if (err) { return callback.onFailure(err); }
root.logger('Fetching remote updates... found ' + remoteRecords.records.length + '.');
var mergedNameList = remoteRecords.getMergedDatasetNameList();
root.logger('Checking for remote merged datasets... found ' + mergedNameList.length + '.');
if (mergedNameList.length > 0) {
root.logger('Deferring to .onDatasetsMerged.');
// Merged datasets exist. Use callback to determine action.
return callback.onDatasetsMerged(root, mergedNameList, function(doContinue) {
if (!doContinue) { callback.onFailure(new Error('Cancelled due to .onDatasetsMerged result.')); }
else { root._synchronizeInternal(callback, --retry); }
});
}
// Check if dataset doesn't exist or is deleted.
if (syncCount !== 0 && !remoteRecords || remoteRecords.isDeleted()) {
return callback.onDatasetDeleted(root, remoteRecords.getDatasetName(), function(doContinue) {
root.logger('Dataset should be deleted. Deferring to .onDatasetDeleted.');
if (doContinue) {
root.logger('.onDatasetDeleted returned true, purging dataset locally.');
return root.local.purgeDataset(root.getIdentityId(), root.datasetName, function(err) {
if (err) { return callback.onFailure(err); }
return root._synchronizeInternal(callback, --retry);
});
} else {
root.logger('.onDatasetDeleted returned false, cancelling sync.');
return callback.onFailure(new Error('Cancelled due to .onDatasetDeleted result.'));
}
});
}
var updatedRemoteRecords = remoteRecords.getRecords();
var lastSyncCount = remoteRecords.getSyncCount();
var sessionToken = remoteRecords.getSyncSessionToken();
// Check if there have been any updates since the last sync count.
root.logger('Checking for remote updates since last sync count... found ' + updatedRemoteRecords.length + '.');
if (updatedRemoteRecords.length > 0) {
root._synchronizeResolveLocal(updatedRemoteRecords, function(err, conflicts) {
if (err) { return callback.onFailure(err); }
root.logger('Checking for conflicts... found ' + conflicts.length + '.');
if (conflicts.length > 0) {
root.logger('Conflicts detected. Deferring to .onConflict.');
callback.onConflict(root, conflicts, function(isContinue) {
if (!isContinue) {
root.logger('.onConflict returned false. Cancelling sync.');
return callback.onFailure(new Error('Sync cancelled. Conflict callback returned false.'));
} else {
// Update remote records or we will just hit another sync conflict next go around.
root._synchronizePushRemote(sessionToken, syncCount, function(){
return root.synchronize(callback, --retry);
});
}
});
} else {
// No conflicts, update local records.
root.logger('No conflicts. Updating local records.');
root.local.putRecords(root.getIdentityId(), root.datasetName, updatedRemoteRecords, function(err) {
if (err) { return callback.onFailure(err); }
// Update the local sync count to match.
root.local.updateLastSyncCount(root.getIdentityId(), root.datasetName, lastSyncCount, function(err) {
if (err) { return callback.onFailure(err); }
root.logger('Finished resolving records. Restarting sync.');
// Callback returned true, starting sync.
return root.synchronize(callback, --retry);
});
});
}
});
} else {
// Nothing updated remotely. Push local changes to remote.
root.logger('Nothing updated remotely. Pushing local changes to remote.');
root._synchronizePushRemote(sessionToken, lastSyncCount, function(err) {
if (err) {
root.logger('Remote push failed. Likely concurrent sync conflict. Retrying...');
return root.synchronize(callback, --retry);
}
root.logger('Sync successful.');
return callback.onSuccess(root, updatedRemoteRecords);
});
}
});
}
});
}
});
};
/**
* An internal function for helping the synchronization call to resolve local conflicts.
* @param remoteRecords
* @param callback
* @private
*/
CognitoSyncDataset.prototype._synchronizeResolveLocal = function(remoteRecords, callback) {
// Step two of the synchronization flow.
// The dataset exists remotely so we need to determine if there are any deletions or conflicts.
// Once everything is resolved, we update the local records.
var root = this;
var conflicts = [];
// Make sure there are remote records that need resolving.
if (remoteRecords && remoteRecords.length > 0) {
// Get the local records so we can compare them to the remote records.
root.local.getRecords(root.getIdentityId(), root.datasetName, function(err, localRecords) {
var localMap = {};
var i, key, local;
// Build a map of the local records array for easier key lookup.
for (i=0; i<localRecords.length; i++) {
localMap[localRecords[i].getKey()] = localRecords[i];
}
// Compare local and remote records.
for (i=0; i<remoteRecords.length; i++) {
key = remoteRecords[i].getKey();
local = localMap[key];
if (local && local.isModified() && local.getValue() !== remoteRecords[i].getValue()) {
conflicts.push(new AWS.CognitoSyncManager.Conflict(remoteRecords[i], local));
}
}
return callback(null, conflicts);
});
} else {
// There are no remote records. Nothing to resolve.
return callback(null, conflicts);
}
};
/**
* An internal function for helping the synchronization call to push changes to the server.
* @param sessionToken
* @param syncCount
* @param callback
* @private
*/
CognitoSyncDataset.prototype._synchronizePushRemote = function(sessionToken, syncCount, callback) {
// Step three of the synchronization flow.
// The local dataset has modifications so we need to push the local changes to remote.
// Then we need to update the local metadata and update/verify the sync count.
var root = this;
// Push changes to remote.
this.getModifiedRecords(function(err, localChanges) {
if (localChanges.length > 0) {
root.remote.putRecords(root.datasetName, localChanges, sessionToken, function(err, records) {
if (err) { callback(err); }
// Update local metadata.
root.local.putRecords(root.getIdentityId(), root.datasetName, records, function(err) {
if (err) { return callback(err); }
var newSyncCount = 0;
// Calculate new sync count.
for (var r in records) {
if (records.hasOwnProperty(r)) {
newSyncCount = newSyncCount < records[r].getSyncCount() ? records[r].getSyncCount() : newSyncCount;
}
}
root.local.updateLastSyncCount(root.getIdentityId(), root.datasetName, newSyncCount, function(err) {
if (err) { return callback(err); }
return callback(null, true);
});
});
});
} else {
// Nothing to change.
return callback(null, true);
}
});
};
return CognitoSyncDataset;
})();