@lvce-editor/text-search-worker 3.17.0 → 3.19.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.
@@ -1,4 +1,4 @@
1
- const normalizeLine$1 = line => {
1
+ const normalizeLine = line => {
2
2
  if (line.startsWith('Error: ')) {
3
3
  return line.slice('Error: '.length);
4
4
  }
@@ -7,41 +7,41 @@ const normalizeLine$1 = line => {
7
7
  }
8
8
  return line;
9
9
  };
10
- const getCombinedMessage$1 = (error, message) => {
11
- const stringifiedError = normalizeLine$1(`${error}`);
10
+ const getCombinedMessage = (error, message) => {
11
+ const stringifiedError = normalizeLine(`${error}`);
12
12
  if (message) {
13
13
  return `${message}: ${stringifiedError}`;
14
14
  }
15
15
  return stringifiedError;
16
16
  };
17
- const NewLine$3 = '\n';
18
- const getNewLineIndex$2 = (string, startIndex = undefined) => {
19
- return string.indexOf(NewLine$3, startIndex);
17
+ const NewLine$2 = '\n';
18
+ const getNewLineIndex$1 = (string, startIndex = undefined) => {
19
+ return string.indexOf(NewLine$2, startIndex);
20
20
  };
21
- const mergeStacks$1 = (parent, child) => {
21
+ const mergeStacks = (parent, child) => {
22
22
  if (!child) {
23
23
  return parent;
24
24
  }
25
- const parentNewLineIndex = getNewLineIndex$2(parent);
26
- const childNewLineIndex = getNewLineIndex$2(child);
25
+ const parentNewLineIndex = getNewLineIndex$1(parent);
26
+ const childNewLineIndex = getNewLineIndex$1(child);
27
27
  if (childNewLineIndex === -1) {
28
28
  return parent;
29
29
  }
30
30
  const parentFirstLine = parent.slice(0, parentNewLineIndex);
31
31
  const childRest = child.slice(childNewLineIndex);
32
- const childFirstLine = normalizeLine$1(child.slice(0, childNewLineIndex));
32
+ const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
33
33
  if (parentFirstLine.includes(childFirstLine)) {
34
34
  return parentFirstLine + childRest;
35
35
  }
36
36
  return child;
37
37
  };
38
- let VError$1 = class VError extends Error {
38
+ class VError extends Error {
39
39
  constructor(error, message) {
40
- const combinedMessage = getCombinedMessage$1(error, message);
40
+ const combinedMessage = getCombinedMessage(error, message);
41
41
  super(combinedMessage);
42
42
  this.name = 'VError';
43
43
  if (error instanceof Error) {
44
- this.stack = mergeStacks$1(this.stack, error.stack);
44
+ this.stack = mergeStacks(this.stack, error.stack);
45
45
  }
46
46
  if (error.codeFrame) {
47
47
  // @ts-ignore
@@ -52,7 +52,7 @@ let VError$1 = class VError extends Error {
52
52
  this.code = error.code;
53
53
  }
54
54
  }
55
- };
55
+ }
56
56
 
57
57
  class AssertionError extends Error {
58
58
  constructor(message) {
@@ -107,565 +107,162 @@ const string = value => {
107
107
  }
108
108
  };
109
109
 
110
- const Two = '2.0';
111
- const create$4 = (method, params) => {
112
- return {
113
- jsonrpc: Two,
114
- method,
115
- params
116
- };
110
+ const walkValue = (value, transferrables, isTransferrable) => {
111
+ if (!value) {
112
+ return;
113
+ }
114
+ if (isTransferrable(value)) {
115
+ transferrables.push(value);
116
+ return;
117
+ }
118
+ if (Array.isArray(value)) {
119
+ for (const item of value) {
120
+ walkValue(item, transferrables, isTransferrable);
121
+ }
122
+ return;
123
+ }
124
+ if (typeof value === 'object') {
125
+ for (const property of Object.values(value)) {
126
+ walkValue(property, transferrables, isTransferrable);
127
+ }
128
+ return;
129
+ }
117
130
  };
118
- const state$4 = {
119
- callbacks: Object.create(null)
131
+ const isMessagePort = value => {
132
+ return value && value instanceof MessagePort;
120
133
  };
121
- const set$1 = (id, fn) => {
122
- state$4.callbacks[id] = fn;
134
+ const isMessagePortMain = value => {
135
+ return value && value.constructor && value.constructor.name === 'MessagePortMain';
123
136
  };
124
- const get$1 = id => {
125
- return state$4.callbacks[id];
137
+ const isOffscreenCanvas = value => {
138
+ return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
126
139
  };
127
- const remove$1 = id => {
128
- delete state$4.callbacks[id];
140
+ const isInstanceOf = (value, constructorName) => {
141
+ return value?.constructor?.name === constructorName;
129
142
  };
130
- let id = 0;
131
- const create$3$1 = () => {
132
- return ++id;
143
+ const isSocket = value => {
144
+ return isInstanceOf(value, 'Socket');
133
145
  };
134
- const warn = (...args) => {
135
- console.warn(...args);
146
+ const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
147
+ const isTransferrable = value => {
148
+ for (const fn of transferrables) {
149
+ if (fn(value)) {
150
+ return true;
151
+ }
152
+ }
153
+ return false;
136
154
  };
137
- const registerPromise = () => {
138
- const id = create$3$1();
139
- const {
140
- resolve,
141
- promise
142
- } = Promise.withResolvers();
143
- set$1(id, resolve);
144
- return {
145
- id,
146
- promise
155
+ const getTransferrables = value => {
156
+ const transferrables = [];
157
+ walkValue(value, transferrables, isTransferrable);
158
+ return transferrables;
159
+ };
160
+ const attachEvents = that => {
161
+ const handleMessage = (...args) => {
162
+ const data = that.getData(...args);
163
+ that.dispatchEvent(new MessageEvent('message', {
164
+ data
165
+ }));
166
+ };
167
+ that.onMessage(handleMessage);
168
+ const handleClose = event => {
169
+ that.dispatchEvent(new Event('close'));
147
170
  };
171
+ that.onClose(handleClose);
148
172
  };
149
- const resolve = (id, response) => {
150
- const fn = get$1(id);
151
- if (!fn) {
152
- console.log(response);
153
- warn(`callback ${id} may already be disposed`);
154
- return;
173
+ class Ipc extends EventTarget {
174
+ constructor(rawIpc) {
175
+ super();
176
+ this._rawIpc = rawIpc;
177
+ attachEvents(this);
155
178
  }
156
- fn(response);
157
- remove$1(id);
179
+ }
180
+ const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
181
+ const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
182
+ const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
183
+ const NewLine$1 = '\n';
184
+ const joinLines$1 = lines => {
185
+ return lines.join(NewLine$1);
158
186
  };
159
- const create$2$2 = (method, params) => {
160
- const {
161
- id,
162
- promise
163
- } = registerPromise();
164
- const message = {
165
- jsonrpc: Two,
166
- method,
167
- params,
168
- id
169
- };
187
+ const splitLines$2 = lines => {
188
+ return lines.split(NewLine$1);
189
+ };
190
+ const isModuleNotFoundMessage = line => {
191
+ return line.includes('[ERR_MODULE_NOT_FOUND]');
192
+ };
193
+ const getModuleNotFoundError = stderr => {
194
+ const lines = splitLines$2(stderr);
195
+ const messageIndex = lines.findIndex(isModuleNotFoundMessage);
196
+ const message = lines[messageIndex];
170
197
  return {
171
198
  message,
172
- promise
199
+ code: ERR_MODULE_NOT_FOUND
173
200
  };
174
201
  };
175
- class JsonRpcError extends Error {
176
- constructor(message) {
177
- super(message);
178
- this.name = 'JsonRpcError';
179
- }
180
- }
181
- const NewLine$2 = '\n';
182
- const DomException = 'DOMException';
183
- const ReferenceError$1 = 'ReferenceError';
184
- const SyntaxError$1 = 'SyntaxError';
185
- const TypeError$1 = 'TypeError';
186
- const getErrorConstructor = (message, type) => {
187
- if (type) {
188
- switch (type) {
189
- case DomException:
190
- return DOMException;
191
- case TypeError$1:
192
- return TypeError;
193
- case SyntaxError$1:
194
- return SyntaxError;
195
- case ReferenceError$1:
196
- return ReferenceError;
197
- default:
198
- return Error;
199
- }
200
- }
201
- if (message.startsWith('TypeError: ')) {
202
- return TypeError;
203
- }
204
- if (message.startsWith('SyntaxError: ')) {
205
- return SyntaxError;
206
- }
207
- if (message.startsWith('ReferenceError: ')) {
208
- return ReferenceError;
209
- }
210
- return Error;
202
+ const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
203
+ const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
204
+ const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
205
+ const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
206
+ const RE_AT = /^\s+at/;
207
+ const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
208
+ const isUnhelpfulNativeModuleError = stderr => {
209
+ return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
211
210
  };
212
- const constructError = (message, type, name) => {
213
- const ErrorConstructor = getErrorConstructor(message, type);
214
- if (ErrorConstructor === DOMException && name) {
215
- return new ErrorConstructor(message, name);
216
- }
217
- if (ErrorConstructor === Error) {
218
- const error = new Error(message);
219
- if (name && name !== 'VError') {
220
- error.name = name;
221
- }
222
- return error;
223
- }
224
- return new ErrorConstructor(message);
211
+ const isMessageCodeBlockStartIndex = line => {
212
+ return RE_MESSAGE_CODE_BLOCK_START.test(line);
225
213
  };
226
- const getNewLineIndex$1 = (string, startIndex = undefined) => {
227
- return string.indexOf(NewLine$2, startIndex);
214
+ const isMessageCodeBlockEndIndex = line => {
215
+ return RE_MESSAGE_CODE_BLOCK_END.test(line);
228
216
  };
229
- const getParentStack = error => {
230
- let parentStack = error.stack || error.data || error.message || '';
231
- if (parentStack.startsWith(' at')) {
232
- parentStack = error.message + NewLine$2 + parentStack;
217
+ const getMessageCodeBlock = stderr => {
218
+ const lines = splitLines$2(stderr);
219
+ const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
220
+ const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
221
+ const relevantLines = lines.slice(startIndex, endIndex);
222
+ const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
223
+ return relevantMessage;
224
+ };
225
+ const getNativeModuleErrorMessage = stderr => {
226
+ const message = getMessageCodeBlock(stderr);
227
+ return {
228
+ message: `Incompatible native node module: ${message}`,
229
+ code: E_INCOMPATIBLE_NATIVE_MODULE
230
+ };
231
+ };
232
+ const isModulesSyntaxError = stderr => {
233
+ if (!stderr) {
234
+ return false;
233
235
  }
234
- return parentStack;
236
+ return stderr.includes('SyntaxError: Cannot use import statement outside a module');
235
237
  };
236
- const joinLines$1 = lines => {
237
- return lines.join(NewLine$2);
238
+ const getModuleSyntaxError = () => {
239
+ return {
240
+ message: `ES Modules are not supported in electron`,
241
+ code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
242
+ };
238
243
  };
239
- const MethodNotFound = -32601;
240
- const Custom = -32001;
241
- const splitLines$2 = lines => {
242
- return lines.split(NewLine$2);
244
+ const isModuleNotFoundError = stderr => {
245
+ if (!stderr) {
246
+ return false;
247
+ }
248
+ return stderr.includes('ERR_MODULE_NOT_FOUND');
243
249
  };
244
- const restoreJsonRpcError = error => {
245
- if (error && error instanceof Error) {
246
- return error;
250
+ const isNormalStackLine = line => {
251
+ return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
252
+ };
253
+ const getDetails = lines => {
254
+ const index = lines.findIndex(isNormalStackLine);
255
+ if (index === -1) {
256
+ return {
257
+ actualMessage: joinLines$1(lines),
258
+ rest: []
259
+ };
247
260
  }
248
- const currentStack = joinLines$1(splitLines$2(new Error().stack || '').slice(1));
249
- if (error && error.code && error.code === MethodNotFound) {
250
- const restoredError = new JsonRpcError(error.message);
251
- const parentStack = getParentStack(error);
252
- restoredError.stack = parentStack + NewLine$2 + currentStack;
253
- return restoredError;
254
- }
255
- if (error && error.message) {
256
- const restoredError = constructError(error.message, error.type, error.name);
257
- if (error.data) {
258
- if (error.data.stack && error.data.type && error.message) {
259
- restoredError.stack = error.data.type + ': ' + error.message + NewLine$2 + error.data.stack + NewLine$2 + currentStack;
260
- } else if (error.data.stack) {
261
- restoredError.stack = error.data.stack;
262
- }
263
- if (error.data.codeFrame) {
264
- // @ts-ignore
265
- restoredError.codeFrame = error.data.codeFrame;
266
- }
267
- if (error.data.code) {
268
- // @ts-ignore
269
- restoredError.code = error.data.code;
270
- }
271
- if (error.data.type) {
272
- // @ts-ignore
273
- restoredError.name = error.data.type;
274
- }
275
- } else {
276
- if (error.stack) {
277
- const lowerStack = restoredError.stack || '';
278
- // @ts-ignore
279
- const indexNewLine = getNewLineIndex$1(lowerStack);
280
- const parentStack = getParentStack(error);
281
- // @ts-ignore
282
- restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
283
- }
284
- if (error.codeFrame) {
285
- // @ts-ignore
286
- restoredError.codeFrame = error.codeFrame;
287
- }
288
- }
289
- return restoredError;
290
- }
291
- if (typeof error === 'string') {
292
- return new Error(`JsonRpc Error: ${error}`);
293
- }
294
- return new Error(`JsonRpc Error: ${error}`);
295
- };
296
- const unwrapJsonRpcResult = responseMessage => {
297
- if ('error' in responseMessage) {
298
- const restoredError = restoreJsonRpcError(responseMessage.error);
299
- throw restoredError;
300
- }
301
- if ('result' in responseMessage) {
302
- return responseMessage.result;
303
- }
304
- throw new JsonRpcError('unexpected response message');
305
- };
306
- const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
307
- const getErrorType = prettyError => {
308
- if (prettyError && prettyError.type) {
309
- return prettyError.type;
310
- }
311
- if (prettyError && prettyError.constructor && prettyError.constructor.name) {
312
- return prettyError.constructor.name;
313
- }
314
- return undefined;
315
- };
316
- const getErrorProperty = (error, prettyError) => {
317
- if (error && error.code === E_COMMAND_NOT_FOUND) {
318
- return {
319
- code: MethodNotFound,
320
- message: error.message,
321
- data: error.stack
322
- };
323
- }
324
- return {
325
- code: Custom,
326
- message: prettyError.message,
327
- data: {
328
- stack: prettyError.stack,
329
- codeFrame: prettyError.codeFrame,
330
- type: getErrorType(prettyError),
331
- code: prettyError.code,
332
- name: prettyError.name
333
- }
334
- };
335
- };
336
- const create$1$2 = (message, error) => {
337
- return {
338
- jsonrpc: Two,
339
- id: message.id,
340
- error
341
- };
342
- };
343
- const getErrorResponse = (message, error, preparePrettyError, logError) => {
344
- const prettyError = preparePrettyError(error);
345
- logError(error, prettyError);
346
- const errorProperty = getErrorProperty(error, prettyError);
347
- return create$1$2(message, errorProperty);
348
- };
349
- const create$6 = (message, result) => {
350
- return {
351
- jsonrpc: Two,
352
- id: message.id,
353
- result: result ?? null
354
- };
355
- };
356
- const getSuccessResponse = (message, result) => {
357
- const resultProperty = result ?? null;
358
- return create$6(message, resultProperty);
359
- };
360
- const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
361
- try {
362
- const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
363
- return getSuccessResponse(message, result);
364
- } catch (error) {
365
- return getErrorResponse(message, error, preparePrettyError, logError);
366
- }
367
- };
368
- const defaultPreparePrettyError = error => {
369
- return error;
370
- };
371
- const defaultLogError = () => {
372
- // ignore
373
- };
374
- const defaultRequiresSocket = () => {
375
- return false;
376
- };
377
- const defaultResolve = resolve;
378
-
379
- // TODO maybe remove this in v6 or v7, only accept options object to simplify the code
380
- const normalizeParams = args => {
381
- if (args.length === 1) {
382
- const options = args[0];
383
- return {
384
- ipc: options.ipc,
385
- message: options.message,
386
- execute: options.execute,
387
- resolve: options.resolve || defaultResolve,
388
- preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
389
- logError: options.logError || defaultLogError,
390
- requiresSocket: options.requiresSocket || defaultRequiresSocket
391
- };
392
- }
393
- return {
394
- ipc: args[0],
395
- message: args[1],
396
- execute: args[2],
397
- resolve: args[3],
398
- preparePrettyError: args[4],
399
- logError: args[5],
400
- requiresSocket: args[6]
401
- };
402
- };
403
- const handleJsonRpcMessage = async (...args) => {
404
- const options = normalizeParams(args);
405
- const {
406
- message,
407
- ipc,
408
- execute,
409
- resolve,
410
- preparePrettyError,
411
- logError,
412
- requiresSocket
413
- } = options;
414
- if ('id' in message) {
415
- if ('method' in message) {
416
- const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
417
- try {
418
- ipc.send(response);
419
- } catch (error) {
420
- const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
421
- ipc.send(errorResponse);
422
- }
423
- return;
424
- }
425
- resolve(message.id, message);
426
- return;
427
- }
428
- if ('method' in message) {
429
- await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
430
- return;
431
- }
432
- throw new JsonRpcError('unexpected message');
433
- };
434
- const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
435
- const {
436
- message,
437
- promise
438
- } = create$2$2(method, params);
439
- if (useSendAndTransfer && ipc.sendAndTransfer) {
440
- ipc.sendAndTransfer(message);
441
- } else {
442
- ipc.send(message);
443
- }
444
- const responseMessage = await promise;
445
- return unwrapJsonRpcResult(responseMessage);
446
- };
447
- const send = (transport, method, ...params) => {
448
- const message = create$4(method, params);
449
- transport.send(message);
450
- };
451
- const invoke$3 = (ipc, method, ...params) => {
452
- return invokeHelper(ipc, method, params, false);
453
- };
454
- const invokeAndTransfer$1 = (ipc, method, ...params) => {
455
- return invokeHelper(ipc, method, params, true);
456
- };
457
-
458
- const commands = Object.create(null);
459
- const register = commandMap => {
460
- Object.assign(commands, commandMap);
461
- };
462
- const getCommand = key => {
463
- return commands[key];
464
- };
465
- const execute = (command, ...args) => {
466
- const fn = getCommand(command);
467
- if (!fn) {
468
- throw new Error(`command not found ${command}`);
469
- }
470
- return fn(...args);
471
- };
472
-
473
- const getData$1 = event => {
474
- return event.data;
475
- };
476
- const attachEvents = that => {
477
- const handleMessage = (...args) => {
478
- const data = that.getData(...args);
479
- that.dispatchEvent(new MessageEvent('message', {
480
- data
481
- }));
482
- };
483
- that.onMessage(handleMessage);
484
- const handleClose = event => {
485
- that.dispatchEvent(new Event('close'));
486
- };
487
- that.onClose(handleClose);
488
- };
489
- class Ipc extends EventTarget {
490
- constructor(rawIpc) {
491
- super();
492
- this._rawIpc = rawIpc;
493
- attachEvents(this);
494
- }
495
- }
496
- const readyMessage = 'ready';
497
- const walkValue = (value, transferrables, isTransferrable) => {
498
- if (!value) {
499
- return;
500
- }
501
- if (isTransferrable(value)) {
502
- transferrables.push(value);
503
- return;
504
- }
505
- if (Array.isArray(value)) {
506
- for (const item of value) {
507
- walkValue(item, transferrables, isTransferrable);
508
- }
509
- return;
510
- }
511
- if (typeof value === 'object') {
512
- for (const property of Object.values(value)) {
513
- walkValue(property, transferrables, isTransferrable);
514
- }
515
- return;
516
- }
517
- };
518
- const isMessagePort = value => {
519
- return value && value instanceof MessagePort;
520
- };
521
- const isMessagePortMain = value => {
522
- return value && value.constructor && value.constructor.name === 'MessagePortMain';
523
- };
524
- const isOffscreenCanvas = value => {
525
- return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
526
- };
527
- const isInstanceOf = (value, constructorName) => {
528
- return value?.constructor?.name === constructorName;
529
- };
530
- const isSocket = value => {
531
- return isInstanceOf(value, 'Socket');
532
- };
533
- const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
534
- const isTransferrable = value => {
535
- for (const fn of transferrables) {
536
- if (fn(value)) {
537
- return true;
538
- }
539
- }
540
- return false;
541
- };
542
- const getTransferrables = value => {
543
- const transferrables = [];
544
- walkValue(value, transferrables, isTransferrable);
545
- return transferrables;
546
- };
547
- const listen$2 = () => {
548
- // @ts-ignore
549
- if (typeof WorkerGlobalScope === 'undefined') {
550
- throw new TypeError('module is not in web worker scope');
551
- }
552
- return globalThis;
553
- };
554
- const signal$2 = global => {
555
- global.postMessage(readyMessage);
556
- };
557
- class IpcChildWithModuleWorker extends Ipc {
558
- getData(event) {
559
- return getData$1(event);
560
- }
561
- send(message) {
562
- // @ts-ignore
563
- this._rawIpc.postMessage(message);
564
- }
565
- sendAndTransfer(message) {
566
- const transfer = getTransferrables(message);
567
- // @ts-ignore
568
- this._rawIpc.postMessage(message, transfer);
569
- }
570
- dispose() {
571
- // ignore
572
- }
573
- onClose(callback) {
574
- // ignore
575
- }
576
- onMessage(callback) {
577
- this._rawIpc.addEventListener('message', callback);
578
- }
579
- }
580
- const wrap$5 = global => {
581
- return new IpcChildWithModuleWorker(global);
582
- };
583
- const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
584
- const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
585
- const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
586
- const NewLine$1 = '\n';
587
- const joinLines = lines => {
588
- return lines.join(NewLine$1);
589
- };
590
- const splitLines$1 = lines => {
591
- return lines.split(NewLine$1);
592
- };
593
- const isModuleNotFoundMessage = line => {
594
- return line.includes('[ERR_MODULE_NOT_FOUND]');
595
- };
596
- const getModuleNotFoundError = stderr => {
597
- const lines = splitLines$1(stderr);
598
- const messageIndex = lines.findIndex(isModuleNotFoundMessage);
599
- const message = lines[messageIndex];
600
- return {
601
- message,
602
- code: ERR_MODULE_NOT_FOUND
603
- };
604
- };
605
- const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
606
- const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
607
- const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
608
- const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
609
- const RE_AT = /^\s+at/;
610
- const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
611
- const isUnhelpfulNativeModuleError = stderr => {
612
- return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
613
- };
614
- const isMessageCodeBlockStartIndex = line => {
615
- return RE_MESSAGE_CODE_BLOCK_START.test(line);
616
- };
617
- const isMessageCodeBlockEndIndex = line => {
618
- return RE_MESSAGE_CODE_BLOCK_END.test(line);
619
- };
620
- const getMessageCodeBlock = stderr => {
621
- const lines = splitLines$1(stderr);
622
- const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
623
- const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
624
- const relevantLines = lines.slice(startIndex, endIndex);
625
- const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
626
- return relevantMessage;
627
- };
628
- const getNativeModuleErrorMessage = stderr => {
629
- const message = getMessageCodeBlock(stderr);
630
- return {
631
- message: `Incompatible native node module: ${message}`,
632
- code: E_INCOMPATIBLE_NATIVE_MODULE
633
- };
634
- };
635
- const isModulesSyntaxError = stderr => {
636
- if (!stderr) {
637
- return false;
638
- }
639
- return stderr.includes('SyntaxError: Cannot use import statement outside a module');
640
- };
641
- const getModuleSyntaxError = () => {
642
- return {
643
- message: `ES Modules are not supported in electron`,
644
- code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
645
- };
646
- };
647
- const isModuleNotFoundError = stderr => {
648
- if (!stderr) {
649
- return false;
650
- }
651
- return stderr.includes('ERR_MODULE_NOT_FOUND');
652
- };
653
- const isNormalStackLine = line => {
654
- return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
655
- };
656
- const getDetails = lines => {
657
- const index = lines.findIndex(isNormalStackLine);
658
- if (index === -1) {
659
- return {
660
- actualMessage: joinLines(lines),
661
- rest: []
662
- };
663
- }
664
- let lastIndex = index - 1;
665
- while (++lastIndex < lines.length) {
666
- if (!isNormalStackLine(lines[lastIndex])) {
667
- break;
668
- }
261
+ let lastIndex = index - 1;
262
+ while (++lastIndex < lines.length) {
263
+ if (!isNormalStackLine(lines[lastIndex])) {
264
+ break;
265
+ }
669
266
  }
670
267
  return {
671
268
  actualMessage: lines[index - 1],
@@ -682,7 +279,7 @@ const getHelpfulChildProcessError = (stdout, stderr) => {
682
279
  if (isModuleNotFoundError(stderr)) {
683
280
  return getModuleNotFoundError(stderr);
684
281
  }
685
- const lines = splitLines$1(stderr);
282
+ const lines = splitLines$2(stderr);
686
283
  const {
687
284
  actualMessage,
688
285
  rest
@@ -693,61 +290,6 @@ const getHelpfulChildProcessError = (stdout, stderr) => {
693
290
  stack: rest
694
291
  };
695
292
  };
696
- const normalizeLine = line => {
697
- if (line.startsWith('Error: ')) {
698
- return line.slice('Error: '.length);
699
- }
700
- if (line.startsWith('VError: ')) {
701
- return line.slice('VError: '.length);
702
- }
703
- return line;
704
- };
705
- const getCombinedMessage = (error, message) => {
706
- const stringifiedError = normalizeLine(`${error}`);
707
- if (message) {
708
- return `${message}: ${stringifiedError}`;
709
- }
710
- return stringifiedError;
711
- };
712
- const NewLine = '\n';
713
- const getNewLineIndex = (string, startIndex = undefined) => {
714
- return string.indexOf(NewLine, startIndex);
715
- };
716
- const mergeStacks = (parent, child) => {
717
- if (!child) {
718
- return parent;
719
- }
720
- const parentNewLineIndex = getNewLineIndex(parent);
721
- const childNewLineIndex = getNewLineIndex(child);
722
- if (childNewLineIndex === -1) {
723
- return parent;
724
- }
725
- const parentFirstLine = parent.slice(0, parentNewLineIndex);
726
- const childRest = child.slice(childNewLineIndex);
727
- const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
728
- if (parentFirstLine.includes(childFirstLine)) {
729
- return parentFirstLine + childRest;
730
- }
731
- return child;
732
- };
733
- class VError extends Error {
734
- constructor(error, message) {
735
- const combinedMessage = getCombinedMessage(error, message);
736
- super(combinedMessage);
737
- this.name = 'VError';
738
- if (error instanceof Error) {
739
- this.stack = mergeStacks(this.stack, error.stack);
740
- }
741
- if (error.codeFrame) {
742
- // @ts-ignore
743
- this.codeFrame = error.codeFrame;
744
- }
745
- if (error.code) {
746
- // @ts-ignore
747
- this.code = error.code;
748
- }
749
- }
750
- }
751
293
  class IpcError extends VError {
752
294
  // @ts-ignore
753
295
  constructor(betterMessage, stdout = '', stderr = '') {
@@ -774,6 +316,46 @@ class IpcError extends VError {
774
316
  this.stderr = stderr;
775
317
  }
776
318
  }
319
+ const readyMessage = 'ready';
320
+ const getData$2 = event => {
321
+ return event.data;
322
+ };
323
+ const listen$6 = () => {
324
+ // @ts-ignore
325
+ if (typeof WorkerGlobalScope === 'undefined') {
326
+ throw new TypeError('module is not in web worker scope');
327
+ }
328
+ return globalThis;
329
+ };
330
+ const signal$6 = global => {
331
+ global.postMessage(readyMessage);
332
+ };
333
+ class IpcChildWithModuleWorker extends Ipc {
334
+ getData(event) {
335
+ return getData$2(event);
336
+ }
337
+ send(message) {
338
+ // @ts-ignore
339
+ this._rawIpc.postMessage(message);
340
+ }
341
+ sendAndTransfer(message) {
342
+ const transfer = getTransferrables(message);
343
+ // @ts-ignore
344
+ this._rawIpc.postMessage(message, transfer);
345
+ }
346
+ dispose() {
347
+ // ignore
348
+ }
349
+ onClose(callback) {
350
+ // ignore
351
+ }
352
+ onMessage(callback) {
353
+ this._rawIpc.addEventListener('message', callback);
354
+ }
355
+ }
356
+ const wrap$d = global => {
357
+ return new IpcChildWithModuleWorker(global);
358
+ };
777
359
  const withResolvers = () => {
778
360
  let _resolve;
779
361
  const promise = new Promise(resolve => {
@@ -796,10 +378,10 @@ const waitForFirstMessage = async port => {
796
378
  // @ts-ignore
797
379
  return event.data;
798
380
  };
799
- const listen$1$1 = async () => {
800
- const parentIpcRaw = listen$2();
801
- signal$2(parentIpcRaw);
802
- const parentIpc = wrap$5(parentIpcRaw);
381
+ const listen$5 = async () => {
382
+ const parentIpcRaw = listen$6();
383
+ signal$6(parentIpcRaw);
384
+ const parentIpc = wrap$d(parentIpcRaw);
803
385
  const firstMessage = await waitForFirstMessage(parentIpc);
804
386
  if (firstMessage.method !== 'initialize') {
805
387
  throw new IpcError('unexpected first message');
@@ -822,7 +404,7 @@ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
822
404
  super(port);
823
405
  }
824
406
  getData(event) {
825
- return getData$1(event);
407
+ return getData$2(event);
826
408
  }
827
409
  send(message) {
828
410
  this._rawIpc.postMessage(message);
@@ -844,15 +426,16 @@ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
844
426
  this._rawIpc.start();
845
427
  }
846
428
  }
847
- const wrap$4 = port => {
429
+ const wrap$c = port => {
848
430
  return new IpcChildWithModuleWorkerAndMessagePort(port);
849
431
  };
850
432
  const IpcChildWithModuleWorkerAndMessagePort$1 = {
851
433
  __proto__: null,
852
- listen: listen$1$1,
853
- wrap: wrap$4
434
+ listen: listen$5,
435
+ wrap: wrap$c
854
436
  };
855
- const Message$1 = 3;
437
+ const Open = 1;
438
+ const Close = 2;
856
439
  const addListener = (emitter, type, callback) => {
857
440
  if ('addEventListener' in emitter) {
858
441
  emitter.addEventListener(type, callback);
@@ -891,7 +474,8 @@ const getFirstEvent = (eventEmitter, eventMap) => {
891
474
  }
892
475
  return promise;
893
476
  };
894
- const create$2$1 = async ({
477
+ const Message$1 = 3;
478
+ const create$4$1 = async ({
895
479
  messagePort,
896
480
  isMessagePortOpen
897
481
  }) => {
@@ -901,106 +485,465 @@ const create$2$1 = async ({
901
485
  if (isMessagePortOpen) {
902
486
  return messagePort;
903
487
  }
904
- const eventPromise = getFirstEvent(messagePort, {
905
- message: Message$1
906
- });
907
- messagePort.start();
908
- const event = await eventPromise;
909
- // @ts-ignore
910
- if (event.data !== readyMessage) {
911
- throw new IpcError('unexpected first message');
488
+ const eventPromise = getFirstEvent(messagePort, {
489
+ message: Message$1
490
+ });
491
+ messagePort.start();
492
+ const event = await eventPromise;
493
+ // @ts-ignore
494
+ if (event.data !== readyMessage) {
495
+ throw new IpcError('unexpected first message');
496
+ }
497
+ return messagePort;
498
+ };
499
+ const signal = messagePort => {
500
+ messagePort.start();
501
+ };
502
+ class IpcParentWithMessagePort extends Ipc {
503
+ constructor(port) {
504
+ super(port);
505
+ }
506
+ getData = getData$2;
507
+ send(message) {
508
+ this._rawIpc.postMessage(message);
509
+ }
510
+ sendAndTransfer(message) {
511
+ throw new Error('not implemented');
512
+ }
513
+ dispose() {
514
+ this._rawIpc.close();
515
+ }
516
+ onMessage(callback) {
517
+ this._rawIpc.addEventListener('message', callback);
518
+ }
519
+ onClose(callback) {}
520
+ }
521
+ const wrap$4 = messagePort => {
522
+ return new IpcParentWithMessagePort(messagePort);
523
+ };
524
+ const IpcParentWithMessagePort$1 = {
525
+ __proto__: null,
526
+ create: create$4$1,
527
+ signal,
528
+ wrap: wrap$4
529
+ };
530
+ const stringifyCompact = value => {
531
+ return JSON.stringify(value);
532
+ };
533
+ const parse = content => {
534
+ if (content === 'undefined') {
535
+ return null;
536
+ }
537
+ try {
538
+ return JSON.parse(content);
539
+ } catch (error) {
540
+ throw new VError(error, 'failed to parse json');
541
+ }
542
+ };
543
+ const waitForWebSocketToBeOpen = webSocket => {
544
+ return getFirstEvent(webSocket, {
545
+ open: Open,
546
+ close: Close
547
+ });
548
+ };
549
+ const create$7 = async ({
550
+ webSocket
551
+ }) => {
552
+ const firstWebSocketEvent = await waitForWebSocketToBeOpen(webSocket);
553
+ // @ts-ignore
554
+ if (firstWebSocketEvent.type === Close) {
555
+ throw new IpcError('Websocket connection was immediately closed');
556
+ }
557
+ return webSocket;
558
+ };
559
+ class IpcParentWithWebSocket extends Ipc {
560
+ getData(event) {
561
+ return parse(event.data);
562
+ }
563
+ send(message) {
564
+ this._rawIpc.send(stringifyCompact(message));
565
+ }
566
+ sendAndTransfer(message) {
567
+ throw new Error('sendAndTransfer not supported');
568
+ }
569
+ dispose() {
570
+ this._rawIpc.close();
571
+ }
572
+ onClose(callback) {
573
+ this._rawIpc.addEventListener('close', callback);
574
+ }
575
+ onMessage(callback) {
576
+ this._rawIpc.addEventListener('message', callback);
577
+ }
578
+ }
579
+ const wrap$1 = webSocket => {
580
+ return new IpcParentWithWebSocket(webSocket);
581
+ };
582
+ const IpcParentWithWebSocket$1 = {
583
+ __proto__: null,
584
+ create: create$7,
585
+ wrap: wrap$1
586
+ };
587
+
588
+ const Two = '2.0';
589
+ const create$4 = (method, params) => {
590
+ return {
591
+ jsonrpc: Two,
592
+ method,
593
+ params
594
+ };
595
+ };
596
+ const callbacks = Object.create(null);
597
+ const set$1 = (id, fn) => {
598
+ callbacks[id] = fn;
599
+ };
600
+ const get$1 = id => {
601
+ return callbacks[id];
602
+ };
603
+ const remove$1 = id => {
604
+ delete callbacks[id];
605
+ };
606
+ let id = 0;
607
+ const create$3 = () => {
608
+ return ++id;
609
+ };
610
+ const warn = (...args) => {
611
+ console.warn(...args);
612
+ };
613
+ const registerPromise = () => {
614
+ const id = create$3();
615
+ const {
616
+ resolve,
617
+ promise
618
+ } = Promise.withResolvers();
619
+ set$1(id, resolve);
620
+ return {
621
+ id,
622
+ promise
623
+ };
624
+ };
625
+ const resolve = (id, response) => {
626
+ const fn = get$1(id);
627
+ if (!fn) {
628
+ console.log(response);
629
+ warn(`callback ${id} may already be disposed`);
630
+ return;
631
+ }
632
+ fn(response);
633
+ remove$1(id);
634
+ };
635
+ const create$2$1 = (method, params) => {
636
+ const {
637
+ id,
638
+ promise
639
+ } = registerPromise();
640
+ const message = {
641
+ jsonrpc: Two,
642
+ method,
643
+ params,
644
+ id
645
+ };
646
+ return {
647
+ message,
648
+ promise
649
+ };
650
+ };
651
+ class JsonRpcError extends Error {
652
+ constructor(message) {
653
+ super(message);
654
+ this.name = 'JsonRpcError';
655
+ }
656
+ }
657
+ const NewLine = '\n';
658
+ const DomException = 'DOMException';
659
+ const ReferenceError$1 = 'ReferenceError';
660
+ const SyntaxError$1 = 'SyntaxError';
661
+ const TypeError$1 = 'TypeError';
662
+ const getErrorConstructor = (message, type) => {
663
+ if (type) {
664
+ switch (type) {
665
+ case DomException:
666
+ return DOMException;
667
+ case TypeError$1:
668
+ return TypeError;
669
+ case SyntaxError$1:
670
+ return SyntaxError;
671
+ case ReferenceError$1:
672
+ return ReferenceError;
673
+ default:
674
+ return Error;
675
+ }
676
+ }
677
+ if (message.startsWith('TypeError: ')) {
678
+ return TypeError;
679
+ }
680
+ if (message.startsWith('SyntaxError: ')) {
681
+ return SyntaxError;
682
+ }
683
+ if (message.startsWith('ReferenceError: ')) {
684
+ return ReferenceError;
685
+ }
686
+ return Error;
687
+ };
688
+ const constructError = (message, type, name) => {
689
+ const ErrorConstructor = getErrorConstructor(message, type);
690
+ if (ErrorConstructor === DOMException && name) {
691
+ return new ErrorConstructor(message, name);
692
+ }
693
+ if (ErrorConstructor === Error) {
694
+ const error = new Error(message);
695
+ if (name && name !== 'VError') {
696
+ error.name = name;
697
+ }
698
+ return error;
699
+ }
700
+ return new ErrorConstructor(message);
701
+ };
702
+ const getNewLineIndex = (string, startIndex = undefined) => {
703
+ return string.indexOf(NewLine, startIndex);
704
+ };
705
+ const getParentStack = error => {
706
+ let parentStack = error.stack || error.data || error.message || '';
707
+ if (parentStack.startsWith(' at')) {
708
+ parentStack = error.message + NewLine + parentStack;
709
+ }
710
+ return parentStack;
711
+ };
712
+ const joinLines = lines => {
713
+ return lines.join(NewLine);
714
+ };
715
+ const MethodNotFound = -32601;
716
+ const Custom = -32001;
717
+ const splitLines$1 = lines => {
718
+ return lines.split(NewLine);
719
+ };
720
+ const restoreJsonRpcError = error => {
721
+ if (error && error instanceof Error) {
722
+ return error;
723
+ }
724
+ const currentStack = joinLines(splitLines$1(new Error().stack || '').slice(1));
725
+ if (error && error.code && error.code === MethodNotFound) {
726
+ const restoredError = new JsonRpcError(error.message);
727
+ const parentStack = getParentStack(error);
728
+ restoredError.stack = parentStack + NewLine + currentStack;
729
+ return restoredError;
730
+ }
731
+ if (error && error.message) {
732
+ const restoredError = constructError(error.message, error.type, error.name);
733
+ if (error.data) {
734
+ if (error.data.stack && error.data.type && error.message) {
735
+ restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
736
+ } else if (error.data.stack) {
737
+ restoredError.stack = error.data.stack;
738
+ }
739
+ if (error.data.codeFrame) {
740
+ // @ts-ignore
741
+ restoredError.codeFrame = error.data.codeFrame;
742
+ }
743
+ if (error.data.code) {
744
+ // @ts-ignore
745
+ restoredError.code = error.data.code;
746
+ }
747
+ if (error.data.type) {
748
+ // @ts-ignore
749
+ restoredError.name = error.data.type;
750
+ }
751
+ } else {
752
+ if (error.stack) {
753
+ const lowerStack = restoredError.stack || '';
754
+ // @ts-ignore
755
+ const indexNewLine = getNewLineIndex(lowerStack);
756
+ const parentStack = getParentStack(error);
757
+ // @ts-ignore
758
+ restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
759
+ }
760
+ if (error.codeFrame) {
761
+ // @ts-ignore
762
+ restoredError.codeFrame = error.codeFrame;
763
+ }
764
+ }
765
+ return restoredError;
766
+ }
767
+ if (typeof error === 'string') {
768
+ return new Error(`JsonRpc Error: ${error}`);
912
769
  }
913
- return messagePort;
914
- };
915
- const signal = messagePort => {
916
- messagePort.start();
770
+ return new Error(`JsonRpc Error: ${error}`);
917
771
  };
918
- class IpcParentWithMessagePort extends Ipc {
919
- constructor(port) {
920
- super(port);
772
+ const unwrapJsonRpcResult = responseMessage => {
773
+ if ('error' in responseMessage) {
774
+ const restoredError = restoreJsonRpcError(responseMessage.error);
775
+ throw restoredError;
921
776
  }
922
- getData = getData$1;
923
- send(message) {
924
- this._rawIpc.postMessage(message);
777
+ if ('result' in responseMessage) {
778
+ return responseMessage.result;
925
779
  }
926
- sendAndTransfer(message) {
927
- throw new Error('not implemented');
780
+ throw new JsonRpcError('unexpected response message');
781
+ };
782
+ const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
783
+ const getErrorType = prettyError => {
784
+ if (prettyError && prettyError.type) {
785
+ return prettyError.type;
928
786
  }
929
- dispose() {
930
- this._rawIpc.close();
787
+ if (prettyError && prettyError.constructor && prettyError.constructor.name) {
788
+ return prettyError.constructor.name;
931
789
  }
932
- onMessage(callback) {
933
- this._rawIpc.addEventListener('message', callback);
790
+ return undefined;
791
+ };
792
+ const getErrorProperty = (error, prettyError) => {
793
+ if (error && error.code === E_COMMAND_NOT_FOUND) {
794
+ return {
795
+ code: MethodNotFound,
796
+ message: error.message,
797
+ data: error.stack
798
+ };
934
799
  }
935
- onClose(callback) {}
936
- }
937
- const wrap$2 = messagePort => {
938
- return new IpcParentWithMessagePort(messagePort);
800
+ return {
801
+ code: Custom,
802
+ message: prettyError.message,
803
+ data: {
804
+ stack: prettyError.stack,
805
+ codeFrame: prettyError.codeFrame,
806
+ type: getErrorType(prettyError),
807
+ code: prettyError.code,
808
+ name: prettyError.name
809
+ }
810
+ };
939
811
  };
940
- const IpcParentWithMessagePort$1 = {
941
- __proto__: null,
942
- create: create$2$1,
943
- signal,
944
- wrap: wrap$2
812
+ const create$1$2 = (message, error) => {
813
+ return {
814
+ jsonrpc: Two,
815
+ id: message.id,
816
+ error
817
+ };
945
818
  };
946
- const Open = 1;
947
- const Close = 2;
948
- const stringifyCompact = value => {
949
- return JSON.stringify(value);
819
+ const getErrorResponse = (message, error, preparePrettyError, logError) => {
820
+ const prettyError = preparePrettyError(error);
821
+ logError(error, prettyError);
822
+ const errorProperty = getErrorProperty(error, prettyError);
823
+ return create$1$2(message, errorProperty);
950
824
  };
951
- const parse = content => {
952
- if (content === 'undefined') {
953
- return null;
954
- }
825
+ const create$6 = (message, result) => {
826
+ return {
827
+ jsonrpc: Two,
828
+ id: message.id,
829
+ result: result ?? null
830
+ };
831
+ };
832
+ const getSuccessResponse = (message, result) => {
833
+ const resultProperty = result ?? null;
834
+ return create$6(message, resultProperty);
835
+ };
836
+ const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
955
837
  try {
956
- return JSON.parse(content);
838
+ const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
839
+ return getSuccessResponse(message, result);
957
840
  } catch (error) {
958
- throw new VError(error, 'failed to parse json');
841
+ return getErrorResponse(message, error, preparePrettyError, logError);
959
842
  }
960
843
  };
961
- const waitForWebSocketToBeOpen = webSocket => {
962
- return getFirstEvent(webSocket, {
963
- open: Open,
964
- close: Close
965
- });
844
+ const defaultPreparePrettyError = error => {
845
+ return error;
966
846
  };
967
- const create$3 = async ({
968
- webSocket
969
- }) => {
970
- const firstWebSocketEvent = await waitForWebSocketToBeOpen(webSocket);
971
- // @ts-ignore
972
- if (firstWebSocketEvent.type === Close) {
973
- throw new IpcError('Websocket connection was immediately closed');
974
- }
975
- return webSocket;
847
+ const defaultLogError = () => {
848
+ // ignore
976
849
  };
977
- class IpcParentWithWebSocket extends Ipc {
978
- getData(event) {
979
- return parse(event.data);
980
- }
981
- send(message) {
982
- this._rawIpc.send(stringifyCompact(message));
983
- }
984
- sendAndTransfer(message) {
985
- throw new Error('sendAndTransfer not supported');
850
+ const defaultRequiresSocket = () => {
851
+ return false;
852
+ };
853
+ const defaultResolve = resolve;
854
+
855
+ // TODO maybe remove this in v6 or v7, only accept options object to simplify the code
856
+ const normalizeParams = args => {
857
+ if (args.length === 1) {
858
+ const options = args[0];
859
+ return {
860
+ ipc: options.ipc,
861
+ message: options.message,
862
+ execute: options.execute,
863
+ resolve: options.resolve || defaultResolve,
864
+ preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
865
+ logError: options.logError || defaultLogError,
866
+ requiresSocket: options.requiresSocket || defaultRequiresSocket
867
+ };
986
868
  }
987
- dispose() {
988
- this._rawIpc.close();
869
+ return {
870
+ ipc: args[0],
871
+ message: args[1],
872
+ execute: args[2],
873
+ resolve: args[3],
874
+ preparePrettyError: args[4],
875
+ logError: args[5],
876
+ requiresSocket: args[6]
877
+ };
878
+ };
879
+ const handleJsonRpcMessage = async (...args) => {
880
+ const options = normalizeParams(args);
881
+ const {
882
+ message,
883
+ ipc,
884
+ execute,
885
+ resolve,
886
+ preparePrettyError,
887
+ logError,
888
+ requiresSocket
889
+ } = options;
890
+ if ('id' in message) {
891
+ if ('method' in message) {
892
+ const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
893
+ try {
894
+ ipc.send(response);
895
+ } catch (error) {
896
+ const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
897
+ ipc.send(errorResponse);
898
+ }
899
+ return;
900
+ }
901
+ resolve(message.id, message);
902
+ return;
989
903
  }
990
- onClose(callback) {
991
- this._rawIpc.addEventListener('close', callback);
904
+ if ('method' in message) {
905
+ await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
906
+ return;
992
907
  }
993
- onMessage(callback) {
994
- this._rawIpc.addEventListener('message', callback);
908
+ throw new JsonRpcError('unexpected message');
909
+ };
910
+ const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
911
+ const {
912
+ message,
913
+ promise
914
+ } = create$2$1(method, params);
915
+ if (useSendAndTransfer && ipc.sendAndTransfer) {
916
+ ipc.sendAndTransfer(message);
917
+ } else {
918
+ ipc.send(message);
995
919
  }
996
- }
997
- const wrap$1 = webSocket => {
998
- return new IpcParentWithWebSocket(webSocket);
920
+ const responseMessage = await promise;
921
+ return unwrapJsonRpcResult(responseMessage);
999
922
  };
1000
- const IpcParentWithWebSocket$1 = {
1001
- __proto__: null,
1002
- create: create$3,
1003
- wrap: wrap$1
923
+ const send = (transport, method, ...params) => {
924
+ const message = create$4(method, params);
925
+ transport.send(message);
926
+ };
927
+ const invoke$3 = (ipc, method, ...params) => {
928
+ return invokeHelper(ipc, method, params, false);
929
+ };
930
+ const invokeAndTransfer$1 = (ipc, method, ...params) => {
931
+ return invokeHelper(ipc, method, params, true);
932
+ };
933
+
934
+ const commands = Object.create(null);
935
+ const register = commandMap => {
936
+ Object.assign(commands, commandMap);
937
+ };
938
+ const getCommand = key => {
939
+ return commands[key];
940
+ };
941
+ const execute = (command, ...args) => {
942
+ const fn = getCommand(command);
943
+ if (!fn) {
944
+ throw new Error(`command not found ${command}`);
945
+ }
946
+ return fn(...args);
1004
947
  };
1005
948
 
1006
949
  const createRpc = ipc => {
@@ -1030,7 +973,8 @@ const logError = () => {
1030
973
  // handled by renderer worker
1031
974
  };
1032
975
  const handleMessage = event => {
1033
- return handleJsonRpcMessage(event.target, event.data, execute, resolve, preparePrettyError, logError, requiresSocket);
976
+ const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
977
+ return handleJsonRpcMessage(event.target, event.data, execute, resolve, preparePrettyError, logError, actualRequiresSocket);
1034
978
  };
1035
979
  const handleIpc = ipc => {
1036
980
  if ('addEventListener' in ipc) {
@@ -2434,17 +2378,22 @@ const handleError = error => {
2434
2378
  console.error(`[text-search-worker] ${error}`);
2435
2379
  };
2436
2380
 
2437
- const getFileIcons = async files => {
2381
+ const isFile = match => {
2382
+ return match.type === File$1;
2383
+ };
2384
+ const getFiles = matches => {
2385
+ return matches.filter(isFile);
2386
+ };
2387
+
2388
+ const getFileIcon = file => {
2389
+ return invoke$2('IconTheme.getFileIcon', {
2390
+ name: file.text
2391
+ });
2392
+ };
2393
+ const getFileIcons = async matches => {
2438
2394
  // TODO cache file icons
2439
- const promises = [];
2440
- for (const file of files) {
2441
- if (file.type === Match) {
2442
- continue;
2443
- }
2444
- promises.push(invoke$2('IconTheme.getFileIcon', {
2445
- name: file.text
2446
- }));
2447
- }
2395
+ const files = getFiles(matches);
2396
+ const promises = files.map(getFileIcon);
2448
2397
  const icons = await Promise.all(promises);
2449
2398
  return icons;
2450
2399
  };
@@ -2540,7 +2489,7 @@ const getJson = async url => {
2540
2489
  const json = await response.json();
2541
2490
  return json;
2542
2491
  } catch (error) {
2543
- throw new VError$1(error, `Failed to get json`);
2492
+ throw new VError(error, `Failed to get json`);
2544
2493
  }
2545
2494
  };
2546
2495
 
@@ -2553,7 +2502,7 @@ const getText = async url => {
2553
2502
  const text = await response.text();
2554
2503
  return text;
2555
2504
  } catch (error) {
2556
- throw new VError$1(error, `Failed to get text`);
2505
+ throw new VError(error, `Failed to get text`);
2557
2506
  }
2558
2507
  };
2559
2508
 
@@ -2629,7 +2578,7 @@ const getChildHandles = async handle => {
2629
2578
  try {
2630
2579
  return await getChildHandles$1(handle);
2631
2580
  } catch (error) {
2632
- throw new VError$1(error, 'failed to get child handles');
2581
+ throw new VError(error, 'failed to get child handles');
2633
2582
  }
2634
2583
  };
2635
2584
 
@@ -2915,6 +2864,7 @@ const getHandleDb = async () => {
2915
2864
  });
2916
2865
  return db;
2917
2866
  };
2867
+
2918
2868
  const getHandle$1 = async uri => {
2919
2869
  const handleDb = await getHandleDb();
2920
2870
  const handle = await handleDb.get('file-handles-store', uri);
@@ -2928,7 +2878,7 @@ const getHandle = async uri => {
2928
2878
  const handle = await getHandle$1(uri);
2929
2879
  return handle;
2930
2880
  } catch (error) {
2931
- throw new VError$1(error, 'Failed to get handle');
2881
+ throw new VError(error, 'Failed to get handle');
2932
2882
  }
2933
2883
  };
2934
2884