@lvce-editor/file-system-process 5.15.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.
Files changed (2) hide show
  1. package/dist/index.js +45 -1
  2. package/package.json +1 -1
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
- return createHash('sha256').update(content).digest('hex');
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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lvce-editor/file-system-process",
3
- "version": "5.15.0",
3
+ "version": "5.15.1",
4
4
  "description": "File System Process",
5
5
  "keywords": [
6
6
  "Lvce Editor",