@lvce-editor/file-system-process 5.14.0 → 5.15.1
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.
- package/README.md +1 -1
- package/dist/index.js +62 -1
- package/package.json +1 -1
package/README.md
CHANGED
package/dist/index.js
CHANGED
|
@@ -1682,13 +1682,57 @@ const readFile = async (uri, encoding = Utf8) => {
|
|
|
1682
1682
|
throw new VError(error, `Failed to read file "${uri}"`);
|
|
1683
1683
|
}
|
|
1684
1684
|
};
|
|
1685
|
+
const fileHashCache = new Map();
|
|
1686
|
+
const maxFileHashCacheEntries = 50_000;
|
|
1687
|
+
const getFileFingerprint = stats => {
|
|
1688
|
+
return `${stats.dev}:${stats.ino}:${stats.size}:${stats.mtimeNs}:${stats.ctimeNs}`;
|
|
1689
|
+
};
|
|
1690
|
+
const getCachedFileHash = (path, fingerprint) => {
|
|
1691
|
+
const entry = fileHashCache.get(path);
|
|
1692
|
+
if (!entry || entry.fingerprint !== fingerprint) {
|
|
1693
|
+
return undefined;
|
|
1694
|
+
}
|
|
1695
|
+
fileHashCache.delete(path);
|
|
1696
|
+
fileHashCache.set(path, entry);
|
|
1697
|
+
return entry.hash;
|
|
1698
|
+
};
|
|
1699
|
+
const setCachedFileHash = (path, fingerprint, hash) => {
|
|
1700
|
+
fileHashCache.delete(path);
|
|
1701
|
+
fileHashCache.set(path, {
|
|
1702
|
+
fingerprint,
|
|
1703
|
+
hash
|
|
1704
|
+
});
|
|
1705
|
+
if (fileHashCache.size <= maxFileHashCacheEntries) {
|
|
1706
|
+
return;
|
|
1707
|
+
}
|
|
1708
|
+
const oldestPath = fileHashCache.keys().next().value;
|
|
1709
|
+
if (oldestPath) {
|
|
1710
|
+
fileHashCache.delete(oldestPath);
|
|
1711
|
+
}
|
|
1712
|
+
};
|
|
1685
1713
|
const getFileHash = async uri => {
|
|
1686
1714
|
try {
|
|
1687
1715
|
string(uri);
|
|
1688
1716
|
assertUri(uri);
|
|
1689
1717
|
const path = fileURLToPath(uri);
|
|
1718
|
+
const statsBeforeRead = await fs.stat(path, {
|
|
1719
|
+
bigint: true
|
|
1720
|
+
});
|
|
1721
|
+
const fingerprintBeforeRead = getFileFingerprint(statsBeforeRead);
|
|
1722
|
+
const cachedHash = getCachedFileHash(path, fingerprintBeforeRead);
|
|
1723
|
+
if (cachedHash) {
|
|
1724
|
+
return cachedHash;
|
|
1725
|
+
}
|
|
1690
1726
|
const content = await fs.readFile(path);
|
|
1691
|
-
|
|
1727
|
+
const hash = createHash('sha256').update(content).digest('hex');
|
|
1728
|
+
const statsAfterRead = await fs.stat(path, {
|
|
1729
|
+
bigint: true
|
|
1730
|
+
});
|
|
1731
|
+
const fingerprintAfterRead = getFileFingerprint(statsAfterRead);
|
|
1732
|
+
if (fingerprintBeforeRead === fingerprintAfterRead) {
|
|
1733
|
+
setCachedFileHash(path, fingerprintAfterRead, hash);
|
|
1734
|
+
}
|
|
1735
|
+
return hash;
|
|
1692
1736
|
} catch (error) {
|
|
1693
1737
|
if (isEnoentError(error)) {
|
|
1694
1738
|
throw new FileNotFoundError(uri);
|
|
@@ -1797,6 +1841,22 @@ const remove = async uri => {
|
|
|
1797
1841
|
throw new VError(error, `Failed to remove "${uri}"`);
|
|
1798
1842
|
}
|
|
1799
1843
|
};
|
|
1844
|
+
const forceRemove = async uri => {
|
|
1845
|
+
assertUri(uri);
|
|
1846
|
+
const path = fileURLToPath(uri);
|
|
1847
|
+
if (!isOkayToRemove(path)) {
|
|
1848
|
+
throw new Error('Cannot remove a filesystem root or home directory');
|
|
1849
|
+
}
|
|
1850
|
+
try {
|
|
1851
|
+
const stats = await fs.lstat(path);
|
|
1852
|
+
await fs.rm(path, {
|
|
1853
|
+
force: false,
|
|
1854
|
+
recursive: stats.isDirectory() && !stats.isSymbolicLink()
|
|
1855
|
+
});
|
|
1856
|
+
} catch (error) {
|
|
1857
|
+
throw new VError(error, `Failed to permanently remove "${uri}"`);
|
|
1858
|
+
}
|
|
1859
|
+
};
|
|
1800
1860
|
const toPrettyDirent = dirent => {
|
|
1801
1861
|
return {
|
|
1802
1862
|
name: dirent.name,
|
|
@@ -1976,6 +2036,7 @@ const commandMap = {
|
|
|
1976
2036
|
'FileSystem.chmod': chmod,
|
|
1977
2037
|
'FileSystem.copy': copy,
|
|
1978
2038
|
'FileSystem.exists': exists,
|
|
2039
|
+
'FileSystem.forceRemove': forceRemove,
|
|
1979
2040
|
'FileSystem.getFileHash': getFileHash,
|
|
1980
2041
|
'FileSystem.getFileHashes': getFileHashes,
|
|
1981
2042
|
'FileSystem.getFolderSize': getFolderSize,
|