@lvce-editor/ipc 3.0.0 → 3.1.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.
@@ -0,0 +1,18 @@
1
+ interface IpcChild {
2
+ readonly listen: any
3
+ readonly wrap: any
4
+ }
5
+
6
+ export const IpcChildWithElectronMessagePort: IpcChild
7
+ export const IpcChildWithElectronUtilityProcess: IpcChild
8
+ export const IpcChildWithNodeForkedProcess: IpcChild
9
+ export const IpcChildWithWebSocket: IpcChild
10
+
11
+ interface IpcParent {
12
+ readonly create: any
13
+ readonly wrap: any
14
+ }
15
+
16
+ export const IpcParentWithElectronUtilityProcess: IpcParent
17
+ export const IpcParentWithNodeForkedProcess: IpcParent
18
+ export const IpcParentWithNodeWorker: IpcParent
package/dist/index.js CHANGED
@@ -1,7 +1,5 @@
1
1
  import { VError } from '@lvce-editor/verror';
2
2
  import { string, array } from '@lvce-editor/assert';
3
- import { fork } from 'node:child_process';
4
- import { Worker } from 'node:worker_threads';
5
3
 
6
4
  const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
7
5
  const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
@@ -9,14 +7,14 @@ const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
9
7
 
10
8
  const NewLine = '\n';
11
9
 
12
- const splitLines = lines => {
13
- return lines.split(NewLine);
14
- };
15
-
16
10
  const joinLines = lines => {
17
11
  return lines.join(NewLine);
18
12
  };
19
13
 
14
+ const splitLines = lines => {
15
+ return lines.split(NewLine);
16
+ };
17
+
20
18
  const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
21
19
  const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
22
20
  const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
@@ -53,7 +51,7 @@ const isModulesSyntaxError = stderr => {
53
51
  }
54
52
  return stderr.includes('SyntaxError: Cannot use import statement outside a module');
55
53
  };
