@lvce-editor/opener-worker 1.5.0 → 1.9.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.
@@ -463,6 +463,27 @@ const IpcParentWithMessagePort$1 = {
463
463
  wrap: wrap$5
464
464
  };
465
465
 
466
+ class CommandNotFoundError extends Error {
467
+ constructor(command) {
468
+ super(`Command not found ${command}`);
469
+ this.name = 'CommandNotFoundError';
470
+ }
471
+ }
472
+ const commands = Object.create(null);
473
+ const register = commandMap => {
474
+ Object.assign(commands, commandMap);
475
+ };
476
+ const getCommand = key => {
477
+ return commands[key];
478
+ };
479
+ const execute = (command, ...args) => {
480
+ const fn = getCommand(command);
481
+ if (!fn) {
482
+ throw new CommandNotFoundError(command);
483
+ }
484
+ return fn(...args);
485
+ };
486
+
466
487
  const Two$1 = '2.0';
467
488
  const callbacks = Object.create(null);
468
489
  const get$2 = id => {
@@ -487,12 +508,12 @@ const getErrorConstructor = (message, type) => {
487
508
  switch (type) {
488
509
  case DomException:
489
510
  return DOMException;
490
- case TypeError$1:
491
- return TypeError;
492
- case SyntaxError$1:
493
- return SyntaxError;
494
511
  case ReferenceError$1:
495
512
  return ReferenceError;
513
+ case SyntaxError$1:
514
+ return SyntaxError;
515
+ case TypeError$1:
516
+ return TypeError;
496
517
  default:
497
518
  return Error;
498
519
  }
@@ -648,56 +669,56 @@ const getErrorProperty = (error, prettyError) => {
648
669
  if (error && error.code === E_COMMAND_NOT_FOUND) {
649
670
  return {
650
671
  code: MethodNotFound,
651
- message: error.message,
652
- data: error.stack
672
+ data: error.stack,
673
+ message: error.message
653
674
  };
654
675
  }
655
676
  return {
656
677
  code: Custom,
657
- message: prettyError.message,
658
678
  data: {
659
- stack: getStack(prettyError),
660
- codeFrame: prettyError.codeFrame,
661
- type: getErrorType(prettyError),
662
679
  code: prettyError.code,
663
- name: prettyError.name
664
- }
680
+ codeFrame: prettyError.codeFrame,
681
+ name: prettyError.name,
682
+ stack: getStack(prettyError),
683
+ type: getErrorType(prettyError)
684
+ },
685
+ message: prettyError.message
665
686
  };
666
687
  };
667
- const create$1 = (id, error) => {
688
+ const create$1$1 = (id, error) => {
668
689
  return {
669
- jsonrpc: Two$1,
690
+ error,
670
691
  id,
671
- error
692
+ jsonrpc: Two$1
672
693
  };
673
694
  };
674
695
  const getErrorResponse = (id, error, preparePrettyError, logError) => {
675
696
  const prettyError = preparePrettyError(error);
676
697
  logError(error, prettyError);
677
698
  const errorProperty = getErrorProperty(error, prettyError);
678
- return create$1(id, errorProperty);
699
+ return create$1$1(id, errorProperty);
679
700
  };
680
- const create$6 = (message, result) => {
701
+ const create$8 = (message, result) => {
681
702
  return {
682
- jsonrpc: Two$1,
683
703
  id: message.id,
704
+ jsonrpc: Two$1,
684
705
  result: result ?? null
685
706
  };
686
707
  };
687
708
  const getSuccessResponse = (message, result) => {
688
709
  const resultProperty = result ?? null;
689
- return create$6(message, resultProperty);
710
+ return create$8(message, resultProperty);
690
711
  };
691
712
  const getErrorResponseSimple = (id, error) => {
692
713
  return {
693
- jsonrpc: Two$1,
694
- id,
695
714
  error: {
696
715
  code: Custom,
716
+ data: error,
697
717
  // @ts-ignore
698
- message: error.message,
699
- data: error
700
- }
718
+ message: error.message
719
+ },
720
+ id,
721
+ jsonrpc: Two$1
701
722
  };
702
723
  };
703
724
  const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
@@ -727,35 +748,35 @@ const normalizeParams = args => {
727
748
  if (args.length === 1) {
728
749
  const options = args[0];
729
750
  return {
751
+ execute: options.execute,
730
752
  ipc: options.ipc,
753
+ logError: options.logError || defaultLogError,
731
754
  message: options.message,
732
- execute: options.execute,
733
- resolve: options.resolve || defaultResolve,
734
755
  preparePrettyError: options.preparePrettyError || defaultPreparePrettyError,
735
- logError: options.logError || defaultLogError,
736
- requiresSocket: options.requiresSocket || defaultRequiresSocket
756
+ requiresSocket: options.requiresSocket || defaultRequiresSocket,
757
+ resolve: options.resolve || defaultResolve
737
758
  };
738
759
  }
739
760
  return {
761
+ execute: args[2],
740
762
  ipc: args[0],
763
+ logError: args[5],
741
764
  message: args[1],
742
- execute: args[2],
743
- resolve: args[3],
744
765
  preparePrettyError: args[4],
745
- logError: args[5],
746
- requiresSocket: args[6]
766
+ requiresSocket: args[6],
767
+ resolve: args[3]
747
768
  };
748
769
  };
749
770
  const handleJsonRpcMessage = async (...args) => {
750
771
  const options = normalizeParams(args);
751
772
  const {
752
- message,
753
- ipc,
754
773
  execute,
755
- resolve,
756
- preparePrettyError,
774
+ ipc,
757
775
  logError,
758
- requiresSocket
776
+ message,
777
+ preparePrettyError,
778
+ requiresSocket,
779
+ resolve
759
780
  } = options;
760
781
  if ('id' in message) {
761
782
  if ('method' in message) {
@@ -778,36 +799,17 @@ const handleJsonRpcMessage = async (...args) => {
778
799
  throw new JsonRpcError('unexpected message');
779
800
  };
780
801
 
781
- class CommandNotFoundError extends Error {
782
- constructor(command) {
783
- super(`Command not found ${command}`);
784
- this.name = 'CommandNotFoundError';
785
- }
786
- }
787
- const commands = Object.create(null);
788
- const register = commandMap => {
789
- Object.assign(commands, commandMap);
790
- };
791
- const getCommand = key => {
792
- return commands[key];
793
- };
794
- const execute = (command, ...args) => {
795
- const fn = getCommand(command);
796
- if (!fn) {
797
- throw new CommandNotFoundError(command);
798
- }
799
- return fn(...args);
800
- };
801
-
802
802
  const Two = '2.0';
803
- const create$t = (method, params) => {
803
+
804
+ const create$7 = (method, params) => {
804
805
  return {
805
806
  jsonrpc: Two,
806
807
  method,
807
808
  params
808
809
  };
809
810
  };
810
- const create$s = (id, method, params) => {
811
+
812
+ const create$6 = (id, method, params) => {
811
813
  const message = {
812
814
  id,
813
815
  jsonrpc: Two,
@@ -816,15 +818,14 @@ const create$s = (id, method, params) => {
816
818
  };
817
819
  return message;
818
820
  };
821
+
819
822
  let id = 0;
820
- const create$r = () => {
823
+ const create$5 = () => {
821
824
  return ++id;
822
825
  };
823
826
 
824
- /* eslint-disable n/no-unsupported-features/es-syntax */
825
-
826
827
  const registerPromise = map => {
827
- const id = create$r();
828
+ const id = create$5();
828
829
  const {
829
830
  promise,
830
831
  resolve
@@ -836,13 +837,12 @@ const registerPromise = map => {
836
837
  };
837
838
  };
838
839
 
839
- // @ts-ignore
840
840
  const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer) => {
841
841
  const {
842
842
  id,
843
843
  promise
844
844
  } = registerPromise(callbacks);
845
- const message = create$s(id, method, params);
845
+ const message = create$6(id, method, params);
846
846
  if (useSendAndTransfer && ipc.sendAndTransfer) {
847
847
  ipc.sendAndTransfer(message);
848
848
  } else {
@@ -878,12 +878,13 @@ const createRpc = ipc => {
878
878
  * @deprecated
879
879
  */
880
880
  send(method, ...params) {
881
- const message = create$t(method, params);
881
+ const message = create$7(method, params);
882
882
  ipc.send(message);
883
883
  }
884
884
  };
885
885
  return rpc;
886
886
  };
887
+
887
888
  const requiresSocket = () => {
888
889
  return false;
889
890
  };
@@ -898,6 +899,7 @@ const handleMessage = event => {
898
899
  const actualExecute = event?.target?.execute || execute;
899
900
  return handleJsonRpcMessage(event.target, event.data, actualExecute, event.target._resolve, preparePrettyError, logError, actualRequiresSocket);
900
901
  };
902
+
901
903
  const handleIpc = ipc => {
902
904
  if ('addEventListener' in ipc) {
903
905
  ipc.addEventListener('message', handleMessage);
@@ -906,6 +908,7 @@ const handleIpc = ipc => {
906
908
  ipc.on('message', handleMessage);
907
909
  }
908
910
  };
911
+
909
912
  const listen$1 = async (module, options) => {
910
913
  const rawIpc = await module.listen(options);
911
914
  if (module.signal) {
@@ -914,7 +917,8 @@ const listen$1 = async (module, options) => {
914
917
  const ipc = module.wrap(rawIpc);
915
918
  return ipc;
916
919
  };
917
- const create$5 = async ({
920
+
921
+ const create$4 = async ({
918
922
  commandMap,
919
923
  isMessagePortOpen = true,
920
924
  messagePort
@@ -931,19 +935,7 @@ const create$5 = async ({
931
935
  messagePort.start();
932
936
  return rpc;
933
937
  };
934
- const create$4 = async ({
935
- commandMap,
936
- messagePort
937
- }) => {
938
- return create$5({
939
- commandMap,
940
- messagePort
941
- });
942
- };
943
- const PlainMessagePortRpcParent = {
944
- __proto__: null,
945
- create: create$4
946
- };
938
+
947
939
  const create$3 = async ({
948
940
  commandMap,
949
941
  isMessagePortOpen,
@@ -954,17 +946,24 @@ const create$3 = async ({
954
946
  port2
955
947
  } = new MessageChannel();
956
948
  await send(port1);
957
- return create$5({
949
+ return create$4({
958
950
  commandMap,
959
951
  isMessagePortOpen,
960
952
  messagePort: port2
961
953
  });
962
954
  };
963
- const TransferMessagePortRpcParent = {
964
- __proto__: null,
965
- create: create$3
966
- };
955
+
967
956
  const create$2 = async ({
957
+ commandMap,
958
+ messagePort
959
+ }) => {
960
+ return create$4({
961
+ commandMap,
962
+ messagePort
963
+ });
964
+ };
965
+
966
+ const create$1 = async ({
968
967
  commandMap
969
968
  }) => {
970
969
  // TODO create a commandMap per rpc instance
@@ -974,10 +973,7 @@ const create$2 = async ({
974
973
  const rpc = createRpc(ipc);
975
974
  return rpc;
976
975
  };
977
- const WebWorkerRpcClient = {
978
- __proto__: null,
979
- create: create$2
980
- };
976
+
981
977
  const createMockRpc = ({
982
978
  commandMap
983
979
  }) => {
@@ -998,11 +994,6 @@ const createMockRpc = ({
998
994
  return mockRpc;
999
995
  };
1000
996
 
1001
- const Electron = 2;
1002
-
1003
- const RendererWorker = 1;
1004
- const SharedProcess = 1492;
1005
-
1006
997
  const rpcs = Object.create(null);
1007
998
  const set$3 = (id, rpc) => {
1008
999
  rpcs[id] = rpc;
@@ -1051,6 +1042,11 @@ const create = rpcId => {
1051
1042
  };
1052
1043
  };
1053
1044
 
1045
+ const Electron = 2;
1046
+
1047
+ const RendererWorker = 1;
1048
+ const SharedProcess = 1492;
1049
+
1054
1050
  const {
1055
1051
  invoke: invoke$2,
1056
1052
  invokeAndTransfer,
@@ -1058,7 +1054,6 @@ const {
1058
1054
  } = create(RendererWorker);
1059
1055
  const sendMessagePortToSharedProcess = async port => {
1060
1056
  const command = 'HandleElectronMessagePort.handleElectronMessagePort';
1061
- // @ts-ignore
1062
1057
  await invokeAndTransfer('SendMessagePortToExtensionHostWorker.sendMessagePortToSharedProcess', port, command, 0);
1063
1058
  };
1064
1059
 
@@ -1068,7 +1063,7 @@ const {
1068
1063
  } = create(SharedProcess);
1069
1064
 
1070
1065
  const handleMessagePort = async (port, rpcId) => {
1071
- const rpc = await PlainMessagePortRpcParent.create({
1066
+ const rpc = await create$2({
1072
1067
  commandMap: {},
1073
1068
  messagePort: port
1074
1069
  });
@@ -1077,23 +1072,31 @@ const handleMessagePort = async (port, rpcId) => {
1077
1072
  }
1078
1073
  };
1079
1074
 
1075
+ const initialize = async platform => {
1076
+ if (platform === Electron) {
1077
+ await initializeMainProcess();
1078
+ }
1079
+ };
1080
+
1080
1081
  const openNew = async url => {
1081
1082
  await invoke$1('OpenExternal.openExternal', url);
1082
1083
  };
1083
1084
 
1084
- let enabled = false;
1085
- let text = '';
1085
+ const state = {
1086
+ enabled: false,
1087
+ text: ''
1088
+ };
1086
1089
  const set = value => {
1087
- enabled = value;
1090
+ state.enabled = value;
1088
1091
  };
1089
1092
  const get = () => {
1090
- return enabled;
1093
+ return state.enabled;
1091
1094
  };
1092
1095
  const writeUrl = value => {
1093
- text = value;
1096
+ state.text = value;
1094
1097
  };
1095
1098
  const readUrl = () => {
1096
- return text;
1099
+ return state.text;
1097
1100
  };
1098
1101
 
1099
1102
  const invoke = async (method, ...params) => {
@@ -1113,10 +1116,10 @@ const OpenExternal = {
1113
1116
  openExternal: openExternal$1
1114
1117
  };
1115
1118
 
1116
- const openUrlWeb = async url => {
1119
+ const openUrlWeb = async (url, useRedirect) => {
1117
1120
  try {
1118
1121
  // TODO make platform argument required
1119
- await invoke$2('Open.openUrl', url);
1122
+ await invoke$2('Open.openUrl', url, useRedirect);
1120
1123
  } catch (error) {
1121
1124
  throw new VError(error, `Failed to open url ${url}`);
1122
1125
  }
@@ -1126,7 +1129,7 @@ const openUrlElectron = async url => {
1126
1129
  };
1127
1130
 
1128
1131
  // TODO add required platform argument
1129
- const openUrl = async (url, platform) => {
1132
+ const openUrl = async (url, platform, useRedirect = false) => {
1130
1133
  if (get()) {
1131
1134
  writeUrl(url);
1132
1135
  return;
@@ -1135,7 +1138,7 @@ const openUrl = async (url, platform) => {
1135
1138
  case Electron:
1136
1139
  return openUrlElectron(url);
1137
1140
  default:
1138
- return openUrlWeb(url);
1141
+ return openUrlWeb(url, useRedirect);
1139
1142
  }
1140
1143
  };
1141
1144
  const {
@@ -1153,21 +1156,22 @@ const getWorkspaceUri = async () => {
1153
1156
  return invoke$2('Workspace.getUri');
1154
1157
  };
1155
1158
 
1156
- let saveDialogMockReturnValue = null;
1159
+ const saveDialogMock = {
1160
+ returnValue: null
1161
+ };
1157
1162
  const registerSaveDialogMock = value => {
1158
- saveDialogMockReturnValue = value;
1163
+ saveDialogMock.returnValue = value;
1159
1164
  };
1160
1165
  const clearSaveDialogMock = () => {
1161
- saveDialogMockReturnValue = null;
1166
+ saveDialogMock.returnValue = null;
1162
1167
  };
1163
1168
  const showSaveDialog = async (title, properties, platform) => {
1164
- if (saveDialogMockReturnValue !== null) {
1165
- return saveDialogMockReturnValue;
1169
+ if (saveDialogMock.returnValue !== null) {
1170
+ return saveDialogMock.returnValue;
1166
1171
  }
1167
1172
  if (platform === Electron) {
1168
1173
  return invoke$1('ElectronDialog.showSaveDialog', title, properties);
1169
1174
  }
1170
- // When running in web, prompt for filename and combine with workspace path
1171
1175
  const fileName = await invoke$2('Prompt.prompt', title);
1172
1176
  if (!fileName) {
1173
1177
  return {
@@ -1185,6 +1189,7 @@ const showSaveDialog = async (title, properties, platform) => {
1185
1189
  const commandMap = {
1186
1190
  'HandleMessagePort.handleMessagePort': handleMessagePort,
1187
1191
  'Open.enableMemoryOpener': enable,
1192
+ 'Open.initialize': initialize,
1188
1193
  'Open.openExternal': openExternal,
1189
1194
  'Open.openUrl': openUrl,
1190
1195
  'Open.readOpenedUrl': readOpenedMemory,
@@ -1197,7 +1202,7 @@ const send = async port => {
1197
1202
  await sendMessagePortToSharedProcess(port);
1198
1203
  };
1199
1204
  const initializeMainProcess = async () => {
1200
- const rpc = await TransferMessagePortRpcParent.create({
1205
+ const rpc = await create$3({
1201
1206
  commandMap: commandMap,
1202
1207
  send
1203
1208
  });
@@ -1205,7 +1210,7 @@ const initializeMainProcess = async () => {
1205
1210
  };
1206
1211
 
1207
1212
  const initializeRendererWorker = async () => {
1208
- const rpc = await WebWorkerRpcClient.create({
1213
+ const rpc = await create$1({
1209
1214
  commandMap: commandMap
1210
1215
  });
1211
1216
  set$2(rpc);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/opener-worker",
3
- "version": "1.5.0",
3
+ "version": "1.9.0",
4
4
  "description": "Opener Worker",
5
5
  "repository": {
6
6
  "type": "git",