@lvce-editor/file-system-worker 2.1.0 → 3.1.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
  };
@@ -1246,8 +1246,8 @@ const FileSystemProcess = {
1246
1246
  readFile: readFile$4,
1247
1247
  readJson: readJson$3,
1248
1248
  registerMockRpc: registerMockRpc$1,
1249
- remove: remove$2,
1250
- rename: rename$2,
1249
+ remove: remove$3,
1250
+ rename: rename$3,
1251
1251
  set: set$3,
1252
1252
  stat: stat$2,
1253
1253
  writeFile: writeFile$3
@@ -1654,31 +1654,29 @@ const invoke$1 = async (method, ...params) => {
1654
1654
  return rpc.invoke(method, ...params);
1655
1655
  };
1656
1656
 
1657
- const readFile$2 = async uri => {
1658
- return invoke$1('FileSystemMemory.readFile', uri);
1659
- };
1660
- const readJson$2 = async uri => {
1661
- throw new Error('not implemented');
1662
- };
1663
- const writeFile$2 = async (uri, content) => {
1664
- 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
+ }
1665
1663
  };
1666
1664
 
1667
1665
  const {
1668
1666
  set,
1669
- rename: rename$1,
1667
+ rename: rename$2,
1670
1668
  copy: copy$1,
1671
1669
  mkdir: mkdir$1,
1672
1670
  invoke,
1673
1671
  getFolderSize: getFolderSize$1,
1674
- writeFile: writeFile$1,
1672
+ writeFile: writeFile$2,
1675
1673
  stat: stat$1,
1676
- readJson: readJson$1,
1674
+ readJson: readJson$2,
1677
1675
  getPathSeparator: getPathSeparator$1,
1678
1676
  getRealPath: getRealPath$1,
1679
1677
  readDirWithFileTypes: readDirWithFileTypes$1,
1680
- readFile: readFile$1,
1681
- remove: remove$1,
1678
+ readFile: readFile$2,
1679
+ remove: remove$2,
1682
1680
  exists: exists$1,
1683
1681
  appendFile: appendFile$1
1684
1682
  } = FileSystemProcess;
@@ -1688,6 +1686,98 @@ const Memory = 'memfs:';
1688
1686
  const Https = 'https:';
1689
1687
  const File = 'file:';
1690
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
+
1691
1781
  const isHttp = uri => {
1692
1782
  return uri.startsWith(Http) || uri.startsWith(Https);
1693
1783
  };
@@ -1696,17 +1786,21 @@ const isMemory = uri => {
1696
1786
  return uri.startsWith(Memory);
1697
1787
  };
1698
1788
 
