@lvce-editor/cache-worker 0.1.2 → 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 +670 -301
- 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,108 +22,151 @@ 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
|
-
|
|
82
|
-
|
|
83
|
-
|
|
74
|
+
const listen$8 = ({
|
|
75
|
+
port
|
|
76
|
+
}) => {
|
|
77
|
+
return port;
|
|
78
|
+
};
|
|
79
|
+
const signal$9 = port => {
|
|
80
|
+
port.postMessage(readyMessage);
|
|
81
|
+
};
|
|
82
|
+
class IpcChildWithMessagePort extends Ipc {
|
|
83
|
+
getData(event) {
|
|
84
|
+
return getData$2(event);
|
|
85
|
+
}
|
|
86
|
+
send(message) {
|
|
87
|
+
this._rawIpc.postMessage(message);
|
|
88
|
+
}
|
|
89
|
+
sendAndTransfer(message) {
|
|
90
|
+
const transfer = getTransferrables(message);
|
|
91
|
+
this._rawIpc.postMessage(message, transfer);
|
|
92
|
+
}
|
|
93
|
+
dispose() {
|
|
94
|
+
// ignore
|
|
95
|
+
}
|
|
96
|
+
onClose(callback) {
|
|
97
|
+
// ignore
|
|
98
|
+
}
|
|
99
|
+
onMessage(callback) {
|
|
100
|
+
this._rawIpc.addEventListener('message', callback);
|
|
101
|
+
this._rawIpc.start();
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
const wrap$g = port => {
|
|
105
|
+
return new IpcChildWithMessagePort(port);
|
|
106
|
+
};
|
|
107
|
+
const IpcChildWithMessagePort$1 = {
|
|
108
|
+
__proto__: null,
|
|
109
|
+
listen: listen$8,
|
|
110
|
+
signal: signal$9,
|
|
111
|
+
wrap: wrap$g
|
|
112
|
+
};
|
|
113
|
+
const listen$7 = () => {
|
|
114
|
+
// @ts-ignore
|
|
115
|
+
if (typeof WorkerGlobalScope === 'undefined') {
|
|
116
|
+
throw new TypeError('module is not in web worker scope');
|
|
84
117
|
}
|
|
85
118
|
return globalThis;
|
|
86
119
|
};
|
|
87
|
-
|
|
120
|
+
const signal$8 = global => {
|
|
88
121
|
global.postMessage(readyMessage);
|
|
89
122
|
};
|
|
90
|
-
|
|
123
|
+
class IpcChildWithModuleWorker extends Ipc {
|
|
91
124
|
getData(event) {
|
|
92
125
|
return getData$2(event);
|
|
93
126
|
}
|
|
94
127
|
send(message) {
|
|
128
|
+
// @ts-ignore
|
|
95
129
|
this._rawIpc.postMessage(message);
|
|
96
130
|
}
|
|
97
131
|
sendAndTransfer(message) {
|
|
98
132
|
const transfer = getTransferrables(message);
|
|
133
|
+
// @ts-ignore
|
|
99
134
|
this._rawIpc.postMessage(message, transfer);
|
|
100
135
|
}
|
|
101
136
|
dispose() {
|
|
137
|
+
// ignore
|
|
102
138
|
}
|
|
103
139
|
onClose(callback) {
|
|
140
|
+
// ignore
|
|
104
141
|
}
|
|
105
142
|
onMessage(callback) {
|
|
106
|
-
this._rawIpc.addEventListener(
|
|
143
|
+
this._rawIpc.addEventListener('message', callback);
|
|
107
144
|
}
|
|
108
|
-
}
|
|
109
|
-
|
|
145
|
+
}
|
|
146
|
+
const wrap$f = global => {
|
|
110
147
|
return new IpcChildWithModuleWorker(global);
|
|
111
148
|
};
|
|
112
|
-
|
|
149
|
+
const IpcChildWithModuleWorker$1 = {
|
|
113
150
|
__proto__: null,
|
|
114
151
|
listen: listen$7,
|
|
115
152
|
signal: signal$8,
|
|
116
153
|
wrap: wrap$f
|
|
117
154
|
};
|
|
118
155
|
|
|
119
|
-
|
|
120
|
-
var CommandNotFoundError = class extends Error {
|
|
156
|
+
class CommandNotFoundError extends Error {
|
|
121
157
|
constructor(command) {
|
|
122
158
|
super(`Command not found ${command}`);
|
|
123
|
-
this.name =
|
|
159
|
+
this.name = 'CommandNotFoundError';
|
|
124
160
|
}
|
|
161
|
+
}
|
|
162
|
+
const commands = Object.create(null);
|
|
163
|
+
const register = commandMap => {
|
|
164
|
+
Object.assign(commands, commandMap);
|
|
125
165
|
};
|
|
126
|
-
|
|
127
|
-
var register = (commandMap2) => {
|
|
128
|
-
Object.assign(commands, commandMap2);
|
|
129
|
-
};
|
|
130
|
-
var getCommand = (key) => {
|
|
166
|
+
const getCommand = key => {
|
|
131
167
|
return commands[key];
|
|
132
168
|
};
|
|
133
|
-
|
|
169
|
+
const execute = (command, ...args) => {
|
|
134
170
|
const fn = getCommand(command);
|
|
135
171
|
if (!fn) {
|
|
136
172
|
throw new CommandNotFoundError(command);
|
|
@@ -138,27 +174,26 @@ var execute = (command, ...args) => {
|
|
|
138
174
|
return fn(...args);
|
|
139
175
|
};
|
|
140
176
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
return callbacks[id2];
|
|
177
|
+
const Two$1 = '2.0';
|
|
178
|
+
const callbacks = Object.create(null);
|
|
179
|
+
const get = id => {
|
|
180
|
+
return callbacks[id];
|
|
146
181
|
};
|
|
147
|
-
|
|
148
|
-
delete callbacks[
|
|
182
|
+
const remove = id => {
|
|
183
|
+
delete callbacks[id];
|
|
149
184
|
};
|
|
150
|
-
|
|
185
|
+
class JsonRpcError extends Error {
|
|
151
186
|
constructor(message) {
|
|
152
187
|
super(message);
|
|
153
|
-
this.name =
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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) => {
|
|
162
197
|
if (type) {
|
|
163
198
|
switch (type) {
|
|
164
199
|
case DomException:
|
|
@@ -173,26 +208,26 @@ var getErrorConstructor = (message, type) => {
|
|
|
173
208
|
return Error;
|
|
174
209
|
}
|
|
175
210
|
}
|
|
176
|
-
if (message.startsWith(
|
|
211
|
+
if (message.startsWith('TypeError: ')) {
|
|
177
212
|
return TypeError;
|
|
178
213
|
}
|
|
179
|
-
if (message.startsWith(
|
|
214
|
+
if (message.startsWith('SyntaxError: ')) {
|
|
180
215
|
return SyntaxError;
|
|
181
216
|
}
|
|
182
|
-
if (message.startsWith(
|
|
217
|
+
if (message.startsWith('ReferenceError: ')) {
|
|
183
218
|
return ReferenceError;
|
|
184
219
|
}
|
|
185
220
|
return Error;
|
|
186
221
|
};
|
|
187
|
-
|
|
222
|
+
const constructError = (message, type, name) => {
|
|
188
223
|
const ErrorConstructor = getErrorConstructor(message, type);
|
|
189
224
|
if (ErrorConstructor === DOMException && name) {
|
|
190
225
|
return new ErrorConstructor(message, name);
|
|
191
226
|
}
|
|
192
227
|
if (ErrorConstructor === Error) {
|
|
193
228
|
const error = new Error(message);
|
|
194
|
-
if (name && name !==
|
|
195
|
-
Object.defineProperty(error,
|
|
229
|
+
if (name && name !== 'VError') {
|
|
230
|
+
Object.defineProperty(error, 'name', {
|
|
196
231
|
configurable: true,
|
|
197
232
|
value: name
|
|
198
233
|
});
|
|
@@ -201,33 +236,33 @@ var constructError = (message, type, name) => {
|
|
|
201
236
|
}
|
|
202
237
|
return new ErrorConstructor(message);
|
|
203
238
|
};
|
|
204
|
-
|
|
239
|
+
const joinLines = lines => {
|
|
205
240
|
return lines.join(NewLine);
|
|
206
241
|
};
|
|
207
|
-
|
|
242
|
+
const splitLines = lines => {
|
|
208
243
|
return lines.split(NewLine);
|
|
209
244
|
};
|
|
210
|
-
|
|
245
|
+
const getCurrentStack = () => {
|
|
211
246
|
const stackLinesToSkip = 3;
|
|
212
|
-
const currentStack = joinLines(splitLines(new Error().stack ||
|
|
247
|
+
const currentStack = joinLines(splitLines(new Error().stack || '').slice(stackLinesToSkip));
|
|
213
248
|
return currentStack;
|
|
214
249
|
};
|
|
215
|
-
|
|
250
|
+
const getNewLineIndex = (string, startIndex) => {
|
|
216
251
|
{
|
|
217
|
-
return
|
|
252
|
+
return string.indexOf(NewLine);
|
|
218
253
|
}
|
|
219
254
|
};
|
|
220
|
-
|
|
221
|
-
let parentStack = error.stack || error.data || error.message ||
|
|
222
|
-
if (parentStack.startsWith(
|
|
255
|
+
const getParentStack = error => {
|
|
256
|
+
let parentStack = error.stack || error.data || error.message || '';
|
|
257
|
+
if (parentStack.startsWith(' at')) {
|
|
223
258
|
parentStack = error.message + NewLine + parentStack;
|
|
224
259
|
}
|
|
225
260
|
return parentStack;
|
|
226
261
|
};
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
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');
|
|
231
266
|
if (descriptor) {
|
|
232
267
|
if (!descriptor.configurable && !descriptor.writable) {
|
|
233
268
|
return;
|
|
@@ -237,19 +272,19 @@ var setStack = (error, stack) => {
|
|
|
237
272
|
return;
|
|
238
273
|
}
|
|
239
274
|
}
|
|
240
|
-
Object.defineProperty(error,
|
|
275
|
+
Object.defineProperty(error, 'stack', {
|
|
241
276
|
configurable: true,
|
|
242
277
|
value: stack,
|
|
243
278
|
writable: true
|
|
244
279
|
});
|
|
245
280
|
};
|
|
246
|
-
|
|
247
|
-
if (typeof error.stack ===
|
|
281
|
+
const restoreExistingError = (error, currentStack) => {
|
|
282
|
+
if (typeof error.stack === 'string') {
|
|
248
283
|
setStack(error, `${error.stack}${NewLine}${currentStack}`);
|
|
249
284
|
}
|
|
250
285
|
return error;
|
|
251
286
|
};
|
|
252
|
-
|
|
287
|
+
const restoreMethodNotFoundError = (error, currentStack) => {
|
|
253
288
|
const restoredError = new JsonRpcError(error.message);
|
|
254
289
|
Object.assign(restoredError, {
|
|
255
290
|
code: error.code
|
|
@@ -258,7 +293,7 @@ var restoreMethodNotFoundError = (error, currentStack) => {
|
|
|
258
293
|
setStack(restoredError, `${parentStack}${NewLine}${currentStack}`);
|
|
259
294
|
return restoredError;
|
|
260
295
|
};
|
|
261
|
-
|
|
296
|
+
const restoreStackFromData = (restoredError, error, currentStack) => {
|
|
262
297
|
if (error.data.stack && error.data.type && error.message) {
|
|
263
298
|
setStack(restoredError, `${error.data.type}: ${error.message}${NewLine}${error.data.stack}${NewLine}${currentStack}`);
|
|
264
299
|
return;
|
|
@@ -267,37 +302,42 @@ var restoreStackFromData = (restoredError, error, currentStack) => {
|
|
|
267
302
|
setStack(restoredError, error.data.stack);
|
|
268
303
|
}
|
|
269
304
|
};
|
|
270
|
-
|
|
305
|
+
const applyDataProperties = (restoredError, error) => {
|
|
271
306
|
restoreStackFromData(restoredError, error, getCurrentStack());
|
|
272
307
|
if (error.data.codeFrame) {
|
|
308
|
+
// @ts-ignore
|
|
273
309
|
restoredError.codeFrame = error.data.codeFrame;
|
|
274
310
|
}
|
|
275
|
-
if (typeof error.data.code ===
|
|
276
|
-
|
|
311
|
+
if (typeof error.data.code === 'string' || typeof error.data.code === 'number') {
|
|
312
|
+
// @ts-ignore
|
|
313
|
+
Object.defineProperty(restoredError, 'code', {
|
|
277
314
|
configurable: true,
|
|
278
315
|
value: error.data.code,
|
|
279
316
|
writable: true
|
|
280
317
|
});
|
|
281
318
|
}
|
|
282
319
|
if (error.data.type) {
|
|
320
|
+
// @ts-ignore
|
|
283
321
|
restoredError.name = error.data.type;
|
|
284
322
|
}
|
|
285
323
|
};
|
|
286
|
-
|
|
324
|
+
const applyDirectProperties = (restoredError, error) => {
|
|
287
325
|
if (error.stack) {
|
|
288
|
-
const lowerStack = restoredError.stack ||
|
|
326
|
+
const lowerStack = restoredError.stack || '';
|
|
289
327
|
const indexNewLine = getNewLineIndex(lowerStack);
|
|
290
328
|
const parentStack = getParentStack(error);
|
|
329
|
+
// @ts-ignore
|
|
291
330
|
setStack(restoredError, `${parentStack}${lowerStack.slice(indexNewLine)}`);
|
|
292
331
|
}
|
|
293
332
|
if (error.codeFrame) {
|
|
333
|
+
// @ts-ignore
|
|
294
334
|
restoredError.codeFrame = error.codeFrame;
|
|
295
335
|
}
|
|
296
336
|
};
|
|
297
|
-
|
|
337
|
+
const restoreMessageError = (error, _currentStack) => {
|
|
298
338
|
const restoredError = constructError(error.message, error.type || error.data?.type, error.name);
|
|
299
|
-
if (typeof error.code ===
|
|
300
|
-
Object.defineProperty(restoredError,
|
|
339
|
+
if (typeof error.code === 'string' || typeof error.code === 'number') {
|
|
340
|
+
Object.defineProperty(restoredError, 'code', {
|
|
301
341
|
configurable: true,
|
|
302
342
|
value: error.code,
|
|
303
343
|
writable: true
|
|
@@ -310,15 +350,15 @@ var restoreMessageError = (error, _currentStack) => {
|
|
|
310
350
|
}
|
|
311
351
|
return restoredError;
|
|
312
352
|
};
|
|
313
|
-
|
|
314
|
-
if (error && typeof error.message ===
|
|
353
|
+
const getFallbackMessage = error => {
|
|
354
|
+
if (error && typeof error.message === 'string' && error.message) {
|
|
315
355
|
return error.message;
|
|
316
356
|
}
|
|
317
357
|
if (error && error.data) {
|
|
318
358
|
return getFallbackMessage(error.data);
|
|
319
359
|
}
|
|
320
360
|
const stringified = String(error);
|
|
321
|
-
if (stringified !==
|
|
361
|
+
if (stringified !== '[object Object]') {
|
|
322
362
|
return stringified;
|
|
323
363
|
}
|
|
324
364
|
try {
|
|
@@ -327,7 +367,7 @@ var getFallbackMessage = (error) => {
|
|
|
327
367
|
return stringified;
|
|
328
368
|
}
|
|
329
369
|
};
|
|
330
|
-
|
|
370
|
+
const restoreJsonRpcError = error => {
|
|
331
371
|
const currentStack = getCurrentStack();
|
|
332
372
|
if (error && error instanceof Error) {
|
|
333
373
|
return restoreExistingError(error, currentStack);
|
|
@@ -343,51 +383,51 @@ var restoreJsonRpcError = (error) => {
|
|
|
343
383
|
}
|
|
344
384
|
return new Error(`JsonRpc Error: ${getFallbackMessage(error)}`);
|
|
345
385
|
};
|
|
346
|
-
|
|
347
|
-
if (
|
|
386
|
+
const unwrapJsonRpcResult = responseMessage => {
|
|
387
|
+
if ('error' in responseMessage) {
|
|
348
388
|
const restoredError = restoreJsonRpcError(responseMessage.error);
|
|
349
389
|
throw restoredError;
|
|
350
390
|
}
|
|
351
|
-
if (
|
|
391
|
+
if ('result' in responseMessage) {
|
|
352
392
|
return responseMessage.result;
|
|
353
393
|
}
|
|
354
|
-
throw new JsonRpcError(
|
|
394
|
+
throw new JsonRpcError('unexpected response message');
|
|
355
395
|
};
|
|
356
|
-
|
|
396
|
+
const warn = (...args) => {
|
|
357
397
|
console.warn(...args);
|
|
358
398
|
};
|
|
359
|
-
|
|
360
|
-
const fn = get(
|
|
399
|
+
const resolve = (id, response) => {
|
|
400
|
+
const fn = get(id);
|
|
361
401
|
if (!fn) {
|
|
362
402
|
console.log(response);
|
|
363
|
-
warn(`callback ${
|
|
403
|
+
warn(`callback ${id} may already be disposed`);
|
|
364
404
|
return;
|
|
365
405
|
}
|
|
366
406
|
fn(response);
|
|
367
|
-
remove(
|
|
407
|
+
remove(id);
|
|
368
408
|
};
|
|
369
|
-
|
|
370
|
-
|
|
409
|
+
const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
|
|
410
|
+
const getErrorType = prettyError => {
|
|
371
411
|
if (prettyError && prettyError.type) {
|
|
372
412
|
return prettyError.type;
|
|
373
413
|
}
|
|
374
414
|
if (prettyError && prettyError.constructor && prettyError.constructor.name) {
|
|
375
415
|
return prettyError.constructor.name;
|
|
376
416
|
}
|
|
377
|
-
return
|
|
417
|
+
return undefined;
|
|
378
418
|
};
|
|
379
|
-
|
|
380
|
-
return line.trim().startsWith(
|
|
419
|
+
const isAlreadyStack = line => {
|
|
420
|
+
return line.trim().startsWith('at ');
|
|
381
421
|
};
|
|
382
|
-
|
|
383
|
-
const stackString = prettyError.stack ||
|
|
384
|
-
const newLineIndex = stackString.indexOf(
|
|
422
|
+
const getStack = prettyError => {
|
|
423
|
+
const stackString = prettyError.stack || '';
|
|
424
|
+
const newLineIndex = stackString.indexOf('\n');
|
|
385
425
|
if (newLineIndex !== -1 && !isAlreadyStack(stackString.slice(0, newLineIndex))) {
|
|
386
426
|
return stackString.slice(newLineIndex + 1);
|
|
387
427
|
}
|
|
388
428
|
return stackString;
|
|
389
429
|
};
|
|
390
|
-
|
|
430
|
+
const getErrorProperty = (error, prettyError) => {
|
|
391
431
|
if (error && error.code === E_COMMAND_NOT_FOUND) {
|
|
392
432
|
return {
|
|
393
433
|
code: MethodNotFound,
|
|
@@ -407,68 +447,71 @@ var getErrorProperty = (error, prettyError) => {
|
|
|
407
447
|
message: prettyError.message
|
|
408
448
|
};
|
|
409
449
|
};
|
|
410
|
-
|
|
450
|
+
const create$1$1 = (id, error) => {
|
|
411
451
|
return {
|
|
412
452
|
error,
|
|
413
|
-
id
|
|
414
|
-
jsonrpc: Two
|
|
453
|
+
id,
|
|
454
|
+
jsonrpc: Two$1
|
|
415
455
|
};
|
|
416
456
|
};
|
|
417
|
-
|
|
418
|
-
const prettyError =
|
|
419
|
-
|
|
457
|
+
const getErrorResponse = (id, error, preparePrettyError, logError) => {
|
|
458
|
+
const prettyError = preparePrettyError(error);
|
|
459
|
+
logError(error, prettyError);
|
|
420
460
|
const errorProperty = getErrorProperty(error, prettyError);
|
|
421
|
-
return create$1(
|
|
461
|
+
return create$1$1(id, errorProperty);
|
|
422
462
|
};
|
|
423
|
-
|
|
463
|
+
const create$5 = (message, result) => {
|
|
424
464
|
return {
|
|
425
465
|
id: message.id,
|
|
426
|
-
jsonrpc: Two,
|
|
466
|
+
jsonrpc: Two$1,
|
|
427
467
|
result: result ?? null
|
|
428
468
|
};
|
|
429
469
|
};
|
|
430
|
-
|
|
470
|
+
const getSuccessResponse = (message, result) => {
|
|
431
471
|
const resultProperty = result ?? null;
|
|
432
|
-
return create(message, resultProperty);
|
|
472
|
+
return create$5(message, resultProperty);
|
|
433
473
|
};
|
|
434
|
-
|
|
474
|
+
const getErrorResponseSimple = (id, error) => {
|
|
435
475
|
return {
|
|
436
476
|
error: {
|
|
437
477
|
code: Custom,
|
|
438
478
|
data: error instanceof Error ? {
|
|
439
479
|
...error,
|
|
440
|
-
code:
|
|
480
|
+
code: 'code' in error ? error.code : undefined,
|
|
441
481
|
stack: error.stack,
|
|
442
482
|
type: error.name
|
|
443
483
|
} : error,
|
|
444
484
|
// @ts-ignore
|
|
445
485
|
message: error.message
|
|
446
486
|
},
|
|
447
|
-
id
|
|
448
|
-
jsonrpc: Two
|
|
487
|
+
id,
|
|
488
|
+
jsonrpc: Two$1
|
|
449
489
|
};
|
|
450
490
|
};
|
|
451
|
-
|
|
491
|
+
const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
|
|
452
492
|
try {
|
|
453
|
-
const result =
|
|
493
|
+
const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
|
|
454
494
|
return getSuccessResponse(message, result);
|
|
455
495
|
} catch (error) {
|
|
456
496
|
if (ipc.canUseSimpleErrorResponse) {
|
|
457
497
|
return getErrorResponseSimple(message.id, error);
|
|
458
498
|
}
|
|
459
|
-
return getErrorResponse(message.id, error,
|
|
499
|
+
return getErrorResponse(message.id, error, preparePrettyError, logError);
|
|
460
500
|
}
|
|
461
501
|
};
|
|
462
|
-
|
|
502
|
+
const defaultPreparePrettyError = error => {
|
|
463
503
|
return error;
|
|
464
504
|
};
|
|
465
|
-
|
|
505
|
+
const defaultLogError = () => {
|
|
506
|
+
// ignore
|
|
466
507
|
};
|
|
467
|
-
|
|
508
|
+
const defaultRequiresSocket = () => {
|
|
468
509
|
return false;
|
|
469
510
|
};
|
|
470
|
-
|
|
471
|
-
|
|
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 => {
|
|
472
515
|
if (args.length === 1) {
|
|
473
516
|
const options = args[0];
|
|
474
517
|
return {
|
|
@@ -491,79 +534,82 @@ var normalizeParams = (args) => {
|
|
|
491
534
|
resolve: args[3]
|
|
492
535
|
};
|
|
493
536
|
};
|
|
494
|
-
|
|
537
|
+
const handleJsonRpcMessage = async (...args) => {
|
|
495
538
|
const options = normalizeParams(args);
|
|
496
539
|
const {
|
|
497
|
-
execute
|
|
540
|
+
execute,
|
|
498
541
|
ipc,
|
|
499
|
-
logError
|
|
542
|
+
logError,
|
|
500
543
|
message,
|
|
501
|
-
preparePrettyError
|
|
502
|
-
requiresSocket
|
|
503
|
-
resolve
|
|
544
|
+
preparePrettyError,
|
|
545
|
+
requiresSocket,
|
|
546
|
+
resolve
|
|
504
547
|
} = options;
|
|
505
|
-
if (
|
|
506
|
-
if (
|
|
507
|
-
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);
|
|
508
551
|
try {
|
|
509
552
|
ipc.send(response);
|
|
510
553
|
} catch (error) {
|
|
511
|
-
const errorResponse = getErrorResponse(message.id, error,
|
|
554
|
+
const errorResponse = getErrorResponse(message.id, error, preparePrettyError, logError);
|
|
512
555
|
ipc.send(errorResponse);
|
|
513
556
|
}
|
|
514
557
|
return;
|
|
515
558
|
}
|
|
516
|
-
|
|
559
|
+
resolve(message.id, message);
|
|
517
560
|
return;
|
|
518
561
|
}
|
|
519
|
-
if (
|
|
520
|
-
await getResponse(message, ipc,
|
|
562
|
+
if ('method' in message) {
|
|
563
|
+
await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
521
564
|
return;
|
|
522
565
|
}
|
|
523
|
-
throw new JsonRpcError(
|
|
566
|
+
throw new JsonRpcError('unexpected message');
|
|
524
567
|
};
|
|
525
568
|
|
|
526
|
-
|
|
527
|
-
var Two2 = "2.0";
|
|
569
|
+
const Two = '2.0';
|
|
528
570
|
|
|
529
|
-
|
|
530
|
-
var create2 = (method, params) => {
|
|
571
|
+
const create$4 = (method, params) => {
|
|
531
572
|
return {
|
|
532
|
-
jsonrpc:
|
|
573
|
+
jsonrpc: Two,
|
|
533
574
|
method,
|
|
534
575
|
params
|
|
535
576
|
};
|
|
536
577
|
};
|
|
537
578
|
|
|
538
|
-
|
|
539
|
-
var create3 = (id2, method, params) => {
|
|
579
|
+
const create$3 = (id, method, params) => {
|
|
540
580
|
const message = {
|
|
541
|
-
id
|
|
542
|
-
jsonrpc:
|
|
581
|
+
id,
|
|
582
|
+
jsonrpc: Two,
|
|
543
583
|
method,
|
|
544
584
|
params
|
|
545
585
|
};
|
|
546
586
|
return message;
|
|
547
587
|
};
|
|
548
588
|
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
var create4 = () => {
|
|
589
|
+
let id = 0;
|
|
590
|
+
const create$2 = () => {
|
|
552
591
|
return ++id;
|
|
553
592
|
};
|
|
554
593
|
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
const
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
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
|
+
};
|
|
561
605
|
};
|
|
562
606
|
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
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);
|
|
567
613
|
if (useSendAndTransfer && ipc.sendAndTransfer) {
|
|
568
614
|
ipc.sendAndTransfer(message);
|
|
569
615
|
} else {
|
|
@@ -572,26 +618,26 @@ var invokeHelper = async (callbacks2, ipc, method, params, useSendAndTransfer) =
|
|
|
572
618
|
const responseMessage = await promise;
|
|
573
619
|
return unwrapJsonRpcResult(responseMessage);
|
|
574
620
|
};
|
|
575
|
-
|
|
576
|
-
const
|
|
577
|
-
ipc._resolve = (
|
|
578
|
-
const fn =
|
|
621
|
+
const createRpc = ipc => {
|
|
622
|
+
const callbacks = Object.create(null);
|
|
623
|
+
ipc._resolve = (id, response) => {
|
|
624
|
+
const fn = callbacks[id];
|
|
579
625
|
if (!fn) {
|
|
580
|
-
console.warn(`callback ${
|
|
626
|
+
console.warn(`callback ${id} may already be disposed`);
|
|
581
627
|
return;
|
|
582
628
|
}
|
|
583
629
|
fn(response);
|
|
584
|
-
delete
|
|
630
|
+
delete callbacks[id];
|
|
585
631
|
};
|
|
586
632
|
const rpc = {
|
|
587
633
|
async dispose() {
|
|
588
634
|
await ipc?.dispose();
|
|
589
635
|
},
|
|
590
636
|
invoke(method, ...params) {
|
|
591
|
-
return invokeHelper(
|
|
637
|
+
return invokeHelper(callbacks, ipc, method, params, false);
|
|
592
638
|
},
|
|
593
639
|
invokeAndTransfer(method, ...params) {
|
|
594
|
-
return invokeHelper(
|
|
640
|
+
return invokeHelper(callbacks, ipc, method, params, true);
|
|
595
641
|
},
|
|
596
642
|
// @ts-ignore
|
|
597
643
|
ipc,
|
|
@@ -599,24 +645,24 @@ var createRpc = (ipc) => {
|
|
|
599
645
|
* @deprecated
|
|
600
646
|
*/
|
|
601
647
|
send(method, ...params) {
|
|
602
|
-
const message =
|
|
648
|
+
const message = create$4(method, params);
|
|
603
649
|
ipc.send(message);
|
|
604
650
|
}
|
|
605
651
|
};
|
|
606
652
|
return rpc;
|
|
607
653
|
};
|
|
608
654
|
|
|
609
|
-
|
|
610
|
-
var requiresSocket = () => {
|
|
655
|
+
const requiresSocket = () => {
|
|
611
656
|
return false;
|
|
612
657
|
};
|
|
613
|
-
|
|
658
|
+
const preparePrettyError = error => {
|
|
614
659
|
return error;
|
|
615
660
|
};
|
|
616
|
-
|
|
661
|
+
const logError = () => {
|
|
662
|
+
// handled by renderer worker
|
|
617
663
|
};
|
|
618
|
-
|
|
619
|
-
if (event.data ===
|
|
664
|
+
const handleMessage = event => {
|
|
665
|
+
if (event.data === 'ready') {
|
|
620
666
|
return Promise.resolve();
|
|
621
667
|
}
|
|
622
668
|
const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
|
|
@@ -624,17 +670,16 @@ var handleMessage = (event) => {
|
|
|
624
670
|
return handleJsonRpcMessage(event.target, event.data, actualExecute, event.target._resolve, preparePrettyError, logError, actualRequiresSocket);
|
|
625
671
|
};
|
|
626
672
|
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
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);
|
|
633
679
|
}
|
|
634
680
|
};
|
|
635
681
|
|
|
636
|
-
|
|
637
|
-
var listen = async (module, options) => {
|
|
682
|
+
const listen$1 = async (module, options) => {
|
|
638
683
|
const rawIpc = await module.listen(options);
|
|
639
684
|
if (module.signal) {
|
|
640
685
|
module.signal(rawIpc);
|
|
@@ -643,38 +688,51 @@ var listen = async (module, options) => {
|
|
|
643
688
|
return ipc;
|
|
644
689
|
};
|
|
645
690
|
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
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
|
+
});
|
|
700
|
+
handleIpc(ipc);
|
|
701
|
+
const rpc = createRpc(ipc);
|
|
702
|
+
return rpc;
|
|
703
|
+
};
|
|
704
|
+
|
|
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);
|
|
654
711
|
handleIpc(ipc);
|
|
655
712
|
const rpc = createRpc(ipc);
|
|
656
713
|
return rpc;
|
|
657
714
|
};
|
|
658
715
|
|
|
659
|
-
|
|
660
|
-
var getCache = async (cacheName, bucketName, bucketOptions) => {
|
|
716
|
+
const getCache = async (cacheName, bucketName, bucketOptions) => {
|
|
661
717
|
if (bucketName) {
|
|
662
|
-
if (typeof navigator ===
|
|
663
|
-
throw new Error(
|
|
718
|
+
if (typeof navigator === 'undefined') {
|
|
719
|
+
throw new Error('Storage Buckets are not available in this context');
|
|
664
720
|
}
|
|
665
|
-
const {
|
|
721
|
+
const {
|
|
722
|
+
storageBuckets
|
|
723
|
+
} = navigator;
|
|
666
724
|
if (!storageBuckets) {
|
|
667
|
-
throw new Error(
|
|
725
|
+
throw new Error('Storage Buckets are not available in this context');
|
|
668
726
|
}
|
|
669
727
|
const bucket = await storageBuckets.open(bucketName, bucketOptions);
|
|
670
728
|
return bucket.caches.open(cacheName);
|
|
671
729
|
}
|
|
672
|
-
if (typeof caches ===
|
|
673
|
-
throw new Error(
|
|
730
|
+
if (typeof caches === 'undefined') {
|
|
731
|
+
throw new Error('Cache Storage is not available in this context');
|
|
674
732
|
}
|
|
675
733
|
return caches.open(cacheName);
|
|
676
734
|
};
|
|
677
|
-
|
|
735
|
+
const getCacheStorageItem = async (request, cacheName = 'lvce-cache', bucketName = '', bucketOptions = {}) => {
|
|
678
736
|
const cache = await getCache(cacheName, bucketName, bucketOptions);
|
|
679
737
|
const response = await cache.match(request);
|
|
680
738
|
if (!response) {
|
|
@@ -687,117 +745,421 @@ var getCacheStorageItem = async (request, cacheName = "lvce-cache", bucketName =
|
|
|
687
745
|
statusText: response.statusText
|
|
688
746
|
};
|
|
689
747
|
};
|
|
690
|
-
|
|
748
|
+
const setCacheStorageItem = async (request, value, cacheName = 'lvce-cache', headers = {}, bucketName = '', bucketOptions = {}) => {
|
|
691
749
|
try {
|
|
692
750
|
const cache = await getCache(cacheName, bucketName, bucketOptions);
|
|
693
|
-
const response = new Response(value, {
|
|
751
|
+
const response = new Response(value, {
|
|
752
|
+
headers: new Headers(headers)
|
|
753
|
+
});
|
|
694
754
|
await cache.put(request, response);
|
|
695
|
-
return {
|
|
755
|
+
return {
|
|
756
|
+
success: true
|
|
757
|
+
};
|
|
696
758
|
} catch (error) {
|
|
697
759
|
return {
|
|
698
|
-
errorCode:
|
|
760
|
+
errorCode: 'CACHE_STORAGE_WRITE_FAILED',
|
|
699
761
|
errorMessage: error instanceof Error ? error.message : String(error),
|
|
700
762
|
success: false
|
|
701
763
|
};
|
|
702
764
|
}
|
|
703
765
|
};
|
|
704
|
-
|
|
766
|
+
const removeCacheStorageItem = async (request, cacheName = 'lvce-cache', bucketName = '', bucketOptions = {}) => {
|
|
705
767
|
const cache = await getCache(cacheName, bucketName, bucketOptions);
|
|
706
768
|
return cache.delete(request);
|
|
707
769
|
};
|
|
708
770
|
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
}
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
const
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
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);
|
|
790
|
+
};
|
|
791
|
+
const success = () => {
|
|
792
|
+
resolve(wrap(request.result));
|
|
793
|
+
unlisten();
|
|
730
794
|
};
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
795
|
+
const error = () => {
|
|
796
|
+
reject(request.error);
|
|
797
|
+
unlisten();
|
|
734
798
|
};
|
|
735
|
-
request.
|
|
736
|
-
request.
|
|
799
|
+
request.addEventListener('success', success);
|
|
800
|
+
request.addEventListener('error', error);
|
|
737
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
|
+
}
|
|
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
|
+
}
|
|
738
1064
|
};
|
|
739
|
-
|
|
1065
|
+
const addIndexedDbFileHandle = async (key, handle, databaseName = 'lvce-cache-worker') => {
|
|
740
1066
|
const database = await openDatabase(databaseName);
|
|
741
|
-
const transaction = database.transaction(
|
|
742
|
-
const done = transactionDone(transaction);
|
|
743
|
-
const request = transaction.objectStore("file-handles").put(handle, cloneDatabaseKey(key));
|
|
1067
|
+
const transaction = database.transaction('file-handles', 'readwrite');
|
|
744
1068
|
try {
|
|
745
|
-
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;
|
|
746
1083
|
} finally {
|
|
747
1084
|
database.close();
|
|
748
1085
|
}
|
|
749
1086
|
};
|
|
750
|
-
|
|
1087
|
+
const getIndexedDbFileHandle = async (key, databaseName = 'lvce-cache-worker') => {
|
|
751
1088
|
const database = await openDatabase(databaseName);
|
|
752
|
-
const transaction = database.transaction(
|
|
753
|
-
const done = transactionDone(transaction);
|
|
1089
|
+
const transaction = database.transaction('file-handles', 'readonly');
|
|
754
1090
|
try {
|
|
755
|
-
const value = await
|
|
756
|
-
await done;
|
|
1091
|
+
const value = await transaction.store.get(cloneDatabaseKey(key));
|
|
1092
|
+
await transaction.done;
|
|
757
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;
|
|
758
1106
|
} finally {
|
|
759
1107
|
database.close();
|
|
760
1108
|
}
|
|
761
1109
|
};
|
|
762
|
-
|
|
1110
|
+
const removeIndexedDbFileHandle = async (key, databaseName = 'lvce-cache-worker') => {
|
|
763
1111
|
const database = await openDatabase(databaseName);
|
|
764
|
-
const transaction = database.transaction(
|
|
765
|
-
const done = transactionDone(transaction);
|
|
766
|
-
const request = transaction.objectStore("file-handles").delete(cloneDatabaseKey(key));
|
|
1112
|
+
const transaction = database.transaction('file-handles', 'readwrite');
|
|
767
1113
|
try {
|
|
768
|
-
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;
|
|
769
1128
|
} finally {
|
|
770
1129
|
database.close();
|
|
771
1130
|
}
|
|
772
1131
|
};
|
|
773
1132
|
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
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');
|
|
778
1136
|
}
|
|
779
|
-
const {
|
|
780
|
-
|
|
781
|
-
|
|
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');
|
|
782
1142
|
}
|
|
783
1143
|
return storage.getDirectory();
|
|
784
1144
|
};
|
|
785
|
-
|
|
786
|
-
if (typeof name !==
|
|
787
|
-
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');
|
|
788
1148
|
}
|
|
789
1149
|
};
|
|
790
|
-
|
|
1150
|
+
const readFile = async name => {
|
|
791
1151
|
validateName(name);
|
|
792
1152
|
const root = await getRoot();
|
|
793
1153
|
const handle = await root.getFileHandle(name);
|
|
794
1154
|
const file = await handle.getFile();
|
|
795
1155
|
return file.text();
|
|
796
1156
|
};
|
|
797
|
-
|
|
1157
|
+
const writeFile = async (name, content) => {
|
|
798
1158
|
validateName(name);
|
|
799
1159
|
const root = await getRoot();
|
|
800
|
-
const handle = await root.getFileHandle(name, {
|
|
1160
|
+
const handle = await root.getFileHandle(name, {
|
|
1161
|
+
create: true
|
|
1162
|
+
});
|
|
801
1163
|
const writable = await handle.createWritable();
|
|
802
1164
|
try {
|
|
803
1165
|
await writable.write(content);
|
|
@@ -807,29 +1169,36 @@ var writeFile = async (name, content) => {
|
|
|
807
1169
|
}
|
|
808
1170
|
await writable.close();
|
|
809
1171
|
};
|
|
810
|
-
|
|
1172
|
+
const removeFile = async name => {
|
|
811
1173
|
validateName(name);
|
|
812
1174
|
const root = await getRoot();
|
|
813
1175
|
await root.removeEntry(name);
|
|
814
1176
|
};
|
|
815
1177
|
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
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
|
|
827
1190
|
};
|
|
828
1191
|
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
1192
|
+
const listen = async () => {
|
|
1193
|
+
await create({
|
|
1194
|
+
commandMap: commandMap
|
|
1195
|
+
});
|
|
1196
|
+
};
|
|
1197
|
+
const handleMessagePort = async messagePort => {
|
|
1198
|
+
await create$1({
|
|
1199
|
+
commandMap: commandMap,
|
|
1200
|
+
messagePort
|
|
1201
|
+
});
|
|
832
1202
|
};
|
|
833
1203
|
|
|
834
|
-
|
|
835
|
-
await listen2();
|
|
1204
|
+
await listen();
|