@lvce-editor/about-view 3.0.0 → 4.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,747 +1,746 @@
1
- const Two = '2.0';
2
- const create$4 = (method, params) => {
3
- return {
4
- jsonrpc: Two,
5
- method,
6
- params
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 resolve = (id, response) => {
41
- const fn = get(id);
42
- if (!fn) {
43
- console.log(response);
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
- fn(response);
48
- remove(id);
15
+ return stringifiedError;
49
16
  };
50
- const create$2 = (method, params) => {
51
- const {
52
- id,
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
- class JsonRpcError extends Error {
67
- constructor(message) {
68
- super(message);
69
- this.name = 'JsonRpcError';
21
+ const mergeStacks = (parent, child) => {
22
+ if (!child) {
23
+ return parent;
70
24
  }
71
- }
72
- const NewLine$3 = '\n';
73
- const DomException = 'DOMException';
74
- const ReferenceError$1 = 'ReferenceError';
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
- if (message.startsWith('TypeError: ')) {
93
- return TypeError;
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
- if (message.startsWith('SyntaxError: ')) {
96
- return SyntaxError;
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
- if (message.startsWith('ReferenceError: ')) {
99
- return ReferenceError;
55
+ }
56
+
57
+ const walkValue = (value, transferrables, isTransferrable) => {
58
+ if (!value) {
59
+ return;
100
60
  }
101
- return Error;
102
- };
103
- const constructError = (message, type, name) => {
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 (ErrorConstructor === Error) {
109
- const error = new Error(message);
110
- if (name && name !== 'VError') {
111
- error.name = name;
65
+ if (Array.isArray(value)) {
66
+ for (const item of value) {
67
+ walkValue(item, transferrables, isTransferrable);
112
68
  }
113
- return error;
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 getNewLineIndex$1 = (string, startIndex = undefined) => {
118
- return string.indexOf(NewLine$3, startIndex);
78
+ const isMessagePort = value => {
79
+ return value && value instanceof MessagePort;
119
80
  };
120
- const getParentStack = error => {
121
- let parentStack = error.stack || error.data || error.message || '';
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 joinLines$2 = lines => {
128
- return lines.join(NewLine$3);
84
+ const isOffscreenCanvas = value => {
85
+ return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
129
86
  };
130
- const MethodNotFound = -32601;
131
- const Custom = -32001;
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 restoreJsonRpcError = error => {
136
- if (error && error instanceof Error) {
137
- return error;
138
- }
139
- const currentStack = joinLines$2(splitLines$1(new Error().stack || '').slice(1));
140
- if (error && error.code && error.code === MethodNotFound) {
141
- const restoredError = new JsonRpcError(error.message);
142
- const parentStack = getParentStack(error);
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
- }
182
- if (typeof error === 'string') {
183
- return new Error(`JsonRpc Error: ${error}`);
184
99
  }
185
- return new Error(`JsonRpc Error: ${error}`);
100
+ return false;
186
101
  };
187
- const unwrapJsonRpcResult = responseMessage => {
188
- if ('error' in responseMessage) {
189
- const restoredError = restoreJsonRpcError(responseMessage.error);
190
- throw restoredError;
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 E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
198
- const getErrorType = prettyError => {
199
- if (prettyError && prettyError.type) {
200
- return prettyError.type;
201
- }
202
- if (prettyError && prettyError.constructor && prettyError.constructor.name) {
203
- return prettyError.constructor.name;
204
- }
205
- return undefined;
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
- const getErrorProperty = (error, prettyError) => {
208
- if (error && error.code === E_COMMAND_NOT_FOUND) {
209
- return {
210
- code: MethodNotFound,
211
- message: error.message,
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
- return {
216
- code: Custom,
217
- message: prettyError.message,
218
- data: {
219
- stack: prettyError.stack,
220
- codeFrame: prettyError.codeFrame,
221
- type: getErrorType(prettyError),
222
- code: prettyError.code,
223
- name: prettyError.name
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);
226
133
  };
227
- const create$1$1 = (message, error) => {
134
+ const splitLines$1 = lines => {
135
+ return lines.split(NewLine$2);
136
+ };
137
+ const isModuleNotFoundMessage = line => {
138
+ return line.includes('[ERR_MODULE_NOT_FOUND]');
139
+ };
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
- jsonrpc: Two,
230
- id: message.id,
231
- error
145
+ message,
146
+ code: ERR_MODULE_NOT_FOUND
232
147
  };
233
148
  };
234
- const getErrorResponse = (message, error, preparePrettyError, logError) => {
235
- const prettyError = preparePrettyError(error);
236
- logError(error, prettyError);
237
- const errorProperty = getErrorProperty(error, prettyError);
238
- return create$1$1(message, errorProperty);
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);
157
+ };
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;
239
171
  };
240
- const create = (message, result) => {
172
+ const getNativeModuleErrorMessage = stderr => {
173
+ const message = getMessageCodeBlock(stderr);
241
174
  return {
242
- jsonrpc: Two,
243
- id: message.id,
244
- result: result ?? null
175
+ message: `Incompatible native node module: ${message}`,
176
+ code: E_INCOMPATIBLE_NATIVE_MODULE
245
177
  };
246
178
  };
247
- const getSuccessResponse = (message, result) => {
248
- const resultProperty = result ?? null;
249
- return create(message, resultProperty);
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 defaultPreparePrettyError = error => {
260
- return error;
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 defaultLogError = () => {
263
- // ignore
191
+ const isModuleNotFoundError = stderr => {
192
+ if (!stderr) {
193
+ return false;
194
+ }
195
+ return stderr.includes('ERR_MODULE_NOT_FOUND');
264
196
  };
265
- const defaultRequiresSocket = () => {
266
- return false;
197
+ const isNormalStackLine = line => {
198
+ return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
267
199
  };
268
- const defaultResolve = resolve;
269
-
270
- // TODO maybe remove this in v6 or v7, only accept options object to simplify the code
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
- ipc: options.ipc,
276
- message: options.message,
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
- ipc: args[0],
286
- message: args[1],
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 handleJsonRpcMessage = async (...args) => {
295
- const options = normalizeParams(args);
219
+ const getHelpfulChildProcessError = (stdout, stderr) => {
220
+ if (isUnhelpfulNativeModuleError(stderr)) {
221
+ return getNativeModuleErrorMessage(stderr);
222
+ }
223
+ if (isModulesSyntaxError(stderr)) {
224
+ return getModuleSyntaxError();
225
+ }
226
+ if (isModuleNotFoundError(stderr)) {
227
+ return getModuleNotFoundError(stderr);
228
+ }
229
+ const lines = splitLines$1(stderr);
296
230
  const {
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;
231
+ actualMessage,
232
+ rest
233
+ } = getDetails(lines);
234
+ return {
235
+ message: `${actualMessage}`,
236
+ code: '',
237
+ stack: rest
238
+ };
239
+ };
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);
315
257
  }
316
- resolve(message.id, message);
317
- return;
318
- }
319
- if ('method' in message) {
320
- await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
321
- return;
258
+ // @ts-ignore
259
+ this.name = 'IpcError';
260
+ // @ts-ignore
261
+ this.stdout = stdout;
262
+ // @ts-ignore
263
+ this.stderr = stderr;
322
264
  }
323
- throw new JsonRpcError('unexpected message');
265
+ }
266
+ const readyMessage = 'ready';
267
+ const getData$2 = event => {
268
+ return event.data;
324
269
  };
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);
270
+ const listen$6 = () => {
271
+ // @ts-ignore
272
+ if (typeof WorkerGlobalScope === 'undefined') {
273
+ throw new TypeError('module is not in web worker scope');
334
274
  }
335
- const responseMessage = await promise;
336
- return unwrapJsonRpcResult(responseMessage);
337
- };
338
- const send = (transport, method, ...params) => {
339
- const message = create$4(method, params);
340
- transport.send(message);
341
- };
342
- const invoke$1 = (ipc, method, ...params) => {
343
- return invokeHelper(ipc, method, params, false);
344
- };
345
- const invokeAndTransfer = (ipc, method, ...params) => {
346
- return invokeHelper(ipc, method, params, true);
347
- };
348
-
349
- const commands = Object.create(null);
350
- const register = commandMap => {
351
- Object.assign(commands, commandMap);
275
+ return globalThis;
352
276
  };
353
- const getCommand = key => {
354
- return commands[key];
277
+ const signal$6 = global => {
278
+ global.postMessage(readyMessage);
355
279
  };
356
- const execute = (command, ...args) => {
357
- const fn = getCommand(command);
358
- if (!fn) {
359
- throw new Error(`command not found ${command}`);
280
+ class IpcChildWithModuleWorker extends Ipc {
281
+ getData(event) {
282
+ return getData$2(event);
360
283
  }
361
- return fn(...args);
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
- const getData$1 = event => {
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 attachEvents = that => {
368
- const handleMessage = (...args) => {
369
- const data = that.getData(...args);
370
- that.dispatchEvent(new MessageEvent('message', {
371
- data
372
- }));
373
- };
374
- that.onMessage(handleMessage);
375
- const handleClose = event => {
376
- that.dispatchEvent(new Event('close'));
377
- };
378
- that.onClose(handleClose);
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 Ipc extends EventTarget {
381
- constructor(rawIpc) {
382
- super();
383
- this._rawIpc = rawIpc;
384
- attachEvents(this);
349
+ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
350
+ constructor(port) {
351
+ super(port);
385
352
  }
386
- }
387
- const readyMessage = 'ready';
388
- const walkValue = (value, transferrables, isTransferrable) => {
389
- if (!value) {
390
- return;
353
+ getData(event) {
354
+ return getData$2(event);
391
355
  }
392
- if (isTransferrable(value)) {
393
- transferrables.push(value);
394
- return;
356
+ send(message) {
357
+ this._rawIpc.postMessage(message);
395
358
  }
396
- if (Array.isArray(value)) {
397
- for (const item of value) {
398
- walkValue(item, transferrables, isTransferrable);
399
- }
400
- return;
359
+ sendAndTransfer(message) {
360
+ const transfer = getTransferrables(message);
361
+ this._rawIpc.postMessage(message, transfer);
401
362
  }
402
- if (typeof value === 'object') {
403
- for (const property of Object.values(value)) {
404
- walkValue(property, transferrables, isTransferrable);
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 isMessagePort = value => {
410
- return value && value instanceof MessagePort;
379
+ const IpcChildWithModuleWorkerAndMessagePort$1 = {
380
+ __proto__: null,
381
+ listen: listen$5,
382
+ wrap: wrap$c
411
383
  };
412
- const isMessagePortMain = value => {
413
- return value && value.constructor && value.constructor.name === 'MessagePortMain';
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 isOffscreenCanvas = value => {
416
- return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
393
+ const callbacks = Object.create(null);
394
+ const set = (id, fn) => {
395
+ callbacks[id] = fn;
417
396
  };
418
- const isInstanceOf = (value, constructorName) => {
419
- return value?.constructor?.name === constructorName;
397
+ const get = id => {
398
+ return callbacks[id];
420
399
  };
421
- const isSocket = value => {
422
- return isInstanceOf(value, 'Socket');
400
+ const remove = id => {
401
+ delete callbacks[id];
423
402
  };
424
- const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
425
- const isTransferrable = value => {
426
- for (const fn of transferrables) {
427
- if (fn(value)) {
428
- return true;
429
- }
403
+ let id = 0;
404
+ const create$3 = () => {
405
+ return ++id;
406
+ };
407
+ const warn = (...args) => {
408
+ console.warn(...args);
409
+ };
410
+ const registerPromise = () => {
411
+ const id = create$3();
412
+ const {
413
+ resolve,
414
+ promise
415
+ } = Promise.withResolvers();
416
+ set(id, resolve);
417
+ return {
418
+ id,
419
+ promise
420
+ };
421
+ };
422
+ const resolve = (id, response) => {
423
+ const fn = get(id);
424
+ if (!fn) {
425
+ console.log(response);
426
+ warn(`callback ${id} may already be disposed`);
427
+ return;
430
428
  }
431
- return false;
429
+ fn(response);
430
+ remove(id);
432
431
  };
433
- const getTransferrables = value => {
434
- const transferrables = [];
435
- walkValue(value, transferrables, isTransferrable);
436
- return transferrables;
432
+ const create$2 = (method, params) => {
433
+ const {
434
+ id,
435
+ promise
436
+ } = registerPromise();
437
+ const message = {
438
+ jsonrpc: Two,
439
+ method,
440
+ params,
441
+ id
442
+ };
443
+ return {
444
+ message,
445
+ promise
446
+ };
437
447
  };
438
- const listen$2 = () => {
439
- // @ts-ignore
440
- if (typeof WorkerGlobalScope === 'undefined') {
441
- throw new TypeError('module is not in web worker scope');
448
+ class JsonRpcError extends Error {
449
+ constructor(message) {
450
+ super(message);
451
+ this.name = 'JsonRpcError';
442
452
  }
443
- return globalThis;
444
- };
445
- const signal$2 = global => {
446
- global.postMessage(readyMessage);
447
- };
448
- class IpcChildWithModuleWorker extends Ipc {
449
- getData(event) {
450
- return getData$1(event);
453
+ }
454
+ const NewLine$1 = '\n';
455
+ const DomException = 'DOMException';
456
+ const ReferenceError$1 = 'ReferenceError';
457
+ const SyntaxError$1 = 'SyntaxError';
458
+ const TypeError$1 = 'TypeError';
459
+ const getErrorConstructor = (message, type) => {
460
+ if (type) {
461
+ switch (type) {
462
+ case DomException:
463
+ return DOMException;
464
+ case TypeError$1:
465
+ return TypeError;
466
+ case SyntaxError$1:
467
+ return SyntaxError;
468
+ case ReferenceError$1:
469
+ return ReferenceError;
470
+ default:
471
+ return Error;
472
+ }
451
473
  }
452
- send(message) {
453
- // @ts-ignore
454
- this._rawIpc.postMessage(message);
474
+ if (message.startsWith('TypeError: ')) {
475
+ return TypeError;
455
476
  }
456
- sendAndTransfer(message) {
457
- const transfer = getTransferrables(message);
458
- // @ts-ignore
459
- this._rawIpc.postMessage(message, transfer);
477
+ if (message.startsWith('SyntaxError: ')) {
478
+ return SyntaxError;
460
479
  }
461
- dispose() {
462
- // ignore
480
+ if (message.startsWith('ReferenceError: ')) {
481
+ return ReferenceError;
463
482
  }
464
- onClose(callback) {
465
- // ignore
483
+ return Error;
484
+ };
485
+ const constructError = (message, type, name) => {
486
+ const ErrorConstructor = getErrorConstructor(message, type);
487
+ if (ErrorConstructor === DOMException && name) {
488
+ return new ErrorConstructor(message, name);
466
489
  }
467
- onMessage(callback) {
468
- this._rawIpc.addEventListener('message', callback);
490
+ if (ErrorConstructor === Error) {
491
+ const error = new Error(message);
492
+ if (name && name !== 'VError') {
493
+ error.name = name;
494
+ }
495
+ return error;
469
496
  }
470
- }
471
- const wrap$5 = global => {
472
- return new IpcChildWithModuleWorker(global);
497
+ return new ErrorConstructor(message);
498
+ };
499
+ const getNewLineIndex = (string, startIndex = undefined) => {
500
+ return string.indexOf(NewLine$1, startIndex);
501
+ };
502
+ const getParentStack = error => {
503
+ let parentStack = error.stack || error.data || error.message || '';
504
+ if (parentStack.startsWith(' at')) {
505
+ parentStack = error.message + NewLine$1 + parentStack;
506
+ }
507
+ return parentStack;
473
508
  };
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
509
  const joinLines$1 = lines => {
479
510
  return lines.join(NewLine$1);
480
511
  };
512
+ const MethodNotFound = -32601;
513
+ const Custom = -32001;
481
514
  const splitLines = lines => {
482
515
  return lines.split(NewLine$1);
483
516
  };
484
- const isModuleNotFoundMessage = line => {
485
- return line.includes('[ERR_MODULE_NOT_FOUND]');
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;
517
+ const restoreJsonRpcError = error => {
518
+ if (error && error instanceof Error) {
519
+ return error;
529
520
  }
530
- return stderr.includes('SyntaxError: Cannot use import statement outside a module');
531
- };
532
- const getModuleSyntaxError = () => {
533
- return {
534
- message: `ES Modules are not supported in electron`,
535
- code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
536
- };
521
+ const currentStack = joinLines$1(splitLines(new Error().stack || '').slice(1));
522
+ if (error && error.code && error.code === MethodNotFound) {
523
+ const restoredError = new JsonRpcError(error.message);
524
+ const parentStack = getParentStack(error);
525
+ restoredError.stack = parentStack + NewLine$1 + currentStack;
526
+ return restoredError;
527
+ }
528
+ if (error && error.message) {
529
+ const restoredError = constructError(error.message, error.type, error.name);
530
+ if (error.data) {
531
+ if (error.data.stack && error.data.type && error.message) {
532
+ restoredError.stack = error.data.type + ': ' + error.message + NewLine$1 + error.data.stack + NewLine$1 + currentStack;
533
+ } else if (error.data.stack) {
534
+ restoredError.stack = error.data.stack;
535
+ }
536
+ if (error.data.codeFrame) {
537
+ // @ts-ignore
538
+ restoredError.codeFrame = error.data.codeFrame;
539
+ }
540
+ if (error.data.code) {
541
+ // @ts-ignore
542
+ restoredError.code = error.data.code;
543
+ }
544
+ if (error.data.type) {
545
+ // @ts-ignore
546
+ restoredError.name = error.data.type;
547
+ }
548
+ } else {
549
+ if (error.stack) {
550
+ const lowerStack = restoredError.stack || '';
551
+ // @ts-ignore
552
+ const indexNewLine = getNewLineIndex(lowerStack);
553
+ const parentStack = getParentStack(error);
554
+ // @ts-ignore
555
+ restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
556
+ }
557
+ if (error.codeFrame) {
558
+ // @ts-ignore
559
+ restoredError.codeFrame = error.codeFrame;
560
+ }
561
+ }
562
+ return restoredError;
563
+ }
564
+ if (typeof error === 'string') {
565
+ return new Error(`JsonRpc Error: ${error}`);
566
+ }
567
+ return new Error(`JsonRpc Error: ${error}`);
537
568
  };
538
- const isModuleNotFoundError = stderr => {
539
- if (!stderr) {
540
- return false;
569
+ const unwrapJsonRpcResult = responseMessage => {
570
+ if ('error' in responseMessage) {
571
+ const restoredError = restoreJsonRpcError(responseMessage.error);
572
+ throw restoredError;
541
573
  }
542
- return stderr.includes('ERR_MODULE_NOT_FOUND');
574
+ if ('result' in responseMessage) {
575
+ return responseMessage.result;
576
+ }
577
+ throw new JsonRpcError('unexpected response message');
543
578
  };
544
- const isNormalStackLine = line => {
545
- return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
579
+ const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
580
+ const getErrorType = prettyError => {
581
+ if (prettyError && prettyError.type) {
582
+ return prettyError.type;
583
+ }
584
+ if (prettyError && prettyError.constructor && prettyError.constructor.name) {
585
+ return prettyError.constructor.name;
586
+ }
587
+ return undefined;
546
588
  };
547
- const getDetails = lines => {
548
- const index = lines.findIndex(isNormalStackLine);
549
- if (index === -1) {
589
+ const getErrorProperty = (error, prettyError) => {
590
+ if (error && error.code === E_COMMAND_NOT_FOUND) {
550
591
  return {
551
- actualMessage: joinLines$1(lines),
552
- rest: []
592
+ code: MethodNotFound,
593
+ message: error.message,
594
+ data: error.stack
553
595
  };
554
596
  }
555
- let lastIndex = index - 1;
556
- while (++lastIndex < lines.length) {
557
- if (!isNormalStackLine(lines[lastIndex])) {
558
- break;
597
+ return {
598
+ code: Custom,
599
+ message: prettyError.message,
600
+ data: {
601
+ stack: prettyError.stack,
602
+ codeFrame: prettyError.codeFrame,
603
+ type: getErrorType(prettyError),
604
+ code: prettyError.code,
605
+ name: prettyError.name
559
606
  }
560
- }
607
+ };
608
+ };
609
+ const create$1 = (message, error) => {
561
610
  return {
562
- actualMessage: lines[index - 1],
563
- rest: lines.slice(index, lastIndex)
611
+ jsonrpc: Two,
612
+ id: message.id,
613
+ error
564
614
  };
565
615
  };
566
- const getHelpfulChildProcessError = (stdout, stderr) => {
567
- if (isUnhelpfulNativeModuleError(stderr)) {
568
- return getNativeModuleErrorMessage(stderr);
569
- }
570
- if (isModulesSyntaxError(stderr)) {
571
- return getModuleSyntaxError();
572
- }
573
- if (isModuleNotFoundError(stderr)) {
574
- return getModuleNotFoundError(stderr);
575
- }
576
- const lines = splitLines(stderr);
577
- const {
578
- actualMessage,
579
- rest
580
- } = getDetails(lines);
616
+ const getErrorResponse = (message, error, preparePrettyError, logError) => {
617
+ const prettyError = preparePrettyError(error);
618
+ logError(error, prettyError);
619
+ const errorProperty = getErrorProperty(error, prettyError);
620
+ return create$1(message, errorProperty);
621
+ };
622
+ const create$5 = (message, result) => {
581
623
  return {
582
- message: `${actualMessage}`,
583
- code: '',
584
- stack: rest
624
+ jsonrpc: Two,
625
+ id: message.id,
626
+ result: result ?? null
585
627
  };
586
628
  };
587
- const normalizeLine = line => {
588
- if (line.startsWith('Error: ')) {
589
- return line.slice('Error: '.length);
590
- }
591
- if (line.startsWith('VError: ')) {
592
- return line.slice('VError: '.length);
593
- }
594
- return line;
629
+ const getSuccessResponse = (message, result) => {
630
+ const resultProperty = result ?? null;
631
+ return create$5(message, resultProperty);
595
632
  };
596
- const getCombinedMessage = (error, message) => {
597
- const stringifiedError = normalizeLine(`${error}`);
598
- if (message) {
599
- return `${message}: ${stringifiedError}`;
633
+ const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
634
+ try {
635
+ const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
636
+ return getSuccessResponse(message, result);
637
+ } catch (error) {
638
+ return getErrorResponse(message, error, preparePrettyError, logError);
600
639
  }
601
- return stringifiedError;
602
640
  };
603
- const NewLine$2 = '\n';
604
- const getNewLineIndex = (string, startIndex = undefined) => {
605
- return string.indexOf(NewLine$2, startIndex);
641
+ const defaultPreparePrettyError = error => {
642
+ return error;
606
643
  };
607
- const mergeStacks = (parent, child) => {
608
- if (!child) {
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;
644
+ const defaultLogError = () => {
645
+ // ignore
623
646
  };
624
- class VError extends Error {
625
- constructor(error, message) {
626
- const combinedMessage = getCombinedMessage(error, message);
627
- super(combinedMessage);
628
- this.name = 'VError';
629
- if (error instanceof Error) {
630
- this.stack = mergeStacks(this.stack, error.stack);
631
- }
632
- if (error.codeFrame) {
633
- // @ts-ignore
634
- this.codeFrame = error.codeFrame;
635
- }
636
- if (error.code) {
637
- // @ts-ignore
638
- this.code = error.code;
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;
647
+ const defaultRequiresSocket = () => {
648
+ return false;
649
+ };
650
+ const defaultResolve = resolve;
651
+
652
+ // TODO maybe remove this in v6 or v7, only accept options object to simplify the code
653
+ const normalizeParams = args => {
654
+ if (args.length === 1) {
655
+ const options = args[0];
656
+ return {
657
+ ipc: options.ipc,
658
+ message: options.message,
659
+ execute: options.execute,
660
+ resolve: options.resolve || defaultResolve,
661
+ preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
662
+ logError: options.logError || defaultLogError,
663
+ requiresSocket: options.requiresSocket || defaultRequiresSocket
664
+ };
666
665
  }
667
- }
668
- const withResolvers = () => {
669
- let _resolve;
670
- const promise = new Promise(resolve => {
671
- _resolve = resolve;
672
- });
673
666
  return {
674
- resolve: _resolve,
675
- promise
667
+ ipc: args[0],
668
+ message: args[1],
669
+ execute: args[2],
670
+ resolve: args[3],
671
+ preparePrettyError: args[4],
672
+ logError: args[5],
673
+ requiresSocket: args[6]
676
674
  };
677
675
  };
678
- const waitForFirstMessage = async port => {
676
+ const handleJsonRpcMessage = async (...args) => {
677
+ const options = normalizeParams(args);
679
678
  const {
679
+ message,
680
+ ipc,
681
+ execute,
680
682
  resolve,
681
- promise
682
- } = withResolvers();
683
- port.addEventListener('message', resolve, {
684
- once: true
685
- });
686
- const event = await promise;
687
- // @ts-ignore
688
- return event.data;
689
- };
690
- const listen$1$1 = async () => {
691
- const parentIpcRaw = listen$2();
692
- signal$2(parentIpcRaw);
693
- const parentIpc = wrap$5(parentIpcRaw);
694
- const firstMessage = await waitForFirstMessage(parentIpc);
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();
683
+ preparePrettyError,
684
+ logError,
685
+ requiresSocket
686
+ } = options;
687
+ if ('id' in message) {
688
+ if ('method' in message) {
689
+ const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
690
+ try {
691
+ ipc.send(response);
692
+ } catch (error) {
693
+ const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
694
+ ipc.send(errorResponse);
695
+ }
696
+ return;
728
697
  }
698
+ resolve(message.id, message);
699
+ return;
729
700
  }
730
- onClose(callback) {
731
- // ignore
701
+ if ('method' in message) {
702
+ await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
703
+ return;
732
704
  }
733
- onMessage(callback) {
734
- this._rawIpc.addEventListener('message', callback);
735
- this._rawIpc.start();
705
+ throw new JsonRpcError('unexpected message');
706
+ };
707
+ const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
708
+ const {
709
+ message,
710
+ promise
711
+ } = create$2(method, params);
712
+ if (useSendAndTransfer && ipc.sendAndTransfer) {
713
+ ipc.sendAndTransfer(message);
714
+ } else {
715
+ ipc.send(message);
736
716
  }
737
- }
738
- const wrap$4 = port => {
739
- return new IpcChildWithModuleWorkerAndMessagePort(port);
717
+ const responseMessage = await promise;
718
+ return unwrapJsonRpcResult(responseMessage);
740
719
  };
741
- const IpcChildWithModuleWorkerAndMessagePort$1 = {
742
- __proto__: null,
743
- listen: listen$1$1,
744
- wrap: wrap$4
720
+ const send = (transport, method, ...params) => {
721
+ const message = create$4(method, params);
722
+ transport.send(message);
723
+ };
724
+ const invoke$1 = (ipc, method, ...params) => {
725
+ return invokeHelper(ipc, method, params, false);
726
+ };
727
+ const invokeAndTransfer = (ipc, method, ...params) => {
728
+ return invokeHelper(ipc, method, params, true);
729
+ };
730
+
731
+ const commands = Object.create(null);
732
+ const register = commandMap => {
733
+ Object.assign(commands, commandMap);
734
+ };
735
+ const getCommand = key => {
736
+ return commands[key];
737
+ };
738
+ const execute = (command, ...args) => {
739
+ const fn = getCommand(command);
740
+ if (!fn) {
741
+ throw new Error(`command not found ${command}`);
742
+ }
743
+ return fn(...args);
745
744
  };
746
745
 
747
746
  const createRpc = ipc => {
@@ -771,44 +770,50 @@ const logError = () => {
771
770
  // handled by renderer worker
772
771
  };
773
772
  const handleMessage = event => {
774
- return handleJsonRpcMessage(event.target, event.data, execute, resolve, preparePrettyError, logError, requiresSocket);
773
+ const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
774
+ return handleJsonRpcMessage(event.target, event.data, execute, resolve, preparePrettyError, logError, actualRequiresSocket);
775
775
  };
776
776
  const handleIpc = ipc => {
777
- ipc.addEventListener('message', handleMessage);
777
+ if ('addEventListener' in ipc) {
778
+ ipc.addEventListener('message', handleMessage);
779
+ } else if ('on' in ipc) {
780
+ // deprecated
781
+ ipc.on('message', handleMessage);
782
+ }
778
783
  };
779
-
780
- // @ts-ignore
781
- const listen$1 = async () => {
782
- const module = IpcChildWithModuleWorkerAndMessagePort$1;
783
- const rawIpc = await module.listen();
784
+ const listen$1 = async (module, options) => {
785
+ const rawIpc = await module.listen(options);
786
+ if (module.signal) {
787
+ module.signal(rawIpc);
788
+ }
784
789
  const ipc = module.wrap(rawIpc);
785
790
  return ipc;
786
791
  };
787
- const create$1 = async ({
792
+ const create = async ({
788
793
  commandMap
789
794
  }) => {
790
795
  // TODO create a commandMap per rpc instance
791
796
  register(commandMap);
792
- const ipc = await listen$1();
797
+ const ipc = await listen$1(IpcChildWithModuleWorkerAndMessagePort$1);
793
798
  handleIpc(ipc);
794
799
  const rpc = createRpc(ipc);
795
800
  return rpc;
796
801
  };
797
802
  const WebWorkerRpcClient = {
798
803
  __proto__: null,
799
- create: create$1
804
+ create
800
805
  };
801
806
 
802
807
  const None = 0;
803
- const Ok$1 = 1;
804
- const Copy$1 = 2;
808
+ const Ok$2 = 1;
809
+ const Copy$2 = 2;
805
810
 
806
811
  const getNextFocus = focusId => {
807
812
  switch (focusId) {
808
- case Ok$1:
809
- return Copy$1;
810
- case Copy$1:
811
- return Ok$1;
813
+ case Ok$2:
814
+ return Copy$2;
815
+ case Copy$2:
816
+ return Ok$2;
812
817
  default:
813
818
  return None;
814
819
  }
@@ -826,10 +831,10 @@ const focusNext = state => {
826
831
 
827
832
  const getPreviousFocus = focusId => {
828
833
  switch (focusId) {
829
- case Ok$1:
830
- return Copy$1;
831
- case Copy$1:
832
- return Ok$1;
834
+ case Ok$2:
835
+ return Copy$2;
836
+ case Copy$2:
837
+ return Ok$2;
833
838
  default:
834
839
  return None;
835
840
  }
@@ -845,6 +850,35 @@ const focusPrevious = state => {
845
850
  };
846
851
  };
847
852
 
853
+ const OneSecondAgo = '1 second ago';
854
+ const SomeSecondsAgo = '{PH1} seconds ago';
855
+ const OneMinuteAgo = '1 minute ago';
856
+ const SomeMinutesAgo = '{PH1} minutes ago';
857
+ const OneHourAgo = '1 hour ago';
858
+ const SomeHoursAgo = '{PH1} hours ago';
859
+ const OneDayAgo = '1 day ago';
860
+ const SomeDaysAgo = '{PH1} days ago';
861
+ const OneWeekAgo = '1 week ago';
862
+ const SomeWeeksAgo = '{PH1} weeks ago';
863
+ const OneMonthAgo = '1 month ago';
864
+ const SomeMonthsAgo = '{PH1} months ago';
865
+ const OneYearAgo = '1 year ago';
866
+ const SomeYearsAgo = '{PH1} years ago';
867
+ const InOneSecond = 'in 1 second';
868
+ const InSomeSeconds = 'in {PH1} seconds';
869
+ const InOneMinute = 'in 1 minute';
870
+ const InSomeMinutes = 'in {PH1} minutes';
871
+ const InOneHour = 'in 1 hour';
872
+ const InSomeHours = 'in {PH1} hours';
873
+ const InOneDay = 'in 1 day';
874
+ const InSomeDays = 'in {PH1} days';
875
+ const InOneWeek = 'in 1 week';
876
+ const InSomeWeeks = 'in {PH1} weeks';
877
+ const InOneMonth = 'in 1 month';
878
+ const InSomeMonths = 'in {PH1} months';
879
+ const InOneYear = 'in 1 year';
880
+ const InSomeYears = 'in {PH1} years';
881
+
848
882
  const emptyObject = {};
849
883
  const RE_PLACEHOLDER = /\{(PH\d+)\}/g;
850
884
  const i18nString = (key, placeholders = emptyObject) => {
@@ -858,149 +892,115 @@ const i18nString = (key, placeholders = emptyObject) => {
858
892
  };
859
893
 
860
894
  // based on https://github.com/microsoft/vscode/blob/bd782eb059e133d3a20fdb446b8feb0010a278ad/src/vs/base/common/date.ts (License MIT)
861
-
862
- /**
863
- * @enum {string}
864
- */
865
- const UiStrings$1 = {
866
- OneSecondAgo: '1 second ago',
867
- SomeSecondsAgo: '{PH1} seconds ago',
868
- OneMinuteAgo: '1 minute ago',
869
- SomeMinutesAgo: '{PH1} minutes ago',
870
- OneHourAgo: '1 hour ago',
871
- SomeHoursAgo: '{PH1} hours ago',
872
- OneDayAgo: '1 day ago',
873
- SomeDaysAgo: '{PH1} days ago',
874
- OneWeekAgo: '1 week ago',
875
- SomeWeeksAgo: '{PH1} weeks ago',
876
- OneMonthAgo: '1 month ago',
877
- SomeMonthsAgo: '{PH1} months ago',
878
- OneYearAgo: '1 year ago',
879
- SomeYearsAgo: '{PH1} years ago',
880
- InOneSecond: 'in 1 second',
881
- InSomeSeconds: 'in {PH1} seconds',
882
- InOneMinute: 'in 1 minute',
883
- InSomeMinutes: 'in {PH1} minutes',
884
- InOneHour: 'in 1 hour',
885
- InSomeHours: 'in {PH1} hours',
886
- InOneDay: 'in 1 day',
887
- InSomeDays: 'in {PH1} days',
888
- InOneWeek: 'in 1 week',
889
- InSomeWeeks: 'in {PH1} weeks',
890
- InOneMonth: 'in 1 month',
891
- InSomeMonths: 'in {PH1} months',
892
- InOneYear: 'in 1 year',
893
- InSomeYears: 'in {PH1} years'
894
- };
895
895
  const oneSecondAgo = () => {
896
- return i18nString(UiStrings$1.OneSecondAgo);
896
+ return i18nString(OneSecondAgo);
897
897
  };
898
898
  const someSecondsAgo = seconds => {
899
- return i18nString(UiStrings$1.SomeSecondsAgo, {
899
+ return i18nString(SomeSecondsAgo, {
900
900
  PH1: seconds
901
901
  });
902
902
  };
903
903
  const oneMinuteAgo = () => {
904
- return i18nString(UiStrings$1.OneMinuteAgo);
904
+ return i18nString(OneMinuteAgo);
905
905
  };
906
906
  const someMinutesAgo = minutes => {
907
- return i18nString(UiStrings$1.SomeMinutesAgo, {
907
+ return i18nString(SomeMinutesAgo, {
908
908
  PH1: minutes
909
909
  });
910
910
  };
911
911
  const oneHourAgo = () => {
912
- return i18nString(UiStrings$1.OneHourAgo);
912
+ return i18nString(OneHourAgo);
913
913
  };
914
914
  const someHoursAgo = hours => {
915
- return i18nString(UiStrings$1.SomeHoursAgo, {
915
+ return i18nString(SomeHoursAgo, {
916
916
  PH1: hours
917
917
  });
918
918
  };
919
919
  const oneDayAgo = () => {
920
- return i18nString(UiStrings$1.OneDayAgo);
920
+ return i18nString(OneDayAgo);
921
921
  };
922
922
  const someDaysAgo = days => {
923
- return i18nString(UiStrings$1.SomeDaysAgo, {
923
+ return i18nString(SomeDaysAgo, {
924
924
  PH1: days
925
925
  });
926
926
  };
927
927
  const oneWeekAgo = () => {
928
- return i18nString(UiStrings$1.OneWeekAgo);
928
+ return i18nString(OneWeekAgo);
929
929
  };
930
930
  const someWeeksAgo = weeks => {
931
- return i18nString(UiStrings$1.SomeWeeksAgo, {
931
+ return i18nString(SomeWeeksAgo, {
932
932
  PH1: weeks
933
933
  });
934
934
  };
935
935
  const oneMonthAgo = () => {
936
- return i18nString(UiStrings$1.OneMonthAgo);
936
+ return i18nString(OneMonthAgo);
937
937
  };
938
938
  const someMonthsAgo = months => {
939
- return i18nString(UiStrings$1.SomeMonthsAgo, {
939
+ return i18nString(SomeMonthsAgo, {
940
940
  PH1: months
941
941
  });
942
942
  };
943
943
  const oneYearAgo = () => {
944
- return i18nString(UiStrings$1.OneYearAgo);
944
+ return i18nString(OneYearAgo);
945
945
  };
946
946
  const someYearsAgo = years => {
947
- return i18nString(UiStrings$1.SomeYearsAgo, {
947
+ return i18nString(SomeYearsAgo, {
948
948
  PH1: years
949
949
  });
950
950
  };
951
951
  const inOneSecond = () => {
952
- return i18nString(UiStrings$1.InOneSecond);
952
+ return i18nString(InOneSecond);
953
953
  };
954
954
  const inSomeSeconds = seconds => {
955
- return i18nString(UiStrings$1.InSomeSeconds, {
955
+ return i18nString(InSomeSeconds, {
956
956
  PH1: seconds
957
957
  });
958
958
  };
959
959
  const inOneMinute = () => {
960
- return i18nString(UiStrings$1.InOneMinute);
960
+ return i18nString(InOneMinute);
961
961
  };
962
962
  const inSomeMinutes = minutes => {
963
- return i18nString(UiStrings$1.InSomeMinutes, {
963
+ return i18nString(InSomeMinutes, {
964
964
  PH1: minutes
965
965
  });
966
966
  };
967
967
  const inOneHour = () => {
968
- return i18nString(UiStrings$1.InOneHour);
968
+ return i18nString(InOneHour);
969
969
  };
970
970
  const inSomeHours = hours => {
971
- return i18nString(UiStrings$1.InSomeHours, {
971
+ return i18nString(InSomeHours, {
972
972
  PH1: hours
973
973
  });
974
974
  };
975
975
  const inOneDay = () => {
976
- return i18nString(UiStrings$1.InOneDay);
976
+ return i18nString(InOneDay);
977
977
  };
978
978
  const inSomeDays = days => {
979
- return i18nString(UiStrings$1.InSomeDays, {
979
+ return i18nString(InSomeDays, {
980
980
  PH1: days
981
981
  });
982
982
  };
983
983
  const inOneWeek = () => {
984
- return i18nString(UiStrings$1.InOneWeek);
984
+ return i18nString(InOneWeek);
985
985
  };
986
986
  const inSomeWeeks = weeks => {
987
- return i18nString(UiStrings$1.InSomeWeeks, {
987
+ return i18nString(InSomeWeeks, {
988
988
  PH1: weeks
989
989
  });
990
990
  };
991
991
  const inOneMonth = () => {
992
- return i18nString(UiStrings$1.InOneMonth);
992
+ return i18nString(InOneMonth);
993
993
  };
994
994
  const inSomeMonths = months => {
995
- return i18nString(UiStrings$1.InSomeMonths, {
995
+ return i18nString(InSomeMonths, {
996
996
  PH1: months
997
997
  });
998
998
  };
999
999
  const inOneYear = () => {
1000
- return i18nString(UiStrings$1.InOneYear);
1000
+ return i18nString(InOneYear);
1001
1001
  };
1002
1002
  const inSomeYears = years => {
1003
- return i18nString(UiStrings$1.InSomeYears, {
1003
+ return i18nString(InSomeYears, {
1004
1004
  PH1: years
1005
1005
  });
1006
1006
  };
@@ -1249,6 +1249,7 @@ const renderLine = (line, index) => {
1249
1249
  }
1250
1250
  return [br, text(line)];
1251
1251
  };
1252
+
1252
1253
  const getAboutContentVirtualDom = lines => {
1253
1254
  const dom = [{
1254
1255
  type: Div,
@@ -1280,6 +1281,7 @@ const getPrimaryButtonVirtualDom = (message, onClick, name) => {
1280
1281
  name
1281
1282
  }, text(message)];
1282
1283
  };
1284
+
1283
1285
  const getSecondaryButtonVirtualDom = (message, onClick, name) => {
1284
1286
  return [{
1285
1287
  type: Button$1,
@@ -1293,8 +1295,8 @@ const getSecondaryButtonVirtualDom = (message, onClick, name) => {
1293
1295
  const DialogIcon = 'DialogIcon';
1294
1296
  const DialogHeading = 'DialogHeading';
1295
1297
 
1296
- const Ok = 'Ok';
1297
- const Copy = 'Copy';
1298
+ const Ok$1 = 'Ok';
1299
+ const Copy$1 = 'Copy';
1298
1300
 
1299
1301
  const Focusable = -1;
1300
1302
 
@@ -1346,7 +1348,7 @@ const getDialogVirtualDom = (content, closeMessage, infoMessage, okMessage, copy
1346
1348
  type: Div,
1347
1349
  className: DialogButtonsRow,
1348
1350
  childCount: 2
1349
- }, ...getSecondaryButtonVirtualDom(okMessage, HandleClickOk, Ok), ...getPrimaryButtonVirtualDom(copyMessage, HandleClickCopy, Copy)];
1351
+ }, ...getSecondaryButtonVirtualDom(okMessage, HandleClickOk, Ok$1), ...getPrimaryButtonVirtualDom(copyMessage, HandleClickCopy, Copy$1)];
1350
1352
  return dom;
1351
1353
  };
1352
1354
 
@@ -1389,43 +1391,34 @@ const loadContent = state => {
1389
1391
  ...state,
1390
1392
  productName: productNameLong$1,
1391
1393
  lines,
1392
- focusId: Ok$1
1394
+ focusId: Ok$2
1393
1395
  };
1394
1396
  };
1395
1397
 
1396
- /**
1397
- * @enum {string}
1398
- */
1399
- const UiStrings = {
1400
- Ok: 'Ok',
1401
- Copy: 'Copy',
1402
- Version: 'Version',
1403
- Commit: 'Commit',
1404
- Date: 'Date',
1405
- Browser: 'Browser',
1406
- Info: 'Info',
1407
- Close: 'Close',
1408
- CloseDialog: 'Close Dialog'
1409
- };
1398
+ const Ok = 'Ok';
1399
+ const Copy = 'Copy';
1400
+ const Info$1 = 'Info';
1401
+ const CloseDialog = 'Close Dialog';
1402
+
1410
1403
  const ok = () => {
1411
- return i18nString(UiStrings.Ok);
1404
+ return i18nString(Ok);
1412
1405
  };
1413
1406
  const copy = () => {
1414
- return i18nString(UiStrings.Copy);
1407
+ return i18nString(Copy);
1415
1408
  };
1416
1409
  const info = () => {
1417
- return i18nString(UiStrings.Info);
1410
+ return i18nString(Info$1);
1418
1411
  };
1419
1412
  const closeDialog = () => {
1420
- return i18nString(UiStrings.CloseDialog);
1413
+ return i18nString(CloseDialog);
1421
1414
  };
1422
1415
 
1423
1416
  const getFocusSelector = focusId => {
1424
1417
  switch (focusId) {
1425
- case Copy$1:
1426
- return Copy;
1427
- case Ok$1:
1428
- return Ok;
1418
+ case Copy$2:
1419
+ return Copy$1;
1420
+ case Ok$2:
1421
+ return Ok$1;
1429
1422
  default:
1430
1423
  return '';
1431
1424
  }
@@ -1464,7 +1457,7 @@ const doRender = (oldState, newState) => {
1464
1457
  return commands;
1465
1458
  };
1466
1459
 
1467
- const renderEventListers = () => {
1460
+ const renderEventListeners = () => {
1468
1461
  return [{
1469
1462
  name: HandleClickOk,
1470
1463
  params: ['handleClickOk']
@@ -1538,7 +1531,7 @@ const commandMap = {
1538
1531
  'About.getVirtualDom': getAboutVirtualDom,
1539
1532
  'About.loadContent': loadContent,
1540
1533
  'About.render': doRender,
1541
- 'About.renderEventListeners': renderEventListers,
1534
+ 'About.renderEventListeners': renderEventListeners,
1542
1535
  'About.showAboutElectron': showAboutElectron
1543
1536
  };
1544
1537