@lvce-editor/iframe-worker 2.0.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,30 +1,15 @@
1
- const commands = Object.create(null);
2
- const register = commandMap => {
3
- Object.assign(commands, commandMap);
4
- };
5
- const getCommand = key => {
6
- return commands[key];
7
- };
8
- const execute = (command, ...args) => {
9
- const fn = getCommand(command);
10
- if (!fn) {
11
- throw new Error(`command not found ${command}`);
12
- }
13
- return fn(...args);
14
- };
15
-
16
1
  const Two = '2.0';
17
- const state = {
2
+ const state$1 = {
18
3
  callbacks: Object.create(null)
19
4
  };
20
5
  const set = (id, fn) => {
21
- state.callbacks[id] = fn;
6
+ state$1.callbacks[id] = fn;
22
7
  };
23
8
  const get = id => {
24
- return state.callbacks[id];
9
+ return state$1.callbacks[id];
25
10
  };
26
11
  const remove = id => {
27
- delete state.callbacks[id];
12
+ delete state$1.callbacks[id];
28
13
  };
29
14
  let id = 0;
30
15
  const create$3 = () => {
@@ -232,7 +217,7 @@ const getErrorProperty = (error, prettyError) => {
232
217
  }
233
218
  };
234
219
  };
235
- const create$1 = (message, error) => {
220
+ const create$1$2 = (message, error) => {
236
221
  return {
237
222
  jsonrpc: Two,
238
223
  id: message.id,
@@ -243,7 +228,7 @@ const getErrorResponse = (message, error, preparePrettyError, logError) => {
243
228
  const prettyError = preparePrettyError(error);
244
229
  logError(error, prettyError);
245
230
  const errorProperty = getErrorProperty(error, prettyError);
246
- return create$1(message, errorProperty);
231
+ return create$1$2(message, errorProperty);
247
232
  };
248
233
  const create$4 = (message, result) => {
249
234
  return {
@@ -335,19 +320,60 @@ const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
335
320
  message,
336
321
  promise
337
322
  } = create$2(method, params);
338
- {
323
+ if (useSendAndTransfer && ipc.sendAndTransfer) {
324
+ ipc.sendAndTransfer(message);
325
+ } else {
339
326
  ipc.send(message);
340
327
  }
341
328
  const responseMessage = await promise;
342
329
  return unwrapJsonRpcResult(responseMessage);
343
330
  };
344
- const invoke = (ipc, method, ...params) => {
345
- return invokeHelper(ipc, method, params);
331
+ const invoke$3 = (ipc, method, ...params) => {
332
+ return invokeHelper(ipc, method, params, false);
333
+ };
334
+ const invokeAndTransfer$1 = (ipc, method, ...params) => {
335
+ return invokeHelper(ipc, method, params, true);
336
+ };
337
+
338
+ const commands = Object.create(null);
339
+ const register$4 = commandMap => {
340
+ Object.assign(commands, commandMap);
341
+ };
342
+ const getCommand = key => {
343
+ return commands[key];
344
+ };
345
+ const execute = (command, ...args) => {
346
+ const fn = getCommand(command);
347
+ if (!fn) {
348
+ throw new Error(`command not found ${command}`);
349
+ }
350
+ return fn(...args);
346
351
  };
347
352
 
348
353
  const getData$1 = event => {
349
354
  return event.data;
350
355
  };
356
+ const attachEvents = that => {
357
+ const handleMessage = (...args) => {
358
+ const data = that.getData(...args);
359
+ that.dispatchEvent(new MessageEvent('message', {
360
+ data
361
+ }));
362
+ };
363
+ that.onMessage(handleMessage);
364
+ const handleClose = event => {
365
+ that.dispatchEvent(new Event('close'));
366
+ };
367
+ that.onClose(handleClose);
368
+ };
369
+ class Ipc extends EventTarget {
370
+ constructor(rawIpc) {
371
+ super();
372
+ this._rawIpc = rawIpc;
373
+ attachEvents(this);
374
+ }
375
+ }
376
+ const readyMessage = 'ready';
351
377
  const walkValue = (value, transferrables, isTransferrable) => {
352
378
  if (!value) {
353
379
  return;
@@ -398,35 +424,14 @@ const getTransferrables = value => {
398
424
  walkValue(value, transferrables, isTransferrable);
399
425
  return transferrables;
400
426
  };
401
- const attachEvents = that => {
402
- const handleMessage = (...args) => {
403
- const data = that.getData(...args);
404
- that.dispatchEvent(new MessageEvent('message', {
405
- data
406
- }));
407
- };
408
- that.onMessage(handleMessage);
409
- const handleClose = event => {
410
- that.dispatchEvent(new Event('close'));
411
- };
412
- that.onClose(handleClose);
413
- };
414
- class Ipc extends EventTarget {
415
- constructor(rawIpc) {
416
- super();
417
- this._rawIpc = rawIpc;
418
- attachEvents(this);
419
- }
420
- }
421
- const readyMessage = 'ready';
422
- const listen$4 = () => {
427
+ const listen$2 = () => {
423
428
  // @ts-ignore
424
429
  if (typeof WorkerGlobalScope === 'undefined') {
425
430
  throw new TypeError('module is not in web worker scope');
426
431
  }
427
432
  return globalThis;
428
433
  };
429
- const signal$3 = global => {
434
+ const signal$2 = global => {
430
435
  global.postMessage(readyMessage);
431
436
  };
432
437
  class IpcChildWithModuleWorker extends Ipc {
@@ -452,7 +457,7 @@ class IpcChildWithModuleWorker extends Ipc {
452
457
  this._rawIpc.addEventListener('message', callback);
453
458
  }
454
459
  }
455
- const wrap$6 = global => {
460
+ const wrap$5 = global => {
456
461
  return new IpcChildWithModuleWorker(global);
457
462
  };
458
463
  const E_INCOMPATIBLE_NATIVE_MODULE = 'E_INCOMPATIBLE_NATIVE_MODULE';
@@ -570,10 +575,10 @@ const getHelpfulChildProcessError = (stdout, stderr) => {
570
575
  };
571
576
  const normalizeLine$1 = line => {
572
577
  if (line.startsWith('Error: ')) {
573
- return line.slice(`Error: `.length);
578
+ return line.slice('Error: '.length);
574
579
  }
575
580
  if (line.startsWith('VError: ')) {
576
- return line.slice(`VError: `.length);
581
+ return line.slice('VError: '.length);
577
582
  }
578
583
  return line;
579
584
  };
@@ -671,10 +676,10 @@ const waitForFirstMessage = async port => {
671
676
  // @ts-ignore
672
677
  return event.data;
673
678
  };
674
- const listen$3 = async () => {
675
- const parentIpcRaw = listen$4();
676
- signal$3(parentIpcRaw);
677
- const parentIpc = wrap$6(parentIpcRaw);
679
+ const listen$1$1 = async () => {
680
+ const parentIpcRaw = listen$2();
681
+ signal$2(parentIpcRaw);
682
+ const parentIpc = wrap$5(parentIpcRaw);
678
683
  const firstMessage = await waitForFirstMessage(parentIpc);
679
684
  if (firstMessage.method !== 'initialize') {
680
685
  throw new IpcError('unexpected first message');
@@ -719,19 +724,22 @@ class IpcChildWithModuleWorkerAndMessagePort extends Ipc {
719
724
  this._rawIpc.start();
720
725
  }
721
726
  }
722
- const wrap$5 = port => {
727
+ const wrap$4 = port => {
723
728
  return new IpcChildWithModuleWorkerAndMessagePort(port);
724
729
  };
725
730
  const IpcChildWithModuleWorkerAndMessagePort$1 = {
726
731
  __proto__: null,
727
- listen: listen$3,
728
- wrap: wrap$5
732
+ listen: listen$1$1,
733
+ wrap: wrap$4
729
734
  };
730
735
 
731
736
  const createRpc = ipc => {
732
737
  const rpc = {
733
738
  invoke(method, ...params) {
734
- return invoke(ipc, method, ...params);
739
+ return invoke$3(ipc, method, ...params);
740
+ },
741
+ invokeAndTransfer(method, ...params) {
742
+ return invokeAndTransfer$1(ipc, method, ...params);
735
743
  }
736
744
  };
737
745
  return rpc;
@@ -759,11 +767,11 @@ const listen$1 = async () => {
759
767
  const ipc = module.wrap(rawIpc);
760
768
  return ipc;
761
769
  };
762
- const create = async ({
770
+ const create$1$1 = async ({
763
771
  commandMap
764
772
  }) => {
765
773
  // TODO create a commandMap per rpc instance
766
- register(commandMap);
774
+ register$4(commandMap);
767
775
  const ipc = await listen$1();
768
776
  handleIpc(ipc);
769
777
  const rpc = createRpc(ipc);
@@ -771,7 +779,11 @@ const create = async ({
771
779
  };
772
780
  const WebWorkerRpcClient = {
773
781
  __proto__: null,
774
- create
782
+ create: create$1$1
783
+ };
784
+
785
+ const invoke$2 = async (method, ...params) => {
786
+ // TODO
775
787
  };
776
788
 
777
789
  const createUrl = (protocol, host) => {
@@ -850,7 +862,7 @@ const getWebViewHtml = (baseUrl, locationOrigin, elements) => {
850
862
  }
851
863
  }
852
864
  const middleHtml = middle.join('\n ');
853
- let html = `<!DOCTYPE html>
865
+ const html = `<!DOCTYPE html>
854
866
  <html>
855
867
  <head>
856
868
  ${middleHtml}
@@ -1047,6 +1059,40 @@ const getIframeSrc = (webViews, webViewId, webViewPort, root, isGitpod, location
1047
1059
  }
1048
1060
  };
1049
1061
 
1062
+ const getPortTuple = () => {
1063
+ const {
1064
+ port1,
1065
+ port2
1066
+ } = new MessageChannel();
1067
+ return {
1068
+ port1,
1069
+ port2
1070
+ };
1071
+ };
1072
+
1073
+ const getJson = async () => {};
1074
+
1075
+ const getSavedState = async () => {
1076
+ const value = await getJson();
1077
+ return value;
1078
+ };
1079
+
1080
+ const getSavedWebViewState = async id => {
1081
+ const states = await getSavedState();
1082
+ if (!states) {
1083
+ return undefined;
1084
+ }
1085
+ if (!Array.isArray(states)) {
1086
+ return undefined;
1087
+ }
1088
+ for (const item of states) {
1089
+ if (item && item.key && item.key === id && item.value && item.value.state) {
1090
+ return item.value.state;
1091
+ }
1092
+ }
1093
+ return undefined;
1094
+ };
1095
+
1050
1096
  const SemiColon = ';';
1051
1097
  const Space = ' ';
1052
1098
 
@@ -1084,6 +1130,42 @@ const getWebViewOrigin = webViewPort => {
1084
1130
  return origin;
1085
1131
  };
1086
1132
 
1133
+ const invoke$1 = async (method, ...params) => {
1134
+ // TODO
1135
+ };
1136
+
1137
+ const getWebViewsNode = async () => {
1138
+ const webViews = await invoke$1();
1139
+ return webViews;
1140
+ };
1141
+
1142
+ const getWebViewsWeb = async () => {
1143
+ // const url = `${AssetDir.assetDir}/config/webViews.json`
1144
+ // TODO move this to shared-process-web / network-process-web
1145
+ // TODO handle error ?
1146
+ // TODO use parent rpc invoke
1147
+ // return Command.execute(/* Ajax.getJson */ 'Ajax.getJson', /* url */ url)
1148
+ return [];
1149
+ };
1150
+
1151
+ const getWebViewsDefault = async () => {
1152
+ switch (platform) {
1153
+ case Web:
1154
+ return getWebViewsWeb();
1155
+ case Test:
1156
+ return [];
1157
+ default:
1158
+ return getWebViewsNode();
1159
+ }
1160
+ };
1161
+ const getWebViews = async () => {
1162
+ const nodeWebViews = await getWebViewsDefault();
1163
+ // TODO ask renderer worker for webviews
1164
+ // const registeredWebViews = WebViews.get()
1165
+ const allWebViews = [...nodeWebViews];
1166
+ return allWebViews;
1167
+ };
1168
+
1087
1169
  const AllowScripts = 'allow-scripts';
1088
1170
  const AllowSameOrigin = 'allow-same-origin';
1089
1171
 
@@ -1098,6 +1180,13 @@ const getIframeSandbox = (webView, platform) => {
1098
1180
  return [AllowScripts, AllowSameOrigin, ...extensionSandbox];
1099
1181
  };
1100
1182
 
1183
+ const state = {
1184
+ id: 0
1185
+ };
1186
+ const create$1 = () => {
1187
+ return ++state.id;
1188
+ };
1189
+
1101
1190
  const getOrigin = () => {
1102
1191
  return location.origin;
1103
1192
  };
@@ -1108,7 +1197,154 @@ const getProtocol = () => {
1108
1197
  return location.protocol;
1109
1198
  };
1110
1199
 
1200
+ const invoke = async (method, ...params) => {
1201
+ // TODO
1202
+ };
1203
+ const invokeAndTransfer = async (method, ...params) => {
1204
+ // TODO
1205
+ };
1206
+
1207
+ const setPort = async (uid, port, origin, portType) => {
1208
+ await invokeAndTransfer('WebView.setPort', uid, port, origin, portType);
1209
+ };
1210
+
1211
+ const registerProtocol = async () => {
1212
+ await invoke$1();
1213
+ };
1214
+ const create = async previewServerId => {
1215
+ await invoke$1('WebViewServer.create', previewServerId);
1216
+ };
1217
+ const start = async (previewServerId, webViewPort) => {
1218
+ await invoke$1('WebViewServer.start', previewServerId, webViewPort);
1219
+ };
1220
+ const setHandler = async (previewServerId, frameAncestors, webViewRoot, contentSecurityPolicy, iframeContent) => {
1221
+ await invoke$1('WebViewServer.setHandler', previewServerId, frameAncestors, webViewRoot, contentSecurityPolicy, iframeContent);
1222
+ };
1223
+
1224
+ const register$3 = async previewServerId => {
1225
+ await registerProtocol();
1226
+ await create(previewServerId); // TODO move this up
1227
+ };
1228
+
1229
+ const WebViewProtocolElectron = {
1230
+ __proto__: null,
1231
+ register: register$3
1232
+ };
1233
+
1234
+ const register$2 = async (previewServerId, webViewPort, frameAncestors, webViewRoot, csp, iframeContent) => {
1235
+ // TODO apply something similar for electron
1236
+ // TODO pass webview root, so that only these resources can be accessed
1237
+ // TODO pass csp configuration to server
1238
+ // TODO pass coop / coep configuration to server
1239
+ await create(previewServerId); // TODO move this up
1240
+ await start(previewServerId, webViewPort); // TODO move this up
1241
+ await setHandler(previewServerId, frameAncestors, webViewRoot, csp, iframeContent);
1242
+ // TODO make this work in gitpod also
1243
+ };
1244
+
1245
+ const WebViewProtocolRemote = {
1246
+ __proto__: null,
1247
+ register: register$2
1248
+ };
1249
+
1250
+ const register$1 = async () => {
1251
+ // noop
1252
+ };
1253
+
1254
+ const WebViewProtocolWeb = {
1255
+ __proto__: null,
1256
+ register: register$1
1257
+ };
1258
+
1259
+ const getModule = platform => {
1260
+ switch (platform) {
1261
+ case Remote:
1262
+ return WebViewProtocolRemote;
1263
+ case Electron:
1264
+ return WebViewProtocolElectron;
1265
+ case Web:
1266
+ default:
1267
+ return WebViewProtocolWeb;
1268
+ }
1269
+ };
1270
+ const register = async (previewServerId, webViewPort, frameAncestors, webViewRoot, csp, iframeContent) => {
1271
+ const module = getModule(platform);
1272
+ return module.register(previewServerId, webViewPort, frameAncestors, webViewRoot, csp, iframeContent);
1273
+ };
1274
+
1275
+ const create2 = async ({
1276
+ id,
1277
+ webViewPort,
1278
+ webViewId,
1279
+ previewServerId,
1280
+ uri,
1281
+ platform: platform$1,
1282
+ isGitpod
1283
+ }) => {
1284
+ let root = '';
1285
+ if (platform === Remote) {
1286
+ root = await invoke$1();
1287
+ }
1288
+ const webViews = await getWebViews();
1289
+ const locationProtocol = getProtocol();
1290
+ const locationHost = getHost();
1291
+ const locationOrigin = getOrigin();
1292
+ const iframeResult = getIframeSrc(webViews, webViewId, webViewPort, root, isGitpod, locationProtocol, locationHost, locationOrigin);
1293
+ if (!iframeResult) {
1294
+ return undefined;
1295
+ }
1296
+ const webView = getWebView(webViews, webViewId);
1297
+
1298
+ // TODO move all of this to iframe worker
1299
+ const {
1300
+ iframeSrc,
1301
+ webViewRoot,
1302
+ srcDoc,
1303
+ iframeContent
1304
+ } = iframeResult;
1305
+ const frameAncestors = getWebViewFrameAncestors(locationProtocol, locationHost);
1306
+
1307
+ // TODO figure out order for events, e.g.
1308
+ // 1. activate extension, create webview and ports in parallel
1309
+ // 2. wait for webview to load (?)
1310
+ // 3. setup extension host worker rpc
1311
+ // 4. create webview in extension host worker and load content
1312
+
1313
+ const csp = getWebViewCsp(webView);
1314
+ const sandbox = getIframeSandbox(webView, platform$1);
1315
+ const iframeCsp = platform$1 === Web ? csp : '';
1316
+ const credentialless = true;
1317
+
1318
+ // TODO ask renderer worker to activate
1319
+ // await ExtensionHostManagement.activateByEvent(`onWebView:${webViewId}`)
1320
+ const {
1321
+ port1
1322
+ } = getPortTuple();
1323
+ const portId = create$1();
1324
+ await register(previewServerId, webViewPort, frameAncestors, webViewRoot, csp, iframeContent);
1325
+ await invoke('WebView.create', id, iframeSrc, sandbox, iframeCsp, credentialless);
1326
+ await invoke('WebView.load', id);
1327
+ const origin = getWebViewOrigin(webViewPort);
1328
+ const portType = '';
1329
+ await setPort(id, port1, origin, portType);
1330
+
1331
+ // TODO
1332
+ // await ExtensionHostWorker.invokeAndTransfer('ExtensionHostWebView.create', webViewId, port2, uri, id, origin)
1333
+
1334
+ const savedState = await getSavedWebViewState(webViewId);
1335
+ await invoke$2('ExtensionHostWebView.load', webViewId, savedState);
1336
+ return {
1337
+ srcDoc,
1338
+ iframeSrc,
1339
+ sandbox,
1340
+ portId,
1341
+ origin,
1342
+ csp: iframeCsp
1343
+ };
1344
+ };
1345
+
1111
1346
  const commandMap = {
1347
+ // deprecated
1112
1348
  'Location.getHost': getHost,
1113
1349
  'Location.getOrigin': getOrigin,
1114
1350
  'Location.getProtocol': getProtocol,
@@ -1118,7 +1354,9 @@ const commandMap = {
1118
1354
  'WebView.getIframeSrc': getIframeSrc,
1119
1355
  'WebView.getOrigin': getWebViewOrigin,
1120
1356
  'WebView.getSandbox': getIframeSandbox,
1121
- 'WebView.getWebViewCsp': getWebViewCsp
1357
+ 'WebView.getWebViewCsp': getWebViewCsp,
1358
+ // new
1359
+ 'WebView.create2': create2
1122
1360
  };
1123
1361
 
1124
1362
  const listen = async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/iframe-worker",
3
- "version": "2.0.0",
3
+ "version": "2.2.0",
4
4
  "description": "Web Worker to manage creation and lifecycle of iframes in Lvce Editor",
5
5
  "main": "dist/iframeWorkerMain.js",
6
6
  "type": "module",