-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
cookies-storage.js
166 lines (151 loc) · 4.27 KB
/
cookies-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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var helpers = require('./helpers.js');
var DEFAULT_TTL = 3.154e+8; // 10 years
var TTL_SUFFIX = '.___exp';
/**
* @locus Client
* @class CookiesStorage
* @param cookieString {String} - Current cookies as String
* @summary Cookie-driven storage
*/
function CookiesStorage(clientStorage, cookieString) {
if (clientStorage) {
this.data = clientStorage.data;
this.ttlData = clientStorage.ttlData;
} else {
this.data = {};
this.ttlData = {};
}
if (cookieString && typeof cookieString === 'string') {
this.init(cookieString);
}
}
/**
* @locus Client
* @memberOf CookiesStorage
* @name init
* @param cookieString {String} - Current cookies as String
* @summary parse document.cookie string
* @returns {void 0}
*/
CookiesStorage.prototype.init = function (cookieString) {
if (typeof cookieString === 'string' && cookieString.length) {
var self = this;
var i;
var key;
var val;
cookieString.split(/; */).forEach(function (pair) {
i = pair.indexOf('=');
if (i < 0) {
return;
}
key = this.unescape(pair.substr(0, i).trim());
val = pair.substr(++i, pair.length).trim();
if (val[0] === '"') {
val = val.slice(1, -1);
}
if (self.data[key] === void 0) {
if (typeof key === 'string' && !!~key.indexOf(TTL_SUFFIX)) {
self.ttlData[key] = parseInt(val);
} else {
try {
self.data[key] = this.unescape(val);
} catch (e) {
self.data[key] = val;
}
}
}
});
}
};
/**
* @locus Client
* @memberOf CookiesStorage
* @name set
* @param {String} key - The name of the cookie to create/overwrite
* @param {String} value - The value of the cookie
* @param {Number} ttl - CookiesStorage TTL (e.g. max-age) in seconds
* @summary Create/overwrite a cookie.
* @returns {Boolean}
*/
CookiesStorage.prototype.set = function (key, value, _ttl) {
var ttl = _ttl;
if (!ttl || typeof ttl !== 'number') {
ttl = DEFAULT_TTL;
}
if (typeof key === 'string') {
document.cookie = this.escape(key) + '=' + this.escape(value) + '; Max-Age=' + ttl + '; Path=/';
this.data[key] = value;
this.ttlData[key] = Date.now() + (ttl * 1000);
document.cookie = this.escape(key) + TTL_SUFFIX + '=' + this.ttlData[key] + '; Max-Age=' + ttl + '; Path=/';
return true;
}
return false;
};
/**
* @locus Client
* @memberOf CookiesStorage
* @name remove
* @param {String} key - The name of the cookie to remove
* @summary Remove a cookie(s).
* @returns {Boolean}
*/
CookiesStorage.prototype.remove = function (key) {
if (typeof key === 'string' && this.data.hasOwnProperty(key)) {
delete this.data[key];
delete this.ttlData[key];
document.cookie = this.escape(key) + '=; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Path=/';
document.cookie = this.escape(key) + TTL_SUFFIX + '=; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Path=/';
return true;
}
if (key === void 0) {
var keys = Object.keys(this.data);
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 CookiesStorage
* @name escape
* @param {mix} val - The value to escape
* @summary Escape and stringify the value
* @returns {String}
*/
CookiesStorage.prototype.escape = function (val) {
return escape(helpers.escape(val));
};
/**
* @locus Client
* @memberOf CookiesStorage
* @name unescape
* @param {String} val - The string to unescape
* @summary Escape and restore original data-type of the value
* @returns {mix}
*/
CookiesStorage.prototype.unescape = function (val) {
return helpers.unescape(unescape(val));
};
/**
* @locus Client
* @memberOf CookiesStorage
* @name isSupported
* @summary Returns `true` is this storage driver is supported
* @returns {Boolean}
*/
CookiesStorage.isSupported = function () {
var result;
try {
document.cookie = '___isSupported___=value; Max-Age=' + DEFAULT_TTL + '; Path=/';
result = document.cookie.includes('___isSupported___');
document.cookie = '___isSupported___=; Expires=Thu, 01 Jan 1970 00:00:00 GMT; Path=/';
} catch (e) {
return false;
}
return result && navigator.cookieEnabled;
};
module.exports = CookiesStorage;