@lvce-editor/ipc 3.6.0 → 3.7.1
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/dist/browser.js +221 -17
- package/dist/index.js +14 -7
- package/package.json +1 -1
package/dist/browser.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
const getData = event => {
|
|
1
|
+
const getData$1 = event => {
|
|
2
2
|
return event.data;
|
|
3
3
|
};
|
|
4
4
|
|
|
5
|
-
const
|
|
5
|
+
const readyMessage = 'ready';
|
|
6
|
+
|
|
7
|
+
const listen$2 = () => {
|
|
6
8
|
// @ts-ignore
|
|
7
9
|
if (typeof WorkerGlobalScope === 'undefined') {
|
|
8
10
|
throw new TypeError('module is not in web worker scope');
|
|
9
11
|
}
|
|
10
|
-
// @ts-ignore
|
|
11
|
-
globalThis.postMessage('ready');
|
|
12
12
|
return globalThis;
|
|
13
13
|
};
|
|
14
|
-
const signal = global => {
|
|
15
|
-
global.postMessage(
|
|
14
|
+
const signal$1 = global => {
|
|
15
|
+
global.postMessage(readyMessage);
|
|
16
16
|
};
|
|
17
|
-
const wrap$
|
|
17
|
+
const wrap$3 = global => {
|
|
18
18
|
return {
|
|
19
19
|
global,
|
|
20
20
|
/**
|
|
@@ -32,7 +32,7 @@ const wrap$1 = global => {
|
|
|
32
32
|
},
|
|
33
33
|
set onmessage(listener) {
|
|
34
34
|
const wrappedListener = event => {
|
|
35
|
-
const data = getData(event);
|
|
35
|
+
const data = getData$1(event);
|
|
36
36
|
// @ts-expect-error
|
|
37
37
|
listener({
|
|
38
38
|
data,
|
|
@@ -41,15 +41,20 @@ const wrap$1 = global => {
|
|
|
41
41
|
};
|
|
42
42
|
this.listener = listener;
|
|
43
43
|
this.global.onmessage = wrappedListener;
|
|
44
|
+
},
|
|
45
|
+
dispose() {
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
this.listener = null;
|
|
48
|
+
this.global.onmessage = null;
|
|
44
49
|
}
|
|
45
50
|
};
|
|
46
51
|
};
|
|
47
52
|
|
|
48
53
|
const IpcChildWithModuleWorker = {
|
|
49
54
|
__proto__: null,
|
|
50
|
-
listen: listen$
|
|
51
|
-
signal,
|
|
52
|
-
wrap: wrap$
|
|
55
|
+
listen: listen$2,
|
|
56
|
+
signal: signal$1,
|
|
57
|
+
wrap: wrap$3
|
|
53
58
|
};
|
|
54
59
|
|
|
55
60
|
const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
|
|
@@ -282,21 +287,23 @@ const waitForFirstMessage = async port => {
|
|
|
282
287
|
return event.data;
|
|
283
288
|
};
|
|
284
289
|
|
|
285
|
-
const listen = async () => {
|
|
286
|
-
const parentIpcRaw = listen$
|
|
287
|
-
|
|
290
|
+
const listen$1 = async () => {
|
|
291
|
+
const parentIpcRaw = listen$2();
|
|
292
|
+
signal$1(parentIpcRaw);
|
|
293
|
+
const parentIpc = wrap$3(parentIpcRaw);
|
|
288
294
|
const firstMessage = await waitForFirstMessage(parentIpc);
|
|
289
295
|
if (firstMessage.method !== 'initialize') {
|
|
290
296
|
throw new IpcError('unexpected first message');
|
|
291
297
|
}
|
|
292
298
|
const type = firstMessage.params[0];
|
|
293
299
|
if (type === 'message-port') {
|
|
300
|
+
parentIpc.dispose();
|
|
294
301
|
const port = firstMessage.params[1];
|
|
295
302
|
return port;
|
|
296
303
|
}
|
|
297
304
|
return globalThis;
|
|
298
305
|
};
|
|
299
|
-
const wrap = port => {
|
|
306
|
+
const wrap$2 = port => {
|
|
300
307
|
return {
|
|
301
308
|
port,
|
|
302
309
|
/**
|
|
@@ -316,7 +323,7 @@ const wrap = port => {
|
|
|
316
323
|
if (listener) {
|
|
317
324
|
// @ts-expect-error
|
|
318
325
|
this.wrappedListener = event => {
|
|
319
|
-
const data = getData(event);
|
|
326
|
+
const data = getData$1(event);
|
|
320
327
|
// @ts-expect-error
|
|
321
328
|
listener({
|
|
322
329
|
data,
|
|
@@ -332,9 +339,206 @@ const wrap = port => {
|
|
|
332
339
|
};
|
|
333
340
|
|
|
334
341
|
const IpcChildWithModuleWorkerAndMessagePort = {
|
|
342
|
+
__proto__: null,
|
|
343
|
+
listen: listen$1,
|
|
344
|
+
wrap: wrap$2
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
const listen = () => {
|
|
348
|
+
// @ts-ignore
|
|
349
|
+
return window;
|
|
350
|
+
};
|
|
351
|
+
const signal = global => {
|
|
352
|
+
global.postMessage(readyMessage);
|
|
353
|
+
};
|
|
354
|
+
const wrap$1 = window => {
|
|
355
|
+
return {
|
|
356
|
+
window,
|
|
357
|
+
/**
|
|
358
|
+
* @type {any}
|
|
359
|
+
*/
|
|
360
|
+
listener: undefined,
|
|
361
|
+
get onmessage() {
|
|
362
|
+
return this.listener;
|
|
363
|
+
},
|
|
364
|
+
set onmessage(listener) {
|
|
365
|
+
this.listener = listener;
|
|
366
|
+
const wrappedListener = event => {
|
|
367
|
+
const data = event.data;
|
|
368
|
+
if ('method' in data) {
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
// @ts-ignore
|
|
372
|
+
listener({
|
|
373
|
+
data,
|
|
374
|
+
target: this
|
|
375
|
+
});
|
|
376
|
+
};
|
|
377
|
+
this.window.onmessage = wrappedListener;
|
|
378
|
+
},
|
|
379
|
+
send(message) {
|
|
380
|
+
this.window.postMessage(message);
|
|
381
|
+
},
|
|
382
|
+
sendAndTransfer(message, transfer) {
|
|
383
|
+
this.window.postMessage(message, '*', transfer);
|
|
384
|
+
},
|
|
385
|
+
dispose() {
|
|
386
|
+
this.window.onmessage = null;
|
|
387
|
+
this.window = undefined;
|
|
388
|
+
this.listener = undefined;
|
|
389
|
+
}
|
|
390
|
+
};
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
const IpcChildWithWindow = {
|
|
335
394
|
__proto__: null,
|
|
336
395
|
listen,
|
|
396
|
+
signal,
|
|
397
|
+
wrap: wrap$1
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
const Message = 'message';
|
|
401
|
+
const Error$1 = 'error';
|
|
402
|
+
|
|
403
|
+
const getFirstEvent = (eventEmitter, eventMap) => {
|
|
404
|
+
const {
|
|
405
|
+
resolve,
|
|
406
|
+
promise
|
|
407
|
+
} = withResolvers();
|
|
408
|
+
const listenerMap = Object.create(null);
|
|
409
|
+
// @ts-ignore
|
|
410
|
+
const cleanup = value => {
|
|
411
|
+
for (const event of Object.keys(eventMap)) {
|
|
412
|
+
eventEmitter.off(event, listenerMap[event]);
|
|
413
|
+
}
|
|
414
|
+
// @ts-ignore
|
|
415
|
+
resolve(value);
|
|
416
|
+
};
|
|
417
|
+
for (const [event, type] of Object.entries(eventMap)) {
|
|
418
|
+
// @ts-ignore
|
|
419
|
+
const listener = event => {
|
|
420
|
+
cleanup({
|
|
421
|
+
type,
|
|
422
|
+
event
|
|
423
|
+
});
|
|
424
|
+
};
|
|
425
|
+
eventEmitter.on(event, listener);
|
|
426
|
+
listenerMap[event] = listener;
|
|
427
|
+
}
|
|
428
|
+
return promise;
|
|
429
|
+
};
|
|
430
|
+
|
|
431
|
+
const getFirstWorkerEvent = worker => {
|
|
432
|
+
return getFirstEvent(worker, {
|
|
433
|
+
message: Message,
|
|
434
|
+
error: Error$1
|
|
435
|
+
});
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
const isErrorEvent = event => {
|
|
439
|
+
return event instanceof ErrorEvent;
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
const getWorkerDisplayName = name => {
|
|
443
|
+
if (!name) {
|
|
444
|
+
return '<unknown> worker';
|
|
445
|
+
}
|
|
446
|
+
if (name.endsWith('Worker') || name.endsWith('worker')) {
|
|
447
|
+
return name.toLowerCase();
|
|
448
|
+
}
|
|
449
|
+
return `${name} Worker`;
|
|
450
|
+
};
|
|
451
|
+
|
|
452
|
+
const tryToGetActualErrorMessage = async ({
|
|
453
|
+
name
|
|
454
|
+
}) => {
|
|
455
|
+
const displayName = getWorkerDisplayName(name);
|
|
456
|
+
return `Failed to start ${displayName}: Worker Launch Error`;
|
|
457
|
+
};
|
|
458
|
+
|
|
459
|
+
class WorkerError extends Error {
|
|
460
|
+
constructor(event) {
|
|
461
|
+
super(event.message);
|
|
462
|
+
const stackLines = splitLines(this.stack || '');
|
|
463
|
+
const relevantLines = stackLines.slice(1);
|
|
464
|
+
const relevant = joinLines(relevantLines);
|
|
465
|
+
this.stack = `${event.message}
|
|
466
|
+
at Module (${event.filename}:${event.lineno}:${event.colno})
|
|
467
|
+
${relevant}`;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
const Module = 'module';
|
|
472
|
+
|
|
473
|
+
const create = async ({
|
|
474
|
+
url,
|
|
475
|
+
name
|
|
476
|
+
}) => {
|
|
477
|
+
const worker = new Worker(url, {
|
|
478
|
+
type: Module,
|
|
479
|
+
name
|
|
480
|
+
});
|
|
481
|
+
const {
|
|
482
|
+
type,
|
|
483
|
+
event
|
|
484
|
+
} = await getFirstWorkerEvent(worker);
|
|
485
|
+
switch (type) {
|
|
486
|
+
case Message:
|
|
487
|
+
if (event.data !== readyMessage) {
|
|
488
|
+
throw new IpcError('unexpected first message from worker');
|
|
489
|
+
}
|
|
490
|
+
break;
|
|
491
|
+
case Error$1:
|
|
492
|
+
if (isErrorEvent(event)) {
|
|
493
|
+
throw new WorkerError(event);
|
|
494
|
+
}
|
|
495
|
+
const actualErrorMessage = await tryToGetActualErrorMessage({
|
|
496
|
+
name
|
|
497
|
+
});
|
|
498
|
+
throw new Error(actualErrorMessage);
|
|
499
|
+
}
|
|
500
|
+
return worker;
|
|
501
|
+
};
|
|
502
|
+
const getData = event => {
|
|
503
|
+
// TODO why are some events not instance of message event?
|
|
504
|
+
if (event instanceof MessageEvent) {
|
|
505
|
+
return event.data;
|
|
506
|
+
}
|
|
507
|
+
return event;
|
|
508
|
+
};
|
|
509
|
+
const wrap = worker => {
|
|
510
|
+
let handleMessage;
|
|
511
|
+
return {
|
|
512
|
+
get onmessage() {
|
|
513
|
+
return handleMessage;
|
|
514
|
+
},
|
|
515
|
+
set onmessage(listener) {
|
|
516
|
+
if (listener) {
|
|
517
|
+
handleMessage = event => {
|
|
518
|
+
const data = getData(event);
|
|
519
|
+
listener({
|
|
520
|
+
data,
|
|
521
|
+
target: this
|
|
522
|
+
});
|
|
523
|
+
};
|
|
524
|
+
} else {
|
|
525
|
+
handleMessage = null;
|
|
526
|
+
}
|
|
527
|
+
worker.onmessage = handleMessage;
|
|
528
|
+
},
|
|
529
|
+
send(message) {
|
|
530
|
+
worker.postMessage(message);
|
|
531
|
+
},
|
|
532
|
+
sendAndTransfer(message, transfer) {
|
|
533
|
+
worker.postMessage(message, transfer);
|
|
534
|
+
}
|
|
535
|
+
};
|
|
536
|
+
};
|
|
537
|
+
|
|
538
|
+
const IpcParentWithModuleWorker = {
|
|
539
|
+
__proto__: null,
|
|
540
|
+
create,
|
|
337
541
|
wrap
|
|
338
542
|
};
|
|
339
543
|
|
|
340
|
-
export { IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort };
|
|
544
|
+
export { IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort, IpcChildWithWindow, IpcParentWithModuleWorker };
|
package/dist/index.js
CHANGED
|
@@ -232,6 +232,8 @@ const getUtilityProcessPortData = event => {
|
|
|
232
232
|
};
|
|
233
233
|
};
|
|
234
234
|
|
|
235
|
+
const readyMessage = 'ready';
|
|
236
|
+
|
|
235
237
|
const listen$6 = () => {
|
|
236
238
|
// @ts-ignore
|
|
237
239
|
const {
|
|
@@ -245,7 +247,7 @@ const listen$6 = () => {
|
|
|
245
247
|
|
|
246
248
|
// @ts-ignore
|
|
247
249
|
const signal$3 = parentPort => {
|
|
248
|
-
parentPort.postMessage(
|
|
250
|
+
parentPort.postMessage(readyMessage);
|
|
249
251
|
};
|
|
250
252
|
|
|
251
253
|
// @ts-ignore
|
|
@@ -301,12 +303,10 @@ const listen$5 = () => {
|
|
|
301
303
|
if (typeof WorkerGlobalScope === 'undefined') {
|
|
302
304
|
throw new TypeError('module is not in web worker scope');
|
|
303
305
|
}
|
|
304
|
-
// @ts-ignore
|
|
305
|
-
globalThis.postMessage('ready');
|
|
306
306
|
return globalThis;
|
|
307
307
|
};
|
|
308
308
|
const signal$2 = global => {
|
|
309
|
-
global.postMessage(
|
|
309
|
+
global.postMessage(readyMessage);
|
|
310
310
|
};
|
|
311
311
|
const wrap$8 = global => {
|
|
312
312
|
return {
|
|
@@ -335,6 +335,11 @@ const wrap$8 = global => {
|
|
|
335
335
|
};
|
|
336
336
|
this.listener = listener;
|
|
337
337
|
this.global.onmessage = wrappedListener;
|
|
338
|
+
},
|
|
339
|
+
dispose() {
|
|
340
|
+
// @ts-ignore
|
|
341
|
+
this.listener = null;
|
|
342
|
+
this.global.onmessage = null;
|
|
338
343
|
}
|
|
339
344
|
};
|
|
340
345
|
};
|
|
@@ -377,6 +382,7 @@ const waitForFirstMessage = async port => {
|
|
|
377
382
|
|
|
378
383
|
const listen$4 = async () => {
|
|
379
384
|
const parentIpcRaw = listen$5();
|
|
385
|
+
signal$2(parentIpcRaw);
|
|
380
386
|
const parentIpc = wrap$8(parentIpcRaw);
|
|
381
387
|
const firstMessage = await waitForFirstMessage(parentIpc);
|
|
382
388
|
if (firstMessage.method !== 'initialize') {
|
|
@@ -384,6 +390,7 @@ const listen$4 = async () => {
|
|
|
384
390
|
}
|
|
385
391
|
const type = firstMessage.params[0];
|
|
386
392
|
if (type === 'message-port') {
|
|
393
|
+
parentIpc.dispose();
|
|
387
394
|
const port = firstMessage.params[1];
|
|
388
395
|
return port;
|
|
389
396
|
}
|
|
@@ -439,7 +446,7 @@ const listen$3 = async () => {
|
|
|
439
446
|
|
|
440
447
|
// @ts-ignore
|
|
441
448
|
const signal$1 = process => {
|
|
442
|
-
process.send(
|
|
449
|
+
process.send(readyMessage);
|
|
443
450
|
};
|
|
444
451
|
|
|
445
452
|
// @ts-ignore
|
|
@@ -540,7 +547,7 @@ const listen$1 = async () => {
|
|
|
540
547
|
return parentPort;
|
|
541
548
|
};
|
|
542
549
|
const signal = parentPort => {
|
|
543
|
-
parentPort.postMessage(
|
|
550
|
+
parentPort.postMessage(readyMessage);
|
|
544
551
|
};
|
|
545
552
|
const wrap$4 = parentPort => {
|
|
546
553
|
return {
|
|
@@ -1079,7 +1086,7 @@ const create = async ({
|
|
|
1079
1086
|
if (type === Error$1) {
|
|
1080
1087
|
throw new IpcError(`Worker threw an error before ipc connection was established: ${event}`);
|
|
1081
1088
|
}
|
|
1082
|
-
if (event !==
|
|
1089
|
+
if (event !== readyMessage) {
|
|
1083
1090
|
throw new IpcError('unexpected first message from worker');
|
|
1084
1091
|
}
|
|
1085
1092
|
return worker;
|