@lvce-editor/extension-management-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.
@@ -1486,7 +1486,7 @@ const clear = () => {
1486
1486
  delete cache[id];
1487
1487
  };
1488
1488
 
1489
- const getJson = async url => {
1489
+ const getJson$1 = async url => {
1490
1490
  try {
1491
1491
  const response = await fetch(url);
1492
1492
  if (!response.ok) {
@@ -1501,7 +1501,7 @@ const getJson = async url => {
1501
1501
 
1502
1502
  const getWebExtensionManifest = async (path, manifestPath) => {
1503
1503
  try {
1504
- const manifest = await getJson(manifestPath);
1504
+ const manifest = await getJson$1(manifestPath);
1505
1505
  return {
1506
1506
  ...manifest,
1507
1507
  path,
@@ -1585,11 +1585,49 @@ const createWebViewWorkerRpc = async (rpcInfo, port) => {
1585
1585
  });
1586
1586
  };
1587
1587
 
1588
- const invalidateExtensionsCache = async () => {
1589
- await invoke$2('ExtensionManagement.invalidateExtensionsCache');
1588
+ const ContentType = 'Content-Type';
1589
+ const ContentLength = 'Content-Length';
1590
+
1591
+ const createResponseFromData = data => {
1592
+ const responseString = JSON.stringify(data);
1593
+ const response = new Response(responseString, {
1594
+ headers: {
1595
+ [ContentLength]: `${responseString.length}`,
1596
+ [ContentType]: 'application/json'
1597
+ }
1598
+ });
1599
+ return response;
1600
+ };
1601
+
1602
+ const cacheName = 'Extensions'; // TODO
1603
+
1604
+ const getJson = async cacheKey => {
1605
+ const response = await caches.match(cacheKey, {
1606
+ cacheName
1607
+ });
1608
+ if (!response) {
1609
+ return undefined;
1610
+ }
1611
+ const json = await response.json();
1612
+ return json;
1613
+ };
1614
+ const setJson = async (cacheKey, data) => {
1615
+ try {
1616
+ const cache = await caches.open(cacheName);
1617
+ const response = createResponseFromData(data);
1618
+ await cache.put(cacheKey, response);
1619
+ } catch (error) {
1620
+ throw new VError(error, `Failed to add to cache`);
1621
+ }
1590
1622
  };
1591
1623
 
1624
+ const disabledExtensionsCacheKey = '/cache/disabledExtensions.json';
1625
+
1626
+ /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
1627
+
1592
1628
  let state = {
1629
+ activatedExtensions: Object.create(null),
1630
+ cachedActivationEvents: Object.create(null),
1593
1631
  disabledIds: [],
1594
1632
  platform: 0
1595
1633
  };
@@ -1607,19 +1645,58 @@ const get$1 = () => {
1607
1645
  return state;
1608
1646
  };
1609
1647
 
1610
- const disableExtension2 = async (id, platform) => {
1648
+ const disableExtension2$1 = async (id, platform) => {
1611
1649
  const isTest = platform === Test;
1650
+ const isWeb = platform === Web;
1651
+ const oldState = get$1(); // TODO maybe pass in an application id? Would allow multiple editors with different extensions.
1652
+ if (isTest) {
1653
+ const newState = {
1654
+ ...oldState,
1655
+ disabledIds: [...oldState.disabledIds, id]
1656
+ };
1657
+ set$1(newState);
1658
+ } else if (isWeb) {
1659
+ const cached = await getJson(disabledExtensionsCacheKey);
1660
+ const oldDisabled = cached?.disabledExtensions || [];
1661
+ const newDisabled = [...oldDisabled, id];
1662
+ const newData = {
1663
+ disabledExtensions: newDisabled
1664
+ };
1665
+ await setJson(disabledExtensionsCacheKey, newData);
1666
+ } else {
1667
+ await invoke$1('ExtensionManagement.disable', id);
1668
+ }
1669
+ };
1670
+ const enableExtension2$1 = async (id, platform) => {
1671
+ const isTest = platform === Test;
1672
+ const isWeb = platform === Web;
1612
1673
  const oldState = get$1();
1674
+ if (isTest) {
1675
+ const newState = {
1676
+ ...oldState,
1677
+ disabledIds: oldState.disabledIds.filter(existing => existing !== id)
1678
+ };
1679
+ set$1(newState);
1680
+ } else if (isWeb) {
1681
+ const cached = await getJson(disabledExtensionsCacheKey);
1682
+ const oldDisabled = cached?.disabledExtensions || [];
1683
+ const newDisabled = oldDisabled.filter(item => item !== id);
1684
+ const newData = {
1685
+ disabledExtensions: newDisabled
1686
+ };
1687
+ await setJson(disabledExtensionsCacheKey, newData);
1688
+ }
1689
+ };
1690
+
1691
+ const invalidateExtensionsCache = async () => {
1692
+ await invoke$2('ExtensionManagement.invalidateExtensionsCache');
1693
+ };
1694
+
1695
+ const disableExtension2 = async (id, platform) => {
1696
+ string(id);
1697
+ number(platform);
1613
1698
  try {
1614
- if (isTest) {
1615
- const newState = {
1616
- ...oldState,
1617
- disabledIds: [...oldState.disabledIds, id]
1618
- };
1619
- set$1(newState);
1620
- } else {
1621
- await invoke$1('ExtensionManagement.disable', id);
1622
- }
1699
+ await disableExtension2$1(id, platform);
1623
1700
  await invalidateExtensionsCache();
1624
1701
  return undefined;
1625
1702
  } catch (error) {
@@ -1633,10 +1710,10 @@ const disableExtension = async (id, isTest) => {
1633
1710
  };
1634
1711
 
1635
1712
  const enableExtension2 = async (id, platform) => {
1636
- const isTest = platform === Test;
1637
- const oldState = get$1();
1713
+ string(id);
1714
+ number(platform);
1638
1715
  if (platform === Remote || platform === Electron) {
1639
- const disabledExtensionsJsonPath = await invoke$2('PlatformPaths.getBuiltinExtensionsJsonPath');
1716
+ const disabledExtensionsJsonPath = await invoke$2('WebView.compatSharedProcessInvoke', 'PlatformPaths.getDisabledExtensionsJsonUri');
1640
1717
  const exists$1 = await exists(disabledExtensionsJsonPath);
1641
1718
  if (!exists$1) {
1642
1719
  return undefined;
@@ -1651,14 +1728,7 @@ const enableExtension2 = async (id, platform) => {
1651
1728
  const newContent = JSON.stringify(newData, null, 2) + '\n';
1652
1729
  await writeFile(disabledExtensionsJsonPath, newContent);
1653
1730
  }
1654
- if (isTest) {
1655
- const newState = {
1656
- ...oldState,
1657
- disabledIds: oldState.disabledIds.filter(existing => existing !== id)
1658
- };
1659
- set$1(newState);
1660
- }
1661
- return undefined;
1731
+ await enableExtension2$1(id, platform);
1662
1732
  };
1663
1733
 
1664
1734
  const enableExtension = async (id, isTest) => {
@@ -1840,13 +1910,19 @@ const getWebExtensionsUrl = assetDir => {
1840
1910
 
1841
1911
  const getWebExtensions = async assetDir => {
1842
1912
  try {
1843
- return await getJson(getWebExtensionsUrl(assetDir));
1913
+ return await getJson$1(getWebExtensionsUrl(assetDir));
1844
1914
  } catch {
1845
1915
  return [];
1846
1916
  }
1847
1917
  };
1848
1918
 
1849
1919
  const getAllExtensions = async (assetDir, platform) => {
1920
+ if (!assetDir) {
1921
+ assetDir = await invoke$2('Layout.getAssetDir');
1922
+ }
1923
+ if (!platform) {
1924
+ platform = await invoke$2('Layout.getPlatform');
1925
+ }
1850
1926
  string(assetDir);
1851
1927
  number(platform);
1852
1928
  const meta = getDynamicWebExtensions();
@@ -1874,7 +1950,7 @@ const getColorThemeUrlWeb = (assetDir, colorThemeId) => {
1874
1950
  const getColorThemeJson$1 = (colorThemeId, assetDir) => {
1875
1951
  const url = getColorThemeUrlWeb(assetDir, colorThemeId);
1876
1952
  // TODO handle error ?
1877
- return getJson(url);
1953
+ return getJson$1(url);
1878
1954
  };
1879
1955
 
1880
1956
  const getColorThemeJson = (colorThemeId, platform, assetDir) => {
@@ -1913,6 +1989,8 @@ const getColorThemeNamesFromExtensions = async extensions => {
1913
1989
  };
1914
1990
 
1915
1991
  const getColorThemeNames = async (assetDir, platform) => {
1992
+ string(assetDir);
1993
+ number(platform);
1916
1994
  const extensions = await getAllExtensions(assetDir, platform);
1917
1995
  const colorThemeNames = getColorThemeNamesFromExtensions(extensions);
1918
1996
  return colorThemeNames;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/extension-management-worker",
3
- "version": "2.1.0",
3
+ "version": "2.3.0",
4
4
  "description": "Webworker for the Extension Management functionality in Lvce Editor.",
5
5
  "keywords": [
6
6
  "web-worker"