@lvce-editor/iframe-worker 2.1.0 → 2.3.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,15 +1,15 @@
1
1
  const Two = '2.0';
2
- const state = {
2
+ const state$2 = {
3
3
  callbacks: Object.create(null)
4
4
  };
5
5
  const set = (id, fn) => {
6
- state.callbacks[id] = fn;
6
+ state$2.callbacks[id] = fn;
7
7
  };
8
8
  const get = id => {
9
- return state.callbacks[id];
9
+ return state$2.callbacks[id];
10
10
  };
11
11
  const remove = id => {
12
- delete state.callbacks[id];
12
+ delete state$2.callbacks[id];
13
13
  };
14
14
  let id = 0;
15
15
  const create$3 = () => {
@@ -217,7 +217,7 @@ const getErrorProperty = (error, prettyError) => {
217
217
  }
218
218
  };
219
219
  };
220
- const create$1$1 = (message, error) => {
220
+ const create$1$2 = (message, error) => {
221
221
  return {
222
222
  jsonrpc: Two,
223
223
  id: message.id,
@@ -228,9 +228,9 @@ const getErrorResponse = (message, error, preparePrettyError, logError) => {
228
228
  const prettyError = preparePrettyError(error);
229
229
  logError(error, prettyError);
230
230
  const errorProperty = getErrorProperty(error, prettyError);
231
- return create$1$1(message, errorProperty);
231
+ return create$1$2(message, errorProperty);
232
232
  };
233
- const create = (message, result) => {
233
+ const create$4 = (message, result) => {
234
234
  return {
235
235
  jsonrpc: Two,
236
236
  id: message.id,
@@ -239,7 +239,7 @@ const create = (message, result) => {
239
239
  };
240
240
  const getSuccessResponse = (message, result) => {
241
241
  const resultProperty = result ?? null;
242
- return create(message, resultProperty);
242
+ return create$4(message, resultProperty);
243
243
  };
244
244
  const getResponse = async (message, ipc, execute, preparePrettyError, logError, requiresSocket) => {
245
245
  try {
@@ -328,15 +328,15 @@ const invokeHelper = async (ipc, method, params, useSendAndTransfer) => {
328
328
  const responseMessage = await promise;
329
329
  return unwrapJsonRpcResult(responseMessage);
330
330
  };
331
- const invoke = (ipc, method, ...params) => {
331
+ const invoke$4 = (ipc, method, ...params) => {
332
332
  return invokeHelper(ipc, method, params, false);
333
333
  };
334
- const invokeAndTransfer = (ipc, method, ...params) => {
334
+ const invokeAndTransfer$3 = (ipc, method, ...params) => {
335
335
  return invokeHelper(ipc, method, params, true);
336
336
  };
337
337
 
338
338
  const commands = Object.create(null);
339
- const register = commandMap => {
339
+ const register$4 = commandMap => {
340
340
  Object.assign(commands, commandMap);
341
341
  };
342
342
  const getCommand = key => {
@@ -736,10 +736,10 @@ const IpcChildWithModuleWorkerAndMessagePort$1 = {
736
736
  const createRpc = ipc => {
737
737
  const rpc = {
738
738
  invoke(method, ...params) {
739
- return invoke(ipc, method, ...params);
739
+ return invoke$4(ipc, method, ...params);
740
740
  },
741
741
  invokeAndTransfer(method, ...params) {
742
- return invokeAndTransfer(ipc, method, ...params);
742
+ return invokeAndTransfer$3(ipc, method, ...params);
743
743
  }
744
744
  };
745
745
  return rpc;
@@ -767,11 +767,11 @@ const listen$1 = async () => {
767
767
  const ipc = module.wrap(rawIpc);
768
768
  return ipc;
769
769
  };
770
- const create$1 = async ({
770
+ const create$1$1 = async ({
771
771
  commandMap
772
772
  }) => {
773
773
  // TODO create a commandMap per rpc instance
774
- register(commandMap);
774
+ register$4(commandMap);
775
775
  const ipc = await listen$1();
776
776
  handleIpc(ipc);
777
777
  const rpc = createRpc(ipc);
@@ -779,7 +779,31 @@ const create$1 = async ({
779
779
  };
780
780
  const WebWorkerRpcClient = {
781
781
  __proto__: null,
782
- create: create$1
782
+ create: create$1$1
783
+ };
784
+
785
+ const state$1 = {
786
+ rpc: undefined
787
+ };
788
+ const invoke$3 = (method, ...params) => {
789
+ const rpc = state$1.rpc;
790
+ // @ts-ignore
791
+ return rpc.invoke(method, ...params);
792
+ };
793
+ const invokeAndTransfer$2 = (method, ...params) => {
794
+ const rpc = state$1.rpc;
795
+ // @ts-ignore
796
+ return rpc.invokeAndTransfer(method, ...params);
797
+ };
798
+ const setRpc = rpc => {
799
+ state$1.rpc = rpc;
800
+ };
801
+
802
+ const invoke$2 = async (method, ...params) => {
803
+ return invoke$3('WebView.compatExtensionHostWorkerInvoke', method, ...params);
804
+ };
805
+ const invokeAndTransfer$1 = async (method, ...params) => {
806
+ return invokeAndTransfer$2('WebView.compatExtensionHostWorkerInvokeAndTransfer', method, ...params);
783
807
  };
784
808
 
785
809
  const createUrl = (protocol, host) => {
@@ -896,19 +920,23 @@ const getWebViewUri = (webViews, webViewId) => {
896
920
  }
897
921
  return webViewPath;
898
922
  };
899
- const getIframeSrcRemote = (webViews, webViewPort, webViewId, locationProtocol, locationHost, isGitpod, root) => {
923
+ const getIframeSrcRemote = (webViews, webViewPort, webViewId, locationProtocol, locationHost, isGitpod, root, platform$1 = platform) => {
900
924
  const webView = getWebView$1(webViews, webViewId);
901
925
  const webViewUri = getWebViewUri(webViews, webViewId);
926
+ console.log({
927
+ webViews,
928
+ webViewId
929
+ });
902
930
  if (!webViewUri) {
903
931
  return undefined;
904
932
  }
905
933
  let iframeSrc = webViewUri;
906
934
  let webViewRoot = webViewUri;
907
- if (platform === Electron) {
935
+ if (platform$1 === Electron) {
908
936
  const relativePath = new URL(webViewUri).pathname.replace('/index.html', '');
909
937
  iframeSrc = `${WebView}://-${relativePath}/`;
910
938
  // TODO
911
- } else if (platform === Remote) {
939
+ } else if (platform$1 === Remote) {
912
940
  const relativePath = new URL(webViewUri).pathname.replace('/index.html', '');
913
941
  if (webViewUri.startsWith('file://')) {
914
942
  // ignore
@@ -1043,10 +1071,10 @@ class VError extends Error {
1043
1071
  }
1044
1072
  }
1045
1073
 
1046
- const getIframeSrc = (webViews, webViewId, webViewPort, root, isGitpod, locationProtocol, locationHost, locationOrigin) => {
1074
+ const getIframeSrc = (webViews, webViewId, webViewPort, root, isGitpod, locationProtocol, locationHost, locationOrigin, platform$1 = platform) => {
1047
1075
  try {
1048
1076
  const webView = getWebView(webViews, webViewId);
1049
- if (platform === Web) {
1077
+ if (platform$1 === Web) {
1050
1078
  return getIframeSrc$1(webView, locationOrigin);
1051
1079
  }
1052
1080
  return getIframeSrcRemote(webViews, webViewPort, webViewId, locationProtocol, locationHost, isGitpod, root);
@@ -1055,6 +1083,40 @@ const getIframeSrc = (webViews, webViewId, webViewPort, root, isGitpod, location
1055
1083
  }
1056
1084
  };
1057
1085
 
1086
+ const getPortTuple = () => {
1087
+ const {
1088
+ port1,
1089
+ port2
1090
+ } = new MessageChannel();
1091
+ return {
1092
+ port1,
1093
+ port2
1094
+ };
1095
+ };
1096
+
1097
+ const getJson = async () => {};
1098
+
1099
+ const getSavedState = async () => {
1100
+ const value = await getJson();
1101
+ return value;
1102
+ };
1103
+
1104
+ const getSavedWebViewState = async id => {
1105
+ const states = await getSavedState();
1106
+ if (!states) {
1107
+ return undefined;
1108
+ }
1109
+ if (!Array.isArray(states)) {
1110
+ return undefined;
1111
+ }
1112
+ for (const item of states) {
1113
+ if (item && item.key && item.key === id && item.value && item.value.state) {
1114
+ return item.value.state;
1115
+ }
1116
+ }
1117
+ return undefined;
1118
+ };
1119
+
1058
1120
  const SemiColon = ';';
1059
1121
  const Space = ' ';
1060
1122
 
@@ -1092,6 +1154,10 @@ const getWebViewOrigin = webViewPort => {
1092
1154
  return origin;
1093
1155
  };
1094
1156
 
1157
+ const getWebViews = async () => {
1158
+ return invoke$3('WebView.getWebViews');
1159
+ };
1160
+
1095
1161
  const AllowScripts = 'allow-scripts';
1096
1162
  const AllowSameOrigin = 'allow-same-origin';
1097
1163
 
@@ -1106,6 +1172,13 @@ const getIframeSandbox = (webView, platform) => {
1106
1172
  return [AllowScripts, AllowSameOrigin, ...extensionSandbox];
1107
1173
  };
1108
1174
 
1175
+ const state = {
1176
+ id: 0
1177
+ };
1178
+ const create$1 = () => {
1179
+ return ++state.id;
1180
+ };
1181
+
1109
1182
  const getOrigin = () => {
1110
1183
  return location.origin;
1111
1184
  };
@@ -1116,7 +1189,154 @@ const getProtocol = () => {
1116
1189
  return location.protocol;
1117
1190
  };
1118
1191
 
1192
+ const invoke$1 = async (method, ...params) => {
1193
+ return invoke$3('WebView.compatRendererProcessInvoke', method, ...params);
1194
+ };
1195
+ const invokeAndTransfer = async (method, ...params) => {
1196
+ return invokeAndTransfer$2('WebView.compatRendererProcessInvokeAndTransfer', method, ...params);
1197
+ };
1198
+
1199
+ const setPort = async (uid, port, origin, portType) => {
1200
+ await invokeAndTransfer('WebView.setPort', uid, port, origin, portType);
1201
+ };
1202
+
1203
+ const invoke = async (method, ...params) => {
1204
+ return invoke$3('WebView.compatSharedProcessInvoke', method, ...params);
1205
+ };
1206
+
1207
+ const registerProtocol = async () => {
1208
+ await invoke('WebViewServer.registerProtocol');
1209
+ };
1210
+ const create = async previewServerId => {
1211
+ await invoke('WebViewServer.create', previewServerId);
1212
+ };
1213
+ const start = async (previewServerId, webViewPort) => {
1214
+ await invoke('WebViewServer.start', previewServerId, webViewPort);
1215
+ };
1216
+ const setHandler = async (previewServerId, frameAncestors, webViewRoot, contentSecurityPolicy, iframeContent) => {
1217
+ await invoke('WebViewServer.setHandler', previewServerId, frameAncestors, webViewRoot, contentSecurityPolicy, iframeContent);
1218
+ };
1219
+
1220
+ const register$3 = async previewServerId => {
1221
+ await registerProtocol();
1222
+ await create(previewServerId); // TODO move this up
1223
+ };
1224
+
1225
+ const WebViewProtocolElectron = {
1226
+ __proto__: null,
1227
+ register: register$3
1228
+ };
1229
+
1230
+ const register$2 = async (previewServerId, webViewPort, frameAncestors, webViewRoot, csp, iframeContent) => {
1231
+ // TODO apply something similar for electron
1232
+ // TODO pass webview root, so that only these resources can be accessed
1233
+ // TODO pass csp configuration to server
1234
+ // TODO pass coop / coep configuration to server
1235
+ await create(previewServerId); // TODO move this up
1236
+ await start(previewServerId, webViewPort); // TODO move this up
1237
+ await setHandler(previewServerId, frameAncestors, webViewRoot, csp, iframeContent);
1238
+ // TODO make this work in gitpod also
1239
+ };
1240
+
1241
+ const WebViewProtocolRemote = {
1242
+ __proto__: null,
1243
+ register: register$2
1244
+ };
1245
+
1246
+ const register$1 = async () => {
1247
+ // noop
1248
+ };
1249
+
1250
+ const WebViewProtocolWeb = {
1251
+ __proto__: null,
1252
+ register: register$1
1253
+ };
1254
+
1255
+ const getModule = platform => {
1256
+ switch (platform) {
1257
+ case Remote:
1258
+ return WebViewProtocolRemote;
1259
+ case Electron:
1260
+ return WebViewProtocolElectron;
1261
+ case Web:
1262
+ default:
1263
+ return WebViewProtocolWeb;
1264
+ }
1265
+ };
1266
+ const register = async (previewServerId, webViewPort, frameAncestors, webViewRoot, csp, iframeContent) => {
1267
+ const module = getModule(platform);
1268
+ return module.register(previewServerId, webViewPort, frameAncestors, webViewRoot, csp, iframeContent);
1269
+ };
1270
+
1271
+ const create2 = async ({
1272
+ id,
1273
+ webViewPort,
1274
+ webViewId,
1275
+ previewServerId,
1276
+ uri,
1277
+ platform,
1278
+ isGitpod
1279
+ }) => {
1280
+ let root = '';
1281
+ if (platform === Remote) {
1282
+ root = await invoke('Platform.getRoot');
1283
+ }
1284
+ const webViews = await getWebViews();
1285
+ const locationProtocol = getProtocol();
1286
+ const locationHost = getHost();
1287
+ const locationOrigin = getOrigin();
1288
+ const iframeResult = getIframeSrc(webViews, webViewId, webViewPort, root, isGitpod, locationProtocol, locationHost, locationOrigin, platform);
1289
+ if (!iframeResult) {
1290
+ return undefined;
1291
+ }
1292
+ const webView = getWebView(webViews, webViewId);
1293
+
1294
+ // TODO move all of this to iframe worker
1295
+ const {
1296
+ iframeSrc,
1297
+ webViewRoot,
1298
+ srcDoc,
1299
+ iframeContent
1300
+ } = iframeResult;
1301
+ const frameAncestors = getWebViewFrameAncestors(locationProtocol, locationHost);
1302
+
1303
+ // TODO figure out order for events, e.g.
1304
+ // 1. activate extension, create webview and ports in parallel
1305
+ // 2. wait for webview to load (?)
1306
+ // 3. setup extension host worker rpc
1307
+ // 4. create webview in extension host worker and load content
1308
+
1309
+ const csp = getWebViewCsp(webView);
1310
+ const sandbox = getIframeSandbox(webView, platform);
1311
+ const iframeCsp = platform === Web ? csp : '';
1312
+ const credentialless = true;
1313
+ await invoke$3('ExtensionHostManagement.activateByEvent', `onWebView:${webViewId}`);
1314
+ const {
1315
+ port1,
1316
+ port2
1317
+ } = getPortTuple();
1318
+ const portId = create$1();
1319
+ await register(previewServerId, webViewPort, frameAncestors, webViewRoot, csp, iframeContent);
1320
+ await invoke$1('WebView.create', id, iframeSrc, sandbox, iframeCsp, credentialless);
1321
+ await invoke$1('WebView.load', id);
1322
+ const origin = getWebViewOrigin(webViewPort);
1323
+ const portType = '';
1324
+ await setPort(id, port1, origin, portType);
1325
+ await invokeAndTransfer$1('ExtensionHostWebView.create', webViewId, port2, uri, id, origin);
1326
+ const savedState = await getSavedWebViewState(webViewId);
1327
+ await invoke$2('ExtensionHostWebView.load', webViewId, savedState);
1328
+ return {
1329
+ srcDoc,
1330
+ iframeSrc,
1331
+ sandbox,
1332
+ portId,
1333
+ origin,
1334
+ csp: iframeCsp
1335
+ };
1336
+ };
1337
+
1119
1338
  const commandMap = {
1339
+ // deprecated
1120
1340
  'Location.getHost': getHost,
1121
1341
  'Location.getOrigin': getOrigin,
1122
1342
  'Location.getProtocol': getProtocol,
@@ -1126,13 +1346,16 @@ const commandMap = {
1126
1346
  'WebView.getIframeSrc': getIframeSrc,
1127
1347
  'WebView.getOrigin': getWebViewOrigin,
1128
1348
  'WebView.getSandbox': getIframeSandbox,
1129
- 'WebView.getWebViewCsp': getWebViewCsp
1349
+ 'WebView.getWebViewCsp': getWebViewCsp,
1350
+ // new
1351
+ 'WebView.create2': create2
1130
1352
  };
1131
1353
 
1132
1354
  const listen = async () => {
1133
- await WebWorkerRpcClient.create({
1355
+ const rpc = await WebWorkerRpcClient.create({
1134
1356
  commandMap: commandMap
1135
1357
  });
1358
+ setRpc(rpc);
1136
1359
  };
1137
1360
 
1138
1361
  const main = async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/iframe-worker",
3
- "version": "2.1.0",
3
+ "version": "2.3.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",