56
- const getModuleSyntaxError = stderr => {
54
+ const getModuleSyntaxError = () => {
57
55
  return {
58
56
  message: `ES Modules are not supported in electron`,
59
57
  code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
@@ -122,6 +120,7 @@ const getHelpfulChildProcessError = (stdout, stderr) => {
122
120
  };
123
121
 
124
122
  class IpcError extends VError {
123
+ // @ts-ignore
125
124
  constructor(message, stdout = '', stderr = '') {
126
125
  if (stdout || stderr) {
127
126
  // @ts-ignore
@@ -151,6 +150,7 @@ const isMessagePortMain = value => {
151
150
  return value && value.constructor && value.constructor.name === 'MessagePortMain';
152
151
  };
153
152
 
153
+ // @ts-ignore
154
154
  const listen$3 = ({
155
155
  messagePort
156
156
  }) => {
@@ -159,6 +159,8 @@ const listen$3 = ({
159
159
  }
160
160
  return messagePort;
161
161
  };
162
+
163
+ // @ts-ignore
162
164
  const getActualData$1 = event => {
163
165
  const {
164
166
  data,
@@ -172,11 +174,15 @@ const getActualData$1 = event => {
172
174
  params: [...ports, ...data.params]
173
175
  };
174
176
  };
177
+
178
+ // @ts-ignore
175
179
  const wrap$6 = messagePort => {
176
180
  return {
177
181
  messagePort,
182
+ // @ts-ignore
178
183
  on(event, listener) {
179
184
  if (event === 'message') {
185
+ // @ts-ignore
180
186
  const wrappedListener = event => {
181
187
  const actualData = getActualData$1(event);
182
188
  listener(actualData);
@@ -188,9 +194,11 @@ const wrap$6 = messagePort => {
188
194
  throw new Error('unsupported event type');
189
195
  }
190
196
  },
197
+ // @ts-ignore
191
198
  off(event, listener) {
192
199
  this.messagePort.off(event, listener);
193
200
  },
201
+ // @ts-ignore
194
202
  send(message) {
195
203
  this.messagePort.postMessage(message);
196
204
  },
@@ -209,6 +217,7 @@ const IpcChildWithElectronMessagePort = {
209
217
  wrap: wrap$6
210
218
  };
211
219
 
220
+ // @ts-ignore
212
221
  const getUtilityProcessPortData = event => {
213
222
  const {
214
223
  data,
@@ -233,14 +242,20 @@ const listen$2 = () => {
233
242
  }
234
243
  return parentPort;
235
244
  };
245
+
246
+ // @ts-ignore
236
247
  const signal$1 = parentPort => {
237
248
  parentPort.postMessage('ready');
238
249
  };
250
+
251
+ // @ts-ignore
239
252
  const wrap$5 = parentPort => {
240
253
  return {
241
254
  parentPort,
255
+ // @ts-ignore
242
256
  on(event, listener) {
243
257
  if (event === 'message') {
258
+ // @ts-ignore
244
259
  const wrappedListener = event => {
245
260
  const actualData = getUtilityProcessPortData(event);
246
261
  listener(actualData);
@@ -252,12 +267,15 @@ const wrap$5 = parentPort => {
252
267
  throw new Error('unsupported event type');
253
268
  }
254
269
  },
270
+ // @ts-ignore
255
271
  off(event, listener) {
256
272
  this.parentPort.off(event, listener);
257
273
  },
274
+ // @ts-ignore
258
275
  send(message) {
259
276
  this.parentPort.postMessage(message);
260
277
  },
278
+ // @ts-ignore
261
279
  sendAndTransfer(message, transfer) {
262
280
  this.parentPort.postMessage(message, transfer);
263
281
  },
@@ -280,9 +298,13 @@ const listen$1 = async () => {
280
298
  }
281
299
  return process;
282
300
  };
301
+
302
+ // @ts-ignore
283
303
  const signal = process => {
284
304
  process.send('ready');
285
305
  };
306
+
307
+ // @ts-ignore
286
308
  const getActualData = (message, handle) => {
287
309
  if (handle) {
288
310
  return {
@@ -292,11 +314,15 @@ const getActualData = (message, handle) => {
292
314
  }
293
315
  return message;
294
316
  };
317
+
318
+ // @ts-ignore
295
319
  const wrap$4 = process => {
296
320
  return {
297
321
  process,
322
+ // @ts-ignore
298
323
  on(event, listener) {
299
324
  if (event === 'message') {
325
+ // @ts-ignore
300
326
  const wrappedListener = (event, handle) => {
301
327
  const actualData = getActualData(event, handle);
302
328
  listener(actualData);
@@ -308,9 +334,11 @@ const wrap$4 = process => {
308
334
  throw new Error('unsupported event type');
309
335
  }
310
336
  },
337
+ // @ts-ignore
311
338
  off(event, listener) {
312
339
  this.process.off(event, listener);
313
340
  },
341
+ // @ts-ignore
314
342
  send(message) {
315
343
  this.process.send(message);
316
344
  },
@@ -329,38 +357,33 @@ const Open = 1;
329
357
  const Close = 2;
330
358
 
331
359
  const withResolvers = () => {
332
- /**
333
- * @type {any}
334
- */
335
360
  let _resolve;
336
- /**
337
- * @type {any}
338
- */
339
- let _reject;
340
- const promise = new Promise((resolve, reject) => {
361
+ const promise = new Promise(resolve => {
341
362
  _resolve = resolve;
342
- _reject = reject;
343
363
  });
344
364
  return {
345
365
  resolve: _resolve,
346
- reject: _reject,
347
366
  promise
348
367
  };
349
368
  };
350
369
 
370
+ // @ts-ignore
351
371
  const getFirstEvent = (eventEmitter, eventMap) => {
352
372
  const {
353
373
  resolve,
354
374
  promise
355
375
  } = withResolvers();
356
376
  const listenerMap = Object.create(null);
377
+ // @ts-ignore
357
378
  const cleanup = value => {
358
379
  for (const event of Object.keys(eventMap)) {
359
380
  eventEmitter.off(event, listenerMap[event]);
360
381
  }
382
+ // @ts-ignore
361
383
  resolve(value);
362
384
  };
363
385
  for (const [event, type] of Object.entries(eventMap)) {
386
+ // @ts-ignore
364
387
  const listener = event => {
365
388
  cleanup({
366
389
  type,
@@ -373,7 +396,9 @@ const getFirstEvent = (eventEmitter, eventMap) => {
373
396
  return promise;
374
397
  };
375
398
 
399
+ // @ts-ignore
376
400
  const getFirstWebSocketEvent = async webSocket => {
401
+ // @ts-ignore
377
402
  const {
378
403
  WebSocket
379
404
  } = await import('ws');
@@ -403,26 +428,33 @@ const getFirstWebSocketEvent = async webSocket => {
403
428
  };
404
429
  };
405
430
 
431
+ // @ts-ignore
406
432
  const isWebSocketOpen = async webSocket => {
433
+ // @ts-ignore
407
434
  const {
408
435
  WebSocket
409
436
  } = await import('ws');
410
437
  return webSocket.readyState === WebSocket.OPEN;
411
438
  };
412
439
 
440
+ // @ts-ignore
413
441
  const serialize = message => {
414
442
  return JSON.stringify(message);
415
443
  };
444
+
445
+ // @ts-ignore
416
446
  const deserialize = message => {
417
447
  return JSON.parse(message.toString());
418
448
  };
419
449
 
450
+ // @ts-ignore
420
451
  const handleUpgrade = async (...args) => {
421
452
  const module = await import('@lvce-editor/web-socket-server');
422
453
  // @ts-ignore
423
454
  return module.handleUpgrade(...args);
424
455
  };
425
456
 
457
+ // @ts-ignore
426
458
  const listen = async ({
427
459
  request,
428
460
  handle
@@ -440,6 +472,8 @@ const listen = async ({
440
472
  }
441
473
  return webSocket;
442
474
  };
475
+
476
+ // @ts-ignore
443
477
  const wrap$3 = webSocket => {
444
478
  return {
445
479
  webSocket,
@@ -447,9 +481,11 @@ const wrap$3 = webSocket => {
447
481
  * @type {any}
448
482
  */
449
483
  wrappedListener: undefined,
484
+ // @ts-ignore
450
485
  on(event, listener) {
451
486
  switch (event) {
452
487
  case 'message':
488
+ // @ts-ignore
453
489
  const wrappedListener = message => {
454
490
  const data = deserialize(message);
455
491
  listener(data);
@@ -463,9 +499,11 @@ const wrap$3 = webSocket => {
463
499
  throw new Error('unknown event listener type');
464
500
  }
465
501
  },
502
+ // @ts-ignore
466
503
  off(event, listener) {
467
504
  this.webSocket.off(event, listener);
468
505
  },
506
+ // @ts-ignore
469
507
  send(message) {
470
508
  const stringifiedMessage = serialize(message);
471
509
  this.webSocket.send(stringifiedMessage);
@@ -494,6 +532,7 @@ const Message = 3;
494
532
  * @param {any} utilityProcess
495
533
  * @returns
496
534
  */
535
+ // @ts-ignore
497
536
  const getFirstUtilityProcessEvent = async utilityProcess => {
498
537
  const {
499
538
  resolve,
@@ -501,6 +540,7 @@ const getFirstUtilityProcessEvent = async utilityProcess => {
501
540
  } = withResolvers();
502
541
  let stdout = '';
503
542
  let stderr = '';
543
+ // @ts-ignore
504
544
  const cleanup = value => {
505
545
  // @ts-ignore
506
546
  utilityProcess.stderr.off('data', handleStdErrData);
@@ -508,14 +548,18 @@ const getFirstUtilityProcessEvent = async utilityProcess => {
508
548
  utilityProcess.stdout.off('data', handleStdoutData);
509
549
  utilityProcess.off('message', handleMessage);
510
550
  utilityProcess.off('exit', handleExit);
551
+ // @ts-ignore
511
552
  resolve(value);
512
553
  };
554
+ // @ts-ignore
513
555
  const handleStdErrData = data => {
514
556
  stderr += data;
515
557
  };
558
+ // @ts-ignore
516
559
  const handleStdoutData = data => {
517
560
  stdout += data;
518
561
  };
562
+ // @ts-ignore
519
563
  const handleMessage = event => {
520
564
  cleanup({
521
565
  type: Message,
@@ -524,6 +568,7 @@ const getFirstUtilityProcessEvent = async utilityProcess => {
524
568
  stderr
525
569
  });
526
570
  };
571
+ // @ts-ignore
527
572
  const handleExit = event => {
528
573
  cleanup({
529
574
  type: Exit,
@@ -551,6 +596,7 @@ const getFirstUtilityProcessEvent = async utilityProcess => {
551
596
  };
552
597
  };
553
598
 
599
+ // @ts-ignore
554
600
  const create$2 = async ({
555
601
  path,
556
602
  argv = [],
@@ -583,15 +629,20 @@ const create$2 = async ({
583
629
  childProcess.stderr.pipe(process.stderr);
584
630
  return childProcess;
585
631
  };
632
+
633
+ // @ts-ignore
586
634
  const wrap$2 = process => {
587
635
  return {
588
636
  process,
637
+ // @ts-ignore
589
638
  on(event, listener) {
590
639
  this.process.on(event, listener);
591
640
  },
641
+ // @ts-ignore
592
642
  send(message) {
593
643
  this.process.postMessage(message);
594
644
  },
645
+ // @ts-ignore
595
646
  sendAndTransfer(message, transfer) {
596
647
  array(transfer);
597
648
  this.process.postMessage(message, transfer);
@@ -609,6 +660,7 @@ const IpcParentWithElectronUtilityProcess = {
609
660
  };
610
661
 
611
662
  class ChildProcessError extends Error {
663
+ // @ts-ignore
612
664
  constructor(stderr) {
613
665
  // @ts-ignore
614
666
  const {
@@ -623,6 +675,7 @@ class ChildProcessError extends Error {
623
675
  this.code = code;
624
676
  }
625
677
  if (stack) {
678
+ // @ts-ignore
626
679
  const lines = splitLines(this.stack);
627
680
  const [firstLine, ...stackLines] = lines;
628
681
  const newStackLines = [firstLine, ...stack, ...stackLines];
@@ -632,6 +685,7 @@ class ChildProcessError extends Error {
632
685
  }
633
686
  }
634
687
 
688
+ // @ts-ignore
635
689
  const getFirstNodeChildProcessEvent = async childProcess => {
636
690
  // @ts-ignore
637
691
  const {
@@ -642,6 +696,7 @@ const getFirstNodeChildProcessEvent = async childProcess => {
642
696
  } = await new Promise((resolve, reject) => {
643
697
  let stderr = '';
644
698
  let stdout = '';
699
+ // @ts-ignore
645
700
  const cleanup = value => {
646
701
  if (childProcess.stdout && childProcess.stderr) {
647
702
  childProcess.stderr.off('data', handleStdErrData);
@@ -652,12 +707,15 @@ const getFirstNodeChildProcessEvent = async childProcess => {
652
707
  childProcess.off('error', handleError);
653
708
  resolve(value);
654
709
  };
710
+ // @ts-ignore
655
711
  const handleStdErrData = data => {
656
712
  stderr += data;
657
713
  };
714
+ // @ts-ignore
658
715
  const handleStdoutData = data => {
659
716
  stdout += data;
660
717
  };
718
+ // @ts-ignore
661
719
  const handleMessage = event => {
662
720
  cleanup({
663
721
  type: Message,
@@ -666,6 +724,7 @@ const getFirstNodeChildProcessEvent = async childProcess => {
666
724
  stderr
667
725
  });
668
726
  };
727
+ // @ts-ignore
669
728
  const handleExit = event => {
670
729
  cleanup({
671
730
  type: Exit,
@@ -674,6 +733,7 @@ const getFirstNodeChildProcessEvent = async childProcess => {
674
733
  stderr
675
734
  });
676
735
  };
736
+ // @ts-ignore
677
737
  const handleError = event => {
678
738
  cleanup({
679
739
  type: Error$1,
@@ -698,6 +758,7 @@ const getFirstNodeChildProcessEvent = async childProcess => {
698
758
  };
699
759
  };
700
760
 
761
+ // @ts-ignore
701
762
  const create$1 = async ({
702
763
  path,
703
764
  argv = [],
@@ -706,9 +767,13 @@ const create$1 = async ({
706
767
  stdio = 'inherit',
707
768
  name = 'child process'
708
769
  }) => {
770
+ // @ts-ignore
709
771
  try {
710
772
  string(path);
711
773
  const actualArgv = ['--ipc-type=node-forked-process', ...argv];
774
+ const {
775
+ fork
776
+ } = await import('node:child_process');
712
777
  const childProcess = fork(path, actualArgv, {
713
778
  env,
714
779
  execArgv,
@@ -735,18 +800,24 @@ const create$1 = async ({
735
800
  throw new VError(error, `Failed to launch ${name}`);
736
801
  }
737
802
  };
803
+
804
+ // @ts-ignore
738
805
  const wrap$1 = childProcess => {
739
806
  return {
740
807
  childProcess,
808
+ // @ts-ignore
741
809
  on(event, listener) {
742
810
  this.childProcess.on(event, listener);
743
811
  },
812
+ // @ts-ignore
744
813
  off(event, listener) {
745
814
  this.childProcess.off(event, listener);
746
815
  },
816
+ // @ts-ignore
747
817
  send(message) {
748
818
  this.childProcess.send(message);
749
819
  },
820
+ // @ts-ignore
750
821
  sendAndTransfer(message, handle) {
751
822
  this.childProcess.send(message, handle);
752
823
  },
@@ -763,6 +834,7 @@ const IpcParentWithNodeForkedProcess = {
763
834
  wrap: wrap$1
764
835
  };
765
836
 
837
+ // @ts-ignore
766
838
  const getFirstNodeWorkerEvent = worker => {
767
839
  return getFirstEvent(worker, {
768
840
  exit: Exit,
@@ -770,18 +842,23 @@ const getFirstNodeWorkerEvent = worker => {
770
842
  });
771
843
  };
772
844
 
845
+ // @ts-ignore
773
846
  const create = async ({
774
847
  path,
775
848
  argv = [],
776
849
  env = process.env,
777
850
  execArgv = []
778
851
  }) => {
852
+ // @ts-ignore
779
853
  string(path);
780
854
  const actualArgv = ['--ipc-type=node-worker', ...argv];
781
855
  const actualEnv = {
782
856
  ...env,
783
857
  ELECTRON_RUN_AS_NODE: '1'
784
858
  };
859
+ const {
860
+ Worker
861
+ } = await import('node:worker_threads');
785
862
  const worker = new Worker(path, {
786
863
  argv: actualArgv,
787
864
  env: actualEnv,
@@ -803,15 +880,20 @@ const create = async ({
803
880
  }
804
881
  return worker;
805
882
  };
883
+
884
+ // @ts-ignore
806
885
  const wrap = worker => {
807
886
  return {
808
887
  worker,
888
+ // @ts-ignore
809
889
  on(event, listener) {
810
890
  this.worker.on(event, listener);
811
891
  },
892
+ // @ts-ignore
812
893
  send(message) {
813
894
  this.worker.postMessage(message);
814
895
  },
896
+ // @ts-ignore
815
897
  sendAndTransfer(message, transfer) {
816
898
  array(transfer);
817
899
  this.worker.postMessage(message, transfer);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/ipc",
3
- "version": "3.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Inter Process Communication for Lvce Editor",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -20,5 +20,6 @@
20
20
  },
21
21
  "engines": {
22
22
  "node": ">=18"
23
- }
23
+ },
24
+ "types": "dist/index.d.ts"
24
25
  }