@lvce-editor/ipc 11.1.0 → 11.3.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
@@ -2,6 +2,30 @@ const getData$1 = event => {
2
2
  return event.data;
3
3
  };
4
4
 
5
+ const attachEvents = that => {
6
+ const handleMessage = (...args) => {
7
+ const data = that.getData(...args);
8
+ that.dispatchEvent(new MessageEvent('message', {
9
+ data
10
+ }));
11
+ };
12
+ that.onMessage(handleMessage);
13
+ const handleClose = event => {
14
+ that.dispatchEvent(new Event('close'));
15
+ };
16
+ that.onClose(handleClose);
17
+ };
18
+
19
+ class Ipc extends EventTarget {
20
+ constructor(rawIpc) {
21
+ super();
22
+ this._rawIpc = rawIpc;
23
+ attachEvents(this);
24
+ }
25
+ }
26
+
27
+ const readyMessage = 'ready';
28
+
5
29
  const walkValue = (value, transferrables, isTransferrable) => {
6
30
  if (!value) {
7
31
  return;
@@ -61,38 +85,146 @@ const getTransferrables = value => {
61
85
  return transferrables;
62
86
  };
63
87
 
64
- const attachEvents = that => {
65
- const handleMessage = (...args) => {
66
- const data = that.getData(...args);
67
- that.dispatchEvent(new MessageEvent('message', {
68
- data
69
- }));
70
- };
71
- that.onMessage(handleMessage);
72
- const handleClose = event => {
73
- that.dispatchEvent(new Event('close'));
88
+ const removeValues = (value, toRemove) => {
89
+ if (!value) {
90
+ return value;
91
+ }
92
+ if (Array.isArray(value)) {
93
+ const newItems = [];
94
+ for (const item of value) {
95
+ if (!toRemove.includes(item)) {
96
+ newItems.push(removeValues(item, toRemove));
97
+ }
98
+ }
99
+ return newItems;
100
+ }
101
+ if (typeof value === 'object') {
102
+ const newObject = Object.create(null);
103
+ for (const [key, property] of Object.entries(value)) {
104
+ if (!toRemove.includes(property)) {
105
+ newObject[key] = removeValues(property, toRemove);
106
+ }
107
+ }
108
+ return newObject;
109
+ }
110
+ return value;
111
+ };
112
+
113
+ // workaround for electron not supporting transferrable objects
114
+ // as parameters. If the transferrable object is a parameter, in electron
115
+ // only an empty objected is received in the main process
116
+ const fixElectronParameters = value => {
117
+ const transfer = getTransferrables(value);
118
+ const newValue = removeValues(value, transfer);
119
+ return {
120
+ newValue,
121
+ transfer
74
122
  };
75
- that.onClose(handleClose);
76
123
  };
77
124
 
78
- class Ipc extends EventTarget {
79
- constructor(rawIpc) {
80
- super();
81
- this._rawIpc = rawIpc;
82
- attachEvents(this);
125
+ const listen$4 = () => {
126
+ return window;
127
+ };
128
+ const signal$4 = global => {
129
+ global.postMessage(readyMessage);
130
+ };
131
+ class IpcChildWithElectronWindow extends Ipc {
132
+ getData(event) {
133
+ return getData$1(event);
134
+ }
135
+ send(message) {
136
+ this._rawIpc.postMessage(message);
137
+ }
138
+ sendAndTransfer(message) {
139
+ const {
140
+ newValue,
141
+ transfer
142
+ } = fixElectronParameters(message);
143
+ this._rawIpc.postMessage(newValue, location.origin, transfer);
144
+ }
145
+ dispose() {
146
+ // ignore
147
+ }
148
+ onClose(callback) {
149
+ // ignore
150
+ }
151
+ onMessage(callback) {
152
+ const wrapped = event => {
153
+ const {
154
+ ports
155
+ } = event;
156
+ if (ports.length) {
157
+ return;
158
+ }
159
+ callback(event);
160
+ this._rawIpc.removeEventListener('message', wrapped);
161
+ };
162
+ this._rawIpc.addEventListener('message', wrapped);
83
163
  }
84
164
  }
165
+ const wrap$7 = window => {
166
+ return new IpcChildWithElectronWindow(window);
167
+ };
85
168
 
86
- const readyMessage = 'ready';
169
+ const IpcChildWithElectronWindow$1 = {
170
+ __proto__: null,
171
+ listen: listen$4,
172
+ signal: signal$4,
173
+ wrap: wrap$7
174
+ };
87
175
 
88
- const listen$4 = () => {
176
+ const listen$3 = ({
177
+ port
178
+ }) => {
179
+ return port;
180
+ };
181
+ const signal$3 = port => {
182
+ port.postMessage(readyMessage);
183
+ };
184
+ class IpcChildWithMessagePort extends Ipc {
185
+ constructor(port) {
186
+ super(port);
187
+ }
188
+ getData(event) {
189
+ return getData$1(event);
190
+ }
191
+ send(message) {
192
+ this._rawIpc.postMessage(message);
193
+ }
194
+ sendAndTransfer(message) {
195
+ const transfer = getTransferrables(message);
196
+ this._rawIpc.postMessage(message, transfer);
197
+ }
198
+ dispose() {
199
+ // ignore
200
+ }
201
+ onClose(callback) {
202
+ // ignore
203
+ }
204
+ onMessage(callback) {
205
+ this._rawIpc.addEventListener('message', callback);
206
+ this._rawIpc.start();
207
+ }
208
+ }
209
+ const wrap$6 = port => {
210
+ return new IpcChildWithMessagePort(port);
211
+ };
212
+
213
+ const IpcChildWithMessagePort$1 = {
214
+ __proto__: null,
215
+ listen: listen$3,
216
+ signal: signal$3,
217
+ wrap: wrap$6
218
+ };
219
+
220
+ const listen$2 = () => {
89
221
  // @ts-ignore
90
222
  if (typeof WorkerGlobalScope === 'undefined') {
91
223
  throw new TypeError('module is not in web worker scope');
92
224
  }
93
225
  return globalThis;
94
226
  };
95
- const signal$3 = global => {
227
+ const signal$2 = global => {
96
228
  global.postMessage(readyMessage);
97
229
  };
98
230
  class IpcChildWithModuleWorker extends Ipc {
@@ -118,15 +250,15 @@ class IpcChildWithModuleWorker extends Ipc {
118
250
  this._rawIpc.addEventListener('message', callback);
119
251
  }
120
252
  }
121
- const wrap$6 = global => {
253
+ const wrap$5 = global => {
122
254
  return new IpcChildWithModuleWorker(global);
123
255
  };
124
256
 
125
257
  const IpcChildWithModuleWorker$1 = {
126
258
  __proto__: null,
127
- listen: listen$4,
128
- signal: signal$3,
129
- wrap: wrap$6
259
+ listen: listen$2,
260
+ signal: signal$2,
261
+ wrap: wrap$5
130
262
  };
131
263
 
132
264
  const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
@@ -250,10 +382,10 @@ const getHelpfulChildProcessError = (stdout, stderr) => {
250
382
 
251
383
  const normalizeLine = line => {
252
384
  if (line.startsWith('Error: ')) {
253
- return line.slice(`Error: `.length);
385
+ return line.slice('Error: '.length);
254
386
  }
255
387
  if (line.startsWith('VError: ')) {
256
- return line.slice(`VError: `.length);
388
+ return line.slice('VError: '.length);
257
389
  }
258
390
  return line;
259
391
  };
@@ -355,10 +487,10 @@ const waitForFirstMessage = async port => {
355
487
  return event.data;
356
488
  };
357
489
 
358
- const listen$3 = async () => {
359
- const parentIpcRaw = listen$4();
360
- signal$3(parentIpcRaw);
361
- const parentIpc = wrap$6(parentIpcRaw);
490
+ const listen$1 = async () => {
491
+ const parentIpcRaw = listen$2();
492
+ signal$2(parentIpcRaw);
493
+ const parentIpc = wrap$5(parentIpcRaw);
362
494
  const firstMessage = await waitForFirstMessage(parentIpc);
363
495
  if (firstMessage.method !== 'initialize') {
364
496
  throw new IpcError('unexpected first message');
@@ -403,20 +535,20 @@ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
403
535
  this._rawIpc.start();
404
536
  }
405
537
  }
406
- const wrap$5 = port => {
538
+ const wrap$4 = port => {
407
539
  return new IpcChildWithModuleWorkerAndMessagePort(port);
408
540
  };
409
541
 
410
542
  const IpcChildWithModuleWorkerAndMessagePort$1 = {
411
543
  __proto__: null,
412
- listen: listen$3,
413
- wrap: wrap$5
544
+ listen: listen$1,
545
+ wrap: wrap$4
414
546
  };
415
547
 
416
- const listen$2 = () => {
548
+ const listen = () => {
417
549
  return window;
418
550
  };
419
- const signal$2 = global => {
551
+ const signal$1 = global => {
420
552
  global.postMessage(readyMessage);
421
553
  };
422
554
  class IpcChildWithWindow extends Ipc {
@@ -450,151 +582,18 @@ class IpcChildWithWindow extends Ipc {
450
582
  this._rawIpc.addEventListener('message', wrapped);
451
583
  }
452
584
  }
453
- const wrap$4 = window => {
585
+ const wrap$3 = window => {
454
586
  return new IpcChildWithWindow(window);
455
587
  };
456
588
 
457
589
  const IpcChildWithWindow$1 = {
458
590
  __proto__: null,
459
- listen: listen$2,
460
- signal: signal$2,
461
- wrap: wrap$4
462
- };
463
-
464
- const removeValues = (value, toRemove) => {
465
- if (!value) {
466
- return value;
467
- }
468
- if (Array.isArray(value)) {
469
- const newItems = [];
470
- for (const item of value) {
471
- if (!toRemove.includes(item)) {
472
- newItems.push(removeValues(item, toRemove));
473
- }
474
- }
475
- return newItems;
476
- }
477
- if (typeof value === 'object') {
478
- const newObject = Object.create(null);
479
- for (const [key, property] of Object.entries(value)) {
480
- if (!toRemove.includes(property)) {
481
- newObject[key] = removeValues(property, toRemove);
482
- }
483
- }
484
- return newObject;
485
- }
486
- return value;
487
- };
488
-
489
- // workaround for electron not supporting transferrable objects
490
- // as parameters. If the transferrable object is a parameter, in electron
491
- // only an empty objected is received in the main process
492
- const fixElectronParameters = value => {
493
- const transfer = getTransferrables(value);
494
- const newValue = removeValues(value, transfer);
495
- return {
496
- newValue,
497
- transfer
498
- };
499
- };
500
-
501
- const listen$1 = () => {
502
- return window;
503
- };
504
- const signal$1 = global => {
505
- global.postMessage(readyMessage);
506
- };
507
- class IpcChildWithElectronWindow extends Ipc {
508
- getData(event) {
509
- return getData$1(event);
510
- }
511
- send(message) {
512
- this._rawIpc.postMessage(message);
513
- }
514
- sendAndTransfer(message) {
515
- const {
516
- newValue,
517
- transfer
518
- } = fixElectronParameters(message);
519
- this._rawIpc.postMessage(newValue, location.origin, transfer);
520
- }
521
- dispose() {
522
- // ignore
523
- }
524
- onClose(callback) {
525
- // ignore
526
- }
527
- onMessage(callback) {
528
- const wrapped = event => {
529
- const {
530
- ports
531
- } = event;
532
- if (ports.length) {
533
- return;
534
- }
535
- callback(event);
536
- this._rawIpc.removeEventListener('message', wrapped);
537
- };
538
- this._rawIpc.addEventListener('message', wrapped);
539
- }
540
- }
541
- const wrap$3 = window => {
542
- return new IpcChildWithElectronWindow(window);
543
- };
544
-
545
- const IpcChildWithElectronWindow$1 = {
546
- __proto__: null,
547
- listen: listen$1,
591
+ listen,
548
592
  signal: signal$1,
549
593
  wrap: wrap$3
550
594
  };
551
595
 
552
- const listen = ({
553
- port
554
- }) => {
555
- return port;
556
- };
557
- const signal = port => {
558
- port.postMessage(readyMessage);
559
- };
560
- class IpcChildWithMessagePort extends Ipc {
561
- constructor(port) {
562
- super(port);
563
- }
564
- getData(event) {
565
- return getData$1(event);
566
- }
567
- send(message) {
568
- this._rawIpc.postMessage(message);
569
- }
570
- sendAndTransfer(message) {
571
- const transfer = getTransferrables(message);
572
- this._rawIpc.postMessage(message, transfer);
573
- }
574
- dispose() {
575
- // ignore
576
- }
577
- onClose(callback) {
578
- // ignore
579
- }
580
- onMessage(callback) {
581
- this._rawIpc.addEventListener('message', callback);
582
- this._rawIpc.start();
583
- }
584
- }
585
- const wrap$2 = port => {
586
- return new IpcChildWithMessagePort(port);
587
- };
588
-
589
- const IpcChildWithMessagePort$1 = {
590
- __proto__: null,
591
- listen,
592
- signal,
593
- wrap: wrap$2
594
- };
595
-
596
- const Message = 'message';
597
- const Error$1 = 'error';
596
+ const Message$1 = 3;
598
597
 
599
598
  const addListener = (emitter, type, callback) => {
600
599
  if ('addEventListener' in emitter) {
@@ -635,6 +634,59 @@ const getFirstEvent = (eventEmitter, eventMap) => {
635
634
  return promise;
636
635
  };
637
636
 
637
+ const create$2 = async ({
638
+ messagePort
639
+ }) => {
640
+ if (!isMessagePort(messagePort)) {
641
+ throw new IpcError('port must be of type MessagePort');
642
+ }
643
+ const eventPromise = getFirstEvent(messagePort, {
644
+ message: Message$1
645
+ });
646
+ messagePort.start();
647
+ const event = await eventPromise;
648
+ // @ts-ignore
649
+ if (event.data !== readyMessage) {
650
+ throw new IpcError('unexpected first message');
651
+ }
652
+ return messagePort;
653
+ };
654
+ const signal = messagePort => {
655
+ messagePort.start();
656
+ };
657
+ class IpcParentWithMessagePort extends Ipc {
658
+ constructor(port) {
659
+ super(port);
660
+ }
661
+ getData = getData$1;
662
+ send(message) {
663
+ this._rawIpc.postMessage(message);
664
+ }
665
+ sendAndTransfer(message) {
666
+ throw new Error('not implemented');
667
+ }
668
+ dispose() {
669
+ this._rawIpc.close();
670
+ }
671
+ onMessage(callback) {
672
+ this._rawIpc.addEventListener('message', callback);
673
+ }
674
+ onClose(callback) {}
675
+ }
676
+ const wrap$2 = messagePort => {
677
+ return new IpcParentWithMessagePort(messagePort);
678
+ };
679
+
680
+ const IpcParentWithMessagePort$1 = {
681
+ __proto__: null,
682
+ create: create$2,
683
+ signal,
684
+ wrap: wrap$2
685
+ };
686
+
687
+ const Message = 'message';
688
+ const Error$1 = 'error';
689
+
638
690
  const getFirstWorkerEvent = worker => {
639
691
  return getFirstEvent(worker, {
640
692
  message: Message,
@@ -808,4 +860,4 @@ const IpcParentWithWebSocket$1 = {
808
860
  wrap
809
861
  };
810
862
 
811
- export { IpcChildWithElectronWindow$1 as IpcChildWithElectronWindow, IpcChildWithMessagePort$1 as IpcChildWithMessagePort, IpcChildWithModuleWorker$1 as IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort$1 as IpcChildWithModuleWorkerAndMessagePort, IpcChildWithWindow$1 as IpcChildWithWindow, IpcParentWithModuleWorker$1 as IpcParentWithModuleWorker, IpcParentWithWebSocket$1 as IpcParentWithWebSocket };
863
+ export { IpcChildWithElectronWindow$1 as IpcChildWithElectronWindow, IpcChildWithMessagePort$1 as IpcChildWithMessagePort, IpcChildWithModuleWorker$1 as IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort$1 as IpcChildWithModuleWorkerAndMessagePort, IpcChildWithWindow$1 as IpcChildWithWindow, IpcParentWithMessagePort$1 as IpcParentWithMessagePort, IpcParentWithModuleWorker$1 as IpcParentWithModuleWorker, IpcParentWithWebSocket$1 as IpcParentWithWebSocket };
package/dist/electron.js CHANGED
@@ -356,10 +356,10 @@ const getHelpfulChildProcessError = (stdout, stderr) => {
356
356
 
357
357
  const normalizeLine = line => {
358
358
  if (line.startsWith('Error: ')) {
359
- return line.slice(`Error: `.length);
359
+ return line.slice('Error: '.length);
360
360
  }
361
361
  if (line.startsWith('VError: ')) {
362
- return line.slice(`VError: `.length);
362
+ return line.slice('VError: '.length);
363
363
  }
364
364
  return line;
365
365
  };
package/dist/index.d.ts CHANGED
@@ -26,3 +26,4 @@ export const IpcParentWithElectronUtilityProcess: IpcParent
26
26
  export const IpcParentWithNodeForkedProcess: IpcParent
27
27
  export const IpcParentWithNodeWorker: IpcParent
28
28
  export const IpcParentWithElectronMessagePort: IpcParent
29
+ export const IpcParentWithMessagePort: IpcParent
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@lvce-editor/ipc",
3
- "version": "11.1.0",
3
+ "version": "11.3.0",
4
4
  "description": "Inter Process Communication for Lvce Editor",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
7
+ "sideEffects": false,
7
8
  "keywords": [
8
9
  "ipc"
9
10
  ],
@@ -15,8 +16,8 @@
15
16
  },
16
17
  "dependencies": {
17
18
  "@lvce-editor/assert": "^1.3.0",
18
- "@lvce-editor/verror": "^1.4.0",
19
- "@lvce-editor/web-socket-server": "^1.3.0"
19
+ "@lvce-editor/verror": "^1.6.0",
20
+ "@lvce-editor/web-socket-server": "^1.4.0"
20
21
  },
21
22
  "engines": {
22
23
  "node": ">=18"