@lvce-editor/editor-worker 6.0.0 → 6.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,6 +13,389 @@ const execute$1 = (command, ...args) => {
13
13
  return fn(...args);
14
14
  };
15
15
 
16
+ const state$9 = {
17
+ /**
18
+ * @type {any}
19
+ */
20
+ ipc: undefined
21
+ };
22
+ const get$7 = () => {
23
+ return state$9.ipc;
24
+ };
25
+ const set$7 = ipc => {
26
+ state$9.ipc = ipc;
27
+ };
28
+
29
+ const Two = '2.0';
30
+ const create$4$1 = (method, params) => {
31
+ return {
32
+ jsonrpc: Two,
33
+ method,
34
+ params
35
+ };
36
+ };
37
+ const callbacks = Object.create(null);
38
+ const set$6 = (id, fn) => {
39
+ callbacks[id] = fn;
40
+ };
41
+ const get$6 = id => {
42
+ return callbacks[id];
43
+ };
44
+ const remove$8 = id => {
45
+ delete callbacks[id];
46
+ };
47
+ let id = 0;
48
+ const create$3$1 = () => {
49
+ return ++id;
50
+ };
51
+ const registerPromise = () => {
52
+ const id = create$3$1();
53
+ const {
54
+ resolve,
55
+ promise
56
+ } = Promise.withResolvers();
57
+ set$6(id, resolve);
58
+ return {
59
+ id,
60
+ promise
61
+ };
62
+ };
63
+ const create$2$1 = (method, params) => {
64
+ const {
65
+ id,
66
+ promise
67
+ } = registerPromise();
68
+ const message = {
69
+ jsonrpc: Two,
70
+ method,
71
+ params,
72
+ id
73
+ };
74
+ return {
75
+ message,
76
+ promise
77
+ };
78
+ };
79
+ class JsonRpcError extends Error {
80
+ constructor(message) {
81
+ super(message);
82
+ this.name = 'JsonRpcError';
83
+ }
84
+ }
85
+ const NewLine$3 = '\n';
86
+ const DomException = 'DOMException';
87
+ const ReferenceError$1 = 'ReferenceError';
88
+ const SyntaxError$1 = 'SyntaxError';
89
+ const TypeError$1 = 'TypeError';
90
+ const getErrorConstructor = (message, type) => {
91
+ if (type) {
92
+ switch (type) {
93
+ case DomException:
94
+ return DOMException;
95
+ case TypeError$1:
96
+ return TypeError;
97
+ case SyntaxError$1:
98
+ return SyntaxError;
99
+ case ReferenceError$1:
100
+ return ReferenceError;
101
+ default:
102
+ return Error;
103
+ }
104
+ }
105
+ if (message.startsWith('TypeError: ')) {
106
+ return TypeError;
107
+ }
108
+ if (message.startsWith('SyntaxError: ')) {
109
+ return SyntaxError;
110
+ }
111
+ if (message.startsWith('ReferenceError: ')) {
112
+ return ReferenceError;
113
+ }
114
+ return Error;
115
+ };
116
+ const constructError = (message, type, name) => {
117
+ const ErrorConstructor = getErrorConstructor(message, type);
118
+ if (ErrorConstructor === DOMException && name) {
119
+ return new ErrorConstructor(message, name);
120
+ }
121
+ if (ErrorConstructor === Error) {
122
+ const error = new Error(message);
123
+ if (name && name !== 'VError') {
124
+ error.name = name;
125
+ }
126
+ return error;
127
+ }
128
+ return new ErrorConstructor(message);
129
+ };
130
+ const getNewLineIndex$1 = (string, startIndex = undefined) => {
131
+ return string.indexOf(NewLine$3, startIndex);
132
+ };
133
+ const getParentStack = error => {
134
+ let parentStack = error.stack || error.data || error.message || '';
135
+ if (parentStack.startsWith(' at')) {
136
+ parentStack = error.message + NewLine$3 + parentStack;
137
+ }
138
+ return parentStack;
139
+ };
140
+ const joinLines$2 = lines => {
141
+ return lines.join(NewLine$3);
142
+ };
143
+ const MethodNotFound = -32601;
144
+ const Custom = -32001;
145
+ const splitLines$2 = lines => {
146
+ return lines.split(NewLine$3);
147
+ };
148
+ const restoreJsonRpcError = error => {
149
+ if (error && error instanceof Error) {
150
+ return error;
151
+ }
152
+ const currentStack = joinLines$2(splitLines$2(new Error().stack || '').slice(1));
153
+ if (error && error.code && error.code === MethodNotFound) {
154
+ const restoredError = new JsonRpcError(error.message);
155
+ const parentStack = getParentStack(error);
156
+ restoredError.stack = parentStack + NewLine$3 + currentStack;
157
+ return restoredError;
158
+ }
159
+ if (error && error.message) {
160
+ const restoredError = constructError(error.message, error.type, error.name);
161
+ if (error.data) {
162
+ if (error.data.stack && error.data.type && error.message) {
163
+ restoredError.stack = error.data.type + ': ' + error.message + NewLine$3 + error.data.stack + NewLine$3 + currentStack;
164
+ } else if (error.data.stack) {
165
+ restoredError.stack = error.data.stack;
166
+ }
167
+ if (error.data.codeFrame) {
168
+ // @ts-ignore
169
+ restoredError.codeFrame = error.data.codeFrame;
170
+ }
171
+ if (error.data.code) {
172
+ // @ts-ignore
173
+ restoredError.code = error.data.code;
174
+ }
175
+ if (error.data.type) {
176
+ // @ts-ignore
177
+ restoredError.name = error.data.type;
178
+ }
179
+ } else {
180
+ if (error.stack) {
181
+ const lowerStack = restoredError.stack || '';
182
+ // @ts-ignore
183
+ const indexNewLine = getNewLineIndex$1(lowerStack);
184
+ const parentStack = getParentStack(error);
185
+ // @ts-ignore
186
+ restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
187
+ }
188
+ if (error.codeFrame) {
189
+ // @ts-ignore
190
+ restoredError.codeFrame = error.codeFrame;
191
+ }
192
+ }
193
+ return restoredError;
194
+ }
195
+ if (typeof error === 'string') {
196
+ return new Error(`JsonRpc Error: ${error}`);
197
+ }
198
+ return new Error(`JsonRpc Error: ${error}`);
199
+ };
200
+ const unwrapJsonRpcResult = responseMessage => {
201
+ if ('error' in responseMessage) {
202
+ const restoredError = restoreJsonRpcError(responseMessage.error);
203
+ throw restoredError;
204
+ }
205
+ if ('result' in responseMessage) {
206
+ return responseMessage.result;
207
+ }
208
+ throw new JsonRpcError('unexpected response message');
209
+ };
210
+ const warn$1 = (...args) => {
211
+ console.warn(...args);
212
+ };
213
+ const resolve = (id, response) => {
214
+ const fn = get$6(id);
215
+ if (!fn) {
216
+ console.log(response);
217
+ warn$1(`callback ${id} may already be disposed`);
218
+ return;
219
+ }
220
+ fn(response);
221
+ remove$8(id);
222
+ };
223
+ const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
224
+ const getErrorType = prettyError => {
225
+ if (prettyError && prettyError.type) {
226
+ return prettyError.type;
227
+ }
228
+ if (prettyError && prettyError.constructor && prettyError.constructor.name) {
229
+ return prettyError.constructor.name;
230
+ }
231
+ return undefined;
232
+ };
233
+ const getErrorProperty = (error, prettyError) => {
234
+ if (error && error.code === E_COMMAND_NOT_FOUND) {
235
+ return {
236
+ code: MethodNotFound,
237
+ message: error.message,
238
+ data: error.stack
239
+ };
240
+ }
241
+ return {
242
+ code: Custom,
243
+ message: prettyError.message,
244
+ data: {
245
+ stack: prettyError.stack,
246
+ codeFrame: prettyError.codeFrame,
247
+ type: getErrorType(prettyError),
248
+ code: prettyError.code,
249
+ name: prettyError.name
250
+ }
251
+ };
252
+ };
253
+ const create$1$1 = (message, error) => {
254
+ return {
255
+ jsonrpc: Two,
256
+ id: message.id,
257
+ error
258
+ };
259
+ };
260
+ const getErrorResponse = (message, error, preparePrettyError, logError) => {
261
+ const prettyError = preparePrettyError(error);
262
+ logError(error, prettyError);
263
+ const errorProperty = getErrorProperty(error, prettyError);
264
+ return create$1$1(message, errorProperty);
265
+ };
266
+ const create$e = (message, result) => {
267
+ return {
268
+ jsonrpc: Two,
269
+ id: message.id,
270
+ result: result ?? null
271
+ };
272
+ };
273
+ const getSuccessResponse = (message, result) => {
274
+ const resultProperty = result ?? null;
275
+ return create$e(message, resultProperty);
276
+ };
277
+ const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
278
+ try {
279
+ const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
280
+ return getSuccessResponse(message, result);
281
+ } catch (error) {
282
+ return getErrorResponse(message, error, preparePrettyError, logError);
283
+ }
284
+ };
285
+ const defaultPreparePrettyError = error => {
286
+ return error;
287
+ };
288
+ const defaultLogError = () => {
289
+ // ignore
290
+ };
291
+ const defaultRequiresSocket = () => {
292
+ return false;
293
+ };
294
+ const defaultResolve = resolve;
295
+
296
+ // TODO maybe remove this in v6 or v7, only accept options object to simplify the code
297
+ const normalizeParams = args => {
298
+ if (args.length === 1) {
299
+ const options = args[0];
300
+ return {
301
+ ipc: options.ipc,
302
+ message: options.message,
303
+ execute: options.execute,
304
+ resolve: options.resolve || defaultResolve,
305
+ preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
306
+ logError: options.logError || defaultLogError,
307
+ requiresSocket: options.requiresSocket || defaultRequiresSocket
308
+ };
309
+ }
310
+ return {
311
+ ipc: args[0],
312
+ message: args[1],
313
+ execute: args[2],
314
+ resolve: args[3],
315
+ preparePrettyError: args[4],
316
+ logError: args[5],
317
+ requiresSocket: args[6]
318
+ };
319
+ };
320
+ const handleJsonRpcMessage = async (...args) => {
321
+ const options = normalizeParams(args);
322
+ const {
323
+ message,
324
+ ipc,
325
+ execute,
326
+ resolve,
327
+ preparePrettyError,
328
+ logError,
329
+ requiresSocket
330
+ } = options;
331
+ if ('id' in message) {
332
+ if ('method' in message) {
333
+ const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
334
+ try {
335
+ ipc.send(response);
336
+ } catch (error) {
337
+ const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
338
+ ipc.send(errorResponse);
339
+ }
340
+ return;
341
+ }
342
+ resolve(message.id, message);
343
+ return;
344
+ }
345
+ if ('method' in message) {
346
+ await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
347
+ return;
348
+ }
349
+ throw new JsonRpcError('unexpected message');
350
+ };
351
+ const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
352
+ const {
353
+ message,
354
+ promise
355
+ } = create$2$1(method, params);
356
+ if (useSendAndTransfer && ipc.sendAndTransfer) {
357
+ ipc.sendAndTransfer(message);
358
+ } else {
359
+ ipc.send(message);
360
+ }
361
+ const responseMessage = await promise;
362
+ return unwrapJsonRpcResult(responseMessage);
363
+ };
364
+ const send = (transport, method, ...params) => {
365
+ const message = create$4$1(method, params);
366
+ transport.send(message);
367
+ };
368
+ const invoke$6 = (ipc, method, ...params) => {
369
+ return invokeHelper(ipc, method, params, false);
370
+ };
371
+ const invokeAndTransfer$2 = (ipc, method, ...params) => {
372
+ return invokeHelper(ipc, method, params, true);
373
+ };
374
+
375
+ const invoke$5 = async (method, ...params) => {
376
+ const ipc = get$7();
377
+ return invoke$6(ipc, method, ...params);
378
+ };
379
+ const invokeAndTransfer$1 = async (method, ...params) => {
380
+ const ipc = get$7();
381
+ return invokeAndTransfer$2(ipc, method, ...params);
382
+ };
383
+ const listen$5 = ipc => {
384
+ set$7(ipc);
385
+ };
386
+
387
+ const invoke$4 = async (method, ...params) => {
388
+ return invoke$5(method, ...params);
389
+ };
390
+ const invokeAndTransfer = async (method, ...params) => {
391
+ return invokeAndTransfer$1(method, ...params);
392
+ };
393
+
394
+ // TODO add tests for this
395
+ const activateByEvent = async event => {
396
+ await invoke$4('ExtensionHostManagement.activateByEvent', event);
397
+ };
398
+
16
399
  const codeGeneratorAccept = state => {
17
400
  // TODO close code generator widget
18
401
  return state;
@@ -34,16 +417,16 @@ const getCombinedMessage = (error, message) => {
34
417
  }
35
418
  return stringifiedError;
36
419
  };
