-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
browser-storage.js
150 lines (138 loc) · 3.67 KB
/
browser-storage.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
var helpers = require('./helpers.js');
var TTL_SUFFIX = '.___exp';
var localStorageDriver;
/**
* @locus Client
* @class BrowserStorage
* @summary localStorage driven storage
*/
function BrowserStorage(clientStorage) {
if (clientStorage) {
this.data = clientStorage.data;
this.ttlData = clientStorage.ttlData;
} else {
this.data = {};
this.ttlData = {};
}
this.init();
}
/**
* @locus Client
* @memberOf BrowserStorage
* @name init
* @summary parse document.cookie string
* @returns {void 0}
*/
BrowserStorage.prototype.init = function () {
localStorageDriver = window.localStorage || localStorage;
// CLEAN UP EXPIRED ITEMS
var i = localStorageDriver.length;
var key;
while (i--) {
key = localStorageDriver.key(i);
if (typeof key === 'string' && !!~key.indexOf(TTL_SUFFIX)) {
var expireAt = parseInt(localStorageDriver.getItem(key));
if (expireAt <= Date.now()) {
localStorageDriver.removeItem(key);
localStorageDriver.removeItem(key.replace(TTL_SUFFIX, ''));
} else {
this.ttlData[key] = expireAt;
}
} else {
this.data[key] = this.unescape(localStorageDriver.getItem(key));
}
}
};
/**
* @locus Client
* @memberOf BrowserStorage
* @name set
* @param {String} key - The name of the cookie to create/overwrite
* @param {String} value - The value of the cookie
* @param {Number} ttl - BrowserStorage TTL (e.g. max-age) in seconds
* @summary Create/overwrite a cookie.
* @returns {Boolean}
*/
BrowserStorage.prototype.set = function (key, value, ttl) {
if (typeof key === 'string') {
this.data[key] = value;
localStorageDriver.setItem(key, this.escape(value));
if (typeof ttl === 'number') {
var expireAt = Date.now() + (ttl * 1000);
this.ttlData[key] = expireAt;
localStorageDriver.setItem(key + TTL_SUFFIX, expireAt);
}
return true;
}
return false;
};
/**
* @locus Client
* @memberOf BrowserStorage
* @name remove
* @param {String} key - The name of the record to remove
* @summary Remove a record(s).
* @returns {Boolean}
*/
BrowserStorage.prototype.remove = function (key) {
if (typeof key === 'string' && this.data.hasOwnProperty(key)) {
localStorageDriver.removeItem(key);
localStorageDriver.removeItem(key + TTL_SUFFIX);
delete this.data[key];
delete this.ttlData[key];
return true;
}
if (key === void 0) {
var keys = this.keys();
if (keys.length > 0 && keys[0] !== '') {
for (var i = 0; i < keys.length; i++) {
this.remove(keys[i]);
}
return true;
}
}
return false;
};
/**
* @locus Client
* @memberOf BrowserStorage
* @name escape
* @param {mix} val - The value to escape
* @summary Escape and stringify the value
* @returns {String}
*/
BrowserStorage.prototype.escape = function (val) {
return escape(helpers.escape(val));
};
/**
* @locus Client
* @memberOf BrowserStorage
* @name unescape
* @param {String} val - The string to unescape
* @summary Escape and restore original data-type of the value
* @returns {mix}
*/
BrowserStorage.prototype.unescape = function (val) {
return helpers.unescape(unescape(val));
};
/**
* @locus Client
* @memberOf BrowserStorage
* @name isSupported
* @summary Returns `true` is this storage driver is supported
* @returns {Boolean}
*/
BrowserStorage.isSupported = function () {
try {
if ('localStorage' in window && window.localStorage !== null) {
// Safari will throw an exception in Private mode
window.localStorage.setItem('___test___', 'test');
window.localStorage.removeItem('___test___');
return true;
}
return false;
} catch (e) {
return false;
}
};
module.exports = BrowserStorage;