@lvce-editor/iframe-worker 1.0.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/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/iframeWorkerMain.js +692 -0
- package/package.json +10 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Lvce Editor
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,692 @@
|
|
|
1
|
+
const createUrl = (protocol, host) => {
|
|
2
|
+
return protocol + '//' + host;
|
|
3
|
+
};
|
|
4
|
+
|
|
5
|
+
const getWebViewFrameAncestors = (locationProtocol, locationHost) => {
|
|
6
|
+
const frameAncestors = createUrl(locationProtocol, locationHost);
|
|
7
|
+
return frameAncestors;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
const getOrigin = () => {
|
|
11
|
+
return location.origin;
|
|
12
|
+
};
|
|
13
|
+
const getHost = () => {
|
|
14
|
+
return location.host;
|
|
15
|
+
};
|
|
16
|
+
const getProtocol = () => {
|
|
17
|
+
return location.protocol;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const commandMap = {
|
|
21
|
+
'Location.getProtocol': getProtocol,
|
|
22
|
+
'Location.getHost': getHost,
|
|
23
|
+
'Location.getOrigin': getOrigin,
|
|
24
|
+
'WebView.getFrameAncestors': getWebViewFrameAncestors
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const state = {
|
|
28
|
+
commands: Object.create(null)
|
|
29
|
+
};
|
|
30
|
+
const registerCommand = (key, fn) => {
|
|
31
|
+
state.commands[key] = fn;
|
|
32
|
+
};
|
|
33
|
+
const registerCommands = commandMap => {
|
|
34
|
+
for (const [key, value] of Object.entries(commandMap)) {
|
|
35
|
+
registerCommand(key, value);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const getCommand = key => {
|
|
39
|
+
return state.commands[key];
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
const Two = '2.0';
|
|
43
|
+
class AssertionError extends Error {
|
|
44
|
+
constructor(message) {
|
|
45
|
+
super(message);
|
|
46
|
+
this.name = 'AssertionError';
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const getType$1 = value => {
|
|
50
|
+
switch (typeof value) {
|
|
51
|
+
case 'number':
|
|
52
|
+
return 'number';
|
|
53
|
+
case 'function':
|
|
54
|
+
return 'function';
|
|
55
|
+
case 'string':
|
|
56
|
+
return 'string';
|
|
57
|
+
case 'object':
|
|
58
|
+
if (value === null) {
|
|
59
|
+
return 'null';
|
|
60
|
+
}
|
|
61
|
+
if (Array.isArray(value)) {
|
|
62
|
+
return 'array';
|
|
63
|
+
}
|
|
64
|
+
return 'object';
|
|
65
|
+
case 'boolean':
|
|
66
|
+
return 'boolean';
|
|
67
|
+
default:
|
|
68
|
+
return 'unknown';
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
const number = value => {
|
|
72
|
+
const type = getType$1(value);
|
|
73
|
+
if (type !== 'number') {
|
|
74
|
+
throw new AssertionError('expected value to be of type number');
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const state$1 = {
|
|
78
|
+
callbacks: Object.create(null)
|
|
79
|
+
};
|
|
80
|
+
const get = id => {
|
|
81
|
+
return state$1.callbacks[id];
|
|
82
|
+
};
|
|
83
|
+
const remove = id => {
|
|
84
|
+
delete state$1.callbacks[id];
|
|
85
|
+
};
|
|
86
|
+
const warn = (...args) => {
|
|
87
|
+
console.warn(...args);
|
|
88
|
+
};
|
|
89
|
+
const resolve = (id, args) => {
|
|
90
|
+
number(id);
|
|
91
|
+
const fn = get(id);
|
|
92
|
+
if (!fn) {
|
|
93
|
+
console.log(args);
|
|
94
|
+
warn(`callback ${id} may already be disposed`);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
fn(args);
|
|
98
|
+
remove(id);
|
|
99
|
+
};
|
|
100
|
+
class JsonRpcError extends Error {
|
|
101
|
+
constructor(message) {
|
|
102
|
+
super(message);
|
|
103
|
+
this.name = 'JsonRpcError';
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
const MethodNotFound = -32601;
|
|
107
|
+
const Custom = -32001;
|
|
108
|
+
const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
|
|
109
|
+
const getType = prettyError => {
|
|
110
|
+
if (prettyError && prettyError.type) {
|
|
111
|
+
return prettyError.type;
|
|
112
|
+
}
|
|
113
|
+
if (prettyError && prettyError.constructor && prettyError.constructor.name) {
|
|
114
|
+
return prettyError.constructor.name;
|
|
115
|
+
}
|
|
116
|
+
return undefined;
|
|
117
|
+
};
|
|
118
|
+
const getErrorProperty = (error, prettyError) => {
|
|
119
|
+
if (error && error.code === E_COMMAND_NOT_FOUND) {
|
|
120
|
+
return {
|
|
121
|
+
code: MethodNotFound,
|
|
122
|
+
message: error.message,
|
|
123
|
+
data: error.stack
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
code: Custom,
|
|
128
|
+
message: prettyError.message,
|
|
129
|
+
data: {
|
|
130
|
+
stack: prettyError.stack,
|
|
131
|
+
codeFrame: prettyError.codeFrame,
|
|
132
|
+
type: getType(prettyError),
|
|
133
|
+
code: prettyError.code,
|
|
134
|
+
name: prettyError.name
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
const create$1 = (message, error) => {
|
|
139
|
+
return {
|
|
140
|
+
jsonrpc: Two,
|
|
141
|
+
id: message.id,
|
|
142
|
+
error
|
|
143
|
+
};
|
|
144
|
+
};
|
|
145
|
+
const getErrorResponse = (message, error, preparePrettyError, logError) => {
|
|
146
|
+
const prettyError = preparePrettyError(error);
|
|
147
|
+
logError(error, prettyError);
|
|
148
|
+
const errorProperty = getErrorProperty(error, prettyError);
|
|
149
|
+
return create$1(message, errorProperty);
|
|
150
|
+
};
|
|
151
|
+
const create = (message, result) => {
|
|
152
|
+
return {
|
|
153
|
+
jsonrpc: Two,
|
|
154
|
+
id: message.id,
|
|
155
|
+
result: result ?? null
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
const getSuccessResponse = (message, result) => {
|
|
159
|
+
const resultProperty = result ?? null;
|
|
160
|
+
return create(message, resultProperty);
|
|
161
|
+
};
|
|
162
|
+
const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
|
|
163
|
+
try {
|
|
164
|
+
const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
|
|
165
|
+
return getSuccessResponse(message, result);
|
|
166
|
+
} catch (error) {
|
|
167
|
+
return getErrorResponse(message, error, preparePrettyError, logError);
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
const defaultPreparePrettyError = error => {
|
|
171
|
+
return error;
|
|
172
|
+
};
|
|
173
|
+
const defaultLogError = () => {
|
|
174
|
+
// ignore
|
|
175
|
+
};
|
|
176
|
+
const defaultRequiresSocket = () => {
|
|
177
|
+
return false;
|
|
178
|
+
};
|
|
179
|
+
const defaultResolve = resolve;
|
|
180
|
+
const handleJsonRpcMessage = async (...args) => {
|
|
181
|
+
let message;
|
|
182
|
+
let ipc;
|
|
183
|
+
let execute;
|
|
184
|
+
let preparePrettyError;
|
|
185
|
+
let logError;
|
|
186
|
+
let resolve;
|
|
187
|
+
let requiresSocket;
|
|
188
|
+
if (args.length === 1) {
|
|
189
|
+
const arg = args[0];
|
|
190
|
+
message = arg.message;
|
|
191
|
+
ipc = arg.ipc;
|
|
192
|
+
execute = arg.execute;
|
|
193
|
+
preparePrettyError = arg.preparePrettyError || defaultPreparePrettyError;
|
|
194
|
+
logError = arg.logError || defaultLogError;
|
|
195
|
+
requiresSocket = arg.requiresSocket || defaultRequiresSocket;
|
|
196
|
+
resolve = arg.resolve || defaultResolve;
|
|
197
|
+
} else {
|
|
198
|
+
ipc = args[0];
|
|
199
|
+
message = args[1];
|
|
200
|
+
execute = args[2];
|
|
201
|
+
resolve = args[3];
|
|
202
|
+
preparePrettyError = args[4];
|
|
203
|
+
logError = args[5];
|
|
204
|
+
requiresSocket = args[6];
|
|
205
|
+
}
|
|
206
|
+
if ('id' in message) {
|
|
207
|
+
if ('method' in message) {
|
|
208
|
+
const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
209
|
+
try {
|
|
210
|
+
ipc.send(response);
|
|
211
|
+
} catch (error) {
|
|
212
|
+
const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
|
|
213
|
+
ipc.send(errorResponse);
|
|
214
|
+
}
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
resolve(message.id, message);
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
if ('method' in message) {
|
|
221
|
+
await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
throw new JsonRpcError('unexpected message');
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const execute = (command, ...args) => {
|
|
228
|
+
const fn = getCommand(command);
|
|
229
|
+
if (!fn) {
|
|
230
|
+
throw new Error(`[iframe-worker] command not found ${command}`);
|
|
231
|
+
}
|
|
232
|
+
return fn(...args);
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
const requiresSocket = () => {
|
|
236
|
+
return false;
|
|
237
|
+
};
|
|
238
|
+
const preparePrettyError = error => {
|
|
239
|
+
return error;
|
|
240
|
+
};
|
|
241
|
+
const logError = error => {
|
|
242
|
+
console.error(error);
|
|
243
|
+
};
|
|
244
|
+
const handleMessage = event => {
|
|
245
|
+
return handleJsonRpcMessage(event.target, event.data, execute, resolve, preparePrettyError, logError, requiresSocket);
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
const handleIpc = ipc => {
|
|
249
|
+
ipc.addEventListener('message', handleMessage);
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const MessagePort$1 = 1;
|
|
253
|
+
const ModuleWorker = 2;
|
|
254
|
+
const ReferencePort = 3;
|
|
255
|
+
const ModuleWorkerAndMessagePort = 8;
|
|
256
|
+
const Auto = () => {
|
|
257
|
+
// @ts-ignore
|
|
258
|
+
if (globalThis.acceptPort) {
|
|
259
|
+
return MessagePort$1;
|
|
260
|
+
}
|
|
261
|
+
// @ts-ignore
|
|
262
|
+
if (globalThis.acceptReferencePort) {
|
|
263
|
+
return ReferencePort;
|
|
264
|
+
}
|
|
265
|
+
return ModuleWorkerAndMessagePort;
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
const getData$1 = event => {
|
|
269
|
+
return event.data;
|
|
270
|
+
};
|
|
271
|
+
const walkValue = (value, transferrables, isTransferrable) => {
|
|
272
|
+
if (!value) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
if (isTransferrable(value)) {
|
|
276
|
+
transferrables.push(value);
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
if (Array.isArray(value)) {
|
|
280
|
+
for (const item of value) {
|
|
281
|
+
walkValue(item, transferrables, isTransferrable);
|
|
282
|
+
}
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
if (typeof value === 'object') {
|
|
286
|
+
for (const property of Object.values(value)) {
|
|
287
|
+
walkValue(property, transferrables, isTransferrable);
|
|
288
|
+
}
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
const isMessagePort = value => {
|
|
293
|
+
return value && value instanceof MessagePort;
|
|
294
|
+
};
|
|
295
|
+
const isMessagePortMain = value => {
|
|
296
|
+
return value && value.constructor && value.constructor.name === 'MessagePortMain';
|
|
297
|
+
};
|
|
298
|
+
const isOffscreenCanvas = value => {
|
|
299
|
+
return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
|
|
300
|
+
};
|
|
301
|
+
const isInstanceOf = (value, constructorName) => {
|
|
302
|
+
return value?.constructor?.name === constructorName;
|
|
303
|
+
};
|
|
304
|
+
const isSocket = value => {
|
|
305
|
+
return isInstanceOf(value, 'Socket');
|
|
306
|
+
};
|
|
307
|
+
const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
|
|
308
|
+
const isTransferrable = value => {
|
|
309
|
+
for (const fn of transferrables) {
|
|
310
|
+
if (fn(value)) {
|
|
311
|
+
return true;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return false;
|
|
315
|
+
};
|
|
316
|
+
const getTransferrables = value => {
|
|
317
|
+
const transferrables = [];
|
|
318
|
+
walkValue(value, transferrables, isTransferrable);
|
|
319
|
+
return transferrables;
|
|
320
|
+
};
|
|
321
|
+
const attachEvents = that => {
|
|
322
|
+
const handleMessage = (...args) => {
|
|
323
|
+
const data = that.getData(...args);
|
|
324
|
+
that.dispatchEvent(new MessageEvent('message', {
|
|
325
|
+
data
|
|
326
|
+
}));
|
|
327
|
+
};
|
|
328
|
+
that.onMessage(handleMessage);
|
|
329
|
+
const handleClose = event => {
|
|
330
|
+
that.dispatchEvent(new Event('close'));
|
|
331
|
+
};
|
|
332
|
+
that.onClose(handleClose);
|
|
333
|
+
};
|
|
334
|
+
class Ipc extends EventTarget {
|
|
335
|
+
constructor(rawIpc) {
|
|
336
|
+
super();
|
|
337
|
+
this._rawIpc = rawIpc;
|
|
338
|
+
attachEvents(this);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
const readyMessage = 'ready';
|
|
342
|
+
const listen$4 = () => {
|
|
343
|
+
// @ts-ignore
|
|
344
|
+
if (typeof WorkerGlobalScope === 'undefined') {
|
|
345
|
+
throw new TypeError('module is not in web worker scope');
|
|
346
|
+
}
|
|
347
|
+
return globalThis;
|
|
348
|
+
};
|
|
349
|
+
const signal$3 = global => {
|
|
350
|
+
global.postMessage(readyMessage);
|
|
351
|
+
};
|
|
352
|
+
class IpcChildWithModuleWorker extends Ipc {
|
|
353
|
+
getData(event) {
|
|
354
|
+
return getData$1(event);
|
|
355
|
+
}
|
|
356
|
+
send(message) {
|
|
357
|
+
// @ts-ignore
|
|
358
|
+
this._rawIpc.postMessage(message);
|
|
359
|
+
}
|
|
360
|
+
sendAndTransfer(message) {
|
|
361
|
+
const transfer = getTransferrables(message);
|
|
362
|
+
// @ts-ignore
|
|
363
|
+
this._rawIpc.postMessage(message, transfer);
|
|
364
|
+
}
|
|
365
|
+
dispose() {
|
|
366
|
+
// ignore
|
|
367
|
+
}
|
|
368
|
+
onClose(callback) {
|
|
369
|
+
// ignore
|
|
370
|
+
}
|
|
371
|
+
onMessage(callback) {
|
|
372
|
+
this._rawIpc.addEventListener('message', callback);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
const wrap$6 = global => {
|
|
376
|
+
return new IpcChildWithModuleWorker(global);
|
|
377
|
+
};
|
|
378
|
+
const IpcChildWithModuleWorker$1 = {
|
|
379
|
+
__proto__: null,
|
|
380
|
+
listen: listen$4,
|
|
381
|
+
signal: signal$3,
|
|
382
|
+
wrap: wrap$6
|
|
383
|
+
};
|
|
384
|
+
const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
|
|
385
|
+
const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
|
|
386
|
+
const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
|
|
387
|
+
const NewLine$1 = '\n';
|
|
388
|
+
const joinLines = lines => {
|
|
389
|
+
return lines.join(NewLine$1);
|
|
390
|
+
};
|
|
391
|
+
const splitLines = lines => {
|
|
392
|
+
return lines.split(NewLine$1);
|
|
393
|
+
};
|
|
394
|
+
const isModuleNotFoundMessage = line => {
|
|
395
|
+
return line.includes('[ERR_MODULE_NOT_FOUND]');
|
|
396
|
+
};
|
|
397
|
+
const getModuleNotFoundError = stderr => {
|
|
398
|
+
const lines = splitLines(stderr);
|
|
399
|
+
const messageIndex = lines.findIndex(isModuleNotFoundMessage);
|
|
400
|
+
const message = lines[messageIndex];
|
|
401
|
+
return {
|
|
402
|
+
message,
|
|
403
|
+
code: ERR_MODULE_NOT_FOUND
|
|
404
|
+
};
|
|
405
|
+
};
|
|
406
|
+
const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
|
|
407
|
+
const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
|
|
408
|
+
const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
|
|
409
|
+
const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
|
|
410
|
+
const RE_AT = /^\s+at/;
|
|
411
|
+
const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
|
|
412
|
+
const isUnhelpfulNativeModuleError = stderr => {
|
|
413
|
+
return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
|
|
414
|
+
};
|
|
415
|
+
const isMessageCodeBlockStartIndex = line => {
|
|
416
|
+
return RE_MESSAGE_CODE_BLOCK_START.test(line);
|
|
417
|
+
};
|
|
418
|
+
const isMessageCodeBlockEndIndex = line => {
|
|
419
|
+
return RE_MESSAGE_CODE_BLOCK_END.test(line);
|
|
420
|
+
};
|
|
421
|
+
const getMessageCodeBlock = stderr => {
|
|
422
|
+
const lines = splitLines(stderr);
|
|
423
|
+
const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
|
|
424
|
+
const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
|
|
425
|
+
const relevantLines = lines.slice(startIndex, endIndex);
|
|
426
|
+
const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
|
|
427
|
+
return relevantMessage;
|
|
428
|
+
};
|
|
429
|
+
const getNativeModuleErrorMessage = stderr => {
|
|
430
|
+
const message = getMessageCodeBlock(stderr);
|
|
431
|
+
return {
|
|
432
|
+
message: `Incompatible native node module: ${message}`,
|
|
433
|
+
code: E_INCOMPATIBLE_NATIVE_MODULE
|
|
434
|
+
};
|
|
435
|
+
};
|
|
436
|
+
const isModulesSyntaxError = stderr => {
|
|
437
|
+
if (!stderr) {
|
|
438
|
+
return false;
|
|
439
|
+
}
|
|
440
|
+
return stderr.includes('SyntaxError: Cannot use import statement outside a module');
|
|
441
|
+
};
|
|
442
|
+
const getModuleSyntaxError = () => {
|
|
443
|
+
return {
|
|
444
|
+
message: `ES Modules are not supported in electron`,
|
|
445
|
+
code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
|
|
446
|
+
};
|
|
447
|
+
};
|
|
448
|
+
const isModuleNotFoundError = stderr => {
|
|
449
|
+
if (!stderr) {
|
|
450
|
+
return false;
|
|
451
|
+
}
|
|
452
|
+
return stderr.includes('ERR_MODULE_NOT_FOUND');
|
|
453
|
+
};
|
|
454
|
+
const isNormalStackLine = line => {
|
|
455
|
+
return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
|
|
456
|
+
};
|
|
457
|
+
const getDetails = lines => {
|
|
458
|
+
const index = lines.findIndex(isNormalStackLine);
|
|
459
|
+
if (index === -1) {
|
|
460
|
+
return {
|
|
461
|
+
actualMessage: joinLines(lines),
|
|
462
|
+
rest: []
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
let lastIndex = index - 1;
|
|
466
|
+
while (++lastIndex < lines.length) {
|
|
467
|
+
if (!isNormalStackLine(lines[lastIndex])) {
|
|
468
|
+
break;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
return {
|
|
472
|
+
actualMessage: lines[index - 1],
|
|
473
|
+
rest: lines.slice(index, lastIndex)
|
|
474
|
+
};
|
|
475
|
+
};
|
|
476
|
+
const getHelpfulChildProcessError = (stdout, stderr) => {
|
|
477
|
+
if (isUnhelpfulNativeModuleError(stderr)) {
|
|
478
|
+
return getNativeModuleErrorMessage(stderr);
|
|
479
|
+
}
|
|
480
|
+
if (isModulesSyntaxError(stderr)) {
|
|
481
|
+
return getModuleSyntaxError();
|
|
482
|
+
}
|
|
483
|
+
if (isModuleNotFoundError(stderr)) {
|
|
484
|
+
return getModuleNotFoundError(stderr);
|
|
485
|
+
}
|
|
486
|
+
const lines = splitLines(stderr);
|
|
487
|
+
const {
|
|
488
|
+
actualMessage,
|
|
489
|
+
rest
|
|
490
|
+
} = getDetails(lines);
|
|
491
|
+
return {
|
|
492
|
+
message: `${actualMessage}`,
|
|
493
|
+
code: '',
|
|
494
|
+
stack: rest
|
|
495
|
+
};
|
|
496
|
+
};
|
|
497
|
+
const normalizeLine = line => {
|
|
498
|
+
if (line.startsWith('Error: ')) {
|
|
499
|
+
return line.slice(`Error: `.length);
|
|
500
|
+
}
|
|
501
|
+
if (line.startsWith('VError: ')) {
|
|
502
|
+
return line.slice(`VError: `.length);
|
|
503
|
+
}
|
|
504
|
+
return line;
|
|
505
|
+
};
|
|
506
|
+
const getCombinedMessage = (error, message) => {
|
|
507
|
+
const stringifiedError = normalizeLine(`${error}`);
|
|
508
|
+
if (message) {
|
|
509
|
+
return `${message}: ${stringifiedError}`;
|
|
510
|
+
}
|
|
511
|
+
return stringifiedError;
|
|
512
|
+
};
|
|
513
|
+
const NewLine = '\n';
|
|
514
|
+
const getNewLineIndex = (string, startIndex = undefined) => {
|
|
515
|
+
return string.indexOf(NewLine, startIndex);
|
|
516
|
+
};
|
|
517
|
+
const mergeStacks = (parent, child) => {
|
|
518
|
+
if (!child) {
|
|
519
|
+
return parent;
|
|
520
|
+
}
|
|
521
|
+
const parentNewLineIndex = getNewLineIndex(parent);
|
|
522
|
+
const childNewLineIndex = getNewLineIndex(child);
|
|
523
|
+
if (childNewLineIndex === -1) {
|
|
524
|
+
return parent;
|
|
525
|
+
}
|
|
526
|
+
const parentFirstLine = parent.slice(0, parentNewLineIndex);
|
|
527
|
+
const childRest = child.slice(childNewLineIndex);
|
|
528
|
+
const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
|
|
529
|
+
if (parentFirstLine.includes(childFirstLine)) {
|
|
530
|
+
return parentFirstLine + childRest;
|
|
531
|
+
}
|
|
532
|
+
return child;
|
|
533
|
+
};
|
|
534
|
+
class VError extends Error {
|
|
535
|
+
constructor(error, message) {
|
|
536
|
+
const combinedMessage = getCombinedMessage(error, message);
|
|
537
|
+
super(combinedMessage);
|
|
538
|
+
this.name = 'VError';
|
|
539
|
+
if (error instanceof Error) {
|
|
540
|
+
this.stack = mergeStacks(this.stack, error.stack);
|
|
541
|
+
}
|
|
542
|
+
if (error.codeFrame) {
|
|
543
|
+
// @ts-ignore
|
|
544
|
+
this.codeFrame = error.codeFrame;
|
|
545
|
+
}
|
|
546
|
+
if (error.code) {
|
|
547
|
+
// @ts-ignore
|
|
548
|
+
this.code = error.code;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
class IpcError extends VError {
|
|
553
|
+
// @ts-ignore
|
|
554
|
+
constructor(betterMessage, stdout = '', stderr = '') {
|
|
555
|
+
if (stdout || stderr) {
|
|
556
|
+
// @ts-ignore
|
|
557
|
+
const {
|
|
558
|
+
message,
|
|
559
|
+
code,
|
|
560
|
+
stack
|
|
561
|
+
} = getHelpfulChildProcessError(stdout, stderr);
|
|
562
|
+
const cause = new Error(message);
|
|
563
|
+
// @ts-ignore
|
|
564
|
+
cause.code = code;
|
|
565
|
+
cause.stack = stack;
|
|
566
|
+
super(cause, betterMessage);
|
|
567
|
+
} else {
|
|
568
|
+
super(betterMessage);
|
|
569
|
+
}
|
|
570
|
+
// @ts-ignore
|
|
571
|
+
this.name = 'IpcError';
|
|
572
|
+
// @ts-ignore
|
|
573
|
+
this.stdout = stdout;
|
|
574
|
+
// @ts-ignore
|
|
575
|
+
this.stderr = stderr;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
const withResolvers = () => {
|
|
579
|
+
let _resolve;
|
|
580
|
+
const promise = new Promise(resolve => {
|
|
581
|
+
_resolve = resolve;
|
|
582
|
+
});
|
|
583
|
+
return {
|
|
584
|
+
resolve: _resolve,
|
|
585
|
+
promise
|
|
586
|
+
};
|
|
587
|
+
};
|
|
588
|
+
const waitForFirstMessage = async port => {
|
|
589
|
+
const {
|
|
590
|
+
resolve,
|
|
591
|
+
promise
|
|
592
|
+
} = withResolvers();
|
|
593
|
+
port.addEventListener('message', resolve, {
|
|
594
|
+
once: true
|
|
595
|
+
});
|
|
596
|
+
const event = await promise;
|
|
597
|
+
// @ts-ignore
|
|
598
|
+
return event.data;
|
|
599
|
+
};
|
|
600
|
+
const listen$3 = async () => {
|
|
601
|
+
const parentIpcRaw = listen$4();
|
|
602
|
+
signal$3(parentIpcRaw);
|
|
603
|
+
const parentIpc = wrap$6(parentIpcRaw);
|
|
604
|
+
const firstMessage = await waitForFirstMessage(parentIpc);
|
|
605
|
+
if (firstMessage.method !== 'initialize') {
|
|
606
|
+
throw new IpcError('unexpected first message');
|
|
607
|
+
}
|
|
608
|
+
const type = firstMessage.params[0];
|
|
609
|
+
if (type === 'message-port') {
|
|
610
|
+
parentIpc.send({
|
|
611
|
+
jsonrpc: '2.0',
|
|
612
|
+
id: firstMessage.id,
|
|
613
|
+
result: null
|
|
614
|
+
});
|
|
615
|
+
parentIpc.dispose();
|
|
616
|
+
const port = firstMessage.params[1];
|
|
617
|
+
return port;
|
|
618
|
+
}
|
|
619
|
+
return globalThis;
|
|
620
|
+
};
|
|
621
|
+
class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
|
|
622
|
+
constructor(port) {
|
|
623
|
+
super(port);
|
|
624
|
+
}
|
|
625
|
+
getData(event) {
|
|
626
|
+
return getData$1(event);
|
|
627
|
+
}
|
|
628
|
+
send(message) {
|
|
629
|
+
this._rawIpc.postMessage(message);
|
|
630
|
+
}
|
|
631
|
+
sendAndTransfer(message) {
|
|
632
|
+
const transfer = getTransferrables(message);
|
|
633
|
+
this._rawIpc.postMessage(message, transfer);
|
|
634
|
+
}
|
|
635
|
+
dispose() {
|
|
636
|
+
if (this._rawIpc.close) {
|
|
637
|
+
this._rawIpc.close();
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
onClose(callback) {
|
|
641
|
+
// ignore
|
|
642
|
+
}
|
|
643
|
+
onMessage(callback) {
|
|
644
|
+
this._rawIpc.addEventListener('message', callback);
|
|
645
|
+
this._rawIpc.start();
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
const wrap$5 = port => {
|
|
649
|
+
return new IpcChildWithModuleWorkerAndMessagePort(port);
|
|
650
|
+
};
|
|
651
|
+
const IpcChildWithModuleWorkerAndMessagePort$1 = {
|
|
652
|
+
__proto__: null,
|
|
653
|
+
listen: listen$3,
|
|
654
|
+
wrap: wrap$5
|
|
655
|
+
};
|
|
656
|
+
|
|
657
|
+
const getModule = method => {
|
|
658
|
+
switch (method) {
|
|
659
|
+
case ModuleWorker:
|
|
660
|
+
return IpcChildWithModuleWorker$1;
|
|
661
|
+
case ModuleWorkerAndMessagePort:
|
|
662
|
+
return IpcChildWithModuleWorkerAndMessagePort$1;
|
|
663
|
+
default:
|
|
664
|
+
throw new Error('unexpected ipc type');
|
|
665
|
+
}
|
|
666
|
+
};
|
|
667
|
+
|
|
668
|
+
const listen$1 = async ({
|
|
669
|
+
method
|
|
670
|
+
}) => {
|
|
671
|
+
const module = await getModule(method);
|
|
672
|
+
const rawIpc = await module.listen();
|
|
673
|
+
if (module.signal) {
|
|
674
|
+
module.signal(rawIpc);
|
|
675
|
+
}
|
|
676
|
+
const ipc = module.wrap(rawIpc);
|
|
677
|
+
return ipc;
|
|
678
|
+
};
|
|
679
|
+
|
|
680
|
+
const listen = async () => {
|
|
681
|
+
registerCommands(commandMap);
|
|
682
|
+
const ipc = await listen$1({
|
|
683
|
+
method: Auto()
|
|
684
|
+
});
|
|
685
|
+
handleIpc(ipc);
|
|
686
|
+
};
|
|
687
|
+
|
|
688
|
+
const main = async () => {
|
|
689
|
+
await listen();
|
|
690
|
+
};
|
|
691
|
+
|
|
692
|
+
main();
|