@lvce-editor/embeds-process 4.1.0 → 4.4.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.
@@ -7,65 +7,15 @@ const add = (id, ipc) => {
7
7
  const get$2 = id => {
8
8
  return ipcMap[id];
9
9
  };
10
- const remove$1 = id => {
10
+ const remove$2 = id => {
11
11
  delete ipcMap[id];
12
12
  };
13
13
  const getAll = () => {
14
14
  return Object.entries(ipcMap);
15
15
  };
16
16
 
17
- const rpcs = Object.create(null);
18
- const set$7 = (id, rpc) => {
19
- rpcs[id] = rpc;
20
- };
21
- const get$1 = id => {
22
- return rpcs[id];
23
- };
24
-
25
- /* eslint-disable @typescript-eslint/explicit-function-return-type */
26
-
27
- const create$6 = rpcId => {
28
- return {
29
- // @ts-ignore
30
- invoke(method, ...params) {
31
- const rpc = get$1(rpcId);
32
- // @ts-ignore
33
- return rpc.invoke(method, ...params);
34
- },
35
- // @ts-ignore
36
- invokeAndTransfer(method, ...params) {
37
- const rpc = get$1(rpcId);
38
- // @ts-ignore
39
- return rpc.invokeAndTransfer(method, ...params);
40
- },
41
- set(rpc) {
42
- set$7(rpcId, rpc);
43
- }
44
- };
45
- };
46
- const MainProcess$1 = -5;
47
- const SharedProcess$1 = 1;
48
- const {
49
- invoke: invoke$3,
50
- set: set$3
51
- } = create$6(MainProcess$1);
52
- const MainProcess$2 = {
53
- __proto__: null,
54
- invoke: invoke$3,
55
- set: set$3
56
- };
57
- const {
58
- set: set$4
59
- } = create$6(SharedProcess$1);
60
- const SharedProcess$2 = {
61
- __proto__: null,
62
- set: set$4
63
- };
64
-
65
- const {
66
- invoke: invoke$1,
67
- set: set$2
68
- } = MainProcess$2;
17
+ const MainProcess$2 = -5;
18
+ const SharedProcess$2 = 1492;
69
19
 
70
20
  const normalizeLine = line => {
71
21
  if (line.startsWith('Error: ')) {
@@ -123,175 +73,55 @@ class VError extends Error {
123
73
  }
124
74
  }
125
75
 
126
- const dispose = async id => {
127
- try {
128
- await invoke$1('ElectronWebContents.dispose', id);
129
- } catch (error) {
130
- throw new VError(error, `Failed to dispose webcontents`);
131
- }
132
- };
133
- const callFunction = (webContentsId, method, ...params) => {
134
- return invoke$1(`ElectronWebContents.callFunction`, webContentsId, method, ...params);
135
- };
136
- const focus = webContentsId => {
137
- return callFunction(webContentsId, 'focus');
138
- };
139
- const openDevtools = webContentsId => {
140
- return callFunction(webContentsId, 'openDevTools');
141
- };
142
- const reload = webContentsId => {
143
- return callFunction(webContentsId, 'reload');
144
- };
145
- const forward = webContentsId => {
146
- return callFunction(webContentsId, 'goForward');
147
- };
148
- const backward = webContentsId => {
149
- return callFunction(webContentsId, 'goBack');
150
- };
151
- const inspectElement = (webContentsId, x, y) => {
152
- return callFunction(webContentsId, `inspectElement`, x, y);
153
- };
154
-
155
76
  class AssertionError extends Error {
156
77
  constructor(message) {
157
78
  super(message);
158
79
  this.name = 'AssertionError';
159
80
  }
160
81
  }
82
+ const Object$1 = 1;
83
+ const Number$1 = 2;
84
+ const Array$1 = 3;
85
+ const String = 4;
86
+ const Boolean = 5;
87
+ const Function = 6;
88
+ const Null = 7;
89
+ const Unknown = 8;
161
90
  const getType = value => {
162
91
  switch (typeof value) {
163
92
  case 'number':
164
- return 'number';
93
+ return Number$1;
165
94
  case 'function':
166
- return 'function';
95
+ return Function;
167
96
  case 'string':
168
- return 'string';
97
+ return String;
169
98
  case 'object':
170
99
  if (value === null) {
171
- return 'null';
100
+ return Null;
172
101
  }
173
102
  if (Array.isArray(value)) {
174
- return 'array';
103
+ return Array$1;
175
104
  }
176
- return 'object';
105
+ return Object$1;
177
106
  case 'boolean':
178
- return 'boolean';
107
+ return Boolean;
179
108
  default:
180
- return 'unknown';
109
+ return Unknown;
181
110
  }
182
111
  };
183
112
  const object = value => {
184
113
  const type = getType(value);
185
- if (type !== 'object') {
114
+ if (type !== Object$1) {
186
115
  throw new AssertionError('expected value to be of type object');
187
116
  }
188
117
  };
189
118
  const number = value => {
190
119
  const type = getType(value);
191
- if (type !== 'number') {
120
+ if (type !== Number$1) {
192
121
  throw new AssertionError('expected value to be of type number');
193
122
  }
194
123
  };
195
124
 
196
- const createWebContentsView = async (ipcId, restoreId, fallthroughKeyBindings) => {
197
- number(restoreId);
198
- // TODO race condition: ipc can be disposed while webcontents are being created
199
- const webContentsId = await invoke$1('ElectronWebContentsView.createWebContentsView', restoreId);
200
- add(webContentsId, ipcId);
201
- // TODO get window id from renderer worker
202
- await invoke$1('ElectronWebContentsView.attachEventListeners', webContentsId);
203
- await invoke$1('ElectronWebContentsViewFunctions.setBackgroundColor', webContentsId, 'white');
204
- return webContentsId;
205
- };
206
- const disposeWebContentsView = async id => {
207
- await invoke$1('ElectronWebContentsView.disposeWebContentsView', id);
208
- await dispose(id);
209
- };
210
- const resizeWebContentsView = async (id, ...args) => {
211
- return invoke$1('ElectronWebContentsViewFunctions.resizeBrowserView', id, ...args);
212
- };
213
- const setIframeSrc = async (id, ...args) => {
214
- return invoke$1('ElectronWebContentsViewFunctions.setIframeSrc', id, ...args);
215
- };
216
- const setIframeSrcFallback = async (id, ...args) => {
217
- return invoke$1('ElectronWebContentsViewFunctions.setIframeSrcFallback', id, ...args);
218
- };
219
- const setFallthroughKeyBindings = async (id, ...args) => {
220
- return invoke$1('ElectronWebContentsViewFunctions.setFallthroughKeyBindings', id, ...args);
221
- };
222
- const getStats = async (id, ...args) => {
223
- return invoke$1('ElectronWebContentsViewFunctions.getStats', id, ...args);
224
- };
225
- const getDomTree = async id => {
226
- // @ts-ignore
227
- return invoke$1('ElectronWebContentsViewFunctions.getDomTree', id);
228
- };
229
- const insertCss = async (id, css) => {
230
- // @ts-ignore
231
- return invoke$1('ElectronWebContentsViewFunctions.insertCss', id, css);
232
- };
233
- const insertJavaScript = async (id, css) => {
234
- // @ts-ignore
235
- return invoke$1('ElectronWebContentsViewFunctions.insertJavaScript', id, css);
236
- };
237
- const show = async (id, ...args) => {
238
- return invoke$1('ElectronWebContentsViewFunctions.show', id, ...args);
239
- };
240
- const forwardIpcEvent = key => (id, ...args) => {
241
- const ipc = get$2(id);
242
- if (!ipc) {
243
- return;
244
- }
245
- ipc.send(key, id, ...args);
246
- };
247
- const handleDidNavigate = forwardIpcEvent('ElectronWebContentsView.handleDidNavigate');
248
- const handleTitleUpdated = forwardIpcEvent('ElectronWebContentsView.handleTitleUpdated');
249
- const handleWillNavigate = forwardIpcEvent('ElectronWebContentsView.handleWillNavigate');
250
- const handleContextMenu = forwardIpcEvent('ElectronWebContentsView.handleContextMenu');
251
- const handleKeyBinding = forwardIpcEvent('ElectronWebContentsView.handleKeyBinding');
252
- const handleBrowserViewDestroyed = (id, ...args) => {
253
- // TODO send to embeds worker?
254
- };
255
-
256
- const applyIncomingIpcResponse = async (target, response, ipcId) => {
257
- switch (response.type) {
258
- case 'handle':
259
- break;
260
- default:
261
- throw new Error('unexpected response');
262
- }
263
- };
264
-
265
- const handleIncomingIpcMessagePort = async (module, handle, message) => {
266
- const target = await module.targetMessagePort(handle, message);
267
- const response = module.upgradeMessagePort(handle, message);
268
- return {
269
- target,
270
- response
271
- };
272
- };
273
-
274
- const commandMapRef = {};
275
-
276
- const getIdsToDispose = ipc => {
277
- const entries = getAll();
278
- const toDispose = [];
279
- for (const [id, value] of entries) {
280
- if (value === ipc) {
281
- toDispose.push(Number.parseInt(id));
282
- }
283
- }
284
- return toDispose;
285
- };
286
- const handleIpcClosed = async event => {
287
- const idsToDispose = getIdsToDispose(event.target);
288
- for (const id of idsToDispose) {
289
- remove$1(id);
290
- await disposeWebContentsView(id);
291
- }
292
- // SharedProcessIpc.send('HandleMessagePortForEmbedsProcess.handleEmbedsProcessIpcClosed')
293
- };
294
-
295
125
  const isMessagePort = value => {
296
126
  return value && value instanceof MessagePort;
297
127
  };
@@ -469,8 +299,8 @@ const getModuleNotFoundError = stderr => {
469
299
  const messageIndex = lines.findIndex(isModuleNotFoundMessage);
470
300
  const message = lines[messageIndex];
471
301
  return {
472
- message,
473
- code: ERR_MODULE_NOT_FOUND
302
+ code: ERR_MODULE_NOT_FOUND,
303
+ message
474
304
  };
475
305
  };
476
306
  const isModuleNotFoundError = stderr => {
@@ -493,14 +323,14 @@ const isUnhelpfulNativeModuleError = stderr => {
493
323
  const getNativeModuleErrorMessage = stderr => {
494
324
  const message = getMessageCodeBlock(stderr);
495
325
  return {
496
- message: `Incompatible native node module: ${message}`,
497
- code: E_INCOMPATIBLE_NATIVE_MODULE
326
+ code: E_INCOMPATIBLE_NATIVE_MODULE,
327
+ message: `Incompatible native node module: ${message}`
498
328
  };
499
329
  };
500
330
  const getModuleSyntaxError = () => {
501
331
  return {
502
- message: `ES Modules are not supported in electron`,
503
- code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON
332
+ code: E_MODULES_NOT_SUPPORTED_IN_ELECTRON,
333
+ message: `ES Modules are not supported in electron`
504
334
  };
505
335
  };
506
336
  const getHelpfulChildProcessError = (stdout, stderr) => {
@@ -519,8 +349,8 @@ const getHelpfulChildProcessError = (stdout, stderr) => {
519
349
  rest
520
350
  } = getDetails(lines);
521
351
  return {
522
- message: actualMessage,
523
352
  code: '',
353
+ message: actualMessage,
524
354
  stack: rest
525
355
  };
526
356
  };
@@ -530,8 +360,8 @@ class IpcError extends VError {
530
360
  if (stdout || stderr) {
531
361
  // @ts-ignore
532
362
  const {
533
- message,
534
363
  code,
364
+ message,
535
365
  stack
536
366
  } = getHelpfulChildProcessError(stdout, stderr);
537
367
  const cause = new Error(message);
@@ -614,7 +444,7 @@ const listen$a = () => {
614
444
  parentPort
615
445
  } = process;
616
446
  if (!parentPort) {
617
- throw new Error('parent port must be defined');
447
+ throw new IpcError('parent port must be defined');
618
448
  }
619
449
  return parentPort;
620
450
  };
@@ -672,7 +502,7 @@ const getTransferrablesNode = value => {
672
502
  };
673
503
  const listen$5 = async () => {
674
504
  if (!process.send) {
675
- throw new Error('process.send must be defined');
505
+ throw new IpcError('process.send must be defined');
676
506
  }
677
507
  return process;
678
508
  };
@@ -714,7 +544,7 @@ const listen$3 = async () => {
714
544
  parentPort
715
545
  } = await import('node:worker_threads');
716
546
  if (!parentPort) {
717
- throw new IpcError('parentPort is required');
547
+ throw new IpcError('parentPort is required for node worker threads ipc');
718
548
  }
719
549
  return parentPort;
720
550
  };
@@ -769,8 +599,8 @@ const removeListener = (emitter, type, callback) => {
769
599
  };
770
600
  const getFirstEvent = (eventEmitter, eventMap) => {
771
601
  const {
772
- resolve,
773
- promise
602
+ promise,
603
+ resolve
774
604
  } = Promise.withResolvers();
775
605
  const listenerMap = Object.create(null);
776
606
  const cleanup = value => {
@@ -782,8 +612,8 @@ const getFirstEvent = (eventEmitter, eventMap) => {
782
612
  for (const [event, type] of Object.entries(eventMap)) {
783
613
  const listener = event => {
784
614
  cleanup({
785
- type,
786
- event
615
+ event,
616
+ type
787
617
  });
788
618
  };
789
619
  addListener(eventEmitter, event, listener);
@@ -799,28 +629,28 @@ const getFirstWebSocketEvent = async webSocket => {
799
629
  WebSocket
800
630
  } = await import('ws');
801
631
  switch (webSocket.readyState) {
802
- case WebSocket.OPEN:
632
+ case WebSocket.CLOSED:
803
633
  return {
804
- type: Open,
805
- event: undefined
634
+ event: undefined,
635
+ type: Close
806
636
  };
807
- case WebSocket.CLOSED:
637
+ case WebSocket.OPEN:
808
638
  return {
809
- type: Close,
810
- event: undefined
639
+ event: undefined,
640
+ type: Open
811
641
  };
812
642
  }
813
643
  // @ts-ignore
814
644
  const {
815
- type,
816
- event
645
+ event,
646
+ type
817
647
  } = await getFirstEvent(webSocket, {
818
- open: Open,
819
- close: Close
648
+ close: Close,
649
+ open: Open
820
650
  });
821
651
  return {
822
- type,
823
- event
652
+ event,
653
+ type
824
654
  };
825
655
  };
826
656
 
@@ -849,11 +679,9 @@ const handleUpgrade$1 = async (...args) => {
849
679
  // @ts-ignore
850
680
  return module.handleUpgrade(...args);
851
681
  };
852
-
853
- // @ts-ignore
854
682
  const listen$1$1 = async ({
855
- request,
856
- handle
683
+ handle,
684
+ request
857
685
  }) => {
858
686
  if (!request) {
859
687
  throw new IpcError('request must be defined');
@@ -871,18 +699,21 @@ const listen$1$1 = async ({
871
699
  const signal$4 = webSocket => {
872
700
  webSocket.resume();
873
701
  };
874
-
875
- // @ts-ignore
876
702
  const wrap$9 = webSocket => {
877
703
  return {
878
- webSocket,
879
- /**
880
- * @type {any}
881
- */
882
- wrappedListener: undefined,
704
+ dispose() {
705
+ this.webSocket.close();
706
+ },
707
+ // @ts-ignore
708
+ off(event, listener) {
709
+ this.webSocket.off(event, listener);
710
+ },
883
711
  // @ts-ignore
884
712
  on(event, listener) {
885
713
  switch (event) {
714
+ case 'close':
715
+ webSocket.on('close', listener);
716
+ break;
886
717
  case 'message':
887
718
  // @ts-ignore
888
719
  const wrappedListener = message => {
@@ -895,28 +726,23 @@ const wrap$9 = webSocket => {
895
726
  };
896
727
  webSocket.on('message', wrappedListener);
897
728
  break;
898
- case 'close':
899
- webSocket.on('close', listener);
900
- break;
901
729
  default:
902
730
  throw new Error('unknown event listener type');
903
731
  }
904
732
  },
905
733
  // @ts-ignore
906
- off(event, listener) {
907
- this.webSocket.off(event, listener);
908
- },
909
- // @ts-ignore
910
734
  send(message) {
911
735
  const stringifiedMessage = serialize(message);
912
736
  this.webSocket.send(stringifiedMessage);
913
737
  },
914
- dispose() {
915
- this.webSocket.close();
916
- },
917
738
  start() {
918
739
  throw new Error('start method is deprecated');
919
- }
740
+ },
741
+ webSocket,
742
+ /**
743
+ * @type {any}
744
+ */
745
+ wrappedListener: undefined
920
746
  };
921
747
  };
922
748
  const IpcChildWithWebSocket = {
@@ -926,56 +752,14 @@ const IpcChildWithWebSocket = {
926
752
  wrap: wrap$9
927
753
  };
928
754
 
929
- const Two = '2.0';
930
- const create$4$1 = (method, params) => {
931
- return {
932
- jsonrpc: Two,
933
- method,
934
- params
935
- };
936
- };
755
+ const Two$1 = '2.0';
937
756
  const callbacks = Object.create(null);
938
- const set$1 = (id, fn) => {
939
- callbacks[id] = fn;
940
- };
941
- const get = id => {
757
+ const get$1 = id => {
942
758
  return callbacks[id];
943
759
  };
944
- const remove = id => {
760
+ const remove$1 = id => {
945
761
  delete callbacks[id];
946
762
  };
947
- let id = 0;
948
- const create$3 = () => {
949
- return ++id;
950
- };
951
- const registerPromise = () => {
952
- const id = create$3();
953
- const {
954
- resolve,
955
- promise
956
- } = Promise.withResolvers();
957
- set$1(id, resolve);
958
- return {
959
- id,
960
- promise
961
- };
962
- };
963
- const create$2 = (method, params) => {
964
- const {
965
- id,
966
- promise
967
- } = registerPromise();
968
- const message = {
969
- jsonrpc: Two,
970
- method,
971
- params,
972
- id
973
- };
974
- return {
975
- message,
976
- promise
977
- };
978
- };
979
763
  class JsonRpcError extends Error {
980
764
  constructor(message) {
981
765
  super(message);
@@ -1027,6 +811,17 @@ const constructError = (message, type, name) => {
1027
811
  }
1028
812
  return new ErrorConstructor(message);
1029
813
  };
814
+ const joinLines = lines => {
815
+ return lines.join(NewLine);
816
+ };
817
+ const splitLines = lines => {
818
+ return lines.split(NewLine);
819
+ };
820
+ const getCurrentStack = () => {
821
+ const stackLinesToSkip = 3;
822
+ const currentStack = joinLines(splitLines(new Error().stack || '').slice(stackLinesToSkip));
823
+ return currentStack;
824
+ };
1030
825
  const getNewLineIndex = (string, startIndex = undefined) => {
1031
826
  return string.indexOf(NewLine, startIndex);
1032
827
  };
@@ -1037,19 +832,16 @@ const getParentStack = error => {
1037
832
  }
1038
833
  return parentStack;
1039
834
  };
1040
- const joinLines = lines => {
1041
- return lines.join(NewLine);
1042
- };
1043
835
  const MethodNotFound = -32601;
1044
836
  const Custom = -32001;
1045
- const splitLines = lines => {
1046
- return lines.split(NewLine);
1047
- };
1048
837
  const restoreJsonRpcError = error => {
838
+ const currentStack = getCurrentStack();
1049
839
  if (error && error instanceof Error) {
840
+ if (typeof error.stack === 'string') {
841
+ error.stack = error.stack + NewLine + currentStack;
842
+ }
1050
843
  return error;
1051
844
  }
1052
- const currentStack = joinLines(splitLines(new Error().stack || '').slice(1));
1053
845
  if (error && error.code && error.code === MethodNotFound) {
1054
846
  const restoredError = new JsonRpcError(error.message);
1055
847
  const parentStack = getParentStack(error);
@@ -1111,14 +903,14 @@ const warn = (...args) => {
1111
903
  console.warn(...args);
1112
904
  };
1113
905
  const resolve = (id, response) => {
1114
- const fn = get(id);
906
+ const fn = get$1(id);
1115
907
  if (!fn) {
1116
908
  console.log(response);
1117
909
  warn(`callback ${id} may already be disposed`);
1118
910
  return;
1119
911
  }
1120
912
  fn(response);
1121
- remove(id);
913
+ remove$1(id);
1122
914
  };
1123
915
  const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
1124
916
  const getErrorType = prettyError => {
@@ -1130,6 +922,17 @@ const getErrorType = prettyError => {
1130
922
  }
1131
923
  return undefined;
1132
924
  };
925
+ const isAlreadyStack = line => {
926
+ return line.trim().startsWith('at ');
927
+ };
928
+ const getStack = prettyError => {
929
+ const stackString = prettyError.stack || '';
930
+ const newLineIndex = stackString.indexOf('\n');
931
+ if (newLineIndex !== -1 && !isAlreadyStack(stackString.slice(0, newLineIndex))) {
932
+ return stackString.slice(newLineIndex + 1);
933
+ }
934
+ return stackString;
935
+ };
1133
936
  const getErrorProperty = (error, prettyError) => {
1134
937
  if (error && error.code === E_COMMAND_NOT_FOUND) {
1135
938
  return {
@@ -1142,7 +945,7 @@ const getErrorProperty = (error, prettyError) => {
1142
945
  code: Custom,
1143
946
  message: prettyError.message,
1144
947
  data: {
1145
- stack: prettyError.stack,
948
+ stack: getStack(prettyError),
1146
949
  codeFrame: prettyError.codeFrame,
1147
950
  type: getErrorType(prettyError),
1148
951
  code: prettyError.code,
@@ -1150,36 +953,51 @@ const getErrorProperty = (error, prettyError) => {
1150
953
  }
1151
954
  };
1152
955
  };
1153
- const create$1 = (message, error) => {
956
+ const create$1 = (id, error) => {
1154
957
  return {
1155
- jsonrpc: Two,
1156
- id: message.id,
958
+ jsonrpc: Two$1,
959
+ id,
1157
960
  error
1158
961
  };
1159
962
  };
1160
- const getErrorResponse = (message, error, preparePrettyError, logError) => {
963
+ const getErrorResponse = (id, error, preparePrettyError, logError) => {
1161
964
  const prettyError = preparePrettyError(error);
1162
965
  logError(error, prettyError);
1163
966
  const errorProperty = getErrorProperty(error, prettyError);
1164
- return create$1(message, errorProperty);
967
+ return create$1(id, errorProperty);
1165
968
  };
1166
- const create = (message, result) => {
969
+ const create$2 = (message, result) => {
1167
970
  return {
1168
- jsonrpc: Two,
971
+ jsonrpc: Two$1,
1169
972
  id: message.id,
1170
973
  result: result ?? null
1171
974
  };
1172
975
  };
1173
976
  const getSuccessResponse = (message, result) => {
1174
977
  const resultProperty = result ?? null;
1175
- return create(message, resultProperty);
978
+ return create$2(message, resultProperty);
1176
979
  };
1177
- const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
1178
- try {
1179
- const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
980
+ const getErrorResponseSimple = (id, error) => {
981
+ return {
982
+ jsonrpc: Two$1,
983
+ id,
984
+ error: {
985
+ code: Custom,
986
+ // @ts-ignore
987
+ message: error.message,
988
+ data: error
989
+ }
990
+ };
991
+ };
992
+ const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
993
+ try {
994
+ const result = requiresSocket(message.method) ? await execute(message.method, ipc, ...message.params) : await execute(message.method, ...message.params);
1180
995
  return getSuccessResponse(message, result);
1181
996
  } catch (error) {
1182
- return getErrorResponse(message, error, preparePrettyError, logError);
997
+ if (ipc.canUseSimpleErrorResponse) {
998
+ return getErrorResponseSimple(message.id, error);
999
+ }
1000
+ return getErrorResponse(message.id, error, preparePrettyError, logError);
1183
1001
  }
1184
1002
  };
1185
1003
  const defaultPreparePrettyError = error => {
@@ -1234,7 +1052,7 @@ const handleJsonRpcMessage = async (...args) => {
1234
1052
  try {
1235
1053
  ipc.send(response);
1236
1054
  } catch (error) {
1237
- const errorResponse = getErrorResponse(message, error, preparePrettyError, logError);
1055
+ const errorResponse = getErrorResponse(message.id, error, preparePrettyError, logError);
1238
1056
  ipc.send(errorResponse);
1239
1057
  }
1240
1058
  return;
@@ -1248,30 +1066,13 @@ const handleJsonRpcMessage = async (...args) => {
1248
1066
  }
1249
1067
  throw new JsonRpcError('unexpected message');
1250
1068
  };
1251
- const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
1252
- const {
1253
- message,
1254
- promise
1255
- } = create$2(method, params);
1256
- if (useSendAndTransfer && ipc.sendAndTransfer) {
1257
- ipc.sendAndTransfer(message);
1258
- } else {
1259
- ipc.send(message);
1260
- }
1261
- const responseMessage = await promise;
1262
- return unwrapJsonRpcResult(responseMessage);
1263
- };
1264
- const send = (transport, method, ...params) => {
1265
- const message = create$4$1(method, params);
1266
- transport.send(message);
1267
- };
1268
- const invoke = (ipc, method, ...params) => {
1269
- return invokeHelper(ipc, method, params, false);
1270
- };
1271
- const invokeAndTransfer = (ipc, method, ...params) => {
1272
- return invokeHelper(ipc, method, params, true);
1273
- };
1274
1069
 
1070
+ class CommandNotFoundError extends Error {
1071
+ constructor(command) {
1072
+ super(`Command not found ${command}`);
1073
+ this.name = 'CommandNotFoundError';
1074
+ }
1075
+ }
1275
1076
  const commands = Object.create(null);
1276
1077
  const register = commandMap => {
1277
1078
  Object.assign(commands, commandMap);
@@ -1282,29 +1083,92 @@ const getCommand = key => {
1282
1083
  const execute = (command, ...args) => {
1283
1084
  const fn = getCommand(command);
1284
1085
  if (!fn) {
1285
- throw new Error(`command not found ${command}`);
1086
+ throw new CommandNotFoundError(command);
1286
1087
  }
1287
1088
  return fn(...args);
1288
1089
  };
1289
1090
 
1091
+ const Two = '2.0';
1092
+ const create$t = (method, params) => {
1093
+ return {
1094
+ jsonrpc: Two,
1095
+ method,
1096
+ params
1097
+ };
1098
+ };
1099
+ const create$s = (id, method, params) => {
1100
+ const message = {
1101
+ id,
1102
+ jsonrpc: Two,
1103
+ method,
1104
+ params
1105
+ };
1106
+ return message;
1107
+ };
1108
+ let id = 0;
1109
+ const create$r = () => {
1110
+ return ++id;
1111
+ };
1112
+
1113
+ /* eslint-disable n/no-unsupported-features/es-syntax */
1114
+
1115
+ const registerPromise = map => {
1116
+ const id = create$r();
1117
+ const {
1118
+ promise,
1119
+ resolve
1120
+ } = Promise.withResolvers();
1121
+ map[id] = resolve;
1122
+ return {
1123
+ id,
1124
+ promise
1125
+ };
1126
+ };
1127
+
1128
+ // @ts-ignore
1129
+ const invokeHelper = async (callbacks, ipc, method, params, useSendAndTransfer) => {
1130
+ const {
1131
+ id,
1132
+ promise
1133
+ } = registerPromise(callbacks);
1134
+ const message = create$s(id, method, params);
1135
+ if (useSendAndTransfer && ipc.sendAndTransfer) {
1136
+ ipc.sendAndTransfer(message);
1137
+ } else {
1138
+ ipc.send(message);
1139
+ }
1140
+ const responseMessage = await promise;
1141
+ return unwrapJsonRpcResult(responseMessage);
1142
+ };
1290
1143
  const createRpc = ipc => {
1144
+ const callbacks = Object.create(null);
1145
+ ipc._resolve = (id, response) => {
1146
+ const fn = callbacks[id];
1147
+ if (!fn) {
1148
+ console.warn(`callback ${id} may already be disposed`);
1149
+ return;
1150
+ }
1151
+ fn(response);
1152
+ delete callbacks[id];
1153
+ };
1291
1154
  const rpc = {
1155
+ async dispose() {
1156
+ await ipc?.dispose();
1157
+ },
1158
+ invoke(method, ...params) {
1159
+ return invokeHelper(callbacks, ipc, method, params, false);
1160
+ },
1161
+ invokeAndTransfer(method, ...params) {
1162
+ return invokeHelper(callbacks, ipc, method, params, true);
1163
+ },
1292
1164
  // @ts-ignore
1293
1165
  ipc,
1294
1166
  /**
1295
1167
  * @deprecated
1296
1168
  */
1297
1169
  send(method, ...params) {
1298
- send(ipc, method, ...params);
1299
- },
1300
- invoke(method, ...params) {
1301
- return invoke(ipc, method, ...params);
1302
- },
1303
- invokeAndTransfer(method, ...params) {
1304
- return invokeAndTransfer(ipc, method, ...params);
1305
- },
1306
- async dispose() {
1307
- await ipc?.dispose();
1170
+ const message = create$t(method, params);
1171
+ ipc.send(message);
1308
1172
  }
1309
1173
  };
1310
1174
  return rpc;
@@ -1321,7 +1185,7 @@ const logError = () => {
1321
1185
  const handleMessage = event => {
1322
1186
  const actualRequiresSocket = event?.target?.requiresSocket || requiresSocket;
1323
1187
  const actualExecute = event?.target?.execute || execute;
1324
- return handleJsonRpcMessage(event.target, event.data, actualExecute, resolve, preparePrettyError, logError, actualRequiresSocket);
1188
+ return handleJsonRpcMessage(event.target, event.data, actualExecute, event.target._resolve, preparePrettyError, logError, actualRequiresSocket);
1325
1189
  };
1326
1190
  const handleIpc = ipc => {
1327
1191
  if ('addEventListener' in ipc) {
@@ -1339,7 +1203,7 @@ const listen$2 = async (module, options) => {
1339
1203
  const ipc = module.wrap(rawIpc);
1340
1204
  return ipc;
1341
1205
  };
1342
- const create$e = async ({
1206
+ const create$q = async ({
1343
1207
  commandMap,
1344
1208
  messagePort,
1345
1209
  requiresSocket
@@ -1358,9 +1222,9 @@ const create$e = async ({
1358
1222
  };
1359
1223
  const ElectronMessagePortRpcClient = {
1360
1224
  __proto__: null,
1361
- create: create$e
1225
+ create: create$q
1362
1226
  };
1363
- const create$d = async ({
1227
+ const create$p = async ({
1364
1228
  commandMap
1365
1229
  }) => {
1366
1230
  // TODO create a commandMap per rpc instance
@@ -1372,9 +1236,9 @@ const create$d = async ({
1372
1236
  };
1373
1237
  const ElectronUtilityProcessRpcClient = {
1374
1238
  __proto__: null,
1375
- create: create$d
1239
+ create: create$p
1376
1240
  };
1377
- const create$7 = async ({
1241
+ const create$a = async ({
1378
1242
  commandMap
1379
1243
  }) => {
1380
1244
  // TODO create a commandMap per rpc instance
@@ -1386,19 +1250,19 @@ const create$7 = async ({
1386
1250
  };
1387
1251
  const NodeForkedProcessRpcClient = {
1388
1252
  __proto__: null,
1389
- create: create$7
1253
+ create: create$a
1390
1254
  };
1391
- const create$5 = async ({
1255
+ const create$8 = async ({
1392
1256
  commandMap,
1393
- request,
1394
1257
  handle,
1258
+ request,
1395
1259
  requiresSocket
1396
1260
  }) => {
1397
1261
  // TODO create a commandMap per rpc instance
1398
1262
  register(commandMap);
1399
1263
  const ipc = await listen$2(IpcChildWithWebSocket, {
1400
- request,
1401
- handle
1264
+ handle,
1265
+ request
1402
1266
  });
1403
1267
  if (requiresSocket) {
1404
1268
  ipc.requiresSocket = requiresSocket;
@@ -1409,9 +1273,9 @@ const create$5 = async ({
1409
1273
  };
1410
1274
  const NodeWebSocketRpcClient = {
1411
1275
  __proto__: null,
1412
- create: create$5
1276
+ create: create$8
1413
1277
  };
1414
- const create$4 = async ({
1278
+ const create$7 = async ({
1415
1279
  commandMap
1416
1280
  }) => {
1417
1281
  // TODO create a commandMap per rpc instance
@@ -1423,7 +1287,227 @@ const create$4 = async ({
1423
1287
  };
1424
1288
  const NodeWorkerRpcClient = {
1425
1289
  __proto__: null,
1426
- create: create$4
1290
+ create: create$7
1291
+ };
1292
+ const createMockRpc = ({
1293
+ commandMap
1294
+ }) => {
1295
+ const invocations = [];
1296
+ const invoke = (method, ...params) => {
1297
+ invocations.push([method, ...params]);
1298
+ const command = commandMap[method];
1299
+ if (!command) {
1300
+ throw new Error(`command ${method} not found`);
1301
+ }
1302
+ return command(...params);
1303
+ };
1304
+ const mockRpc = {
1305
+ invocations,
1306
+ invoke,
1307
+ invokeAndTransfer: invoke
1308
+ };
1309
+ return mockRpc;
1310
+ };
1311
+
1312
+ const rpcs = Object.create(null);
1313
+ const set$4 = (id, rpc) => {
1314
+ rpcs[id] = rpc;
1315
+ };
1316
+ const get = id => {
1317
+ return rpcs[id];
1318
+ };
1319
+ const remove = id => {
1320
+ delete rpcs[id];
1321
+ };
1322
+
1323
+ /* eslint-disable @typescript-eslint/explicit-function-return-type */
1324
+ const create = rpcId => {
1325
+ return {
1326
+ async dispose() {
1327
+ const rpc = get(rpcId);
1328
+ await rpc.dispose();
1329
+ },
1330
+ // @ts-ignore
1331
+ invoke(method, ...params) {
1332
+ const rpc = get(rpcId);
1333
+ // @ts-ignore
1334
+ return rpc.invoke(method, ...params);
1335
+ },
1336
+ // @ts-ignore
1337
+ invokeAndTransfer(method, ...params) {
1338
+ const rpc = get(rpcId);
1339
+ // @ts-ignore
1340
+ return rpc.invokeAndTransfer(method, ...params);
1341
+ },
1342
+ registerMockRpc(commandMap) {
1343
+ const mockRpc = createMockRpc({
1344
+ commandMap
1345
+ });
1346
+ set$4(rpcId, mockRpc);
1347
+ // @ts-ignore
1348
+ mockRpc[Symbol.dispose] = () => {
1349
+ remove(rpcId);
1350
+ };
1351
+ // @ts-ignore
1352
+ return mockRpc;
1353
+ },
1354
+ set(rpc) {
1355
+ set$4(rpcId, rpc);
1356
+ }
1357
+ };
1358
+ };
1359
+
1360
+ const {
1361
+ invoke: invoke$1,
1362
+ set: set$3
1363
+ } = create(MainProcess$2);
1364
+
1365
+ const MainProcess$1 = {
1366
+ __proto__: null,
1367
+ invoke: invoke$1,
1368
+ set: set$3
1369
+ };
1370
+
1371
+ const {
1372
+ set: set$2
1373
+ } = create(SharedProcess$2);
1374
+
1375
+ const SharedProcess$1 = {
1376
+ __proto__: null,
1377
+ set: set$2
1378
+ };
1379
+
1380
+ const {
1381
+ invoke,
1382
+ set: set$1
1383
+ } = MainProcess$1;
1384
+
1385
+ const dispose = async id => {
1386
+ try {
1387
+ await invoke('ElectronWebContents.dispose', id);
1388
+ } catch (error) {
1389
+ throw new VError(error, `Failed to dispose webcontents`);
1390
+ }
1391
+ };
1392
+ const callFunction = (webContentsId, method, ...params) => {
1393
+ return invoke(`ElectronWebContents.callFunction`, webContentsId, method, ...params);
1394
+ };
1395
+ const focus = webContentsId => {
1396
+ return callFunction(webContentsId, 'focus');
1397
+ };
1398
+ const openDevtools = webContentsId => {
1399
+ return callFunction(webContentsId, 'openDevTools');
1400
+ };
1401
+ const reload = webContentsId => {
1402
+ return callFunction(webContentsId, 'reload');
1403
+ };
1404
+ const forward = webContentsId => {
1405
+ return callFunction(webContentsId, 'goForward');
1406
+ };
1407
+ const backward = webContentsId => {
1408
+ return callFunction(webContentsId, 'goBack');
1409
+ };
1410
+ const inspectElement = (webContentsId, x, y) => {
1411
+ return callFunction(webContentsId, `inspectElement`, x, y);
1412
+ };
1413
+
1414
+ const createWebContentsView = async (ipcId, restoreId, fallthroughKeyBindings) => {
1415
+ number(restoreId);
1416
+ // TODO race condition: ipc can be disposed while webcontents are being created
1417
+ const webContentsId = await invoke('ElectronWebContentsView.createWebContentsView', restoreId);
1418
+ add(webContentsId, ipcId);
1419
+ // TODO get window id from renderer worker
1420
+ await invoke('ElectronWebContentsView.attachEventListeners', webContentsId);
1421
+ await invoke('ElectronWebContentsViewFunctions.setBackgroundColor', webContentsId, 'white');
1422
+ return webContentsId;
1423
+ };
1424
+ const disposeWebContentsView = async id => {
1425
+ await invoke('ElectronWebContentsView.disposeWebContentsView', id);
1426
+ await dispose(id);
1427
+ };
1428
+ const resizeWebContentsView = async (id, ...args) => {
1429
+ return invoke('ElectronWebContentsViewFunctions.resizeBrowserView', id, ...args);
1430
+ };
1431
+ const setIframeSrc = async (id, ...args) => {
1432
+ return invoke('ElectronWebContentsViewFunctions.setIframeSrc', id, ...args);
1433
+ };
1434
+ const setIframeSrcFallback = async (id, ...args) => {
1435
+ return invoke('ElectronWebContentsViewFunctions.setIframeSrcFallback', id, ...args);
1436
+ };
1437
+ const setFallthroughKeyBindings = async (id, ...args) => {
1438
+ return invoke('ElectronWebContentsViewFunctions.setFallthroughKeyBindings', id, ...args);
1439
+ };
1440
+ const getStats = async (id, ...args) => {
1441
+ return invoke('ElectronWebContentsViewFunctions.getStats', id, ...args);
1442
+ };
1443
+ const getDomTree = async id => {
1444
+ // @ts-ignore
1445
+ return invoke('ElectronWebContentsViewFunctions.getDomTree', id);
1446
+ };
1447
+ const insertCss = async (id, css) => {
1448
+ // @ts-ignore
1449
+ return invoke('ElectronWebContentsViewFunctions.insertCss', id, css);
1450
+ };
1451
+ const insertJavaScript = async (id, css) => {
1452
+ // @ts-ignore
1453
+ return invoke('ElectronWebContentsViewFunctions.insertJavaScript', id, css);
1454
+ };
1455
+ const show = async (id, ...args) => {
1456
+ return invoke('ElectronWebContentsViewFunctions.show', id, ...args);
1457
+ };
1458
+ const forwardIpcEvent = key => (id, ...args) => {
1459
+ const ipc = get$2(id);
1460
+ if (!ipc) {
1461
+ return;
1462
+ }
1463
+ ipc.send(key, id, ...args);
1464
+ };
1465
+ const handleDidNavigate = forwardIpcEvent('ElectronWebContentsView.handleDidNavigate');
1466
+ const handleTitleUpdated = forwardIpcEvent('ElectronWebContentsView.handleTitleUpdated');
1467
+ const handleWillNavigate = forwardIpcEvent('ElectronWebContentsView.handleWillNavigate');
1468
+ const handleContextMenu = forwardIpcEvent('ElectronWebContentsView.handleContextMenu');
1469
+ const handleKeyBinding = forwardIpcEvent('ElectronWebContentsView.handleKeyBinding');
1470
+ const handleBrowserViewDestroyed = (id, ...args) => {
1471
+ // TODO send to embeds worker?
1472
+ };
1473
+
1474
+ const applyIncomingIpcResponse = async (target, response, ipcId) => {
1475
+ switch (response.type) {
1476
+ case 'handle':
1477
+ break;
1478
+ default:
1479
+ throw new Error('unexpected response');
1480
+ }
1481
+ };
1482
+
1483
+ const handleIncomingIpcMessagePort = async (module, handle, message) => {
1484
+ const target = await module.targetMessagePort(handle, message);
1485
+ const response = module.upgradeMessagePort(handle, message);
1486
+ return {
1487
+ response,
1488
+ target
1489
+ };
1490
+ };
1491
+
1492
+ const commandMapRef = {};
1493
+
1494
+ const getIdsToDispose = ipc => {
1495
+ const entries = getAll();
1496
+ const toDispose = [];
1497
+ for (const [id, value] of entries) {
1498
+ if (value === ipc) {
1499
+ toDispose.push(Number.parseInt(id));
1500
+ }
1501
+ }
1502
+ return toDispose;
1503
+ };
1504
+ const handleIpcClosed = async event => {
1505
+ const idsToDispose = getIdsToDispose(event.target);
1506
+ for (const id of idsToDispose) {
1507
+ remove$2(id);
1508
+ await disposeWebContentsView(id);
1509
+ }
1510
+ // SharedProcessIpc.send('HandleMessagePortForEmbedsProcess.handleEmbedsProcessIpcClosed')
1427
1511
  };
1428
1512
 
1429
1513
  const NodeWorker = 1;
@@ -1446,14 +1530,14 @@ const Auto = argv => {
1446
1530
 
1447
1531
  const getModule$1 = method => {
1448
1532
  switch (method) {
1533
+ case ElectronMessagePort:
1534
+ return ElectronMessagePortRpcClient.create;
1535
+ case ElectronUtilityProcess:
1536
+ return ElectronUtilityProcessRpcClient.create;
1449
1537
  case NodeForkedProcess:
1450
1538
  return NodeForkedProcessRpcClient.create;
1451
1539
  case NodeWorker:
1452
1540
  return NodeWorkerRpcClient.create;
1453
- case ElectronUtilityProcess:
1454
- return ElectronUtilityProcessRpcClient.create;
1455
- case ElectronMessagePort:
1456
- return ElectronMessagePortRpcClient.create;
1457
1541
  case WebSocket:
1458
1542
  return NodeWebSocketRpcClient.create;
1459
1543
  default:
@@ -1473,9 +1557,9 @@ const listen$1 = async ({
1473
1557
  const targetMessagePort$2 = async (messagePort, message) => {
1474
1558
  object(messagePort);
1475
1559
  const rpc = await listen$1({
1476
- method: ElectronMessagePort,
1560
+ commandMap: commandMapRef,
1477
1561
  messagePort,
1478
- commandMap: commandMapRef
1562
+ method: ElectronMessagePort
1479
1563
  });
1480
1564
  // @ts-ignore
1481
1565
  const {
@@ -1499,10 +1583,10 @@ const HandleIpcEmbedsWorker = {
1499
1583
  const targetMessagePort$1 = async (messagePort, message) => {
1500
1584
  object(messagePort);
1501
1585
  const rpc = await listen$1({
1502
- method: ElectronMessagePort,
1503
- messagePort
1586
+ messagePort,
1587
+ method: ElectronMessagePort
1504
1588
  });
1505
- set$2(rpc);
1589
+ set$1(rpc);
1506
1590
  return rpc;
1507
1591
  };
1508
1592
  const upgradeMessagePort$1 = () => {
@@ -1519,13 +1603,13 @@ const HandleIpcMainProcess = {
1519
1603
 
1520
1604
  const {
1521
1605
  set
1522
- } = SharedProcess$2;
1606
+ } = SharedProcess$1;
1523
1607
 
1524
1608
  const targetMessagePort = async (messagePort, message) => {
1525
1609
  object(messagePort);
1526
1610
  const rpc = await ElectronMessagePortRpcClient.create({
1527
- messagePort,
1528
- commandMap: commandMapRef
1611
+ commandMap: commandMapRef,
1612
+ messagePort
1529
1613
  });
1530
1614
  set(rpc);
1531
1615
  return rpc;
@@ -1545,20 +1629,22 @@ const HandleIpcSharedProcess = {
1545
1629
  const EmbedsWorker = 77;
1546
1630
  const MainProcess = -5;
1547
1631
  const SharedProcess = 1;
1632
+ const SharedProcessAlt = 1492;
1548
1633
  const EmbedsWorkerAlt = 208;
1549
1634
 
1550
1635
  const getModule = ipcId => {
1551
1636
  number(ipcId);
1552
1637
  switch (ipcId) {
1553
- case SharedProcess:
1554
- return HandleIpcSharedProcess;
1555
- // deprecated
1556
1638
  case EmbedsWorker:
1557
1639
  case EmbedsWorkerAlt:
1558
1640
  return HandleIpcEmbedsWorker;
1559
1641
  case MainProcess:
1560
1642
  return HandleIpcMainProcess;
1561
1643
  // deprecated
1644
+ case SharedProcess:
1645
+ case SharedProcessAlt:
1646
+ return HandleIpcSharedProcess;
1647
+ // deprecated
1562
1648
  default:
1563
1649
  throw new Error(`Embeds process encountered unexpected incoming ipc ${ipcId}`);
1564
1650
  }
@@ -1571,8 +1657,8 @@ const handleIncomingIpc = async (ipcId, handle, message) => {
1571
1657
  number(ipcId);
1572
1658
  const module = getModule(ipcId);
1573
1659
  const {
1574
- target,
1575
- response
1660
+ response,
1661
+ target
1576
1662
  } = await getIpcAndResponse(module, handle, message);
1577
1663
  await applyIncomingIpcResponse(target, response);
1578
1664
  };
@@ -1597,7 +1683,7 @@ const createMainProcessRpc = async port => {
1597
1683
 
1598
1684
  const initialize = async port => {
1599
1685
  const rpc = await createMainProcessRpc(port);
1600
- set$2(rpc);
1686
+ set$1(rpc);
1601
1687
  };
1602
1688
 
1603
1689
  const commandMap = {
@@ -1631,8 +1717,8 @@ const commandMap = {
1631
1717
  const listen = async argv => {
1632
1718
  Object.assign(commandMapRef, commandMap);
1633
1719
  await listen$1({
1634
- method: Auto(argv),
1635
- commandMap: commandMap
1720
+ commandMap: commandMap,
1721
+ method: Auto(argv)
1636
1722
  });
1637
1723
  };
1638
1724
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/embeds-process",
3
- "version": "4.1.0",
3
+ "version": "4.4.0",
4
4
  "description": "Embeds Process",
5
5
  "keywords": [
6
6
  "Lvce Editor"