-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathsimpleStorage.js
514 lines (411 loc) · 14.7 KB
/
simpleStorage.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
/* jshint browser: true */
/* global define: false, module: false */
// AMD shim
(function(root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define(factory);
} else if (typeof exports !== 'undefined') {
module.exports = factory();
} else {
root.simpleStorage = factory();
}
}(this, function() {
'use strict';
var VERSION = '0.2.1';
/* This is the object, that holds the cached values */
var _storage = false;
/* How much space does the storage take */
var _storage_size = 0;
var _storage_available = false;
var _ttl_timeout = null;
var _lsStatus = 'OK';
var LS_NOT_AVAILABLE = 'LS_NOT_AVAILABLE';
var LS_DISABLED = 'LS_DISABLED';
var LS_QUOTA_EXCEEDED = 'LS_QUOTA_EXCEEDED';
// This method might throw as it touches localStorage and doing so
// can be prohibited in some environments
function _init() {
// this method throws if localStorage is not usable, otherwise returns true
_storage_available = _checkAvailability();
// Load data from storage
_loadStorage();
// remove dead keys
_handleTTL();
// start listening for changes
_setupUpdateObserver();
// handle cached navigation
if ('addEventListener' in window) {
window.addEventListener('pageshow', function(event) {
if (event.persisted) {
_reloadData();
}
}, false);
}
_storage_available = true;
}
/**
* Sets up a storage change observer
*/
function _setupUpdateObserver() {
if ('addEventListener' in window) {
window.addEventListener('storage', _reloadData, false);
} else {
document.attachEvent('onstorage', _reloadData);
}
}
/**
* Reload data from storage when needed
*/
function _reloadData() {
try {
_loadStorage();
} catch (E) {
_storage_available = false;
return;
}
_handleTTL();
}
function _loadStorage() {
var source = localStorage.getItem('simpleStorage');
try {
_storage = JSON.parse(source) || {};
} catch (E) {
_storage = {};
}
_storage_size = _get_storage_size();
}
function _save() {
try {
localStorage.setItem('simpleStorage', JSON.stringify(_storage));
_storage_size = _get_storage_size();
} catch (E) {
return _formatError(E);
}
return true;
}
function _get_storage_size() {
var source = localStorage.getItem('simpleStorage');
return source ? String(source).length : 0;
}
function _handleTTL() {
var curtime, i, len, expire, keys, nextExpire = Infinity,
expiredKeysCount = 0;
clearTimeout(_ttl_timeout);
if (!_storage || !_storage.__simpleStorage_meta || !_storage.__simpleStorage_meta.TTL) {
return;
}
curtime = +new Date();
keys = _storage.__simpleStorage_meta.TTL.keys || [];
expire = _storage.__simpleStorage_meta.TTL.expire || {};
for (i = 0, len = keys.length; i < len; i++) {
if (expire[keys[i]] <= curtime) {
expiredKeysCount++;
delete _storage[keys[i]];
delete expire[keys[i]];
} else {
if (expire[keys[i]] < nextExpire) {
nextExpire = expire[keys[i]];
}
break;
}
}
// set next check
if (nextExpire !== Infinity) {
_ttl_timeout = setTimeout(_handleTTL, Math.min(nextExpire - curtime, 0x7FFFFFFF));
}
// remove expired from TTL list and save changes
if (expiredKeysCount) {
keys.splice(0, expiredKeysCount);
_cleanMetaObject();
_save();
}
}
function _setTTL(key, ttl) {
var curtime = +new Date(),
i, len, added = false;
ttl = Number(ttl) || 0;
// Set TTL value for the key
if (ttl !== 0) {
// If key exists, set TTL
if (_storage.hasOwnProperty(key)) {
if (!_storage.__simpleStorage_meta) {
_storage.__simpleStorage_meta = {};
}
if (!_storage.__simpleStorage_meta.TTL) {
_storage.__simpleStorage_meta.TTL = {
expire: {},
keys: []
};
}
_storage.__simpleStorage_meta.TTL.expire[key] = curtime + ttl;
// find the expiring key in the array and remove it and all before it (because of sort)
if (_storage.__simpleStorage_meta.TTL.expire.hasOwnProperty(key)) {
for (i = 0, len = _storage.__simpleStorage_meta.TTL.keys.length; i < len; i++) {
if (_storage.__simpleStorage_meta.TTL.keys[i] === key) {
_storage.__simpleStorage_meta.TTL.keys.splice(i);
}
}
}
// add key to keys array preserving sort (soonest first)
for (i = 0, len = _storage.__simpleStorage_meta.TTL.keys.length; i < len; i++) {
if (_storage.__simpleStorage_meta.TTL.expire[_storage.__simpleStorage_meta.TTL.keys[i]] > (curtime + ttl)) {
_storage.__simpleStorage_meta.TTL.keys.splice(i, 0, key);
added = true;
break;
}
}
// if not added in previous loop, add here
if (!added) {
_storage.__simpleStorage_meta.TTL.keys.push(key);
}
} else {
return false;
}
} else {
// Remove TTL if set
if (_storage && _storage.__simpleStorage_meta && _storage.__simpleStorage_meta.TTL) {
if (_storage.__simpleStorage_meta.TTL.expire.hasOwnProperty(key)) {
delete _storage.__simpleStorage_meta.TTL.expire[key];
for (i = 0, len = _storage.__simpleStorage_meta.TTL.keys.length; i < len; i++) {
if (_storage.__simpleStorage_meta.TTL.keys[i] === key) {
_storage.__simpleStorage_meta.TTL.keys.splice(i, 1);
break;
}
}
}
_cleanMetaObject();
}
}
// schedule next TTL check
clearTimeout(_ttl_timeout);
if (_storage && _storage.__simpleStorage_meta && _storage.__simpleStorage_meta.TTL && _storage.__simpleStorage_meta.TTL.keys.length) {
_ttl_timeout = setTimeout(_handleTTL, Math.min(Math.max(_storage.__simpleStorage_meta.TTL.expire[_storage.__simpleStorage_meta.TTL.keys[0]] - curtime, 0), 0x7FFFFFFF));
}
return true;
}
function _cleanMetaObject() {
var updated = false,
hasProperties = false,
i;
if (!_storage || !_storage.__simpleStorage_meta) {
return updated;
}
// If nothing to TTL, remove the object
if (_storage.__simpleStorage_meta.TTL && !_storage.__simpleStorage_meta.TTL.keys.length) {
delete _storage.__simpleStorage_meta.TTL;
updated = true;
}
// If meta object is empty, remove it
for (i in _storage.__simpleStorage_meta) {
if (_storage.__simpleStorage_meta.hasOwnProperty(i)) {
hasProperties = true;
break;
}
}
if (!hasProperties) {
delete _storage.__simpleStorage_meta;
updated = true;
}
return updated;
}
/**
* Checks if localStorage is available or throws an error
*/
function _checkAvailability() {
var err;
var items = 0;
// Firefox sets localStorage to 'null' if support is disabled
// IE might go crazy if quota is exceeded and start treating it as 'unknown'
if (window.localStorage === null || typeof window.localStorage === 'unknown') {
err = new Error('localStorage is disabled');
err.code = LS_DISABLED;
throw err;
}
// There doesn't seem to be any indication about localStorage support
if (!window.localStorage) {
err = new Error('localStorage not supported');
err.code = LS_NOT_AVAILABLE;
throw err;
}
try {
items = window.localStorage.length;
} catch (E) {
throw _formatError(E);
}
try {
// we try to set a value to see if localStorage is really usable or not
window.localStorage.setItem('__simpleStorageInitTest', (+new Date).toString(16));
window.localStorage.removeItem('__simpleStorageInitTest');
} catch (E) {
if (items) {
// there is already some data stored, so this might mean that storage is full
throw _formatError(E);
} else {
// we do not have any data stored and we can't add anything new
// so we are most probably in Private Browsing mode where
// localStorage is turned off in some browsers (max storage size is 0)
err = new Error('localStorage is disabled');
err.code = LS_DISABLED;
throw err;
}
}
return true;
}
function _formatError(E) {
var err;
// No more storage:
// Mozilla: NS_ERROR_DOM_QUOTA_REACHED, code 1014
// WebKit: QuotaExceededError/QUOTA_EXCEEDED_ERR, code 22
// IE number -2146828281: Out of memory
// IE number -2147024882: Not enough storage is available to complete this operation
if (E.code === 22 || E.code === 1014 || [-2147024882, -2146828281, -21474675259].indexOf(E.number) > 0) {
err = new Error('localStorage quota exceeded');
err.code = LS_QUOTA_EXCEEDED;
return err;
}
// SecurityError, localStorage is turned off
if (E.code === 18 || E.code === 1000) {
err = new Error('localStorage is disabled');
err.code = LS_DISABLED;
return err;
}
// We are trying to access something from an object that is either null or undefined
if (E.name === 'TypeError') {
err = new Error('localStorage is disabled');
err.code = LS_DISABLED;
return err;
}
return E;
}
// Sets value for _lsStatus
function _checkError(err) {
if (!err) {
_lsStatus = 'OK';
return err;
}
switch (err.code) {
case LS_NOT_AVAILABLE:
case LS_DISABLED:
case LS_QUOTA_EXCEEDED:
_lsStatus = err.code;
break;
default:
_lsStatus = err.code || err.number || err.message || err.name;
}
return err;
}
////////////////////////// PUBLIC INTERFACE /////////////////////////
try {
_init();
} catch (E) {
_checkError(E);
}
return {
version: VERSION,
status: _lsStatus,
canUse: function() {
return _lsStatus === 'OK' && !!_storage_available;
},
set: function(key, value, options) {
if (key === '__simpleStorage_meta') {
return false;
}
if (!_storage) {
return false;
}
// undefined values are deleted automatically
if (typeof value === 'undefined') {
return this.deleteKey(key);
}
options = options || {};
// Check if the value is JSON compatible (and remove reference to existing objects/arrays)
try {
value = JSON.parse(JSON.stringify(value));
} catch (E) {
return _formatError(E);
}
_storage[key] = value;
_setTTL(key, options.TTL || 0);
return _save();
},
hasKey: function(key) {
return !!this.get(key);
},
get: function(key) {
if (!_storage) {
return false;
}
if (_storage.hasOwnProperty(key) && key !== '__simpleStorage_meta') {
// TTL value for an existing key is either a positive number or an Infinity
if (this.getTTL(key)) {
return _storage[key];
}
}
},
deleteKey: function(key) {
if (!_storage) {
return false;
}
if (key in _storage) {
delete _storage[key];
_setTTL(key, 0);
return _save();
}
return false;
},
setTTL: function(key, ttl) {
if (!_storage) {
return false;
}
_setTTL(key, ttl);
return _save();
},
getTTL: function(key) {
var ttl;
if (!_storage) {
return false;
}
if (_storage.hasOwnProperty(key)) {
if (_storage.__simpleStorage_meta &&
_storage.__simpleStorage_meta.TTL &&
_storage.__simpleStorage_meta.TTL.expire &&
_storage.__simpleStorage_meta.TTL.expire.hasOwnProperty(key)) {
ttl = Math.max(_storage.__simpleStorage_meta.TTL.expire[key] - (+new Date()) || 0, 0);
return ttl || false;
} else {
return Infinity;
}
}
return false;
},
flush: function() {
if (!_storage) {
return false;
}
_storage = {};
try {
localStorage.removeItem('simpleStorage');
return true;
} catch (E) {
return _formatError(E);
}
},
index: function() {
if (!_storage) {
return false;
}
var index = [],
i;
for (i in _storage) {
if (_storage.hasOwnProperty(i) && i !== '__simpleStorage_meta') {
index.push(i);
}
}
return index;
},
storageSize: function() {
return _storage_size;
}
};
}));