37
- const NewLine$3 = '\n';
38
- const getNewLineIndex$1 = (string, startIndex = undefined) => {
39
- return string.indexOf(NewLine$3, startIndex);
420
+ const NewLine$2 = '\n';
421
+ const getNewLineIndex = (string, startIndex = undefined) => {
422
+ return string.indexOf(NewLine$2, startIndex);
40
423
  };
41
424
  const mergeStacks = (parent, child) => {
42
425
  if (!child) {
43
426
  return parent;
44
427
  }
45
- const parentNewLineIndex = getNewLineIndex$1(parent);
46
- const childNewLineIndex = getNewLineIndex$1(child);
428
+ const parentNewLineIndex = getNewLineIndex(parent);
429
+ const childNewLineIndex = getNewLineIndex(child);
47
430
  if (childNewLineIndex === -1) {
48
431
  return parent;
49
432
  }
@@ -206,9 +589,9 @@ class Ipc extends EventTarget {
206
589
  const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
207
590
  const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
208
591
  const ERR_MODULE_NOT_FOUND = 'ERR_MODULE_NOT_FOUND';
209
- const NewLine$2 = '\n';
210
- const joinLines$2 = lines => {
211
- return lines.join(NewLine$2);
592
+ const NewLine$1 = '\n';
593
+ const joinLines$1 = lines => {
594
+ return lines.join(NewLine$1);
212
595
  };
213
596
  const RE_AT = /^\s+at/;
214
597
  const RE_AT_PROMISE_INDEX = /^\s*at async Promise.all \(index \d+\)$/;
@@ -219,7 +602,7 @@ const getDetails = lines => {
219
602
  const index = lines.findIndex(isNormalStackLine);
220
603
  if (index === -1) {
221
604
  return {
222
- actualMessage: joinLines$2(lines),
605
+ actualMessage: joinLines$1(lines),
223
606
  rest: []
224
607
  };
225
608
  }
@@ -234,8 +617,8 @@ const getDetails = lines => {
234
617
  rest: lines.slice(index, lastIndex)
235
618
  };
236
619
  };
237
- const splitLines$2 = lines => {
238
- return lines.split(NewLine$2);
620
+ const splitLines$1 = lines => {
621
+ return lines.split(NewLine$1);
239
622
  };
240
623
  const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
241
624
  const RE_MESSAGE_CODE_BLOCK_END = /^\s* at/;
@@ -246,7 +629,7 @@ const isMessageCodeBlockEndIndex = line => {
246
629
  return RE_MESSAGE_CODE_BLOCK_END.test(line);
247
630
  };
248
631
  const getMessageCodeBlock = stderr => {
249
- const lines = splitLines$2(stderr);
632
+ const lines = splitLines$1(stderr);
250
633
  const startIndex = lines.findIndex(isMessageCodeBlockStartIndex);
251
634
  const endIndex = startIndex + lines.slice(startIndex).findIndex(isMessageCodeBlockEndIndex, startIndex);
252
635
  const relevantLines = lines.slice(startIndex, endIndex);
@@ -257,7 +640,7 @@ const isModuleNotFoundMessage = line => {
257
640
  return line.includes('[ERR_MODULE_NOT_FOUND]');
258
641
  };
259
642
  const getModuleNotFoundError = stderr => {
260
- const lines = splitLines$2(stderr);
643
+ const lines = splitLines$1(stderr);
261
644
  const messageIndex = lines.findIndex(isModuleNotFoundMessage);
262
645
  const message = lines[messageIndex];
263
646
  return {
@@ -305,7 +688,7 @@ const getHelpfulChildProcessError = (stdout, stderr) => {
305
688
  if (isModuleNotFoundError(stderr)) {
306
689
  return getModuleNotFoundError(stderr);
307
690
  }
308
- const lines = splitLines$2(stderr);
691
+ const lines = splitLines$1(stderr);
309
692
  const {
310
693
  actualMessage,
311
694
  rest
@@ -378,559 +761,213 @@ class IpcChildWithMessagePort extends Ipc {
378
761
  }
379
762
  const wrap$g = port => {
380
763
  return new IpcChildWithMessagePort(port);
381
- };
382
- const IpcChildWithMessagePort$1 = {
383
- __proto__: null,
384
- listen: listen$8,
385
- signal: signal$9,
386
- wrap: wrap$g
387
- };
388
- const listen$7 = () => {
389
- // @ts-ignore
390
- if (typeof WorkerGlobalScope === 'undefined') {
391
- throw new TypeError('module is not in web worker scope');
392
- }
393
- return globalThis;
394
- };
395
- const signal$8 = global => {
396
- global.postMessage(readyMessage);
397
- };
398
- class IpcChildWithModuleWorker extends Ipc {
399
- getData(event) {
400
- return getData$2(event);
401
- }
402
- send(message) {
403
- // @ts-ignore
404
- this._rawIpc.postMessage(message);
405
- }
406
- sendAndTransfer(message) {
407
- const transfer = getTransferrables(message);
408
- // @ts-ignore
409
- this._rawIpc.postMessage(message, transfer);
410
- }
411
- dispose() {
412
- // ignore
413
- }
414
- onClose(callback) {
415
- // ignore
416
- }
417
- onMessage(callback) {
418
- this._rawIpc.addEventListener('message', callback);
419
- }
420
- }
421
- const wrap$f = global => {
422
- return new IpcChildWithModuleWorker(global);
423
- };
424
- const IpcChildWithModuleWorker$1 = {
425
- __proto__: null,
426
- listen: listen$7,
427
- signal: signal$8,
428
- wrap: wrap$f
429
- };
430
- const waitForFirstMessage$1 = async port => {
431
- const {
432
- resolve,
433
- promise
434
- } = Promise.withResolvers();
435
- port.addEventListener('message', resolve, {
436
- once: true
437
- });
438
- const event = await promise;
439
- // @ts-ignore
440
- return event.data;
441
- };
442
- const listen$6 = async () => {
443
- const parentIpcRaw = listen$7();
444
- signal$8(parentIpcRaw);
445
- const parentIpc = wrap$f(parentIpcRaw);
446
- const firstMessage = await waitForFirstMessage$1(parentIpc);
447
- if (firstMessage.method !== 'initialize') {
448
- throw new IpcError$1('unexpected first message');
449
- }
450
- const type = firstMessage.params[0];
451
- if (type === 'message-port') {
452
- parentIpc.send({
453
- jsonrpc: '2.0',
454
- id: firstMessage.id,
455
- result: null
456
- });
457
- parentIpc.dispose();
458
- const port = firstMessage.params[1];
459
- return port;
460
- }
461
- return globalThis;
462
- };
463
- class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
464
- getData(event) {
465
- return getData$2(event);
466
- }
467
- send(message) {
468
- this._rawIpc.postMessage(message);
469
- }
470
- sendAndTransfer(message) {
471
- const transfer = getTransferrables(message);
472
- this._rawIpc.postMessage(message, transfer);
473
- }
474
- dispose() {
475
- if (this._rawIpc.close) {
476
- this._rawIpc.close();
477
- }
478
- }
479
- onClose(callback) {
480
- // ignore
481
- }
482
- onMessage(callback) {
483
- this._rawIpc.addEventListener('message', callback);
484
- this._rawIpc.start();
485
- }
486
- }
487
- const wrap$e = port => {
488
- return new IpcChildWithModuleWorkerAndMessagePort(port);
489
- };
490
- const IpcChildWithModuleWorkerAndMessagePort$1 = {
491
- __proto__: null,
492
- listen: listen$6,
493
- wrap: wrap$e
494
- };
495
- const addListener = (emitter, type, callback) => {
496
- if ('addEventListener' in emitter) {
497
- emitter.addEventListener(type, callback);
498
- } else {
499
- emitter.on(type, callback);
500
- }
501
- };
502
- const removeListener = (emitter, type, callback) => {
503
- if ('removeEventListener' in emitter) {
504
- emitter.removeEventListener(type, callback);
505
- } else {
506
- emitter.off(type, callback);
507
- }
508
- };
509
- const getFirstEvent = (eventEmitter, eventMap) => {
510
- const {
511
- resolve,
512
- promise
513
- } = Promise.withResolvers();
514
- const listenerMap = Object.create(null);
515
- const cleanup = value => {
516
- for (const event of Object.keys(eventMap)) {
517
- removeListener(eventEmitter, event, listenerMap[event]);
518
- }
519
- resolve(value);
520
- };
521
- for (const [event, type] of Object.entries(eventMap)) {
522
- const listener = event => {
523
- cleanup({
524
- type,
525
- event
526
- });
527
- };
528
- addListener(eventEmitter, event, listener);
529
- listenerMap[event] = listener;
530
- }
531
- return promise;
532
- };
533
- const Message$1 = 3;
534
- const create$5$1 = async ({
535
- messagePort,
536
- isMessagePortOpen
537
- }) => {
538
- if (!isMessagePort(messagePort)) {
539
- throw new IpcError$1('port must be of type MessagePort');
540
- }
541
- if (isMessagePortOpen) {
542
- return messagePort;
543
- }
544
- const eventPromise = getFirstEvent(messagePort, {
545
- message: Message$1
546
- });
547
- messagePort.start();
548
- const {
549
- type,
550
- event
551
- } = await eventPromise;
552
- if (type !== Message$1) {
553
- throw new IpcError$1('Failed to wait for ipc message');
554
- }
555
- if (event.data !== readyMessage) {
556
- throw new IpcError$1('unexpected first message');
764
+ };
765
+ const IpcChildWithMessagePort$1 = {
766
+ __proto__: null,
767
+ listen: listen$8,
768
+ signal: signal$9,
769
+ wrap: wrap$g
770
+ };
771
+ const listen$7 = () => {
772
+ // @ts-ignore
773
+ if (typeof WorkerGlobalScope === 'undefined') {
774
+ throw new TypeError('module is not in web worker scope');
557
775
  }
558
- return messagePort;
776
+ return globalThis;
559
777
  };
560
- const signal$1 = messagePort => {
561
- messagePort.start();
778
+ const signal$8 = global => {
779
+ global.postMessage(readyMessage);
562
780
  };
563
- class IpcParentWithMessagePort extends Ipc {
564
- getData = getData$2;
781
+ class IpcChildWithModuleWorker extends Ipc {
782
+ getData(event) {
783
+ return getData$2(event);
784
+ }
565
785
  send(message) {
786
+ // @ts-ignore
566
787
  this._rawIpc.postMessage(message);
567
788
  }
568
789
  sendAndTransfer(message) {
569
790
  const transfer = getTransferrables(message);
791
+ // @ts-ignore
570
792
  this._rawIpc.postMessage(message, transfer);
571
793
  }
572
794
  dispose() {
573
- this._rawIpc.close();
795
+ // ignore
796
+ }
797
+ onClose(callback) {
798
+ // ignore
574
799
  }
575
800
  onMessage(callback) {
576
801
  this._rawIpc.addEventListener('message', callback);
577
802
  }
578
- onClose(callback) {}
579
803
  }
580
- const wrap$5 = messagePort => {
581
- return new IpcParentWithMessagePort(messagePort);
804
+ const wrap$f = global => {
805
+ return new IpcChildWithModuleWorker(global);
582
806
  };
583
- const IpcParentWithMessagePort$1 = {
807
+ const IpcChildWithModuleWorker$1 = {
584
808
  __proto__: null,
585
- create: create$5$1,
586
- signal: signal$1,
587
- wrap: wrap$5
588
- };
589
-
590
- const Two = '2.0';
591
- const create$4$1 = (method, params) => {
592
- return {
593
- jsonrpc: Two,
594
- method,
595
- params
596
- };
597
- };
598
- const callbacks = Object.create(null);
599
- const set$7 = (id, fn) => {
600
- callbacks[id] = fn;
601
- };
602
- const get$7 = id => {
603
- return callbacks[id];
604
- };
605
- const remove$8 = id => {
606
- delete callbacks[id];
607
- };
608
- let id = 0;
609
- const create$3$1 = () => {
610
- return ++id;
809
+ listen: listen$7,
810
+ signal: signal$8,
811
+ wrap: wrap$f
611
812
  };
612
- const registerPromise = () => {
613
- const id = create$3$1();
813
+ const waitForFirstMessage$1 = async port => {
614
814
  const {
615
815
  resolve,
616
816
  promise
617
817
  } = Promise.withResolvers();
618
- set$7(id, resolve);
619
- return {
620
- id,
621
- promise
622
- };
623
- };
624
- const create$2$1 = (method, params) => {
625
- const {
626
- id,
627
- promise
628
- } = registerPromise();
629
- const message = {
630
- jsonrpc: Two,
631
- method,
632
- params,
633
- id
634
- };
635
- return {
636
- message,
637
- promise
638
- };
639
- };
640
- class JsonRpcError extends Error {
641
- constructor(message) {
642
- super(message);
643
- this.name = 'JsonRpcError';
644
- }
645
- }
646
- const NewLine$1 = '\n';
647
- const DomException = 'DOMException';
648
- const ReferenceError$1 = 'ReferenceError';
649
- const SyntaxError$1 = 'SyntaxError';
650
- const TypeError$1 = 'TypeError';
651
- const getErrorConstructor = (message, type) => {
652
- if (type) {
653
- switch (type) {
654
- case DomException:
655
- return DOMException;
656
- case TypeError$1:
657
- return TypeError;
658
- case SyntaxError$1:
659
- return SyntaxError;
660
- case ReferenceError$1:
661
- return ReferenceError;
662
- default:
663
- return Error;
664
- }
665
- }
666
- if (message.startsWith('TypeError: ')) {
667
- return TypeError;
668
- }
669
- if (message.startsWith('SyntaxError: ')) {
670
- return SyntaxError;
671
- }
672
- if (message.startsWith('ReferenceError: ')) {
673
- return ReferenceError;
674
- }
675
- return Error;
818
+ port.addEventListener('message', resolve, {
819
+ once: true
820
+ });
821
+ const event = await promise;
822
+ // @ts-ignore
823
+ return event.data;
676
824
  };
677
- const constructError = (message, type, name) => {
678
- const ErrorConstructor = getErrorConstructor(message, type);
679
- if (ErrorConstructor === DOMException && name) {
680
- return new ErrorConstructor(message, name);
825
+ const listen$6 = async () => {
826
+ const parentIpcRaw = listen$7();
827
+ signal$8(parentIpcRaw);
828
+ const parentIpc = wrap$f(parentIpcRaw);
829
+ const firstMessage = await waitForFirstMessage$1(parentIpc);
830
+ if (firstMessage.method !== 'initialize') {
831
+ throw new IpcError$1('unexpected first message');
681
832
  }
682
- if (ErrorConstructor === Error) {
683
- const error = new Error(message);
684
- if (name && name !== 'VError') {
685
- error.name = name;
686
- }
687
- return error;
833
+ const type = firstMessage.params[0];
834
+ if (type === 'message-port') {
835
+ parentIpc.send({
836
+ jsonrpc: '2.0',
837
+ id: firstMessage.id,
838
+ result: null
839
+ });
840
+ parentIpc.dispose();
841
+ const port = firstMessage.params[1];
842
+ return port;
688
843
  }
689
- return new ErrorConstructor(message);
690
- };
691
- const getNewLineIndex = (string, startIndex = undefined) => {
692
- return string.indexOf(NewLine$1, startIndex);
844
+ return globalThis;
693
845
  };
694
- const getParentStack = error => {
695
- let parentStack = error.stack || error.data || error.message || '';
696
- if (parentStack.startsWith(' at')) {
697
- parentStack = error.message + NewLine$1 + parentStack;
846
+ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
847
+ getData(event) {
848
+ return getData$2(event);
698
849
  }
699
- return parentStack;
700
- };
701
- const joinLines$1 = lines => {
702
- return lines.join(NewLine$1);
703
- };
704
- const MethodNotFound = -32601;
705
- const Custom = -32001;
706
- const splitLines$1 = lines => {
707
- return lines.split(NewLine$1);
708
- };
709
- const restoreJsonRpcError = error => {
710
- if (error && error instanceof Error) {
711
- return error;
850
+ send(message) {
851
+ this._rawIpc.postMessage(message);
712
852
  }
713
- const currentStack = joinLines$1(splitLines$1(new Error().stack || '').slice(1));
714
- if (error && error.code && error.code === MethodNotFound) {
715
- const restoredError = new JsonRpcError(error.message);
716
- const parentStack = getParentStack(error);
717
- restoredError.stack = parentStack + NewLine$1 + currentStack;
718
- return restoredError;
853
+ sendAndTransfer(message) {
854
+ const transfer = getTransferrables(message);
855
+ this._rawIpc.postMessage(message, transfer);
719
856
  }
720
- if (error && error.message) {
721
- const restoredError = constructError(error.message, error.type, error.name);
722
- if (error.data) {
723
- if (error.data.stack && error.data.type && error.message) {
724
- restoredError.stack = error.data.type + ': ' + error.message + NewLine$1 + error.data.stack + NewLine$1 + currentStack;
725
- } else if (error.data.stack) {
726
- restoredError.stack = error.data.stack;
727
- }
728
- if (error.data.codeFrame) {
729
- // @ts-ignore
730
- restoredError.codeFrame = error.data.codeFrame;
731
- }
732
- if (error.data.code) {
733
- // @ts-ignore
734
- restoredError.code = error.data.code;
735
- }
736
- if (error.data.type) {
737
- // @ts-ignore
738
- restoredError.name = error.data.type;
739
- }
740
- } else {
741
- if (error.stack) {
742
- const lowerStack = restoredError.stack || '';
743
- // @ts-ignore
744
- const indexNewLine = getNewLineIndex(lowerStack);
745
- const parentStack = getParentStack(error);
746
- // @ts-ignore
747
- restoredError.stack = parentStack + lowerStack.slice(indexNewLine);
748
- }
749
- if (error.codeFrame) {
750
- // @ts-ignore
751
- restoredError.codeFrame = error.codeFrame;
752
- }
857
+ dispose() {
858
+ if (this._rawIpc.close) {
859
+ this._rawIpc.close();
753
860
  }
754
- return restoredError;
755
- }
756
- if (typeof error === 'string') {
757
- return new Error(`JsonRpc Error: ${error}`);
758
- }
759
- return new Error(`JsonRpc Error: ${error}`);
760
- };
761
- const unwrapJsonRpcResult = responseMessage => {
762
- if ('error' in responseMessage) {
763
- const restoredError = restoreJsonRpcError(responseMessage.error);
764
- throw restoredError;
765
- }
766
- if ('result' in responseMessage) {
767
- return responseMessage.result;
768
- }
769
- throw new JsonRpcError('unexpected response message');
770
- };
771
- const warn$1 = (...args) => {
772
- console.warn(...args);
773
- };
774
- const resolve = (id, response) => {
775
- const fn = get$7(id);
776
- if (!fn) {
777
- console.log(response);
778
- warn$1(`callback ${id} may already be disposed`);
779
- return;
780
- }
781
- fn(response);
782
- remove$8(id);
783
- };
784
- const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
785
- const getErrorType = prettyError => {
786
- if (prettyError && prettyError.type) {
787
- return prettyError.type;
788
861
  }
789
- if (prettyError && prettyError.constructor && prettyError.constructor.name) {
790
- return prettyError.constructor.name;
862
+ onClose(callback) {
863
+ // ignore
791
864
  }
792
- return undefined;
793
- };
794
- const getErrorProperty = (error, prettyError) => {
795
- if (error && error.code === E_COMMAND_NOT_FOUND) {
796
- return {
797
- code: MethodNotFound,
798
- message: error.message,
799
- data: error.stack
800
- };
865
+ onMessage(callback) {
866
+ this._rawIpc.addEventListener('message', callback);
867
+ this._rawIpc.start();
801
868
  }
802
- return {
803
- code: Custom,
804
- message: prettyError.message,
805
- data: {
806
- stack: prettyError.stack,
807
- codeFrame: prettyError.codeFrame,
808
- type: getErrorType(prettyError),
809
- code: prettyError.code,
810
- name: prettyError.name
811
- }
812
- };
813
- };
814
- const create$1$1 = (message, error) => {
815
- return {
816
- jsonrpc: Two,
817
- id: message.id,
818
- error
819
- };
820
- };
821
- const getErrorResponse = (message, error, preparePrettyError, logError) => {
822
- const prettyError = preparePrettyError(error);
823
- logError(error, prettyError);
824
- const errorProperty = getErrorProperty(error, prettyError);
825
- return create$1$1(message, errorProperty);
826
- };
827
- const create$e = (message, result) => {
828
- return {
829
- jsonrpc: Two,
830
- id: message.id,
831
- result: result ?? null
832
- };
869
+ }
870
+ const wrap$e = port => {
871
+ return new IpcChildWithModuleWorkerAndMessagePort(port);
833
872
  };
834
- const getSuccessResponse = (message, result) => {
835
- const resultProperty = result ?? null;
836
- return create$e(message, resultProperty);
873
+ const IpcChildWithModuleWorkerAndMessagePort$1 = {
874
+ __proto__: null,
875
+ listen: listen$6,
876
+ wrap: wrap$e
837
877
  };
838
- const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
839
- try {
840
- const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
841
- return getSuccessResponse(message, result);
842
- } catch (error) {
843
- return getErrorResponse(message, error, preparePrettyError, logError);
878
+ const addListener = (emitter, type, callback) => {
879
+ if ('addEventListener' in emitter) {
880
+ emitter.addEventListener(type, callback);
881
+ } else {
882
+ emitter.on(type, callback);
844
883
  }
845
884
  };
846
- const defaultPreparePrettyError = error => {
847
- return error;
848
- };
849
- const defaultLogError = () => {
850
- // ignore
851
- };
852
- const defaultRequiresSocket = () => {
853
- return false;
854
- };
855
- const defaultResolve = resolve;
856
-
857
- // TODO maybe remove this in v6 or v7, only accept options object to simplify the code
858
- const normalizeParams = args => {
859
- if (args.length === 1) {
860
- const options = args[0];
861
- return {
862
- ipc: options.ipc,
863
- message: options.message,
864
- execute: options.execute,
865
- resolve: options.resolve || defaultResolve,
866
- preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
867
- logError: options.logError || defaultLogError,
868
- requiresSocket: options.requiresSocket || defaultRequiresSocket
869
- };
885
+ const removeListener = (emitter, type, callback) => {
886
+ if ('removeEventListener' in emitter) {
887
+ emitter.removeEventListener(type, callback);
888
+ } else {
889
+ emitter.off(type, callback);
870
890
  }
871
- return {
872
- ipc: args[0],
873
- message: args[1],
874
- execute: args[2],
875
- resolve: args[3],
876
- preparePrettyError: args[4],
877
- logError: args[5],
878
- requiresSocket: args[6]
879
- };
880
891
  };
881
- const handleJsonRpcMessage = async (...args) => {
882
- const options = normalizeParams(args);
892
+ const getFirstEvent = (eventEmitter, eventMap) => {
883
893
  const {
884
- message,
885
- ipc,
886
- execute,
887
894
  resolve,
888
- preparePrettyError,
889
- logError,
890
- requiresSocket
891
- } = options;
892
- if ('id' in message) {
893
- if ('method' in message) {
894
- const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
895
- try {
896
- ipc.send(response);
897
- } catch (error) {
898
- const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
899
- ipc.send(errorResponse);
900
- }
901
- return;
895
+ promise
896
+ } = Promise.withResolvers();
897
+ const listenerMap = Object.create(null);
898
+ const cleanup = value => {
899
+ for (const event of Object.keys(eventMap)) {
900
+ removeListener(eventEmitter, event, listenerMap[event]);
902
901
  }
903
- resolve(message.id, message);
904
- return;
905
- }
906
- if ('method' in message) {
907
- await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
908
- return;
902
+ resolve(value);
903
+ };
904
+ for (const [event, type] of Object.entries(eventMap)) {
905
+ const listener = event => {
906
+ cleanup({
907
+ type,
908
+ event
909
+ });
910
+ };
911
+ addListener(eventEmitter, event, listener);
912
+ listenerMap[event] = listener;
909
913
  }
910
- throw new JsonRpcError('unexpected message');
914
+ return promise;
911
915
  };
912
- const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
916
+ const Message$1 = 3;
917
+ const create$5$1 = async ({
918
+ messagePort,
919
+ isMessagePortOpen
920
+ }) => {
921
+ if (!isMessagePort(messagePort)) {
922
+ throw new IpcError$1('port must be of type MessagePort');
923
+ }
924
+ if (isMessagePortOpen) {
925
+ return messagePort;
926
+ }
927
+ const eventPromise = getFirstEvent(messagePort, {
928
+ message: Message$1
929
+ });
930
+ messagePort.start();
913
931
  const {
914
- message,
915
- promise
916
- } = create$2$1(method, params);
917
- if (useSendAndTransfer && ipc.sendAndTransfer) {
918
- ipc.sendAndTransfer(message);
919
- } else {
920
- ipc.send(message);
932
+ type,
933
+ event
934
+ } = await eventPromise;
935
+ if (type !== Message$1) {
936
+ throw new IpcError$1('Failed to wait for ipc message');
921
937
  }
922
- const responseMessage = await promise;
923
- return unwrapJsonRpcResult(responseMessage);
938
+ if (event.data !== readyMessage) {
939
+ throw new IpcError$1('unexpected first message');
940
+ }
941
+ return messagePort;
924
942
  };
925
- const send = (transport, method, ...params) => {
926
- const message = create$4$1(method, params);
927
- transport.send(message);
943
+ const signal$1 = messagePort => {
944
+ messagePort.start();
928
945
  };
929
- const invoke$6 = (ipc, method, ...params) => {
930
- return invokeHelper(ipc, method, params, false);
946
+ class IpcParentWithMessagePort extends Ipc {
947
+ getData = getData$2;
948
+ send(message) {
949
+ this._rawIpc.postMessage(message);
950
+ }
951
+ sendAndTransfer(message) {
952
+ const transfer = getTransferrables(message);
953
+ this._rawIpc.postMessage(message, transfer);
954
+ }
955
+ dispose() {
956
+ this._rawIpc.close();
957
+ }
958
+ onMessage(callback) {
959
+ this._rawIpc.addEventListener('message', callback);
960
+ }
961
+ onClose(callback) {}
962
+ }
963
+ const wrap$5 = messagePort => {
964
+ return new IpcParentWithMessagePort(messagePort);
931
965
  };
932
- const invokeAndTransfer$2 = (ipc, method, ...params) => {
933
- return invokeHelper(ipc, method, params, true);
966
+ const IpcParentWithMessagePort$1 = {
967
+ __proto__: null,
968
+ create: create$5$1,
969
+ signal: signal$1,
970
+ wrap: wrap$5
934
971
  };
935
972
 
936
973
  const createRpc$1 = ipc => {
@@ -1009,38 +1046,6 @@ const getPortTuple = () => {
1009
1046
  };
1010
1047
  };
1011
1048
 
1012
- const state$9 = {
1013
- /**
1014
- * @type {any}
1015
- */
1016
- ipc: undefined
1017
- };
1018
- const get$6 = () => {
1019
- return state$9.ipc;
1020
- };
1021
- const set$6 = ipc => {
1022
- state$9.ipc = ipc;
1023
- };
1024
-
1025
- const invoke$5 = async (method, ...params) => {
1026
- const ipc = get$6();
1027
- return invoke$6(ipc, method, ...params);
1028
- };
1029
- const invokeAndTransfer$1 = async (method, ...params) => {
1030
- const ipc = get$6();
1031
- return invokeAndTransfer$2(ipc, method, ...params);
1032
- };
1033
- const listen$5 = ipc => {
1034
- set$6(ipc);
1035
- };
1036
-
1037
- const invoke$4 = async (method, ...params) => {
1038
- return invoke$5(method, ...params);
1039
- };
1040
- const invokeAndTransfer = async (method, ...params) => {
1041
- return invokeAndTransfer$1(method, ...params);
1042
- };
1043
-
1044
1049
  const ModuleWorkerAndWorkaroundForChromeDevtoolsBug = 6;
1045
1050
 
1046
1051
  const launchColorPickerWorker = async () => {
@@ -2077,6 +2082,9 @@ class IpcError extends Error {
2077
2082
  const sendMessagePortToExtensionHostWorker = async port => {
2078
2083
  await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToExtensionHostWorker', port, 'HandleMessagePort.handleMessagePort');
2079
2084
  };
2085
+ const sendMessagePortToExtensionHostWorker2 = async (port, initialCommand, rpcId) => {
2086
+ await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToExtensionHostWorker', port, initialCommand, rpcId);
2087
+ };
2080
2088
 
2081
2089
  const withResolvers = () => {
2082
2090
  let _resolve;
@@ -2301,16 +2309,19 @@ const createRpc = method => {
2301
2309
  const invoke = async (method, ...params) => {
2302
2310
  return invoke$6(_ipc, method, ...params);
2303
2311
  };
2312
+ const invokeAndTransfer = async (method, ...params) => {
2313
+ return invokeAndTransfer$2(_ipc, method, ...params);
2314
+ };
2304
2315
  return {
2305
2316
  listen,
2306
- invoke
2317
+ invoke,
2318
+ invokeAndTransfer
2307
2319
  };
2308
2320
  };
2309
2321
 
2310
2322
  const {
2311
2323
  listen: listen$4,
2312
- invoke: invoke$2
2313
- } = createRpc(ExtensionHostWorker);
2324
+ invoke: invoke$2} = createRpc(ExtensionHostWorker);
2314
2325
 
2315
2326
  const ColorPicker$1 = 41;
2316
2327
  const CompletionDetail$1 = 999;
@@ -2339,11 +2350,6 @@ const OnDiagnostic = 'onDiagnostic';
2339
2350
  const OnHover = 'onHover';
2340
2351
  const OnTabCompletion = 'onTabCompletion';
2341
2352
 
2342
- // TODO add tests for this
2343
- const activateByEvent = async event => {
2344
- await invoke$4('ExtensionHostManagement.activateByEvent', event);
2345
- };
2346
-
2347
2353
  const execute = async ({
2348
2354
  editor,
2349
2355
  args,
@@ -10529,7 +10535,7 @@ const keep = [
10529
10535
  // 'ColorPicker.loadContent',
10530
10536
  'Editor.create', 'Editor.getWordAt', 'Editor.getWordBefore', 'Editor.offsetAt', 'Editor.render', 'Editor.getKeyBindings', 'Editor.getPositionAtCursor', 'Editor.getWordAt2', 'Editor.getQuickPickMenuEntries',
10531
10537
  // 'ColorPicker.render',
10532
- 'Editor.getText', 'Editor.getSelections', 'Font.ensure', 'Hover.getHoverInfo', 'Hover.handleSashPointerDown', 'Hover.handleSashPointerMove', 'Hover.handleSashPointerUp', 'Hover.loadContent', 'Hover.render', 'Initialize.initialize'];
10538
+ 'Editor.getText', 'Editor.getSelections', 'Font.ensure', 'SendMessagePortToExtensionHostWorker.sendMessagePortToExtensionHostWorker', 'Hover.getHoverInfo', 'Hover.handleSashPointerDown', 'Hover.handleSashPointerMove', 'Hover.handleSashPointerUp', 'Hover.loadContent', 'Hover.render', 'Initialize.initialize', 'ActivateByEvent.activateByEvent'];
10533
10539
 
10534
10540
  const wrapWidgetCommand = (widgetId, fn) => {
10535
10541
  const isWidget = widget => {
@@ -10609,14 +10615,13 @@ const wrapCommands = commands => {
10609
10615
  };
10610
10616
 
10611
10617
  const commandMap = {
10618
+ 'ActivateByEvent.activateByEvent': activateByEvent,
10612
10619
  'CodeGenerator.accept': codeGeneratorAccept,
10613
10620
  'ColorPicker.loadContent': loadContent$3,
10614
10621
  'Editor.addCursorAbove': addCursorAbove,
10615
10622
  'Editor.addCursorBelow': addCursorBelow,
10616
10623
  'Editor.applyEdit': applyEdit,
10617
10624
  'Editor.braceCompletion': braceCompletion,
10618
- 'Editor.getPositionAtCursor': getPositionAtCursor,
10619
- 'Editor.getWordAt2': getWordAt,
10620
10625
  'Editor.cancelSelection': cancelSelection,
10621
10626
  'Editor.closeCodeGenerator': closeCodeGenerator,
10622
10627
  'Editor.closeCompletion': closeCompletion,
@@ -10660,10 +10665,12 @@ const commandMap = {
10660
10665
  'Editor.findAllReferences': findAllReferences,
10661
10666
  'Editor.format': format,
10662
10667
  'Editor.getKeyBindings': getKeyBindings,
10668
+ 'Editor.getPositionAtCursor': getPositionAtCursor,
10663
10669
  'Editor.getQuickPickMenuEntries': getQuickPickMenuEntries,
10664
10670
  'Editor.getSelections': getSelections,
10665
10671
  'Editor.getText': getText,
10666
10672
  'Editor.getWordAt': getWordAt$1,
10673
+ 'Editor.getWordAt2': getWordAt,
10667
10674
  'Editor.getWordBefore': getWordBefore,
10668
10675
  'Editor.goToDefinition': goToDefinition,
10669
10676
  'Editor.goToTypeDefinition': goToTypeDefinition,
@@ -10801,7 +10808,8 @@ const commandMap = {
10801
10808
  'Hover.handleSashPointerUp': handleSashPointerUp,
10802
10809
  'Hover.loadContent': loadContent,
10803
10810
  'Hover.render': renderHover,
10804
- 'Initialize.initialize': intialize
10811
+ 'Initialize.initialize': intialize,
10812
+ 'SendMessagePortToExtensionHostWorker.sendMessagePortToExtensionHostWorker': sendMessagePortToExtensionHostWorker2
10805
10813
  };
10806
10814
  wrapCommands(commandMap);
10807
10815
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/editor-worker",
3
- "version": "6.0.0",
3
+ "version": "6.1.0",
4
4
  "description": "",
5
5
  "license": "MIT",
6
6
  "author": "Lvce Editor",