@lvce-editor/extension-management-worker 3.3.0 → 4.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.
@@ -608,7 +608,7 @@ const execute = (command, ...args) => {
608
608
 
609
609
  const Two$1 = '2.0';
610
610
  const callbacks = Object.create(null);
611
- const get$6 = id => {
611
+ const get$5 = id => {
612
612
  return callbacks[id];
613
613
  };
614
614
  const remove$1 = id => {
@@ -757,7 +757,7 @@ const warn$1 = (...args) => {
757
757
  console.warn(...args);
758
758
  };
759
759
  const resolve = (id, response) => {
760
- const fn = get$6(id);
760
+ const fn = get$5(id);
761
761
  if (!fn) {
762
762
  console.log(response);
763
763
  warn$1(`callback ${id} may already be disposed`);
@@ -941,9 +941,9 @@ const create$a = (id, method, params) => {
941
941
  return message;
942
942
  };
943
943
 
944
- let id$1 = 0;
944
+ let id = 0;
945
945
  const create$9 = () => {
946
- return ++id$1;
946
+ return ++id;
947
947
  };
948
948
 
949
949
  const registerPromise = map => {
@@ -1215,33 +1215,33 @@ const createMockRpc = ({
1215
1215
  return mockRpc;
1216
1216
  };
1217
1217
 
1218
- const rpcs$1 = Object.create(null);
1218
+ const rpcs$2 = Object.create(null);
1219
1219
  const set$7 = (id, rpc) => {
1220
- rpcs$1[id] = rpc;
1220
+ rpcs$2[id] = rpc;
1221
1221
  };
1222
- const get$5 = id => {
1223
- return rpcs$1[id];
1222
+ const get$4 = id => {
1223
+ return rpcs$2[id];
1224
1224
  };
1225
1225
  const remove = id => {
1226
- delete rpcs$1[id];
1226
+ delete rpcs$2[id];
1227
1227
  };
1228
1228
 
1229
1229
  /* eslint-disable @typescript-eslint/explicit-function-return-type */
1230
1230
  const create$1 = rpcId => {
1231
1231
  return {
1232
1232
  async dispose() {
1233
- const rpc = get$5(rpcId);
1233
+ const rpc = get$4(rpcId);
1234
1234
  await rpc.dispose();
1235
1235
  },
1236
1236
  // @ts-ignore
1237
1237
  invoke(method, ...params) {
1238
- const rpc = get$5(rpcId);
1238
+ const rpc = get$4(rpcId);
1239
1239
  // @ts-ignore
1240
1240
  return rpc.invoke(method, ...params);
1241
1241
  },
1242
1242
  // @ts-ignore
1243
1243
  invokeAndTransfer(method, ...params) {
1244
- const rpc = get$5(rpcId);
1244
+ const rpc = get$4(rpcId);
1245
1245
  // @ts-ignore
1246
1246
  return rpc.invokeAndTransfer(method, ...params);
1247
1247
  },
@@ -1274,9 +1274,41 @@ const RendererWorker = 1;
1274
1274
  const SharedProcess = 1492;
1275
1275
 
1276
1276
  const {
1277
+ dispose,
1277
1278
  invoke: invoke$4,
1279
+ invokeAndTransfer: invokeAndTransfer$2,
1280
+ registerMockRpc,
1278
1281
  set: set$6
1279
1282
  } = create$1(ExtensionHostWorker);
1283
+ const executeReferenceProvider = async (id, offset) => {
1284
+ // @ts-ignore
1285
+ return invoke$4('ExtensionHostReference.executeReferenceProvider', id, offset);
1286
+ };
1287
+ const executeFileReferenceProvider = async id => {
1288
+ // @ts-ignore
1289
+ return invoke$4('ExtensionHostReference.executeFileReferenceProvider', id);
1290
+ };
1291
+ const getRuntimeStatus$2 = async extensionId => {
1292
+ // @ts-ignore
1293
+ return invoke$4('ExtensionHost.getRuntimeStatus', extensionId);
1294
+ };
1295
+ const getEnabledOutputProviderIds = async () => {
1296
+ const channels = await invoke$4('Output.getEnabledProviders');
1297
+ return channels;
1298
+ };
1299
+
1300
+ const ExtensionHost = {
1301
+ __proto__: null,
1302
+ dispose,
1303
+ executeFileReferenceProvider,
1304
+ executeReferenceProvider,
1305
+ getEnabledOutputProviderIds,
1306
+ getRuntimeStatus: getRuntimeStatus$2,
1307
+ invoke: invoke$4,
1308
+ invokeAndTransfer: invokeAndTransfer$2,
1309
+ registerMockRpc,
1310
+ set: set$6
1311
+ };
1280
1312
 
1281
1313
  const {
1282
1314
  invoke: invoke$3,
@@ -1331,6 +1363,91 @@ const isCanceled = token => {
1331
1363
  return token.finished;
1332
1364
  };
1333
1365
 
1366
+ const None = 0;
1367
+ const Importing = 1;
1368
+ const Activating = 2;
1369
+ const Activated = 3;
1370
+ const Error$1 = 4;
1371
+
1372
+ /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
1373
+
1374
+ const createInitialState = () => {
1375
+ return {
1376
+ activatedExtensions: Object.create(null),
1377
+ cachedActivationEvents: Object.create(null),
1378
+ cachedExtensions: undefined,
1379
+ disabledIds: [],
1380
+ platform: 0,
1381
+ runtimeStatuses: Object.create(null),
1382
+ webExtensions: []
1383
+ };
1384
+ };
1385
+ const createEmptyRuntimeStatus = id => {
1386
+ return {
1387
+ activationEndTime: 0,
1388
+ activationEvent: '',
1389
+ activationStartTime: 0,
1390
+ activationTime: 0,
1391
+ id,
1392
+ importEndTime: 0,
1393
+ importStartTime: 0,
1394
+ importTime: 0,
1395
+ status: None
1396
+ };
1397
+ };
1398
+ let extensionsState = createInitialState();
1399
+ const get$3 = () => {
1400
+ return extensionsState;
1401
+ };
1402
+ const set$2 = newState => {
1403
+ extensionsState = newState;
1404
+ };
1405
+ const update = newState => {
1406
+ set$2({
1407
+ ...extensionsState,
1408
+ ...newState
1409
+ });
1410
+ };
1411
+ const setPlatform = platform => {
1412
+ update({
1413
+ platform
1414
+ });
1415
+ };
1416
+ const hasWebExtensionUri = uri => {
1417
+ return extensionsState.webExtensions.some(extension => extension.uri === uri);
1418
+ };
1419
+ const addWebExtension$1 = extension => {
1420
+ update({
1421
+ webExtensions: [...extensionsState.webExtensions, extension]
1422
+ });
1423
+ };
1424
+ const clearCachedExtensions = () => {
1425
+ update({
1426
+ cachedExtensions: undefined
1427
+ });
1428
+ };
1429
+ const setRuntimeStatus = status => {
1430
+ update({
1431
+ runtimeStatuses: {
1432
+ ...extensionsState.runtimeStatuses,
1433
+ [status.id]: {
1434
+ ...status
1435
+ }
1436
+ }
1437
+ });
1438
+ };
1439
+ const updateRuntimeStatus = (id, statusUpdate) => {
1440
+ const previousStatus = extensionsState.runtimeStatuses[id] || createEmptyRuntimeStatus(id);
1441
+ setRuntimeStatus({
1442
+ ...previousStatus,
1443
+ ...statusUpdate,
1444
+ id
1445
+ });
1446
+ };
1447
+ const getRuntimeStatus$1 = extensionId => {
1448
+ return extensionsState.runtimeStatuses[extensionId];
1449
+ };
1450
+
1334
1451
  const baseName = path => {
1335
1452
  const slashIndex = path.lastIndexOf('/');
1336
1453
  return path.slice(slashIndex + 1);
@@ -1361,26 +1478,6 @@ const isImportError = error => {
1361
1478
  return isImportErrorChrome(error) || isImportErrorFirefox(error) || isSyntaxError(error);
1362
1479
  };
1363
1480
 
1364
- const states = Object.create(null);
1365
- const set$2 = status => {
1366
- states[status.id] = status;
1367
- };
1368
- const get$4 = extensionId => {
1369
- return states[extensionId];
1370
- };
1371
- const update$1 = (id, update) => {
1372
- states[id] = {
1373
- ...states[id],
1374
- ...update
1375
- };
1376
- };
1377
-
1378
- const None = 0;
1379
- const Importing = 1;
1380
- const Activating = 2;
1381
- const Activated = 3;
1382
- const Error$1 = 4;
1383
-
1384
1481
  const sleep = duration => {
1385
1482
  const {
1386
1483
  promise,
@@ -1420,24 +1517,24 @@ const rejectAfterTimeout = async (timeout, token) => {
1420
1517
  }
1421
1518
  throw new Error(`Activation timeout of ${timeout}ms exceeded`);
1422
1519
  };
1423
- const activate = async (extensionId, extension) => {
1424
- await invoke$4('ExtensionHost.activateExtension3', extensionId, extension);
1520
+ const activate = async (extensionId, extension, extensionHost) => {
1521
+ await extensionHost.invoke('ExtensionHost.activateExtension3', extensionId, extension);
1425
1522
  };
1426
- const activateExtension2 = async (extensionId, extension, absolutePath) => {
1523
+ const activateExtension2 = async (extensionId, extension, absolutePath, extensionHost = ExtensionHost) => {
1427
1524
  string(extensionId);
1428
1525
  object(extension);
1429
1526
  string(absolutePath);
1430
1527
  const token = create();
1431
1528
  try {
1432
1529
  const startTime = performance.now();
1433
- update$1(extensionId, {
1530
+ updateRuntimeStatus(extensionId, {
1434
1531
  activationStartTime: startTime,
1435
1532
  status: Activating
1436
1533
  });
1437
- await Promise.race([activate(extensionId, extension), rejectAfterTimeout(activationTimeout, token)]);
1534
+ await Promise.race([activate(extensionId, extension, extensionHost), rejectAfterTimeout(activationTimeout, token)]);
1438
1535
  const endTime = performance.now();
1439
1536
  const time = endTime - startTime;
1440
- update$1(extensionId, {
1537
+ updateRuntimeStatus(extensionId, {
1441
1538
  activationEndTime: endTime,
1442
1539
  activationTime: time,
1443
1540
  status: Activated
@@ -1450,7 +1547,7 @@ const activateExtension2 = async (extensionId, extension, absolutePath) => {
1450
1547
  cause: error
1451
1548
  });
1452
1549
  }
1453
- update$1(extensionId, {
1550
+ updateRuntimeStatus(extensionId, {
1454
1551
  status: Error$1 // TODO maybe store error also in runtime status state
1455
1552
  });
1456
1553
  throw new VError(error, `Failed to activate extension ${id}`);
@@ -1459,11 +1556,61 @@ const activateExtension2 = async (extensionId, extension, absolutePath) => {
1459
1556
  }
1460
1557
  };
1461
1558
 
1559
+ const commandMapRef = {};
1560
+
1561
+ const getPortTuple = () => {
1562
+ const {
1563
+ port1,
1564
+ port2
1565
+ } = new MessageChannel();
1566
+ return {
1567
+ port1,
1568
+ port2
1569
+ };
1570
+ };
1571
+
1572
+ const rpcs$1 = Object.create(null);
1573
+ const get$2 = extensionId => {
1574
+ return rpcs$1[extensionId];
1575
+ };
1576
+ const set$1 = (extensionId, rpc) => {
1577
+ rpcs$1[extensionId] = rpc;
1578
+ };
1579
+
1580
+ /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
1581
+ const invoke = (method, ...params) => {
1582
+ return invoke$2(method, ...params);
1583
+ };
1584
+ const invokeAndTransfer = (method, ...params) => {
1585
+ return invokeAndTransfer$1(method, ...params);
1586
+ };
1587
+
1588
+ const getOrCreateIsolatedExtensionHostWorker = async extensionId => {
1589
+ const existingRpc = get$2(extensionId);
1590
+ if (existingRpc) {
1591
+ return existingRpc;
1592
+ }
1593
+ const {
1594
+ port1,
1595
+ port2
1596
+ } = getPortTuple();
1597
+ const rpcPromise = create$3({
1598
+ commandMap: commandMapRef,
1599
+ isMessagePortOpen: true,
1600
+ messagePort: port2
1601
+ });
1602
+ port2.start();
1603
+ await invokeAndTransfer('LaunchIsolatedExtensionHostWorker.launchIsolatedExtensionHostWorker', port1, extensionId);
1604
+ const rpc = await rpcPromise;
1605
+ set$1(extensionId, rpc);
1606
+ return rpc;
1607
+ };
1608
+
1462
1609
  const rpcs = Object.create(null);
1463
1610
  const add = (id, rpc) => {
1464
1611
  rpcs[id] = rpc;
1465
1612
  };
1466
- const get$3 = id => {
1613
+ const get$1 = id => {
1467
1614
  return rpcs[id];
1468
1615
  };
1469
1616
 
@@ -1502,11 +1649,11 @@ const handleRpcInfos = (extension, platform) => {
1502
1649
  }
1503
1650
  };
1504
1651
 
1505
- const importExtension = async (extensionId, absolutePath, activationEvent) => {
1652
+ const importExtension = async (extensionId, absolutePath, activationEvent, extensionHost = ExtensionHost) => {
1506
1653
  try {
1507
1654
  string(absolutePath);
1508
1655
  const startTime = performance.now();
1509
- set$2({
1656
+ setRuntimeStatus({
1510
1657
  activationEndTime: 0,
1511
1658
  activationEvent: activationEvent,
1512
1659
  activationStartTime: performance.now(),
@@ -1518,15 +1665,15 @@ const importExtension = async (extensionId, absolutePath, activationEvent) => {
1518
1665
  status: Importing
1519
1666
  });
1520
1667
  try {
1521
- await invoke$4('ExtensionHost.importExtension2', extensionId, absolutePath);
1668
+ await extensionHost.invoke('ExtensionHost.importExtension2', extensionId, absolutePath);
1522
1669
  const endTime = performance.now();
1523
1670
  const time = endTime - startTime;
1524
- update$1(extensionId, {
1671
+ updateRuntimeStatus(extensionId, {
1525
1672
  importEndTime: endTime,
1526
1673
  importTime: time
1527
1674
  });
1528
1675
  } catch (error) {
1529
- update$1(extensionId, {
1676
+ updateRuntimeStatus(extensionId, {
1530
1677
  status: Error$1 // TODO maybe store error also in runtime status state
1531
1678
  });
1532
1679
  if (isImportError(error)) {
@@ -1550,32 +1697,16 @@ const interExtensionId = path => {
1550
1697
  return path.slice(slashIndex + 1);
1551
1698
  };
1552
1699
 
1700
+ const isExtensionIsolated = extension => {
1701
+ return extension?.isolated === true;
1702
+ };
1703
+
1553
1704
  const activateExtension3 = async (extension, absolutePath, activationEvent, platform) => {
1554
1705
  handleRpcInfos(extension, platform);
1555
1706
  const extensionId = extension.id || interExtensionId(extension.uri);
1556
- await importExtension(extensionId, absolutePath, activationEvent);
1557
- await activateExtension2(extensionId, extension, absolutePath);
1558
- };
1559
-
1560
- const state$1 = {
1561
- webExtensions: []
1562
- };
1563
- const push = extension => {
1564
- state$1.webExtensions.push(extension);
1565
- };
1566
- const hasUri = uri => {
1567
- return state$1.webExtensions.some(extension => extension.uri === uri);
1568
- };
1569
- const get$2 = () => {
1570
- return state$1.webExtensions;
1571
- };
1572
-
1573
- // TODO store one kind of ui state for whole extension management worker
1574
-
1575
- const cache = Object.create(null);
1576
- const id = 1;
1577
- const clear = () => {
1578
- delete cache[id];
1707
+ const extensionHost = isExtensionIsolated(extension) ? await getOrCreateIsolatedExtensionHostWorker(extensionId) : undefined;
1708
+ await importExtension(extensionId, absolutePath, activationEvent, extensionHost);
1709
+ await activateExtension2(extensionId, extension, absolutePath, extensionHost);
1579
1710
  };
1580
1711
 
1581
1712
  const getJson$1 = async url => {
@@ -1611,27 +1742,18 @@ const getWebManifestPath = path => {
1611
1742
  };
1612
1743
 
1613
1744
  const addWebExtension = async path => {
1614
- if (hasUri(path)) {
1745
+ if (hasWebExtensionUri(path)) {
1615
1746
  return undefined;
1616
1747
  }
1617
1748
  const manifestPath = getWebManifestPath(path);
1618
1749
  const manifest = await getWebExtensionManifest(path, manifestPath);
1619
- // TODO avoid mutation if possible
1620
- push(manifest);
1621
- clear();
1750
+ addWebExtension$1(manifest);
1751
+ clearCachedExtensions();
1622
1752
  return manifest;
1623
1753
  };
1624
1754
 
1625
1755
  const ModuleWorkerAndWorkaroundForChromeDevtoolsBug = 6;
1626
1756
 
1627
- /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
1628
- const invoke = (method, ...params) => {
1629
- return invoke$2(method, ...params);
1630
- };
1631
- const invokeAndTransfer = (method, ...params) => {
1632
- return invokeAndTransfer$1(method, ...params);
1633
- };
1634
-
1635
1757
  const createWebViewWorkerRpc2 = async (rpcInfo, port) => {
1636
1758
  // TODO this function is called from the iframe worker to create a direct
1637
1759
  // connection between a webview/iframe and it's webworker. For this to work
@@ -1718,38 +1840,16 @@ const setJson = async (cacheKey, data) => {
1718
1840
 
1719
1841
  const disabledExtensionsCacheKey = '/cache/disabledExtensions.json';
1720
1842
 
1721
- /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
1722
-
1723
- let state = {
1724
- activatedExtensions: Object.create(null),
1725
- cachedActivationEvents: Object.create(null),
1726
- disabledIds: [],
1727
- platform: 0
1728
- };
1729
- const set$1 = newState => {
1730
- state = newState;
1731
- };
1732
- const update = newState => {
1733
- const fullNewState = {
1734
- ...state,
1735
- ...newState
1736
- };
1737
- state = fullNewState;
1738
- };
1739
- const get$1 = () => {
1740
- return state;
1741
- };
1742
-
1743
1843
  const disableExtension2$1 = async (id, platform) => {
1744
1844
  const isTest = platform === Test;
1745
1845
  const isWeb = platform === Web;
1746
- const oldState = get$1(); // TODO maybe pass in an application id? Would allow multiple editors with different extensions.
1846
+ const oldState = get$3(); // TODO maybe pass in an application id? Would allow multiple editors with different extensions.
1747
1847
  if (isTest) {
1748
1848
  const newState = {
1749
1849
  ...oldState,
1750
1850
  disabledIds: [...oldState.disabledIds, id]
1751
1851
  };
1752
- set$1(newState);
1852
+ set$2(newState);
1753
1853
  } else if (isWeb) {
1754
1854
  const cached = await getJson(disabledExtensionsCacheKey);
1755
1855
  const oldDisabled = cached?.disabledExtensions || [];
@@ -1765,13 +1865,13 @@ const disableExtension2$1 = async (id, platform) => {
1765
1865
  const enableExtension2$1 = async (id, platform) => {
1766
1866
  const isTest = platform === Test;
1767
1867
  const isWeb = platform === Web;
1768
- const oldState = get$1();
1868
+ const oldState = get$3();
1769
1869
  if (isTest) {
1770
1870
  const newState = {
1771
1871
  ...oldState,
1772
1872
  disabledIds: oldState.disabledIds.filter(existing => existing !== id)
1773
1873
  };
1774
- set$1(newState);
1874
+ set$2(newState);
1775
1875
  } else if (isWeb) {
1776
1876
  const cached = await getJson(disabledExtensionsCacheKey);
1777
1877
  const oldDisabled = cached?.disabledExtensions || [];
@@ -1800,7 +1900,7 @@ const disableExtension2 = async (id, platform) => {
1800
1900
  };
1801
1901
 
1802
1902
  const disableExtension = async (id, isTest) => {
1803
- const oldState = get$1();
1903
+ const oldState = get$3();
1804
1904
  return disableExtension2(id, oldState.platform);
1805
1905
  };
1806
1906
 
@@ -1827,7 +1927,7 @@ const enableExtension2 = async (id, platform) => {
1827
1927
  };
1828
1928
 
1829
1929
  const enableExtension = async (id, isTest) => {
1830
- const oldState = get$1();
1930
+ const oldState = get$3();
1831
1931
  return enableExtension2(id, oldState.platform);
1832
1932
  };
1833
1933
 
@@ -1996,7 +2096,7 @@ const getColorThemeUri = (extensions, colorThemeId) => {
1996
2096
  };
1997
2097
 
1998
2098
  const getDynamicWebExtensions = () => {
1999
- return get$2();
2099
+ return get$3().webExtensions;
2000
2100
  };
2001
2101
 
2002
2102
  const getWebExtensionsUrl = assetDir => {
@@ -2101,17 +2201,6 @@ const getExtension = async (id, assetDir, platform) => {
2101
2201
  return undefined;
2102
2202
  };
2103
2203
 
2104
- const getPortTuple = () => {
2105
- const {
2106
- port1,
2107
- port2
2108
- } = new MessageChannel();
2109
- return {
2110
- port1,
2111
- port2
2112
- };
2113
- };
2114
-
2115
2204
  const createWebViewIpc = async webView => {
2116
2205
  const {
2117
2206
  origin,
@@ -2158,7 +2247,7 @@ const getRemoteUrlForWebView = async (uri, options = {}) => {
2158
2247
  };
2159
2248
 
2160
2249
  const getRpcInfo = rpcId => {
2161
- const info = get$3(rpcId);
2250
+ const info = get$1(rpcId);
2162
2251
  if (!info) {
2163
2252
  throw new Error(`Rpc not found ${rpcId}`);
2164
2253
  }
@@ -2177,11 +2266,9 @@ const emptyStatus = {
2177
2266
  status: None
2178
2267
  };
2179
2268
  const getRuntimeStatus = extensionId => {
2180
- return get$4(extensionId) || emptyStatus;
2269
+ return getRuntimeStatus$1(extensionId) || emptyStatus;
2181
2270
  };
2182
2271
 
2183
- const commandMapRef = {};
2184
-
2185
2272
  const handleMessagePort = async port => {
2186
2273
  await create$8({
2187
2274
  commandMap: commandMapRef,
@@ -2223,9 +2310,7 @@ const initializeSharedProcess = async platform => {
2223
2310
  };
2224
2311
 
2225
2312
  const initialize = async platform => {
2226
- update({
2227
- platform
2228
- });
2313
+ setPlatform(platform);
2229
2314
  await initializeSharedProcess(platform);
2230
2315
  };
2231
2316
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/extension-management-worker",
3
- "version": "3.3.0",
3
+ "version": "4.0.0",
4
4
  "description": "Webworker for the Extension Management functionality in Lvce Editor.",
5
5
  "keywords": [
6
6
  "web-worker"