@lvce-editor/ipc 11.7.0 → 12.0.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/index.js +194 -51
- package/package.json +1 -1
- package/dist/browser.js +0 -867
- package/dist/electron.js +0 -902
package/dist/electron.js
DELETED
|
@@ -1,902 +0,0 @@
|
|
|
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 string = value => {
|
|
30
|
-
const type = getType(value);
|
|
31
|
-
if (type !== 'string') {
|
|
32
|
-
throw new AssertionError('expected value to be of type string');
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
const Exit = 1;
|
|
37
|
-
const Error$1 = 2;
|
|
38
|
-
const Message = 3;
|
|
39
|
-
|
|
40
|
-
const walkValue = (value, transferrables, isTransferrable) => {
|
|
41
|
-
if (!value) {
|
|
42
|
-
return;
|
|
43
|
-
}
|
|
44
|
-
if (isTransferrable(value)) {
|
|
45
|
-
transferrables.push(value);
|
|
46
|
-
return;
|
|
47
|
-
}
|
|
48
|
-
if (Array.isArray(value)) {
|
|
49
|
-
for (const item of value) {
|
|
50
|
-
walkValue(item, transferrables, isTransferrable);
|
|
51
|
-
}
|
|
52
|
-
return;
|
|
53
|
-
}
|
|
54
|
-
if (typeof value === 'object') {
|
|
55
|
-
for (const property of Object.values(value)) {
|
|
56
|
-
walkValue(property, transferrables, isTransferrable);
|
|
57
|
-
}
|
|
58
|
-
return;
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
|
|
62
|
-
const isMessagePort = value => {
|
|
63
|
-
return value && value instanceof MessagePort;
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
const isMessagePortMain = value => {
|
|
67
|
-
return value && value.constructor && value.constructor.name === 'MessagePortMain';
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
const isOffscreenCanvas = value => {
|
|
71
|
-
return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
const isInstanceOf = (value, constructorName) => {
|
|
75
|
-
return value?.constructor?.name === constructorName;
|
|
76
|
-
};
|
|
77
|
-
|
|
78
|
-
const isSocket = value => {
|
|
79
|
-
return isInstanceOf(value, 'Socket');
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
|
|
83
|
-
|
|
84
|
-
const isTransferrable = value => {
|
|
85
|
-
for (const fn of transferrables) {
|
|
86
|
-
if (fn(value)) {
|
|
87
|
-
return true;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
return false;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
const getTransferrables = value => {
|
|
94
|
-
const transferrables = [];
|
|
95
|
-
walkValue(value, transferrables, isTransferrable);
|
|
96
|
-
return transferrables;
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
const removeValues = (value, toRemove) => {
|
|
100
|
-
if (!value) {
|
|
101
|
-
return value;
|
|
102
|
-
}
|
|
103
|
-
if (Array.isArray(value)) {
|
|
104
|
-
const newItems = [];
|
|
105
|
-
for (const item of value) {
|
|
106
|
-
if (!toRemove.includes(item)) {
|
|
107
|
-
newItems.push(removeValues(item, toRemove));
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
return newItems;
|
|
111
|
-
}
|
|
112
|
-
if (typeof value === 'object') {
|
|
113
|
-
const newObject = Object.create(null);
|
|
114
|
-
for (const [key, property] of Object.entries(value)) {
|
|
115
|
-
if (!toRemove.includes(property)) {
|
|
116
|
-
newObject[key] = removeValues(property, toRemove);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
return newObject;
|
|
120
|
-
}
|
|
121
|
-
return value;
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
// workaround for electron not supporting transferrable objects
|
|
125
|
-
// as parameters. If the transferrable object is a parameter, in electron
|
|
126
|
-
// only an empty objected is received in the main process
|
|
127
|
-
const fixElectronParameters = value => {
|
|
128
|
-
const transfer = getTransferrables(value);
|
|
129
|
-
const newValue = removeValues(value, transfer);
|
|
130
|
-
return {
|
|
131
|
-
newValue,
|
|
132
|
-
transfer
|
|
133
|
-
};
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
const withResolvers = () => {
|
|
137
|
-
let _resolve;
|
|
138
|
-
const promise = new Promise(resolve => {
|
|
139
|
-
_resolve = resolve;
|
|
140
|
-
});
|
|
141
|
-
return {
|
|
142
|
-
resolve: _resolve,
|
|
143
|
-
promise
|
|
144
|
-
};
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
*
|
|
149
|
-
* @param {any} utilityProcess
|
|
150
|
-
* @returns
|
|
151
|
-
*/
|
|
152
|
-
// @ts-ignore
|
|
153
|
-
const getFirstUtilityProcessEvent = async utilityProcess => {
|
|
154
|
-
const {
|
|
155
|
-
resolve,
|
|
156
|
-
promise
|
|
157
|
-
} = withResolvers();
|
|
158
|
-
let stdout = '';
|
|
159
|
-
let stderr = '';
|
|
160
|
-
// @ts-ignore
|
|
161
|
-
const cleanup = value => {
|
|
162
|
-
// @ts-ignore
|
|
163
|
-
utilityProcess.stderr.off('data', handleStdErrData);
|
|
164
|
-
// @ts-ignore
|
|
165
|
-
utilityProcess.stdout.off('data', handleStdoutData);
|
|
166
|
-
utilityProcess.off('message', handleMessage);
|
|
167
|
-
utilityProcess.off('exit', handleExit);
|
|
168
|
-
// @ts-ignore
|
|
169
|
-
resolve(value);
|
|
170
|
-
};
|
|
171
|
-
// @ts-ignore
|
|
172
|
-
const handleStdErrData = data => {
|
|
173
|
-
stderr += data;
|
|
174
|
-
};
|
|
175
|
-
// @ts-ignore
|
|
176
|
-
const handleStdoutData = data => {
|
|
177
|
-
stdout += data;
|
|
178
|
-
};
|
|
179
|
-
// @ts-ignore
|
|
180
|
-
const handleMessage = event => {
|
|
181
|
-
cleanup({
|
|
182
|
-
type: Message,
|
|
183
|
-
event,
|
|
184
|
-
stdout,
|
|
185
|
-
stderr
|
|
186
|
-
});
|
|
187
|
-
};
|
|
188
|
-
// @ts-ignore
|
|
189
|
-
const handleExit = event => {
|
|
190
|
-
cleanup({
|
|
191
|
-
type: Exit,
|
|
192
|
-
event,
|
|
193
|
-
stdout,
|
|
194
|
-
stderr
|
|
195
|
-
});
|
|
196
|
-
};
|
|
197
|
-
// @ts-ignore
|
|
198
|
-
utilityProcess.stderr.on('data', handleStdErrData);
|
|
199
|
-
// @ts-ignore
|
|
200
|
-
utilityProcess.stdout.on('data', handleStdoutData);
|
|
201
|
-
utilityProcess.on('message', handleMessage);
|
|
202
|
-
utilityProcess.on('exit', handleExit);
|
|
203
|
-
// @ts-ignore
|
|
204
|
-
const {
|
|
205
|
-
type,
|
|
206
|
-
event
|
|
207
|
-
} = await promise;
|
|
208
|
-
return {
|
|
209
|
-
type,
|
|
210
|
-
event,
|
|
211
|
-
stdout,
|
|
212
|
-
stderr
|
|
213
|
-
};
|
|
214
|
-
};
|
|
215
|
-
|
|
216
|
-
const attachEvents = that => {
|
|
217
|
-
const handleMessage = (...args) => {
|
|
218
|
-
const data = that.getData(...args);
|
|
219
|
-
that.dispatchEvent(new MessageEvent('message', {
|
|
220
|
-
data
|
|
221
|
-
}));
|
|
222
|
-
};
|
|
223
|
-
that.onMessage(handleMessage);
|
|
224
|
-
const handleClose = event => {
|
|
225
|
-
that.dispatchEvent(new Event('close'));
|
|
226
|
-
};
|
|
227
|
-
that.onClose(handleClose);
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
class Ipc extends EventTarget {
|
|
231
|
-
constructor(rawIpc) {
|
|
232
|
-
super();
|
|
233
|
-
this._rawIpc = rawIpc;
|
|
234
|
-
attachEvents(this);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
|
|
239
|
-
const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
|
|
240
|
-
const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
|
|
241
|
-
|
|
242
|
-
const NewLine$1 = '\n';
|
|
243
|
-
|
|
244
|
-
const joinLines = lines => {
|
|
245
|
-
return lines.join(NewLine$1);
|
|
246
|
-
};
|
|
247
|
-
|
|
248
|
-
const splitLines = lines => {
|
|
249
|
-
return lines.split(NewLine$1);
|
|
250
|
-
};
|
|
251
|
-
|
|
252
|
-
const isModuleNotFoundMessage = line => {
|
|
253
|
-
return line.includes('[ERR_MODULE_NOT_FOUND]');
|
|
254
|
-
};
|
|
255
|
-
const getModuleNotFoundError = stderr => {
|
|
256
|
-
const lines = splitLines(stderr);
|
|
257
|
-
const messageIndex = lines.findIndex(isModuleNotFoundMessage);
|
|
258
|
-
const message = lines[messageIndex];
|
|
259
|
-
return {
|
|
260
|
-
message,
|
|
261
|
-
code: ERR_MODULE_NOT_FOUND
|
|
262
|
-
};
|
|
263
|
-
};
|
|
264
|
-
|
|
265
|
-
const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
|
|
266
|
-
const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
|
|
267
|
-
const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
|
|
268
|
-
const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
|
|
269
|
-
const RE_AT = /^\s+at/;
|
|
270
|
-
const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
|
|
271
|
-
const isUnhelpfulNativeModuleError = stderr => {
|
|
272
|
-
return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
|
|
273
|
-
};
|
|
274
|
-
const isMessageCodeBlockStartIndex = line => {
|
|
275
|
-
return RE_MESSAGE_CODE_BLOCK_START.test(line);
|
|
276
|
-
};
|
|
277
|
-
const isMessageCodeBlockEndIndex = line => {
|
|
278
|
-
return RE_MESSAGE_CODE_BLOCK_END.test(line);
|
|
279
|
-
};
|
|
280
|
-
const getMessageCodeBlock = stderr => {
|
|
281
|
-
const lines = splitLines(stderr);
|
|
282
|
-
const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
|
|
283
|
-
const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
|
|
284
|
-
const relevantLines = lines.slice(startIndex, endIndex);
|
|
285
|
-
const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
|
|
286
|
-
return relevantMessage;
|
|
287
|
-
};
|
|
288
|
-
const getNativeModuleErrorMessage = stderr => {
|
|
289
|
-
const message = getMessageCodeBlock(stderr);
|
|
290
|
-
return {
|
|
291
|
-
message: `Incompatible native node module: ${message}`,
|
|
292
|
-
code: E_INCOMPATIBLE_NATIVE_MODULE
|
|
293
|
-
};
|
|
294
|
-
};
|
|
295
|
-
const isModulesSyntaxError = stderr => {
|
|
296
|
-
if (!stderr) {
|
|
297
|
-
return false;
|
|
298
|
-
}
|
|
299
|
-
return stderr.includes('SyntaxError: Cannot use import statement outside a module');
|
|
300
|
-
};
|
|
301
|
-
const getModuleSyntaxError = () => {
|
|
302
|
-
return {
|
|
303
|
-
message: `ES Modules are not supported in electron`,
|
|
304
|
-
code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
|
|
305
|
-
};
|
|
306
|
-
};
|
|
307
|
-
const isModuleNotFoundError = stderr => {
|
|
308
|
-
if (!stderr) {
|
|
309
|
-
return false;
|
|
310
|
-
}
|
|
311
|
-
return stderr.includes('ERR_MODULE_NOT_FOUND');
|
|
312
|
-
};
|
|
313
|
-
const isNormalStackLine = line => {
|
|
314
|
-
return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
|
|
315
|
-
};
|
|
316
|
-
const getDetails = lines => {
|
|
317
|
-
const index = lines.findIndex(isNormalStackLine);
|
|
318
|
-
if (index === -1) {
|
|
319
|
-
return {
|
|
320
|
-
actualMessage: joinLines(lines),
|
|
321
|
-
rest: []
|
|
322
|
-
};
|
|
323
|
-
}
|
|
324
|
-
let lastIndex = index - 1;
|
|
325
|
-
while (++lastIndex < lines.length) {
|
|
326
|
-
if (!isNormalStackLine(lines[lastIndex])) {
|
|
327
|
-
break;
|
|
328
|
-
}
|
|
329
|
-
}
|
|
330
|
-
return {
|
|
331
|
-
actualMessage: lines[index - 1],
|
|
332
|
-
rest: lines.slice(index, lastIndex)
|
|
333
|
-
};
|
|
334
|
-
};
|
|
335
|
-
const getHelpfulChildProcessError = (stdout, stderr) => {
|
|
336
|
-
if (isUnhelpfulNativeModuleError(stderr)) {
|
|
337
|
-
return getNativeModuleErrorMessage(stderr);
|
|
338
|
-
}
|
|
339
|
-
if (isModulesSyntaxError(stderr)) {
|
|
340
|
-
return getModuleSyntaxError();
|
|
341
|
-
}
|
|
342
|
-
if (isModuleNotFoundError(stderr)) {
|
|
343
|
-
return getModuleNotFoundError(stderr);
|
|
344
|
-
}
|
|
345
|
-
const lines = splitLines(stderr);
|
|
346
|
-
const {
|
|
347
|
-
actualMessage,
|
|
348
|
-
rest
|
|
349
|
-
} = getDetails(lines);
|
|
350
|
-
return {
|
|
351
|
-
message: `${actualMessage}`,
|
|
352
|
-
code: '',
|
|
353
|
-
stack: rest
|
|
354
|
-
};
|
|
355
|
-
};
|
|
356
|
-
|
|
357
|
-
const normalizeLine = line => {
|
|
358
|
-
if (line.startsWith('Error: ')) {
|
|
359
|
-
return line.slice('Error: '.length);
|
|
360
|
-
}
|
|
361
|
-
if (line.startsWith('VError: ')) {
|
|
362
|
-
return line.slice('VError: '.length);
|
|
363
|
-
}
|
|
364
|
-
return line;
|
|
365
|
-
};
|
|
366
|
-
const getCombinedMessage = (error, message) => {
|
|
367
|
-
const stringifiedError = normalizeLine(`${error}`);
|
|
368
|
-
if (message) {
|
|
369
|
-
return `${message}: ${stringifiedError}`;
|
|
370
|
-
}
|
|
371
|
-
return stringifiedError;
|
|
372
|
-
};
|
|
373
|
-
const NewLine = '\n';
|
|
374
|
-
const getNewLineIndex = (string, startIndex = undefined) => {
|
|
375
|
-
return string.indexOf(NewLine, startIndex);
|
|
376
|
-
};
|
|
377
|
-
const mergeStacks = (parent, child) => {
|
|
378
|
-
if (!child) {
|
|
379
|
-
return parent;
|
|
380
|
-
}
|
|
381
|
-
const parentNewLineIndex = getNewLineIndex(parent);
|
|
382
|
-
const childNewLineIndex = getNewLineIndex(child);
|
|
383
|
-
if (childNewLineIndex === -1) {
|
|
384
|
-
return parent;
|
|
385
|
-
}
|
|
386
|
-
const parentFirstLine = parent.slice(0, parentNewLineIndex);
|
|
387
|
-
const childRest = child.slice(childNewLineIndex);
|
|
388
|
-
const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
|
|
389
|
-
if (parentFirstLine.includes(childFirstLine)) {
|
|
390
|
-
return parentFirstLine + childRest;
|
|
391
|
-
}
|
|
392
|
-
return child;
|
|
393
|
-
};
|
|
394
|
-
class VError extends Error {
|
|
395
|
-
constructor(error, message) {
|
|
396
|
-
const combinedMessage = getCombinedMessage(error, message);
|
|
397
|
-
super(combinedMessage);
|
|
398
|
-
this.name = 'VError';
|
|
399
|
-
if (error instanceof Error) {
|
|
400
|
-
this.stack = mergeStacks(this.stack, error.stack);
|
|
401
|
-
}
|
|
402
|
-
if (error.codeFrame) {
|
|
403
|
-
// @ts-ignore
|
|
404
|
-
this.codeFrame = error.codeFrame;
|
|
405
|
-
}
|
|
406
|
-
if (error.code) {
|
|
407
|
-
// @ts-ignore
|
|
408
|
-
this.code = error.code;
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
|
|
413
|
-
class IpcError extends VError {
|
|
414
|
-
// @ts-ignore
|
|
415
|
-
constructor(betterMessage, stdout = '', stderr = '') {
|
|
416
|
-
if (stdout || stderr) {
|
|
417
|
-
// @ts-ignore
|
|
418
|
-
const {
|
|
419
|
-
message,
|
|
420
|
-
code,
|
|
421
|
-
stack
|
|
422
|
-
} = getHelpfulChildProcessError(stdout, stderr);
|
|
423
|
-
const cause = new Error(message);
|
|
424
|
-
// @ts-ignore
|
|
425
|
-
cause.code = code;
|
|
426
|
-
cause.stack = stack;
|
|
427
|
-
super(cause, betterMessage);
|
|
428
|
-
} else {
|
|
429
|
-
super(betterMessage);
|
|
430
|
-
}
|
|
431
|
-
// @ts-ignore
|
|
432
|
-
this.name = 'IpcError';
|
|
433
|
-
// @ts-ignore
|
|
434
|
-
this.stdout = stdout;
|
|
435
|
-
// @ts-ignore
|
|
436
|
-
this.stderr = stderr;
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
// @ts-ignore
|
|
441
|
-
const create$3 = async ({
|
|
442
|
-
path,
|
|
443
|
-
argv = [],
|
|
444
|
-
execArgv = [],
|
|
445
|
-
name,
|
|
446
|
-
env = process.env
|
|
447
|
-
}) => {
|
|
448
|
-
string(path);
|
|
449
|
-
const actualArgv = ['--ipc-type=electron-utility-process', ...argv];
|
|
450
|
-
const {
|
|
451
|
-
utilityProcess
|
|
452
|
-
} = await import('electron');
|
|
453
|
-
const childProcess = utilityProcess.fork(path, actualArgv, {
|
|
454
|
-
execArgv,
|
|
455
|
-
stdio: 'pipe',
|
|
456
|
-
serviceName: name,
|
|
457
|
-
env
|
|
458
|
-
});
|
|
459
|
-
const handleExit = () => {
|
|
460
|
-
// @ts-ignore
|
|
461
|
-
childProcess.stdout.unpipe(process.stdout);
|
|
462
|
-
// @ts-ignore
|
|
463
|
-
childProcess.stderr.unpipe(process.stderr);
|
|
464
|
-
};
|
|
465
|
-
childProcess.once('exit', handleExit);
|
|
466
|
-
// @ts-ignore
|
|
467
|
-
childProcess.stdout.pipe(process.stdout);
|
|
468
|
-
const {
|
|
469
|
-
type,
|
|
470
|
-
stdout,
|
|
471
|
-
stderr
|
|
472
|
-
} = await getFirstUtilityProcessEvent(childProcess);
|
|
473
|
-
if (type === Exit) {
|
|
474
|
-
throw new IpcError(`Utility process exited before ipc connection was established`, stdout, stderr);
|
|
475
|
-
}
|
|
476
|
-
// @ts-ignore
|
|
477
|
-
childProcess.stderr.pipe(process.stderr);
|
|
478
|
-
return childProcess;
|
|
479
|
-
};
|
|
480
|
-
class IpcParentWithElectronUtilityProcess extends Ipc {
|
|
481
|
-
getData(data) {
|
|
482
|
-
return data;
|
|
483
|
-
}
|
|
484
|
-
send(message) {
|
|
485
|
-
this._rawIpc.postMessage(message);
|
|
486
|
-
}
|
|
487
|
-
sendAndTransfer(message) {
|
|
488
|
-
const {
|
|
489
|
-
newValue,
|
|
490
|
-
transfer
|
|
491
|
-
} = fixElectronParameters(message);
|
|
492
|
-
this._rawIpc.postMessage(newValue, transfer);
|
|
493
|
-
}
|
|
494
|
-
dispose() {
|
|
495
|
-
this._rawIpc.kill();
|
|
496
|
-
}
|
|
497
|
-
onClose(callback) {
|
|
498
|
-
this._rawIpc.on('exit', callback);
|
|
499
|
-
}
|
|
500
|
-
onMessage(callback) {
|
|
501
|
-
this._rawIpc.on('message', callback);
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
const wrap$3 = process => {
|
|
505
|
-
return new IpcParentWithElectronUtilityProcess(process);
|
|
506
|
-
};
|
|
507
|
-
|
|
508
|
-
const IpcParentWithElectronUtilityProcess$1 = {
|
|
509
|
-
__proto__: null,
|
|
510
|
-
create: create$3,
|
|
511
|
-
wrap: wrap$3
|
|
512
|
-
};
|
|
513
|
-
|
|
514
|
-
class ChildProcessError extends Error {
|
|
515
|
-
// @ts-ignore
|
|
516
|
-
constructor(stderr) {
|
|
517
|
-
// @ts-ignore
|
|
518
|
-
const {
|
|
519
|
-
message,
|
|
520
|
-
code,
|
|
521
|
-
stack
|
|
522
|
-
} = getHelpfulChildProcessError('', stderr);
|
|
523
|
-
super(message || 'child process error');
|
|
524
|
-
this.name = 'ChildProcessError';
|
|
525
|
-
if (code) {
|
|
526
|
-
// @ts-ignore
|
|
527
|
-
this.code = code;
|
|
528
|
-
}
|
|
529
|
-
if (stack) {
|
|
530
|
-
// @ts-ignore
|
|
531
|
-
const lines = splitLines(this.stack);
|
|
532
|
-
const [firstLine, ...stackLines] = lines;
|
|
533
|
-
const newStackLines = [firstLine, ...stack, ...stackLines];
|
|
534
|
-
const newStack = joinLines(newStackLines);
|
|
535
|
-
this.stack = newStack;
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
}
|
|
539
|
-
|
|
540
|
-
// workaround for node not supporting transferrable objects
|
|
541
|
-
// as parameters. If the transferrable object is a parameter,
|
|
542
|
-
// it is received as a plain object is received in the receiving process
|
|
543
|
-
const fixNodeChildProcessParameters = value => {
|
|
544
|
-
const transfer = getTransferrables(value);
|
|
545
|
-
if (transfer.length === 0) {
|
|
546
|
-
throw new IpcError('no transferrables found');
|
|
547
|
-
}
|
|
548
|
-
const newValue = removeValues(value, transfer);
|
|
549
|
-
return {
|
|
550
|
-
newValue,
|
|
551
|
-
transfer: transfer[0]
|
|
552
|
-
};
|
|
553
|
-
};
|
|
554
|
-
|
|
555
|
-
// @ts-ignore
|
|
556
|
-
const getFirstNodeChildProcessEvent = async childProcess => {
|
|
557
|
-
// @ts-ignore
|
|
558
|
-
const {
|
|
559
|
-
type,
|
|
560
|
-
event,
|
|
561
|
-
stdout,
|
|
562
|
-
stderr
|
|
563
|
-
} = await new Promise((resolve, reject) => {
|
|
564
|
-
let stderr = '';
|
|
565
|
-
let stdout = '';
|
|
566
|
-
// @ts-ignore
|
|
567
|
-
const cleanup = value => {
|
|
568
|
-
if (childProcess.stdout && childProcess.stderr) {
|
|
569
|
-
childProcess.stderr.off('data', handleStdErrData);
|
|
570
|
-
childProcess.stdout.off('data', handleStdoutData);
|
|
571
|
-
}
|
|
572
|
-
childProcess.off('message', handleMessage);
|
|
573
|
-
childProcess.off('exit', handleExit);
|
|
574
|
-
childProcess.off('error', handleError);
|
|
575
|
-
resolve(value);
|
|
576
|
-
};
|
|
577
|
-
// @ts-ignore
|
|
578
|
-
const handleStdErrData = data => {
|
|
579
|
-
stderr += data;
|
|
580
|
-
};
|
|
581
|
-
// @ts-ignore
|
|
582
|
-
const handleStdoutData = data => {
|
|
583
|
-
stdout += data;
|
|
584
|
-
};
|
|
585
|
-
// @ts-ignore
|
|
586
|
-
const handleMessage = event => {
|
|
587
|
-
cleanup({
|
|
588
|
-
type: Message,
|
|
589
|
-
event,
|
|
590
|
-
stdout,
|
|
591
|
-
stderr
|
|
592
|
-
});
|
|
593
|
-
};
|
|
594
|
-
// @ts-ignore
|
|
595
|
-
const handleExit = event => {
|
|
596
|
-
cleanup({
|
|
597
|
-
type: Exit,
|
|
598
|
-
event,
|
|
599
|
-
stdout,
|
|
600
|
-
stderr
|
|
601
|
-
});
|
|
602
|
-
};
|
|
603
|
-
// @ts-ignore
|
|
604
|
-
const handleError = event => {
|
|
605
|
-
cleanup({
|
|
606
|
-
type: Error$1,
|
|
607
|
-
event,
|
|
608
|
-
stdout,
|
|
609
|
-
stderr
|
|
610
|
-
});
|
|
611
|
-
};
|
|
612
|
-
if (childProcess.stdout && childProcess.stderr) {
|
|
613
|
-
childProcess.stderr.on('data', handleStdErrData);
|
|
614
|
-
childProcess.stdout.on('data', handleStdoutData);
|
|
615
|
-
}
|
|
616
|
-
childProcess.on('message', handleMessage);
|
|
617
|
-
childProcess.on('exit', handleExit);
|
|
618
|
-
childProcess.on('error', handleError);
|
|
619
|
-
});
|
|
620
|
-
return {
|
|
621
|
-
type,
|
|
622
|
-
event,
|
|
623
|
-
stdout,
|
|
624
|
-
stderr
|
|
625
|
-
};
|
|
626
|
-
};
|
|
627
|
-
|
|
628
|
-
// @ts-ignore
|
|
629
|
-
const create$2 = async ({
|
|
630
|
-
path,
|
|
631
|
-
argv = [],
|
|
632
|
-
env,
|
|
633
|
-
execArgv = [],
|
|
634
|
-
stdio = 'inherit',
|
|
635
|
-
name = 'child process'
|
|
636
|
-
}) => {
|
|
637
|
-
try {
|
|
638
|
-
string(path);
|
|
639
|
-
const actualArgv = ['--ipc-type=node-forked-process', ...argv];
|
|
640
|
-
const {
|
|
641
|
-
fork
|
|
642
|
-
} = await import('node:child_process');
|
|
643
|
-
const childProcess = fork(path, actualArgv, {
|
|
644
|
-
env,
|
|
645
|
-
execArgv,
|
|
646
|
-
stdio: 'pipe'
|
|
647
|
-
});
|
|
648
|
-
const {
|
|
649
|
-
type,
|
|
650
|
-
event,
|
|
651
|
-
stderr
|
|
652
|
-
} = await getFirstNodeChildProcessEvent(childProcess);
|
|
653
|
-
if (type === Exit) {
|
|
654
|
-
throw new ChildProcessError(stderr);
|
|
655
|
-
}
|
|
656
|
-
if (type === Error$1) {
|
|
657
|
-
throw new Error(`child process had an error ${event}`);
|
|
658
|
-
}
|
|
659
|
-
if (stdio === 'inherit' && childProcess.stdout && childProcess.stderr) {
|
|
660
|
-
childProcess.stdout.pipe(process.stdout);
|
|
661
|
-
childProcess.stderr.pipe(process.stderr);
|
|
662
|
-
}
|
|
663
|
-
return childProcess;
|
|
664
|
-
} catch (error) {
|
|
665
|
-
throw new VError(error, `Failed to launch ${name}`);
|
|
666
|
-
}
|
|
667
|
-
};
|
|
668
|
-
class IpcParentWithNodeForkedProcess extends Ipc {
|
|
669
|
-
constructor(childProcess) {
|
|
670
|
-
super(childProcess);
|
|
671
|
-
this.pid = childProcess.pid;
|
|
672
|
-
}
|
|
673
|
-
getData(message) {
|
|
674
|
-
return message;
|
|
675
|
-
}
|
|
676
|
-
send(message) {
|
|
677
|
-
this._rawIpc.send(message);
|
|
678
|
-
}
|
|
679
|
-
sendAndTransfer(message) {
|
|
680
|
-
const {
|
|
681
|
-
newValue,
|
|
682
|
-
transfer
|
|
683
|
-
} = fixNodeChildProcessParameters(message);
|
|
684
|
-
this._rawIpc.send(newValue, transfer);
|
|
685
|
-
}
|
|
686
|
-
dispose() {
|
|
687
|
-
this._rawIpc.kill();
|
|
688
|
-
}
|
|
689
|
-
onClose(callback) {
|
|
690
|
-
this._rawIpc.on('close', callback);
|
|
691
|
-
}
|
|
692
|
-
onMessage(callback) {
|
|
693
|
-
this._rawIpc.on('message', callback);
|
|
694
|
-
}
|
|
695
|
-
}
|
|
696
|
-
const wrap$2 = childProcess => {
|
|
697
|
-
return new IpcParentWithNodeForkedProcess(childProcess);
|
|
698
|
-
};
|
|
699
|
-
|
|
700
|
-
const IpcParentWithNodeForkedProcess$1 = {
|
|
701
|
-
__proto__: null,
|
|
702
|
-
create: create$2,
|
|
703
|
-
wrap: wrap$2
|
|
704
|
-
};
|
|
705
|
-
|
|
706
|
-
const fixNodeWorkerParameters = value => {
|
|
707
|
-
const transfer = getTransferrables(value);
|
|
708
|
-
if (transfer.length === 0) {
|
|
709
|
-
throw new IpcError('no transferrables found');
|
|
710
|
-
}
|
|
711
|
-
return {
|
|
712
|
-
newValue: value,
|
|
713
|
-
transfer: transfer
|
|
714
|
-
};
|
|
715
|
-
};
|
|
716
|
-
|
|
717
|
-
const addListener = (emitter, type, callback) => {
|
|
718
|
-
if ('addEventListener' in emitter) {
|
|
719
|
-
emitter.addEventListener(type, callback);
|
|
720
|
-
} else {
|
|
721
|
-
emitter.on(type, callback);
|
|
722
|
-
}
|
|
723
|
-
};
|
|
724
|
-
const removeListener = (emitter, type, callback) => {
|
|
725
|
-
if ('removeEventListener' in emitter) {
|
|
726
|
-
emitter.removeEventListener(type, callback);
|
|
727
|
-
} else {
|
|
728
|
-
emitter.off(type, callback);
|
|
729
|
-
}
|
|
730
|
-
};
|
|
731
|
-
const getFirstEvent = (eventEmitter, eventMap) => {
|
|
732
|
-
const {
|
|
733
|
-
resolve,
|
|
734
|
-
promise
|
|
735
|
-
} = withResolvers();
|
|
736
|
-
const listenerMap = Object.create(null);
|
|
737
|
-
const cleanup = value => {
|
|
738
|
-
for (const event of Object.keys(eventMap)) {
|
|
739
|
-
removeListener(eventEmitter, event, listenerMap[event]);
|
|
740
|
-
}
|
|
741
|
-
resolve(value);
|
|
742
|
-
};
|
|
743
|
-
for (const [event, type] of Object.entries(eventMap)) {
|
|
744
|
-
const listener = event => {
|
|
745
|
-
cleanup({
|
|
746
|
-
type,
|
|
747
|
-
event
|
|
748
|
-
});
|
|
749
|
-
};
|
|
750
|
-
addListener(eventEmitter, event, listener);
|
|
751
|
-
listenerMap[event] = listener;
|
|
752
|
-
}
|
|
753
|
-
return promise;
|
|
754
|
-
};
|
|
755
|
-
|
|
756
|
-
const getFirstNodeWorkerEvent = worker => {
|
|
757
|
-
return getFirstEvent(worker, {
|
|
758
|
-
message: Message,
|
|
759
|
-
exit: Exit,
|
|
760
|
-
error: Error$1
|
|
761
|
-
});
|
|
762
|
-
};
|
|
763
|
-
|
|
764
|
-
const readyMessage = 'ready';
|
|
765
|
-
|
|
766
|
-
// @ts-ignore
|
|
767
|
-
const create$1 = async ({
|
|
768
|
-
path,
|
|
769
|
-
argv = [],
|
|
770
|
-
env = process.env,
|
|
771
|
-
execArgv = []
|
|
772
|
-
}) => {
|
|
773
|
-
// @ts-ignore
|
|
774
|
-
string(path);
|
|
775
|
-
const actualArgv = ['--ipc-type=node-worker', ...argv];
|
|
776
|
-
const actualEnv = {
|
|
777
|
-
...env,
|
|
778
|
-
ELECTRON_RUN_AS_NODE: '1'
|
|
779
|
-
};
|
|
780
|
-
const {
|
|
781
|
-
Worker
|
|
782
|
-
} = await import('node:worker_threads');
|
|
783
|
-
const worker = new Worker(path, {
|
|
784
|
-
argv: actualArgv,
|
|
785
|
-
env: actualEnv,
|
|
786
|
-
execArgv
|
|
787
|
-
});
|
|
788
|
-
// @ts-ignore
|
|
789
|
-
const {
|
|
790
|
-
type,
|
|
791
|
-
event
|
|
792
|
-
} = await getFirstNodeWorkerEvent(worker);
|
|
793
|
-
if (type === Exit) {
|
|
794
|
-
throw new IpcError(`Worker exited before ipc connection was established`);
|
|
795
|
-
}
|
|
796
|
-
if (type === Error$1) {
|
|
797
|
-
throw new IpcError(`Worker threw an error before ipc connection was established: ${event}`);
|
|
798
|
-
}
|
|
799
|
-
if (event !== readyMessage) {
|
|
800
|
-
throw new IpcError('unexpected first message from worker');
|
|
801
|
-
}
|
|
802
|
-
return worker;
|
|
803
|
-
};
|
|
804
|
-
class IpcParentWithNodeWorker extends Ipc {
|
|
805
|
-
constructor(worker) {
|
|
806
|
-
super(worker);
|
|
807
|
-
}
|
|
808
|
-
getData(message) {
|
|
809
|
-
return message;
|
|
810
|
-
}
|
|
811
|
-
send(message) {
|
|
812
|
-
this._rawIpc.postMessage(message);
|
|
813
|
-
}
|
|
814
|
-
sendAndTransfer(message) {
|
|
815
|
-
const {
|
|
816
|
-
newValue,
|
|
817
|
-
transfer
|
|
818
|
-
} = fixNodeWorkerParameters(message);
|
|
819
|
-
this._rawIpc.postMessage(newValue, transfer);
|
|
820
|
-
}
|
|
821
|
-
dispose() {
|
|
822
|
-
this._rawIpc.terminate();
|
|
823
|
-
}
|
|
824
|
-
onClose(callback) {
|
|
825
|
-
this._rawIpc.on('exit', callback);
|
|
826
|
-
}
|
|
827
|
-
onMessage(callback) {
|
|
828
|
-
this._rawIpc.on('message', callback);
|
|
829
|
-
}
|
|
830
|
-
}
|
|
831
|
-
const wrap$1 = worker => {
|
|
832
|
-
return new IpcParentWithNodeWorker(worker);
|
|
833
|
-
};
|
|
834
|
-
|
|
835
|
-
const IpcParentWithNodeWorker$1 = {
|
|
836
|
-
__proto__: null,
|
|
837
|
-
create: create$1,
|
|
838
|
-
wrap: wrap$1
|
|
839
|
-
};
|
|
840
|
-
|
|
841
|
-
const getActualDataElectron = event => {
|
|
842
|
-
const {
|
|
843
|
-
data,
|
|
844
|
-
ports
|
|
845
|
-
} = event;
|
|
846
|
-
if (ports.length === 0) {
|
|
847
|
-
return data;
|
|
848
|
-
}
|
|
849
|
-
return {
|
|
850
|
-
...data,
|
|
851
|
-
params: [...ports, ...data.params]
|
|
852
|
-
};
|
|
853
|
-
};
|
|
854
|
-
|
|
855
|
-
const create = ({
|
|
856
|
-
messagePort
|
|
857
|
-
}) => {
|
|
858
|
-
if (!isMessagePortMain(messagePort)) {
|
|
859
|
-
throw new IpcError('port must be of type MessagePortMain');
|
|
860
|
-
}
|
|
861
|
-
return messagePort;
|
|
862
|
-
};
|
|
863
|
-
const signal = messagePort => {
|
|
864
|
-
messagePort.start();
|
|
865
|
-
};
|
|
866
|
-
class IpcParentWithElectronMessagePort extends Ipc {
|
|
867
|
-
constructor(port) {
|
|
868
|
-
super(port);
|
|
869
|
-
}
|
|
870
|
-
getData = getActualDataElectron;
|
|
871
|
-
send(message) {
|
|
872
|
-
this._rawIpc.postMessage(message);
|
|
873
|
-
}
|
|
874
|
-
sendAndTransfer(message) {
|
|
875
|
-
const {
|
|
876
|
-
newValue,
|
|
877
|
-
transfer
|
|
878
|
-
} = fixElectronParameters(message);
|
|
879
|
-
this._rawIpc.postMessage(newValue, transfer);
|
|
880
|
-
}
|
|
881
|
-
dispose() {
|
|
882
|
-
this._rawIpc.close();
|
|
883
|
-
}
|
|
884
|
-
onMessage(callback) {
|
|
885
|
-
this._rawIpc.on('message', callback);
|
|
886
|
-
}
|
|
887
|
-
onClose(callback) {
|
|
888
|
-
this._rawIpc.on('close', callback);
|
|
889
|
-
}
|
|
890
|
-
}
|
|
891
|
-
const wrap = messagePort => {
|
|
892
|
-
return new IpcParentWithElectronMessagePort(messagePort);
|
|
893
|
-
};
|
|
894
|
-
|
|
895
|
-
const IpcParentWithElectronMessagePort$1 = {
|
|
896
|
-
__proto__: null,
|
|
897
|
-
create,
|
|
898
|
-
signal,
|
|
899
|
-
wrap
|
|
900
|
-
};
|
|
901
|
-
|
|
902
|
-
export { IpcParentWithElectronMessagePort$1 as IpcParentWithElectronMessagePort, IpcParentWithElectronUtilityProcess$1 as IpcParentWithElectronUtilityProcess, IpcParentWithNodeForkedProcess$1 as IpcParentWithNodeForkedProcess, IpcParentWithNodeWorker$1 as IpcParentWithNodeWorker };
|