@lvce-editor/file-search-worker 3.1.0 → 3.3.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,368 +1,108 @@
1
- const Two = '2.0';
2
- const create$4 = (method, params) => {
3
- return {
4
- jsonrpc: Two,
5
- method,
6
- params
7
- };
8
- };
9
- const state$3 = {
10
- callbacks: Object.create(null)
11
- };
12
- const set = (id, fn) => {
13
- state$3.callbacks[id] = fn;
14
- };
15
- const get = id => {
16
- return state$3.callbacks[id];
17
- };
18
- const remove$2 = id => {
19
- delete state$3.callbacks[id];
20
- };
21
- let id = 0;
22
- const create$3 = () => {
23
- return ++id;
24
- };
25
- const warn = (...args) => {
26
- console.warn(...args);
27
- };
28
- const registerPromise = () => {
29
- const id = create$3();
30
- const {
31
- resolve,
32
- promise
33
- } = Promise.withResolvers();
34
- set(id, resolve);
35
- return {
36
- id,
37
- promise
38
- };
39
- };
40
- const resolve = (id, response) => {
41
- const fn = get(id);
42
- if (!fn) {
43
- console.log(response);
44
- warn(`callback ${id} may already be disposed`);
45
- return;
46
- }
47
- fn(response);
48
- remove$2(id);
49
- };
50
- const create$2 = (method, params) => {
51
- const {
52
- id,
53
- promise
54
- } = registerPromise();
55
- const message = {
56
- jsonrpc: Two,
57
- method,
58
- params,
59
- id
60
- };
61
- return {
62
- message,
63
- promise
64
- };
65
- };
66
- class JsonRpcError extends Error {
67
- constructor(message) {
68
- super(message);
69
- this.name = 'JsonRpcError';
70
- }
71
- }
72
- const NewLine$3 = '\n';
73
- const DomException = 'DOMException';
74
- const ReferenceError$1 = 'ReferenceError';
75
- const SyntaxError$1 = 'SyntaxError';
76
- const TypeError$1 = 'TypeError';
77
- const getErrorConstructor = (message, type) => {
78
- if (type) {
79
- switch (type) {
80
- case DomException:
81
- return DOMException;
82
- case TypeError$1:
83
- return TypeError;
84
- case SyntaxError$1:
85
- return SyntaxError;
86
- case ReferenceError$1:
87
- return ReferenceError;
88
- default:
89
- return Error;
90
- }
91
- }
92
- if (message.startsWith('TypeError: ')) {
93
- return TypeError;
94
- }
95
- if (message.startsWith('SyntaxError: ')) {
96
- return SyntaxError;
97
- }
98
- if (message.startsWith('ReferenceError: ')) {
99
- return ReferenceError;
100
- }
101
- return Error;
102
- };
103
- const constructError = (message, type, name) => {
104
- const ErrorConstructor = getErrorConstructor(message, type);
105
- if (ErrorConstructor === DOMException && name) {
106
- return new ErrorConstructor(message, name);
107
- }
108
- if (ErrorConstructor === Error) {
109
- const error = new Error(message);
110
- if (name && name !== 'VError') {
111
- error.name = name;
112
- }
113
- return error;
114
- }
115
- return new ErrorConstructor(message);
116
- };
117
- const getNewLineIndex$2 = (string, startIndex = undefined) => {
118
- return string.indexOf(NewLine$3, startIndex);
119
- };
120
- const getParentStack = error => {
121
- let parentStack = error.stack || error.data || error.message || '';
122
- if (parentStack.startsWith(' at')) {
123
- parentStack = error.message + NewLine$3 + parentStack;
124
- }
125
- return parentStack;
126
- };
127
- const joinLines$1 = lines => {
128
- return lines.join(NewLine$3);
129
- };
130
- const MethodNotFound = -32601;
131
- const Custom = -32001;
132
- const splitLines$2 = lines => {
133
- return lines.split(NewLine$3);
134
- };
135
- const restoreJsonRpcError = error => {
136
- if (error && error instanceof Error) {
137
- return error;
138
- }
139
- const currentStack = joinLines$1(splitLines$2(new Error().stack || '').slice(1));
140
- if (error && error.code && error.code === MethodNotFound) {
141
- const restoredError = new JsonRpcError(error.message);
142
- const parentStack = getParentStack(error);
143
- restoredError.stack = parentStack + NewLine$3 + currentStack;
144
- return restoredError;
145
- }
146
- if (error && error.message) {
147
- const restoredError = constructError(error.message, error.type, error.name);
148
- if (error.data) {
149
- if (error.data.stack && error.data.type && error.message) {
150
- restoredError.stack = error.data.type + ': ' + error.message + NewLine$3 + error.data.stack + NewLine$3 + currentStack;
151
- } else if (error.data.stack) {
152
- restoredError.stack = error.data.stack;
153
- }
154
- if (error.data.codeFrame) {
155
- // @ts-ignore
156
- restoredError.codeFrame = error.data.codeFrame;
157
- }
158
- if (error.data.code) {
159
- // @ts-ignore
160
- restoredError.code = error.data.code;
161
- }
162
- if (error.data.type) {
163
- // @ts-ignore
164
- restoredError.name = error.data.type;
165
- }
166
- } else {
167
- if (error.stack) {
168
- const lowerStack = restoredError.stack || '';
169
- // @ts-ignore
170
- const indexNewLine = getNewLineIndex$2(lowerStack);
171
- const parentStack = getParentStack(error);
172
- // @ts-ignore
173
- restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
174
- }
175
- if (error.codeFrame) {
176
- // @ts-ignore
177
- restoredError.codeFrame = error.codeFrame;
178
- }
179
- }
180
- return restoredError;
181
- }
182
- if (typeof error === 'string') {
183
- return new Error(`JsonRpc Error: ${error}`);
184
- }
185
- return new Error(`JsonRpc Error: ${error}`);
186
- };
187
- const unwrapJsonRpcResult = responseMessage => {
188
- if ('error' in responseMessage) {
189
- const restoredError = restoreJsonRpcError(responseMessage.error);
190
- throw restoredError;
191
- }
192
- if ('result' in responseMessage) {
193
- return responseMessage.result;
194
- }
195
- throw new JsonRpcError('unexpected response message');
196
- };
197
- const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
198
- const getErrorType = prettyError => {
199
- if (prettyError && prettyError.type) {
200
- return prettyError.type;
201
- }
202
- if (prettyError && prettyError.constructor && prettyError.constructor.name) {
203
- return prettyError.constructor.name;
1
+ const normalizeLine = line => {
2
+ if (line.startsWith('Error: ')) {
3
+ return line.slice('Error: '.length);
204
4
  }
205
- return undefined;
206
- };
207
- const getErrorProperty = (error, prettyError) => {
208
- if (error && error.code === E_COMMAND_NOT_FOUND) {
209
- return {
210
- code: MethodNotFound,
211
- message: error.message,
212
- data: error.stack
213
- };
5
+ if (line.startsWith('VError: ')) {
6
+ return line.slice('VError: '.length);
214
7
  }
215
- return {
216
- code: Custom,
217
- message: prettyError.message,
218
- data: {
219
- stack: prettyError.stack,
220
- codeFrame: prettyError.codeFrame,
221
- type: getErrorType(prettyError),
222
- code: prettyError.code,
223
- name: prettyError.name
224
- }
225
- };
226
- };
227
- const create$1$1 = (message, error) => {
228
- return {
229
- jsonrpc: Two,
230
- id: message.id,
231
- error
232
- };
233
- };
234
- const getErrorResponse = (message, error, preparePrettyError, logError) => {
235
- const prettyError = preparePrettyError(error);
236
- logError(error, prettyError);
237
- const errorProperty = getErrorProperty(error, prettyError);
238
- return create$1$1(message, errorProperty);
239
- };
240
- const create = (message, result) => {
241
- return {
242
- jsonrpc: Two,
243
- id: message.id,
244
- result: result ?? null
245
- };
246
- };
247
- const getSuccessResponse = (message, result) => {
248
- const resultProperty = result ?? null;
249
- return create(message, resultProperty);
8
+ return line;
250
9
  };
251
- const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
252
- try {
253
- const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
254
- return getSuccessResponse(message, result);
255
- } catch (error) {
256
- return getErrorResponse(message, error, preparePrettyError, logError);
10
+ const getCombinedMessage = (error, message) => {
11
+ const stringifiedError = normalizeLine(`${error}`);
12
+ if (message) {
13
+ return `${message}: ${stringifiedError}`;
257
14
  }
15
+ return stringifiedError;
258
16
  };
259
- const defaultPreparePrettyError = error => {
260
- return error;
261
- };
262
- const defaultLogError = () => {
263
- // ignore
264
- };
265
- const defaultRequiresSocket = () => {
266
- return false;
17
+ const NewLine$2 = '\n';
18
+ const getNewLineIndex$1 = (string, startIndex = undefined) => {
19
+ return string.indexOf(NewLine$2, startIndex);
267
20
  };
268
- const defaultResolve = resolve;
269
-
270
- // TODO maybe remove this in v6 or v7, only accept options object to simplify the code
271
- const normalizeParams = args => {
272
- if (args.length === 1) {
273
- const options = args[0];
274
- return {
275
- ipc: options.ipc,
276
- message: options.message,
277
- execute: options.execute,
278
- resolve: options.resolve || defaultResolve,
279
- preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
280
- logError: options.logError || defaultLogError,
281
- requiresSocket: options.requiresSocket || defaultRequiresSocket
282
- };
21
+ const mergeStacks = (parent, child) => {
22
+ if (!child) {
23
+ return parent;
283
24
  }
284
- return {
285
- ipc: args[0],
286
- message: args[1],
287
- execute: args[2],
288
- resolve: args[3],
289
- preparePrettyError: args[4],
290
- logError: args[5],
291
- requiresSocket: args[6]
292
- };
293
- };
294
- const handleJsonRpcMessage = async (...args) => {
295
- const options = normalizeParams(args);
296
- const {
297
- message,
298
- ipc,
299
- execute,
300
- resolve,
301
- preparePrettyError,
302
- logError,
303
- requiresSocket
304
- } = options;
305
- if ('id' in message) {
306
- if ('method' in message) {
307
- const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
308
- try {
309
- ipc.send(response);
310
- } catch (error) {
311
- const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
312
- ipc.send(errorResponse);
313
- }
314
- return;
315
- }
316
- resolve(message.id, message);
317
- return;
25
+ const parentNewLineIndex = getNewLineIndex$1(parent);
26
+ const childNewLineIndex = getNewLineIndex$1(child);
27
+ if (childNewLineIndex === -1) {
28
+ return parent;
318
29
  }
319
- if ('method' in message) {
320
- await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
321
- return;
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;
322
35
  }
323
- throw new JsonRpcError('unexpected message');
36
+ return child;
324
37
  };
325
- const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
326
- const {
327
- message,
328
- promise
329
- } = create$2(method, params);
330
- if (useSendAndTransfer && ipc.sendAndTransfer) {
331
- ipc.sendAndTransfer(message);
332
- } else {
333
- ipc.send(message);
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
+ }
334
54
  }
335
- const responseMessage = await promise;
336
- return unwrapJsonRpcResult(responseMessage);
55
+ }
56
+
57
+ const isMessagePort = value => {
58
+ return value && value instanceof MessagePort;
337
59
  };
338
- const send = (transport, method, ...params) => {
339
- const message = create$4(method, params);
340
- transport.send(message);
60
+ const isMessagePortMain = value => {
61
+ return value && value.constructor && value.constructor.name === 'MessagePortMain';
341
62
  };
342
- const invoke$2 = (ipc, method, ...params) => {
343
- return invokeHelper(ipc, method, params, false);
63
+ const isOffscreenCanvas = value => {
64
+ return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
344
65
  };
345
- const invokeAndTransfer = (ipc, method, ...params) => {
346
- return invokeHelper(ipc, method, params, true);
66
+ const isInstanceOf = (value, constructorName) => {
67
+ return value?.constructor?.name === constructorName;
347
68
  };
348
-
349
- const commands = Object.create(null);
350
- const register = commandMap => {
351
- Object.assign(commands, commandMap);
69
+ const isSocket = value => {
70
+ return isInstanceOf(value, 'Socket');
352
71
  };
353
- const getCommand = key => {
354
- return commands[key];
72
+ const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
73
+ const isTransferrable = value => {
74
+ for (const fn of transferrables) {
75
+ if (fn(value)) {
76
+ return true;
77
+ }
78
+ }
79
+ return false;
355
80
  };
356
- const execute = (command, ...args) => {
357
- const fn = getCommand(command);
358
- if (!fn) {
359
- throw new Error(`command not found ${command}`);
81
+ const walkValue = (value, transferrables, isTransferrable) => {
82
+ if (!value) {
83
+ return;
84
+ }
85
+ if (isTransferrable(value)) {
86
+ transferrables.push(value);
87
+ return;
88
+ }
89
+ if (Array.isArray(value)) {
90
+ for (const item of value) {
91
+ walkValue(item, transferrables, isTransferrable);
92
+ }
93
+ return;
94
+ }
95
+ if (typeof value === 'object') {
96
+ for (const property of Object.values(value)) {
97
+ walkValue(property, transferrables, isTransferrable);
98
+ }
99
+ return;
360
100
  }
361
- return fn(...args);
362
101
  };
363
-
364
- const getData$1 = event => {
365
- return event.data;
102
+ const getTransferrables = value => {
103
+ const transferrables = [];
104
+ walkValue(value, transferrables, isTransferrable);
105
+ return transferrables;
366
106
  };
367
107
  const attachEvents = that => {
368
108
  const handleMessage = (...args) => {
@@ -384,70 +124,162 @@ class Ipc extends EventTarget {
384
124
  attachEvents(this);
385
125
  }
386
126
  }
387
- const readyMessage = 'ready';
388
- const walkValue = (value, transferrables, isTransferrable) => {
389
- if (!value) {
390
- return;
391
- }
392
- if (isTransferrable(value)) {
393
- transferrables.push(value);
394
- return;
395
- }
396
- if (Array.isArray(value)) {
397
- for (const item of value) {
398
- walkValue(item, transferrables, isTransferrable);
399
- }
400
- return;
401
- }
402
- if (typeof value === 'object') {
403
- for (const property of Object.values(value)) {
404
- walkValue(property, transferrables, isTransferrable);
405
- }
406
- return;
407
- }
127
+ const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
128
+ const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
129
+ const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
130
+ const NewLine$1 = '\n';
131
+ const splitLines$2 = lines => {
132
+ return lines.split(NewLine$1);
408
133
  };
409
- const isMessagePort = value => {
410
- return value && value instanceof MessagePort;
134
+ const isModuleNotFoundMessage = line => {
135
+ return line.includes('[ERR_MODULE_NOT_FOUND]');
411
136
  };
412
- const isMessagePortMain = value => {
413
- return value && value.constructor && value.constructor.name === 'MessagePortMain';
137
+ const getModuleNotFoundError = stderr => {
138
+ const lines = splitLines$2(stderr);
139
+ const messageIndex = lines.findIndex(isModuleNotFoundMessage);
140
+ const message = lines[messageIndex];
141
+ return {
142
+ message,
143
+ code: ERR_MODULE_NOT_FOUND
144
+ };
414
145
  };
415
- const isOffscreenCanvas = value => {
416
- return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
146
+ const joinLines$1 = lines => {
147
+ return lines.join(NewLine$1);
417
148
  };
418
- const isInstanceOf = (value, constructorName) => {
419
- return value?.constructor?.name === constructorName;
149
+ const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
150
+ const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
151
+ const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
152
+ const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
153
+ const RE_AT = /^\s+at/;
154
+ const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
155
+ const isUnhelpfulNativeModuleError = stderr => {
156
+ return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
420
157
  };
421
- const isSocket = value => {
422
- return isInstanceOf(value, 'Socket');
158
+ const isMessageCodeBlockStartIndex = line => {
159
+ return RE_MESSAGE_CODE_BLOCK_START.test(line);
423
160
  };
424
- const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
425
- const isTransferrable = value => {
426
- for (const fn of transferrables) {
427
- if (fn(value)) {
428
- return true;
161
+ const isMessageCodeBlockEndIndex = line => {
162
+ return RE_MESSAGE_CODE_BLOCK_END.test(line);
163
+ };
164
+ const getMessageCodeBlock = stderr => {
165
+ const lines = splitLines$2(stderr);
166
+ const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
167
+ const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
168
+ const relevantLines = lines.slice(startIndex, endIndex);
169
+ const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
170
+ return relevantMessage;
171
+ };
172
+ const getNativeModuleErrorMessage = stderr => {
173
+ const message = getMessageCodeBlock(stderr);
174
+ return {
175
+ message: `Incompatible native node module: ${message}`,
176
+ code: E_INCOMPATIBLE_NATIVE_MODULE
177
+ };
178
+ };
179
+ const isModulesSyntaxError = stderr => {
180
+ if (!stderr) {
181
+ return false;
182
+ }
183
+ return stderr.includes('SyntaxError: Cannot use import statement outside a module');
184
+ };
185
+ const getModuleSyntaxError = () => {
186
+ return {
187
+ message: `ES Modules are not supported in electron`,
188
+ code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
189
+ };
190
+ };
191
+ const isModuleNotFoundError = stderr => {
192
+ if (!stderr) {
193
+ return false;
194
+ }
195
+ return stderr.includes('ERR_MODULE_NOT_FOUND');
196
+ };
197
+ const isNormalStackLine = line => {
198
+ return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
199
+ };
200
+ const getDetails = lines => {
201
+ const index = lines.findIndex(isNormalStackLine);
202
+ if (index === -1) {
203
+ return {
204
+ actualMessage: joinLines$1(lines),
205
+ rest: []
206
+ };
207
+ }
208
+ let lastIndex = index - 1;
209
+ while (++lastIndex < lines.length) {
210
+ if (!isNormalStackLine(lines[lastIndex])) {
211
+ break;
429
212
  }
430
213
  }
431
- return false;
214
+ return {
215
+ actualMessage: lines[index - 1],
216
+ rest: lines.slice(index, lastIndex)
217
+ };
432
218
  };
433
- const getTransferrables = value => {
434
- const transferrables = [];
435
- walkValue(value, transferrables, isTransferrable);
436
- return transferrables;
219
+ const getHelpfulChildProcessError = (stdout, stderr) => {
220
+ if (isUnhelpfulNativeModuleError(stderr)) {
221
+ return getNativeModuleErrorMessage(stderr);
222
+ }
223
+ if (isModulesSyntaxError(stderr)) {
224
+ return getModuleSyntaxError();
225
+ }
226
+ if (isModuleNotFoundError(stderr)) {
227
+ return getModuleNotFoundError(stderr);
228
+ }
229
+ const lines = splitLines$2(stderr);
230
+ const {
231
+ actualMessage,
232
+ rest
233
+ } = getDetails(lines);
234
+ return {
235
+ message: actualMessage,
236
+ code: '',
237
+ stack: rest
238
+ };
239
+ };
240
+ class IpcError extends VError {
241
+ // @ts-ignore
242
+ constructor(betterMessage, stdout = '', stderr = '') {
243
+ if (stdout || stderr) {
244
+ // @ts-ignore
245
+ const {
246
+ message,
247
+ code,
248
+ stack
249
+ } = getHelpfulChildProcessError(stdout, stderr);
250
+ const cause = new Error(message);
251
+ // @ts-ignore
252
+ cause.code = code;
253
+ cause.stack = stack;
254
+ super(cause, betterMessage);
255
+ } else {
256
+ super(betterMessage);
257
+ }
258
+ // @ts-ignore
259
+ this.name = 'IpcError';
260
+ // @ts-ignore
261
+ this.stdout = stdout;
262
+ // @ts-ignore
263
+ this.stderr = stderr;
264
+ }
265
+ }
266
+ const readyMessage = 'ready';
267
+ const getData$2 = event => {
268
+ return event.data;
437
269
  };
438
- const listen$2 = () => {
270
+ const listen$7 = () => {
439
271
  // @ts-ignore
440
272
  if (typeof WorkerGlobalScope === 'undefined') {
441
273
  throw new TypeError('module is not in web worker scope');
442
274
  }
443
275
  return globalThis;
444
276
  };
445
- const signal$2 = global => {
277
+ const signal$7 = global => {
446
278
  global.postMessage(readyMessage);
447
279
  };
448
280
  class IpcChildWithModuleWorker extends Ipc {
449
281
  getData(event) {
450
- return getData$1(event);
282
+ return getData$2(event);
451
283
  }
452
284
  send(message) {
453
285
  // @ts-ignore
@@ -468,280 +300,445 @@ class IpcChildWithModuleWorker extends Ipc {
468
300
  this._rawIpc.addEventListener('message', callback);
469
301
  }
470
302
  }
471
- const wrap$5 = global => {
303
+ const wrap$e = global => {
472
304
  return new IpcChildWithModuleWorker(global);
473
305
  };
474
- const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
475
- const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
476
- const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
477
- const NewLine$1 = '\n';
478
- const joinLines = lines => {
479
- return lines.join(NewLine$1);
306
+ const withResolvers = () => {
307
+ let _resolve;
308
+ const promise = new Promise(resolve => {
309
+ _resolve = resolve;
310
+ });
311
+ return {
312
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
313
+ resolve: _resolve,
314
+ promise
315
+ };
480
316
  };
481
- const splitLines$1 = lines => {
482
- return lines.split(NewLine$1);
317
+ const waitForFirstMessage = async port => {
318
+ const {
319
+ resolve,
320
+ promise
321
+ } = withResolvers();
322
+ port.addEventListener('message', resolve, {
323
+ once: true
324
+ });
325
+ const event = await promise;
326
+ // @ts-ignore
327
+ return event.data;
483
328
  };
484
- const isModuleNotFoundMessage = line => {
485
- return line.includes('[ERR_MODULE_NOT_FOUND]');
329
+ const listen$6 = async () => {
330
+ const parentIpcRaw = listen$7();
331
+ signal$7(parentIpcRaw);
332
+ const parentIpc = wrap$e(parentIpcRaw);
333
+ const firstMessage = await waitForFirstMessage(parentIpc);
334
+ if (firstMessage.method !== 'initialize') {
335
+ throw new IpcError('unexpected first message');
336
+ }
337
+ const type = firstMessage.params[0];
338
+ if (type === 'message-port') {
339
+ parentIpc.send({
340
+ jsonrpc: '2.0',
341
+ id: firstMessage.id,
342
+ result: null
343
+ });
344
+ parentIpc.dispose();
345
+ const port = firstMessage.params[1];
346
+ return port;
347
+ }
348
+ return globalThis;
486
349
  };
487
- const getModuleNotFoundError = stderr => {
488
- const lines = splitLines$1(stderr);
489
- const messageIndex = lines.findIndex(isModuleNotFoundMessage);
490
- const message = lines[messageIndex];
350
+ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
351
+ getData(event) {
352
+ return getData$2(event);
353
+ }
354
+ send(message) {
355
+ this._rawIpc.postMessage(message);
356
+ }
357
+ sendAndTransfer(message) {
358
+ const transfer = getTransferrables(message);
359
+ this._rawIpc.postMessage(message, transfer);
360
+ }
361
+ dispose() {
362
+ if (this._rawIpc.close) {
363
+ this._rawIpc.close();
364
+ }
365
+ }
366
+ onClose(callback) {
367
+ // ignore
368
+ }
369
+ onMessage(callback) {
370
+ this._rawIpc.addEventListener('message', callback);
371
+ this._rawIpc.start();
372
+ }
373
+ }
374
+ const wrap$d = port => {
375
+ return new IpcChildWithModuleWorkerAndMessagePort(port);
376
+ };
377
+ const IpcChildWithModuleWorkerAndMessagePort$1 = {
378
+ __proto__: null,
379
+ listen: listen$6,
380
+ wrap: wrap$d
381
+ };
382
+
383
+ const Two = '2.0';
384
+ const create$4 = (method, params) => {
491
385
  return {
492
- message,
493
- code: ERR_MODULE_NOT_FOUND
386
+ jsonrpc: Two,
387
+ method,
388
+ params
494
389
  };
495
390
  };
496
- const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
497
- const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
498
- const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
499
- const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
500
- const RE_AT = /^\s+at/;
501
- const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
502
- const isUnhelpfulNativeModuleError = stderr => {
503
- return RE_NATIVE_MODULE_ERROR.test(stderr) && RE_NATIVE_MODULE_ERROR_2.test(stderr);
391
+ const callbacks = Object.create(null);
392
+ const set = (id, fn) => {
393
+ callbacks[id] = fn;
504
394
  };
505
- const isMessageCodeBlockStartIndex = line => {
506
- return RE_MESSAGE_CODE_BLOCK_START.test(line);
395
+ const get = id => {
396
+ return callbacks[id];
507
397
  };
508
- const isMessageCodeBlockEndIndex = line => {
509
- return RE_MESSAGE_CODE_BLOCK_END.test(line);
398
+ const remove$2 = id => {
399
+ delete callbacks[id];
510
400
  };
511
- const getMessageCodeBlock = stderr => {
512
- const lines = splitLines$1(stderr);
513
- const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
514
- const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
515
- const relevantLines = lines.slice(startIndex, endIndex);
516
- const relevantMessage = relevantLines.join(' ').slice('Error: '.length);
517
- return relevantMessage;
401
+ let id = 0;
402
+ const create$3 = () => {
403
+ return ++id;
518
404
  };
519
- const getNativeModuleErrorMessage = stderr => {
520
- const message = getMessageCodeBlock(stderr);
405
+ const registerPromise = () => {
406
+ const id = create$3();
407
+ const {
408
+ resolve,
409
+ promise
410
+ } = Promise.withResolvers();
411
+ set(id, resolve);
521
412
  return {
522
- message: `Incompatible native node module: ${message}`,
523
- code: E_INCOMPATIBLE_NATIVE_MODULE
413
+ id,
414
+ promise
524
415
  };
525
416
  };
526
- const isModulesSyntaxError = stderr => {
527
- if (!stderr) {
528
- return false;
417
+ const create$2 = (method, params) => {
418
+ const {
419
+ id,
420
+ promise
421
+ } = registerPromise();
422
+ const message = {
423
+ jsonrpc: Two,
424
+ method,
425
+ params,
426
+ id
427
+ };
428
+ return {
429
+ message,
430
+ promise
431
+ };
432
+ };
433
+ class JsonRpcError extends Error {
434
+ constructor(message) {
435
+ super(message);
436
+ this.name = 'JsonRpcError';
437
+ }
438
+ }
439
+ const NewLine = '\n';
440
+ const DomException = 'DOMException';
441
+ const ReferenceError$1 = 'ReferenceError';
442
+ const SyntaxError$1 = 'SyntaxError';
443
+ const TypeError$1 = 'TypeError';
444
+ const getErrorConstructor = (message, type) => {
445
+ if (type) {
446
+ switch (type) {
447
+ case DomException:
448
+ return DOMException;
449
+ case TypeError$1:
450
+ return TypeError;
451
+ case SyntaxError$1:
452
+ return SyntaxError;
453
+ case ReferenceError$1:
454
+ return ReferenceError;
455
+ default:
456
+ return Error;
457
+ }
458
+ }
459
+ if (message.startsWith('TypeError: ')) {
460
+ return TypeError;
461
+ }
462
+ if (message.startsWith('SyntaxError: ')) {
463
+ return SyntaxError;
464
+ }
465
+ if (message.startsWith('ReferenceError: ')) {
466
+ return ReferenceError;
467
+ }
468
+ return Error;
469
+ };
470
+ const constructError = (message, type, name) => {
471
+ const ErrorConstructor = getErrorConstructor(message, type);
472
+ if (ErrorConstructor === DOMException && name) {
473
+ return new ErrorConstructor(message, name);
474
+ }
475
+ if (ErrorConstructor === Error) {
476
+ const error = new Error(message);
477
+ if (name && name !== 'VError') {
478
+ error.name = name;
479
+ }
480
+ return error;
481
+ }
482
+ return new ErrorConstructor(message);
483
+ };
484
+ const getNewLineIndex = (string, startIndex = undefined) => {
485
+ return string.indexOf(NewLine, startIndex);
486
+ };
487
+ const getParentStack = error => {
488
+ let parentStack = error.stack || error.data || error.message || '';
489
+ if (parentStack.startsWith(' at')) {
490
+ parentStack = error.message + NewLine + parentStack;
491
+ }
492
+ return parentStack;
493
+ };
494
+ const joinLines = lines => {
495
+ return lines.join(NewLine);
496
+ };
497
+ const MethodNotFound = -32601;
498
+ const Custom$1 = -32001;
499
+ const splitLines$1 = lines => {
500
+ return lines.split(NewLine);
501
+ };
502
+ const restoreJsonRpcError = error => {
503
+ if (error && error instanceof Error) {
504
+ return error;
505
+ }
506
+ const currentStack = joinLines(splitLines$1(new Error().stack || '').slice(1));
507
+ if (error && error.code && error.code === MethodNotFound) {
508
+ const restoredError = new JsonRpcError(error.message);
509
+ const parentStack = getParentStack(error);
510
+ restoredError.stack = parentStack + NewLine + currentStack;
511
+ return restoredError;
512
+ }
513
+ if (error && error.message) {
514
+ const restoredError = constructError(error.message, error.type, error.name);
515
+ if (error.data) {
516
+ if (error.data.stack && error.data.type && error.message) {
517
+ restoredError.stack = error.data.type + ': ' + error.message + NewLine + error.data.stack + NewLine + currentStack;
518
+ } else if (error.data.stack) {
519
+ restoredError.stack = error.data.stack;
520
+ }
521
+ if (error.data.codeFrame) {
522
+ // @ts-ignore
523
+ restoredError.codeFrame = error.data.codeFrame;
524
+ }
525
+ if (error.data.code) {
526
+ // @ts-ignore
527
+ restoredError.code = error.data.code;
528
+ }
529
+ if (error.data.type) {
530
+ // @ts-ignore
531
+ restoredError.name = error.data.type;
532
+ }
533
+ } else {
534
+ if (error.stack) {
535
+ const lowerStack = restoredError.stack || '';
536
+ // @ts-ignore
537
+ const indexNewLine = getNewLineIndex(lowerStack);
538
+ const parentStack = getParentStack(error);
539
+ // @ts-ignore
540
+ restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
541
+ }
542
+ if (error.codeFrame) {
543
+ // @ts-ignore
544
+ restoredError.codeFrame = error.codeFrame;
545
+ }
546
+ }
547
+ return restoredError;
548
+ }
549
+ if (typeof error === 'string') {
550
+ return new Error(`JsonRpc Error: ${error}`);
551
+ }
552
+ return new Error(`JsonRpc Error: ${error}`);
553
+ };
554
+ const unwrapJsonRpcResult = responseMessage => {
555
+ if ('error' in responseMessage) {
556
+ const restoredError = restoreJsonRpcError(responseMessage.error);
557
+ throw restoredError;
529
558
  }
530
- return stderr.includes('SyntaxError: Cannot use import statement outside a module');
559
+ if ('result' in responseMessage) {
560
+ return responseMessage.result;
561
+ }
562
+ throw new JsonRpcError('unexpected response message');
531
563
  };
532
- const getModuleSyntaxError = () => {
533
- return {
534
- message: `ES Modules are not supported in electron`,
535
- code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
536
- };
564
+ const warn$1 = (...args) => {
565
+ console.warn(...args);
537
566
  };
538
- const isModuleNotFoundError = stderr => {
539
- if (!stderr) {
540
- return false;
567
+ const resolve = (id, response) => {
568
+ const fn = get(id);
569
+ if (!fn) {
570
+ console.log(response);
571
+ warn$1(`callback ${id} may already be disposed`);
572
+ return;
541
573
  }
542
- return stderr.includes('ERR_MODULE_NOT_FOUND');
574
+ fn(response);
575
+ remove$2(id);
543
576
  };
544
- const isNormalStackLine = line => {
545
- return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
577
+ const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
578
+ const getErrorType = prettyError => {
579
+ if (prettyError && prettyError.type) {
580
+ return prettyError.type;
581
+ }
582
+ if (prettyError && prettyError.constructor && prettyError.constructor.name) {
583
+ return prettyError.constructor.name;
584
+ }
585
+ return undefined;
546
586
  };
547
- const getDetails = lines => {
548
- const index = lines.findIndex(isNormalStackLine);
549
- if (index === -1) {
587
+ const getErrorProperty = (error, prettyError) => {
588
+ if (error && error.code === E_COMMAND_NOT_FOUND) {
550
589
  return {
551
- actualMessage: joinLines(lines),
552
- rest: []
590
+ code: MethodNotFound,
591
+ message: error.message,
592
+ data: error.stack
553
593
  };
554
594
  }
555
- let lastIndex = index - 1;
556
- while (++lastIndex < lines.length) {
557
- if (!isNormalStackLine(lines[lastIndex])) {
558
- break;
595
+ return {
596
+ code: Custom$1,
597
+ message: prettyError.message,
598
+ data: {
599
+ stack: prettyError.stack,
600
+ codeFrame: prettyError.codeFrame,
601
+ type: getErrorType(prettyError),
602
+ code: prettyError.code,
603
+ name: prettyError.name
559
604
  }
560
- }
605
+ };
606
+ };
607
+ const create$1 = (message, error) => {
561
608
  return {
562
- actualMessage: lines[index - 1],
563
- rest: lines.slice(index, lastIndex)
609
+ jsonrpc: Two,
610
+ id: message.id,
611
+ error
564
612
  };
565
613
  };
566
- const getHelpfulChildProcessError = (stdout, stderr) => {
567
- if (isUnhelpfulNativeModuleError(stderr)) {
568
- return getNativeModuleErrorMessage(stderr);
569
- }
570
- if (isModulesSyntaxError(stderr)) {
571
- return getModuleSyntaxError();
572
- }
573
- if (isModuleNotFoundError(stderr)) {
574
- return getModuleNotFoundError(stderr);
575
- }
576
- const lines = splitLines$1(stderr);
577
- const {
578
- actualMessage,
579
- rest
580
- } = getDetails(lines);
614
+ const getErrorResponse = (message, error, preparePrettyError, logError) => {
615
+ const prettyError = preparePrettyError(error);
616
+ logError(error, prettyError);
617
+ const errorProperty = getErrorProperty(error, prettyError);
618
+ return create$1(message, errorProperty);
619
+ };
620
+ const create$5 = (message, result) => {
581
621
  return {
582
- message: `${actualMessage}`,
583
- code: '',
584
- stack: rest
622
+ jsonrpc: Two,
623
+ id: message.id,
624
+ result: result ?? null
585
625
  };
586
626
  };
587
- const normalizeLine$1 = line => {
588
- if (line.startsWith('Error: ')) {
589
- return line.slice('Error: '.length);
590
- }
591
- if (line.startsWith('VError: ')) {
592
- return line.slice('VError: '.length);
593
- }
594
- return line;
627
+ const getSuccessResponse = (message, result) => {
628
+ const resultProperty = result ?? null;
629
+ return create$5(message, resultProperty);
595
630
  };
596
- const getCombinedMessage$1 = (error, message) => {
597
- const stringifiedError = normalizeLine$1(`${error}`);
598
- if (message) {
599
- return `${message}: ${stringifiedError}`;
631
+ const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
632
+ try {
633
+ const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
634
+ return getSuccessResponse(message, result);
635
+ } catch (error) {
636
+ return getErrorResponse(message, error, preparePrettyError, logError);
600
637
  }
601
- return stringifiedError;
602
638
  };
603
- const NewLine$2 = '\n';
604
- const getNewLineIndex$1 = (string, startIndex = undefined) => {
605
- return string.indexOf(NewLine$2, startIndex);
639
+ const defaultPreparePrettyError = error => {
640
+ return error;
606
641
  };
607
- const mergeStacks$1 = (parent, child) => {
608
- if (!child) {
609
- return parent;
610
- }
611
- const parentNewLineIndex = getNewLineIndex$1(parent);
612
- const childNewLineIndex = getNewLineIndex$1(child);
613
- if (childNewLineIndex === -1) {
614
- return parent;
615
- }
616
- const parentFirstLine = parent.slice(0, parentNewLineIndex);
617
- const childRest = child.slice(childNewLineIndex);
618
- const childFirstLine = normalizeLine$1(child.slice(0, childNewLineIndex));
619
- if (parentFirstLine.includes(childFirstLine)) {
620
- return parentFirstLine + childRest;
621
- }
622
- return child;
642
+ const defaultLogError = () => {
643
+ // ignore
623
644
  };
624
- let VError$1 = class VError extends Error {
625
- constructor(error, message) {
626
- const combinedMessage = getCombinedMessage$1(error, message);
627
- super(combinedMessage);
628
- this.name = 'VError';
629
- if (error instanceof Error) {
630
- this.stack = mergeStacks$1(this.stack, error.stack);
631
- }
632
- if (error.codeFrame) {
633
- // @ts-ignore
634
- this.codeFrame = error.codeFrame;
635
- }
636
- if (error.code) {
637
- // @ts-ignore
638
- this.code = error.code;
639
- }
640
- }
645
+ const defaultRequiresSocket = () => {
646
+ return false;
641
647
  };
642
- class IpcError extends VError$1 {
643
- // @ts-ignore
644
- constructor(betterMessage, stdout = '', stderr = '') {
645
- if (stdout || stderr) {
646
- // @ts-ignore
647
- const {
648
- message,
649
- code,
650
- stack
651
- } = getHelpfulChildProcessError(stdout, stderr);
652
- const cause = new Error(message);
653
- // @ts-ignore
654
- cause.code = code;
655
- cause.stack = stack;
656
- super(cause, betterMessage);
657
- } else {
658
- super(betterMessage);
659
- }
660
- // @ts-ignore
661
- this.name = 'IpcError';
662
- // @ts-ignore
663
- this.stdout = stdout;
664
- // @ts-ignore
665
- this.stderr = stderr;
648
+ const defaultResolve = resolve;
649
+
650
+ // TODO maybe remove this in v6 or v7, only accept options object to simplify the code
651
+ const normalizeParams = args => {
652
+ if (args.length === 1) {
653
+ const options = args[0];
654
+ return {
655
+ ipc: options.ipc,
656
+ message: options.message,
657
+ execute: options.execute,
658
+ resolve: options.resolve || defaultResolve,
659
+ preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
660
+ logError: options.logError || defaultLogError,
661
+ requiresSocket: options.requiresSocket || defaultRequiresSocket
662
+ };
666
663
  }
667
- }
668
- const withResolvers = () => {
669
- let _resolve;
670
- const promise = new Promise(resolve => {
671
- _resolve = resolve;
672
- });
673
664
  return {
674
- resolve: _resolve,
675
- promise
665
+ ipc: args[0],
666
+ message: args[1],
667
+ execute: args[2],
668
+ resolve: args[3],
669
+ preparePrettyError: args[4],
670
+ logError: args[5],
671
+ requiresSocket: args[6]
676
672
  };
677
673
  };
678
- const waitForFirstMessage = async port => {
674
+ const handleJsonRpcMessage = async (...args) => {
675
+ const options = normalizeParams(args);
679
676
  const {
677
+ message,
678
+ ipc,
679
+ execute,
680
680
  resolve,
681
- promise
682
- } = withResolvers();
683
- port.addEventListener('message', resolve, {
684
- once: true
685
- });
686
- const event = await promise;
687
- // @ts-ignore
688
- return event.data;
689
- };
690
- const listen$1$1 = async () => {
691
- const parentIpcRaw = listen$2();
692
- signal$2(parentIpcRaw);
693
- const parentIpc = wrap$5(parentIpcRaw);
694
- const firstMessage = await waitForFirstMessage(parentIpc);
695
- if (firstMessage.method !== 'initialize') {
696
- throw new IpcError('unexpected first message');
697
- }
698
- const type = firstMessage.params[0];
699
- if (type === 'message-port') {
700
- parentIpc.send({
701
- jsonrpc: '2.0',
702
- id: firstMessage.id,
703
- result: null
704
- });
705
- parentIpc.dispose();
706
- const port = firstMessage.params[1];
707
- return port;
708
- }
709
- return globalThis;
710
- };
711
- class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
712
- constructor(port) {
713
- super(port);
714
- }
715
- getData(event) {
716
- return getData$1(event);
717
- }
718
- send(message) {
719
- this._rawIpc.postMessage(message);
720
- }
721
- sendAndTransfer(message) {
722
- const transfer = getTransferrables(message);
723
- this._rawIpc.postMessage(message, transfer);
724
- }
725
- dispose() {
726
- if (this._rawIpc.close) {
727
- this._rawIpc.close();
681
+ preparePrettyError,
682
+ logError,
683
+ requiresSocket
684
+ } = options;
685
+ if ('id' in message) {
686
+ if ('method' in message) {
687
+ const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
688
+ try {
689
+ ipc.send(response);
690
+ } catch (error) {
691
+ const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
692
+ ipc.send(errorResponse);
693
+ }
694
+ return;
728
695
  }
696
+ resolve(message.id, message);
697
+ return;
729
698
  }
730
- onClose(callback) {
731
- // ignore
699
+ if ('method' in message) {
700
+ await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
701
+ return;
732
702
  }
733
- onMessage(callback) {
734
- this._rawIpc.addEventListener('message', callback);
735
- this._rawIpc.start();
703
+ throw new JsonRpcError('unexpected message');
704
+ };
705
+ const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
706
+ const {
707
+ message,
708
+ promise
709
+ } = create$2(method, params);
710
+ if (useSendAndTransfer && ipc.sendAndTransfer) {
711
+ ipc.sendAndTransfer(message);
712
+ } else {
713
+ ipc.send(message);
736
714
  }
737
- }
738
- const wrap$4 = port => {
739
- return new IpcChildWithModuleWorkerAndMessagePort(port);
715
+ const responseMessage = await promise;
716
+ return unwrapJsonRpcResult(responseMessage);
740
717
  };
741
- const IpcChildWithModuleWorkerAndMessagePort$1 = {
742
- __proto__: null,
743
- listen: listen$1$1,
744
- wrap: wrap$4
718
+ const send = (transport, method, ...params) => {
719
+ const message = create$4(method, params);
720
+ transport.send(message);
721
+ };
722
+ const invoke$2 = (ipc, method, ...params) => {
723
+ return invokeHelper(ipc, method, params, false);
724
+ };
725
+ const invokeAndTransfer = (ipc, method, ...params) => {
726
+ return invokeHelper(ipc, method, params, true);
727
+ };
728
+
729
+ const commands = Object.create(null);
730
+ const register = commandMap => {
731
+ Object.assign(commands, commandMap);
732
+ };
733
+ const getCommand = key => {
734
+ return commands[key];
735
+ };
736
+ const execute$1 = (command, ...args) => {
737
+ const fn = getCommand(command);
738
+ if (!fn) {
739
+ throw new Error(`command not found ${command}`);
740
+ }
741
+ return fn(...args);
745
742
  };
746
743
 
747
744
  const createRpc = ipc => {
@@ -771,96 +768,59 @@ const logError = () => {
771
768
  // handled by renderer worker
772
769
  };
773
770
  const handleMessage = event => {
774
- return handleJsonRpcMessage(event.target, event.data, execute, resolve, preparePrettyError, logError, requiresSocket);
771
+ const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
772
+ return handleJsonRpcMessage(event.target, event.data, execute$1, resolve, preparePrettyError, logError, actualRequiresSocket);
775
773
  };
776
774
  const handleIpc = ipc => {
777
- ipc.addEventListener('message', handleMessage);
775
+ if ('addEventListener' in ipc) {
776
+ ipc.addEventListener('message', handleMessage);
777
+ } else if ('on' in ipc) {
778
+ // deprecated
779
+ ipc.on('message', handleMessage);
780
+ }
778
781
  };
779
-
780
- // @ts-ignore
781
- const listen$1 = async () => {
782
- const module = IpcChildWithModuleWorkerAndMessagePort$1;
783
- const rawIpc = await module.listen();
782
+ const listen$1 = async (module, options) => {
783
+ const rawIpc = await module.listen(options);
784
+ if (module.signal) {
785
+ module.signal(rawIpc);
786
+ }
784
787
  const ipc = module.wrap(rawIpc);
785
788
  return ipc;
786
789
  };
787
- const create$1 = async ({
790
+ const create = async ({
788
791
  commandMap
789
792
  }) => {
790
793
  // TODO create a commandMap per rpc instance
791
794
  register(commandMap);
792
- const ipc = await listen$1();
795
+ const ipc = await listen$1(IpcChildWithModuleWorkerAndMessagePort$1);
793
796
  handleIpc(ipc);
794
797
  const rpc = createRpc(ipc);
795
798
  return rpc;
796
799
  };
797
800
  const WebWorkerRpcClient = {
798
801
  __proto__: null,
799
- create: create$1
802
+ create
800
803
  };
801
804
 
802
805
  const assetDir = '';
803
806
 
804
807
  const Directory$1 = 3;
805
- const File$1 = 7;
808
+ const File$2 = 7;
806
809
 
807
810
  const fileMapUrl = `${assetDir}/config/fileMap.json`;
808
811
 
809
- const normalizeLine = line => {
810
- if (line.startsWith('Error: ')) {
811
- return line.slice('Error: '.length);
812
- }
813
- if (line.startsWith('VError: ')) {
814
- return line.slice('VError: '.length);
815
- }
816
- return line;
817
- };
818
- const getCombinedMessage = (error, message) => {
819
- const stringifiedError = normalizeLine(`${error}`);
820
- if (message) {
821
- return `${message}: ${stringifiedError}`;
822
- }
823
- return stringifiedError;
824
- };
825
- const NewLine = '\n';
826
- const getNewLineIndex = (string, startIndex = undefined) => {
827
- return string.indexOf(NewLine, startIndex);
828
- };
829
- const mergeStacks = (parent, child) => {
830
- if (!child) {
831
- return parent;
832
- }
833
- const parentNewLineIndex = getNewLineIndex(parent);
834
- const childNewLineIndex = getNewLineIndex(child);
835
- if (childNewLineIndex === -1) {
836
- return parent;
837
- }
838
- const parentFirstLine = parent.slice(0, parentNewLineIndex);
839
- const childRest = child.slice(childNewLineIndex);
840
- const childFirstLine = normalizeLine(child.slice(0, childNewLineIndex));
841
- if (parentFirstLine.includes(childFirstLine)) {
842
- return parentFirstLine + childRest;
843
- }
844
- return child;
845
- };
846
- class VError extends Error {
847
- constructor(error, message) {
848
- const combinedMessage = getCombinedMessage(error, message);
849
- super(combinedMessage);
850
- this.name = 'VError';
851
- if (error instanceof Error) {
852
- this.stack = mergeStacks(this.stack, error.stack);
853
- }
854
- if (error.codeFrame) {
855
- // @ts-ignore
856
- this.codeFrame = error.codeFrame;
857
- }
858
- if (error.code) {
859
- // @ts-ignore
860
- this.code = error.code;
812
+ const getBlob$2 = async url => {
813
+ try {
814
+ const response = await fetch(url);
815
+ if (!response.ok) {
816
+ throw new Error(response.statusText);
861
817
  }
818
+ const text = await response.blob();
819
+ return text;
820
+ } catch (error) {
821
+ throw new VError(error, `Failed to request blob for ${url}`);
862
822
  }
863
- }
823
+ };
864
824
 
865
825
  const getJson = async url => {
866
826
  try {
@@ -925,7 +885,7 @@ const readDirWithFileTypes$1 = async uri => {
925
885
  });
926
886
  } else {
927
887
  dirents.push({
928
- type: File$1,
888
+ type: File$2,
929
889
  name: rest
930
890
  });
931
891
  }
@@ -936,9 +896,9 @@ const readDirWithFileTypes$1 = async uri => {
936
896
  const chmod$1 = () => {
937
897
  throw new Error('[memfs] chmod not implemented');
938
898
  };
939
- const getBlob$1 = async uri => {
940
- const content = await readFile$1(uri);
941
- const blob = new Blob([content]);
899
+ const getBlob$1 = async (uri, type) => {
900
+ const fetchUri = `${assetDir}${uri}`;
901
+ const blob = getBlob$2(fetchUri);
942
902
  return blob;
943
903
  };
944
904
 
@@ -1013,18 +973,18 @@ const getContentType = uri => {
1013
973
 
1014
974
  // TODO move this to an extension?
1015
975
 
1016
- const state$2 = {
976
+ const state$5 = {
1017
977
  files: Object.create(null)
1018
978
  };
1019
979
  const getDirent = uri => {
1020
- return state$2.files[uri];
980
+ return state$5.files[uri];
1021
981
  };
1022
982
  const readFile = uri => {
1023
983
  const dirent = getDirent(uri);
1024
984
  if (!dirent) {
1025
985
  throw new FileNotFoundError(uri);
1026
986
  }
1027
- if (dirent.type !== File$1) {
987
+ if (dirent.type !== File$2) {
1028
988
  throw new Error('file is a directory');
1029
989
  }
1030
990
  return dirent.content;
@@ -1034,7 +994,7 @@ const ensureParentDir = uri => {
1034
994
  let endIndex = uri.indexOf(Slash);
1035
995
  while (endIndex >= 0) {
1036
996
  const part = uri.slice(startIndex, endIndex + 1);
1037
- state$2.files[part] = {
997
+ state$5.files[part] = {
1038
998
  type: Directory$1,
1039
999
  content: ''
1040
1000
  };
@@ -1047,8 +1007,8 @@ const writeFile = (uri, content) => {
1047
1007
  dirent.content = content;
1048
1008
  } else {
1049
1009
  ensureParentDir(uri);
1050
- state$2.files[uri] = {
1051
- type: File$1,
1010
+ state$5.files[uri] = {
1011
+ type: File$2,
1052
1012
  content
1053
1013
  };
1054
1014
  }
@@ -1058,20 +1018,20 @@ const mkdir = uri => {
1058
1018
  uri += Slash;
1059
1019
  }
1060
1020
  ensureParentDir(uri);
1061
- state$2.files[uri] = {
1021
+ state$5.files[uri] = {
1062
1022
  type: Directory$1,
1063
1023
  content: ''
1064
1024
  };
1065
1025
  };
1066
1026
  const remove = uri => {
1067
1027
  const toDelete = [];
1068
- for (const key of Object.keys(state$2.files)) {
1028
+ for (const key of Object.keys(state$5.files)) {
1069
1029
  if (key.startsWith(uri)) {
1070
1030
  toDelete.push(key);
1071
1031
  }
1072
1032
  }
1073
1033
  for (const key of toDelete) {
1074
- delete state$2.files[key];
1034
+ delete state$5.files[key];
1075
1035
  }
1076
1036
  };
1077
1037
  const readDirWithFileTypes = uri => {
@@ -1079,7 +1039,7 @@ const readDirWithFileTypes = uri => {
1079
1039
  uri += Slash;
1080
1040
  }
1081
1041
  const dirents = [];
1082
- for (const [key, value] of Object.entries(state$2.files)) {
1042
+ for (const [key, value] of Object.entries(state$5.files)) {
1083
1043
  if (key.startsWith(uri)) {
1084
1044
  // @ts-ignore
1085
1045
  switch (value.type) {
@@ -1092,7 +1052,7 @@ const readDirWithFileTypes = uri => {
1092
1052
  });
1093
1053
  }
1094
1054
  break;
1095
- case File$1:
1055
+ case File$2:
1096
1056
  if (!key.includes(Slash, uri.length + 1)) {
1097
1057
  dirents.push({
1098
1058
  // @ts-ignore
@@ -1106,16 +1066,16 @@ const readDirWithFileTypes = uri => {
1106
1066
  }
1107
1067
  return dirents;
1108
1068
  };
1109
- const getBlob = uri => {
1069
+ const getBlob = (uri, type) => {
1110
1070
  const content = readFile(uri);
1111
- const contentType = getContentType(uri);
1071
+ const contentType = type || getContentType(uri);
1112
1072
  const blob = new Blob([content], {
1113
1073
  type: contentType
1114
1074
  });
1115
1075
  return blob;
1116
1076
  };
1117
- const getBlobUrl = uri => {
1118
- const blob = getBlob(uri);
1077
+ const getBlobUrl = (uri, type) => {
1078
+ const blob = getBlob(uri, type);
1119
1079
  const url = URL.createObjectURL(blob);
1120
1080
  return url;
1121
1081
  };
@@ -1123,7 +1083,7 @@ const chmod = () => {
1123
1083
  throw new Error('[memfs] chmod not implemented');
1124
1084
  };
1125
1085
  const getFiles = () => {
1126
- return state$2.files;
1086
+ return state$5.files;
1127
1087
  };
1128
1088
 
1129
1089
  const emptyMatches = [];
@@ -1360,6 +1320,7 @@ const getFolderIcon = () => {
1360
1320
  };
1361
1321
 
1362
1322
  const Hide = 'hide';
1323
+ const KeepOpen = '';
1363
1324
 
1364
1325
  const emptyObject = {};
1365
1326
  const RE_PLACEHOLDER = /\{(PH\d+)\}/g;
@@ -1392,6 +1353,30 @@ const UiStrings = {
1392
1353
  TypeNameOfCommandToRun: 'Type the name of a command to run.',
1393
1354
  TypeTheNameOfAViewToOpen: 'Type the name of a view, output channel or terminal to open.'
1394
1355
  };
1356
+ const noMatchingColorThemesFound = () => {
1357
+ return i18nString(UiStrings.NoMatchingColorThemesFound);
1358
+ };
1359
+ const selectColorTheme = () => {
1360
+ return i18nString(UiStrings.SelectColorTheme);
1361
+ };
1362
+ const typeNameofCommandToRun = () => {
1363
+ return i18nString(UiStrings.TypeNameOfCommandToRun);
1364
+ };
1365
+ const showAndRunCommands = () => {
1366
+ return i18nString(UiStrings.ShowAndRunCommands);
1367
+ };
1368
+ const noMatchingResults = () => {
1369
+ return i18nString(UiStrings.NoMatchingResults);
1370
+ };
1371
+ const files = () => {
1372
+ return i18nString(UiStrings.Files);
1373
+ };
1374
+ const goToFile = () => {
1375
+ return i18nString(UiStrings.GoToFile);
1376
+ };
1377
+ const noResults = () => {
1378
+ return i18nString(UiStrings.NoResults);
1379
+ };
1395
1380
  const selectToOpen = () => {
1396
1381
  return i18nString(UiStrings.SelectToOpen);
1397
1382
  };
@@ -1401,17 +1386,23 @@ const openRecent = () => {
1401
1386
  const noRecentlyOpenedFoldersFound = () => {
1402
1387
  return i18nString(UiStrings.NoRecentlyOpenedFoldersFound);
1403
1388
  };
1389
+ const noSymbolFound = () => {
1390
+ return i18nString(UiStrings.NoSymbolFound);
1391
+ };
1392
+ const noWorkspaceSymbolsFound = () => {
1393
+ return i18nString(UiStrings.NoWorkspaceSymbolsFound);
1394
+ };
1404
1395
 
1405
- const state$1 = {
1396
+ const state$4 = {
1406
1397
  rpc: undefined
1407
1398
  };
1408
1399
  const invoke$1 = (method, ...params) => {
1409
- const rpc = state$1.rpc;
1400
+ const rpc = state$4.rpc;
1410
1401
  // @ts-ignore
1411
1402
  return rpc.invoke(method, ...params);
1412
1403
  };
1413
1404
  const setRpc = rpc => {
1414
- state$1.rpc = rpc;
1405
+ state$4.rpc = rpc;
1415
1406
  };
1416
1407
 
1417
1408
  // TODO this should be in FileSystem module
@@ -1435,16 +1426,16 @@ const getRecentlyOpened = () => {
1435
1426
  const openWorkspaceFolder = uri => {
1436
1427
  return invoke$1(/* Workspace.setPath */'Workspace.setPath', /* path */uri);
1437
1428
  };
1438
- const getPlaceholder = () => {
1429
+ const getPlaceholder$b = () => {
1439
1430
  return selectToOpen();
1440
1431
  };
1441
- const getLabel = () => {
1432
+ const getLabel$5 = () => {
1442
1433
  return openRecent();
1443
1434
  };
1444
- const getHelpEntries = () => {
1435
+ const getHelpEntries$9 = () => {
1445
1436
  return [];
1446
1437
  };
1447
- const getNoResults = () => {
1438
+ const getNoResults$a = () => {
1448
1439
  return {
1449
1440
  label: noRecentlyOpenedFoldersFound()
1450
1441
  };
@@ -1455,68 +1446,368 @@ const getNoResults = () => {
1455
1446
  // This would make the code more module since the code for getting the picks
1456
1447
  // would be more independent of the specific data format of the quickpick provider
1457
1448
 
1458
- const getPicks = async () => {
1449
+ const getPicks$b = async () => {
1459
1450
  const recentlyOpened = await getRecentlyOpened();
1460
1451
  return recentlyOpened;
1461
1452
  };
1462
1453
 
1463
1454
  // TODO selectPick should be independent of show/hide
1464
- const selectPick = async pick => {
1455
+ const selectPick$b = async pick => {
1465
1456
  const path = pick;
1466
1457
  await openWorkspaceFolder(path);
1467
1458
  return {
1468
1459
  command: Hide
1469
1460
  };
1470
1461
  };
1471
- const getFilterValue = value => {
1462
+ const getFilterValue$a = value => {
1472
1463
  return pathBaseName(value);
1473
1464
  };
1474
- const getPickFilterValue = pick => {
1465
+ const getPickFilterValue$6 = pick => {
1475
1466
  return pathBaseName(pick);
1476
1467
  };
1477
- const getPickLabel = pick => {
1468
+ const getPickLabel$5 = pick => {
1478
1469
  return pathBaseName(pick);
1479
1470
  };
1480
- const getPickDescription = pick => {
1471
+ const getPickDescription$3 = pick => {
1481
1472
  return pathDirName(pick);
1482
1473
  };
1483
- const getPickIcon = () => {
1474
+ const getPickIcon$5 = () => {
1484
1475
  return '';
1485
1476
  };
1486
- const getPickFileIcon = () => {
1477
+ const getPickFileIcon$2 = () => {
1487
1478
  return getFolderIcon();
1488
1479
  };
1489
1480
 
1490
1481
  const QuickPickEntriesOpenRecent = {
1491
1482
  __proto__: null,
1492
- getFilterValue,
1493
- getHelpEntries,
1494
- getLabel,
1495
- getNoResults,
1496
- getPickDescription,
1497
- getPickFileIcon,
1498
- getPickFilterValue,
1499
- getPickIcon,
1500
- getPickLabel,
1501
- getPicks,
1502
- getPlaceholder,
1503
- selectPick
1483
+ getFilterValue: getFilterValue$a,
1484
+ getHelpEntries: getHelpEntries$9,
1485
+ getLabel: getLabel$5,
1486
+ getNoResults: getNoResults$a,
1487
+ getPickDescription: getPickDescription$3,
1488
+ getPickFileIcon: getPickFileIcon$2,
1489
+ getPickFilterValue: getPickFilterValue$6,
1490
+ getPickIcon: getPickIcon$5,
1491
+ getPickLabel: getPickLabel$5,
1492
+ getPicks: getPicks$b,
1493
+ getPlaceholder: getPlaceholder$b,
1494
+ selectPick: selectPick$b
1495
+ };
1496
+
1497
+ const CommandPalette = 'quickPick://commandPalette';
1498
+ const File$1 = 'quickPick://file';
1499
+ const EveryThing = 'quickPick://everything';
1500
+ const Number$1 = 'quickPick://number';
1501
+ const Recent = 'quickPick://recent';
1502
+ const ColorTheme = 'quickPick://color-theme';
1503
+ const Symbol$2 = 'quickPick://symbol';
1504
+ const View$1 = 'quickPick://view';
1505
+ const WorkspaceSymbol$1 = 'quickPick://workspace-symbol';
1506
+ const Custom = 'quickPick://custom';
1507
+
1508
+ const loadQuickPickEntries = moduleId => {
1509
+ switch (moduleId) {
1510
+ case Recent:
1511
+ return QuickPickEntriesOpenRecent;
1512
+ default:
1513
+ throw new Error(`unknown module "${moduleId}"`);
1514
+ }
1515
+ };
1516
+
1517
+ const getColorThemeNames = async () => {
1518
+ return invoke$1(/* Ajax.getJson */'ColorTheme.getColorThemeNames');
1519
+ };
1520
+
1521
+ const setColorTheme = id => {
1522
+ return invoke$1(/* ColorTheme.setColorTheme */'ColorTheme.setColorTheme', /* colorThemeId */id);
1523
+ };
1524
+ const getPlaceholder$a = () => {
1525
+ return selectColorTheme();
1526
+ };
1527
+ const getLabel$4 = () => {
1528
+ return selectColorTheme();
1529
+ };
1530
+ const getPicks$a = async searchValue => {
1531
+ const colorThemeNames = await getColorThemeNames();
1532
+ return colorThemeNames;
1533
+ };
1534
+ const selectPick$a = async pick => {
1535
+ await setColorTheme(/* colorThemeId */pick);
1536
+ return {
1537
+ command: Hide
1538
+ };
1539
+ };
1540
+ const focusPick = async pick => {
1541
+ await setColorTheme(/* colorThemeId */pick);
1542
+ };
1543
+ const getFilterValue$9 = value => {
1544
+ return value;
1545
+ };
1546
+ const getNoResults$9 = () => {
1547
+ return {
1548
+ label: noMatchingColorThemesFound()
1549
+ };
1550
+ };
1551
+ const getPickFilterValue$5 = pick => {
1552
+ return pick;
1553
+ };
1554
+ const getPickLabel$4 = pick => {
1555
+ return pick;
1556
+ };
1557
+ const getPickIcon$4 = pick => {
1558
+ return '';
1559
+ };
1560
+
1561
+ const QuickPickEntriesColorTheme = {
1562
+ __proto__: null,
1563
+ focusPick,
1564
+ getFilterValue: getFilterValue$9,
1565
+ getLabel: getLabel$4,
1566
+ getNoResults: getNoResults$9,
1567
+ getPickFilterValue: getPickFilterValue$5,
1568
+ getPickIcon: getPickIcon$4,
1569
+ getPickLabel: getPickLabel$4,
1570
+ getPicks: getPicks$a,
1571
+ getPlaceholder: getPlaceholder$a,
1572
+ selectPick: selectPick$a,
1573
+ setColorTheme
1574
+ };
1575
+
1576
+ const Tag$1 = 'Tag';
1577
+ const Cloud$1 = 'Cloud';
1578
+ const SourceControl$1 = 'SourceControl';
1579
+ const None$1 = '';
1580
+
1581
+ const SourceControl = 1;
1582
+ const Cloud = 2;
1583
+ const Tag = 3;
1584
+
1585
+ const name$8 = 'custom';
1586
+ const state$3 = {
1587
+ args: []
1588
+ };
1589
+ const setArgs = args => {
1590
+ state$3.args = args;
1591
+ };
1592
+ const getPlaceholder$9 = () => {
1593
+ return '';
1594
+ };
1595
+ const getLabel$3 = () => {
1596
+ return 'Custom';
1597
+ };
1598
+
1599
+ // TODO help entries should not be here
1600
+ const getHelpEntries$8 = () => {
1601
+ return [];
1602
+ };
1603
+ const getNoResults$8 = () => {
1604
+ return {
1605
+ label: noMatchingResults()
1606
+ };
1607
+ };
1608
+ const getPicks$9 = async searchValue => {
1609
+ const items = state$3.args[1] || [];
1610
+ return items;
1611
+ };
1612
+ const selectPick$9 = async pick => {
1613
+ const {
1614
+ args
1615
+ } = state$3;
1616
+ const resolve = args[2];
1617
+ resolve(pick);
1618
+ return {
1619
+ command: Hide
1620
+ };
1621
+ };
1622
+ const getFilterValue$8 = value => {
1623
+ return value;
1624
+ };
1625
+ const getPickFilterValue$4 = pick => {
1626
+ return pick;
1627
+ };
1628
+ const getPickLabel$3 = pick => {
1629
+ return pick.label;
1630
+ };
1631
+ const getPickDescription$2 = pick => {
1632
+ return pick.description || '';
1633
+ };
1634
+ const convertIcon = icon => {
1635
+ switch (icon) {
1636
+ case SourceControl:
1637
+ return SourceControl$1;
1638
+ case Cloud:
1639
+ return Cloud$1;
1640
+ case Tag:
1641
+ return Tag$1;
1642
+ default:
1643
+ return None$1;
1644
+ }
1645
+ };
1646
+ const getPickIcon$3 = pick => {
1647
+ return convertIcon(pick.icon);
1648
+ };
1649
+
1650
+ const QuickPickEntriesCustom = {
1651
+ __proto__: null,
1652
+ getFilterValue: getFilterValue$8,
1653
+ getHelpEntries: getHelpEntries$8,
1654
+ getLabel: getLabel$3,
1655
+ getNoResults: getNoResults$8,
1656
+ getPickDescription: getPickDescription$2,
1657
+ getPickFilterValue: getPickFilterValue$4,
1658
+ getPickIcon: getPickIcon$3,
1659
+ getPickLabel: getPickLabel$3,
1660
+ getPicks: getPicks$9,
1661
+ getPlaceholder: getPlaceholder$9,
1662
+ name: name$8,
1663
+ selectPick: selectPick$9,
1664
+ setArgs,
1665
+ state: state$3
1666
+ };
1667
+
1668
+ const handleError = async (error, notify = true, prefix = '') => {
1669
+ console.error(error);
1670
+ };
1671
+ const showErrorDialog = async () => {};
1672
+ const warn = (...args) => {
1673
+ console.warn(...args);
1674
+ };
1675
+
1676
+ const state$2 = {
1677
+ menuEntries: []
1678
+ };
1679
+ const getAll = () => {
1680
+ return state$2.menuEntries;
1681
+ };
1682
+
1683
+ const name$7 = 'command';
1684
+ const getPlaceholder$8 = () => {
1685
+ return typeNameofCommandToRun();
1686
+ };
1687
+ const helpEntries = () => {
1688
+ return [{
1689
+ description: showAndRunCommands(),
1690
+ category: 'global commands'
1691
+ }];
1692
+ };
1693
+ const getLabel$2 = () => {
1694
+ return '';
1695
+ };
1696
+ const getNoResults$7 = () => {
1697
+ return {
1698
+ label: noMatchingResults()
1699
+ };
1700
+ };
1701
+
1702
+ // TODO combine Ajax with cache (specify strategy: cacheFirst, networkFirst)
1703
+ const getBuiltinPicks = async () => {
1704
+ const builtinPicks = getAll();
1705
+ return builtinPicks;
1706
+ };
1707
+ const prefixIdWithExt = item => {
1708
+ if (!item.label) {
1709
+ warn('[QuickPick] item has missing label', item);
1710
+ }
1711
+ if (!item.id) {
1712
+ warn('[QuickPick] item has missing id', item);
1713
+ }
1714
+ return {
1715
+ ...item,
1716
+ id: `ext.${item.id}`,
1717
+ label: item.label || item.id
1718
+ };
1719
+ };
1720
+ const getExtensionPicks = async () => {
1721
+ try {
1722
+ // TODO don't call this every time
1723
+ const extensionPicks = await invoke$1('ExtensionHostCommands.getCommands');
1724
+ if (!extensionPicks) {
1725
+ return [];
1726
+ }
1727
+ const mappedPicks = extensionPicks.map(prefixIdWithExt);
1728
+ return mappedPicks;
1729
+ } catch (error) {
1730
+ console.error(`Failed to get extension picks: ${error}`);
1731
+ return [];
1732
+ }
1733
+ };
1734
+
1735
+ // TODO send strings to renderer process only once for next occurrence send uint16array of ids of strings
1736
+
1737
+ const getPicks$8 = async () => {
1738
+ const builtinPicks = await getBuiltinPicks();
1739
+ const extensionPicks = await getExtensionPicks();
1740
+ return [...builtinPicks, ...extensionPicks];
1741
+ };
1742
+ const shouldHide = item => {
1743
+ if (item.id === 'Viewlet.openWidget' && item.args[0] === 'QuickPick') {
1744
+ return false;
1745
+ }
1746
+ return true;
1747
+ };
1748
+ const selectPickBuiltin = async item => {
1749
+ const args = item.args || [];
1750
+ // TODO ids should be all numbers for efficiency -> also directly can call command
1751
+ await invoke$1(item.id, ...args);
1752
+ if (shouldHide(item)) {
1753
+ return {
1754
+ command: Hide
1755
+ };
1756
+ }
1757
+ return {
1758
+ command: KeepOpen
1759
+ };
1760
+ };
1761
+ const selectPickExtension = async item => {
1762
+ const id = item.id.slice(4); // TODO lots of string allocation with 'ext.' find a better way to separate builtin commands from extension commands
1763
+ try {
1764
+ await invoke$1('ExtensionHostCommands.executeCommand', id);
1765
+ } catch (error) {
1766
+ await handleError(error, false);
1767
+ // @ts-ignore
1768
+ await showErrorDialog();
1769
+ }
1770
+ return {
1771
+ command: Hide
1772
+ };
1504
1773
  };
1505
-
1506
- const Recent = 'quickPick://recent';
1507
-
1508
- const loadQuickPickEntries = moduleId => {
1509
- switch (moduleId) {
1510
- case Recent:
1511
- return QuickPickEntriesOpenRecent;
1512
- default:
1513
- throw new Error(`unknown module "${moduleId}"`);
1774
+ const selectPick$8 = async item => {
1775
+ if (item.id.startsWith('ext.')) {
1776
+ return selectPickExtension(item);
1514
1777
  }
1778
+ return selectPickBuiltin(item);
1779
+ };
1780
+ const getFilterValue$7 = value => {
1781
+ return value;
1782
+ };
1783
+ const getPickFilterValue$3 = pick => {
1784
+ return pick.label;
1785
+ };
1786
+ const getPickLabel$2 = pick => {
1787
+ return pick.label;
1788
+ };
1789
+ const getPickIcon$2 = () => {
1790
+ return '';
1515
1791
  };
1516
1792
 
1517
- const Memfs = 'memfs';
1518
- const Html = 'html';
1519
- const Fetch = 'fetch';
1793
+ const QuickPickEntriesCommand = {
1794
+ __proto__: null,
1795
+ getFilterValue: getFilterValue$7,
1796
+ getLabel: getLabel$2,
1797
+ getNoResults: getNoResults$7,
1798
+ getPickFilterValue: getPickFilterValue$3,
1799
+ getPickIcon: getPickIcon$2,
1800
+ getPickLabel: getPickLabel$2,
1801
+ getPicks: getPicks$8,
1802
+ getPlaceholder: getPlaceholder$8,
1803
+ helpEntries,
1804
+ name: name$7,
1805
+ selectPick: selectPick$8
1806
+ };
1807
+
1808
+ const execute = async (method, ...params) => {
1809
+ // TODO
1810
+ };
1520
1811
 
1521
1812
  const RE_PROTOCOL = /^([a-z-]+):\/\//;
1522
1813
  const getProtocol = uri => {
@@ -1527,6 +1818,10 @@ const getProtocol = uri => {
1527
1818
  return '';
1528
1819
  };
1529
1820
 
1821
+ const Memfs = 'memfs';
1822
+ const Html = 'html';
1823
+ const Fetch = 'fetch';
1824
+
1530
1825
  const searchFile$4 = async () => {
1531
1826
  const files = await getFiles();
1532
1827
  const keys = Object.keys(files);
@@ -1620,7 +1915,7 @@ function promisifyRequest(request) {
1620
1915
  request.addEventListener('success', success);
1621
1916
  request.addEventListener('error', error);
1622
1917
  });
1623
- // This mapping exists in reverseTransformCache but doesn't doesn't exist in transformCache. This
1918
+ // This mapping exists in reverseTransformCache but doesn't exist in transformCache. This
1624
1919
  // is because we create many promises from a single IDBRequest.
1625
1920
  reverseTransformCache.set(promise, request);
1626
1921
  return promise;
@@ -1843,7 +2138,7 @@ replaceTraps(oldTraps => ({
1843
2138
  }
1844
2139
  }));
1845
2140
 
1846
- const state = {
2141
+ const state$1 = {
1847
2142
  databases: Object.create(null),
1848
2143
  eventId: 0,
1849
2144
  dbVersion: 1,
@@ -1854,7 +2149,7 @@ const state = {
1854
2149
 
1855
2150
  const getHandleDb = async () => {
1856
2151
  // @ts-ignore
1857
- const db = await openDB('handle', state.dbVersion, {
2152
+ const db = await openDB('handle', state$1.dbVersion, {
1858
2153
  async upgrade(db) {
1859
2154
  if (!db.objectStoreNames.contains('file-handles-store')) {
1860
2155
  await db.createObjectStore('file-handles-store', {});
@@ -1987,6 +2282,538 @@ const searchFile = async (path, value, prepare, assetDir) => {
1987
2282
  return result;
1988
2283
  };
1989
2284
 
2285
+ const name$6 = 'file';
2286
+ const getPlaceholder$7 = () => {
2287
+ return '';
2288
+ };
2289
+ const getLabel$1 = () => {
2290
+ return files();
2291
+ };
2292
+
2293
+ // TODO help entries should not be here
2294
+ const getHelpEntries$7 = () => {
2295
+ return [{
2296
+ description: goToFile(),
2297
+ category: 'global commands'
2298
+ }];
2299
+ };
2300
+ const getNoResults$6 = () => {
2301
+ return {
2302
+ label: noMatchingResults()
2303
+ };
2304
+ };
2305
+ const getPicks$7 = async searchValue => {
2306
+ {
2307
+ return [];
2308
+ }
2309
+ };
2310
+ const selectPick$7 = async pick => {
2311
+ if (typeof pick === 'object') {
2312
+ pick = pick.pick;
2313
+ }
2314
+ const workspace = '';
2315
+ const absolutePath = `${workspace}/${pick}`;
2316
+ await execute(/* Main.openUri */'Main.openUri', /* uri */absolutePath);
2317
+ return {
2318
+ command: Hide
2319
+ };
2320
+ };
2321
+ const getFilterValue$6 = value => {
2322
+ return value;
2323
+ };
2324
+ const getPickFilterValue$2 = pick => {
2325
+ if (typeof pick === 'object') {
2326
+ pick = pick.pick;
2327
+ }
2328
+ return pick;
2329
+ };
2330
+ const getPickLabel$1 = pick => {
2331
+ if (typeof pick === 'object') {
2332
+ pick = pick.pick;
2333
+ }
2334
+ const baseName = pathBaseName(pick);
2335
+ return baseName;
2336
+ };
2337
+ const getPickDescription$1 = pick => {
2338
+ if (typeof pick === 'object') {
2339
+ pick = pick.pick;
2340
+ }
2341
+ const dirName = pathDirName(pick);
2342
+ return dirName;
2343
+ };
2344
+ const getPickIcon$1 = () => {
2345
+ return '';
2346
+ };
2347
+ const getPickFileIcon$1 = pick => {
2348
+ return '';
2349
+ };
2350
+ const isPrepared$1 = () => {
2351
+ const workspace = '';
2352
+ // TODO protocol should always be defined. For files it should use file protocol
2353
+ const protocol = getProtocol(workspace);
2354
+ return !protocol;
2355
+ };
2356
+
2357
+ const QuickPickEntriesFile = {
2358
+ __proto__: null,
2359
+ getFilterValue: getFilterValue$6,
2360
+ getHelpEntries: getHelpEntries$7,
2361
+ getLabel: getLabel$1,
2362
+ getNoResults: getNoResults$6,
2363
+ getPickDescription: getPickDescription$1,
2364
+ getPickFileIcon: getPickFileIcon$1,
2365
+ getPickFilterValue: getPickFilterValue$2,
2366
+ getPickIcon: getPickIcon$1,
2367
+ getPickLabel: getPickLabel$1,
2368
+ getPicks: getPicks$7,
2369
+ getPlaceholder: getPlaceholder$7,
2370
+ isPrepared: isPrepared$1,
2371
+ name: name$6,
2372
+ selectPick: selectPick$7
2373
+ };
2374
+
2375
+ const name$5 = 'goToLine';
2376
+ const getPlaceholder$6 = () => {
2377
+ return '';
2378
+ };
2379
+ const getHelpEntries$6 = () => {
2380
+ return [];
2381
+ };
2382
+ const getNoResults$5 = () => {
2383
+ return undefined;
2384
+ };
2385
+ const getPicks$6 = async () => {
2386
+ const picks = [{
2387
+ label: '1'
2388
+ }, {
2389
+ label: '2'
2390
+ }, {
2391
+ label: '3'
2392
+ }, {
2393
+ label: '4'
2394
+ }, {
2395
+ label: '5'
2396
+ }, {
2397
+ label: '6'
2398
+ }];
2399
+ return picks;
2400
+ };
2401
+ const selectPick$6 = async item => {
2402
+ const rowIndex = Number.parseInt(item.label);
2403
+ const position = {
2404
+ rowIndex,
2405
+ columnIndex: 5
2406
+ };
2407
+ await execute(/* EditorSetCursor.editorSetCursor */'TODO', /* position */position);
2408
+ // TODO put cursor onto that line
2409
+ return {
2410
+ command: Hide
2411
+ };
2412
+ };
2413
+ const getFilterValue$5 = value => {
2414
+ return value;
2415
+ };
2416
+
2417
+ const QuickPickEntriesGoToLine = {
2418
+ __proto__: null,
2419
+ getFilterValue: getFilterValue$5,
2420
+ getHelpEntries: getHelpEntries$6,
2421
+ getNoResults: getNoResults$5,
2422
+ getPicks: getPicks$6,
2423
+ getPlaceholder: getPlaceholder$6,
2424
+ name: name$5,
2425
+ selectPick: selectPick$6
2426
+ };
2427
+
2428
+ const name$4 = 'noop';
2429
+ const getPlaceholder$5 = () => {
2430
+ return '';
2431
+ };
2432
+ const getHelpEntries$5 = () => {
2433
+ return [];
2434
+ };
2435
+ const getNoResults$4 = () => {
2436
+ return noResults();
2437
+ };
2438
+ const getPicks$5 = async value => {
2439
+ return [];
2440
+ };
2441
+ const selectPick$5 = async item => {
2442
+ return {
2443
+ command: Hide
2444
+ };
2445
+ };
2446
+ const getFilterValue$4 = value => {
2447
+ return value;
2448
+ };
2449
+ const getPickFilterValue$1 = pick => {
2450
+ return pick;
2451
+ };
2452
+
2453
+ const QuickPickNoop = {
2454
+ __proto__: null,
2455
+ getFilterValue: getFilterValue$4,
2456
+ getHelpEntries: getHelpEntries$5,
2457
+ getNoResults: getNoResults$4,
2458
+ getPickFilterValue: getPickFilterValue$1,
2459
+ getPicks: getPicks$5,
2460
+ getPlaceholder: getPlaceholder$5,
2461
+ name: name$4,
2462
+ selectPick: selectPick$5
2463
+ };
2464
+
2465
+ const name$3 = 'symbol';
2466
+ const getPlaceholder$4 = () => {
2467
+ return '';
2468
+ };
2469
+ const getHelpEntries$4 = () => {
2470
+ return [];
2471
+ };
2472
+ const getNoResults$3 = () => {
2473
+ return {
2474
+ label: noSymbolFound()
2475
+ };
2476
+ };
2477
+ const getPicks$4 = async () => {
2478
+ const picks = [];
2479
+ return picks;
2480
+ };
2481
+ const selectPick$4 = async item => {
2482
+ return {
2483
+ command: Hide
2484
+ };
2485
+ };
2486
+ const getFilterValue$3 = value => {
2487
+ return value;
2488
+ };
2489
+
2490
+ const QuickPickEntriesSymbol = {
2491
+ __proto__: null,
2492
+ getFilterValue: getFilterValue$3,
2493
+ getHelpEntries: getHelpEntries$4,
2494
+ getNoResults: getNoResults$3,
2495
+ getPicks: getPicks$4,
2496
+ getPlaceholder: getPlaceholder$4,
2497
+ name: name$3,
2498
+ selectPick: selectPick$4
2499
+ };
2500
+
2501
+ // TODO probably not needed
2502
+
2503
+ const getPlaceholder$3 = () => {
2504
+ return typeNameofCommandToRun();
2505
+ };
2506
+ const getHelpEntries$3 = () => {
2507
+ return undefined;
2508
+ };
2509
+ const getPicks$3 = async () => {
2510
+ // const views = ViewService.getViews()
2511
+ // const picks = views.map(toPick)
2512
+ // return picks
2513
+ return [];
2514
+ };
2515
+ const selectPick$3 = async item => {
2516
+ // Command.execute(/* openView */ 549, /* viewName */ item.label)
2517
+ // return {
2518
+ // command: QuickPickReturnValue.Hide,
2519
+ // }
2520
+ };
2521
+ const getFilterValue$2 = value => {
2522
+ return value;
2523
+ };
2524
+
2525
+ const QuickPickEntriesView = {
2526
+ __proto__: null,
2527
+ getFilterValue: getFilterValue$2,
2528
+ getHelpEntries: getHelpEntries$3,
2529
+ getPicks: getPicks$3,
2530
+ getPlaceholder: getPlaceholder$3,
2531
+ selectPick: selectPick$3
2532
+ };
2533
+
2534
+ const name$2 = 'workspace-symbol';
2535
+ const getPlaceholder$2 = () => {
2536
+ return '';
2537
+ };
2538
+ const getHelpEntries$2 = () => {
2539
+ return [];
2540
+ };
2541
+ const getNoResults$2 = () => {
2542
+ return {
2543
+ label: noWorkspaceSymbolsFound()
2544
+ };
2545
+ };
2546
+ const getPicks$2 = async () => {
2547
+ const picks = [];
2548
+ return picks;
2549
+ };
2550
+ const selectPick$2 = async item => {
2551
+ return {
2552
+ command: Hide
2553
+ };
2554
+ };
2555
+ const getFilterValue$1 = value => {
2556
+ return value;
2557
+ };
2558
+
2559
+ const QuickPickEntriesWorkspaceSymbol = {
2560
+ __proto__: null,
2561
+ getFilterValue: getFilterValue$1,
2562
+ getHelpEntries: getHelpEntries$2,
2563
+ getNoResults: getNoResults$2,
2564
+ getPicks: getPicks$2,
2565
+ getPlaceholder: getPlaceholder$2,
2566
+ name: name$2,
2567
+ selectPick: selectPick$2
2568
+ };
2569
+
2570
+ const Command = '>';
2571
+ const Symbol$1 = '@';
2572
+ const WorkspaceSymbol = '#';
2573
+ const GoToLine = ':';
2574
+ const View = 'view ';
2575
+ const None = '';
2576
+
2577
+ // TODO avoid global variable
2578
+
2579
+ const state = {
2580
+ // providerId: PROVIDER_NOOP,
2581
+ provider: QuickPickNoop,
2582
+ prefix: 'string-that-should-never-match-another-string'
2583
+ };
2584
+
2585
+ /**
2586
+ * @type {string}
2587
+ */
2588
+ const name$1 = 'everything';
2589
+ const getPlaceholder$1 = () => {
2590
+ return state.provider.getPlaceholder();
2591
+ };
2592
+ const getLabel = () => {
2593
+ return '';
2594
+ };
2595
+ const getHelpEntries$1 = () => {
2596
+ return state.provider.getHelpEntries();
2597
+ };
2598
+ const getNoResults$1 = () => {
2599
+ return state.provider.getNoResults();
2600
+ };
2601
+ const getPrefix = value => {
2602
+ if (value.startsWith(Command)) {
2603
+ return Command;
2604
+ }
2605
+ if (value.startsWith(Symbol$1)) {
2606
+ return Symbol$1;
2607
+ }
2608
+ if (value.startsWith(WorkspaceSymbol)) {
2609
+ return WorkspaceSymbol;
2610
+ }
2611
+ if (value.startsWith(GoToLine)) {
2612
+ return GoToLine;
2613
+ }
2614
+ if (value.startsWith(View)) {
2615
+ return View;
2616
+ }
2617
+ return None;
2618
+ };
2619
+ const getQuickPickProvider = prefix => {
2620
+ // TODO could use enum for prefix
2621
+ // TODO could use regex to extract prefix
2622
+ // TODO or could check first letter char code (less comparisons)
2623
+ switch (prefix) {
2624
+ case Command:
2625
+ return QuickPickEntriesCommand;
2626
+ case Symbol$1:
2627
+ return QuickPickEntriesSymbol;
2628
+ case WorkspaceSymbol:
2629
+ return QuickPickEntriesWorkspaceSymbol;
2630
+ case GoToLine:
2631
+ return QuickPickEntriesGoToLine;
2632
+ case View:
2633
+ return QuickPickEntriesView;
2634
+ default:
2635
+ return QuickPickEntriesFile;
2636
+ }
2637
+ };
2638
+ const getPicks$1 = async value => {
2639
+ const prefix = getPrefix(value);
2640
+ // TODO race condition
2641
+ if (state.prefix !== prefix) {
2642
+ state.prefix = prefix;
2643
+ // @ts-ignore
2644
+ state.provider = await getQuickPickProvider(prefix);
2645
+ }
2646
+ // TODO this line is a bit duplicated with getFilterValue
2647
+ const slicedValue = value.slice(prefix.length);
2648
+ const picks = await state.provider.getPicks(slicedValue);
2649
+ return picks;
2650
+ };
2651
+ const selectPick$1 = item => {
2652
+ const {
2653
+ provider
2654
+ } = state;
2655
+ return provider.selectPick(item);
2656
+ };
2657
+ const openCommandPalette = () => {
2658
+ // show('>')
2659
+ };
2660
+ const openView = () => {
2661
+ // show('view ')
2662
+ };
2663
+ const getFilterValue = value => {
2664
+ return value.slice(state.prefix.length);
2665
+ };
2666
+ const getPickFilterValue = pick => {
2667
+ const {
2668
+ provider
2669
+ } = state;
2670
+ return provider.getPickFilterValue(pick);
2671
+ };
2672
+ const getPickDescription = pick => {
2673
+ const {
2674
+ provider
2675
+ } = state;
2676
+ // @ts-ignore
2677
+ if (provider.getPickDescription) {
2678
+ // @ts-ignore
2679
+ return provider.getPickDescription(pick);
2680
+ }
2681
+ return '';
2682
+ };
2683
+ const getPickLabel = pick => {
2684
+ const {
2685
+ provider
2686
+ } = state;
2687
+ // @ts-ignore
2688
+ return provider.getPickLabel(pick);
2689
+ };
2690
+ const getPickIcon = pick => {
2691
+ const {
2692
+ provider
2693
+ } = state;
2694
+ // @ts-ignore
2695
+ return provider.getPickIcon(pick);
2696
+ };
2697
+ const getPickFileIcon = pick => {
2698
+ const {
2699
+ provider
2700
+ } = state;
2701
+ // @ts-ignore
2702
+ if (provider.getPickFileIcon) {
2703
+ // @ts-ignore
2704
+ return provider.getPickFileIcon(pick);
2705
+ }
2706
+ return '';
2707
+ };
2708
+ const isPrepared = () => {
2709
+ const {
2710
+ provider
2711
+ } = state;
2712
+ // @ts-ignore
2713
+ if (provider.isPrepared) {
2714
+ // @ts-ignore
2715
+ return provider.isPrepared();
2716
+ }
2717
+ return false;
2718
+ };
2719
+
2720
+ const QuickPickEntriesEverything = {
2721
+ __proto__: null,
2722
+ getFilterValue,
2723
+ getHelpEntries: getHelpEntries$1,
2724
+ getLabel,
2725
+ getNoResults: getNoResults$1,
2726
+ getPickDescription,
2727
+ getPickFileIcon,
2728
+ getPickFilterValue,
2729
+ getPickIcon,
2730
+ getPickLabel,
2731
+ getPicks: getPicks$1,
2732
+ getPlaceholder: getPlaceholder$1,
2733
+ isPrepared,
2734
+ name: name$1,
2735
+ openCommandPalette,
2736
+ openView,
2737
+ selectPick: selectPick$1,
2738
+ state
2739
+ };
2740
+
2741
+ const name = 'number';
2742
+ const getPlaceholder = () => {
2743
+ return '';
2744
+ };
2745
+ const getHelpEntries = () => {
2746
+ return [];
2747
+ };
2748
+ const getNoResults = () => {
2749
+ return {
2750
+ label: noMatchingResults()
2751
+ };
2752
+ };
2753
+ const getPicks = async () => {
2754
+ const picks = [{
2755
+ label: '1'
2756
+ }, {
2757
+ label: '2'
2758
+ }, {
2759
+ label: '3'
2760
+ }, {
2761
+ label: '4'
2762
+ }, {
2763
+ label: '5'
2764
+ }, {
2765
+ label: '6'
2766
+ }, {
2767
+ label: '7'
2768
+ }, {
2769
+ label: '8'
2770
+ }, {
2771
+ label: '9'
2772
+ }, {
2773
+ label: '10'
2774
+ }];
2775
+ return picks;
2776
+ };
2777
+ const selectPick = async item => {
2778
+ return {
2779
+ command: Hide
2780
+ };
2781
+ };
2782
+
2783
+ const QuickPickEntriesNumber = {
2784
+ __proto__: null,
2785
+ getHelpEntries,
2786
+ getNoResults,
2787
+ getPicks,
2788
+ getPlaceholder,
2789
+ name,
2790
+ selectPick
2791
+ };
2792
+
2793
+ const load = moduleId => {
2794
+ switch (moduleId) {
2795
+ case CommandPalette:
2796
+ case File$1:
2797
+ case EveryThing:
2798
+ case WorkspaceSymbol$1:
2799
+ return QuickPickEntriesEverything;
2800
+ case Number$1:
2801
+ return QuickPickEntriesNumber;
2802
+ case Recent:
2803
+ return QuickPickEntriesOpenRecent;
2804
+ case ColorTheme:
2805
+ return QuickPickEntriesColorTheme;
2806
+ case Symbol$2:
2807
+ return QuickPickEntriesSymbol;
2808
+ case View$1:
2809
+ return QuickPickEntriesView;
2810
+ case Custom:
2811
+ return QuickPickEntriesCustom;
2812
+ default:
2813
+ throw new Error(`unknown module "${moduleId}"`);
2814
+ }
2815
+ };
2816
+
1990
2817
  const commandMap = {
1991
2818
  'FileSystemFetch.chmod': chmod$1,
1992
2819
  'FileSystemFetch.getBlob': getBlob$1,
@@ -2006,6 +2833,7 @@ const commandMap = {
2006
2833
  'FileSystemMemory.writeFile': writeFile,
2007
2834
  'QuickPick.getKeyBindings': getKeyBindings,
2008
2835
  'QuickPick.loadEntries': loadQuickPickEntries,
2836
+ 'QuickPick.loadEntries2': load,
2009
2837
  'SearchFile.filter': filterQuickPickItems,
2010
2838
  'SearchFile.searchFile': searchFile,
2011
2839
  'SearchFile.searchFileWithFetch': searchFile$3,