@lvce-editor/ipc 3.2.0 → 3.4.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,340 @@
1
+ const getData = event => {
2
+ return event.data;
3
+ };
4
+
5
+ const listen$1 = () => {
6
+ // @ts-ignore
7
+ if (typeof WorkerGlobalScope === 'undefined') {
8
+ throw new TypeError('module is not in web worker scope');
9
+ }
10
+ // @ts-ignore
11
+ globalThis.postMessage('ready');
12
+ return globalThis;
13
+ };
14
+ const signal = global => {
15
+ global.postMessage('ready');
16
+ };
17
+ const wrap$1 = global => {
18
+ return {
19
+ global,
20
+ /**
21
+ * @type {any}
22
+ */
23
+ listener: undefined,
24
+ send(message) {
25
+ this.global.postMessage(message);
26
+ },
27
+ sendAndTransfer(message, transferables) {
28
+ this.global.postMessage(message, transferables);
29
+ },
30
+ get onmessage() {
31
+ return this.listener;
32
+ },
33
+ set onmessage(listener) {
34
+ const wrappedListener = event => {
35
+ const data = getData(event);
36
+ // @ts-expect-error
37
+ listener({
38
+ data,
39
+ target: this
40
+ });
41
+ };
42
+ this.listener = listener;
43
+ this.global.onmessage = wrappedListener;
44
+ }
45
+ };
46
+ };
47
+
48
+ const IpcChildWithModuleWorker = {
49
+ __proto__: null,
50
+ listen: listen$1,
51
+ signal,
52
+ wrap: wrap$1
53
+ };
54
+
55
+ const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
56
+ const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
57
+ const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
58
+
59
+ const NewLine$1 = '\n';
60
+
61
+ const joinLines = lines => {
62
+ return lines.join(NewLine$1);
63
+ };
64
+
65
+ const splitLines = lines => {
66
+ return lines.split(NewLine$1);
67
+ };
68
+
69
+ const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
70
+ const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
71
+ const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
72
+ const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
73
+ const RE_AT = /^\s+at/;
74
+ const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
75
+ const isUnhelpfulNativeModuleError = stderr => {
76
+ return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
77
+ };
78
+ const isMessageCodeBlockStartIndex = line => {
79
+ return RE_MESSAGE_CODE_BLOCK_START.test(line);
80
+ };
81
+ const isMessageCodeBlockEndIndex = line => {
82
+ return RE_MESSAGE_CODE_BLOCK_END.test(line);
83
+ };
84
+ const getMessageCodeBlock = stderr => {
85
+ const lines = splitLines(stderr);
86
+ const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
87
+ const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
88
+ const relevantLines = lines.slice(startIndex, endIndex);
89
+ const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
90
+ return relevantMessage;
91
+ };
92
+ const getNativeModuleErrorMessage = stderr => {
93
+ const message = getMessageCodeBlock(stderr);
94
+ return {
95
+ message: `Incompatible native node module: ${message}`,
96
+ code: E_INCOMPATIBLE_NATIVE_MODULE
97
+ };
98
+ };
99
+ const isModulesSyntaxError = stderr => {
100
+ if (!stderr) {
101
+ return false;
102
+ }
103
+ return stderr.includes('SyntaxError: Cannot use import statement outside a module');
104
+ };
105
+ const getModuleSyntaxError = () => {
106
+ return {
107
+ message: `ES Modules are not supported in electron`,
108
+ code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
109
+ };
110
+ };
111
+ const isModuleNotFoundError = stderr => {
112
+ if (!stderr) {
113
+ return false;
114
+ }
115
+ return stderr.includes('ERR_MODULE_NOT_FOUND');
116
+ };
117
+ const isModuleNotFoundMessage = line => {
118
+ return line.includes('ERR_MODULE_NOT_FOUND');
119
+ };
120
+ const getModuleNotFoundError = stderr => {
121
+ const lines = splitLines(stderr);
122
+ const messageIndex = lines.findIndex(isModuleNotFoundMessage);
123
+ const message = lines[messageIndex];
124
+ return {
125
+ message,
126
+ code: ERR_MODULE_NOT_FOUND
127
+ };
128
+ };
129
+ const isNormalStackLine = line => {
130
+ return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
131
+ };
132
+ const getDetails = lines => {
133
+ const index = lines.findIndex(isNormalStackLine);
134
+ if (index === -1) {
135
+ return {
136
+ actualMessage: joinLines(lines),
137
+ rest: []
138
+ };
139
+ }
140
+ let lastIndex = index - 1;
141
+ while (++lastIndex < lines.length) {
142
+ if (!isNormalStackLine(lines[lastIndex])) {
143
+ break;
144
+ }
145
+ }
146
+ return {
147
+ actualMessage: lines[index - 1],
148
+ rest: lines.slice(index, lastIndex)
149
+ };
150
+ };
151
+ const getHelpfulChildProcessError = (stdout, stderr) => {
152
+ if (isUnhelpfulNativeModuleError(stderr)) {
153
+ return getNativeModuleErrorMessage(stderr);
154
+ }
155
+ if (isModulesSyntaxError(stderr)) {
156
+ return getModuleSyntaxError();
157
+ }
158
+ if (isModuleNotFoundError(stderr)) {
159
+ return getModuleNotFoundError(stderr);
160
+ }
161
+ const lines = splitLines(stderr);
162
+ const {
163
+ actualMessage,
164
+ rest
165
+ } = getDetails(lines);
166
+ return {
167
+ message: `${actualMessage}`,
168
+ code: '',
169
+ stack: rest
170
+ };
171
+ };
172
+
173
+ const normalizeLine = line => {
174
+ if (line.startsWith('Error: ')) {
175
+ return line.slice(`Error: `.length);
176
+ }
177
+ if (line.startsWith('VError: ')) {
178
+ return line.slice(`VError: `.length);
179
+ }
180
+ return line;
181
+ };
182
+ const getCombinedMessage = (error, message) => {
183
+ const stringifiedError = normalizeLine(`${error}`);
184
+ if (message) {
185
+ return `${message}: ${stringifiedError}`;
186
+ }
187
+ return stringifiedError;
188
+ };
189
+ const NewLine = '\n';
190
+ const getNewLineIndex = (string, startIndex = undefined) => {
191
+ return string.indexOf(NewLine, startIndex);
192
+ };
193
+ const mergeStacks = (parent, child) => {
194
+ if (!child) {
195
+ return parent;
196
+ }
197
+ const parentNewLineIndex = getNewLineIndex(parent);
198
+ const childNewLineIndex = getNewLineIndex(child);
199
+ if (childNewLineIndex === -1) {
200
+ return parent;
201
+ }
202
+ const parentFirstLine = parent.slice(0, parentNewLineIndex);
203
+ const childRest = child.slice(childNewLineIndex);
204
+ const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
205
+ if (parentFirstLine.includes(childFirstLine)) {
206
+ return parentFirstLine + childRest;
207
+ }
208
+ return child;
209
+ };
210
+ class VError extends Error {
211
+ constructor(error, message) {
212
+ const combinedMessage = getCombinedMessage(error, message);
213
+ super(combinedMessage);
214
+ this.name = 'VError';
215
+ if (error instanceof Error) {
216
+ this.stack = mergeStacks(this.stack, error.stack);
217
+ }
218
+ if (error.codeFrame) {
219
+ // @ts-ignore
220
+ this.codeFrame = error.codeFrame;
221
+ }
222
+ if (error.code) {
223
+ // @ts-ignore
224
+ this.code = error.code;
225
+ }
226
+ }
227
+ }
228
+
229
+ class IpcError extends VError {
230
+ // @ts-ignore
231
+ constructor(message, stdout = '', stderr = '') {
232
+ if (stdout || stderr) {
233
+ // @ts-ignore
234
+ const {
235
+ message,
236
+ code,
237
+ stack
238
+ } = getHelpfulChildProcessError(stdout, stderr);
239
+ const cause = new Error(message);
240
+ // @ts-ignore
241
+ cause.code = code;
242
+ cause.stack = stack;
243
+ super(cause, message);
244
+ } else {
245
+ super(message);
246
+ }
247
+ // @ts-ignore
248
+ this.name = 'IpcError';
249
+ // @ts-ignore
250
+ this.stdout = stdout;
251
+ // @ts-ignore
252
+ this.stderr = stderr;
253
+ }
254
+ }
255
+
256
+ const withResolvers = () => {
257
+ let _resolve;
258
+ const promise = new Promise(resolve => {
259
+ _resolve = resolve;
260
+ });
261
+ return {
262
+ resolve: _resolve,
263
+ promise
264
+ };
265
+ };
266
+
267
+ const waitForFirstMessage = async port => {
268
+ const {
269
+ resolve,
270
+ promise
271
+ } = withResolvers();
272
+ const cleanup = value => {
273
+ port.onmessage = null;
274
+ resolve(value);
275
+ };
276
+ const handleMessage = event => {
277
+ cleanup(event);
278
+ };
279
+ port.onmessage = handleMessage;
280
+ const event = await promise;
281
+ // @ts-expect-error
282
+ return event.data;
283
+ };
284
+
285
+ const listen = async () => {
286
+ const parentIpcRaw = listen$1();
287
+ const parentIpc = wrap$1(parentIpcRaw);
288
+ const firstMessage = await waitForFirstMessage(parentIpc);
289
+ if (firstMessage.method !== 'initialize') {
290
+ throw new IpcError('unexpected first message');
291
+ }
292
+ const type = firstMessage.params[0];
293
+ if (type === 'message-port') {
294
+ const port = firstMessage.params[1];
295
+ return port;
296
+ }
297
+ return globalThis;
298
+ };
299
+ const wrap = port => {
300
+ return {
301
+ port,
302
+ /**
303
+ * @type {any}
304
+ */
305
+ wrappedListener: undefined,
306
+ send(message) {
307
+ this.port.postMessage(message);
308
+ },
309
+ sendAndTransfer(message, transferables) {
310
+ this.port.postMessage(message, transferables);
311
+ },
312
+ get onmessage() {
313
+ return this.wrappedListener;
314
+ },
315
+ set onmessage(listener) {
316
+ if (listener) {
317
+ // @ts-expect-error
318
+ this.wrappedListener = event => {
319
+ const data = getData(event);
320
+ // @ts-expect-error
321
+ listener({
322
+ data,
323
+ target: this
324
+ });
325
+ };
326
+ } else {
327
+ this.wrappedListener = undefined;
328
+ }
329
+ this.port.onmessage = this.wrappedListener;
330
+ }
331
+ };
332
+ };
333
+
334
+ const IpcChildWithModuleWorkerAndMessagePort = {
335
+ __proto__: null,
336
+ listen,
337
+ wrap
338
+ };
339
+
340
+ export { IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort };
package/dist/index.d.ts CHANGED
@@ -7,8 +7,6 @@ export const IpcChildWithElectronMessagePort: IpcChild
7
7
  export const IpcChildWithElectronUtilityProcess: IpcChild
