-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
366 lines (314 loc) · 10.4 KB
/
index.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
(function (global) {
const isInWindow = typeof Window !== 'undefined'
const isInWorker = typeof WorkerGlobalScope !== 'undefined'
const isInSupportedPlatform = isInWindow || isInWorker
if (!global.STORAGE_BUCKETS_POLYFILL_DISABLE_SUPPORTED_PLATFORM_CHECK && !isInSupportedPlatform) {
throw new TypeError('Invalid calling environment')
}
const isInSecureContext = global.isSecureContext
if (!global.STORAGE_BUCKETS_POLYFILL_DISABLE_SECURE_CONTEXT_CHECK && !isInSecureContext) {
return
}
const isBuiltinSupported = typeof StorageBucketManager !== 'undefined' && typeof StorageBucket !== 'undefined'
if (!global.STORAGE_BUCKETS_POLYFILL_DISABLE_BUILTIN_CHECK && isBuiltinSupported) {
return
}
let allowConstruct = false
const MetaDataStorageKey = 'storage-buckets-polyfill'
const $createEntry = ({
name,
}) => {
return {
name,
// persisted: false,
// quota: 0,
// expires: 0,
indexdb: [],
cache: [],
// opfs: [],
}
}
const $readEntries = async () => {
const rootHandle = await global.navigator.storage.getDirectory()
const fileHandle = await rootHandle.getFileHandle(MetaDataStorageKey)
const file = await fileHandle.getFile()
const data = await file.text()
const entries = JSON.parse(data)
return entries
}
const $writeEntries = async (entries) => {
const rootHandle = await global.navigator.storage.getDirectory()
const fileHandle = await rootHandle.getFileHandle(MetaDataStorageKey, { create: true })
const writableStream = await fileHandle.createWritable({ keepExistingData: false, mode: 'exclusive' })
const data = JSON.stringify(entries)
await writableStream.write(data)
await writableStream.close()
}
/** @type {StorageBucketManager} */
const $StorageBucketManager = function StorageBucketManager() {
if (!allowConstruct) {
throw new TypeError('Illegal constructor')
}
}
Object.defineProperty($StorageBucketManager, 'name', {
configurable: true,
enumerable: false,
value: 'StorageBucketManager',
writable: false,
})
Object.defineProperty($StorageBucketManager.prototype, Symbol.toStringTag, {
configurable: true,
enumerable: false,
value: 'StorageBucketManager',
writable: false,
})
Object.defineProperty(global, 'StorageBucketManager', {
configurable: true,
enumerable: false,
value: $StorageBucketManager,
writable: true,
})
/** @type {StorageBucketManager['open']} */
const $open = async function (name) {
try {
if (Object.getPrototypeOf(this) !== $StorageBucketManager.prototype) {
throw new TypeError('Failed to execute \'open\' on \'StorageBucketManager\': Illegal invocation')
}
if (arguments.length === 0) {
throw new TypeError('Failed to execute \'open\' on \'StorageBucketManager\': 1 argument required, but only 0 present.')
}
const entries = await $readEntries()
if (entries[name] == null) {
entries[name] = $createEntry({
name,
})
await $writeEntries(entries)
}
} catch (error) {
if (error instanceof DOMException && error.name === 'NotFoundError') {
const entries = {}
entries[name] = $createEntry({
name,
}),
await $writeEntries(entries)
} else {
throw error
}
}
allowConstruct = true
const storageBucket = new $StorageBucket(name)
allowConstruct = false
return storageBucket
}
Object.defineProperty($open, 'name', {
configurable: true,
enumerable: false,
value: 'open',
writable: false,
})
Object.defineProperty($StorageBucketManager.prototype, 'open', {
configurable: true,
enumerable: true,
value: $open,
writable: true,
})
/** @type {StorageBucketManager['keys']} */
const $keys = async function () {
try {
if (Object.getPrototypeOf(this) !== $StorageBucketManager.prototype) {
throw new TypeError('Failed to execute \'keys\' on \'StorageBucketManager\': Illegal invocation')
}
const entries = await $readEntries()
const keys = Object.keys(entries)
return keys
} catch (error) {
if (error instanceof DOMException && error.name === 'NotFoundError') {
const keys = []
return keys
} else {
throw error
}
}
}
Object.defineProperty($keys, 'name', {
configurable: true,
enumerable: false,
value: 'keys',
writable: false,
})
Object.defineProperty($StorageBucketManager.prototype, 'keys', {
configurable: true,
enumerable: true,
value: $keys,
writable: true,
})
/** @type {StorageBucketManager['delete']} */
const $delete = async function (name) {
try {
if (Object.getPrototypeOf(this) !== $StorageBucketManager.prototype) {
throw new TypeError('Failed to execute \'delete\' on \'StorageBucketManager\': Illegal invocation')
}
if (arguments.length === 0) {
throw new TypeError('Failed to execute \'delete\' on \'StorageBucketManager\': 1 argument required, but only 0 present.')
}
const entries = await $readEntries()
const entry = entries[name]
if (entry == null) {
return
}
const { indexdb, cache, opfs } = entry
indexdb.forEach((el) => {
global.indexedDB.deleteDatabase(MetaDataStorageKey + name + el)
})
cache.forEach((el) => {
global.caches.delete(MetaDataStorageKey + name + el)
})
// opfs.forEach((el) => {
// rootHandle.removeEntry(MetaDataStorageKey + name + el)
// })
delete entries[name]
await $writeEntries(entries)
} catch (error) {
if (error instanceof DOMException && error.name === 'NotFoundError') {
return
} else {
throw error
}
}
}
Object.defineProperty($delete, 'name', {
configurable: true,
enumerable: false,
value: 'delete',
writable: false,
})
Object.defineProperty($StorageBucketManager.prototype, 'delete', {
configurable: true,
enumerable: true,
value: $delete,
writable: true,
})
const $$name = Symbol()
/** @type {StorageBucket} */
const $StorageBucket = function StorageBucket(name) {
if (!allowConstruct) {
throw new TypeError('Illegal constructor')
}
this[$$name] = name
}
Object.defineProperty($StorageBucket, 'name', {
configurable: true,
enumerable: false,
value: 'StorageBucket',
writable: false,
})
Object.defineProperty($StorageBucket.prototype, Symbol.toStringTag, {
configurable: true,
enumerable: false,
value: 'StorageBucket',
writable: false,
})
Object.defineProperty(global, 'StorageBucket', {
configurable: true,
enumerable: false,
value: $StorageBucket,
writable: true,
})
/** @type {() => StorageBucket['name']} */
const $name = function () {
if (Object.getPrototypeOf(this) !== $StorageBucket.prototype) {
throw new TypeError('Illegal invocation')
}
return this[$$name]
}
Object.defineProperty($StorageBucket.prototype, 'name', {
configurable: true,
enumerable: true,
get: $name,
set: undefined,
})
/** @type {() => StorageBucket['indexedDB']} */
const $indexedDB = function () {
const searchReg = new RegExp('^' + MetaDataStorageKey + this[$$name])
const $name = this[$$name]
return new Proxy(global.indexedDB, {
get: (target, p, receiver) => {
switch (p) {
case 'cmp':
return (first, second) => Reflect.get(target, 'cmp', receiver).call(target, first, second)
case 'databases':
return () => Reflect.get(target, 'databases', receiver).call(target).then((databases) => databases.filter((database) => database.name.startsWith(MetaDataStorageKey + $name)).map(({ name, version }) => ({ name: name.replace(searchReg, ''), version })))
case 'deleteDatabase':
return (name) => Reflect.get(target, 'deleteDatabase', receiver).call(target, MetaDataStorageKey + $name + name)
case 'open':
return (name, version) => Reflect.get(target, 'open', receiver).call(target, MetaDataStorageKey + $name + name, version)
}
},
})
}
Object.defineProperty($StorageBucket.prototype, 'indexedDB', {
configurable: true,
enumerable: true,
get: $indexedDB,
set: undefined,
})
/** @type {() => StorageBucket['caches']} */
const $caches = function () {
const searchReg = new RegExp('^' + MetaDataStorageKey + this[$$name])
const $name = this[$$name]
return new Proxy(global.caches, {
get: (target, p, receiver) => {
switch (p) {
case 'delete':
return (cacheName) => Reflect.get(target, 'delete', receiver).call(target, MetaDataStorageKey + $name + cacheName)
case 'has':
return (cacheName) => Reflect.get(target, 'has', receiver).call(target, MetaDataStorageKey + $name + cacheName)
case 'keys':
return () => Reflect.get(target, 'keys', receiver).call(target).then((keys) => keys.map(key => key.replace(searchReg, '')))
case 'match':
return (request, options) => Reflect.get(target, 'match', receiver).call(target, request, options)
case 'open':
return (cacheName) => Reflect.get(target, 'open', receiver).call(target, MetaDataStorageKey + $name + cacheName)
}
},
})
}
Object.defineProperty($StorageBucket.prototype, 'caches', {
configurable: true,
enumerable: true,
get: $caches,
set: undefined,
})
if (isInWindow) {
allowConstruct = true
const $storageBuckets = new $StorageBucketManager()
allowConstruct = false
Object.defineProperty(Navigator.prototype, 'storageBuckets', {
configurable: true,
enumerable: true,
get: function () {
if ((Object.getPrototypeOf(this) !== Navigator.prototype)) {
throw new TypeError('Illegal invocation')
}
return $storageBuckets
},
set: undefined,
})
}
if (isInWorker) {
allowConstruct = true
const $storageBuckets = new $StorageBucketManager()
allowConstruct = false
Object.defineProperty(WorkerNavigator.prototype, 'storageBuckets', {
configurable: true,
enumerable: true,
get: function () {
if ((Object.getPrototypeOf(this) !== WorkerNavigator.prototype)) {
throw new TypeError('Illegal invocation')
}
return $storageBuckets
},
set: undefined,
})
}
})(globalThis)