@lvce-editor/editor-worker 2.5.0 → 3.0.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.
@@ -1256,7 +1256,6 @@ const getErrorConstructor = (message, type) => {
1256
1256
  if (type) {
1257
1257
  switch (type) {
1258
1258
  case DomException:
1259
- // @ts-ignore
1260
1259
  return DOMException;
1261
1260
  case TypeError$1:
1262
1261
  return TypeError;
@@ -1281,7 +1280,6 @@ const getErrorConstructor = (message, type) => {
1281
1280
  };
1282
1281
  const constructError = (message, type, name) => {
1283
1282
  const ErrorConstructor = getErrorConstructor(message, type);
1284
- // @ts-ignore
1285
1283
  if (ErrorConstructor === DOMException && name) {
1286
1284
  return new ErrorConstructor(message, name);
1287
1285
  }
@@ -1297,6 +1295,13 @@ const constructError = (message, type, name) => {
1297
1295
  const getNewLineIndex$2 = (string, startIndex = undefined) => {
1298
1296
  return string.indexOf(NewLine$3, startIndex);
1299
1297
  };
1298
+ const getParentStack = error => {
1299
+ let parentStack = error.stack || error.data || error.message || '';
1300
+ if (parentStack.startsWith(' at')) {
1301
+ parentStack = error.message + NewLine$3 + parentStack;
1302
+ }
1303
+ return parentStack;
1304
+ };
1300
1305
  const joinLines$1 = lines => {
1301
1306
  return lines.join(NewLine$3);
1302
1307
  };
@@ -1305,18 +1310,11 @@ const Custom = -32001;
1305
1310
  const splitLines$1 = lines => {
1306
1311
  return lines.split(NewLine$3);
1307
1312
  };
1308
- const getParentStack = error => {
1309
- let parentStack = error.stack || error.data || error.message || '';
1310
- if (parentStack.startsWith(' at')) {
1311
- parentStack = error.message + NewLine$3 + parentStack;
1312
- }
1313
- return parentStack;
1314
- };
1315
1313
  const restoreJsonRpcError = error => {
1316
1314
  if (error && error instanceof Error) {
1317
1315
  return error;
1318
1316
  }
1319
- const currentStack = joinLines$1(splitLines$1(new Error().stack).slice(1));
1317
+ const currentStack = joinLines$1(splitLines$1(new Error().stack || '').slice(1));
1320
1318
  if (error && error.code && error.code === MethodNotFound) {
1321
1319
  const restoredError = new JsonRpcError(error.message);
1322
1320
  const parentStack = getParentStack(error);
@@ -1345,7 +1343,6 @@ const restoreJsonRpcError = error => {
1345
1343
  }
1346
1344
  } else {
1347
1345
  if (error.stack) {
1348
- // TODO accessing stack might be slow
1349
1346
  const lowerStack = restoredError.stack || '';
1350
1347
  // @ts-ignore
1351
1348
  const indexNewLine = getNewLineIndex$2(lowerStack);
@@ -1427,7 +1424,42 @@ const getResponse = async (message, ipc, execute, preparePrettyError, logError,
1427
1424
  return getErrorResponse(message, error, preparePrettyError, logError);
1428
1425
  }
1429
1426
  };
1430
- const handleJsonRpcMessage = async (ipc, message, execute, resolve, preparePrettyError, logError, requiresSocket) => {
1427
+ const defaultPreparePrettyError = error => {
1428
+ return error;
1429
+ };
1430
+ const defaultLogError = () => {
1431
+ // ignore
1432
+ };
1433
+ const defaultRequiresSocket = () => {
1434
+ return false;
1435
+ };
1436
+ const defaultResolve = resolve;
1437
+ const handleJsonRpcMessage = async (...args) => {
1438
+ let message;
1439
+ let ipc;
1440
+ let execute;
1441
+ let preparePrettyError;
1442
+ let logError;
1443
+ let resolve;
1444
+ let requiresSocket;
1445
+ if (args.length === 1) {
1446
+ const arg = args[0];
1447
+ message = arg.message;
1448
+ ipc = arg.ipc;
1449
+ execute = arg.execute;
1450
+ preparePrettyError = arg.preparePrettyError || defaultPreparePrettyError;
1451
+ logError = arg.logError || defaultLogError;
1452
+ requiresSocket = arg.requiresSocket || defaultRequiresSocket;
1453
+ resolve = arg.resolve || defaultResolve;
1454
+ } else {
1455
+ ipc = args[0];
1456
+ message = args[1];
1457
+ execute = args[2];
1458
+ resolve = args[3];
1459
+ preparePrettyError = args[4];
1460
+ logError = args[5];
1461
+ requiresSocket = args[6];
1462
+ }
1431
1463
  if ('id' in message) {
1432
1464
  if ('method' in message) {
1433
1465
  const response = await getResponse(message, ipc, execute, preparePrettyError, logError, requiresSocket);
@@ -1458,12 +1490,19 @@ const invoke$5 = async (ipc, method, ...params) => {
1458
1490
  const result = unwrapJsonRpcResult(responseMessage);
1459
1491
  return result;
1460
1492
  };
1493
+
1494
+ // TODO deprecated old typings,
1495
+ // always use automatic transferrable detection
1461
1496
  const invokeAndTransfer$2 = async (ipc, handle, method, ...params) => {
1497
+ if (typeof handle === 'string') {
1498
+ params = [method, ...params];
1499
+ method = handle;
1500
+ }
1462
1501
  const {
1463
1502
  message,
1464
1503
  promise
1465
1504
  } = create$2$1(method, params);
1466
- ipc.sendAndTransfer(message, handle);
1505
+ ipc.sendAndTransfer(message);
1467
1506
  const responseMessage = await promise;
1468
1507
  const result = unwrapJsonRpcResult(responseMessage);
1469
1508
  return result;
@@ -1567,9 +1606,9 @@ const invoke$4 = (method, ...params) => {
1567
1606
  const ipc = get$4();
1568
1607
  return invoke$5(ipc, method, ...params);
1569
1608
  };
1570
- const invokeAndTransfer$1 = async (transfer, method, ...params) => {
1609
+ const invokeAndTransfer$1 = async (method, ...params) => {
1571
1610
  const ipc = get$4();
1572
- return invokeAndTransfer$2(ipc, transfer, method, ...params);
1611
+ return invokeAndTransfer$2(ipc, method, ...params);
1573
1612
  };
1574
1613
  const listen$8 = ipc => {
1575
1614
  set$4(ipc);
@@ -1578,12 +1617,12 @@ const listen$8 = ipc => {
1578
1617
  const invoke$3 = async (method, ...params) => {
1579
1618
  return invoke$4(method, ...params);
1580
1619
  };
1581
- const invokeAndTransfer = async (transfer, method, ...params) => {
1582
- return invokeAndTransfer$1(transfer, method, ...params);
1620
+ const invokeAndTransfer = async (method, ...params) => {
1621
+ return invokeAndTransfer$1(method, ...params);
1583
1622
  };
1584
1623
 
1585
1624
  const sendMessagePortToExtensionHostWorker = async port => {
1586
- await invokeAndTransfer([port], 'SendMessagePortToExtensionHostWorker.sendMessagePortToExtensionHostWorker', port, 'HandleMessagePort.handleMessagePort');
1625
+ await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToExtensionHostWorker', port, 'HandleMessagePort.handleMessagePort');
1587
1626
  };
1588
1627
 
1589
1628
  const withResolvers$1 = () => {
@@ -1664,7 +1703,7 @@ const IpcParentWithExtensionHostWorker = {
1664
1703
  };
1665
1704
 
1666
1705
  const sendMessagePortToSyntaxHighlightingWorker = async port => {
1667
- await invokeAndTransfer([port], 'SendMessagePortToSyntaxHighlightingWorker.sendMessagePortToSyntaxHighlightingWorker', port, 'HandleMessagePort.handleMessagePort');
1706
+ await invokeAndTransfer('SendMessagePortToSyntaxHighlightingWorker.sendMessagePortToSyntaxHighlightingWorker', port, 'HandleMessagePort.handleMessagePort');
1668
1707
  };
1669
1708
 
1670
1709
  const create$3 = async () => {
@@ -1717,7 +1756,7 @@ const IpcParentWithSyntaxHighlightingWorker = {
1717
1756
  };
1718
1757
 
1719
1758
  const sendMessagePortToRendererProcess = async port => {
1720
- await invokeAndTransfer([port], 'SendMessagePortToRendererProcess.sendMessagePortToRendererProcess', port, 'HandleMessagePort.handleMessagePort');
1759
+ await invokeAndTransfer('SendMessagePortToRendererProcess.sendMessagePortToRendererProcess', port, 'HandleMessagePort.handleMessagePort');
1721
1760
  };
1722
1761
 
1723
1762
  const create$2 = async () => {
@@ -2364,19 +2403,14 @@ const closeCompletion = editor => {
2364
2403
  };
2365
2404
  };
2366
2405
 
2367
- // @ts-ignore
2368
2406
  const state$6 = {
2369
2407
  isComposing: false,
2370
2408
  compositionText: ''
2371
2409
  };
2372
-
2373
- // @ts-ignore
2374
2410
  const compositionStart = (editor, event) => {
2375
2411
  state$6.isComposing = true;
2376
2412
  return editor;
2377
2413
  };
2378
-
2379
- // @ts-ignore
2380
2414
  const getCompositionChanges = (selections, data) => {
2381
2415
  const changes = [];
2382
2416
  for (let i = 0; i < selections.length; i += 4) {
@@ -2401,16 +2435,12 @@ const getCompositionChanges = (selections, data) => {
2401
2435
  }
2402
2436
  return changes;
2403
2437
  };
2404
-
2405
- // @ts-ignore
2406
2438
  const compositionUpdate = (editor, data) => {
2407
2439
  const selections = editor.selections;
2408
2440
  const changes = getCompositionChanges(selections, data);
2409
2441
  state$6.compositionText = data;
2410
2442
  return scheduleDocumentAndCursorsSelections(editor, changes);
2411
2443
  };
2412
-
2413
- // @ts-ignore
2414
2444
  const compositionEnd = (editor, data) => {
2415
2445
  const selections = editor.selections;
2416
2446
  const changes = getCompositionChanges(selections, data);
@@ -3324,7 +3354,7 @@ const goTo = async ({
3324
3354
  endRowIndex: definition.endRowIndex,
3325
3355
  endColumnIndex: definition.endColumnIndex
3326
3356
  };
3327
- await invoke$3( /* Main.openUri */'Main.openUri', /* uri */uri, /* focus */true, context);
3357
+ await invoke$3(/* Main.openUri */'Main.openUri', /* uri */uri, /* focus */true, context);
3328
3358
  return editor;
3329
3359
  } catch (error) {
3330
3360
  // TODO if editor is already disposed at this point, do nothing
@@ -3449,7 +3479,7 @@ const goToTypeDefinition = (editor, explicit = true) => {
3449
3479
  const Editor = 3;
3450
3480
 
3451
3481
  const handleContextMenu = async (editor, button, x, y) => {
3452
- await invoke$3( /* ContextMenu.show */'ContextMenu.show', /* x */x, /* y */y, /* id */Editor);
3482
+ await invoke$3(/* ContextMenu.show */'ContextMenu.show', /* x */x, /* y */y, /* id */Editor);
3453
3483
  return editor;
3454
3484
  };
3455
3485
 
@@ -6319,7 +6349,7 @@ const toggleComment = async editor => {
6319
6349
  } catch (error$1) {
6320
6350
  error(error$1);
6321
6351
  // TODO use correct position
6322
- await editorShowMessage( /* editor */editor, /* rowIndex */0, /* columnIndex */0, /* message */`${error$1}`, /* isError */true);
6352
+ await editorShowMessage(/* editor */editor, /* rowIndex */0, /* columnIndex */0, /* message */`${error$1}`, /* isError */true);
6323
6353
  return editor;
6324
6354
  }
6325
6355
  };
@@ -8782,14 +8812,14 @@ const commandMap = {
8782
8812
  };
8783
8813
  wrapCommands(commandMap);
8784
8814
 
8785
- const MessagePort = 1;
8815
+ const MessagePort$1 = 1;
8786
8816
  const ModuleWorker = 2;
8787
8817
  const ReferencePort = 3;
8788
8818
  const ModuleWorkerAndMessagePort = 8;
8789
8819
  const Auto = () => {
8790
8820
  // @ts-ignore
8791
8821
  if (globalThis.acceptPort) {
8792
- return MessagePort;
8822
+ return MessagePort$1;
8793
8823
  }
8794
8824
  // @ts-ignore
8795
8825
  if (globalThis.acceptReferencePort) {
@@ -8801,6 +8831,56 @@ const Auto = () => {
8801
8831
  const getData$1 = event => {
8802
8832
  return event.data;
8803
8833
  };
8834
+ const walkValue = (value, transferrables, isTransferrable) => {
8835
+ if (!value) {
8836
+ return;
8837
+ }
8838
+ if (isTransferrable(value)) {
8839
+ transferrables.push(value);
8840
+ return;
8841
+ }
8842
+ if (Array.isArray(value)) {
8843
+ for (const item of value) {
8844
+ walkValue(item, transferrables, isTransferrable);
8845
+ }
8846
+ return;
8847
+ }
8848
+ if (typeof value === 'object') {
8849
+ for (const property of Object.values(value)) {
8850
+ walkValue(property, transferrables, isTransferrable);
8851
+ }
8852
+ return;
8853
+ }
8854
+ };
8855
+ const isMessagePort = value => {
8856
+ return value && value instanceof MessagePort;
8857
+ };
8858
+ const isMessagePortMain = value => {
8859
+ return value && value.constructor && value.constructor.name === 'MessagePortMain';
8860
+ };
8861
+ const isOffscreenCanvas = value => {
8862
+ return typeof OffscreenCanvas !== 'undefined' && value instanceof OffscreenCanvas;
8863
+ };
8864
+ const isInstanceOf = (value, constructorName) => {
8865
+ return value?.constructor?.name === constructorName;
8866
+ };
8867
+ const isSocket = value => {
8868
+ return isInstanceOf(value, 'Socket');
8869
+ };
8870
+ const transferrables = [isMessagePort, isMessagePortMain, isOffscreenCanvas, isSocket];
8871
+ const isTransferrable = value => {
8872
+ for (const fn of transferrables) {
8873
+ if (fn(value)) {
8874
+ return true;
8875
+ }
8876
+ }
8877
+ return false;
8878
+ };
8879
+ const getTransferrables = value => {
8880
+ const transferrables = [];
8881
+ walkValue(value, transferrables, isTransferrable);
8882
+ return transferrables;
8883
+ };
8804
8884
  const attachEvents = that => {
8805
8885
  const handleMessage = (...args) => {
8806
8886
  const data = that.getData(...args);
@@ -8822,14 +8902,14 @@ class Ipc extends EventTarget {
8822
8902
  }
8823
8903
  }
8824
8904
  const readyMessage = 'ready';
8825
- const listen$3 = () => {
8905
+ const listen$4 = () => {
8826
8906
  // @ts-ignore
8827
8907
  if (typeof WorkerGlobalScope === 'undefined') {
8828
8908
  throw new TypeError('module is not in web worker scope');
8829
8909
  }
8830
8910
  return globalThis;
8831
8911
  };
8832
- const signal$2 = global => {
8912
+ const signal$3 = global => {
8833
8913
  global.postMessage(readyMessage);
8834
8914
  };
8835
8915
  class IpcChildWithModuleWorker extends Ipc {
@@ -8840,7 +8920,8 @@ class IpcChildWithModuleWorker extends Ipc {
8840
8920
  // @ts-ignore
8841
8921
  this._rawIpc.postMessage(message);
8842
8922
  }
8843
- sendAndTransfer(message, transfer) {
8923
+ sendAndTransfer(message) {
8924
+ const transfer = getTransferrables(message);
8844
8925
  // @ts-ignore
8845
8926
  this._rawIpc.postMessage(message, transfer);
8846
8927
  }
@@ -8854,14 +8935,14 @@ class IpcChildWithModuleWorker extends Ipc {
8854
8935
  this._rawIpc.addEventListener('message', callback);
8855
8936
  }
8856
8937
  }
8857
- const wrap$5 = global => {
8938
+ const wrap$6 = global => {
8858
8939
  return new IpcChildWithModuleWorker(global);
8859
8940
  };
8860
8941
  const IpcChildWithModuleWorker$1 = {
8861
8942
  __proto__: null,
8862
- listen: listen$3,
8863
- signal: signal$2,
8864
- wrap: wrap$5
8943
+ listen: listen$4,
8944
+ signal: signal$3,
8945
+ wrap: wrap$6
8865
8946
  };
8866
8947
  const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
8867
8948
  const E_MODULES_NOT_SUPPORTED_IN_ELECTRON = 'E_MODULES_NOT_SUPPORTED_IN_ELECTRON';
@@ -8873,6 +8954,18 @@ const joinLines = lines => {
8873
8954
  const splitLines = lines => {
8874
8955
  return lines.split(NewLine$1);
8875
8956
  };
8957
+ const isModuleNotFoundMessage = line => {
8958
+ return line.includes('[ERR_MODULE_NOT_FOUND]');
8959
+ };
8960
+ const getModuleNotFoundError = stderr => {
8961
+ const lines = splitLines(stderr);
8962
+ const messageIndex = lines.findIndex(isModuleNotFoundMessage);
8963
+ const message = lines[messageIndex];
8964
+ return {
8965
+ message,
8966
+ code: ERR_MODULE_NOT_FOUND
8967
+ };
8968
+ };
8876
8969
  const RE_NATIVE_MODULE_ERROR = /^innerError Error: Cannot find module '.*.node'/;
8877
8970
  const RE_NATIVE_MODULE_ERROR_2 = /was compiled against a different Node.js version/;
8878
8971
  const RE_MESSAGE_CODE_BLOCK_START = /^Error: The module '.*'$/;
@@ -8921,18 +9014,6 @@ const isModuleNotFoundError = stderr => {
8921
9014
  }
8922
9015
  return stderr.includes('ERR_MODULE_NOT_FOUND');
8923
9016
  };
8924
- const isModuleNotFoundMessage = line => {
8925
- return line.includes('ERR_MODULE_NOT_FOUND');
8926
- };
8927
- const getModuleNotFoundError = stderr => {
8928
- const lines = splitLines(stderr);
8929
- const messageIndex = lines.findIndex(isModuleNotFoundMessage);
8930
- const message = lines[messageIndex];
8931
- return {
8932
- message,
8933
- code: ERR_MODULE_NOT_FOUND
8934
- };
8935
- };
8936
9017
  const isNormalStackLine = line => {
8937
9018
  return RE_AT.test(line) && !RE_AT_PROMISE_INDEX.test(line);
8938
9019
  };
@@ -9079,16 +9160,21 @@ const waitForFirstMessage = async port => {
9079
9160
  // @ts-ignore
9080
9161
  return event.data;
9081
9162
  };
9082
- const listen$2 = async () => {
9083
- const parentIpcRaw = listen$3();
9084
- signal$2(parentIpcRaw);
9085
- const parentIpc = wrap$5(parentIpcRaw);
9163
+ const listen$3 = async () => {
9164
+ const parentIpcRaw = listen$4();
9165
+ signal$3(parentIpcRaw);
9166
+ const parentIpc = wrap$6(parentIpcRaw);
9086
9167
  const firstMessage = await waitForFirstMessage(parentIpc);
9087
9168
  if (firstMessage.method !== 'initialize') {
9088
9169
  throw new IpcError('unexpected first message');
9089
9170
  }
9090
9171
  const type = firstMessage.params[0];
9091
9172
  if (type === 'message-port') {
9173
+ parentIpc.send({
9174
+ jsonrpc: '2.0',
9175
+ id: firstMessage.id,
9176
+ result: null
9177
+ });
9092
9178
  parentIpc.dispose();
9093
9179
  const port = firstMessage.params[1];
9094
9180
  return port;
@@ -9105,7 +9191,8 @@ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
9105
9191
  send(message) {
9106
9192
  this._rawIpc.postMessage(message);
9107
9193
  }
9108
- sendAndTransfer(message, transfer) {
9194
+ sendAndTransfer(message) {
9195
+ const transfer = getTransferrables(message);
9109
9196
  this._rawIpc.postMessage(message, transfer);
9110
9197
  }
9111
9198
  dispose() {
@@ -9121,15 +9208,15 @@ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
9121
9208
  this._rawIpc.start();
9122
9209
  }
9123
9210
  }
9124
- const wrap$4 = port => {
9211
+ const wrap$5 = port => {
9125
9212
  return new IpcChildWithModuleWorkerAndMessagePort(port);
9126
9213
  };
9127
9214
  const IpcChildWithModuleWorkerAndMessagePort$1 = {
9128
9215
  __proto__: null,
9129
- listen: listen$2,
9130
- wrap: wrap$4
9216
+ listen: listen$3,
9217
+ wrap: wrap$5
9131
9218
  };
9132
- const listen$4 = ({
9219
+ const listen$2 = ({
9133
9220
  port
9134
9221
  }) => {
9135
9222
  return port;
@@ -9147,7 +9234,8 @@ class IpcChildWithMessagePort extends Ipc {
9147
9234
  send(message) {
9148
9235
  this._rawIpc.postMessage(message);
9149
9236
  }
9150
- sendAndTransfer(message, transfer) {
9237
+ sendAndTransfer(message) {
9238
+ const transfer = getTransferrables(message);
9151
9239
  this._rawIpc.postMessage(message, transfer);
9152
9240
  }
9153
9241
  dispose() {
@@ -9166,7 +9254,7 @@ const wrap$2 = port => {
9166
9254
  };
9167
9255
  const IpcChildWithMessagePort$1 = {
9168
9256
  __proto__: null,
9169
- listen: listen$4,
9257
+ listen: listen$2,
9170
9258
  signal,
9171
9259
  wrap: wrap$2
9172
9260
  };
@@ -9177,7 +9265,7 @@ const getModule = method => {
9177
9265
  return IpcChildWithModuleWorker$1;
9178
9266
  case ModuleWorkerAndMessagePort:
9179
9267
  return IpcChildWithModuleWorkerAndMessagePort$1;
9180
- case MessagePort:
9268
+ case MessagePort$1:
9181
9269
  return IpcChildWithMessagePort$1;
9182
9270
  default:
9183
9271
  throw new Error('unexpected ipc type');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/editor-worker",
3
- "version": "2.5.0",
3
+ "version": "3.0.0",
4
4
  "description": "",
5
5
  "main": "dist/testWorkerMain.js",
6
6
  "type": "module",