forked from binary-com/binary-static
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
136 lines (123 loc) · 3.77 KB
/
storage.js
File metadata and controls
136 lines (123 loc) · 3.77 KB
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
const Cookies = require('../../lib/js-cookie');
const isStorageSupported = function(storage) {
if (typeof storage === 'undefined') {
return false;
}
const testKey = 'test';
try {
storage.setItem(testKey, '1');
storage.removeItem(testKey);
return true;
} catch (e) {
return false;
}
};
const Store = function(storage) {
this.storage = storage;
};
Store.prototype = {
get: function(key) {
return this.storage.getItem(key) || undefined;
},
set: function(key, value) {
if (typeof value !== 'undefined') {
this.storage.setItem(key, value);
}
},
remove: function(key) {
this.storage.removeItem(key);
},
clear: function() {
this.storage.clear();
},
};
const InScriptStore = function(object) {
this.store = typeof object !== 'undefined' ? object : {};
};
InScriptStore.prototype = {
get : function(key) { return this.store[key]; },
set : function(key, value) { this.store[key] = value; },
remove: function(key) { delete this.store[key]; },
clear : function() { this.store = {}; },
has : function(key) { return this.get(key) !== undefined; },
keys : function() { return Object.keys(this.store); },
};
const State = new InScriptStore();
const CookieStorage = function (cookie_name, cookie_domain) {
this.initialized = false;
this.cookie_name = cookie_name;
const hostname = window.location.hostname;
this.domain = cookie_domain || (/\.binary\.com/i.test(hostname) ? '.' + hostname.split('.').slice(-2).join('.') : hostname);
this.path = '/';
this.expires = new Date('Thu, 1 Jan 2037 12:00:00 GMT');
this.value = {};
};
CookieStorage.prototype = {
read: function() {
const cookie_value = Cookies.get(this.cookie_name);
try {
this.value = cookie_value ? JSON.parse(cookie_value) : {};
} catch (e) {
this.value = {};
}
this.initialized = true;
},
write: function(value, expireDate, isSecure) {
if (!this.initialized) this.read();
this.value = value;
if (expireDate) this.expires = expireDate;
Cookies.set(this.cookie_name, this.value, {
expires: this.expires,
path : this.path,
domain : this.domain,
secure : !!isSecure,
});
},
get: function(key) {
if (!this.initialized) this.read();
return this.value[key];
},
set: function(key, value) {
if (!this.initialized) this.read();
this.value[key] = value;
Cookies.set(this.cookie_name, this.value, {
expires: new Date(this.expires),
path : this.path,
domain : this.domain,
});
},
remove: function() {
Cookies.remove(this.cookie_name, {
path : this.path,
domain: this.domain,
});
},
};
let SessionStore,
LocalStore;
if (typeof window !== 'undefined' && isStorageSupported(window.localStorage)) {
LocalStore = new Store(window.localStorage);
}
if (typeof window !== 'undefined' && isStorageSupported(window.sessionStorage)) {
if (!LocalStore) {
LocalStore = new Store(window.sessionStorage);
}
SessionStore = new Store(window.sessionStorage);
}
if (!SessionStore || !LocalStore) {
if (!LocalStore) {
LocalStore = new InScriptStore();
}
if (!SessionStore) {
SessionStore = new InScriptStore();
}
}
module.exports = {
isStorageSupported: isStorageSupported,
Store : Store,
InScriptStore : InScriptStore,
CookieStorage : CookieStorage,
State : State,
SessionStore : SessionStore,
LocalStore : LocalStore,
};