@lvce-editor/ipc 9.3.0 → 9.5.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/dist/browser.js CHANGED
@@ -26,14 +26,14 @@ class Ipc extends EventTarget {
26
26
 
27
27
  const readyMessage = 'ready';
28
28
 
29
- const listen$3 = () => {
29
+ const listen$4 = () => {
30
30
  // @ts-ignore
31
31
  if (typeof WorkerGlobalScope === 'undefined') {
32
32
  throw new TypeError('module is not in web worker scope');
33
33
  }
34
34
  return globalThis;
35
35
  };
36
- const signal$2 = global => {
36
+ const signal$3 = global => {
37
37
  global.postMessage(readyMessage);
38
38
  };
39
39
  class IpcChildWithModuleWorker extends Ipc {
@@ -58,15 +58,15 @@ class IpcChildWithModuleWorker extends Ipc {
58
58
  this._rawIpc.addEventListener('message', callback);
59
59
  }
60
60
  }
61
- const wrap$5 = global => {
61
+ const wrap$6 = global => {
62
62
  return new IpcChildWithModuleWorker(global);
63
63
  };
64
64
 
65
65
  const IpcChildWithModuleWorker$1 = {
66
66
  __proto__: null,
67
- listen: listen$3,
68
- signal: signal$2,
69
- wrap: wrap$5
67
+ listen: listen$4,
68
+ signal: signal$3,
69
+ wrap: wrap$6
70
70
  };
71
71
 
72
72
  const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
@@ -294,10 +294,10 @@ const waitForFirstMessage = async port => {
294
294
  return event.data;
295
295
  };
296
296
 
297
- const listen$2 = async () => {
298
- const parentIpcRaw = listen$3();
299
- signal$2(parentIpcRaw);
300
- const parentIpc = wrap$5(parentIpcRaw);
297
+ const listen$3 = async () => {
298
+ const parentIpcRaw = listen$4();
299
+ signal$3(parentIpcRaw);
300
+ const parentIpc = wrap$6(parentIpcRaw);
301
301
  const firstMessage = await waitForFirstMessage(parentIpc);
302
302
  if (firstMessage.method !== 'initialize') {
303
303
  throw new IpcError('unexpected first message');
@@ -336,16 +336,106 @@ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
336
336
  this._rawIpc.start();
337
337
  }
338
338
  }
339
- const wrap$4 = port => {
339
+ const wrap$5 = port => {
340
340
  return new IpcChildWithModuleWorkerAndMessagePort(port);
341
341
  };
342
342
 
343
343
  const IpcChildWithModuleWorkerAndMessagePort$1 = {
344
+ __proto__: null,
345
+ listen: listen$3,
346
+ wrap: wrap$5
347
+ };
348
+
349
+ const listen$2 = () => {
350
+ return window;
351
+ };
352
+ const signal$2 = global => {
353
+ global.postMessage(readyMessage);
354
+ };
355
+ let IpcChildWithWindow$1 = class IpcChildWithWindow extends Ipc {
356
+ getData(event) {
357
+ return getData$1(event);
358
+ }
359
+ send(message) {
360
+ this._rawIpc.postMessage(message);
361
+ }
362
+ sendAndTransfer(message, transfer) {
363
+ this._rawIpc.postMessage(message, location.origin, transfer);
364
+ }
365
+ dispose() {
366
+ // ignore
367
+ }
368
+ onClose(callback) {
369
+ // ignore
370
+ }
371
+ onMessage(callback) {
372
+ const wrapped = event => {
373
+ const {
374
+ ports
375
+ } = event;
376
+ if (ports.length) {
377
+ return;
378
+ }
379
+ callback(event);
380
+ this._rawIpc.removeEventListener('message', wrapped);
381
+ };
382
+ this._rawIpc.addEventListener('message', wrapped);
383
+ }
384
+ };
385
+ const wrap$4 = window => {
386
+ return new IpcChildWithWindow$1(window);
387
+ };
388
+
389
+ const IpcChildWithWindow$2 = {
344
390
  __proto__: null,
345
391
  listen: listen$2,
392
+ signal: signal$2,
346
393
  wrap: wrap$4
347
394
  };
348
395
 
396
+ const isTransferrable = value => {
397
+ return value instanceof MessagePort;
398
+ };
399
+
400
+ const walkValue = (value, transferrables) => {
401
+ if (!value) {
402
+ return value;
403
+ }
404
+ if (isTransferrable(value)) {
405
+ transferrables.push(value);
406
+ return undefined;
407
+ }
408
+ if (Array.isArray(value)) {
409
+ const newItems = [];
410
+ for (const item of value) {
411
+ const newItem = walkValue(item, transferrables);
412
+ newItems.push(newItem);
413
+ }
414
+ return newItems;
415
+ }
416
+ if (typeof value === 'object') {
417
+ const newObject = Object.create(null);
418
+ for (const [key, property] of Object.entries(value)) {
419
+ const newValue = walkValue(property, transferrables);
420
+ newObject[key] = newValue;
421
+ }
422
+ return newObject;
423
+ }
424
+ return value;
425
+ };
426
+
427
+ // workaround for electron not supporting transferrable objects
428
+ // as parameters. If the transferrable object is a parameter, in electron
429
+ // only an empty objected is received in the main process
430
+ const fixElectronParameters = value => {
431
+ const transfer = [];
432
+ const newValue = walkValue(value, transfer);
433
+ return {
434
+ newValue,
435
+ transfer
436
+ };
437
+ };
438
+
349
439
  const listen$1 = () => {
350
440
  return window;
351
441
  };
@@ -359,8 +449,12 @@ class IpcChildWithWindow extends Ipc {
359
449
  send(message) {
360
450
  this._rawIpc.postMessage(message);
361
451
  }
362
- sendAndTransfer(message, transfer) {
363
- this._rawIpc.postMessage(message, location.origin, transfer);
452
+ sendAndTransfer(message, _transfer) {
453
+ const {
454
+ newValue,
455
+ transfer
456
+ } = fixElectronParameters(message);
457
+ this._rawIpc.postMessage(newValue, location.origin, transfer);
364
458
  }
365
459
  dispose() {
366
460
  // ignore
@@ -386,7 +480,7 @@ const wrap$3 = window => {
386
480
  return new IpcChildWithWindow(window);
387
481
  };
388
482
 
389
- const IpcChildWithWindow$1 = {
483
+ const IpcChildWithElectronWindow = {
390
484
  __proto__: null,
391
485
  listen: listen$1,
392
486
  signal: signal$1,
@@ -650,4 +744,4 @@ const IpcParentWithWebSocket$1 = {
650
744
  wrap
651
745
  };
652
746
 
653
- export { IpcChildWithMessagePort$1 as IpcChildWithMessagePort, IpcChildWithModuleWorker$1 as IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort$1 as IpcChildWithModuleWorkerAndMessagePort, IpcChildWithWindow$1 as IpcChildWithWindow, IpcParentWithModuleWorker$1 as IpcParentWithModuleWorker, IpcParentWithWebSocket$1 as IpcParentWithWebSocket };
747
+ export { IpcChildWithElectronWindow, IpcChildWithMessagePort$1 as IpcChildWithMessagePort, IpcChildWithModuleWorker$1 as IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort$1 as IpcChildWithModuleWorkerAndMessagePort, IpcChildWithWindow$2 as IpcChildWithWindow, IpcParentWithModuleWorker$1 as IpcParentWithModuleWorker, IpcParentWithWebSocket$1 as IpcParentWithWebSocket };
@@ -0,0 +1,716 @@
1
+ class AssertionError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ this.name = 'AssertionError';
5
+ }
6
+ }
7
+ const getType = value => {
8
+ switch (typeof value) {
9
+ case 'number':
10
+ return 'number';
11
+ case 'function':
12
+ return 'function';
13
+ case 'string':
14
+ return 'string';
15
+ case 'object':
16
+ if (value === null) {
17
+ return 'null';
18
+ }
19
+ if (Array.isArray(value)) {
20
+ return 'array';
21
+ }
22
+ return 'object';
23
+ case 'boolean':
24
+ return 'boolean';
25
+ default:
26
+ return 'unknown';
27
+ }
28
+ };
29
+ const array = value => {
30
+ const type = getType(value);
31
+ if (type !== 'array') {
32
+ throw new AssertionError('expected value to be of type array');
33
+ }
34
+ };
35
+ const string = value => {
36
+ const type = getType(value);
37
+ if (type !== 'string') {
38
+ throw new AssertionError('expected value to be of type string');
39
+ }
40
+ };
41
+
42
+ const Exit = 1;
43
+ const Error$1 = 2;
44
+ const Message = 3;
45
+
46
+ const withResolvers = () => {
47
+ let _resolve;
48
+ const promise = new Promise(resolve => {
49
+ _resolve = resolve;
50
+ });
51
+ return {
52
+ resolve: _resolve,
53
+ promise
54
+ };
55
+ };
56
+
57
+ /**
58
+ *
59
+ * @param {any} utilityProcess
60
+ * @returns
61
+ */
62
+ // @ts-ignore
63
+ const getFirstUtilityProcessEvent = async utilityProcess => {
64
+ const {
65
+ resolve,
66
+ promise
67
+ } = withResolvers();
68
+ let stdout = '';
69
+ let stderr = '';
70
+ // @ts-ignore
71
+ const cleanup = value => {
72
+ // @ts-ignore
73
+ utilityProcess.stderr.off('data', handleStdErrData);
74
+ // @ts-ignore
75
+ utilityProcess.stdout.off('data', handleStdoutData);
76
+ utilityProcess.off('message', handleMessage);
77
+ utilityProcess.off('exit', handleExit);
78
+ // @ts-ignore
79
+ resolve(value);
80
+ };
81
+ // @ts-ignore
82
+ const handleStdErrData = data => {
83
+ stderr += data;
84
+ };
85
+ // @ts-ignore
86
+ const handleStdoutData = data => {
87
+ stdout += data;
88
+ };
89
+ // @ts-ignore
90
+ const handleMessage = event => {
91
+ cleanup({
92
+ type: Message,
93
+ event,
94
+ stdout,
95
+ stderr
96
+ });
97
+ };
98
+ // @ts-ignore
99
+ const handleExit = event => {
100
+ cleanup({
101
+ type: Exit,
102
+ event,
103
+ stdout,
104
+ stderr
105
+ });
106
+ };
107
+ // @ts-ignore
108
+ utilityProcess.stderr.on('data', handleStdErrData);
109
+ // @ts-ignore
110
+ utilityProcess.stdout.on('data', handleStdoutData);
111
+ utilityProcess.on('message', handleMessage);
112
+ utilityProcess.on('exit', handleExit);
113
+ // @ts-ignore
114
+ const {
115
+ type,
116
+ event
117
+ } = await promise;
118
+ return {
119
+ type,
120
+ event,
121
+ stdout,
122
+ stderr
123
+ };
124
+ };
125
+
126
+ const attachEvents = that => {
127
+ const handleMessage = (...args) => {
128
+ const data = that.getData(...args);
129
+ that.dispatchEvent(new MessageEvent('message', {
130
+ data
131
+ }));
132
+ };
133
+ that.onMessage(handleMessage);
134
+ const handleClose = event => {
135
+ that.dispatchEvent(new Event('close'));
136
+ };
137
+ that.onClose(handleClose);
138
+ };
139
+
140
+ class Ipc extends EventTarget {
141
+ constructor(rawIpc) {
142
+ super();
143
+ this._rawIpc = rawIpc;
144
+ attachEvents(this);
145
+ }
146
+ }
147
+
148
+ const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
149
+ const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
150
+ const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
151
+
152
+ const NewLine$1 = '\n';
153
+
154
+ const joinLines = lines => {
155
+ return lines.join(NewLine$1);
156
+ };
157
+
158
+ const splitLines = lines => {
159
+ return lines.split(NewLine$1);
160
+ };
161
+
162
+ const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
163
+ const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
164
+ const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
165
+ const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
166
+ const RE_AT = /^\s+at/;
167
+ const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
168
+ const isUnhelpfulNativeModuleError = stderr => {
169
+ return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
170
+ };
171
+ const isMessageCodeBlockStartIndex = line => {
172
+ return RE_MESSAGE_CODE_BLOCK_START.test(line);
173
+ };
174
+ const isMessageCodeBlockEndIndex = line => {
175
+ return RE_MESSAGE_CODE_BLOCK_END.test(line);
176
+ };
177
+ const getMessageCodeBlock = stderr => {
178
+ const lines = splitLines(stderr);
179
+ const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
180
+ const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
181
+ const relevantLines = lines.slice(startIndex, endIndex);
182
+ const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
183
+ return relevantMessage;
184
+ };
185
+ const getNativeModuleErrorMessage = stderr => {
186
+ const message = getMessageCodeBlock(stderr);
187
+ return {
188
+ message: `Incompatible native node module: ${message}`,
189
+ code: E_INCOMPATIBLE_NATIVE_MODULE
190
+ };
191
+ };
192
+ const isModulesSyntaxError = stderr => {
193
+ if (!stderr) {
194
+ return false;
195
+ }
196
+ return stderr.includes('SyntaxError: Cannot use import statement outside a module');
197
+ };
198
+ const getModuleSyntaxError = () => {
199
+ return {
200
+ message: `ES Modules are not supported in electron`,
201
+ code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
202
+ };
203
+ };
204
+ const isModuleNotFoundError = stderr => {
205
+ if (!stderr) {
206
+ return false;
207
+ }
208
+ return stderr.includes('ERR_MODULE_NOT_FOUND');
209
+ };
210
+ const isModuleNotFoundMessage = line => {
211
+ return line.includes('ERR_MODULE_NOT_FOUND');
212
+ };
213
+ const getModuleNotFoundError = stderr => {
214
+ const lines = splitLines(stderr);
215
+ const messageIndex = lines.findIndex(isModuleNotFoundMessage);
216
+ const message = lines[messageIndex];
217
+ return {
218
+ message,
219
+ code: ERR_MODULE_NOT_FOUND
220
+ };
221
+ };
222
+ const isNormalStackLine = line => {
223
+ return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
224
+ };
225
+ const getDetails = lines => {
226
+ const index = lines.findIndex(isNormalStackLine);
227
+ if (index === -1) {
228
+ return {
229
+ actualMessage: joinLines(lines),
230
+ rest: []
231
+ };
232
+ }
233
+ let lastIndex = index - 1;
234
+ while (++lastIndex < lines.length) {
235
+ if (!isNormalStackLine(lines[lastIndex])) {
236
+ break;
237
+ }
238
+ }
239
+ return {
240
+ actualMessage: lines[index - 1],
241
+ rest: lines.slice(index, lastIndex)
242
+ };
243
+ };
244
+ const getHelpfulChildProcessError = (stdout, stderr) => {
245
+ if (isUnhelpfulNativeModuleError(stderr)) {
246
+ return getNativeModuleErrorMessage(stderr);
247
+ }
248
+ if (isModulesSyntaxError(stderr)) {
249
+ return getModuleSyntaxError();
250
+ }
251
+ if (isModuleNotFoundError(stderr)) {
252
+ return getModuleNotFoundError(stderr);
253
+ }
254
+ const lines = splitLines(stderr);
255
+ const {
256
+ actualMessage,
257
+ rest
258
+ } = getDetails(lines);
259
+ return {
260
+ message: `${actualMessage}`,
261
+ code: '',
262
+ stack: rest
263
+ };
264
+ };
265
+
266
+ const normalizeLine = line => {
267
+ if (line.startsWith('Error: ')) {
268
+ return line.slice(`Error: `.length);
269
+ }
270
+ if (line.startsWith('VError: ')) {
271
+ return line.slice(`VError: `.length);
272
+ }
273
+ return line;
274
+ };
275
+ const getCombinedMessage = (error, message) => {
276
+ const stringifiedError = normalizeLine(`${error}`);
277
+ if (message) {
278
+ return `${message}: ${stringifiedError}`;
279
+ }
280
+ return stringifiedError;
281
+ };
282
+ const NewLine = '\n';
283
+ const getNewLineIndex = (string, startIndex = undefined) => {
284
+ return string.indexOf(NewLine, startIndex);
285
+ };
286
+ const mergeStacks = (parent, child) => {
287
+ if (!child) {
288
+ return parent;
289
+ }
290
+ const parentNewLineIndex = getNewLineIndex(parent);
291
+ const childNewLineIndex = getNewLineIndex(child);
292
+ if (childNewLineIndex === -1) {
293
+ return parent;
294
+ }
295
+ const parentFirstLine = parent.slice(0, parentNewLineIndex);
296
+ const childRest = child.slice(childNewLineIndex);
297
+ const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
298
+ if (parentFirstLine.includes(childFirstLine)) {
299
+ return parentFirstLine + childRest;
300
+ }
301
+ return child;
302
+ };
303
+ class VError extends Error {
304
+ constructor(error, message) {
305
+ const combinedMessage = getCombinedMessage(error, message);
306
+ super(combinedMessage);
307
+ this.name = 'VError';
308
+ if (error instanceof Error) {
309
+ this.stack = mergeStacks(this.stack, error.stack);
310
+ }
311
+ if (error.codeFrame) {
312
+ // @ts-ignore
313
+ this.codeFrame = error.codeFrame;
314
+ }
315
+ if (error.code) {
316
+ // @ts-ignore
317
+ this.code = error.code;
318
+ }
319
+ }
320
+ }
321
+
322
+ class IpcError extends VError {
323
+ // @ts-ignore
324
+ constructor(betterMessage, stdout = '', stderr = '') {
325
+ if (stdout || stderr) {
326
+ // @ts-ignore
327
+ const {
328
+ message,
329
+ code,
330
+ stack
331
+ } = getHelpfulChildProcessError(stdout, stderr);
332
+ const cause = new Error(message);
333
+ // @ts-ignore
334
+ cause.code = code;
335
+ cause.stack = stack;
336
+ super(cause, betterMessage);
337
+ } else {
338
+ super(betterMessage);
339
+ }
340
+ // @ts-ignore
341
+ this.name = 'IpcError';
342
+ // @ts-ignore
343
+ this.stdout = stdout;
344
+ // @ts-ignore
345
+ this.stderr = stderr;
346
+ }
347
+ }
348
+
349
+ // @ts-ignore
350
+ const create$2 = async ({
351
+ path,
352
+ argv = [],
353
+ execArgv = [],
354
+ name,
355
+ env = process.env
356
+ }) => {
357
+ string(path);
358
+ const actualArgv = ['--ipc-type=electron-utility-process', ...argv];
359
+ const {
360
+ utilityProcess
361
+ } = await import('electron');
362
+ const childProcess = utilityProcess.fork(path, actualArgv, {
363
+ execArgv,
364
+ stdio: 'pipe',
365
+ serviceName: name,
366
+ env
367
+ });
368
+ const handleExit = () => {
369
+ // @ts-ignore
370
+ childProcess.stdout.unpipe(process.stdout);
371
+ // @ts-ignore
372
+ childProcess.stderr.unpipe(process.stderr);
373
+ };
374
+ childProcess.once('exit', handleExit);
375
+ // @ts-ignore
376
+ childProcess.stdout.pipe(process.stdout);
377
+ const {
378
+ type,
379
+ stdout,
380
+ stderr
381
+ } = await getFirstUtilityProcessEvent(childProcess);
382
+ if (type === Exit) {
383
+ throw new IpcError(`Utility process exited before ipc connection was established`, stdout, stderr);
384
+ }
385
+ // @ts-ignore
386
+ childProcess.stderr.pipe(process.stderr);
387
+ return childProcess;
388
+ };
389
+ class IpcParentWithElectronUtilityProcess extends Ipc {
390
+ getData(data) {
391
+ return data;
392
+ }
393
+ send(message) {
394
+ this._rawIpc.postMessage(message);
395
+ }
396
+ sendAndTransfer(message, transfer) {
397
+ this._rawIpc.postMessage(message, transfer);
398
+ }
399
+ dispose() {
400
+ this._rawIpc.kill();
401
+ }
402
+ onClose(callback) {
403
+ this._rawIpc.on('exit', callback);
404
+ }
405
+ onMessage(callback) {
406
+ this._rawIpc.on('message', callback);
407
+ }
408
+ }
409
+ const wrap$2 = process => {
410
+ return new IpcParentWithElectronUtilityProcess(process);
411
+ };
412
+
413
+ const IpcParentWithElectronUtilityProcess$1 = {
414
+ __proto__: null,
415
+ create: create$2,
416
+ wrap: wrap$2
417
+ };
418
+
419
+ class ChildProcessError extends Error {
420
+ // @ts-ignore
421
+ constructor(stderr) {
422
+ // @ts-ignore
423
+ const {
424
+ message,
425
+ code,
426
+ stack
427
+ } = getHelpfulChildProcessError('', stderr);
428
+ super(message || 'child process error');
429
+ this.name = 'ChildProcessError';
430
+ if (code) {
431
+ // @ts-ignore
432
+ this.code = code;
433
+ }
434
+ if (stack) {
435
+ // @ts-ignore
436
+ const lines = splitLines(this.stack);
437
+ const [firstLine, ...stackLines] = lines;
438
+ const newStackLines = [firstLine, ...stack, ...stackLines];
439
+ const newStack = joinLines(newStackLines);
440
+ this.stack = newStack;
441
+ }
442
+ }
443
+ }
444
+
445
+ // @ts-ignore
446
+ const getFirstNodeChildProcessEvent = async childProcess => {
447
+ // @ts-ignore
448
+ const {
449
+ type,
450
+ event,
451
+ stdout,
452
+ stderr
453
+ } = await new Promise((resolve, reject) => {
454
+ let stderr = '';
455
+ let stdout = '';
456
+ // @ts-ignore
457
+ const cleanup = value => {
458
+ if (childProcess.stdout && childProcess.stderr) {
459
+ childProcess.stderr.off('data', handleStdErrData);
460
+ childProcess.stdout.off('data', handleStdoutData);
461
+ }
462
+ childProcess.off('message', handleMessage);
463
+ childProcess.off('exit', handleExit);
464
+ childProcess.off('error', handleError);
465
+ resolve(value);
466
+ };
467
+ // @ts-ignore
468
+ const handleStdErrData = data => {
469
+ stderr += data;
470
+ };
471
+ // @ts-ignore
472
+ const handleStdoutData = data => {
473
+ stdout += data;
474
+ };
475
+ // @ts-ignore
476
+ const handleMessage = event => {
477
+ cleanup({
478
+ type: Message,
479
+ event,
480
+ stdout,
481
+ stderr
482
+ });
483
+ };
484
+ // @ts-ignore
485
+ const handleExit = event => {
486
+ cleanup({
487
+ type: Exit,
488
+ event,
489
+ stdout,
490
+ stderr
491
+ });
492
+ };
493
+ // @ts-ignore
494
+ const handleError = event => {
495
+ cleanup({
496
+ type: Error$1,
497
+ event,
498
+ stdout,
499
+ stderr
500
+ });
501
+ };
502
+ if (childProcess.stdout && childProcess.stderr) {
503
+ childProcess.stderr.on('data', handleStdErrData);
504
+ childProcess.stdout.on('data', handleStdoutData);
505
+ }
506
+ childProcess.on('message', handleMessage);
507
+ childProcess.on('exit', handleExit);
508
+ childProcess.on('error', handleError);
509
+ });
510
+ return {
511
+ type,
512
+ event,
513
+ stdout,
514
+ stderr
515
+ };
516
+ };
517
+
518
+ // @ts-ignore
519
+ const create$1 = async ({
520
+ path,
521
+ argv = [],
522
+ env,
523
+ execArgv = [],
524
+ stdio = 'inherit',
525
+ name = 'child process'
526
+ }) => {
527
+ try {
528
+ string(path);
529
+ const actualArgv = ['--ipc-type=node-forked-process', ...argv];
530
+ const {
531
+ fork
532
+ } = await import('node:child_process');
533
+ const childProcess = fork(path, actualArgv, {
534
+ env,
535
+ execArgv,
536
+ stdio: 'pipe'
537
+ });
538
+ const {
539
+ type,
540
+ event,
541
+ stderr
542
+ } = await getFirstNodeChildProcessEvent(childProcess);
543
+ if (type === Exit) {
544
+ throw new ChildProcessError(stderr);
545
+ }
546
+ if (type === Error$1) {
547
+ throw new Error(`child process had an error ${event}`);
548
+ }
549
+ if (stdio === 'inherit' && childProcess.stdout && childProcess.stderr) {
550
+ childProcess.stdout.pipe(process.stdout);
551
+ childProcess.stderr.pipe(process.stderr);
552
+ }
553
+ return childProcess;
554
+ } catch (error) {
555
+ throw new VError(error, `Failed to launch ${name}`);
556
+ }
557
+ };
558
+ class IpcParentWithNodeForkedProcess extends Ipc {
559
+ constructor(childProcess) {
560
+ super(childProcess);
561
+ this.pid = childProcess.pid;
562
+ }
563
+ getData(message) {
564
+ return message;
565
+ }
566
+ send(message) {
567
+ this._rawIpc.send(message);
568
+ }
569
+ sendAndTransfer(message, transfer) {
570
+ this._rawIpc.send(message, transfer);
571
+ }
572
+ dispose() {
573
+ this._rawIpc.kill();
574
+ }
575
+ onClose(callback) {
576
+ this._rawIpc.on('close', callback);
577
+ }
578
+ onMessage(callback) {
579
+ this._rawIpc.on('message', callback);
580
+ }
581
+ }
582
+ const wrap$1 = childProcess => {
583
+ return new IpcParentWithNodeForkedProcess(childProcess);
584
+ };
585
+
586
+ const IpcParentWithNodeForkedProcess$1 = {
587
+ __proto__: null,
588
+ create: create$1,
589
+ wrap: wrap$1
590
+ };
591
+
592
+ const addListener = (emitter, type, callback) => {
593
+ if ('addEventListener' in emitter) {
594
+ emitter.addEventListener(type, callback);
595
+ } else {
596
+ emitter.on(type, callback);
597
+ }
598
+ };
599
+ const removeListener = (emitter, type, callback) => {
600
+ if ('removeEventListener' in emitter) {
601
+ emitter.removeEventListener(type, callback);
602
+ } else {
603
+ emitter.off(type, callback);
604
+ }
605
+ };
606
+ const getFirstEvent = (eventEmitter, eventMap) => {
607
+ const {
608
+ resolve,
609
+ promise
610
+ } = withResolvers();
611
+ const listenerMap = Object.create(null);
612
+ const cleanup = value => {
613
+ for (const event of Object.keys(eventMap)) {
614
+ removeListener(eventEmitter, event, listenerMap[event]);
615
+ }
616
+ resolve(value);
617
+ };
618
+ for (const [event, type] of Object.entries(eventMap)) {
619
+ const listener = event => {
620
+ cleanup({
621
+ type,
622
+ event
623
+ });
624
+ };
625
+ addListener(eventEmitter, event, listener);
626
+ listenerMap[event] = listener;
627
+ }
628
+ return promise;
629
+ };
630
+
631
+ const getFirstNodeWorkerEvent = worker => {
632
+ return getFirstEvent(worker, {
633
+ message: Message,
634
+ exit: Exit,
635
+ error: Error$1
636
+ });
637
+ };
638
+
639
+ const readyMessage = 'ready';
640
+
641
+ // @ts-ignore
642
+ const create = async ({
643
+ path,
644
+ argv = [],
645
+ env = process.env,
646
+ execArgv = []
647
+ }) => {
648
+ // @ts-ignore
649
+ string(path);
650
+ const actualArgv = ['--ipc-type=node-worker', ...argv];
651
+ const actualEnv = {
652
+ ...env,
653
+ ELECTRON_RUN_AS_NODE: '1'
654
+ };
655
+ const {
656
+ Worker
657
+ } = await import('node:worker_threads');
658
+ const worker = new Worker(path, {
659
+ argv: actualArgv,
660
+ env: actualEnv,
661
+ execArgv
662
+ });
663
+ // @ts-ignore
664
+ const {
665
+ type,
666
+ event
667
+ } = await getFirstNodeWorkerEvent(worker);
668
+ if (type === Exit) {
669
+ throw new IpcError(`Worker exited before ipc connection was established`);
670
+ }
671
+ if (type === Error$1) {
672
+ throw new IpcError(`Worker threw an error before ipc connection was established: ${event}`);
673
+ }
674
+ if (event !== readyMessage) {
675
+ throw new IpcError('unexpected first message from worker');
676
+ }
677
+ return worker;
678
+ };
679
+
680
+ // @ts-ignore
681
+ const wrap = worker => {
682
+ return {
683
+ worker,
684
+ // @ts-ignore
685
+ on(event, listener) {
686
+ const wrappedListener = message => {
687
+ const syntheticEvent = {
688
+ data: message,
689
+ target: this
690
+ };
691
+ listener(syntheticEvent);
692
+ };
693
+ this.worker.on(event, wrappedListener);
694
+ },
695
+ // @ts-ignore
696
+ send(message) {
697
+ this.worker.postMessage(message);
698
+ },
699
+ // @ts-ignore
700
+ sendAndTransfer(message, transfer) {
701
+ array(transfer);
702
+ this.worker.postMessage(message, transfer);
703
+ },
704
+ dispose() {
705
+ this.worker.terminate();
706
+ }
707
+ };
708
+ };
709
+
710
+ const IpcParentWithNodeWorker = {
711
+ __proto__: null,
712
+ create,
713
+ wrap
714
+ };
715
+
716
+ export { IpcParentWithElectronUtilityProcess$1 as IpcParentWithElectronUtilityProcess, IpcParentWithNodeForkedProcess$1 as IpcParentWithNodeForkedProcess, IpcParentWithNodeWorker };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/ipc",
3
- "version": "9.3.0",
3
+ "version": "9.5.0",
4
4
  "description": "Inter Process Communication for Lvce Editor",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -15,7 +15,7 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@lvce-editor/assert": "^1.2.0",
18
- "@lvce-editor/verror": "^1.3.0",
18
+ "@lvce-editor/verror": "^1.4.0",
19
19
  "@lvce-editor/web-socket-server": "^1.2.0"
20
20
  },
21
21
  "engines": {