forked from minj/foxtrick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal-store.js
518 lines (418 loc) · 11.5 KB
/
local-store.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
/**
* storage.set() and storage.get() are a pair of functions that can store
* permanent information.
* The stored value must be a JSON-serializable object, or of native types.
*
* @author convincedd, LA-MJ
*/
'use strict';
/* eslint-disable */
if (!this.Foxtrick)
// @ts-ignore
var Foxtrick = {};
/* eslint-enable */
Foxtrick.storage = {};
// background
if (Foxtrick.context == 'background') {
// localStore is a Promise that
// a) fulfills with a IDBStore value which is **NOT** a promise-aware utility
// b) rejects if initialization failed
Object.defineProperty(Foxtrick, 'localStore', (function defineLocalStore() {
var getIDBShim = function() {
const PREFIX = 'localStore.';
const CLEANUP = Foxtrick.catch(PREFIX);
/** @type {IDBStore} */
const STORE = {
put: function(key, value, success, failure) {
let k = PREFIX + key;
/** @type {Promise<string>} */
let promise = Promise.resolve().then(() => {
let val = JSON.stringify(value);
window.localStorage.setItem(k, val);
return k;
}).then(success, failure);
// return a 'raw' Promise for internal use if failure is null
if (failure === null)
return promise;
// log any errors otherwise
return promise.catch(CLEANUP).then(() => k);
},
get: function(key, success, failure) {
let promise = Promise.resolve().then(() => {
let k = PREFIX + key;
let val = window.localStorage.getItem(k);
return JSON.parse(val);
}).then(success, failure);
// return a 'raw' Promise for internal use if failure is null
if (failure === null)
return promise;
// log any errors otherwise
return promise.catch(CLEANUP);
},
iterate: function(callback, options) {
/**
* @param {string} key
* @return {function(any):void}
*/
var makeIterAdapter = function(key) {
return function iterAdapter(val) {
/** @type {IDBStore.CursorAny} */
let cursor = {
key: key,
source: STORE,
};
if (options.writeAccess) {
let mutCursor = /** @type {IDBStore.Cursor} */ (cursor);
mutCursor.update = function(newVal) {
return Foxtrick.storage.set(key, newVal).catch(CLEANUP);
};
mutCursor.delete = function() {
return Promise.resolve().then(() => {
window.localStorage.removeItem(PREFIX + key);
}).catch(CLEANUP);
};
}
callback(val, cursor);
};
};
options.onError = options.onError || CLEANUP;
let range = options.keyRange || STORE.makeKeyRange({});
let lower = range.lower;
let upper = range.upper;
/**
* @param {string} key
* @return {boolean}
*/
var keyMatches = function(key) {
let lowOK = range.lowerOpen ? key > lower : key >= lower;
let upOK = range.upperOpen ? key < upper : key <= upper;
return lowOK && upOK;
};
var promises = [];
for (let key in window.localStorage) {
key = key.slice(PREFIX.length);
if (keyMatches(key)) {
// get a 'raw' Promise by
// overriding error callback with null
let promise = this.get(key, makeIterAdapter(key), null);
promises.push(promise);
}
}
Promise.all(promises)
.then(options.onEnd, options.onError)
.catch(CLEANUP);
},
makeKeyRange: function(opts) {
const MAX_CHAR_CODE = 0xffff;
const MAX_CHAR = String.fromCharCode(MAX_CHAR_CODE);
const MIN_CHAR = String.fromCharCode(0);
/** @type {IDBStore.KeyRange} */
var ret = {};
if (opts.only) {
ret.lower = opts.only;
let len = ret.lower.length;
if (len) {
let last = ret.lower.charCodeAt(len - 1);
ret.upper = ret.lower.slice(0, len - 1) + String.fromCharCode(last + 1);
}
else {
ret.upper = MIN_CHAR;
}
ret.lowerOpen = false;
ret.upperOpen = true;
}
else {
let lower = ret.lower = opts.lower || '';
let maxString = Foxtrick.repeat(MAX_CHAR, lower.length + 1);
ret.upper = opts.upper || maxString;
ret.lowerOpen = opts.excludeLower;
ret.upperOpen = opts.excludeUpper;
}
return ret;
},
};
return STORE;
};
/** @type {Promise<IDBStore>} */
const STORE_PROMISE = new Promise(function(fulfill, reject) {
if (indexedDB !== null) {
/** @type {IDBStore} */
// @ts-ignore
const STORE = new Foxtrick.IDBStore({
storeName: 'localStore',
storePrefix: 'Foxtrick',
keyPath: null,
autoIncrement: true,
indexes: [],
onStoreReady: function() {
fulfill(STORE);
},
onError: reject,
});
}
else if (window.localStorage) {
Promise.resolve(getIDBShim()).then(fulfill);
}
else {
reject(new Error('No storage implementation available'));
}
});
// return localStore property descriptor
return {
get: function() {
// catch every time localStore is used
// will make it easier to spot in the logs
return STORE_PROMISE.catch(function(e) {
// just log a simple message to disable stack: most likely Private mode
var message = e.message || e.target.error.message;
Foxtrick.log('WARNING: localStore has not been initialized:', message);
// re-throw to disable chain
throw e;
});
},
};
})());
}
/**
* Get a promise when storage value is set.
*
* key should be a string.
* value may be any stringify-able object.
*
* @param {string} key
* @param {*} value
* @return {Promise<string>} {Promise.<key>}
*/
Foxtrick.storage.set = function(key, value) {
if (Foxtrick.context == 'content') {
return new Promise(function(fulfill, reject) {
Foxtrick.SB.ext.sendRequest({
req: 'storageSet',
key: key,
value: value,
}, function onSendResponse(response) {
var err = Foxtrick.jsonError(response);
if (err instanceof Error)
reject(err);
else
fulfill(response);
});
});
}
return Foxtrick.localStore.then(function(store) {
return new Promise(function(fulfill, reject) {
try {
store.put(key, value, fulfill, reject);
}
catch (e) {
if (e.name == 'InvalidStateError')
return;
throw e;
}
}).catch(function(e) {
Foxtrick.log('Error in storage.set', key, e);
throw e;
});
}, function() {
// swallow Foxtrick.localStore failure here
// already logged
throw Foxtrick.SWALLOWED_ERROR;
});
};
/**
* Get a promise for a storage value.
*
* Promise will never reject, returns null instead.
*
* key should be a string.
* value may be any stringify-able object or null if N/A.
*
* @param {string} key
* @return {Promise<*>} {Promise.<?value>}
*/
Foxtrick.storage.get = function(key) {
if (Foxtrick.context == 'content') {
return new Promise(function(fulfill) {
// background never rejects
Foxtrick.SB.ext.sendRequest({ req: 'storageGet', key: key }, fulfill);
});
}
return Foxtrick.localStore.then(function(/** @type {IDBStore} */ store) {
return new Promise(function(resolve, reject) {
try {
store.get(key, function onStoreGet(value) {
let val = value;
// cast undefined to null
if (val == null)
val = null;
resolve(val);
}, reject);
}
catch (e) {
if (e instanceof DOMException && e.name == 'InvalidStateError') {
resolve(null);
return;
}
reject(e);
}
});
}, function() {
// swallow Foxtrick.localStore failure here
// already logged
return null;
}).catch(function(e) {
try {
Foxtrick.log('Error in storage.get', key, e);
}
catch (ee) {}
return null;
});
};
/**
* Get a promise for when a certain storage branch is deleted
*
* @param {string} branch
* @return {Promise<void>}
*/
Foxtrick.storage.deleteBranch = function(branch) {
if (Foxtrick.context == 'content') {
return new Promise(function(fulfill, reject) {
Foxtrick.SB.ext.sendRequest({
req: 'storageDeleteBranch',
branch: branch,
}, function onSendResponse(response) {
var err = Foxtrick.jsonError(response);
if (err instanceof Error)
reject(err);
else
fulfill(response);
});
});
}
return Foxtrick.localStore.then(function(store) {
return new Promise(function(fulfill, reject) {
let br = branch == null ? '' : branch.toString();
/** @type {IDBStore.IterateOpts} */
var options = {
writeAccess: true,
onEnd: function() {
Foxtrick.log('localStore branch "' + br + '" deleted');
fulfill();
},
onError: function(e) {
Foxtrick.log('Error deleting localStore branch', br, e.message);
reject(e);
},
};
try {
if (br !== '') {
options.keyRange = store.makeKeyRange({
lower: br + '.', // charCode 46
upper: br + '/', // charCode 47
excludeUpper: true,
});
}
}
catch (e) {
Foxtrick.log('Error deleting localStore branch', br,
'in makeKeyRange', e.message);
reject(e);
return;
}
try {
store.iterate(function onStoreIterate(_, /** @type {IDBStore.Cursor} */ cursor) {
cursor.delete();
}, options);
}
catch (e) {
if (e.name == 'InvalidStateError')
return;
throw e;
}
}).catch(function(e) {
Foxtrick.log('Error in localDeleteBranch', branch, e);
throw e;
});
});
};
// /////////////////////////
// TODO: remove deprecated
// ////////////////////////
/**
* Save a value in local storage
*
* @deprecated use storage.set() instead
*
* @param {string} key
* @param {any} value
*/
Foxtrick.localSet = function(key, value) {
Foxtrick.storage.set(key, value).catch(Foxtrick.catch('localSet'));
};
/**
* Get a value from local storage
*
* @deprecated use storage.get() instead
*
* @param {string} key
* @param {function(any):any} callback
*/
Foxtrick.localGet = function(key, callback) {
Foxtrick.storage.get(key).then(callback).catch(Foxtrick.catch('localGet'));
};
/**
* Remove a branch from local storage
*
* @deprecated use storage.deleteBranch() instead
*
* @param {string} branch
*/
Foxtrick.localDeleteBranch = function(branch) {
Foxtrick.storage.deleteBranch(branch).catch(Foxtrick.catch('localDeleteBranch'));
};
/* eslint-disable max-len */
/**
* @typedef IDBStore
* @prop {(key:string, success:(val:any)=>any, failure:((err:any)=>void)|null)=>Promise<any>} get
* @prop {(key:string, val:any, success:(key:string)=>any, failure:((err:any)=>void)|null)=>Promise<string>} put
* @prop {(callback:IDBStore.IterateCallback, options:IDBStore.IterateOpts)=>void} iterate
* @prop {(options:IDBStore.KeyRangeOpts)=>IDBStore.KeyRange} makeKeyRange
*/
/* eslint-enable max-len */
/**
* @typedef IDBStore.KeyRangeOpts
* @prop {string} [only]
* @prop {string} [lower]
* @prop {string} [upper]
* @prop {boolean} [excludeLower]
* @prop {boolean} [excludeUpper]
*/
/**
* @typedef IDBStore.KeyRange
* @prop {string} lower
* @prop {string} upper
* @prop {boolean} lowerOpen
* @prop {boolean} upperOpen
*/
/**
* @typedef IDBStore.IterateReadOnlyOpts
* @prop {(values:any[])=>void} onEnd
* @prop {(error:any)=>void} [onError]
* @prop {IDBStore.KeyRange} [keyRange]
*/
/**
* @typedef {IDBStore.IterateReadOnlyOpts & {writeAccess: true}} IDBStore.IterateOpts
*/
/**
* @typedef IDBStore.CursorReadOnly
* @prop {string} key
* @prop {IDBStore} source
*/
/**
* @typedef IDBStore.CursorMutable
* @prop {()=>any} [delete]
* @prop {(val:any)=>any} [update]
*/
/** @typedef {IDBStore.CursorReadOnly & IDBStore.CursorMutable} IDBStore.Cursor */
/** @typedef {IDBStore.CursorReadOnly | IDBStore.Cursor} IDBStore.CursorAny */
/** @typedef {(val:any, cursor:IDBStore.CursorAny)=>void} IDBStore.IterateCallback */