1789
+ /* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
1699
1790
  const remove = async dirent => {
1700
- return remove$1(dirent);
1791
+ if (isMemory(dirent)) {
1792
+ return remove$1(dirent);
1793
+ }
1794
+ return remove$2(dirent);
1701
1795
  };
1702
1796
  const readFile = async uri => {
1703
1797
  if (isHttp(uri)) {
1704
1798
  return readFile$5(uri);
1705
1799
  }
1706
1800
  if (isMemory(uri)) {
1707
- return readFile$2(uri);
1801
+ return readFile$1(uri);
1708
1802
  }
1709
- return readFile$1(uri);
1803
+ return readFile$2(uri);
1710
1804
  };
1711
1805
  const appendFile = async (uri, text) => {
1712
1806
  return appendFile$1(uri, text);
@@ -1722,9 +1816,9 @@ const readJson = async uri => {
1722
1816
  return readJson$4(uri);
1723
1817
  }
1724
1818
  if (isMemory(uri)) {
1725
- return readJson$2();
1819
+ return readJson$1();
1726
1820
  }
1727
- return readJson$1(uri);
1821
+ return readJson$2(uri);
1728
1822
  };
1729
1823
  const getRealPath = async path => {
1730
1824
  return getRealPath$1(path);
@@ -1755,19 +1849,38 @@ const exists = async uri => {
1755
1849
  return exists$1(uri);
1756
1850
  };
1757
1851
  const createFile = async uri => {
1758
- return writeFile$1(uri, '');
1852
+ if (isMemory(uri)) {
1853
+ return createFile$1(uri);
1854
+ }
1855
+ return writeFile$2(uri, '');
1759
1856
  };
1760
1857
  const writeFile = async (uri, content) => {
1761
1858
  if (isMemory(uri)) {
1762
- return writeFile$2(uri, content);
1859
+ return writeFile$1(uri, content);
1763
1860
  }
1764
- return writeFile$1(uri, content);
1861
+ return writeFile$2(uri, content);
1862
+ };
1863
+ const getBytes = async blob => {
1864
+ if (blob.bytes) {
1865
+ return blob.bytes();
1866
+ }
1867
+ const buffer = await blob.arrayBuffer();
1868
+ const bytes = new Uint8Array(buffer);
1869
+ return bytes;
1870
+ };
1871
+ const writeBlob = async (uri, blob) => {
1872
+ const bytes = await getBytes(blob);
1873
+ // @ts-ignore
1874
+ await invoke('FileSystem.writeBuffer', uri, bytes);
1765
1875
  };
1766
1876
  const mkdir = async uri => {
1767
1877
  return mkdir$1(uri);
1768
1878
  };
1769
1879
  const rename = async (oldUri, newUri) => {
1770
- return rename$1(oldUri, newUri);
1880
+ if (isMemory(oldUri) || isMemory(newUri)) {
1881
+ return rename$1(oldUri, newUri);
1882
+ }
1883
+ return rename$2(oldUri, newUri);
1771
1884
  };
1772
1885
  const copy = async (oldUri, newUri) => {
1773
1886
  return copy$1(oldUri, newUri);
@@ -1776,62 +1889,6 @@ const getFolderSize = async uri => {
1776
1889
  return getFolderSize$1(uri);
1777
1890
  };
1778
1891
 
1779
- const RE_PROTOCOL = /^([a-z-]+):\/\//;
1780
- const assertUri = uri => {
1781
- const protocolMatch = uri.match(RE_PROTOCOL);
1782
- if (!protocolMatch) {
1783
- throw new Error(`uri must be a valid uri`);
1784
- }
1785
- };
1786
-
1787
- const watchCallbacks = Object.create(null);
1788
- const registerWatchCallback = (id, rpcId, commandId) => {
1789
- watchCallbacks[id] = {
1790
- rpcId,
1791
- commandId
1792
- };
1793
- };
1794
- const executeWatchCallBack = async id => {
1795
- const entry = watchCallbacks[id];
1796
- if (!entry) {
1797
- throw new Error(`watch callback ${id} not found`);
1798
- }
1799
- const {
1800
- rpcId,
1801
- commandId
1802
- } = entry;
1803
- const rpc = get(rpcId);
1804
- await rpc.invoke(commandId, id);
1805
- };
1806
- const unregisterWatchCallback = id => {
1807
- delete watchCallbacks[id];
1808
- };
1809
-
1810
- // TODO support file watchers for differenr protocols like memfs, http
1811
-
1812
- const watchFile = async (id, uri, rpcId) => {
1813
- assertUri(uri);
1814
- const commandId = 'Output.executeWatchCallback';
1815
- registerWatchCallback(id, rpcId, commandId);
1816
- if (uri.startsWith(File)) {
1817
- // @ts-ignore
1818
- await invoke('FileSystem.watchFile', id, uri);
1819
- }
1820
- };
1821
- const executeWatchCallback = async id => {
1822
- try {
1823
- await executeWatchCallBack(id);
1824
- } catch (error) {
1825
- console.error(error);
1826
- }
1827
- };
1828
- const unwatchFile = async id => {
1829
- unregisterWatchCallback(id);
1830
- // TODO only if it is a file uri
1831
- // @ts-ignore
1832
- await invoke('FileSystem.unwatchFile', id);
1833
- };
1834
-
1835
1892
  const handleMessagePort = async (port, rpcId) => {
1836
1893
  const rpc = await PlainMessagePortRpc.create({
1837
1894
  commandMap: {},
@@ -1931,6 +1988,7 @@ const commandMap = {
1931
1988
  'FileSystem.unwatchFile': unwatchFile,
1932
1989
  'FileSystem.watchFile': watchFile,
1933
1990
  'FileSystem.writeFile': writeFile,
1991
+ 'FileSystem.writeBlob': writeBlob,
1934
1992
  'Initialize.initialize': initialize
1935
1993
  };
1936
1994
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/file-system-worker",
3
- "version": "2.1.0",
3
+ "version": "3.1.0",
4
4
  "description": "File System Worker",
5
5
  "keywords": [
6
6
  "Lvce Editor"