8
8
  export const IpcChildWithNodeForkedProcess: IpcChild
9
9
  export const IpcChildWithWebSocket: IpcChild
10
- export const IpcChildWithModuleWorkerAndMessagePort: IpcChild
11
- export const IpcChildWithModuleWorker: IpcChild
12
10
 
13
11
  interface IpcParent {
14
12
  readonly create: any
package/dist/index.js CHANGED
@@ -151,7 +151,7 @@ const isMessagePortMain = value => {
151
151
  };
152
152
 
153
153
  // @ts-ignore
154
- const listen$5 = ({
154
+ const listen$6 = ({
155
155
  messagePort
156
156
  }) => {
157
157
  if (!isMessagePortMain(messagePort)) {
@@ -176,7 +176,7 @@ const getActualData$1 = event => {
176
176
  };
177
177
 
178
178
  // @ts-ignore
179
- const wrap$8 = messagePort => {
179
+ const wrap$9 = messagePort => {
180
180
  return {
181
181
  messagePort,
182
182
  // @ts-ignore
@@ -213,8 +213,8 @@ const wrap$8 = messagePort => {
213
213
 
214
214
  const IpcChildWithElectronMessagePort = {
215
215
  __proto__: null,
216
- listen: listen$5,
217
- wrap: wrap$8
216
+ listen: listen$6,
217
+ wrap: wrap$9
218
218
  };
219
219
 
220
220
  // @ts-ignore
@@ -232,7 +232,7 @@ const getUtilityProcessPortData = event => {
232
232
  };
233
233
  };
234
234
 
235
- const listen$4 = () => {
235
+ const listen$5 = () => {
236
236
  // @ts-ignore
237
237
  const {
238
238
  parentPort
@@ -249,7 +249,7 @@ const signal$2 = parentPort => {
249
249
  };
250
250
 
251
251
  // @ts-ignore
252
- const wrap$7 = parentPort => {
252
+ const wrap$8 = parentPort => {
253
253
  return {
254
254
  parentPort,
255
255
  // @ts-ignore
@@ -287,16 +287,16 @@ const wrap$7 = parentPort => {
287
287
 
288
288
  const IpcChildWithElectronUtilityProcess = {
289
289
  __proto__: null,
290
- listen: listen$4,
290
+ listen: listen$5,
291
291
  signal: signal$2,
292
- wrap: wrap$7
292
+ wrap: wrap$8
293
293
  };
294
294
 
295
295
  const getData = event => {
296
296
  return event.data;
297
297
  };
298
298
 
299
- const listen$3 = () => {
299
+ const listen$4 = () => {
300
300
  // @ts-ignore
301
301
  if (typeof WorkerGlobalScope === 'undefined') {
302
302
  throw new TypeError('module is not in web worker scope');
@@ -308,7 +308,7 @@ const listen$3 = () => {
308
308
  const signal$1 = global => {
309
309
  global.postMessage('ready');
310
310
  };
311
- const wrap$6 = global => {
311
+ const wrap$7 = global => {
312
312
  return {
313
313
  global,
314
314
  /**
@@ -341,9 +341,9 @@ const wrap$6 = global => {
341
341
 
342
342
  const IpcChildWithModuleWorker = {
343
343
  __proto__: null,
344
- listen: listen$3,
344
+ listen: listen$4,
345
345
  signal: signal$1,
346
- wrap: wrap$6
346
+ wrap: wrap$7
347
347
  };
348
348
 
349
349
  const withResolvers = () => {
@@ -375,9 +375,9 @@ const waitForFirstMessage = async port => {
375
375
  return event.data;
376
376
  };
377
377
 
378
- const listen$2 = async () => {
379
- const parentIpcRaw = listen$3();
380
- const parentIpc = wrap$6(parentIpcRaw);
378
+ const listen$3 = async () => {
379
+ const parentIpcRaw = listen$4();
380
+ const parentIpc = wrap$7(parentIpcRaw);
381
381
  const firstMessage = await waitForFirstMessage(parentIpc);
382
382
  if (firstMessage.method !== 'initialize') {
383
383
  throw new IpcError('unexpected first message');
@@ -389,7 +389,7 @@ const listen$2 = async () => {
389
389
  }
390
390
  return globalThis;
391
391
  };
392
- const wrap$5 = port => {
392
+ const wrap$6 = port => {
393
393
  return {
394
394
  port,
395
395
  /**
@@ -426,11 +426,11 @@ const wrap$5 = port => {
426
426
 
427
427
  const IpcChildWithModuleWorkerAndMessagePort = {
428
428
  __proto__: null,
429
- listen: listen$2,
430
- wrap: wrap$5
429
+ listen: listen$3,
430
+ wrap: wrap$6
431
431
  };
432
432
 
433
- const listen$1 = async () => {
433
+ const listen$2 = async () => {
434
434
  if (!process.send) {
435
435
  throw new Error('process.send must be defined');
436
436
  }
@@ -454,7 +454,7 @@ const getActualData = (message, handle) => {
454
454
  };
455
455
 
456
456
  // @ts-ignore
457
- const wrap$4 = process => {
457
+ const wrap$5 = process => {
458
458
  return {
459
459
  process,
460
460
  // @ts-ignore
@@ -486,8 +486,47 @@ const wrap$4 = process => {
486
486
 
487
487
  const IpcChildWithNodeForkedProcess = {
488
488
  __proto__: null,
489
- listen: listen$1,
489
+ listen: listen$2,
490
490
  signal,
491
+ wrap: wrap$5
492
+ };
493
+
494
+ const isMessagePort = value => {
495
+ return value && value instanceof MessagePort;
496
+ };
497
+
498
+ const listen$1 = async ({
499
+ messagePort
500
+ }) => {
501
+ if (!isMessagePort(messagePort)) {
502
+ throw new IpcError(`port must be of type MessagePort`);
503
+ }
504
+ return messagePort;
505
+ };
506
+ const wrap$4 = port => {
507
+ return {
508
+ port,
509
+ on(event, listener) {
510
+ this.port.on(event, listener);
511
+ },
512
+ off(event, listener) {
513
+ this.port.off(event, listener);
514
+ },
515
+ send(message) {
516
+ this.port.postMessage(message);
517
+ },
518
+ dispose() {
519
+ this.port.close();
520
+ },
521
+ start() {
522
+ this.port.start();
523
+ }
524
+ };
525
+ };
526
+
527
+ const IpcChildWithNodeMessagePort = {
528
+ __proto__: null,
529
+ listen: listen$1,
491
530
  wrap: wrap$4
492
531
  };
493
532
 
@@ -1037,4 +1076,4 @@ const IpcParentWithNodeWorker = {
1037
1076
  wrap
1038
1077
  };
1039
1078
 
1040
- export { IpcChildWithElectronMessagePort, IpcChildWithElectronUtilityProcess, IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort, IpcChildWithNodeForkedProcess, IpcChildWithWebSocket, IpcParentWithElectronUtilityProcess, IpcParentWithNodeForkedProcess, IpcParentWithNodeWorker };
1079
+ export { IpcChildWithElectronMessagePort, IpcChildWithElectronUtilityProcess, IpcChildWithModuleWorker, IpcChildWithModuleWorkerAndMessagePort, IpcChildWithNodeForkedProcess, IpcChildWithNodeMessagePort, IpcChildWithWebSocket, IpcParentWithElectronUtilityProcess, IpcParentWithNodeForkedProcess, IpcParentWithNodeWorker };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/ipc",
3
- "version": "3.2.0",
3
+ "version": "3.4.0",
4
4
  "description": "Inter Process Communication for Lvce Editor",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -21,5 +21,6 @@
21
21
  "engines": {
22
22
  "node": ">=18"
23
23
  },
24
- "types": "dist/index.d.ts"
24
+ "types": "dist/index.d.ts",
25
+ "browser": "dist/browser.js"
25
26
  }