@lvce-editor/file-system-worker 2.0.0 → 3.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.
@@ -541,7 +541,7 @@ const set$5 = (id, fn) => {
541
541
  const get$1 = id => {
542
542
  return callbacks[id];
543
543
  };
544
- const remove$3 = id => {
544
+ const remove$4 = id => {
545
545
  delete callbacks[id];
546
546
  };
547
547
  let id = 0;
@@ -726,7 +726,7 @@ const resolve = (id, response) => {
726
726
  return;
727
727
  }
728
728
  fn(response);
729
- remove$3(id);
729
+ remove$4(id);
730
730
  };
731
731
  const E_COMMAND_NOT_FOUND = 'E_COMMAND_NOT_FOUND';
732
732
  const getErrorType = prettyError => {
@@ -1169,7 +1169,7 @@ const {
1169
1169
  set: set$3,
1170
1170
  dispose: dispose$1
1171
1171
  } = create(FileSystemProcess$1);
1172
- const remove$2 = async uri => {
1172
+ const remove$3 = async uri => {
1173
1173
  return invoke$3('FileSystem.remove', uri);
1174
1174
  };
1175
1175
  const readFile$4 = async uri => {
@@ -1206,7 +1206,7 @@ const mkdir$2 = async path => {
1206
1206
  // @ts-ignore
1207
1207
  return invoke$3('FileSystem.mkdir', path);
1208
1208
  };
1209
- const rename$2 = async (oldUri, newUri) => {
1209
+ const rename$3 = async (oldUri, newUri) => {
1210
1210
  // @ts-ignore
1211
1211
  return invoke$3('FileSystem.rename', oldUri, newUri);
1212
1212
  };
@@ -1222,6 +1222,13 @@ const exists$2 = async uri => {
1222
1222
  // @ts-ignore
1223
1223
  return invoke$3('FileSystem.exists', uri);
1224
1224
  };
1225
+ const registerMockRpc$1 = commandMap => {
1226
+ const mockRpc = createMockRpc({
1227
+ commandMap
1228
+ });
1229
+ set$3(mockRpc);
1230
+ return mockRpc;
1231
+ };
1225
1232
 
1226
1233
  const FileSystemProcess = {
1227
1234
  __proto__: null,
@@ -1238,8 +1245,9 @@ const FileSystemProcess = {
1238
1245
  readDirWithFileTypes: readDirWithFileTypes$2,
1239
1246
  readFile: readFile$4,
1240
1247
  readJson: readJson$3,
1241
- remove: remove$2,
1242
- rename: rename$2,
1248
+ registerMockRpc: registerMockRpc$1,
1249
+ remove: remove$3,
1250
+ rename: rename$3,
1243
1251
  set: set$3,
1244
1252
  stat: stat$2,
1245
1253
  writeFile: writeFile$3
@@ -1307,6 +1315,11 @@ const sendMessagePortToMarkdownWorker = async (port, rpcId) => {
1307
1315
  // @ts-ignore
1308
1316
  await invokeAndTransfer$1('SendMessagePortToExtensionHostWorker.sendMessagePortToMarkdownWorker', port, command, rpcId);
1309
1317
  };
1318
+ const sendMessagePortToIconThemeWorker = async (port, rpcId) => {
1319
+ const command = 'IconTheme.handleMessagePort';
1320
+ // @ts-ignore
1321
+ await invokeAndTransfer$1('SendMessagePortToExtensionHostWorker.sendMessagePortToIconThemeWorker', port, command, rpcId);
1322
+ };
1310
1323
  const sendMessagePortToFileSystemWorker = async (port, rpcId) => {
1311
1324
  const command = 'FileSystem.handleMessagePort';
1312
1325
  // @ts-ignore
@@ -1573,6 +1586,7 @@ const RendererWorker = {
1573
1586
  sendMessagePortToErrorWorker,
1574
1587
  sendMessagePortToExtensionHostWorker: sendMessagePortToExtensionHostWorker$1,
1575
1588
  sendMessagePortToFileSystemWorker,
1589
+ sendMessagePortToIconThemeWorker,
1576
1590
  sendMessagePortToMarkdownWorker,
1577
1591
  sendMessagePortToRendererProcess,
1578
1592
  sendMessagePortToSearchProcess,
@@ -1640,31 +1654,29 @@ const invoke$1 = async (method, ...params) => {
1640
1654
  return rpc.invoke(method, ...params);
1641
1655
  };
1642
1656
 
1643
- const readFile$2 = async uri => {
1644
- return invoke$1('FileSystemMemory.readFile', uri);
1645
- };
1646
- const readJson$2 = async uri => {
1647
- throw new Error('not implemented');
1648
- };
1649
- const writeFile$2 = async (uri, content) => {
1650
- return invoke$1('FileSystemMemory.writeFile', uri, content);
1657
+ const RE_PROTOCOL = /^([a-z-]+):\/\//;
1658
+ const assertUri = uri => {
1659
+ const protocolMatch = uri.match(RE_PROTOCOL);
1660
+ if (!protocolMatch) {
1661
+ throw new Error(`uri must be a valid uri`);
1662
+ }
1651
1663
  };
1652
1664
 
1653
1665
  const {
1654
1666
  set,
1655
- rename: rename$1,
1667
+ rename: rename$2,
1656
1668
  copy: copy$1,
1657
1669
  mkdir: mkdir$1,
1658
1670
  invoke,
1659
1671
  getFolderSize: getFolderSize$1,
1660
- writeFile: writeFile$1,
1672
+ writeFile: writeFile$2,
1661
1673
  stat: stat$1,
1662
- readJson: readJson$1,
1674
+ readJson: readJson$2,
1663
1675
  getPathSeparator: getPathSeparator$1,
1664
1676
  getRealPath: getRealPath$1,
1665
1677
  readDirWithFileTypes: readDirWithFileTypes$1,
1666
- readFile: readFile$1,
1667
- remove: remove$1,
1678
+ readFile: readFile$2,
1679
+ remove: remove$2,
1668
1680
  exists: exists$1,
1669
1681
  appendFile: appendFile$1
1670
1682
  } = FileSystemProcess;
@@ -1674,6 +1686,98 @@ const Memory = 'memfs:';
1674
1686
  const Https = 'https:';
1675
1687
  const File = 'file:';
1676
1688
 
1689
+ const watchCallbacks = Object.create(null);
1690
+ const registerWatchCallback = (id, rpcId, commandId, uri) => {
1691
+ watchCallbacks[id] = {
1692
+ rpcId,
1693
+ commandId,
1694
+ uri
1695
+ };
1696
+ };
1697
+ const executeWatchCallBack = async id => {
1698
+ const entry = watchCallbacks[id];
1699
+ if (!entry) {
1700
+ throw new Error(`watch callback ${id} not found`);
1701
+ }
1702
+ const {
1703
+ rpcId,
1704
+ commandId
1705
+ } = entry;
1706
+ const rpc = get(rpcId);
1707
+ await rpc.invoke(commandId, id);
1708
+ };
1709
+ const unregisterWatchCallback = id => {
1710
+ delete watchCallbacks[id];
1711
+ };
1712
+ const getWatchCallbackIdsForUri = uri => {
1713
+ const watchIds = [];
1714
+ for (const [id, entry] of Object.entries(watchCallbacks)) {
1715
+ if (entry.uri === uri) {
1716
+ watchIds.push(Number(id));
1717
+ }
1718
+ }
1719
+ return watchIds;
1720
+ };
1721
+
1722
+ const watchFile = async (id, uri, rpcId) => {
1723
+ assertUri(uri);
1724
+ const commandId = 'Output.executeWatchCallback';
1725
+ registerWatchCallback(id, rpcId, commandId, uri);
1726
+ if (uri.startsWith(File)) {
1727
+ // @ts-ignore
1728
+ await invoke('FileSystem.watchFile', id, uri);
1729
+ }
1730
+ // memfs file watchers are handled in-memory, no need to register with FileSystemProcess
1731
+ };
1732
+ const executeWatchCallback = async id => {
1733
+ try {
1734
+ await executeWatchCallBack(id);
1735
+ } catch (error) {
1736
+ console.error(error);
1737
+ }
1738
+ };
1739
+ const unwatchFile = async id => {
1740
+ unregisterWatchCallback(id);
1741
+ // TODO only if it is a file uri
1742
+ // @ts-ignore
1743
+ await invoke('FileSystem.unwatchFile', id);
1744
+ };
1745
+ const triggerMemfsFileWatcher = async uri => {
1746
+ // Find all registered watchers for this URI and trigger them
1747
+ const watchCallbackIds = getWatchCallbackIdsForUri(uri);
1748
+ for (const id of watchCallbackIds) {
1749
+ await executeWatchCallback(id);
1750
+ }
1751
+ };
1752
+
1753
+ const remove$1 = async dirent => {
1754
+ await invoke$1('FileSystemMemory.remove', dirent);
1755
+ // Trigger file watchers for memfs files
1756
+ await triggerMemfsFileWatcher(dirent);
1757
+ };
1758
+ const readFile$1 = async uri => {
1759
+ return invoke$1('FileSystemMemory.readFile', uri);
1760
+ };
1761
+ const readJson$1 = async uri => {
1762
+ throw new Error('not implemented');
1763
+ };
1764
+ const createFile$1 = async uri => {
1765
+ await invoke$1('FileSystemMemory.createFile', uri);
1766
+ // Trigger file watchers for memfs files
1767
+ await triggerMemfsFileWatcher(uri);
1768
+ };
1769
+ const writeFile$1 = async (uri, content) => {
1770
+ await invoke$1('FileSystemMemory.writeFile', uri, content);
1771
+ // Trigger file watchers for memfs files
1772
+ await triggerMemfsFileWatcher(uri);
1773
+ };
1774
+ const rename$1 = async (oldUri, newUri) => {
1775
+ await invoke$1('FileSystemMemory.rename', oldUri, newUri);
1776
+ // Trigger file watchers for both old and new URIs
1777
+ await triggerMemfsFileWatcher(oldUri);
1778
+ await triggerMemfsFileWatcher(newUri);
1779
+ };
1780
+
1677
1781
  const isHttp = uri => {
1678
1782
  return uri.startsWith(Http) || uri.startsWith(Https);
1679
1783
  };
@@ -1682,17 +1786,21 @@ const isMemory = uri => {
1682
1786
  return uri.startsWith(Memory);
1683
1787
  };
1684
1788
 
1789
+ /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
1685
1790
  const remove = async dirent => {
1686
- return remove$1(dirent);
1791
+ if (isMemory(dirent)) {
1792
+ return remove$1(dirent);
1793
+ }
1794
+ return remove$2(dirent);
1687
1795
  };
1688
1796
  const readFile = async uri => {
1689
1797
  if (isHttp(uri)) {
1690
1798
  return readFile$5(uri);
1691
1799
  }
1692
1800
  if (isMemory(uri)) {
1693
- return readFile$2(uri);
1801
+ return readFile$1(uri);
1694
1802
  }
1695
- return readFile$1(uri);
1803
+ return readFile$2(uri);
1696
1804
  };
1697
1805
  const appendFile = async (uri, text) => {
1698
1806
  return appendFile$1(uri, text);
@@ -1708,9 +1816,9 @@ const readJson = async uri => {
1708
1816
  return readJson$4(uri);
1709
1817
  }
1710
1818
  if (isMemory(uri)) {
1711
- return readJson$2();
1819
+ return readJson$1();
1712
1820
  }
1713
- return readJson$1(uri);
1821
+ return readJson$2(uri);
1714
1822
  };
1715
1823
  const getRealPath = async path => {
1716
1824
  return getRealPath$1(path);
@@ -1741,19 +1849,30 @@ const exists = async uri => {
1741
1849
  return exists$1(uri);
1742
1850
  };
1743
1851
  const createFile = async uri => {
1744
- return writeFile$1(uri, '');
1852
+ if (isMemory(uri)) {
1853
+ return createFile$1(uri);
1854
+ }
1855
+ return writeFile$2(uri, '');
1745
1856
  };
1746
1857
  const writeFile = async (uri, content) => {
1747
1858
  if (isMemory(uri)) {
1748
- return writeFile$2(uri, content);
1859
+ return writeFile$1(uri, content);
1749
1860
  }
1750
- return writeFile$1(uri, content);
1861
+ return writeFile$2(uri, content);
1862
+ };
1863
+ const writeBlob = async (uri, blob) => {
1864
+ const buffer = await blob.arrayBuffer();
1865
+ // @ts-ignore
1866
+ await invoke('FileSystem.writeBuffer', uri, buffer);
1751
1867
  };
1752
1868
  const mkdir = async uri => {
1753
1869
  return mkdir$1(uri);
1754
1870
  };
1755
1871
  const rename = async (oldUri, newUri) => {
1756
- return rename$1(oldUri, newUri);
1872
+ if (isMemory(oldUri) || isMemory(newUri)) {
1873
+ return rename$1(oldUri, newUri);
1874
+ }
1875
+ return rename$2(oldUri, newUri);
1757
1876
  };
1758
1877
  const copy = async (oldUri, newUri) => {
1759
1878
  return copy$1(oldUri, newUri);
@@ -1762,62 +1881,6 @@ const getFolderSize = async uri => {
1762
1881
  return getFolderSize$1(uri);
1763
1882
  };
1764
1883
 
1765
- const RE_PROTOCOL = /^([a-z-]+):\/\//;
1766
- const assertUri = uri => {
1767
- const protocolMatch = uri.match(RE_PROTOCOL);
1768
- if (!protocolMatch) {
1769
- throw new Error(`uri must be a valid uri`);
1770
- }
1771
- };
1772
-
1773
- const watchCallbacks = Object.create(null);
1774
- const registerWatchCallback = (id, rpcId, commandId) => {
1775
- watchCallbacks[id] = {
1776
- rpcId,
1777
- commandId
1778
- };
1779
- };
1780
- const executeWatchCallBack = async id => {
1781
- const entry = watchCallbacks[id];
1782
- if (!entry) {
1783
- throw new Error(`watch callback ${id} not found`);
1784
- }
1785
- const {
1786
- rpcId,
1787
- commandId
1788
- } = entry;
1789
- const rpc = get(rpcId);
1790
- await rpc.invoke(commandId, id);
1791
- };
1792
- const unregisterWatchCallback = id => {
1793
- delete watchCallbacks[id];
1794
- };
1795
-
1796
- // TODO support file watchers for differenr protocols like memfs, http
1797
-
1798
- const watchFile = async (id, uri, rpcId) => {
1799
- assertUri(uri);
1800
- const commandId = 'Output.executeWatchCallback';
1801
- registerWatchCallback(id, rpcId, commandId);
1802
- if (uri.startsWith(File)) {
1803
- // @ts-ignore
1804
- await invoke('FileSystem.watchFile', id, uri);
1805
- }
1806
- };
1807
- const executeWatchCallback = async id => {
1808
- try {
1809
- await executeWatchCallBack(id);
1810
- } catch (error) {
1811
- console.error(error);
1812
- }
1813
- };
1814
- const unwatchFile = async id => {
1815
- unregisterWatchCallback(id);
1816
- // TODO only if it is a file uri
1817
- // @ts-ignore
1818
- await invoke('FileSystem.unwatchFile', id);
1819
- };
1820
-
1821
1884
  const handleMessagePort = async (port, rpcId) => {
1822
1885
  const rpc = await PlainMessagePortRpc.create({
1823
1886
  commandMap: {},
@@ -1917,6 +1980,7 @@ const commandMap = {
1917
1980
  'FileSystem.unwatchFile': unwatchFile,
1918
1981
  'FileSystem.watchFile': watchFile,
1919
1982
  'FileSystem.writeFile': writeFile,
1983
+ 'FileSystem.writeBlob': writeBlob,
1920
1984
  'Initialize.initialize': initialize
1921
1985
  };
1922
1986
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/file-system-worker",
3
- "version": "2.0.0",
3
+ "version": "3.0.0",
4
4
  "description": "File System Worker",
5
5
  "keywords": [
6
6
  "Lvce Editor"