@lvce-editor/about-view 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.
- package/dist/aboutWorkerMain.js +650 -641
- package/package.json +1 -1
package/dist/aboutWorkerMain.js
CHANGED
@@ -1,747 +1,748 @@
|
|
1
|
-
const
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
}
|
8
|
-
|
9
|
-
const state$1 = {
|
10
|
-
callbacks: Object.create(null)
|
11
|
-
};
|
12
|
-
const set = (id, fn) => {
|
13
|
-
state$1.callbacks[id] = fn;
|
14
|
-
};
|
15
|
-
const get = id => {
|
16
|
-
return state$1.callbacks[id];
|
17
|
-
};
|
18
|
-
const remove = id => {
|
19
|
-
delete state$1.callbacks[id];
|
20
|
-
};
|
21
|
-
let id = 0;
|
22
|
-
const create$3 = () => {
|
23
|
-
return ++id;
|
24
|
-
};
|
25
|
-
const warn = (...args) => {
|
26
|
-
console.warn(...args);
|
27
|
-
};
|
28
|
-
const registerPromise = () => {
|
29
|
-
const id = create$3();
|
30
|
-
const {
|
31
|
-
resolve,
|
32
|
-
promise
|
33
|
-
} = Promise.withResolvers();
|
34
|
-
set(id, resolve);
|
35
|
-
return {
|
36
|
-
id,
|
37
|
-
promise
|
38
|
-
};
|
1
|
+
const normalizeLine = line => {
|
2
|
+
if (line.startsWith('Error: ')) {
|
3
|
+
return line.slice('Error: '.length);
|
4
|
+
}
|
5
|
+
if (line.startsWith('VError: ')) {
|
6
|
+
return line.slice('VError: '.length);
|
7
|
+
}
|
8
|
+
return line;
|
39
9
|
};
|
40
|
-
const
|
41
|
-
const
|
42
|
-
if (
|
43
|
-
|
44
|
-
warn(`callback ${id} may already be disposed`);
|
45
|
-
return;
|
10
|
+
const getCombinedMessage = (error, message) => {
|
11
|
+
const stringifiedError = normalizeLine(`${error}`);
|
12
|
+
if (message) {
|
13
|
+
return `${message}: ${stringifiedError}`;
|
46
14
|
}
|
47
|
-
|
48
|
-
remove(id);
|
15
|
+
return stringifiedError;
|
49
16
|
};
|
50
|
-
const
|
51
|
-
|
52
|
-
|
53
|
-
promise
|
54
|
-
} = registerPromise();
|
55
|
-
const message = {
|
56
|
-
jsonrpc: Two,
|
57
|
-
method,
|
58
|
-
params,
|
59
|
-
id
|
60
|
-
};
|
61
|
-
return {
|
62
|
-
message,
|
63
|
-
promise
|
64
|
-
};
|
17
|
+
const NewLine$3 = '\n';
|
18
|
+
const getNewLineIndex$1 = (string, startIndex = undefined) => {
|
19
|
+
return string.indexOf(NewLine$3, startIndex);
|
65
20
|
};
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
this.name = 'JsonRpcError';
|
21
|
+
const mergeStacks = (parent, child) => {
|
22
|
+
if (!child) {
|
23
|
+
return parent;
|
70
24
|
}
|
71
|
-
|
72
|
-
const
|
73
|
-
|
74
|
-
|
75
|
-
const SyntaxError$1 = 'SyntaxError';
|
76
|
-
const TypeError$1 = 'TypeError';
|
77
|
-
const getErrorConstructor = (message, type) => {
|
78
|
-
if (type) {
|
79
|
-
switch (type) {
|
80
|
-
case DomException:
|
81
|
-
return DOMException;
|
82
|
-
case TypeError$1:
|
83
|
-
return TypeError;
|
84
|
-
case SyntaxError$1:
|
85
|
-
return SyntaxError;
|
86
|
-
case ReferenceError$1:
|
87
|
-
return ReferenceError;
|
88
|
-
default:
|
89
|
-
return Error;
|
90
|
-
}
|
25
|
+
const parentNewLineIndex = getNewLineIndex$1(parent);
|
26
|
+
const childNewLineIndex = getNewLineIndex$1(child);
|
27
|
+
if (childNewLineIndex === -1) {
|
28
|
+
return parent;
|
91
29
|
}
|
92
|
-
|
93
|
-
|
30
|
+
const parentFirstLine = parent.slice(0, parentNewLineIndex);
|
31
|
+
const childRest = child.slice(childNewLineIndex);
|
32
|
+
const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
|
33
|
+
if (parentFirstLine.includes(childFirstLine)) {
|
34
|
+
return parentFirstLine + childRest;
|
94
35
|
}
|
95
|
-
|
96
|
-
|
36
|
+
return child;
|
37
|
+
};
|
38
|
+
class VError extends Error {
|
39
|
+
constructor(error, message) {
|
40
|
+
const combinedMessage = getCombinedMessage(error, message);
|
41
|
+
super(combinedMessage);
|
42
|
+
this.name = 'VError';
|
43
|
+
if (error instanceof Error) {
|
44
|
+
this.stack = mergeStacks(this.stack, error.stack);
|
45
|
+
}
|
46
|
+
if (error.codeFrame) {
|
47
|
+
// @ts-ignore
|
48
|
+
this.codeFrame = error.codeFrame;
|
49
|
+
}
|
50
|
+
if (error.code) {
|
51
|
+
// @ts-ignore
|
52
|
+
this.code = error.code;
|
53
|
+
}
|
97
54
|
}
|
98
|
-
|
99
|
-
|
55
|
+
}
|
56
|
+
|
57
|
+
const walkValue = (value, transferrables, isTransferrable) => {
|
58
|
+
if (!value) {
|
59
|
+
return;
|
100
60
|
}
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
const ErrorConstructor = getErrorConstructor(message, type);
|
105
|
-
if (ErrorConstructor === DOMException && name) {
|
106
|
-
return new ErrorConstructor(message, name);
|
61
|
+
if (isTransferrable(value)) {
|
62
|
+
transferrables.push(value);
|
63
|
+
return;
|
107
64
|
}
|
108
|
-
if (
|
109
|
-
const
|
110
|
-
|
111
|
-
error.name = name;
|
65
|
+
if (Array.isArray(value)) {
|
66
|
+
for (const item of value) {
|
67
|
+
walkValue(item, transferrables, isTransferrable);
|
112
68
|
}
|
113
|
-
return
|
69
|
+
return;
|
70
|
+
}
|
71
|
+
if (typeof value === 'object') {
|
72
|
+
for (const property of Object.values(value)) {
|
73
|
+
walkValue(property, transferrables, isTransferrable);
|
74
|
+
}
|
75
|
+
return;
|
114
76
|
}
|
115
|
-
return new ErrorConstructor(message);
|
116
77
|
};
|
117
|
-
const
|
118
|
-
return
|
78
|
+
const isMessagePort = value => {
|
79
|
+
return value && value instanceof MessagePort;
|
119
80
|
};
|
120
|
-
const
|
121
|
-
|
122
|
-
if (parentStack.startsWith(' at')) {
|
123
|
-
parentStack = error.message + NewLine$3 + parentStack;
|
124
|
-
}
|
125
|
-
return parentStack;
|
81
|
+
const isMessagePortMain = value => {
|
82
|
+
return value && value.constructor && value.constructor.name === 'MessagePortMain';
|
126
83
|
};
|
127
|
-
const
|
128
|
-
return
|
84
|
+
const isOffscreenCanvas = value => {
|
85
|
+
return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
|
129
86
|
};
|
130
|
-
const
|
131
|
-
|
132
|
-
const splitLines$1 = lines => {
|
133
|
-
return lines.split(NewLine$3);
|
87
|
+
const isInstanceOf = (value, constructorName) => {
|
88
|
+
return value?.constructor?.name === constructorName;
|
134
89
|
};
|
135
|
-
const
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
restoredError.stack = parentStack + NewLine$3 + currentStack;
|
144
|
-
return restoredError;
|
145
|
-
}
|
146
|
-
if (error && error.message) {
|
147
|
-
const restoredError = constructError(error.message, error.type, error.name);
|
148
|
-
if (error.data) {
|
149
|
-
if (error.data.stack && error.data.type && error.message) {
|
150
|
-
restoredError.stack = error.data.type + ': ' + error.message + NewLine$3 + error.data.stack + NewLine$3 + currentStack;
|
151
|
-
} else if (error.data.stack) {
|
152
|
-
restoredError.stack = error.data.stack;
|
153
|
-
}
|
154
|
-
if (error.data.codeFrame) {
|
155
|
-
// @ts-ignore
|
156
|
-
restoredError.codeFrame = error.data.codeFrame;
|
157
|
-
}
|
158
|
-
if (error.data.code) {
|
159
|
-
// @ts-ignore
|
160
|
-
restoredError.code = error.data.code;
|
161
|
-
}
|
162
|
-
if (error.data.type) {
|
163
|
-
// @ts-ignore
|
164
|
-
restoredError.name = error.data.type;
|
165
|
-
}
|
166
|
-
} else {
|
167
|
-
if (error.stack) {
|
168
|
-
const lowerStack = restoredError.stack || '';
|
169
|
-
// @ts-ignore
|
170
|
-
const indexNewLine = getNewLineIndex$1(lowerStack);
|
171
|
-
const parentStack = getParentStack(error);
|
172
|
-
// @ts-ignore
|
173
|
-
restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
|
174
|
-
}
|
175
|
-
if (error.codeFrame) {
|
176
|
-
// @ts-ignore
|
177
|
-
restoredError.codeFrame = error.codeFrame;
|
178
|
-
}
|
90
|
+
const isSocket = value => {
|
91
|
+
return isInstanceOf(value, 'Socket');
|
92
|
+
};
|
93
|
+
const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
|
94
|
+
const isTransferrable = value => {
|
95
|
+
for (const fn of transferrables) {
|
96
|
+
if (fn(value)) {
|
97
|
+
return true;
|
179
98
|
}
|
180
|
-
return restoredError;
|
181
99
|
}
|
182
|
-
|
183
|
-
return new Error(`JsonRpc Error: ${error}`);
|
184
|
-
}
|
185
|
-
return new Error(`JsonRpc Error: ${error}`);
|
100
|
+
return false;
|
186
101
|
};
|
187
|
-
const
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
}
|
192
|
-
if ('result' in responseMessage) {
|
193
|
-
return responseMessage.result;
|
194
|
-
}
|
195
|
-
throw new JsonRpcError('unexpected response message');
|
102
|
+
const getTransferrables = value => {
|
103
|
+
const transferrables = [];
|
104
|
+
walkValue(value, transferrables, isTransferrable);
|
105
|
+
return transferrables;
|
196
106
|
};
|
197
|
-
const
|
198
|
-
const
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
107
|
+
const attachEvents = that => {
|
108
|
+
const handleMessage = (...args) => {
|
109
|
+
const data = that.getData(...args);
|
110
|
+
that.dispatchEvent(new MessageEvent('message', {
|
111
|
+
data
|
112
|
+
}));
|
113
|
+
};
|
114
|
+
that.onMessage(handleMessage);
|
115
|
+
const handleClose = event => {
|
116
|
+
that.dispatchEvent(new Event('close'));
|
117
|
+
};
|
118
|
+
that.onClose(handleClose);
|
206
119
|
};
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
data: error.stack
|
213
|
-
};
|
120
|
+
class Ipc extends EventTarget {
|
121
|
+
constructor(rawIpc) {
|
122
|
+
super();
|
123
|
+
this._rawIpc = rawIpc;
|
124
|
+
attachEvents(this);
|
214
125
|
}
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
126
|
+
}
|
127
|
+
const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
|
128
|
+
const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
|
129
|
+
const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
|
130
|
+
const NewLine$2 = '\n';
|
131
|
+
const joinLines$2 = lines => {
|
132
|
+
return lines.join(NewLine$2);
|
133
|
+
};
|
134
|
+
const splitLines$1 = lines => {
|
135
|
+
return lines.split(NewLine$2);
|
136
|
+
};
|
137
|
+
const isModuleNotFoundMessage = line => {
|
138
|
+
return line.includes('[ERR_MODULE_NOT_FOUND]');
|
226
139
|
};
|
227
|
-
const
|
140
|
+
const getModuleNotFoundError = stderr => {
|
141
|
+
const lines = splitLines$1(stderr);
|
142
|
+
const messageIndex = lines.findIndex(isModuleNotFoundMessage);
|
143
|
+
const message = lines[messageIndex];
|
228
144
|
return {
|
229
|
-
|
230
|
-
|
231
|
-
error
|
145
|
+
message,
|
146
|
+
code: ERR_MODULE_NOT_FOUND
|
232
147
|
};
|
233
148
|
};
|
234
|
-
const
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
149
|
+
const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
|
150
|
+
const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
|
151
|
+
const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
|
152
|
+
const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
|
153
|
+
const RE_AT = /^\s+at/;
|
154
|
+
const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
|
155
|
+
const isUnhelpfulNativeModuleError = stderr => {
|
156
|
+
return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
|
239
157
|
};
|
240
|
-
const
|
158
|
+
const isMessageCodeBlockStartIndex = line => {
|
159
|
+
return RE_MESSAGE_CODE_BLOCK_START.test(line);
|
160
|
+
};
|
161
|
+
const isMessageCodeBlockEndIndex = line => {
|
162
|
+
return RE_MESSAGE_CODE_BLOCK_END.test(line);
|
163
|
+
};
|
164
|
+
const getMessageCodeBlock = stderr => {
|
165
|
+
const lines = splitLines$1(stderr);
|
166
|
+
const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
|
167
|
+
const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
|
168
|
+
const relevantLines = lines.slice(startIndex, endIndex);
|
169
|
+
const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
|
170
|
+
return relevantMessage;
|
171
|
+
};
|
172
|
+
const getNativeModuleErrorMessage = stderr => {
|
173
|
+
const message = getMessageCodeBlock(stderr);
|
241
174
|
return {
|
242
|
-
|
243
|
-
|
244
|
-
result: result ?? null
|
175
|
+
message: `Incompatible native node module: ${message}`,
|
176
|
+
code: E_INCOMPATIBLE_NATIVE_MODULE
|
245
177
|
};
|
246
178
|
};
|
247
|
-
const
|
248
|
-
|
249
|
-
|
250
|
-
};
|
251
|
-
const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
|
252
|
-
try {
|
253
|
-
const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
|
254
|
-
return getSuccessResponse(message, result);
|
255
|
-
} catch (error) {
|
256
|
-
return getErrorResponse(message, error, preparePrettyError, logError);
|
179
|
+
const isModulesSyntaxError = stderr => {
|
180
|
+
if (!stderr) {
|
181
|
+
return false;
|
257
182
|
}
|
183
|
+
return stderr.includes('SyntaxError: Cannot use import statement outside a module');
|
258
184
|
};
|
259
|
-
const
|
260
|
-
return
|
185
|
+
const getModuleSyntaxError = () => {
|
186
|
+
return {
|
187
|
+
message: `ES Modules are not supported in electron`,
|
188
|
+
code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
|
189
|
+
};
|
261
190
|
};
|
262
|
-
const
|
263
|
-
|
191
|
+
const isModuleNotFoundError = stderr => {
|
192
|
+
if (!stderr) {
|
193
|
+
return false;
|
194
|
+
}
|
195
|
+
return stderr.includes('ERR_MODULE_NOT_FOUND');
|
264
196
|
};
|
265
|
-
const
|
266
|
-
return
|
197
|
+
const isNormalStackLine = line => {
|
198
|
+
return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
|
267
199
|
};
|
268
|
-
const
|
269
|
-
|
270
|
-
|
271
|
-
const normalizeParams = args => {
|
272
|
-
if (args.length === 1) {
|
273
|
-
const options = args[0];
|
200
|
+
const getDetails = lines => {
|
201
|
+
const index = lines.findIndex(isNormalStackLine);
|
202
|
+
if (index === -1) {
|
274
203
|
return {
|
275
|
-
|
276
|
-
|
277
|
-
execute: options.execute,
|
278
|
-
resolve: options.resolve || defaultResolve,
|
279
|
-
preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
|
280
|
-
logError: options.logError || defaultLogError,
|
281
|
-
requiresSocket: options.requiresSocket || defaultRequiresSocket
|
204
|
+
actualMessage: joinLines$2(lines),
|
205
|
+
rest: []
|
282
206
|
};
|
283
207
|
}
|
208
|
+
let lastIndex = index - 1;
|
209
|
+
while (++lastIndex < lines.length) {
|
210
|
+
if (!isNormalStackLine(lines[lastIndex])) {
|
211
|
+
break;
|
212
|
+
}
|
213
|
+
}
|
284
214
|
return {
|
285
|
-
|
286
|
-
|
287
|
-
execute: args[2],
|
288
|
-
resolve: args[3],
|
289
|
-
preparePrettyError: args[4],
|
290
|
-
logError: args[5],
|
291
|
-
requiresSocket: args[6]
|
215
|
+
actualMessage: lines[index - 1],
|
216
|
+
rest: lines.slice(index, lastIndex)
|
292
217
|
};
|
293
218
|
};
|
294
|
-
const
|
295
|
-
|
296
|
-
|
297
|
-
message,
|
298
|
-
ipc,
|
299
|
-
execute,
|
300
|
-
resolve,
|
301
|
-
preparePrettyError,
|
302
|
-
logError,
|
303
|
-
requiresSocket
|
304
|
-
} = options;
|
305
|
-
if ('id' in message) {
|
306
|
-
if ('method' in message) {
|
307
|
-
const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
308
|
-
try {
|
309
|
-
ipc.send(response);
|
310
|
-
} catch (error) {
|
311
|
-
const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
|
312
|
-
ipc.send(errorResponse);
|
313
|
-
}
|
314
|
-
return;
|
315
|
-
}
|
316
|
-
resolve(message.id, message);
|
317
|
-
return;
|
219
|
+
const getHelpfulChildProcessError = (stdout, stderr) => {
|
220
|
+
if (isUnhelpfulNativeModuleError(stderr)) {
|
221
|
+
return getNativeModuleErrorMessage(stderr);
|
318
222
|
}
|
319
|
-
if (
|
320
|
-
|
321
|
-
return;
|
223
|
+
if (isModulesSyntaxError(stderr)) {
|
224
|
+
return getModuleSyntaxError();
|
322
225
|
}
|
323
|
-
|
324
|
-
|
325
|
-
const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
|
326
|
-
const {
|
327
|
-
message,
|
328
|
-
promise
|
329
|
-
} = create$2(method, params);
|
330
|
-
if (useSendAndTransfer && ipc.sendAndTransfer) {
|
331
|
-
ipc.sendAndTransfer(message);
|
332
|
-
} else {
|
333
|
-
ipc.send(message);
|
226
|
+
if (isModuleNotFoundError(stderr)) {
|
227
|
+
return getModuleNotFoundError(stderr);
|
334
228
|
}
|
335
|
-
const
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
}
|
342
|
-
|
343
|
-
|
229
|
+
const lines = splitLines$1(stderr);
|
230
|
+
const {
|
231
|
+
actualMessage,
|
232
|
+
rest
|
233
|
+
} = getDetails(lines);
|
234
|
+
return {
|
235
|
+
message: `${actualMessage}`,
|
236
|
+
code: '',
|
237
|
+
stack: rest
|
238
|
+
};
|
344
239
|
};
|
345
|
-
|
346
|
-
|
240
|
+
class IpcError extends VError {
|
241
|
+
// @ts-ignore
|
242
|
+
constructor(betterMessage, stdout = '', stderr = '') {
|
243
|
+
if (stdout || stderr) {
|
244
|
+
// @ts-ignore
|
245
|
+
const {
|
246
|
+
message,
|
247
|
+
code,
|
248
|
+
stack
|
249
|
+
} = getHelpfulChildProcessError(stdout, stderr);
|
250
|
+
const cause = new Error(message);
|
251
|
+
// @ts-ignore
|
252
|
+
cause.code = code;
|
253
|
+
cause.stack = stack;
|
254
|
+
super(cause, betterMessage);
|
255
|
+
} else {
|
256
|
+
super(betterMessage);
|
257
|
+
}
|
258
|
+
// @ts-ignore
|
259
|
+
this.name = 'IpcError';
|
260
|
+
// @ts-ignore
|
261
|
+
this.stdout = stdout;
|
262
|
+
// @ts-ignore
|
263
|
+
this.stderr = stderr;
|
264
|
+
}
|
265
|
+
}
|
266
|
+
const readyMessage = 'ready';
|
267
|
+
const getData$2 = event => {
|
268
|
+
return event.data;
|
347
269
|
};
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
|
270
|
+
const listen$6 = () => {
|
271
|
+
// @ts-ignore
|
272
|
+
if (typeof WorkerGlobalScope === 'undefined') {
|
273
|
+
throw new TypeError('module is not in web worker scope');
|
274
|
+
}
|
275
|
+
return globalThis;
|
352
276
|
};
|
353
|
-
const
|
354
|
-
|
277
|
+
const signal$6 = global => {
|
278
|
+
global.postMessage(readyMessage);
|
355
279
|
};
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
throw new Error(`command not found ${command}`);
|
280
|
+
class IpcChildWithModuleWorker extends Ipc {
|
281
|
+
getData(event) {
|
282
|
+
return getData$2(event);
|
360
283
|
}
|
361
|
-
|
284
|
+
send(message) {
|
285
|
+
// @ts-ignore
|
286
|
+
this._rawIpc.postMessage(message);
|
287
|
+
}
|
288
|
+
sendAndTransfer(message) {
|
289
|
+
const transfer = getTransferrables(message);
|
290
|
+
// @ts-ignore
|
291
|
+
this._rawIpc.postMessage(message, transfer);
|
292
|
+
}
|
293
|
+
dispose() {
|
294
|
+
// ignore
|
295
|
+
}
|
296
|
+
onClose(callback) {
|
297
|
+
// ignore
|
298
|
+
}
|
299
|
+
onMessage(callback) {
|
300
|
+
this._rawIpc.addEventListener('message', callback);
|
301
|
+
}
|
302
|
+
}
|
303
|
+
const wrap$d = global => {
|
304
|
+
return new IpcChildWithModuleWorker(global);
|
362
305
|
};
|
363
|
-
|
364
|
-
|
306
|
+
const withResolvers = () => {
|
307
|
+
let _resolve;
|
308
|
+
const promise = new Promise(resolve => {
|
309
|
+
_resolve = resolve;
|
310
|
+
});
|
311
|
+
return {
|
312
|
+
resolve: _resolve,
|
313
|
+
promise
|
314
|
+
};
|
315
|
+
};
|
316
|
+
const waitForFirstMessage = async port => {
|
317
|
+
const {
|
318
|
+
resolve,
|
319
|
+
promise
|
320
|
+
} = withResolvers();
|
321
|
+
port.addEventListener('message', resolve, {
|
322
|
+
once: true
|
323
|
+
});
|
324
|
+
const event = await promise;
|
325
|
+
// @ts-ignore
|
365
326
|
return event.data;
|
366
327
|
};
|
367
|
-
const
|
368
|
-
const
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
|
-
|
375
|
-
const
|
376
|
-
|
377
|
-
|
378
|
-
|
328
|
+
const listen$5 = async () => {
|
329
|
+
const parentIpcRaw = listen$6();
|
330
|
+
signal$6(parentIpcRaw);
|
331
|
+
const parentIpc = wrap$d(parentIpcRaw);
|
332
|
+
const firstMessage = await waitForFirstMessage(parentIpc);
|
333
|
+
if (firstMessage.method !== 'initialize') {
|
334
|
+
throw new IpcError('unexpected first message');
|
335
|
+
}
|
336
|
+
const type = firstMessage.params[0];
|
337
|
+
if (type === 'message-port') {
|
338
|
+
parentIpc.send({
|
339
|
+
jsonrpc: '2.0',
|
340
|
+
id: firstMessage.id,
|
341
|
+
result: null
|
342
|
+
});
|
343
|
+
parentIpc.dispose();
|
344
|
+
const port = firstMessage.params[1];
|
345
|
+
return port;
|
346
|
+
}
|
347
|
+
return globalThis;
|
379
348
|
};
|
380
|
-
class
|
381
|
-
constructor(
|
382
|
-
super();
|
383
|
-
this._rawIpc = rawIpc;
|
384
|
-
attachEvents(this);
|
349
|
+
class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
|
350
|
+
constructor(port) {
|
351
|
+
super(port);
|
385
352
|
}
|
386
|
-
|
387
|
-
|
388
|
-
const walkValue = (value, transferrables, isTransferrable) => {
|
389
|
-
if (!value) {
|
390
|
-
return;
|
353
|
+
getData(event) {
|
354
|
+
return getData$2(event);
|
391
355
|
}
|
392
|
-
|
393
|
-
|
394
|
-
return;
|
356
|
+
send(message) {
|
357
|
+
this._rawIpc.postMessage(message);
|
395
358
|
}
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
}
|
400
|
-
return;
|
359
|
+
sendAndTransfer(message) {
|
360
|
+
const transfer = getTransferrables(message);
|
361
|
+
this._rawIpc.postMessage(message, transfer);
|
401
362
|
}
|
402
|
-
|
403
|
-
|
404
|
-
|
363
|
+
dispose() {
|
364
|
+
if (this._rawIpc.close) {
|
365
|
+
this._rawIpc.close();
|
405
366
|
}
|
406
|
-
return;
|
407
367
|
}
|
368
|
+
onClose(callback) {
|
369
|
+
// ignore
|
370
|
+
}
|
371
|
+
onMessage(callback) {
|
372
|
+
this._rawIpc.addEventListener('message', callback);
|
373
|
+
this._rawIpc.start();
|
374
|
+
}
|
375
|
+
}
|
376
|
+
const wrap$c = port => {
|
377
|
+
return new IpcChildWithModuleWorkerAndMessagePort(port);
|
408
378
|
};
|
409
|
-
const
|
410
|
-
|
379
|
+
const IpcChildWithModuleWorkerAndMessagePort$1 = {
|
380
|
+
__proto__: null,
|
381
|
+
listen: listen$5,
|
382
|
+
wrap: wrap$c
|
411
383
|
};
|
412
|
-
|
413
|
-
|
384
|
+
|
385
|
+
const Two = '2.0';
|
386
|
+
const create$4 = (method, params) => {
|
387
|
+
return {
|
388
|
+
jsonrpc: Two,
|
389
|
+
method,
|
390
|
+
params
|
391
|
+
};
|
414
392
|
};
|
415
|
-
const
|
416
|
-
|
393
|
+
const state$1 = {
|
394
|
+
callbacks: Object.create(null)
|
417
395
|
};
|
418
|
-
const
|
419
|
-
|
396
|
+
const set = (id, fn) => {
|
397
|
+
state$1.callbacks[id] = fn;
|
420
398
|
};
|
421
|
-
const
|
422
|
-
return
|
399
|
+
const get = id => {
|
400
|
+
return state$1.callbacks[id];
|
423
401
|
};
|
424
|
-
const
|
425
|
-
|
426
|
-
for (const fn of transferrables) {
|
427
|
-
if (fn(value)) {
|
428
|
-
return true;
|
429
|
-
}
|
430
|
-
}
|
431
|
-
return false;
|
402
|
+
const remove = id => {
|
403
|
+
delete state$1.callbacks[id];
|
432
404
|
};
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
405
|
+
let id = 0;
|
406
|
+
const create$3 = () => {
|
407
|
+
return ++id;
|
408
|
+
};
|
409
|
+
const warn = (...args) => {
|
410
|
+
console.warn(...args);
|
411
|
+
};
|
412
|
+
const registerPromise = () => {
|
413
|
+
const id = create$3();
|
414
|
+
const {
|
415
|
+
resolve,
|
416
|
+
promise
|
417
|
+
} = Promise.withResolvers();
|
418
|
+
set(id, resolve);
|
419
|
+
return {
|
420
|
+
id,
|
421
|
+
promise
|
422
|
+
};
|
437
423
|
};
|
438
|
-
const
|
439
|
-
|
440
|
-
if (
|
441
|
-
|
424
|
+
const resolve = (id, response) => {
|
425
|
+
const fn = get(id);
|
426
|
+
if (!fn) {
|
427
|
+
console.log(response);
|
428
|
+
warn(`callback ${id} may already be disposed`);
|
429
|
+
return;
|
442
430
|
}
|
443
|
-
|
431
|
+
fn(response);
|
432
|
+
remove(id);
|
444
433
|
};
|
445
|
-
const
|
446
|
-
|
434
|
+
const create$2 = (method, params) => {
|
435
|
+
const {
|
436
|
+
id,
|
437
|
+
promise
|
438
|
+
} = registerPromise();
|
439
|
+
const message = {
|
440
|
+
jsonrpc: Two,
|
441
|
+
method,
|
442
|
+
params,
|
443
|
+
id
|
444
|
+
};
|
445
|
+
return {
|
446
|
+
message,
|
447
|
+
promise
|
448
|
+
};
|
447
449
|
};
|
448
|
-
class
|
449
|
-
|
450
|
-
|
450
|
+
class JsonRpcError extends Error {
|
451
|
+
constructor(message) {
|
452
|
+
super(message);
|
453
|
+
this.name = 'JsonRpcError';
|
451
454
|
}
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
+
}
|
456
|
+
const NewLine$1 = '\n';
|
457
|
+
const DomException = 'DOMException';
|
458
|
+
const ReferenceError$1 = 'ReferenceError';
|
459
|
+
const SyntaxError$1 = 'SyntaxError';
|
460
|
+
const TypeError$1 = 'TypeError';
|
461
|
+
const getErrorConstructor = (message, type) => {
|
462
|
+
if (type) {
|
463
|
+
switch (type) {
|
464
|
+
case DomException:
|
465
|
+
return DOMException;
|
466
|
+
case TypeError$1:
|
467
|
+
return TypeError;
|
468
|
+
case SyntaxError$1:
|
469
|
+
return SyntaxError;
|
470
|
+
case ReferenceError$1:
|
471
|
+
return ReferenceError;
|
472
|
+
default:
|
473
|
+
return Error;
|
474
|
+
}
|
455
475
|
}
|
456
|
-
|
457
|
-
|
458
|
-
// @ts-ignore
|
459
|
-
this._rawIpc.postMessage(message, transfer);
|
476
|
+
if (message.startsWith('TypeError: ')) {
|
477
|
+
return TypeError;
|
460
478
|
}
|
461
|
-
|
462
|
-
|
479
|
+
if (message.startsWith('SyntaxError: ')) {
|
480
|
+
return SyntaxError;
|
463
481
|
}
|
464
|
-
|
465
|
-
|
482
|
+
if (message.startsWith('ReferenceError: ')) {
|
483
|
+
return ReferenceError;
|
466
484
|
}
|
467
|
-
|
468
|
-
|
485
|
+
return Error;
|
486
|
+
};
|
487
|
+
const constructError = (message, type, name) => {
|
488
|
+
const ErrorConstructor = getErrorConstructor(message, type);
|
489
|
+
if (ErrorConstructor === DOMException && name) {
|
490
|
+
return new ErrorConstructor(message, name);
|
469
491
|
}
|
470
|
-
|
471
|
-
const
|
472
|
-
|
492
|
+
if (ErrorConstructor === Error) {
|
493
|
+
const error = new Error(message);
|
494
|
+
if (name && name !== 'VError') {
|
495
|
+
error.name = name;
|
496
|
+
}
|
497
|
+
return error;
|
498
|
+
}
|
499
|
+
return new ErrorConstructor(message);
|
500
|
+
};
|
501
|
+
const getNewLineIndex = (string, startIndex = undefined) => {
|
502
|
+
return string.indexOf(NewLine$1, startIndex);
|
503
|
+
};
|
504
|
+
const getParentStack = error => {
|
505
|
+
let parentStack = error.stack || error.data || error.message || '';
|
506
|
+
if (parentStack.startsWith(' at')) {
|
507
|
+
parentStack = error.message + NewLine$1 + parentStack;
|
508
|
+
}
|
509
|
+
return parentStack;
|
473
510
|
};
|
474
|
-
const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
|
475
|
-
const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
|
476
|
-
const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
|
477
|
-
const NewLine$1 = '\n';
|
478
511
|
const joinLines$1 = lines => {
|
479
512
|
return lines.join(NewLine$1);
|
480
513
|
};
|
514
|
+
const MethodNotFound = -32601;
|
515
|
+
const Custom = -32001;
|
481
516
|
const splitLines = lines => {
|
482
517
|
return lines.split(NewLine$1);
|
483
518
|
};
|
484
|
-
const
|
485
|
-
|
486
|
-
|
487
|
-
const getModuleNotFoundError = stderr => {
|
488
|
-
const lines = splitLines(stderr);
|
489
|
-
const messageIndex = lines.findIndex(isModuleNotFoundMessage);
|
490
|
-
const message = lines[messageIndex];
|
491
|
-
return {
|
492
|
-
message,
|
493
|
-
code: ERR_MODULE_NOT_FOUND
|
494
|
-
};
|
495
|
-
};
|
496
|
-
const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
|
497
|
-
const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
|
498
|
-
const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
|
499
|
-
const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
|
500
|
-
const RE_AT = /^\s+at/;
|
501
|
-
const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
|
502
|
-
const isUnhelpfulNativeModuleError = stderr => {
|
503
|
-
return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
|
504
|
-
};
|
505
|
-
const isMessageCodeBlockStartIndex = line => {
|
506
|
-
return RE_MESSAGE_CODE_BLOCK_START.test(line);
|
507
|
-
};
|
508
|
-
const isMessageCodeBlockEndIndex = line => {
|
509
|
-
return RE_MESSAGE_CODE_BLOCK_END.test(line);
|
510
|
-
};
|
511
|
-
const getMessageCodeBlock = stderr => {
|
512
|
-
const lines = splitLines(stderr);
|
513
|
-
const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
|
514
|
-
const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
|
515
|
-
const relevantLines = lines.slice(startIndex, endIndex);
|
516
|
-
const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
|
517
|
-
return relevantMessage;
|
518
|
-
};
|
519
|
-
const getNativeModuleErrorMessage = stderr => {
|
520
|
-
const message = getMessageCodeBlock(stderr);
|
521
|
-
return {
|
522
|
-
message: `Incompatible native node module: ${message}`,
|
523
|
-
code: E_INCOMPATIBLE_NATIVE_MODULE
|
524
|
-
};
|
525
|
-
};
|
526
|
-
const isModulesSyntaxError = stderr => {
|
527
|
-
if (!stderr) {
|
528
|
-
return false;
|
519
|
+
const restoreJsonRpcError = error => {
|
520
|
+
if (error && error instanceof Error) {
|
521
|
+
return error;
|
529
522
|
}
|
530
|
-
|
531
|
-
|
532
|
-
const
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
}
|
523
|
+
const currentStack = joinLines$1(splitLines(new Error().stack || '').slice(1));
|
524
|
+
if (error && error.code && error.code === MethodNotFound) {
|
525
|
+
const restoredError = new JsonRpcError(error.message);
|
526
|
+
const parentStack = getParentStack(error);
|
527
|
+
restoredError.stack = parentStack + NewLine$1 + currentStack;
|
528
|
+
return restoredError;
|
529
|
+
}
|
530
|
+
if (error && error.message) {
|
531
|
+
const restoredError = constructError(error.message, error.type, error.name);
|
532
|
+
if (error.data) {
|
533
|
+
if (error.data.stack && error.data.type && error.message) {
|
534
|
+
restoredError.stack = error.data.type + ': ' + error.message + NewLine$1 + error.data.stack + NewLine$1 + currentStack;
|
535
|
+
} else if (error.data.stack) {
|
536
|
+
restoredError.stack = error.data.stack;
|
537
|
+
}
|
538
|
+
if (error.data.codeFrame) {
|
539
|
+
// @ts-ignore
|
540
|
+
restoredError.codeFrame = error.data.codeFrame;
|
541
|
+
}
|
542
|
+
if (error.data.code) {
|
543
|
+
// @ts-ignore
|
544
|
+
restoredError.code = error.data.code;
|
545
|
+
}
|
546
|
+
if (error.data.type) {
|
547
|
+
// @ts-ignore
|
548
|
+
restoredError.name = error.data.type;
|
549
|
+
}
|
550
|
+
} else {
|
551
|
+
if (error.stack) {
|
552
|
+
const lowerStack = restoredError.stack || '';
|
553
|
+
// @ts-ignore
|
554
|
+
const indexNewLine = getNewLineIndex(lowerStack);
|
555
|
+
const parentStack = getParentStack(error);
|
556
|
+
// @ts-ignore
|
557
|
+
restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
|
558
|
+
}
|
559
|
+
if (error.codeFrame) {
|
560
|
+
// @ts-ignore
|
561
|
+
restoredError.codeFrame = error.codeFrame;
|
562
|
+
}
|
563
|
+
}
|
564
|
+
return restoredError;
|
565
|
+
}
|
566
|
+
if (typeof error === 'string') {
|
567
|
+
return new Error(`JsonRpc Error: ${error}`);
|
568
|
+
}
|
569
|
+
return new Error(`JsonRpc Error: ${error}`);
|
537
570
|
};
|
538
|
-
const
|
539
|
-
if (
|
540
|
-
|
571
|
+
const unwrapJsonRpcResult = responseMessage => {
|
572
|
+
if ('error' in responseMessage) {
|
573
|
+
const restoredError = restoreJsonRpcError(responseMessage.error);
|
574
|
+
throw restoredError;
|
541
575
|
}
|
542
|
-
|
576
|
+
if ('result' in responseMessage) {
|
577
|
+
return responseMessage.result;
|
578
|
+
}
|
579
|
+
throw new JsonRpcError('unexpected response message');
|
543
580
|
};
|
544
|
-
const
|
545
|
-
|
581
|
+
const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
|
582
|
+
const getErrorType = prettyError => {
|
583
|
+
if (prettyError && prettyError.type) {
|
584
|
+
return prettyError.type;
|
585
|
+
}
|
586
|
+
if (prettyError && prettyError.constructor && prettyError.constructor.name) {
|
587
|
+
return prettyError.constructor.name;
|
588
|
+
}
|
589
|
+
return undefined;
|
546
590
|
};
|
547
|
-
const
|
548
|
-
|
549
|
-
if (index === -1) {
|
591
|
+
const getErrorProperty = (error, prettyError) => {
|
592
|
+
if (error && error.code === E_COMMAND_NOT_FOUND) {
|
550
593
|
return {
|
551
|
-
|
552
|
-
|
594
|
+
code: MethodNotFound,
|
595
|
+
message: error.message,
|
596
|
+
data: error.stack
|
553
597
|
};
|
554
598
|
}
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
599
|
+
return {
|
600
|
+
code: Custom,
|
601
|
+
message: prettyError.message,
|
602
|
+
data: {
|
603
|
+
stack: prettyError.stack,
|
604
|
+
codeFrame: prettyError.codeFrame,
|
605
|
+
type: getErrorType(prettyError),
|
606
|
+
code: prettyError.code,
|
607
|
+
name: prettyError.name
|
559
608
|
}
|
560
|
-
}
|
609
|
+
};
|
610
|
+
};
|
611
|
+
const create$1 = (message, error) => {
|
561
612
|
return {
|
562
|
-
|
563
|
-
|
613
|
+
jsonrpc: Two,
|
614
|
+
id: message.id,
|
615
|
+
error
|
564
616
|
};
|
565
617
|
};
|
566
|
-
const
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
if (isModuleNotFoundError(stderr)) {
|
574
|
-
return getModuleNotFoundError(stderr);
|
575
|
-
}
|
576
|
-
const lines = splitLines(stderr);
|
577
|
-
const {
|
578
|
-
actualMessage,
|
579
|
-
rest
|
580
|
-
} = getDetails(lines);
|
618
|
+
const getErrorResponse = (message, error, preparePrettyError, logError) => {
|
619
|
+
const prettyError = preparePrettyError(error);
|
620
|
+
logError(error, prettyError);
|
621
|
+
const errorProperty = getErrorProperty(error, prettyError);
|
622
|
+
return create$1(message, errorProperty);
|
623
|
+
};
|
624
|
+
const create$5 = (message, result) => {
|
581
625
|
return {
|
582
|
-
|
583
|
-
|
584
|
-
|
626
|
+
jsonrpc: Two,
|
627
|
+
id: message.id,
|
628
|
+
result: result ?? null
|
585
629
|
};
|
586
630
|
};
|
587
|
-
const
|
588
|
-
|
589
|
-
|
590
|
-
}
|
591
|
-
if (line.startsWith('VError: ')) {
|
592
|
-
return line.slice('VError: '.length);
|
593
|
-
}
|
594
|
-
return line;
|
631
|
+
const getSuccessResponse = (message, result) => {
|
632
|
+
const resultProperty = result ?? null;
|
633
|
+
return create$5(message, resultProperty);
|
595
634
|
};
|
596
|
-
const
|
597
|
-
|
598
|
-
|
599
|
-
return
|
635
|
+
const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
|
636
|
+
try {
|
637
|
+
const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
|
638
|
+
return getSuccessResponse(message, result);
|
639
|
+
} catch (error) {
|
640
|
+
return getErrorResponse(message, error, preparePrettyError, logError);
|
600
641
|
}
|
601
|
-
return stringifiedError;
|
602
642
|
};
|
603
|
-
const
|
604
|
-
|
605
|
-
return string.indexOf(NewLine$2, startIndex);
|
643
|
+
const defaultPreparePrettyError = error => {
|
644
|
+
return error;
|
606
645
|
};
|
607
|
-
const
|
608
|
-
|
609
|
-
return parent;
|
610
|
-
}
|
611
|
-
const parentNewLineIndex = getNewLineIndex(parent);
|
612
|
-
const childNewLineIndex = getNewLineIndex(child);
|
613
|
-
if (childNewLineIndex === -1) {
|
614
|
-
return parent;
|
615
|
-
}
|
616
|
-
const parentFirstLine = parent.slice(0, parentNewLineIndex);
|
617
|
-
const childRest = child.slice(childNewLineIndex);
|
618
|
-
const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
|
619
|
-
if (parentFirstLine.includes(childFirstLine)) {
|
620
|
-
return parentFirstLine + childRest;
|
621
|
-
}
|
622
|
-
return child;
|
646
|
+
const defaultLogError = () => {
|
647
|
+
// ignore
|
623
648
|
};
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
633
|
-
|
634
|
-
|
635
|
-
|
636
|
-
|
637
|
-
|
638
|
-
|
639
|
-
|
640
|
-
|
641
|
-
}
|
642
|
-
class IpcError extends VError {
|
643
|
-
// @ts-ignore
|
644
|
-
constructor(betterMessage, stdout = '', stderr = '') {
|
645
|
-
if (stdout || stderr) {
|
646
|
-
// @ts-ignore
|
647
|
-
const {
|
648
|
-
message,
|
649
|
-
code,
|
650
|
-
stack
|
651
|
-
} = getHelpfulChildProcessError(stdout, stderr);
|
652
|
-
const cause = new Error(message);
|
653
|
-
// @ts-ignore
|
654
|
-
cause.code = code;
|
655
|
-
cause.stack = stack;
|
656
|
-
super(cause, betterMessage);
|
657
|
-
} else {
|
658
|
-
super(betterMessage);
|
659
|
-
}
|
660
|
-
// @ts-ignore
|
661
|
-
this.name = 'IpcError';
|
662
|
-
// @ts-ignore
|
663
|
-
this.stdout = stdout;
|
664
|
-
// @ts-ignore
|
665
|
-
this.stderr = stderr;
|
649
|
+
const defaultRequiresSocket = () => {
|
650
|
+
return false;
|
651
|
+
};
|
652
|
+
const defaultResolve = resolve;
|
653
|
+
|
654
|
+
// TODO maybe remove this in v6 or v7, only accept options object to simplify the code
|
655
|
+
const normalizeParams = args => {
|
656
|
+
if (args.length === 1) {
|
657
|
+
const options = args[0];
|
658
|
+
return {
|
659
|
+
ipc: options.ipc,
|
660
|
+
message: options.message,
|
661
|
+
execute: options.execute,
|
662
|
+
resolve: options.resolve || defaultResolve,
|
663
|
+
preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
|
664
|
+
logError: options.logError || defaultLogError,
|
665
|
+
requiresSocket: options.requiresSocket || defaultRequiresSocket
|
666
|
+
};
|
666
667
|
}
|
667
|
-
}
|
668
|
-
const withResolvers = () => {
|
669
|
-
let _resolve;
|
670
|
-
const promise = new Promise(resolve => {
|
671
|
-
_resolve = resolve;
|
672
|
-
});
|
673
668
|
return {
|
674
|
-
|
675
|
-
|
669
|
+
ipc: args[0],
|
670
|
+
message: args[1],
|
671
|
+
execute: args[2],
|
672
|
+
resolve: args[3],
|
673
|
+
preparePrettyError: args[4],
|
674
|
+
logError: args[5],
|
675
|
+
requiresSocket: args[6]
|
676
676
|
};
|
677
677
|
};
|
678
|
-
const
|
678
|
+
const handleJsonRpcMessage = async (...args) => {
|
679
|
+
const options = normalizeParams(args);
|
679
680
|
const {
|
681
|
+
message,
|
682
|
+
ipc,
|
683
|
+
execute,
|
680
684
|
resolve,
|
681
|
-
|
682
|
-
|
683
|
-
|
684
|
-
|
685
|
-
|
686
|
-
|
687
|
-
|
688
|
-
|
689
|
-
|
690
|
-
|
691
|
-
|
692
|
-
|
693
|
-
|
694
|
-
|
695
|
-
if (firstMessage.method !== 'initialize') {
|
696
|
-
throw new IpcError('unexpected first message');
|
697
|
-
}
|
698
|
-
const type = firstMessage.params[0];
|
699
|
-
if (type === 'message-port') {
|
700
|
-
parentIpc.send({
|
701
|
-
jsonrpc: '2.0',
|
702
|
-
id: firstMessage.id,
|
703
|
-
result: null
|
704
|
-
});
|
705
|
-
parentIpc.dispose();
|
706
|
-
const port = firstMessage.params[1];
|
707
|
-
return port;
|
708
|
-
}
|
709
|
-
return globalThis;
|
710
|
-
};
|
711
|
-
class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
|
712
|
-
constructor(port) {
|
713
|
-
super(port);
|
714
|
-
}
|
715
|
-
getData(event) {
|
716
|
-
return getData$1(event);
|
717
|
-
}
|
718
|
-
send(message) {
|
719
|
-
this._rawIpc.postMessage(message);
|
720
|
-
}
|
721
|
-
sendAndTransfer(message) {
|
722
|
-
const transfer = getTransferrables(message);
|
723
|
-
this._rawIpc.postMessage(message, transfer);
|
724
|
-
}
|
725
|
-
dispose() {
|
726
|
-
if (this._rawIpc.close) {
|
727
|
-
this._rawIpc.close();
|
685
|
+
preparePrettyError,
|
686
|
+
logError,
|
687
|
+
requiresSocket
|
688
|
+
} = options;
|
689
|
+
if ('id' in message) {
|
690
|
+
if ('method' in message) {
|
691
|
+
const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
692
|
+
try {
|
693
|
+
ipc.send(response);
|
694
|
+
} catch (error) {
|
695
|
+
const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
|
696
|
+
ipc.send(errorResponse);
|
697
|
+
}
|
698
|
+
return;
|
728
699
|
}
|
700
|
+
resolve(message.id, message);
|
701
|
+
return;
|
729
702
|
}
|
730
|
-
|
731
|
-
|
703
|
+
if ('method' in message) {
|
704
|
+
await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
|
705
|
+
return;
|
732
706
|
}
|
733
|
-
|
734
|
-
|
735
|
-
|
707
|
+
throw new JsonRpcError('unexpected message');
|
708
|
+
};
|
709
|
+
const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
|
710
|
+
const {
|
711
|
+
message,
|
712
|
+
promise
|
713
|
+
} = create$2(method, params);
|
714
|
+
if (useSendAndTransfer && ipc.sendAndTransfer) {
|
715
|
+
ipc.sendAndTransfer(message);
|
716
|
+
} else {
|
717
|
+
ipc.send(message);
|
736
718
|
}
|
737
|
-
|
738
|
-
|
739
|
-
return new IpcChildWithModuleWorkerAndMessagePort(port);
|
719
|
+
const responseMessage = await promise;
|
720
|
+
return unwrapJsonRpcResult(responseMessage);
|
740
721
|
};
|
741
|
-
const
|
742
|
-
|
743
|
-
|
744
|
-
|
722
|
+
const send = (transport, method, ...params) => {
|
723
|
+
const message = create$4(method, params);
|
724
|
+
transport.send(message);
|
725
|
+
};
|
726
|
+
const invoke$1 = (ipc, method, ...params) => {
|
727
|
+
return invokeHelper(ipc, method, params, false);
|
728
|
+
};
|
729
|
+
const invokeAndTransfer = (ipc, method, ...params) => {
|
730
|
+
return invokeHelper(ipc, method, params, true);
|
731
|
+
};
|
732
|
+
|
733
|
+
const commands = Object.create(null);
|
734
|
+
const register = commandMap => {
|
735
|
+
Object.assign(commands, commandMap);
|
736
|
+
};
|
737
|
+
const getCommand = key => {
|
738
|
+
return commands[key];
|
739
|
+
};
|
740
|
+
const execute = (command, ...args) => {
|
741
|
+
const fn = getCommand(command);
|
742
|
+
if (!fn) {
|
743
|
+
throw new Error(`command not found ${command}`);
|
744
|
+
}
|
745
|
+
return fn(...args);
|
745
746
|
};
|
746
747
|
|
747
748
|
const createRpc = ipc => {
|
@@ -771,32 +772,38 @@ const logError = () => {
|
|
771
772
|
// handled by renderer worker
|
772
773
|
};
|
773
774
|
const handleMessage = event => {
|
774
|
-
|
775
|
+
const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
|
776
|
+
return handleJsonRpcMessage(event.target, event.data, execute, resolve, preparePrettyError, logError, actualRequiresSocket);
|
775
777
|
};
|
776
778
|
const handleIpc = ipc => {
|
777
|
-
|
779
|
+
if ('addEventListener' in ipc) {
|
780
|
+
ipc.addEventListener('message', handleMessage);
|
781
|
+
} else if ('on' in ipc) {
|
782
|
+
// deprecated
|
783
|
+
ipc.on('message', handleMessage);
|
784
|
+
}
|
778
785
|
};
|
779
|
-
|
780
|
-
|
781
|
-
|
782
|
-
|
783
|
-
|
786
|
+
const listen$1 = async (module, options) => {
|
787
|
+
const rawIpc = await module.listen(options);
|
788
|
+
if (module.signal) {
|
789
|
+
module.signal(rawIpc);
|
790
|
+
}
|
784
791
|
const ipc = module.wrap(rawIpc);
|
785
792
|
return ipc;
|
786
793
|
};
|
787
|
-
const create
|
794
|
+
const create = async ({
|
788
795
|
commandMap
|
789
796
|
}) => {
|
790
797
|
// TODO create a commandMap per rpc instance
|
791
798
|
register(commandMap);
|
792
|
-
const ipc = await listen$1();
|
799
|
+
const ipc = await listen$1(IpcChildWithModuleWorkerAndMessagePort$1);
|
793
800
|
handleIpc(ipc);
|
794
801
|
const rpc = createRpc(ipc);
|
795
802
|
return rpc;
|
796
803
|
};
|
797
804
|
const WebWorkerRpcClient = {
|
798
805
|
__proto__: null,
|
799
|
-
create
|
806
|
+
create
|
800
807
|
};
|
801
808
|
|
802
809
|
const None = 0;
|
@@ -1249,6 +1256,7 @@ const renderLine = (line, index) => {
|
|
1249
1256
|
}
|
1250
1257
|
return [br, text(line)];
|
1251
1258
|
};
|
1259
|
+
|
1252
1260
|
const getAboutContentVirtualDom = lines => {
|
1253
1261
|
const dom = [{
|
1254
1262
|
type: Div,
|
@@ -1280,6 +1288,7 @@ const getPrimaryButtonVirtualDom = (message, onClick, name) => {
|
|
1280
1288
|
name
|
1281
1289
|
}, text(message)];
|
1282
1290
|
};
|
1291
|
+
|
1283
1292
|
const getSecondaryButtonVirtualDom = (message, onClick, name) => {
|
1284
1293
|
return [{
|
1285
1294
|
type: Button$1,
|
@@ -1464,7 +1473,7 @@ const doRender = (oldState, newState) => {
|
|
1464
1473
|
return commands;
|
1465
1474
|
};
|
1466
1475
|
|
1467
|
-
const
|
1476
|
+
const renderEventListeners = () => {
|
1468
1477
|
return [{
|
1469
1478
|
name: HandleClickOk,
|
1470
1479
|
params: ['handleClickOk']
|
@@ -1538,7 +1547,7 @@ const commandMap = {
|
|
1538
1547
|
'About.getVirtualDom': getAboutVirtualDom,
|
1539
1548
|
'About.loadContent': loadContent,
|
1540
1549
|
'About.render': doRender,
|
1541
|
-
'About.renderEventListeners':
|
1550
|
+
'About.renderEventListeners': renderEventListeners,
|
1542
1551
|
'About.showAboutElectron': showAboutElectron
|
1543
1552
|
};
|
1544
1553
|
|