persistentStorage.js
3.38 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
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
const { encrypt, decrypt } = CryptoJS.AES
const { parse } = CryptoJS.enc.Utf8;
const pkcs7 = CryptoJS.pad.Pkcs7;
const ECB = CryptoJS.mode.ECB;
const UTF8 = CryptoJS.enc.Utf8;
const isDEV = location.href.includes('dev=1')
const cacheCipher = {
key: '_11111000001111@',
iv: '@11111000001111_',
}
const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7;
const isNullOrUnDef = (val) => typeof val === 'undefined' || val === null
class AesEncryption {
key;
iv;
constructor(opt = {}) {
const { key, iv } = opt;
if (key) {
this.key = parse(key);
}
if (iv) {
this.iv = parse(iv);
}
}
get getOptions() {
return {
mode: ECB,
padding: pkcs7,
iv: this.iv,
};
}
encryptByAES(cipherText) {
return encrypt(cipherText, this.key, this.getOptions).toString();
}
decryptByAES(cipherText) {
return decrypt(cipherText, this.key, this.getOptions).toString(UTF8);
}
}
const createStorage = (
{
prefixKey = '',
storage = sessionStorage,
key = cacheCipher.key,
iv = cacheCipher.iv,
timeout = null,
hasEncrypt = !isDEV,
} = {}) => {
if (hasEncrypt && [key.length, iv.length].some((item) => item !== 16)) {
throw new Error('When hasEncrypt is true, the key or iv must be 16 bits!');
}
const encryption = new AesEncryption({ key, iv });
/**
*Cache class
*Construction parameters can be passed into sessionStorage, localStorage,
* @class Cache
* @example
*/
const WebStorage = class WebStorage {
storage
prefixKey
encryption
hasEncrypt
/**
*
* @param {*} storage
*/
constructor() {
this.storage = storage;
this.prefixKey = prefixKey;
this.encryption = encryption;
this.hasEncrypt = hasEncrypt;
}
getKey(key) {
return `${this.prefixKey}${key}`.toUpperCase();
}
/**
*
* Set cache
* @param {string} key
* @param {*} value
* @param {*} expire
* @expire Expiration time in seconds
* @memberof Cache
*/
set(key, value, expire = timeout) {
const stringData = JSON.stringify({
value,
time: Date.now(),
expire: !isNullOrUnDef(expire) ? new Date().getTime() + expire * 1000 : null,
});
const stringifyValue = this.hasEncrypt
? this.encryption.encryptByAES(stringData)
: stringData;
this.storage.setItem(this.getKey(key), stringifyValue);
}
generatorValue(key, value, expire = timeout) {
const stringData = {
value,
time: Date.now(),
expire: !isNullOrUnDef(expire) ? new Date().getTime() + expire * 1000 : null,
};
const stringifyValue = this.hasEncrypt
? this.encryption.encryptByAES(stringData)
: stringData;
return stringifyValue
}
/**
*Read cache
* @param {string} key
* @param {any} [def=null] def
* @memberof Cache
*/
get(key, def = null) {
const val = this.storage.getItem(this.getKey(key));
if (!val) return def;
try {
const decVal = this.hasEncrypt ? this.encryption.decryptByAES(val) : val;
const data = JSON.parse(decVal);
const { value, expire } = data;
// if (isNullOrUnDef(expire) || expire >= new Date().getTime()) {
// return value;
// }
return value
// this.remove(key);
} catch (e) {
return def;
}
}
/**
* Delete cache based on key
* @param {string} key
* @memberof Cache
*/
remove(key) {
this.storage.removeItem(this.getKey(key));
}
/**
* Delete all caches of this instance
*/
clear() {
this.storage.clear();
}
};
return new WebStorage();
}