@lvce-editor/cache-worker 0.1.3 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -3
- package/cacheWorkerMain.js +632 -320
- package/index.js +3 -4
- package/package.json +1 -1
package/cacheWorkerMain.js
CHANGED
|
@@ -1,27 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
var __export = (target, all) => {
|
|
3
|
-
for (var name in all)
|
|
4
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
5
|
-
};
|
|
6
|
-
|
|
7
|
-
// node_modules/@lvce-editor/ipc/dist/index.js
|
|
8
|
-
var isMessagePort = (value) => {
|
|
1
|
+
const isMessagePort = value => {
|
|
9
2
|
return value && value instanceof MessagePort;
|
|
10
3
|
};
|
|
11
|
-
|
|
12
|
-
return value && value.constructor && value.constructor.name ===
|
|
4
|
+
const isMessagePortMain = value => {
|
|
5
|
+
return value && value.constructor && value.constructor.name === 'MessagePortMain';
|
|
13
6
|
};
|
|
14
|
-
|
|
15
|
-
return typeof OffscreenCanvas !==
|
|
7
|
+
const isOffscreenCanvas = value => {
|
|
8
|
+
return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
|
|
16
9
|
};
|
|
17
|
-
|
|
10
|
+
const isInstanceOf = (value, constructorName) => {
|
|
18
11
|
return value?.constructor?.name === constructorName;
|
|
19
12
|
};
|
|
20
|
-
|
|
21
|
-
return isInstanceOf(value,
|
|
13
|
+
const isSocket = value => {
|
|
14
|
+
return isInstanceOf(value, 'Socket');
|
|
22
15
|
};
|
|
23
|
-
|
|
24
|
-
|
|
16
|
+
const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
|
|
17
|
+
const isTransferrable = value => {
|
|
25
18
|
for (const fn of transferrables) {
|
|
26
19
|
if (fn(value)) {
|
|
27
20
|
return true;
|
|
@@ -29,64 +22,64 @@ var isTransferrable = (value) => {
|
|
|
29
22
|
}
|
|
30
23
|
return false;
|
|
31
24
|
};
|
|
32
|
-
|
|
25
|
+
const walkValue = (value, transferrables, isTransferrable) => {
|
|
33
26
|
if (!value) {
|
|
34
27
|
return;
|
|
35
28
|
}
|
|
36
|
-
if (
|
|
37
|
-
|
|
29
|
+
if (isTransferrable(value)) {
|
|
30
|
+
transferrables.push(value);
|
|
38
31
|
return;
|
|
39
32
|
}
|
|
40
33
|
if (Array.isArray(value)) {
|
|
41
34
|
for (const item of value) {
|
|
42
|
-
walkValue(item,
|
|
35
|
+
walkValue(item, transferrables, isTransferrable);
|
|
43
36
|
}
|
|
44
37
|
return;
|
|
45
38
|
}
|
|
46
|
-
if (typeof value ===
|
|
39
|
+
if (typeof value === 'object') {
|
|
47
40
|
for (const property of Object.values(value)) {
|
|
48
|
-
walkValue(property,
|
|
41
|
+
walkValue(property, transferrables, isTransferrable);
|
|
49
42
|
}
|
|
50
43
|
}
|
|
51
44
|
};
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
walkValue(value,
|
|
55
|
-
return
|
|
45
|
+
const getTransferrables = value => {
|
|
46
|
+
const transferrables = [];
|
|
47
|
+
walkValue(value, transferrables, isTransferrable);
|
|
48
|
+
return transferrables;
|
|
56
49
|
};
|
|
57
|
-
|
|
58
|
-
const
|
|
50
|
+
const attachEvents = that => {
|
|
51
|
+
const handleMessage = (...args) => {
|
|
59
52
|
const data = that.getData(...args);
|
|
60
|
-
that.dispatchEvent(new MessageEvent(
|
|
53
|
+
that.dispatchEvent(new MessageEvent('message', {
|
|
61
54
|
data
|
|
62
55
|
}));
|
|
63
56
|
};
|
|
64
|
-
that.onMessage(
|
|
65
|
-
const handleClose =
|
|
66
|
-
that.dispatchEvent(new Event(
|
|
57
|
+
that.onMessage(handleMessage);
|
|
58
|
+
const handleClose = event => {
|
|
59
|
+
that.dispatchEvent(new Event('close'));
|
|
67
60
|
};
|
|
68
61
|
that.onClose(handleClose);
|
|
69
62
|
};
|
|
70
|
-
|
|
63
|
+
class Ipc extends EventTarget {
|
|
71
64
|
constructor(rawIpc) {
|
|
72
65
|
super();
|
|
73
66
|
this._rawIpc = rawIpc;
|
|
74
67
|
attachEvents(this);
|
|
75
68
|
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
69
|
+
}
|
|
70
|
+
const readyMessage = 'ready';
|
|
71
|
+
const getData$2 = event => {
|
|
79
72
|
return event.data;
|
|
80
73
|
};
|
|
81
|
-
|
|
74
|
+
const listen$8 = ({
|
|
82
75
|
port
|
|
83
76
|
}) => {
|
|
84
77
|
return port;
|
|
85
78
|
};
|
|
86
|
-
|
|
79
|
+
const signal$9 = port => {
|
|
87
80
|
port.postMessage(readyMessage);
|
|
88
81
|
};
|
|
89
|
-
|
|
82
|
+
class IpcChildWithMessagePort extends Ipc {
|
|
90
83
|
getData(event) {
|
|
91
84
|
return getData$2(event);
|
|
92
85
|
}
|
|
@@ -98,76 +91,82 @@ var IpcChildWithMessagePort = class extends Ipc {
|
|
|
98
91
|
this._rawIpc.postMessage(message, transfer);
|
|
99
92
|
}
|
|
100
93
|
dispose() {
|
|
94
|
+
// ignore
|
|
101
95
|
}
|
|
102
96
|
onClose(callback) {
|
|
97
|
+
// ignore
|
|
103
98
|
}
|
|
104
99
|
onMessage(callback) {
|
|
105
|
-
this._rawIpc.addEventListener(
|
|
100
|
+
this._rawIpc.addEventListener('message', callback);
|
|
106
101
|
this._rawIpc.start();
|
|
107
102
|
}
|
|
108
|
-
}
|
|
109
|
-
|
|
103
|
+
}
|
|
104
|
+
const wrap$g = port => {
|
|
110
105
|
return new IpcChildWithMessagePort(port);
|
|
111
106
|
};
|
|
112
|
-
|
|
107
|
+
const IpcChildWithMessagePort$1 = {
|
|
113
108
|
__proto__: null,
|
|
114
109
|
listen: listen$8,
|
|
115
110
|
signal: signal$9,
|
|
116
111
|
wrap: wrap$g
|
|
117
112
|
};
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
113
|
+
const listen$7 = () => {
|
|
114
|
+
// @ts-ignore
|
|
115
|
+
if (typeof WorkerGlobalScope === 'undefined') {
|
|
116
|
+
throw new TypeError('module is not in web worker scope');
|
|
121
117
|
}
|
|
122
118
|
return globalThis;
|
|
123
119
|
};
|
|
124
|
-
|
|
120
|
+
const signal$8 = global => {
|
|
125
121
|
global.postMessage(readyMessage);
|
|
126
122
|
};
|
|
127
|
-
|
|
123
|
+
class IpcChildWithModuleWorker extends Ipc {
|
|
128
124
|
getData(event) {
|
|
129
125
|
return getData$2(event);
|
|
130
126
|
}
|
|
131
127
|
send(message) {
|
|
128
|
+
// @ts-ignore
|
|
132
129
|
this._rawIpc.postMessage(message);
|
|
133
130
|
}
|
|
134
131
|
sendAndTransfer(message) {
|
|
135
132
|
const transfer = getTransferrables(message);
|
|
133
|
+
// @ts-ignore
|
|
136
134
|
this._rawIpc.postMessage(message, transfer);
|
|
137
135
|
}
|
|
138
136
|
dispose() {
|
|
137
|
+
// ignore
|
|
139
138
|
}
|
|
140
139
|
onClose(callback) {
|
|
140
|
+
// ignore
|
|
141
141
|
}
|
|
142
142
|
onMessage(callback) {
|
|
143
|
-
this._rawIpc.addEventListener(
|
|
143
|
+
this._rawIpc.addEventListener('message', callback);
|
|
144
144
|
}
|
|
145
|
-
}
|
|
146
|
-
|
|
145
|
+
}
|
|
146
|
+
const wrap$f = global => {
|
|
147
147
|
return new IpcChildWithModuleWorker(global);
|
|
148
148
|
};
|
|
149
|
-
|
|
149
|
+
const IpcChildWithModuleWorker$1 = {
|
|
150
150
|
__proto__: null,
|
|
151
151
|
listen: listen$7,
|
|
152
152
|
signal: signal$8,
|
|
153
153
|
wrap: wrap$f
|
|
154
154
|
};
|
|
155
155
|
|
|
156
|
-
|
|
157
|
-
var CommandNotFoundError = class extends Error {
|
|
156
|
+
class CommandNotFoundError extends Error {
|
|
158
157
|
constructor(command) {
|
|
159
158
|
super(`Command not found ${command}`);
|
|
160
|
-
this.name =
|
|
159
|
+
this.name = 'CommandNotFoundError';
|
|
161
160
|
}
|
|
161
|
+
}
|
|
162
|
+
const commands = Object.create(null);
|
|
163
|
+
const register = commandMap => {
|
|
164
|
+
Object.assign(commands, commandMap);
|
|
162
165
|
};
|
|
163
|
-
|
|
164
|
-
var register = (commandMap2) => {
|
|
165
|
-
Object.assign(commands, commandMap2);
|
|
166
|
-
};
|
|
167
|
-
var getCommand = (key) => {
|
|
166
|
+
const getCommand = key => {
|
|
168
167
|
return commands[key];
|
|
169
168
|
};
|
|
170
|
-
|
|
169
|
+
const execute = (command, ...args) => {
|
|
171
170
|
const fn = getCommand(command);
|
|
172
171
|
if (!fn) {
|
|
173
172
|
throw new CommandNotFoundError(command);
|
|
@@ -175,27 +174,26 @@ var execute = (command, ...args) => {
|
|
|
175
174
|
return fn(...args);
|
|
176
175
|
};
|
|
177
176
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
return callbacks[id2];
|
|
177
|
+
const Two$1 = '2.0';
|
|
178
|
+
const callbacks = Object.create(null);
|
|
179
|
+
const get = id => {
|
|
180
|
+
return callbacks[id];
|
|
183
181
|
};
|
|
184
|
-
|
|
185
|
-
delete callbacks[
|
|
182
|
+
const remove = id => {
|
|
183
|
+
delete callbacks[id];
|
|
186
184
|
};
|
|
187
|
-
|
|
185
|
+
class JsonRpcError extends Error {
|
|
188
186
|
constructor(message) {
|
|
189
187
|
super(message);
|
|
190
|
-
this.name =
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
188
|
+
this.name = 'JsonRpcError';
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
const NewLine = '\n';
|
|
192
|
+
const DomException = 'DOMException';
|
|
193
|
+
const ReferenceError$1 = 'ReferenceError';
|
|
194
|
+
const SyntaxError$1 = 'SyntaxError';
|
|
195
|
+
const TypeError$1 = 'TypeError';
|
|
196
|
+
const getErrorConstructor = (message, type) => {
|
|
199
197
|
if (type) {
|
|
200
198
|
switch (type) {
|
|
201
199
|
case DomException:
|
|
@@ -210,26 +208,26 @@ var getErrorConstructor = (message, type) => {
|
|
|
210
208
|
return Error;
|
|
211
209
|
}
|
|
212
210
|
}
|
|
213
|
-
if (message.startsWith(
|
|
211
|
+
if (message.startsWith('TypeError: ')) {
|
|
214
212
|
return TypeError;
|
|
215
213
|
}
|
|
216
|
-
if (message.startsWith(
|
|
214
|
+
if (message.startsWith('SyntaxError: ')) {
|
|
217
215
|
return SyntaxError;
|
|
218
216
|
}
|
|
219
|
-
if (message.startsWith(
|
|
217
|
+
if (message.startsWith('ReferenceError: ')) {
|
|
220
218
|
return ReferenceError;
|
|
221
219
|
}
|
|
222
220
|
return Error;
|
|
223
221
|
};
|
|
224
|
-
|
|
222
|
+
const constructError = (message, type, name) => {
|
|
225
223
|
const ErrorConstructor = getErrorConstructor(message, type);
|
|
226
224
|
if (ErrorConstructor === DOMException && name) {
|
|
227
225
|
return new ErrorConstructor(message, name);
|
|
228
226
|
}
|
|
229
227
|
if (ErrorConstructor === Error) {
|
|
230
228
|
const error = new Error(message);
|
|
231
|
-
if (name && name !==
|
|
232
|
-
Object.defineProperty(error,
|
|
229
|
+
if (name && name !== 'VError') {
|
|
230
|
+
Object.defineProperty(error, 'name', {
|
|
233
231
|
configurable: true,
|
|
234
232
|
value: name
|
|
235
233
|
});
|
|
@@ -238,33 +236,33 @@ var constructError = (message, type, name) => {
|
|
|
238
236
|
}
|
|
239
237
|
return new ErrorConstructor(message);
|
|
240
238
|
};
|
|
241
|
-
|
|
239
|
+
const joinLines = lines => {
|
|
242
240
|
return lines.join(NewLine);
|
|
243
241
|
};
|
|
244
|
-
|
|
242
|
+
const splitLines = lines => {
|
|
245
243
|
return lines.split(NewLine);
|
|
246
244
|
};
|
|
247
|
-
|
|
245
|
+
const getCurrentStack = () => {
|
|
248
246
|
const stackLinesToSkip = 3;
|
|
249
|
-
const currentStack = joinLines(splitLines(new Error().stack ||
|
|
247
|
+
const currentStack = joinLines(splitLines(new Error().stack || '').slice(stackLinesToSkip));
|
|
250
248
|
return currentStack;
|
|
251
249
|
};
|
|
252
|
-
|
|
250
|
+
const getNewLineIndex = (string, startIndex) => {
|
|
253
251
|
{
|
|
254
|
-
return
|
|
252
|
+
return string.indexOf(NewLine);
|
|
255
253
|
}
|
|
256
254
|
};
|
|
257
|
-
|
|
258
|
-
let parentStack = error.stack || error.data || error.message ||
|
|
259
|
-
if (parentStack.startsWith(
|
|
255
|
+
const getParentStack = error => {
|
|
256
|
+
let parentStack = error.stack || error.data || error.message || '';
|
|
257
|
+
if (parentStack.startsWith(' at')) {
|
|
260
258
|
parentStack = error.message + NewLine + parentStack;
|
|
261
259
|
}
|
|
262
260
|
return parentStack;
|
|
263
261
|
};
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
const descriptor = Object.getOwnPropertyDescriptor(error,
|
|
262
|
+
const MethodNotFound = -32601;
|
|
263
|
+
const Custom = -32001;
|
|
264
|
+
const setStack = (error, stack) => {
|
|
265
|
+
const descriptor = Object.getOwnPropertyDescriptor(error, 'stack');
|
|
268
266
|
if (descriptor) {
|
|
269
267
|
if (!descriptor.configurable && !descriptor.writable) {
|
|
270
268
|
return;
|
|
@@ -274,19 +272,19 @@ var setStack = (error, stack) => {
|
|
|
274
272
|
return;
|
|
275
273
|
}
|
|
276
274
|
}
|
|
277
|
-
Object.defineProperty(error,
|
|
275
|
+
Object.defineProperty(error, 'stack', {
|
|
278
276
|
configurable: true,
|
|
279
277
|
value: stack,
|
|
280
278
|
writable: true
|
|
281
279
|
});
|
|
282
280
|
};
|
|
283
|
-
|
|
284
|
-
if (typeof error.stack ===
|
|
281
|
+
const restoreExistingError = (error, currentStack) => {
|
|
282
|
+
if (typeof error.stack === 'string') {
|
|
285
283
|
setStack(error, `${error.stack}${NewLine}${currentStack}`);
|
|
286
284
|
}
|
|
287
285
|
return error;
|
|
288
286
|
};
|
|
289
|
-
|
|
287
|
+
const restoreMethodNotFoundError = (error, currentStack) => {
|
|
290
288
|
const restoredError = new JsonRpcError(error.message);
|
|
291
289
|
Object.assign(restoredError, {
|
|
292
290
|
code: error.code
|
|
@@ -295,7 +293,7 @@ var restoreMethodNotFoundError = (error, currentStack) => {
|
|
|
295
293
|
setStack(restoredError, `${parentStack}${NewLine}${currentStack}`);
|
|
296
294
|
return restoredError;
|
|
297
295
|
};
|
|
298
|
-
|
|
296
|
+
const restoreStackFromData = (restoredError, error, currentStack) => {
|
|
299
297
|
if (error.data.stack && error.data.type && error.message) {
|
|
300
298
|
setStack(restoredError, `${error.data.type}: ${error.message}${NewLine}${error.data.stack}${NewLine}${currentStack}`);
|
|
301
299
|
return;
|
|
@@ -304,37 +302,42 @@ var restoreStackFromData = (restoredError, error, currentStack) => {
|
|
|
304
302
|
setStack(restoredError, error.data.stack);
|
|
305
303
|
}
|
|
306
304
|
};
|
|
307
|
-
|
|
305
|
+
const applyDataProperties = (restoredError, error) => {
|
|
308
306
|
restoreStackFromData(restoredError, error, getCurrentStack());
|
|
309
307
|
if (error.data.codeFrame) {
|
|
308
|
+
// @ts-ignore
|
|
310
309
|
restoredError.codeFrame = error.data.codeFrame;
|
|
311
310
|
}
|
|
312
|
-
if (typeof error.data.code ===
|
|
313
|
-
|
|
311
|
+
if (typeof error.data.code === 'string' || typeof error.data.code === 'number') {
|
|
312
|
+
// @ts-ignore
|
|
313
|
+
Object.defineProperty(restoredError, 'code', {
|
|
314
314
|
configurable: true,
|
|
315
315
|
value: error.data.code,
|
|
316
316
|
writable: true
|
|
317
317
|
});
|
|
318
318
|
}
|
|
319
319
|
if (error.data.type) {
|
|
320
|
+
// @ts-ignore
|
|
320
321
|
restoredError.name = error.data.type;
|
|
321
322
|
}
|
|
322
323
|
};
|
|
323
|
-
|
|
324
|
+
const applyDirectProperties = (restoredError, error) => {
|
|
324
325
|
if (error.stack) {
|
|
325
|
-
const lowerStack = restoredError.stack ||
|
|
326
|
+
const lowerStack = restoredError.stack || '';
|
|
326
327
|
const indexNewLine = getNewLineIndex(lowerStack);
|
|
327
328
|
const parentStack = getParentStack(error);
|
|
329
|
+
// @ts-ignore
|
|
328
330
|
setStack(restoredError, `${parentStack}${lowerStack.slice(indexNewLine)}`);
|
|
329
331
|
}
|
|
330
332
|
if (error.codeFrame) {
|
|
333
|
+
// @ts-ignore
|
|
331
334
|
restoredError.codeFrame = error.codeFrame;
|
|
332
335
|
}
|
|
333
336
|
};
|
|
334
|
-
|
|
337
|
+
const restoreMessageError = (error, _currentStack) => {
|
|
335
338
|
const restoredError = constructError(error.message, error.type || error.data?.type, error.name);
|
|
336
|
-
if (typeof error.code ===
|
|
337
|
-
Object.defineProperty(restoredError,
|
|
339
|
+
if (typeof error.code === 'string' || typeof error.code === 'number') {
|
|
340
|
+
Object.defineProperty(restoredError, 'code', {
|
|
338
341
|
configurable: true,
|
|
339
342
|
value: error.code,
|
|
340
343
|
writable: true
|
|
@@ -347,15 +350,15 @@ var restoreMessageError = (error, _currentStack) => {
|
|
|
347
350
|
}
|
|
348
351
|
return restoredError;
|
|
349
352
|
};
|
|
350
|
-
|
|
351
|
-
if (error && typeof error.message ===
|
|
353
|
+
const getFallbackMessage = error => {
|
|
354
|
+
if (error && typeof error.message === 'string' && error.message) {
|
|
352
355
|
return error.message;
|
|
353
356
|
}
|
|
354
357
|
if (error && error.data) {
|
|
355
358
|
return getFallbackMessage(error.data);
|
|
356
359
|
}
|
|
357
360
|
const stringified = String(error);
|
|
358
|
-
if (stringified !==
|
|
361
|
+
if (stringified !== '[object Object]') {
|
|
359
362
|
return stringified;
|
|
360
363
|
}
|
|
361
364
|
try {
|
|
@@ -364,7 +367,7 @@ var getFallbackMessage = (error) => {
|
|
|
364
367
|
return stringified;
|
|
365
368
|
}
|
|
366
369
|
};
|
|
367
|
-
|
|
370
|
+
const restoreJsonRpcError = error => {
|
|
368
371
|
const currentStack = getCurrentStack();
|
|
369
372
|
if (error && error instanceof Error) {
|
|
370
373
|
return restoreExistingError(error, currentStack);
|
|
@@ -380,51 +383,51 @@ var restoreJsonRpcError = (error) => {
|
|
|
380
383
|
}
|
|
381
384
|
return new Error(`JsonRpc Error: ${getFallbackMessage(error)}`);
|
|
382
385
|
};
|
|
383
|
-
|
|
384
|
-
if (
|
|
386
|
+
const unwrapJsonRpcResult = responseMessage => {
|
|
387
|
+
if ('error' in responseMessage) {
|
|
385
388
|
const restoredError = restoreJsonRpcError(responseMessage.error);
|
|
386
389
|
throw restoredError;
|
|
387
390
|
}
|
|
388
|
-
if (
|
|
391
|
+
if ('result' in responseMessage) {
|
|
389
392
|
return responseMessage.result;
|
|
390
393
|
}
|
|
391
|
-
throw new JsonRpcError(
|
|
394
|
+
throw new JsonRpcError('unexpected response message');
|
|
392
395
|
};
|
|
393
|
-
|
|
396
|
+
const warn = (...args) => {
|
|
394
397
|
console.warn(...args);
|
|
395
398
|
};
|
|
396
|
-
|
|
397
|
-
const fn = get(
|
|
399
|
+
const resolve = (id, response) => {
|
|
400
|
+
const fn = get(id);
|
|
398
401
|
if (!fn) {
|
|
399
402
|
console.log(response);
|
|
400
|
-
warn(`callback ${
|
|
403
|
+
warn(`callback ${id} may already be disposed`);
|
|
401
404
|
return;
|
|
402
405
|
}
|
|
403
406
|
fn(response);
|
|
404
|
-
remove(
|
|
407
|
+
remove(id);
|
|
405
408
|
};
|
|
406
|
-
|
|
407
|
-
|
|
409
|
+
const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
|
|
410
|
+
const getErrorType = prettyError => {
|
|
408
411
|
if (prettyError && prettyError.type) {
|
|
409
412
|
return prettyError.type;
|
|
410
413
|
}
|
|
411
414
|
if (prettyError && prettyError.constructor && prettyError.constructor.name) {
|
|
412
415
|
return prettyError.constructor.name;
|
|
413
416
|
}
|
|
414
|
-
return
|
|
417
|
+
return undefined;
|
|
415
418
|
};
|
|
416
|
-
|
|
417
|
-
return line.trim().startsWith(
|
|
419
|
+
const isAlreadyStack = line => {
|
|
420
|
+
return line.trim().startsWith('at ');
|
|
418
421
|
};
|
|
419
|
-
|
|
420
|
-
const stackString = prettyError.stack ||
|
|
421
|
-
const newLineIndex = stackString.indexOf(
|
|
422
|
+
const getStack = prettyError => {
|
|
423
|
+
const stackString = prettyError.stack || '';
|
|
424
|
+
const newLineIndex = stackString.indexOf('\n');
|
|
422
425
|
if (newLineIndex !== -1 && !isAlreadyStack(stackString.slice(0, newLineIndex))) {
|
|
423
426
|
return stackString.slice(newLineIndex + 1);
|
|
424
427
|
}
|
|
425
428
|
return stackString;
|
|
426
429
|
};
|
|
427
|
-
|
|
430
|
+
const getErrorProperty = (error, prettyError) => {
|
|
428
431
|
if (error && error.code === E_COMMAND_NOT_FOUND) {
|
|
429
432
|
return {
|
|
430
433
|
code: MethodNotFound,
|
|
@@ -444,68 +447,71 @@ var getErrorProperty = (error, prettyError) => {
|
|
|
444
447
|
message: prettyError.message
|
|
445
448
|
};
|
|
446
449
|
};
|
|
447
|
-
|
|
450
|
+
const create$1$1 = (id, error) => {
|
|
448
451
|
return {
|
|
449
452
|
error,
|
|
450
|
-
id
|
|
451
|
-
jsonrpc: Two
|
|
453
|
+
id,
|
|
454
|
+
jsonrpc: Two$1
|
|
452
455
|
};
|
|
453
456
|
};
|
|
454
|
-
|
|
455
|
-
const prettyError =
|
|
456
|
-
|
|
457
|
+
const getErrorResponse = (id, error, preparePrettyError, logError) => {
|
|
458
|
+
const prettyError = preparePrettyError(error);
|
|
459
|
+
logError(error, prettyError);
|
|
457
460
|
const errorProperty = getErrorProperty(error, prettyError);
|
|
458
|
-
return create$1(
|
|
461
|
+
return create$1$1(id, errorProperty);
|
|
459
462
|
};
|
|
460
|
-
|
|
463
|
+
const create$5 = (message, result) => {
|
|
461
464
|
return {
|
|
462
465
|
id: message.id,
|
|
463
|
-
jsonrpc: Two,
|
|
466
|
+
jsonrpc: Two$1,
|
|
464
467
|
result: result ?? null
|
|
465
468
|
};
|
|
466
469
|
};
|
|
467
|
-
|
|
470
|
+
const getSuccessResponse = (message, result) => {
|
|
468
471
|
const resultProperty = result ?? null;
|
|
469
|
-
return create(message, resultProperty);
|
|
472
|
+
return create$5(message, resultProperty);
|
|
470
473
|
};
|
|
471
|
-
|
|
474
|
+
const getErrorResponseSimple = (id, error) => {
|
|
472
475
|
return {
|
|
473
476
|
error: {
|
|
474
477
|
code: Custom,
|
|
475
478
|
data: error instanceof Error ? {
|
|
476
479
|
...error,
|
|
477
|
-
code:
|
|
480
|
+
code: 'code' in error ? error.code : undefined,
|
|
478
481
|
stack: error.stack,
|
|
479
482
|
type: error.name
|
|
480
483
|
} : error,
|
|
481
484
|
// @ts-ignore
|
|
482
485
|
message: error.message
|
|
483
486
|
},
|
|
484
|
-
id
|
|
485
|
-
jsonrpc: Two
|
|
487
|
+
id,
|
|
488
|
+
jsonrpc: Two$1
|
|
486
489
|
};
|
|
487
490
|
};
|
|
488
|
-
|
|
491
|
+
const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
|
|
489
492
|
try {
|
|
490
|
-
const result =
|
|
493
|
+
const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
|
|
491
494
|
return getSuccessResponse(message, result);
|
|
492
495
|
} catch (error) {
|
|
493
496
|
if (ipc.canUseSimpleErrorResponse) {
|
|
494
497
|
return getErrorResponseSimple(message.id, error);
|
|
495
498
|
}
|
|
496
|
-
return getErrorResponse(message.id, error,
|
|
499
|
+
return getErrorResponse(message.id, error, preparePrettyError, logError);
|
|
497
500
|
}
|
|
498
501
|
};
|
|
499
|
-
|
|
502
|
+
const defaultPreparePrettyError = error => {
|
|
500
503
|
return error;
|
|
501
504
|
};
|
|
502
|
-
|
|
505
|
+
const defaultLogError = () => {
|
|
506
|
+
// ignore
|
|
503
507
|
};
|
|
504
|
-
|
|
508
|
+
const defaultRequiresSocket = () => {
|
|
505
509
|
return false;
|
|
506
510
|
};
|
|
507
|
-
|
|
508
|
-
|
|
511
|
+
const defaultResolve = resolve;
|
|
512
|
+
|
|
513
|
+
// TODO maybe remove this in v6 or v7, only accept options object to simplify the code
|
|
514
|
+
const normalizeParams = args => {
|
|
509
515
|
if (args.length === 1) {
|
|
510
516
|
const options = args[0];
|
|
511
517
|
return {
|
|
@@ -528,79 +534,82 @@ var normalizeParams = (args) => {
|
|
|
528
534
|
resolve: args[3]
|
|
529
535
|
};
|
|
530
536
|
};
|
|
531
|
-
|
|
537
|
+
const handleJsonRpcMessage = async (...args) => {
|
|
532
538
|
const options = normalizeParams(args);
|
|
533
539
|
const {
|
|
534
|
-
execute
|
|
540
|
+
execute,
|
|
535
541
|
ipc,
|
|
536
|
-
logError
|
|
542
|
+
logError,
|
|
537
543
|
message,
|
|
538
|
-
preparePrettyError
|
|
539
|
-
requiresSocket
|
|
540
|
-
resolve
|
|
544
|
+
preparePrettyError,
|
|
545
|
+
requiresSocket,
|
|
546
|
+
resolve
|
|
541
547
|
} = options;
|
|
542
|
-
if (
|
|
543
|
-
if (
|
|
544
|
-
const response = await getResponse(message, ipc,
|
|
548
|
+
if ('id' in message) {
|
|
549
|
+
if ('method' in message) {
|
|
550
|
+
const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
545
551
|
try {
|
|
546
552
|
ipc.send(response);
|
|
547
553
|
} catch (error) {
|
|
548
|
-
const errorResponse = getErrorResponse(message.id, error,
|
|
554
|
+
const errorResponse = getErrorResponse(message.id, error, preparePrettyError, logError);
|
|
549
555
|
ipc.send(errorResponse);
|
|
550
556
|
}
|
|
551
557
|
return;
|
|
552
558
|
}
|
|
553
|
-
|
|
559
|
+
resolve(message.id, message);
|
|
554
560
|
return;
|
|
555
561
|
}
|
|
556
|
-
if (
|
|
557
|
-
await getResponse(message, ipc,
|
|
562
|
+
if ('method' in message) {
|
|
563
|
+
await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
558
564
|
return;
|
|
559
565
|
}
|
|
560
|
-
throw new JsonRpcError(
|
|
566
|
+
throw new JsonRpcError('unexpected message');
|
|
561
567
|
};
|
|
562
568
|
|
|
563
|
-
|
|
564
|
-
var Two2 = "2.0";
|
|
569
|
+
const Two = '2.0';
|
|
565
570
|
|
|
566
|
-
|
|
567
|
-
var create2 = (method, params) => {
|
|
571
|
+
const create$4 = (method, params) => {
|
|
568
572
|
return {
|
|
569
|
-
jsonrpc:
|
|
573
|
+
jsonrpc: Two,
|
|
570
574
|
method,
|
|
571
575
|
params
|
|
572
576
|
};
|
|
573
577
|
};
|
|
574
578
|
|
|
575
|
-
|
|
576
|
-
var create3 = (id2, method, params) => {
|
|
579
|
+
const create$3 = (id, method, params) => {
|
|
577
580
|
const message = {
|
|
578
|
-
id
|
|
579
|
-
jsonrpc:
|
|
581
|
+
id,
|
|
582
|
+
jsonrpc: Two,
|
|
580
583
|
method,
|
|
581
584
|
params
|
|
582
585
|
};
|
|
583
586
|
return message;
|
|
584
587
|
};
|
|
585
588
|
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
var create4 = () => {
|
|
589
|
+
let id = 0;
|
|
590
|
+
const create$2 = () => {
|
|
589
591
|
return ++id;
|
|
590
592
|
};
|
|
591
593
|
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
const
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
594
|
+
const registerPromise = map => {
|
|
595
|
+
const id = create$2();
|
|
596
|
+
const {
|
|
597
|
+
promise,
|
|
598
|
+
resolve
|
|
599
|
+
} = Promise.withResolvers();
|
|
600
|
+
map[id] = resolve;
|
|
601
|
+
return {
|
|
602
|
+
id,
|
|
603
|
+
promise
|
|
604
|
+
};
|
|
598
605
|
};
|
|
599
606
|
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
607
|
+
const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer) => {
|
|
608
|
+
const {
|
|
609
|
+
id,
|
|
610
|
+
promise
|
|
611
|
+
} = registerPromise(callbacks);
|
|
612
|
+
const message = create$3(id, method, params);
|
|
604
613
|
if (useSendAndTransfer && ipc.sendAndTransfer) {
|
|
605
614
|
ipc.sendAndTransfer(message);
|
|
606
615
|
} else {
|
|
@@ -609,26 +618,26 @@ var invokeHelper = async (callbacks2, ipc, method, params, useSendAndTransfer) =
|
|
|
609
618
|
const responseMessage = await promise;
|
|
610
619
|
return unwrapJsonRpcResult(responseMessage);
|
|
611
620
|
};
|
|
612
|
-
|
|
613
|
-
const
|
|
614
|
-
ipc._resolve = (
|
|
615
|
-
const fn =
|
|
621
|
+
const createRpc = ipc => {
|
|
622
|
+
const callbacks = Object.create(null);
|
|
623
|
+
ipc._resolve = (id, response) => {
|
|
624
|
+
const fn = callbacks[id];
|
|
616
625
|
if (!fn) {
|
|
617
|
-
console.warn(`callback ${
|
|
626
|
+
console.warn(`callback ${id} may already be disposed`);
|
|
618
627
|
return;
|
|
619
628
|
}
|
|
620
629
|
fn(response);
|
|
621
|
-
delete
|
|
630
|
+
delete callbacks[id];
|
|
622
631
|
};
|
|
623
632
|
const rpc = {
|
|
624
633
|
async dispose() {
|
|
625
634
|
await ipc?.dispose();
|
|
626
635
|
},
|
|
627
636
|
invoke(method, ...params) {
|
|
628
|
-
return invokeHelper(
|
|
637
|
+
return invokeHelper(callbacks, ipc, method, params, false);
|
|
629
638
|
},
|
|
630
639
|
invokeAndTransfer(method, ...params) {
|
|
631
|
-
return invokeHelper(
|
|
640
|
+
return invokeHelper(callbacks, ipc, method, params, true);
|
|
632
641
|
},
|
|
633
642
|
// @ts-ignore
|
|
634
643
|
ipc,
|
|
@@ -636,24 +645,24 @@ var createRpc = (ipc) => {
|
|
|
636
645
|
* @deprecated
|
|
637
646
|
*/
|
|
638
647
|
send(method, ...params) {
|
|
639
|
-
const message =
|
|
648
|
+
const message = create$4(method, params);
|
|
640
649
|
ipc.send(message);
|
|
641
650
|
}
|
|
642
651
|
};
|
|
643
652
|
return rpc;
|
|
644
653
|
};
|
|
645
654
|
|
|
646
|
-
|
|
647
|
-
var requiresSocket = () => {
|
|
655
|
+
const requiresSocket = () => {
|
|
648
656
|
return false;
|
|
649
657
|
};
|
|
650
|
-
|
|
658
|
+
const preparePrettyError = error => {
|
|
651
659
|
return error;
|
|
652
660
|
};
|
|
653
|
-
|
|
661
|
+
const logError = () => {
|
|
662
|
+
// handled by renderer worker
|
|
654
663
|
};
|
|
655
|
-
|
|
656
|
-
if (event.data ===
|
|
664
|
+
const handleMessage = event => {
|
|
665
|
+
if (event.data === 'ready') {
|
|
657
666
|
return Promise.resolve();
|
|
658
667
|
}
|
|
659
668
|
const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
|
|
@@ -661,17 +670,16 @@ var handleMessage = (event) => {
|
|
|
661
670
|
return handleJsonRpcMessage(event.target, event.data, actualExecute, event.target._resolve, preparePrettyError, logError, actualRequiresSocket);
|
|
662
671
|
};
|
|
663
672
|
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
ipc.on(
|
|
673
|
+
const handleIpc = ipc => {
|
|
674
|
+
if ('addEventListener' in ipc) {
|
|
675
|
+
ipc.addEventListener('message', handleMessage);
|
|
676
|
+
} else if ('on' in ipc) {
|
|
677
|
+
// deprecated
|
|
678
|
+
ipc.on('message', handleMessage);
|
|
670
679
|
}
|
|
671
680
|
};
|
|
672
681
|
|
|
673
|
-
|
|
674
|
-
var listen = async (module, options) => {
|
|
682
|
+
const listen$1 = async (module, options) => {
|
|
675
683
|
const rawIpc = await module.listen(options);
|
|
676
684
|
if (module.signal) {
|
|
677
685
|
module.signal(rawIpc);
|
|
@@ -680,51 +688,51 @@ var listen = async (module, options) => {
|
|
|
680
688
|
return ipc;
|
|
681
689
|
};
|
|
682
690
|
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
+
const create$1 = async ({
|
|
692
|
+
commandMap,
|
|
693
|
+
messagePort
|
|
694
|
+
}) => {
|
|
695
|
+
// TODO create a commandMap per rpc instance
|
|
696
|
+
register(commandMap);
|
|
697
|
+
const ipc = await listen$1(IpcChildWithMessagePort$1, {
|
|
698
|
+
port: messagePort
|
|
699
|
+
});
|
|
691
700
|
handleIpc(ipc);
|
|
692
701
|
const rpc = createRpc(ipc);
|
|
693
702
|
return rpc;
|
|
694
703
|
};
|
|
695
704
|
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
create
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
register(commandMap2);
|
|
703
|
-
const ipc = await listen(IpcChildWithModuleWorker$1);
|
|
705
|
+
const create = async ({
|
|
706
|
+
commandMap
|
|
707
|
+
}) => {
|
|
708
|
+
// TODO create a commandMap per rpc instance
|
|
709
|
+
register(commandMap);
|
|
710
|
+
const ipc = await listen$1(IpcChildWithModuleWorker$1);
|
|
704
711
|
handleIpc(ipc);
|
|
705
712
|
const rpc = createRpc(ipc);
|
|
706
713
|
return rpc;
|
|
707
714
|
};
|
|
708
715
|
|
|
709
|
-
|
|
710
|
-
var getCache = async (cacheName, bucketName, bucketOptions) => {
|
|
716
|
+
const getCache = async (cacheName, bucketName, bucketOptions) => {
|
|
711
717
|
if (bucketName) {
|
|
712
|
-
if (typeof navigator ===
|
|
713
|
-
throw new Error(
|
|
718
|
+
if (typeof navigator === 'undefined') {
|
|
719
|
+
throw new Error('Storage Buckets are not available in this context');
|
|
714
720
|
}
|
|
715
|
-
const {
|
|
721
|
+
const {
|
|
722
|
+
storageBuckets
|
|
723
|
+
} = navigator;
|
|
716
724
|
if (!storageBuckets) {
|
|
717
|
-
throw new Error(
|
|
725
|
+
throw new Error('Storage Buckets are not available in this context');
|
|
718
726
|
}
|
|
719
727
|
const bucket = await storageBuckets.open(bucketName, bucketOptions);
|
|
720
728
|
return bucket.caches.open(cacheName);
|
|
721
729
|
}
|
|
722
|
-
if (typeof caches ===
|
|
723
|
-
throw new Error(
|
|
730
|
+
if (typeof caches === 'undefined') {
|
|
731
|
+
throw new Error('Cache Storage is not available in this context');
|
|
724
732
|
}
|
|
725
733
|
return caches.open(cacheName);
|
|
726
734
|
};
|
|
727
|
-
|
|
735
|
+
const getCacheStorageItem = async (request, cacheName = 'lvce-cache', bucketName = '', bucketOptions = {}) => {
|
|
728
736
|
const cache = await getCache(cacheName, bucketName, bucketOptions);
|
|
729
737
|
const response = await cache.match(request);
|
|
730
738
|
if (!response) {
|
|
@@ -737,117 +745,421 @@ var getCacheStorageItem = async (request, cacheName = "lvce-cache", bucketName =
|
|
|
737
745
|
statusText: response.statusText
|
|
738
746
|
};
|
|
739
747
|
};
|
|
740
|
-
|
|
748
|
+
const setCacheStorageItem = async (request, value, cacheName = 'lvce-cache', headers = {}, bucketName = '', bucketOptions = {}) => {
|
|
741
749
|
try {
|
|
742
750
|
const cache = await getCache(cacheName, bucketName, bucketOptions);
|
|
743
|
-
const response = new Response(value, {
|
|
751
|
+
const response = new Response(value, {
|
|
752
|
+
headers: new Headers(headers)
|
|
753
|
+
});
|
|
744
754
|
await cache.put(request, response);
|
|
745
|
-
return {
|
|
755
|
+
return {
|
|
756
|
+
success: true
|
|
757
|
+
};
|
|
746
758
|
} catch (error) {
|
|
747
759
|
return {
|
|
748
|
-
errorCode:
|
|
760
|
+
errorCode: 'CACHE_STORAGE_WRITE_FAILED',
|
|
749
761
|
errorMessage: error instanceof Error ? error.message : String(error),
|
|
750
762
|
success: false
|
|
751
763
|
};
|
|
752
764
|
}
|
|
753
765
|
};
|
|
754
|
-
|
|
766
|
+
const removeCacheStorageItem = async (request, cacheName = 'lvce-cache', bucketName = '', bucketOptions = {}) => {
|
|
755
767
|
const cache = await getCache(cacheName, bucketName, bucketOptions);
|
|
756
768
|
return cache.delete(request);
|
|
757
769
|
};
|
|
758
770
|
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
}
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
const
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
request.result.createObjectStore("file-handles");
|
|
779
|
-
}
|
|
771
|
+
const instanceOfAny = (object, constructors) => constructors.some(c => object instanceof c);
|
|
772
|
+
let idbProxyableTypes;
|
|
773
|
+
let cursorAdvanceMethods;
|
|
774
|
+
// This is a function to prevent it throwing up in node environments.
|
|
775
|
+
function getIdbProxyableTypes() {
|
|
776
|
+
return idbProxyableTypes || (idbProxyableTypes = [IDBDatabase, IDBObjectStore, IDBIndex, IDBCursor, IDBTransaction]);
|
|
777
|
+
}
|
|
778
|
+
// This is a function to prevent it throwing up in node environments.
|
|
779
|
+
function getCursorAdvanceMethods() {
|
|
780
|
+
return cursorAdvanceMethods || (cursorAdvanceMethods = [IDBCursor.prototype.advance, IDBCursor.prototype.continue, IDBCursor.prototype.continuePrimaryKey]);
|
|
781
|
+
}
|
|
782
|
+
const transactionDoneMap = new WeakMap();
|
|
783
|
+
const transformCache = new WeakMap();
|
|
784
|
+
const reverseTransformCache = new WeakMap();
|
|
785
|
+
function promisifyRequest(request) {
|
|
786
|
+
const promise = new Promise((resolve, reject) => {
|
|
787
|
+
const unlisten = () => {
|
|
788
|
+
request.removeEventListener('success', success);
|
|
789
|
+
request.removeEventListener('error', error);
|
|
780
790
|
};
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
791
|
+
const success = () => {
|
|
792
|
+
resolve(wrap(request.result));
|
|
793
|
+
unlisten();
|
|
784
794
|
};
|
|
785
|
-
|
|
786
|
-
|
|
795
|
+
const error = () => {
|
|
796
|
+
reject(request.error);
|
|
797
|
+
unlisten();
|
|
798
|
+
};
|
|
799
|
+
request.addEventListener('success', success);
|
|
800
|
+
request.addEventListener('error', error);
|
|
801
|
+
});
|
|
802
|
+
// This mapping exists in reverseTransformCache but doesn't exist in transformCache. This
|
|
803
|
+
// is because we create many promises from a single IDBRequest.
|
|
804
|
+
reverseTransformCache.set(promise, request);
|
|
805
|
+
return promise;
|
|
806
|
+
}
|
|
807
|
+
function cacheDonePromiseForTransaction(tx) {
|
|
808
|
+
// Early bail if we've already created a done promise for this transaction.
|
|
809
|
+
if (transactionDoneMap.has(tx)) return;
|
|
810
|
+
const done = new Promise((resolve, reject) => {
|
|
811
|
+
const unlisten = () => {
|
|
812
|
+
tx.removeEventListener('complete', complete);
|
|
813
|
+
tx.removeEventListener('error', error);
|
|
814
|
+
tx.removeEventListener('abort', error);
|
|
815
|
+
};
|
|
816
|
+
const complete = () => {
|
|
817
|
+
resolve();
|
|
818
|
+
unlisten();
|
|
819
|
+
};
|
|
820
|
+
const error = () => {
|
|
821
|
+
reject(tx.error || new DOMException('AbortError', 'AbortError'));
|
|
822
|
+
unlisten();
|
|
823
|
+
};
|
|
824
|
+
tx.addEventListener('complete', complete);
|
|
825
|
+
tx.addEventListener('error', error);
|
|
826
|
+
tx.addEventListener('abort', error);
|
|
827
|
+
});
|
|
828
|
+
// Cache it for later retrieval.
|
|
829
|
+
transactionDoneMap.set(tx, done);
|
|
830
|
+
}
|
|
831
|
+
let idbProxyTraps = {
|
|
832
|
+
get(target, prop, receiver) {
|
|
833
|
+
if (target instanceof IDBTransaction) {
|
|
834
|
+
// Special handling for transaction.done.
|
|
835
|
+
if (prop === 'done') return transactionDoneMap.get(target);
|
|
836
|
+
// Make tx.store return the only store in the transaction, or undefined if there are many.
|
|
837
|
+
if (prop === 'store') {
|
|
838
|
+
return receiver.objectStoreNames[1] ? undefined : receiver.objectStore(receiver.objectStoreNames[0]);
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
// Else transform whatever we get back.
|
|
842
|
+
return wrap(target[prop]);
|
|
843
|
+
},
|
|
844
|
+
set(target, prop, value) {
|
|
845
|
+
target[prop] = value;
|
|
846
|
+
return true;
|
|
847
|
+
},
|
|
848
|
+
has(target, prop) {
|
|
849
|
+
if (target instanceof IDBTransaction && (prop === 'done' || prop === 'store')) {
|
|
850
|
+
return true;
|
|
851
|
+
}
|
|
852
|
+
return prop in target;
|
|
853
|
+
}
|
|
854
|
+
};
|
|
855
|
+
function replaceTraps(callback) {
|
|
856
|
+
idbProxyTraps = callback(idbProxyTraps);
|
|
857
|
+
}
|
|
858
|
+
function wrapFunction(func) {
|
|
859
|
+
// Due to expected object equality (which is enforced by the caching in `wrap`), we
|
|
860
|
+
// only create one new func per func.
|
|
861
|
+
// Cursor methods are special, as the behaviour is a little more different to standard IDB. In
|
|
862
|
+
// IDB, you advance the cursor and wait for a new 'success' on the IDBRequest that gave you the
|
|
863
|
+
// cursor. It's kinda like a promise that can resolve with many values. That doesn't make sense
|
|
864
|
+
// with real promises, so each advance methods returns a new promise for the cursor object, or
|
|
865
|
+
// undefined if the end of the cursor has been reached.
|
|
866
|
+
if (getCursorAdvanceMethods().includes(func)) {
|
|
867
|
+
return function (...args) {
|
|
868
|
+
// Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
|
|
869
|
+
// the original object.
|
|
870
|
+
func.apply(unwrap(this), args);
|
|
871
|
+
return wrap(this.request);
|
|
872
|
+
};
|
|
873
|
+
}
|
|
874
|
+
return function (...args) {
|
|
875
|
+
// Calling the original function with the proxy as 'this' causes ILLEGAL INVOCATION, so we use
|
|
876
|
+
// the original object.
|
|
877
|
+
return wrap(func.apply(unwrap(this), args));
|
|
878
|
+
};
|
|
879
|
+
}
|
|
880
|
+
function transformCachableValue(value) {
|
|
881
|
+
if (typeof value === 'function') return wrapFunction(value);
|
|
882
|
+
// This doesn't return, it just creates a 'done' promise for the transaction,
|
|
883
|
+
// which is later returned for transaction.done (see idbObjectHandler).
|
|
884
|
+
if (value instanceof IDBTransaction) cacheDonePromiseForTransaction(value);
|
|
885
|
+
if (instanceOfAny(value, getIdbProxyableTypes())) return new Proxy(value, idbProxyTraps);
|
|
886
|
+
// Return the same value back if we're not going to transform it.
|
|
887
|
+
return value;
|
|
888
|
+
}
|
|
889
|
+
function wrap(value) {
|
|
890
|
+
// We sometimes generate multiple promises from a single IDBRequest (eg when cursoring), because
|
|
891
|
+
// IDB is weird and a single IDBRequest can yield many responses, so these can't be cached.
|
|
892
|
+
if (value instanceof IDBRequest) return promisifyRequest(value);
|
|
893
|
+
// If we've already transformed this value before, reuse the transformed value.
|
|
894
|
+
// This is faster, but it also provides object equality.
|
|
895
|
+
if (transformCache.has(value)) return transformCache.get(value);
|
|
896
|
+
const newValue = transformCachableValue(value);
|
|
897
|
+
// Not all types are transformed.
|
|
898
|
+
// These may be primitive types, so they can't be WeakMap keys.
|
|
899
|
+
if (newValue !== value) {
|
|
900
|
+
transformCache.set(value, newValue);
|
|
901
|
+
reverseTransformCache.set(newValue, value);
|
|
902
|
+
}
|
|
903
|
+
return newValue;
|
|
904
|
+
}
|
|
905
|
+
const unwrap = value => reverseTransformCache.get(value);
|
|
906
|
+
|
|
907
|
+
/**
|
|
908
|
+
* Open a database.
|
|
909
|
+
*
|
|
910
|
+
* @param name Name of the database.
|
|
911
|
+
* @param version Schema version.
|
|
912
|
+
* @param callbacks Additional callbacks.
|
|
913
|
+
*/
|
|
914
|
+
function openDB(name, version, {
|
|
915
|
+
blocked,
|
|
916
|
+
upgrade,
|
|
917
|
+
blocking,
|
|
918
|
+
terminated
|
|
919
|
+
} = {}) {
|
|
920
|
+
const request = indexedDB.open(name, version);
|
|
921
|
+
const openPromise = wrap(request);
|
|
922
|
+
if (upgrade) {
|
|
923
|
+
request.addEventListener('upgradeneeded', event => {
|
|
924
|
+
upgrade(wrap(request.result), event.oldVersion, event.newVersion, wrap(request.transaction), event);
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
if (blocked) {
|
|
928
|
+
request.addEventListener('blocked', event => blocked(
|
|
929
|
+
// Casting due to https://github.com/microsoft/TypeScript-DOM-lib-generator/pull/1405
|
|
930
|
+
event.oldVersion, event.newVersion, event));
|
|
931
|
+
}
|
|
932
|
+
openPromise.then(db => {
|
|
933
|
+
if (terminated) db.addEventListener('close', () => terminated());
|
|
934
|
+
if (blocking) {
|
|
935
|
+
db.addEventListener('versionchange', event => blocking(event.oldVersion, event.newVersion, event));
|
|
936
|
+
}
|
|
937
|
+
}).catch(() => {});
|
|
938
|
+
return openPromise;
|
|
939
|
+
}
|
|
940
|
+
const readMethods = ['get', 'getKey', 'getAll', 'getAllKeys', 'count'];
|
|
941
|
+
const writeMethods = ['put', 'add', 'delete', 'clear'];
|
|
942
|
+
const cachedMethods = new Map();
|
|
943
|
+
function getMethod(target, prop) {
|
|
944
|
+
if (!(target instanceof IDBDatabase && !(prop in target) && typeof prop === 'string')) {
|
|
945
|
+
return;
|
|
946
|
+
}
|
|
947
|
+
if (cachedMethods.get(prop)) return cachedMethods.get(prop);
|
|
948
|
+
const targetFuncName = prop.replace(/FromIndex$/, '');
|
|
949
|
+
const useIndex = prop !== targetFuncName;
|
|
950
|
+
const isWrite = writeMethods.includes(targetFuncName);
|
|
951
|
+
if (
|
|
952
|
+
// Bail if the target doesn't exist on the target. Eg, getAll isn't in Edge.
|
|
953
|
+
!(targetFuncName in (useIndex ? IDBIndex : IDBObjectStore).prototype) || !(isWrite || readMethods.includes(targetFuncName))) {
|
|
954
|
+
return;
|
|
955
|
+
}
|
|
956
|
+
const method = async function (storeName, ...args) {
|
|
957
|
+
// isWrite ? 'readwrite' : undefined gzipps better, but fails in Edge :(
|
|
958
|
+
const tx = this.transaction(storeName, isWrite ? 'readwrite' : 'readonly');
|
|
959
|
+
let target = tx.store;
|
|
960
|
+
if (useIndex) target = target.index(args.shift());
|
|
961
|
+
// Must reject if op rejects.
|
|
962
|
+
// If it's a write operation, must reject if tx.done rejects.
|
|
963
|
+
// Must reject with op rejection first.
|
|
964
|
+
// Must resolve with op value.
|
|
965
|
+
// Must handle both promises (no unhandled rejections)
|
|
966
|
+
return (await Promise.all([target[targetFuncName](...args), isWrite && tx.done]))[0];
|
|
967
|
+
};
|
|
968
|
+
cachedMethods.set(prop, method);
|
|
969
|
+
return method;
|
|
970
|
+
}
|
|
971
|
+
replaceTraps(oldTraps => ({
|
|
972
|
+
...oldTraps,
|
|
973
|
+
get: (target, prop, receiver) => getMethod(target, prop) || oldTraps.get(target, prop, receiver),
|
|
974
|
+
has: (target, prop) => !!getMethod(target, prop) || oldTraps.has(target, prop)
|
|
975
|
+
}));
|
|
976
|
+
const advanceMethodProps = ['continue', 'continuePrimaryKey', 'advance'];
|
|
977
|
+
const methodMap = {};
|
|
978
|
+
const advanceResults = new WeakMap();
|
|
979
|
+
const ittrProxiedCursorToOriginalProxy = new WeakMap();
|
|
980
|
+
const cursorIteratorTraps = {
|
|
981
|
+
get(target, prop) {
|
|
982
|
+
if (!advanceMethodProps.includes(prop)) return target[prop];
|
|
983
|
+
let cachedFunc = methodMap[prop];
|
|
984
|
+
if (!cachedFunc) {
|
|
985
|
+
cachedFunc = methodMap[prop] = function (...args) {
|
|
986
|
+
advanceResults.set(this, ittrProxiedCursorToOriginalProxy.get(this)[prop](...args));
|
|
987
|
+
};
|
|
988
|
+
}
|
|
989
|
+
return cachedFunc;
|
|
990
|
+
}
|
|
991
|
+
};
|
|
992
|
+
async function* iterate(...args) {
|
|
993
|
+
// tslint:disable-next-line:no-this-assignment
|
|
994
|
+
let cursor = this;
|
|
995
|
+
if (!(cursor instanceof IDBCursor)) {
|
|
996
|
+
cursor = await cursor.openCursor(...args);
|
|
997
|
+
}
|
|
998
|
+
if (!cursor) return;
|
|
999
|
+
cursor = cursor;
|
|
1000
|
+
const proxiedCursor = new Proxy(cursor, cursorIteratorTraps);
|
|
1001
|
+
ittrProxiedCursorToOriginalProxy.set(proxiedCursor, cursor);
|
|
1002
|
+
// Map this double-proxy back to the original, so other cursor methods work.
|
|
1003
|
+
reverseTransformCache.set(proxiedCursor, unwrap(cursor));
|
|
1004
|
+
while (cursor) {
|
|
1005
|
+
yield proxiedCursor;
|
|
1006
|
+
// If one of the advancing methods was not called, call continue().
|
|
1007
|
+
cursor = await (advanceResults.get(proxiedCursor) || cursor.continue());
|
|
1008
|
+
advanceResults.delete(proxiedCursor);
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
function isIteratorProp(target, prop) {
|
|
1012
|
+
return prop === Symbol.asyncIterator && instanceOfAny(target, [IDBIndex, IDBObjectStore, IDBCursor]) || prop === 'iterate' && instanceOfAny(target, [IDBIndex, IDBObjectStore]);
|
|
1013
|
+
}
|
|
1014
|
+
replaceTraps(oldTraps => ({
|
|
1015
|
+
...oldTraps,
|
|
1016
|
+
get(target, prop, receiver) {
|
|
1017
|
+
if (isIteratorProp(target, prop)) return iterate;
|
|
1018
|
+
return oldTraps.get(target, prop, receiver);
|
|
1019
|
+
},
|
|
1020
|
+
has(target, prop) {
|
|
1021
|
+
return isIteratorProp(target, prop) || oldTraps.has(target, prop);
|
|
1022
|
+
}
|
|
1023
|
+
}));
|
|
1024
|
+
|
|
1025
|
+
const cloneDatabaseKey = key => structuredClone(key);
|
|
1026
|
+
const closeDatabaseWhenOpened = async opening => {
|
|
1027
|
+
try {
|
|
1028
|
+
const openedDatabase = await opening;
|
|
1029
|
+
openedDatabase.close();
|
|
1030
|
+
} catch {
|
|
1031
|
+
// The open request can also fail after reporting that it was blocked.
|
|
1032
|
+
}
|
|
1033
|
+
};
|
|
1034
|
+
const openDatabase = async name => {
|
|
1035
|
+
if (typeof indexedDB === 'undefined') {
|
|
1036
|
+
throw new Error('IndexedDB is not available in this context');
|
|
1037
|
+
}
|
|
1038
|
+
let database;
|
|
1039
|
+
const {
|
|
1040
|
+
promise: blocked,
|
|
1041
|
+
reject: rejectBlocked
|
|
1042
|
+
} = Promise.withResolvers();
|
|
1043
|
+
const opening = openDB(name, 1, {
|
|
1044
|
+
blocked() {
|
|
1045
|
+
rejectBlocked(new Error(`Opening IndexedDB database "${name}" was blocked`));
|
|
1046
|
+
},
|
|
1047
|
+
blocking() {
|
|
1048
|
+
database?.close();
|
|
1049
|
+
},
|
|
1050
|
+
upgrade(database) {
|
|
1051
|
+
if (!database.objectStoreNames.contains('file-handles')) {
|
|
1052
|
+
database.createObjectStore('file-handles');
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
787
1055
|
});
|
|
1056
|
+
try {
|
|
1057
|
+
const openedDatabase = await Promise.race([opening, blocked]);
|
|
1058
|
+
database = openedDatabase;
|
|
1059
|
+
return openedDatabase;
|
|
1060
|
+
} catch (error) {
|
|
1061
|
+
void closeDatabaseWhenOpened(opening);
|
|
1062
|
+
throw error;
|
|
1063
|
+
}
|
|
788
1064
|
};
|
|
789
|
-
|
|
1065
|
+
const addIndexedDbFileHandle = async (key, handle, databaseName = 'lvce-cache-worker') => {
|
|
790
1066
|
const database = await openDatabase(databaseName);
|
|
791
|
-
const transaction = database.transaction(
|
|
792
|
-
const done = transactionDone(transaction);
|
|
793
|
-
const request = transaction.objectStore("file-handles").put(handle, cloneDatabaseKey(key));
|
|
1067
|
+
const transaction = database.transaction('file-handles', 'readwrite');
|
|
794
1068
|
try {
|
|
795
|
-
await
|
|
1069
|
+
await transaction.store.put(handle, cloneDatabaseKey(key));
|
|
1070
|
+
await transaction.done;
|
|
1071
|
+
} catch (error) {
|
|
1072
|
+
try {
|
|
1073
|
+
transaction.abort();
|
|
1074
|
+
} catch {
|
|
1075
|
+
// The transaction may already have completed or aborted.
|
|
1076
|
+
}
|
|
1077
|
+
try {
|
|
1078
|
+
await transaction.done;
|
|
1079
|
+
} catch {
|
|
1080
|
+
// The transaction has already aborted.
|
|
1081
|
+
}
|
|
1082
|
+
throw error;
|
|
796
1083
|
} finally {
|
|
797
1084
|
database.close();
|
|
798
1085
|
}
|
|
799
1086
|
};
|
|
800
|
-
|
|
1087
|
+
const getIndexedDbFileHandle = async (key, databaseName = 'lvce-cache-worker') => {
|
|
801
1088
|
const database = await openDatabase(databaseName);
|
|
802
|
-
const transaction = database.transaction(
|
|
803
|
-
const done = transactionDone(transaction);
|
|
1089
|
+
const transaction = database.transaction('file-handles', 'readonly');
|
|
804
1090
|
try {
|
|
805
|
-
const value = await
|
|
806
|
-
await done;
|
|
1091
|
+
const value = await transaction.store.get(cloneDatabaseKey(key));
|
|
1092
|
+
await transaction.done;
|
|
807
1093
|
return value;
|
|
1094
|
+
} catch (error) {
|
|
1095
|
+
try {
|
|
1096
|
+
transaction.abort();
|
|
1097
|
+
} catch {
|
|
1098
|
+
// The transaction may already have completed or aborted.
|
|
1099
|
+
}
|
|
1100
|
+
try {
|
|
1101
|
+
await transaction.done;
|
|
1102
|
+
} catch {
|
|
1103
|
+
// The transaction has already aborted.
|
|
1104
|
+
}
|
|
1105
|
+
throw error;
|
|
808
1106
|
} finally {
|
|
809
1107
|
database.close();
|
|
810
1108
|
}
|
|
811
1109
|
};
|
|
812
|
-
|
|
1110
|
+
const removeIndexedDbFileHandle = async (key, databaseName = 'lvce-cache-worker') => {
|
|
813
1111
|
const database = await openDatabase(databaseName);
|
|
814
|
-
const transaction = database.transaction(
|
|
815
|
-
const done = transactionDone(transaction);
|
|
816
|
-
const request = transaction.objectStore("file-handles").delete(cloneDatabaseKey(key));
|
|
1112
|
+
const transaction = database.transaction('file-handles', 'readwrite');
|
|
817
1113
|
try {
|
|
818
|
-
await
|
|
1114
|
+
await transaction.store.delete(cloneDatabaseKey(key));
|
|
1115
|
+
await transaction.done;
|
|
1116
|
+
} catch (error) {
|
|
1117
|
+
try {
|
|
1118
|
+
transaction.abort();
|
|
1119
|
+
} catch {
|
|
1120
|
+
// The transaction may already have completed or aborted.
|
|
1121
|
+
}
|
|
1122
|
+
try {
|
|
1123
|
+
await transaction.done;
|
|
1124
|
+
} catch {
|
|
1125
|
+
// The transaction has already aborted.
|
|
1126
|
+
}
|
|
1127
|
+
throw error;
|
|
819
1128
|
} finally {
|
|
820
1129
|
database.close();
|
|
821
1130
|
}
|
|
822
1131
|
};
|
|
823
1132
|
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
throw new TypeError("Origin Private File System is not available in this context");
|
|
1133
|
+
const getRoot = async () => {
|
|
1134
|
+
if (typeof navigator === 'undefined') {
|
|
1135
|
+
throw new TypeError('Origin Private File System is not available in this context');
|
|
828
1136
|
}
|
|
829
|
-
const {
|
|
830
|
-
|
|
831
|
-
|
|
1137
|
+
const {
|
|
1138
|
+
storage
|
|
1139
|
+
} = navigator;
|
|
1140
|
+
if (typeof storage.getDirectory !== 'function') {
|
|
1141
|
+
throw new TypeError('Origin Private File System is not available in this context');
|
|
832
1142
|
}
|
|
833
1143
|
return storage.getDirectory();
|
|
834
1144
|
};
|
|
835
|
-
|
|
836
|
-
if (typeof name !==
|
|
837
|
-
throw new TypeError(
|
|
1145
|
+
const validateName = name => {
|
|
1146
|
+
if (typeof name !== 'string' || name.length === 0 || name === '.' || name === '..' || /[/\\]/.test(name)) {
|
|
1147
|
+
throw new TypeError('OPFS filename must be a non-empty single path component');
|
|
838
1148
|
}
|
|
839
1149
|
};
|
|
840
|
-
|
|
1150
|
+
const readFile = async name => {
|
|
841
1151
|
validateName(name);
|
|
842
1152
|
const root = await getRoot();
|
|
843
1153
|
const handle = await root.getFileHandle(name);
|
|
844
1154
|
const file = await handle.getFile();
|
|
845
1155
|
return file.text();
|
|
846
1156
|
};
|
|
847
|
-
|
|
1157
|
+
const writeFile = async (name, content) => {
|
|
848
1158
|
validateName(name);
|
|
849
1159
|
const root = await getRoot();
|
|
850
|
-
const handle = await root.getFileHandle(name, {
|
|
1160
|
+
const handle = await root.getFileHandle(name, {
|
|
1161
|
+
create: true
|
|
1162
|
+
});
|
|
851
1163
|
const writable = await handle.createWritable();
|
|
852
1164
|
try {
|
|
853
1165
|
await writable.write(content);
|
|
@@ -857,36 +1169,36 @@ var writeFile = async (name, content) => {
|
|
|
857
1169
|
}
|
|
858
1170
|
await writable.close();
|
|
859
1171
|
};
|
|
860
|
-
|
|
1172
|
+
const removeFile = async name => {
|
|
861
1173
|
validateName(name);
|
|
862
1174
|
const root = await getRoot();
|
|
863
1175
|
await root.removeEntry(name);
|
|
864
1176
|
};
|
|
865
1177
|
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
1178
|
+
const handleMessagePort$1 = messagePort => handleMessagePort(messagePort);
|
|
1179
|
+
const commandMap = {
|
|
1180
|
+
'Cache.getCacheStorageItem': getCacheStorageItem,
|
|
1181
|
+
'Cache.removeCacheStorageItem': removeCacheStorageItem,
|
|
1182
|
+
'Cache.setCacheStorageItem': setCacheStorageItem,
|
|
1183
|
+
'CacheWorker.handleMessagePort': handleMessagePort$1,
|
|
1184
|
+
'IndexedDb.addIndexedDbFileHandle': addIndexedDbFileHandle,
|
|
1185
|
+
'IndexedDb.getIndexedDbFileHandle': getIndexedDbFileHandle,
|
|
1186
|
+
'IndexedDb.removeIndexedDbFileHandle': removeIndexedDbFileHandle,
|
|
1187
|
+
'Opfs.readFile': readFile,
|
|
1188
|
+
'Opfs.removeFile': removeFile,
|
|
1189
|
+
'Opfs.writeFile': writeFile
|
|
878
1190
|
};
|
|
879
1191
|
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
1192
|
+
const listen = async () => {
|
|
1193
|
+
await create({
|
|
1194
|
+
commandMap: commandMap
|
|
1195
|
+
});
|
|
883
1196
|
};
|
|
884
|
-
|
|
885
|
-
await
|
|
886
|
-
commandMap,
|
|
1197
|
+
const handleMessagePort = async messagePort => {
|
|
1198
|
+
await create$1({
|
|
1199
|
+
commandMap: commandMap,
|
|
887
1200
|
messagePort
|
|
888
1201
|
});
|
|
889
1202
|
};
|
|
890
1203
|
|
|
891
|
-
|
|
892
|
-
await listen2();
|
|
1204
|
+
await listen();
|