@lvce-editor/test-worker 2.0.0 → 2.2.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.
Files changed (2) hide show
  1. package/dist/testWorkerMain.js +767 -731
  2. package/package.json +1 -1
@@ -1,740 +1,797 @@
1
- const Two = '2.0';
2
- const state$2 = {
3
- callbacks: Object.create(null)
4
- };
5
- const set = (id, fn) => {
6
- state$2.callbacks[id] = fn;
7
- };
8
- const get = id => {
9
- return state$2.callbacks[id];
10
- };
11
- const remove = id => {
12
- delete state$2.callbacks[id];
13
- };
14
- let id = 0;
15
- const create$3 = () => {
16
- return ++id;
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;
17
9
  };
18
- const warn = (...args) => {
19
- console.warn(...args);
10
+ const getCombinedMessage = (error, message) => {
11
+ const stringifiedError = normalizeLine(`${error}`);
12
+ if (message) {
13
+ return `${message}: ${stringifiedError}`;
14
+ }
15
+ return stringifiedError;
20
16
  };
21
- const registerPromise = () => {
22
- const id = create$3();
23
- const {
24
- resolve,
25
- promise
26
- } = Promise.withResolvers();
27
- set(id, resolve);
28
- return {
29
- id,
30
- promise
31
- };
17
+ const NewLine$2 = '\n';
18
+ const getNewLineIndex$1 = (string, startIndex = undefined) => {
19
+ return string.indexOf(NewLine$2, startIndex);
32
20
  };
33
- const resolve = (id, response) => {
34
- const fn = get(id);
35
- if (!fn) {
36
- console.log(response);
37
- warn(`callback ${id} may already be disposed`);
38
- return;
21
+ const mergeStacks = (parent, child) => {
22
+ if (!child) {
23
+ return parent;
39
24
  }
40
- fn(response);
41
- remove(id);
42
- };
43
- const create$2 = (method, params) => {
44
- const {
45
- id,
46
- promise
47
- } = registerPromise();
48
- const message = {
49
- jsonrpc: Two,
50
- method,
51
- params,
52
- id
53
- };
54
- return {
55
- message,
56
- promise
57
- };
25
+ const parentNewLineIndex = getNewLineIndex$1(parent);
26
+ const childNewLineIndex = getNewLineIndex$1(child);
27
+ if (childNewLineIndex === -1) {
28
+ return parent;
29
+ }
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;
35
+ }
36
+ return child;
58
37
  };
59
- class JsonRpcError extends Error {
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
+ }
54
+ }
55
+ }
56
+
57
+ class AssertionError extends Error {
60
58
  constructor(message) {
61
59
  super(message);
62
- this.name = 'JsonRpcError';
60
+ this.name = 'AssertionError';
63
61
  }
64
62
  }
65
- const NewLine$3 = '\n';
66
- const DomException = 'DOMException';
67
- const ReferenceError$1 = 'ReferenceError';
68
- const SyntaxError$1 = 'SyntaxError';
69
- const TypeError$1 = 'TypeError';
70
- const getErrorConstructor = (message, type) => {
71
- if (type) {
72
- switch (type) {
73
- case DomException:
74
- return DOMException;
75
- case TypeError$1:
76
- return TypeError;
77
- case SyntaxError$1:
78
- return SyntaxError;
79
- case ReferenceError$1:
80
- return ReferenceError;
81
- default:
82
- return Error;
83
- }
84
- }
85
- if (message.startsWith('TypeError: ')) {
86
- return TypeError;
63
+ const getType = value => {
64
+ switch (typeof value) {
65
+ case 'number':
66
+ return 'number';
67
+ case 'function':
68
+ return 'function';
69
+ case 'string':
70
+ return 'string';
71
+ case 'object':
72
+ if (value === null) {
73
+ return 'null';
74
+ }
75
+ if (Array.isArray(value)) {
76
+ return 'array';
77
+ }
78
+ return 'object';
79
+ case 'boolean':
80
+ return 'boolean';
81
+ default:
82
+ return 'unknown';
87
83
  }
88
- if (message.startsWith('SyntaxError: ')) {
89
- return SyntaxError;
84
+ };
85
+ const object = value => {
86
+ const type = getType(value);
87
+ if (type !== 'object') {
88
+ throw new AssertionError('expected value to be of type object');
90
89
  }
91
- if (message.startsWith('ReferenceError: ')) {
92
- return ReferenceError;
90
+ };
91
+ const string = value => {
92
+ const type = getType(value);
93
+ if (type !== 'string') {
94
+ throw new AssertionError('expected value to be of type string');
93
95
  }
94
- return Error;
95
96
  };
96
- const constructError = (message, type, name) => {
97
- const ErrorConstructor = getErrorConstructor(message, type);
98
- if (ErrorConstructor === DOMException && name) {
99
- return new ErrorConstructor(message, name);
97
+
98
+ const walkValue = (value, transferrables, isTransferrable) => {
99
+ if (!value) {
100
+ return;
100
101
  }
101
- if (ErrorConstructor === Error) {
102
- const error = new Error(message);
103
- if (name && name !== 'VError') {
104
- error.name = name;
102
+ if (isTransferrable(value)) {
103
+ transferrables.push(value);
104
+ return;
105
+ }
106
+ if (Array.isArray(value)) {
107
+ for (const item of value) {
108
+ walkValue(item, transferrables, isTransferrable);
105
109
  }
106
- return error;
110
+ return;
111
+ }
112
+ if (typeof value === 'object') {
113
+ for (const property of Object.values(value)) {
114
+ walkValue(property, transferrables, isTransferrable);
115
+ }
116
+ return;
107
117
  }
108
- return new ErrorConstructor(message);
109
118
  };
110
- const getNewLineIndex$2 = (string, startIndex = undefined) => {
111
- return string.indexOf(NewLine$3, startIndex);
119
+ const isMessagePort = value => {
120
+ return value && value instanceof MessagePort;
112
121
  };
113
- const getParentStack = error => {
114
- let parentStack = error.stack || error.data || error.message || '';
115
- if (parentStack.startsWith(' at')) {
116
- parentStack = error.message + NewLine$3 + parentStack;
122
+ const isMessagePortMain = value => {
123
+ return value && value.constructor && value.constructor.name === 'MessagePortMain';
124
+ };
125
+ const isOffscreenCanvas = value => {
126
+ return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
127
+ };
128
+ const isInstanceOf = (value, constructorName) => {
129
+ return value?.constructor?.name === constructorName;
130
+ };
131
+ const isSocket = value => {
132
+ return isInstanceOf(value, 'Socket');
133
+ };
134
+ const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
135
+ const isTransferrable = value => {
136
+ for (const fn of transferrables) {
137
+ if (fn(value)) {
138
+ return true;
139
+ }
117
140
  }
118
- return parentStack;
141
+ return false;
142
+ };
143
+ const getTransferrables = value => {
144
+ const transferrables = [];
145
+ walkValue(value, transferrables, isTransferrable);
146
+ return transferrables;
147
+ };
148
+ const attachEvents = that => {
149
+ const handleMessage = (...args) => {
150
+ const data = that.getData(...args);
151
+ that.dispatchEvent(new MessageEvent('message', {
152
+ data
153
+ }));
154
+ };
155
+ that.onMessage(handleMessage);
156
+ const handleClose = event => {
157
+ that.dispatchEvent(new Event('close'));
158
+ };
159
+ that.onClose(handleClose);
119
160
  };
161
+ class Ipc extends EventTarget {
162
+ constructor(rawIpc) {
163
+ super();
164
+ this._rawIpc = rawIpc;
165
+ attachEvents(this);
166
+ }
167
+ }
168
+ const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
169
+ const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
170
+ const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
171
+ const NewLine$1 = '\n';
120
172
  const joinLines$1 = lines => {
121
- return lines.join(NewLine$3);
173
+ return lines.join(NewLine$1);
122
174
  };
123
- const MethodNotFound = -32601;
124
- const Custom = -32001;
125
175
  const splitLines$1 = lines => {
126
- return lines.split(NewLine$3);
176
+ return lines.split(NewLine$1);
127
177
  };
128
- const restoreJsonRpcError = error => {
129
- if (error && error instanceof Error) {
130
- return error;
131
- }
132
- const currentStack = joinLines$1(splitLines$1(new Error().stack || '').slice(1));
133
- if (error && error.code && error.code === MethodNotFound) {
134
- const restoredError = new JsonRpcError(error.message);
135
- const parentStack = getParentStack(error);
136
- restoredError.stack = parentStack + NewLine$3 + currentStack;
137
- return restoredError;
138
- }
139
- if (error && error.message) {
140
- const restoredError = constructError(error.message, error.type, error.name);
141
- if (error.data) {
142
- if (error.data.stack && error.data.type && error.message) {
143
- restoredError.stack = error.data.type + ': ' + error.message + NewLine$3 + error.data.stack + NewLine$3 + currentStack;
144
- } else if (error.data.stack) {
145
- restoredError.stack = error.data.stack;
146
- }
147
- if (error.data.codeFrame) {
148
- // @ts-ignore
149
- restoredError.codeFrame = error.data.codeFrame;
150
- }
151
- if (error.data.code) {
152
- // @ts-ignore
153
- restoredError.code = error.data.code;
154
- }
155
- if (error.data.type) {
156
- // @ts-ignore
157
- restoredError.name = error.data.type;
158
- }
159
- } else {
160
- if (error.stack) {
161
- const lowerStack = restoredError.stack || '';
162
- // @ts-ignore
163
- const indexNewLine = getNewLineIndex$2(lowerStack);
164
- const parentStack = getParentStack(error);
165
- // @ts-ignore
166
- restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
167
- }
168
- if (error.codeFrame) {
169
- // @ts-ignore
170
- restoredError.codeFrame = error.codeFrame;
171
- }
172
- }
173
- return restoredError;
174
- }
175
- if (typeof error === 'string') {
176
- return new Error(`JsonRpc Error: ${error}`);
177
- }
178
- return new Error(`JsonRpc Error: ${error}`);
179
- };
180
- const unwrapJsonRpcResult = responseMessage => {
181
- if ('error' in responseMessage) {
182
- const restoredError = restoreJsonRpcError(responseMessage.error);
183
- throw restoredError;
184
- }
185
- if ('result' in responseMessage) {
186
- return responseMessage.result;
187
- }
188
- throw new JsonRpcError('unexpected response message');
189
- };
190
- const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
191
- const getErrorType = prettyError => {
192
- if (prettyError && prettyError.type) {
193
- return prettyError.type;
194
- }
195
- if (prettyError && prettyError.constructor && prettyError.constructor.name) {
196
- return prettyError.constructor.name;
197
- }
198
- return undefined;
178
+ const isModuleNotFoundMessage = line => {
179
+ return line.includes('[ERR_MODULE_NOT_FOUND]');
199
180
  };
200
- const getErrorProperty = (error, prettyError) => {
201
- if (error && error.code === E_COMMAND_NOT_FOUND) {
202
- return {
203
- code: MethodNotFound,
204
- message: error.message,
205
- data: error.stack
206
- };
207
- }
181
+ const getModuleNotFoundError = stderr => {
182
+ const lines = splitLines$1(stderr);
183
+ const messageIndex = lines.findIndex(isModuleNotFoundMessage);
184
+ const message = lines[messageIndex];
208
185
  return {
209
- code: Custom,
210
- message: prettyError.message,
211
- data: {
212
- stack: prettyError.stack,
213
- codeFrame: prettyError.codeFrame,
214
- type: getErrorType(prettyError),
215
- code: prettyError.code,
216
- name: prettyError.name
217
- }
186
+ message,
187
+ code: ERR_MODULE_NOT_FOUND
218
188
  };
219
189
  };
220
- const create$1$1 = (message, error) => {
221
- return {
222
- jsonrpc: Two,
223
- id: message.id,
224
- error
225
- };
190
+ const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
191
+ const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
192
+ const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
193
+ const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
194
+ const RE_AT = /^\s+at/;
195
+ const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
196
+ const isUnhelpfulNativeModuleError = stderr => {
197
+ return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
226
198
  };
227
- const getErrorResponse = (message, error, preparePrettyError, logError) => {
228
- const prettyError = preparePrettyError(error);
229
- logError(error, prettyError);
230
- const errorProperty = getErrorProperty(error, prettyError);
231
- return create$1$1(message, errorProperty);
199
+ const isMessageCodeBlockStartIndex = line => {
200
+ return RE_MESSAGE_CODE_BLOCK_START.test(line);
201
+ };
202
+ const isMessageCodeBlockEndIndex = line => {
203
+ return RE_MESSAGE_CODE_BLOCK_END.test(line);
232
204
  };
233
- const create$4 = (message, result) => {
205
+ const getMessageCodeBlock = stderr => {
206
+ const lines = splitLines$1(stderr);
207
+ const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
208
+ const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
209
+ const relevantLines = lines.slice(startIndex, endIndex);
210
+ const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
211
+ return relevantMessage;
212
+ };
213
+ const getNativeModuleErrorMessage = stderr => {
214
+ const message = getMessageCodeBlock(stderr);
234
215
  return {
235
- jsonrpc: Two,
236
- id: message.id,
237
- result: result ?? null
216
+ message: `Incompatible native node module: ${message}`,
217
+ code: E_INCOMPATIBLE_NATIVE_MODULE
238
218
  };
239
219
  };
240
- const getSuccessResponse = (message, result) => {
241
- const resultProperty = result ?? null;
242
- return create$4(message, resultProperty);
243
- };
244
- const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
245
- try {
246
- const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
247
- return getSuccessResponse(message, result);
248
- } catch (error) {
249
- return getErrorResponse(message, error, preparePrettyError, logError);
220
+ const isModulesSyntaxError = stderr => {
221
+ if (!stderr) {
222
+ return false;
250
223
  }
224
+ return stderr.includes('SyntaxError: Cannot use import statement outside a module');
251
225
  };
252
- const defaultPreparePrettyError = error => {
253
- return error;
226
+ const getModuleSyntaxError = () => {
227
+ return {
228
+ message: `ES Modules are not supported in electron`,
229
+ code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
230
+ };
254
231
  };
255
- const defaultLogError = () => {
256
- // ignore
232
+ const isModuleNotFoundError = stderr => {
233
+ if (!stderr) {
234
+ return false;
235
+ }
236
+ return stderr.includes('ERR_MODULE_NOT_FOUND');
257
237
  };
258
- const defaultRequiresSocket = () => {
259
- return false;
238
+ const isNormalStackLine = line => {
239
+ return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
260
240
  };
261
- const defaultResolve = resolve;
262
-
263
- // TODO maybe remove this in v6 or v7, only accept options object to simplify the code
264
- const normalizeParams = args => {
265
- if (args.length === 1) {
266
- const options = args[0];
241
+ const getDetails = lines => {
242
+ const index = lines.findIndex(isNormalStackLine);
243
+ if (index === -1) {
267
244
  return {
268
- ipc: options.ipc,
269
- message: options.message,
270
- execute: options.execute,
271
- resolve: options.resolve || defaultResolve,
272
- preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
273
- logError: options.logError || defaultLogError,
274
- requiresSocket: options.requiresSocket || defaultRequiresSocket
245
+ actualMessage: joinLines$1(lines),
246
+ rest: []
275
247
  };
276
248
  }
249
+ let lastIndex = index - 1;
250
+ while (++lastIndex < lines.length) {
251
+ if (!isNormalStackLine(lines[lastIndex])) {
252
+ break;
253
+ }
254
+ }
277
255
  return {
278
- ipc: args[0],
279
- message: args[1],
280
- execute: args[2],
281
- resolve: args[3],
282
- preparePrettyError: args[4],
283
- logError: args[5],
284
- requiresSocket: args[6]
256
+ actualMessage: lines[index - 1],
257
+ rest: lines.slice(index, lastIndex)
285
258
  };
286
259
  };
287
- const handleJsonRpcMessage = async (...args) => {
288
- const options = normalizeParams(args);
260
+ const getHelpfulChildProcessError = (stdout, stderr) => {
261
+ if (isUnhelpfulNativeModuleError(stderr)) {
262
+ return getNativeModuleErrorMessage(stderr);
263
+ }
264
+ if (isModulesSyntaxError(stderr)) {
265
+ return getModuleSyntaxError();
266
+ }
267
+ if (isModuleNotFoundError(stderr)) {
268
+ return getModuleNotFoundError(stderr);
269
+ }
270
+ const lines = splitLines$1(stderr);
289
271
  const {
290
- message,
291
- ipc,
292
- execute,
293
- resolve,
294
- preparePrettyError,
295
- logError,
296
- requiresSocket
297
- } = options;
298
- if ('id' in message) {
299
- if ('method' in message) {
300
- const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
301
- try {
302
- ipc.send(response);
303
- } catch (error) {
304
- const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
305
- ipc.send(errorResponse);
306
- }
307
- return;
272
+ actualMessage,
273
+ rest
274
+ } = getDetails(lines);
275
+ return {
276
+ message: `${actualMessage}`,
277
+ code: '',
278
+ stack: rest
279
+ };
280
+ };
281
+ class IpcError extends VError {
282
+ // @ts-ignore
283
+ constructor(betterMessage, stdout = '', stderr = '') {
284
+ if (stdout || stderr) {
285
+ // @ts-ignore
286
+ const {
287
+ message,
288
+ code,
289
+ stack
290
+ } = getHelpfulChildProcessError(stdout, stderr);
291
+ const cause = new Error(message);
292
+ // @ts-ignore
293
+ cause.code = code;
294
+ cause.stack = stack;
295
+ super(cause, betterMessage);
296
+ } else {
297
+ super(betterMessage);
308
298
  }
309
- resolve(message.id, message);
310
- return;
299
+ // @ts-ignore
300
+ this.name = 'IpcError';
301
+ // @ts-ignore
302
+ this.stdout = stdout;
303
+ // @ts-ignore
304
+ this.stderr = stderr;
311
305
  }
312
- if ('method' in message) {
313
- await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
314
- return;
306
+ }
307
+ const readyMessage = 'ready';
308
+ const getData$2 = event => {
309
+ return event.data;
310
+ };
311
+ const listen$7 = () => {
312
+ // @ts-ignore
313
+ if (typeof WorkerGlobalScope === 'undefined') {
314
+ throw new TypeError('module is not in web worker scope');
315
315
  }
316
- throw new JsonRpcError('unexpected message');
316
+ return globalThis;
317
317
  };
318
- const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
319
- const {
320
- message,
321
- promise
322
- } = create$2(method, params);
323
- if (useSendAndTransfer && ipc.sendAndTransfer) {
324
- ipc.sendAndTransfer(message);
325
- } else {
326
- ipc.send(message);
327
- }
328
- const responseMessage = await promise;
329
- return unwrapJsonRpcResult(responseMessage);
330
- };
331
- const invoke$1 = (ipc, method, ...params) => {
332
- return invokeHelper(ipc, method, params, false);
333
- };
334
- const invokeAndTransfer$1 = (ipc, method, ...params) => {
335
- return invokeHelper(ipc, method, params, true);
336
- };
337
-
338
- const commands = Object.create(null);
339
- const register = commandMap => {
340
- Object.assign(commands, commandMap);
341
- };
342
- const getCommand = key => {
343
- return commands[key];
318
+ const signal$7 = global => {
319
+ global.postMessage(readyMessage);
344
320
  };
345
- const execute$3 = (command, ...args) => {
346
- const fn = getCommand(command);
347
- if (!fn) {
348
- throw new Error(`command not found ${command}`);
321
+ class IpcChildWithModuleWorker extends Ipc {
322
+ getData(event) {
323
+ return getData$2(event);
349
324
  }
350
- return fn(...args);
351
- };
352
-
353
- const getData$1 = event => {
354
- return event.data;
355
- };
356
- const attachEvents = that => {
357
- const handleMessage = (...args) => {
358
- const data = that.getData(...args);
359
- that.dispatchEvent(new MessageEvent('message', {
360
- data
361
- }));
362
- };
363
- that.onMessage(handleMessage);
364
- const handleClose = event => {
365
- that.dispatchEvent(new Event('close'));
366
- };
367
- that.onClose(handleClose);
368
- };
369
- class Ipc extends EventTarget {
370
- constructor(rawIpc) {
371
- super();
372
- this._rawIpc = rawIpc;
373
- attachEvents(this);
325
+ send(message) {
326
+ // @ts-ignore
327
+ this._rawIpc.postMessage(message);
374
328
  }
375
- }
376
- const readyMessage = 'ready';
377
- const walkValue = (value, transferrables, isTransferrable) => {
378
- if (!value) {
379
- return;
329
+ sendAndTransfer(message) {
330
+ const transfer = getTransferrables(message);
331
+ // @ts-ignore
332
+ this._rawIpc.postMessage(message, transfer);
380
333
  }
381
- if (isTransferrable(value)) {
382
- transferrables.push(value);
383
- return;
334
+ dispose() {
335
+ // ignore
384
336
  }
385
- if (Array.isArray(value)) {
386
- for (const item of value) {
387
- walkValue(item, transferrables, isTransferrable);
388
- }
389
- return;
337
+ onClose(callback) {
338
+ // ignore
390
339
  }
391
- if (typeof value === 'object') {
392
- for (const property of Object.values(value)) {
393
- walkValue(property, transferrables, isTransferrable);
394
- }
395
- return;
340
+ onMessage(callback) {
341
+ this._rawIpc.addEventListener('message', callback);
396
342
  }
343
+ }
344
+ const wrap$e = global => {
345
+ return new IpcChildWithModuleWorker(global);
397
346
  };
398
- const isMessagePort = value => {
399
- return value && value instanceof MessagePort;
400
- };
401
- const isMessagePortMain = value => {
402
- return value && value.constructor && value.constructor.name === 'MessagePortMain';
403
- };
404
- const isOffscreenCanvas = value => {
405
- return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
406
- };
407
- const isInstanceOf = (value, constructorName) => {
408
- return value?.constructor?.name === constructorName;
347
+ const withResolvers = () => {
348
+ let _resolve;
349
+ const promise = new Promise(resolve => {
350
+ _resolve = resolve;
351
+ });
352
+ return {
353
+ resolve: _resolve,
354
+ promise
355
+ };
409
356
  };
410
- const isSocket = value => {
411
- return isInstanceOf(value, 'Socket');
357
+ const waitForFirstMessage = async port => {
358
+ const {
359
+ resolve,
360
+ promise
361
+ } = withResolvers();
362
+ port.addEventListener('message', resolve, {
363
+ once: true
364
+ });
365
+ const event = await promise;
366
+ // @ts-ignore
367
+ return event.data;
412
368
  };
413
- const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
414
- const isTransferrable = value => {
415
- for (const fn of transferrables) {
416
- if (fn(value)) {
417
- return true;
418
- }
369
+ const listen$6 = async () => {
370
+ const parentIpcRaw = listen$7();
371
+ signal$7(parentIpcRaw);
372
+ const parentIpc = wrap$e(parentIpcRaw);
373
+ const firstMessage = await waitForFirstMessage(parentIpc);
374
+ if (firstMessage.method !== 'initialize') {
375
+ throw new IpcError('unexpected first message');
419
376
  }
420
- return false;
421
- };
422
- const getTransferrables = value => {
423
- const transferrables = [];
424
- walkValue(value, transferrables, isTransferrable);
425
- return transferrables;
426
- };
427
- const listen$2$1 = () => {
428
- // @ts-ignore
429
- if (typeof WorkerGlobalScope === 'undefined') {
430
- throw new TypeError('module is not in web worker scope');
377
+ const type = firstMessage.params[0];
378
+ if (type === 'message-port') {
379
+ parentIpc.send({
380
+ jsonrpc: '2.0',
381
+ id: firstMessage.id,
382
+ result: null
383
+ });
384
+ parentIpc.dispose();
385
+ const port = firstMessage.params[1];
386
+ return port;
431
387
  }
432
388
  return globalThis;
433
389
  };
434
- const signal$2 = global => {
435
- global.postMessage(readyMessage);
436
- };
437
- class IpcChildWithModuleWorker extends Ipc {
390
+ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
391
+ constructor(port) {
392
+ super(port);
393
+ }
438
394
  getData(event) {
439
- return getData$1(event);
395
+ return getData$2(event);
440
396
  }
441
397
  send(message) {
442
- // @ts-ignore
443
398
  this._rawIpc.postMessage(message);
444
399
  }
445
400
  sendAndTransfer(message) {
446
401
  const transfer = getTransferrables(message);
447
- // @ts-ignore
448
402
  this._rawIpc.postMessage(message, transfer);
449
403
  }
450
404
  dispose() {
451
- // ignore
405
+ if (this._rawIpc.close) {
406
+ this._rawIpc.close();
407
+ }
452
408
  }
453
409
  onClose(callback) {
454
410
  // ignore
455
411
  }
456
412
  onMessage(callback) {
457
413
  this._rawIpc.addEventListener('message', callback);
414
+ this._rawIpc.start();
458
415
  }
459
416
  }
460
- const wrap$5 = global => {
461
- return new IpcChildWithModuleWorker(global);
462
- };
463
- const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
464
- const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
465
- const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
466
- const NewLine$1 = '\n';
467
- const joinLines = lines => {
468
- return lines.join(NewLine$1);
469
- };
470
- const splitLines = lines => {
471
- return lines.split(NewLine$1);
417
+ const wrap$d = port => {
418
+ return new IpcChildWithModuleWorkerAndMessagePort(port);
472
419
  };
473
- const isModuleNotFoundMessage = line => {
474
- return line.includes('[ERR_MODULE_NOT_FOUND]');
420
+ const IpcChildWithModuleWorkerAndMessagePort$1 = {
421
+ __proto__: null,
422
+ listen: listen$6,
423
+ wrap: wrap$d
475
424
  };
476
- const getModuleNotFoundError = stderr => {
477
- const lines = splitLines(stderr);
478
- const messageIndex = lines.findIndex(isModuleNotFoundMessage);
479
- const message = lines[messageIndex];
425
+
426
+ const Two = '2.0';
427
+ const create$4 = (method, params) => {
480
428
  return {
481
- message,
482
- code: ERR_MODULE_NOT_FOUND
429
+ jsonrpc: Two,
430
+ method,
431
+ params
483
432
  };
484
433
  };
485
- const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
486
- const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
487
- const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
488
- const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
489
- const RE_AT = /^\s+at/;
490
- const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
491
- const isUnhelpfulNativeModuleError = stderr => {
492
- return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
493
- };
494
- const isMessageCodeBlockStartIndex = line => {
495
- return RE_MESSAGE_CODE_BLOCK_START.test(line);
434
+ const callbacks = Object.create(null);
435
+ const set = (id, fn) => {
436
+ callbacks[id] = fn;
496
437
  };
497
- const isMessageCodeBlockEndIndex = line => {
498
- return RE_MESSAGE_CODE_BLOCK_END.test(line);
438
+ const get = id => {
439
+ return callbacks[id];
499
440
  };
500
- const getMessageCodeBlock = stderr => {
501
- const lines = splitLines(stderr);
502
- const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
503
- const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
504
- const relevantLines = lines.slice(startIndex, endIndex);
505
- const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
506
- return relevantMessage;
441
+ const remove = id => {
442
+ delete callbacks[id];
507
443
  };
508
- const getNativeModuleErrorMessage = stderr => {
509
- const message = getMessageCodeBlock(stderr);
510
- return {
511
- message: `Incompatible native node module: ${message}`,
512
- code: E_INCOMPATIBLE_NATIVE_MODULE
513
- };
444
+ let id = 0;
445
+ const create$3 = () => {
446
+ return ++id;
514
447
  };
515
- const isModulesSyntaxError = stderr => {
516
- if (!stderr) {
517
- return false;
518
- }
519
- return stderr.includes('SyntaxError: Cannot use import statement outside a module');
448
+ const warn = (...args) => {
449
+ console.warn(...args);
520
450
  };
521
- const getModuleSyntaxError = () => {
451
+ const registerPromise = () => {
452
+ const id = create$3();
453
+ const {
454
+ resolve,
455
+ promise
456
+ } = Promise.withResolvers();
457
+ set(id, resolve);
522
458
  return {
523
- message: `ES Modules are not supported in electron`,
524
- code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
459
+ id,
460
+ promise
525
461
  };
526
462
  };
527
- const isModuleNotFoundError = stderr => {
528
- if (!stderr) {
529
- return false;
463
+ const resolve = (id, response) => {
464
+ const fn = get(id);
465
+ if (!fn) {
466
+ console.log(response);
467
+ warn(`callback ${id} may already be disposed`);
468
+ return;
530
469
  }
531
- return stderr.includes('ERR_MODULE_NOT_FOUND');
532
- };
533
- const isNormalStackLine = line => {
534
- return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
470
+ fn(response);
471
+ remove(id);
535
472
  };
536
- const getDetails = lines => {
537
- const index = lines.findIndex(isNormalStackLine);
538
- if (index === -1) {
539
- return {
540
- actualMessage: joinLines(lines),
541
- rest: []
542
- };
543
- }
544
- let lastIndex = index - 1;
545
- while (++lastIndex < lines.length) {
546
- if (!isNormalStackLine(lines[lastIndex])) {
547
- break;
548
- }
549
- }
473
+ const create$2 = (method, params) => {
474
+ const {
475
+ id,
476
+ promise
477
+ } = registerPromise();
478
+ const message = {
479
+ jsonrpc: Two,
480
+ method,
481
+ params,
482
+ id
483
+ };
550
484
  return {
551
- actualMessage: lines[index - 1],
552
- rest: lines.slice(index, lastIndex)
485
+ message,
486
+ promise
553
487
  };
554
488
  };
555
- const getHelpfulChildProcessError = (stdout, stderr) => {
556
- if (isUnhelpfulNativeModuleError(stderr)) {
557
- return getNativeModuleErrorMessage(stderr);
489
+ class JsonRpcError extends Error {
490
+ constructor(message) {
491
+ super(message);
492
+ this.name = 'JsonRpcError';
558
493
  }
559
- if (isModulesSyntaxError(stderr)) {
560
- return getModuleSyntaxError();
494
+ }
495
+ const NewLine = '\n';
496
+ const DomException = 'DOMException';
497
+ const ReferenceError$1 = 'ReferenceError';
498
+ const SyntaxError$1 = 'SyntaxError';
499
+ const TypeError$1 = 'TypeError';
500
+ const getErrorConstructor = (message, type) => {
501
+ if (type) {
502
+ switch (type) {
503
+ case DomException:
504
+ return DOMException;
505
+ case TypeError$1:
506
+ return TypeError;
507
+ case SyntaxError$1:
508
+ return SyntaxError;
509
+ case ReferenceError$1:
510
+ return ReferenceError;
511
+ default:
512
+ return Error;
513
+ }
561
514
  }
562
- if (isModuleNotFoundError(stderr)) {
563
- return getModuleNotFoundError(stderr);
515
+ if (message.startsWith('TypeError: ')) {
516
+ return TypeError;
564
517
  }
565
- const lines = splitLines(stderr);
566
- const {
567
- actualMessage,
568
- rest
569
- } = getDetails(lines);
570
- return {
571
- message: `${actualMessage}`,
572
- code: '',
573
- stack: rest
574
- };
518
+ if (message.startsWith('SyntaxError: ')) {
519
+ return SyntaxError;
520
+ }
521
+ if (message.startsWith('ReferenceError: ')) {
522
+ return ReferenceError;
523
+ }
524
+ return Error;
575
525
  };
576
- const normalizeLine$1 = line => {
577
- if (line.startsWith('Error: ')) {
578
- return line.slice('Error: '.length);
526
+ const constructError = (message, type, name) => {
527
+ const ErrorConstructor = getErrorConstructor(message, type);
528
+ if (ErrorConstructor === DOMException && name) {
529
+ return new ErrorConstructor(message, name);
579
530
  }
580
- if (line.startsWith('VError: ')) {
581
- return line.slice('VError: '.length);
531
+ if (ErrorConstructor === Error) {
532
+ const error = new Error(message);
533
+ if (name && name !== 'VError') {
534
+ error.name = name;
535
+ }
536
+ return error;
582
537
  }
583
- return line;
538
+ return new ErrorConstructor(message);
584
539
  };
585
- const getCombinedMessage$1 = (error, message) => {
586
- const stringifiedError = normalizeLine$1(`${error}`);
587
- if (message) {
588
- return `${message}: ${stringifiedError}`;
540
+ const getNewLineIndex = (string, startIndex = undefined) => {
541
+ return string.indexOf(NewLine, startIndex);
542
+ };
543
+ const getParentStack = error => {
544
+ let parentStack = error.stack || error.data || error.message || '';
545
+ if (parentStack.startsWith(' at')) {
546
+ parentStack = error.message + NewLine + parentStack;
589
547
  }
590
- return stringifiedError;
548
+ return parentStack;
591
549
  };
592
- const NewLine$2 = '\n';
593
- const getNewLineIndex$1 = (string, startIndex = undefined) => {
594
- return string.indexOf(NewLine$2, startIndex);
550
+ const joinLines = lines => {
551
+ return lines.join(NewLine);
595
552
  };
596
- const mergeStacks$1 = (parent, child) => {
597
- if (!child) {
598
- return parent;
553
+ const MethodNotFound = -32601;
554
+ const Custom = -32001;
555
+ const splitLines = lines => {
556
+ return lines.split(NewLine);
557
+ };
558
+ const restoreJsonRpcError = error => {
559
+ if (error && error instanceof Error) {
560
+ return error;
599
561
  }
600
- const parentNewLineIndex = getNewLineIndex$1(parent);
601
- const childNewLineIndex = getNewLineIndex$1(child);
602
- if (childNewLineIndex === -1) {
603
- return parent;
562
+ const currentStack = joinLines(splitLines(new Error().stack || '').slice(1));
563
+ if (error && error.code && error.code === MethodNotFound) {
564
+ const restoredError = new JsonRpcError(error.message);
565
+ const parentStack = getParentStack(error);
566
+ restoredError.stack = parentStack + NewLine + currentStack;
567
+ return restoredError;
604
568
  }
605
- const parentFirstLine = parent.slice(0, parentNewLineIndex);
606
- const childRest = child.slice(childNewLineIndex);
607
- const childFirstLine = normalizeLine$1(child.slice(0, childNewLineIndex));
608
- if (parentFirstLine.includes(childFirstLine)) {
609
- return parentFirstLine + childRest;
569
+ if (error && error.message) {
570
+ const restoredError = constructError(error.message, error.type, error.name);
571
+ if (error.data) {
572
+ if (error.data.stack && error.data.type && error.message) {
573
+ restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
574
+ } else if (error.data.stack) {
575
+ restoredError.stack = error.data.stack;
576
+ }
577
+ if (error.data.codeFrame) {
578
+ // @ts-ignore
579
+ restoredError.codeFrame = error.data.codeFrame;
580
+ }
581
+ if (error.data.code) {
582
+ // @ts-ignore
583
+ restoredError.code = error.data.code;
584
+ }
585
+ if (error.data.type) {
586
+ // @ts-ignore
587
+ restoredError.name = error.data.type;
588
+ }
589
+ } else {
590
+ if (error.stack) {
591
+ const lowerStack = restoredError.stack || '';
592
+ // @ts-ignore
593
+ const indexNewLine = getNewLineIndex(lowerStack);
594
+ const parentStack = getParentStack(error);
595
+ // @ts-ignore
596
+ restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
597
+ }
598
+ if (error.codeFrame) {
599
+ // @ts-ignore
600
+ restoredError.codeFrame = error.codeFrame;
601
+ }
602
+ }
603
+ return restoredError;
610
604
  }
611
- return child;
605
+ if (typeof error === 'string') {
606
+ return new Error(`JsonRpc Error: ${error}`);
607
+ }
608
+ return new Error(`JsonRpc Error: ${error}`);
612
609
  };
613
- let VError$1 = class VError extends Error {
614
- constructor(error, message) {
615
- const combinedMessage = getCombinedMessage$1(error, message);
616
- super(combinedMessage);
617
- this.name = 'VError';
618
- if (error instanceof Error) {
619
- this.stack = mergeStacks$1(this.stack, error.stack);
620
- }
621
- if (error.codeFrame) {
622
- // @ts-ignore
623
- this.codeFrame = error.codeFrame;
624
- }
625
- if (error.code) {
626
- // @ts-ignore
627
- this.code = error.code;
628
- }
610
+ const unwrapJsonRpcResult = responseMessage => {
611
+ if ('error' in responseMessage) {
612
+ const restoredError = restoreJsonRpcError(responseMessage.error);
613
+ throw restoredError;
614
+ }
615
+ if ('result' in responseMessage) {
616
+ return responseMessage.result;
629
617
  }
618
+ throw new JsonRpcError('unexpected response message');
630
619
  };
631
- class IpcError extends VError$1 {
632
- // @ts-ignore
633
- constructor(betterMessage, stdout = '', stderr = '') {
634
- if (stdout || stderr) {
635
- // @ts-ignore
636
- const {
637
- message,
638
- code,
639
- stack
640
- } = getHelpfulChildProcessError(stdout, stderr);
641
- const cause = new Error(message);
642
- // @ts-ignore
643
- cause.code = code;
644
- cause.stack = stack;
645
- super(cause, betterMessage);
646
- } else {
647
- super(betterMessage);
648
- }
649
- // @ts-ignore
650
- this.name = 'IpcError';
651
- // @ts-ignore
652
- this.stdout = stdout;
653
- // @ts-ignore
654
- this.stderr = stderr;
620
+ const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
621
+ const getErrorType = prettyError => {
622
+ if (prettyError && prettyError.type) {
623
+ return prettyError.type;
624
+ }
625
+ if (prettyError && prettyError.constructor && prettyError.constructor.name) {
626
+ return prettyError.constructor.name;
627
+ }
628
+ return undefined;
629
+ };
630
+ const getErrorProperty = (error, prettyError) => {
631
+ if (error && error.code === E_COMMAND_NOT_FOUND) {
632
+ return {
633
+ code: MethodNotFound,
634
+ message: error.message,
635
+ data: error.stack
636
+ };
655
637
  }
656
- }
657
- const withResolvers = () => {
658
- let _resolve;
659
- const promise = new Promise(resolve => {
660
- _resolve = resolve;
661
- });
662
638
  return {
663
- resolve: _resolve,
664
- promise
639
+ code: Custom,
640
+ message: prettyError.message,
641
+ data: {
642
+ stack: prettyError.stack,
643
+ codeFrame: prettyError.codeFrame,
644
+ type: getErrorType(prettyError),
645
+ code: prettyError.code,
646
+ name: prettyError.name
647
+ }
665
648
  };
666
649
  };
667
- const waitForFirstMessage = async port => {
668
- const {
669
- resolve,
670
- promise
671
- } = withResolvers();
672
- port.addEventListener('message', resolve, {
673
- once: true
674
- });
675
- const event = await promise;
676
- // @ts-ignore
677
- return event.data;
650
+ const create$1$1 = (message, error) => {
651
+ return {
652
+ jsonrpc: Two,
653
+ id: message.id,
654
+ error
655
+ };
678
656
  };
679
- const listen$1$1 = async () => {
680
- const parentIpcRaw = listen$2$1();
681
- signal$2(parentIpcRaw);
682
- const parentIpc = wrap$5(parentIpcRaw);
683
- const firstMessage = await waitForFirstMessage(parentIpc);
684
- if (firstMessage.method !== 'initialize') {
685
- throw new IpcError('unexpected first message');
657
+ const getErrorResponse = (message, error, preparePrettyError, logError) => {
658
+ const prettyError = preparePrettyError(error);
659
+ logError(error, prettyError);
660
+ const errorProperty = getErrorProperty(error, prettyError);
661
+ return create$1$1(message, errorProperty);
662
+ };
663
+ const create$5 = (message, result) => {
664
+ return {
665
+ jsonrpc: Two,
666
+ id: message.id,
667
+ result: result ?? null
668
+ };
669
+ };
670
+ const getSuccessResponse = (message, result) => {
671
+ const resultProperty = result ?? null;
672
+ return create$5(message, resultProperty);
673
+ };
674
+ const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
675
+ try {
676
+ const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
677
+ return getSuccessResponse(message, result);
678
+ } catch (error) {
679
+ return getErrorResponse(message, error, preparePrettyError, logError);
686
680
  }
687
- const type = firstMessage.params[0];
688
- if (type === 'message-port') {
689
- parentIpc.send({
690
- jsonrpc: '2.0',
691
- id: firstMessage.id,
692
- result: null
693
- });
694
- parentIpc.dispose();
695
- const port = firstMessage.params[1];
696
- return port;
681
+ };
682
+ const defaultPreparePrettyError = error => {
683
+ return error;
684
+ };
685
+ const defaultLogError = () => {
686
+ // ignore
687
+ };
688
+ const defaultRequiresSocket = () => {
689
+ return false;
690
+ };
691
+ const defaultResolve = resolve;
692
+
693
+ // TODO maybe remove this in v6 or v7, only accept options object to simplify the code
694
+ const normalizeParams = args => {
695
+ if (args.length === 1) {
696
+ const options = args[0];
697
+ return {
698
+ ipc: options.ipc,
699
+ message: options.message,
700
+ execute: options.execute,
701
+ resolve: options.resolve || defaultResolve,
702
+ preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
703
+ logError: options.logError || defaultLogError,
704
+ requiresSocket: options.requiresSocket || defaultRequiresSocket
705
+ };
697
706
  }
698
- return globalThis;
707
+ return {
708
+ ipc: args[0],
709
+ message: args[1],
710
+ execute: args[2],
711
+ resolve: args[3],
712
+ preparePrettyError: args[4],
713
+ logError: args[5],
714
+ requiresSocket: args[6]
715
+ };
699
716
  };
700
- class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
701
- constructor(port) {
702
- super(port);
703
- }
704
- getData(event) {
705
- return getData$1(event);
706
- }
707
- send(message) {
708
- this._rawIpc.postMessage(message);
709
- }
710
- sendAndTransfer(message) {
711
- const transfer = getTransferrables(message);
712
- this._rawIpc.postMessage(message, transfer);
713
- }
714
- dispose() {
715
- if (this._rawIpc.close) {
716
- this._rawIpc.close();
717
+ const handleJsonRpcMessage = async (...args) => {
718
+ const options = normalizeParams(args);
719
+ const {
720
+ message,
721
+ ipc,
722
+ execute,
723
+ resolve,
724
+ preparePrettyError,
725
+ logError,
726
+ requiresSocket
727
+ } = options;
728
+ if ('id' in message) {
729
+ if ('method' in message) {
730
+ const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
731
+ try {
732
+ ipc.send(response);
733
+ } catch (error) {
734
+ const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
735
+ ipc.send(errorResponse);
736
+ }
737
+ return;
717
738
  }
739
+ resolve(message.id, message);
740
+ return;
718
741
  }
719
- onClose(callback) {
720
- // ignore
742
+ if ('method' in message) {
743
+ await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
744
+ return;
721
745
  }
722
- onMessage(callback) {
723
- this._rawIpc.addEventListener('message', callback);
724
- this._rawIpc.start();
746
+ throw new JsonRpcError('unexpected message');
747
+ };
748
+ const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
749
+ const {
750
+ message,
751
+ promise
752
+ } = create$2(method, params);
753
+ if (useSendAndTransfer && ipc.sendAndTransfer) {
754
+ ipc.sendAndTransfer(message);
755
+ } else {
756
+ ipc.send(message);
725
757
  }
726
- }
727
- const wrap$4 = port => {
728
- return new IpcChildWithModuleWorkerAndMessagePort(port);
758
+ const responseMessage = await promise;
759
+ return unwrapJsonRpcResult(responseMessage);
729
760
  };
730
- const IpcChildWithModuleWorkerAndMessagePort$1 = {
731
- __proto__: null,
732
- listen: listen$1$1,
733
- wrap: wrap$4
761
+ const send = (transport, method, ...params) => {
762
+ const message = create$4(method, params);
763
+ transport.send(message);
764
+ };
765
+ const invoke$1 = (ipc, method, ...params) => {
766
+ return invokeHelper(ipc, method, params, false);
767
+ };
768
+ const invokeAndTransfer$1 = (ipc, method, ...params) => {
769
+ return invokeHelper(ipc, method, params, true);
770
+ };
771
+
772
+ const commands = Object.create(null);
773
+ const register = commandMap => {
774
+ Object.assign(commands, commandMap);
775
+ };
776
+ const getCommand = key => {
777
+ return commands[key];
778
+ };
779
+ const execute$3 = (command, ...args) => {
780
+ const fn = getCommand(command);
781
+ if (!fn) {
782
+ throw new Error(`command not found ${command}`);
783
+ }
784
+ return fn(...args);
734
785
  };
735
786
 
736
787
  const createRpc = ipc => {
737
788
  const rpc = {
789
+ /**
790
+ * @deprecated
791
+ */
792
+ send(method, ...params) {
793
+ send(ipc, method, ...params);
794
+ },
738
795
  invoke(method, ...params) {
739
796
  return invoke$1(ipc, method, ...params);
740
797
  },
@@ -754,16 +811,22 @@ const logError$1 = () => {
754
811
  // handled by renderer worker
755
812
  };
756
813
  const handleMessage = event => {
757
- return handleJsonRpcMessage(event.target, event.data, execute$3, resolve, preparePrettyError$1, logError$1, requiresSocket$1);
814
+ const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket$1;
815
+ return handleJsonRpcMessage(event.target, event.data, execute$3, resolve, preparePrettyError$1, logError$1, actualRequiresSocket);
758
816
  };
759
817
  const handleIpc = ipc => {
760
- ipc.addEventListener('message', handleMessage);
818
+ if ('addEventListener' in ipc) {
819
+ ipc.addEventListener('message', handleMessage);
820
+ } else if ('on' in ipc) {
821
+ // deprecated
822
+ ipc.on('message', handleMessage);
823
+ }
761
824
  };
762
-
763
- // @ts-ignore
764
- const listen$2 = async () => {
765
- const module = IpcChildWithModuleWorkerAndMessagePort$1;
766
- const rawIpc = await module.listen();
825
+ const listen$2 = async (module, options) => {
826
+ const rawIpc = await module.listen(options);
827
+ if (module.signal) {
828
+ module.signal(rawIpc);
829
+ }
767
830
  const ipc = module.wrap(rawIpc);
768
831
  return ipc;
769
832
  };
@@ -772,7 +835,7 @@ const create$1 = async ({
772
835
  }) => {
773
836
  // TODO create a commandMap per rpc instance
774
837
  register(commandMap);
775
- const ipc = await listen$2();
838
+ const ipc = await listen$2(IpcChildWithModuleWorkerAndMessagePort$1);
776
839
  handleIpc(ipc);
777
840
  const rpc = createRpc(ipc);
778
841
  return rpc;
@@ -816,62 +879,6 @@ const now = () => {
816
879
  return performance.now();
817
880
  };
818
881
 
819
- const normalizeLine = line => {
820
- if (line.startsWith('Error: ')) {
821
- return line.slice('Error: '.length);
822
- }
823
- if (line.startsWith('VError: ')) {
824
- return line.slice('VError: '.length);
825
- }
826
- return line;
827
- };
828
- const getCombinedMessage = (error, message) => {
829
- const stringifiedError = normalizeLine(`${error}`);
830
- if (message) {
831
- return `${message}: ${stringifiedError}`;
832
- }
833
- return stringifiedError;
834
- };
835
- const NewLine = '\n';
836
- const getNewLineIndex = (string, startIndex = undefined) => {
837
- return string.indexOf(NewLine, startIndex);
838
- };
839
- const mergeStacks = (parent, child) => {
840
- if (!child) {
841
- return parent;
842
- }
843
- const parentNewLineIndex = getNewLineIndex(parent);
844
- const childNewLineIndex = getNewLineIndex(child);
845
- if (childNewLineIndex === -1) {
846
- return parent;
847
- }
848
- const parentFirstLine = parent.slice(0, parentNewLineIndex);
849
- const childRest = child.slice(childNewLineIndex);
850
- const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
851
- if (parentFirstLine.includes(childFirstLine)) {
852
- return parentFirstLine + childRest;
853
- }
854
- return child;
855
- };
856
- class VError extends Error {
857
- constructor(error, message) {
858
- const combinedMessage = getCombinedMessage(error, message);
859
- super(combinedMessage);
860
- this.name = 'VError';
861
- if (error instanceof Error) {
862
- this.stack = mergeStacks(this.stack, error.stack);
863
- }
864
- if (error.codeFrame) {
865
- // @ts-ignore
866
- this.codeFrame = error.codeFrame;
867
- }
868
- if (error.code) {
869
- // @ts-ignore
870
- this.code = error.code;
871
- }
872
- }
873
- }
874
-
875
882
  const printError = error => {
876
883
  if (error && error.constructor.name === 'AssertionError') {
877
884
  console.error(error.message);
@@ -1049,47 +1056,6 @@ const nameAnonymousFunction = (fn, name) => {
1049
1056
  });
1050
1057
  };
1051
1058
 
1052
- class AssertionError extends Error {
1053
- constructor(message) {
1054
- super(message);
1055
- this.name = 'AssertionError';
1056
- }
1057
- }
1058
- const getType = value => {
1059
- switch (typeof value) {
1060
- case 'number':
1061
- return 'number';
1062
- case 'function':
1063
- return 'function';
1064
- case 'string':
1065
- return 'string';
1066
- case 'object':
1067
- if (value === null) {
1068
- return 'null';
1069
- }
1070
- if (Array.isArray(value)) {
1071
- return 'array';
1072
- }
1073
- return 'object';
1074
- case 'boolean':
1075
- return 'boolean';
1076
- default:
1077
- return 'unknown';
1078
- }
1079
- };
1080
- const object = value => {
1081
- const type = getType(value);
1082
- if (type !== 'object') {
1083
- throw new AssertionError('expected value to be of type object');
1084
- }
1085
- };
1086
- const string = value => {
1087
- const type = getType(value);
1088
- if (type !== 'string') {
1089
- throw new AssertionError('expected value to be of type string');
1090
- }
1091
- };
1092
-
1093
1059
  // @ts-nocheck
1094
1060
 
1095
1061
  const state = {
@@ -1217,19 +1183,40 @@ const TestFrameWork = {
1217
1183
  test
1218
1184
  };
1219
1185
 
1186
+ const show$1 = async () => {
1187
+ return invoke('About.showAbout');
1188
+ };
1189
+ const handleClickOk = async () => {
1190
+ return invoke('About.handleClickOk');
1191
+ };
1192
+ const handleClickClose = async () => {
1193
+ return invoke('About.handleClickClose');
1194
+ };
1195
+ const handleClickCopy = async () => {
1196
+ return invoke('About.handleClickCopy');
1197
+ };
1198
+
1199
+ const TestFrameWorkComponentAbout = {
1200
+ __proto__: null,
1201
+ handleClickClose,
1202
+ handleClickCopy,
1203
+ handleClickOk,
1204
+ show: show$1
1205
+ };
1206
+
1220
1207
  const focus$2 = async () => {
1221
1208
  await invoke('ActivityBar.focus');
1222
1209
  };
1223
- const focusFirst$2 = async () => {
1210
+ const focusFirst$3 = async () => {
1224
1211
  await invoke('ActivityBar.focusFirst');
1225
1212
  };
1226
1213
  const focusLast$2 = async () => {
1227
1214
  await invoke('ActivityBar.focusLast');
1228
1215
  };
1229
- const focusNext$4 = async () => {
1216
+ const focusNext$5 = async () => {
1230
1217
  await invoke('ActivityBar.focusNext');
1231
1218
  };
1232
- const focusPrevious$2 = async () => {
1219
+ const focusPrevious$3 = async () => {
1233
1220
  await invoke('ActivityBar.focusPrevious');
1234
1221
  };
1235
1222
  const handleClick$1 = async index => {
@@ -1245,10 +1232,10 @@ const selectCurrent = async () => {
1245
1232
  const TestFrameworkComponentActivityBar = {
1246
1233
  __proto__: null,
1247
1234
  focus: focus$2,
1248
- focusFirst: focusFirst$2,
1235
+ focusFirst: focusFirst$3,
1249
1236
  focusLast: focusLast$2,
1250
- focusNext: focusNext$4,
1251
- focusPrevious: focusPrevious$2,
1237
+ focusNext: focusNext$5,
1238
+ focusPrevious: focusPrevious$3,
1252
1239
  handleClick: handleClick$1,
1253
1240
  handleContextMenu,
1254
1241
  selectCurrent
@@ -1531,10 +1518,10 @@ const openContextMenu = async index => {
1531
1518
  const focus$1 = async () => {
1532
1519
  await invoke('Explorer.focusIndex', -1);
1533
1520
  };
1534
- const focusNext$3 = async () => {
1521
+ const focusNext$4 = async () => {
1535
1522
  await invoke('Explorer.focusNext');
1536
1523
  };
1537
- const focusIndex$2 = async index => {
1524
+ const focusIndex$3 = async index => {
1538
1525
  await invoke('Explorer.focusIndex', index);
1539
1526
  };
1540
1527
  const clickCurrent = async () => {
@@ -1546,7 +1533,7 @@ const handleArrowLeft = async () => {
1546
1533
  const focusLast$1 = async () => {
1547
1534
  await invoke('Explorer.focusLast');
1548
1535
  };
1549
- const focusFirst$1 = async () => {
1536
+ const focusFirst$2 = async () => {
1550
1537
  await invoke('Explorer.focusFirst');
1551
1538
  };
1552
1539
  const removeDirent = async () => {
@@ -1585,10 +1572,10 @@ const TestFrameWorkComponentExplorer = {
1585
1572
  expandAll,
1586
1573
  expandRecursively,
1587
1574
  focus: focus$1,
1588
- focusFirst: focusFirst$1,
1589
- focusIndex: focusIndex$2,
1575
+ focusFirst: focusFirst$2,
1576
+ focusIndex: focusIndex$3,
1590
1577
  focusLast: focusLast$1,
1591
- focusNext: focusNext$3,
1578
+ focusNext: focusNext$4,
1592
1579
  handleArrowLeft,
1593
1580
  handleClick,
1594
1581
  newFile,
@@ -1668,7 +1655,7 @@ const TestFrameWorkComponentFileSystem = {
1668
1655
  writeFile
1669
1656
  };
1670
1657
 
1671
- const focusNext$2 = async () => {
1658
+ const focusNext$3 = async () => {
1672
1659
  await invoke('FindWidget.focusNext');
1673
1660
  };
1674
1661
  const setValue$2 = async value => {
@@ -1677,7 +1664,7 @@ const setValue$2 = async value => {
1677
1664
 
1678
1665
  const TestFrameWorkComponentFindWidget = {
1679
1666
  __proto__: null,
1680
- focusNext: focusNext$2,
1667
+ focusNext: focusNext$3,
1681
1668
  setValue: setValue$2
1682
1669
  };
1683
1670
 
@@ -1814,13 +1801,13 @@ const open$1 = async () => {
1814
1801
  const setValue$1 = async value => {
1815
1802
  await invoke('QuickPick.handleInput', value, 0);
1816
1803
  };
1817
- const focusNext$1 = async () => {
1804
+ const focusNext$2 = async () => {
1818
1805
  await invoke('QuickPick.focusNext');
1819
1806
  };
1820
- const focusIndex$1 = async index => {
1807
+ const focusIndex$2 = async index => {
1821
1808
  await invoke('QuickPick.focusIndex', index);
1822
1809
  };
1823
- const focusPrevious$1 = async () => {
1810
+ const focusPrevious$2 = async () => {
1824
1811
  await invoke('QuickPick.focusPrevious');
1825
1812
  };
1826
1813
  const selectItem = async label => {
@@ -1835,9 +1822,9 @@ const executeCommand = async label => {
1835
1822
  const TestFrameWorkComponentQuickPick = {
1836
1823
  __proto__: null,
1837
1824
  executeCommand,
1838
- focusIndex: focusIndex$1,
1839
- focusNext: focusNext$1,
1840
- focusPrevious: focusPrevious$1,
1825
+ focusIndex: focusIndex$2,
1826
+ focusNext: focusNext$2,
1827
+ focusPrevious: focusPrevious$2,
1841
1828
  open: open$1,
1842
1829
  selectItem,
1843
1830
  setValue: setValue$1
@@ -1846,10 +1833,58 @@ const TestFrameWorkComponentQuickPick = {
1846
1833
  const setValue = async value => {
1847
1834
  await invoke('Search.handleInput', value);
1848
1835
  };
1836
+ const clearSearchResults = async () => {
1837
+ await invoke('Search.clearSearchResults');
1838
+ };
1839
+ const dismissItem = async () => {
1840
+ await invoke('Search.dismissItem');
1841
+ };
1842
+ const focusFirst$1 = async () => {
1843
+ await invoke('Search.focusFirst');
1844
+ };
1845
+ const focusIndex$1 = async index => {
1846
+ await invoke('Search.focusIndex', index);
1847
+ };
1848
+ const focusNext$1 = async () => {
1849
+ await invoke('Search.focusNext');
1850
+ };
1851
+ const focusNextPage = async () => {
1852
+ await invoke('Search.focusPage');
1853
+ };
1854
+ const focusPreviousPage = async () => {
1855
+ await invoke('Search.focusPreviousPage');
1856
+ };
1857
+ const focusPrevious$1 = async () => {
1858
+ await invoke('Search.focusPrevious');
1859
+ };
1860
+ const toggleMatchCase = async () => {
1861
+ await invoke('Search.toggleMatchCase');
1862
+ };
1863
+ const toggleMatchWholeWord = async () => {
1864
+ await invoke('Search.toggleMatchWholeWord');
1865
+ };
1866
+ const togglePreserveCase = async () => {
1867
+ await invoke('Search.togglePreserveCase');
1868
+ };
1869
+ const toggleUseRegularExpression = async () => {
1870
+ await invoke('Search.toggleUseRegularExpression');
1871
+ };
1849
1872
 
1850
1873
  const TestFrameWorkComponentSearch = {
1851
1874
  __proto__: null,
1852
- setValue
1875
+ clearSearchResults,
1876
+ dismissItem,
1877
+ focusFirst: focusFirst$1,
1878
+ focusIndex: focusIndex$1,
1879
+ focusNext: focusNext$1,
1880
+ focusNextPage,
1881
+ focusPrevious: focusPrevious$1,
1882
+ focusPreviousPage,
1883
+ setValue,
1884
+ toggleMatchCase,
1885
+ toggleMatchWholeWord,
1886
+ togglePreserveCase,
1887
+ toggleUseRegularExpression
1853
1888
  };
1854
1889
 
1855
1890
  const update$1 = settings => {
@@ -2062,6 +2097,7 @@ const TestFrameWorkComponentWorkspace = {
2062
2097
 
2063
2098
  const TestFrameWorkComponent = {
2064
2099
  __proto__: null,
2100
+ About: TestFrameWorkComponentAbout,
2065
2101
  ActivityBar: TestFrameworkComponentActivityBar,
2066
2102
  BaseUrl: TestFrameWorkComponentBaseUrl,
2067
2103
  Command: TestFrameWorkComponentCommand,