-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
224 lines (191 loc) · 6.48 KB
/
storage.js
File metadata and controls
224 lines (191 loc) · 6.48 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
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
(function () {
const {readyDOM} = window.utils;
const storage = {
_prefix: 'storage-',
_naming: {
indexes: 'storageIndexes',
},
_version: 2,
_sizeLimit: 1e6,
_selfCheck: true,
_autoSave: true,
_canOverwrite: true,
_saveTimeout: 5e3,
_deleteCorrupted: true,
_storage: new Map(),
_saveQueue: null,
/**
* Create file record in storage
* @param name {string} Name of the file
* @param content {string} Should be limited by size limit (1000000 by default)
* */
createFile(name, content) {
const storageName = `${this._prefix}${this._storage.size}-${Date.now()}`;
if (content.length > this._sizeLimit) {
throw `File content length must be less ${this._sizeLimit}`;
}
if (this._canOverwrite || !this._storage.has(name)) {
localStorage.setItem(storageName, content);
} else {
throw `File with name "${name}" is already exists`;
}
if (this._selfCheck && localStorage.getItem(storageName) == null) {
throw `Error white writing "${name}" in storage`;
} else {
this._storage.set(name, storageName);
}
if (this._autoSave) {
this.saveIndexes();
}
},
/**
* Delete file record from storage
* @param name {string} Name of the file
*/
deleteFile(name) {
const storageName = this._storage.get(name);
if (storageName != null) {
localStorage.removeItem(storageName);
} else {
return;
}
if (this._selfCheck && localStorage.getItem(storageName) != null) {
throw `Error white erasing "${name}" from storage`;
} else {
this._storage.delete(name);
}
if (this._autoSave) {
this.saveIndexes();
}
},
/**
* Returns file content if it's exist
* @param name {string}
*/
get(name) {
const storageName = this._storage.get(name);
if (storageName == null) {
return null;
}
const content = localStorage.getItem(storageName);
if (this._selfCheck && content == null) {
throw new ReferenceError(`Error while reading "${name}" from storage`);
}
return content;
},
/**
* Generate DOM Node for embedding
* @param name {string}
* @param type {string}
*/
prepareDOMNode(name, type) {
const content = this.get(name);
let blob, element;
switch (type) {
case 'script': {
blob = new Blob([content], {type: 'text/javascript'});
element = document.createElement('script');
element.src = URL.createObjectURL(blob);
element.dataset.name = name;
readyDOM(() => {
document.head.appendChild(element);
});
break;
}
case 'style': {
blob = new Blob([content], {type: 'text/css'});
element = document.createElement('link');
element.href = URL.createObjectURL(blob);
element.rel = 'stylesheet';
element.dataset.name = name;
readyDOM(() => {
document.head.appendChild(element);
});
break;
}
case 'html':
default: {
element = document.createElement('div');
element.dataset.name = name;
element.innerHTML = content;
readyDOM(() => {
document.body.appendChild(element);
});
break;
}
}
},
/**
* @param name {string}
* @return {boolean}
*/
isExists(name) {
return this._storage.has(name);
},
/**
* Load storage indexes
* @private
*/
_loadIndexes() {
const storageIndexesJSON = localStorage.getItem(this._naming.indexes);
let storageIndexes;
try {
storageIndexes = Object.entries(JSON.parse(storageIndexesJSON ?? '{}'));
} catch (e) {
throw `Error while reading storage config`;
}
if (storageIndexes == null) {
return;
}
this._storage = new Map(storageIndexes);
if (this._selfCheck) {
const failed = this._checkIndexes();
if (this._deleteCorrupted) {
failed.forEach((file) => {
this.deleteFile(file);
})
}
}
},
/**
* Save storage indexes with queue delay
*/
saveIndexes(timeout = this._saveTimeout) {
if (this._saveQueue != null) {
return;
}
const timeoutFn = () => {
this._saveQueue = null;
this._saveIndexes();
}
setTimeout(timeoutFn, timeout);
},
/**
* Save storage indexes
*/
_saveIndexes() {
const indexObject = Object.fromEntries(this._storage.entries());
const json = JSON.stringify(indexObject);
localStorage.setItem(this._naming.indexes, json);
},
/**
* Check storage indexes
* @return {string[]}
* @private
*/
_checkIndexes() {
const failed = [];
const listed = new Set();
for (const [name, storageName] of this._storage.entries()) {
if (localStorage.getItem(storageName) == null || listed.has(storageName)) {
failed.push(name);
}
listed.add(storageName);
}
return failed;
},
}
storage._loadIndexes();
window.addEventListener('beforeunload', storage._saveIndexes.bind(storage));
window.storage = storage;
})()