@file-services/cached
Advanced tools
Comparing version 5.4.0 to 5.7.0
@@ -11,7 +11,10 @@ "use strict"; | ||
const realpathCache = new Map(); | ||
const { promises } = fs; | ||
const { promises, delimiter } = fs; | ||
const suffixTrue = delimiter + 'true'; | ||
const suffixFalse = delimiter + 'false'; | ||
const invalidateAbsolute = (absolutePath) => { | ||
const cachePath = getCanonicalPath(absolutePath); | ||
realpathCache.delete(cachePath); | ||
statsCache.delete(cachePath); | ||
statsCache.delete(cachePath + suffixTrue); | ||
statsCache.delete(cachePath + suffixFalse); | ||
}; | ||
@@ -107,5 +110,7 @@ const invalidateAbsoluteByPrefix = (absolutePath) => { | ||
}, | ||
statSync(path) { | ||
statSync(path, options) { | ||
var _a; | ||
path = fs.resolve(path); | ||
const cacheKey = getCanonicalPath(path); | ||
const throwIfNoEntry = (_a = options === null || options === void 0 ? void 0 : options.throwIfNoEntry) !== null && _a !== void 0 ? _a : true; | ||
const cacheKey = getCanonicalPath(path) + (throwIfNoEntry ? suffixTrue : suffixFalse); | ||
const cachedStats = statsCache.get(cacheKey); | ||
@@ -119,3 +124,3 @@ if (cachedStats) { | ||
try { | ||
const stats = fs.statSync(path); | ||
const stats = fs.statSync(path, options); | ||
statsCache.set(cacheKey, { kind: 'success', value: stats }); | ||
@@ -131,3 +136,4 @@ return stats; | ||
path = fs.resolve(path); | ||
const cacheKey = getCanonicalPath(path); | ||
// force throwIfNoEntry, as callback version doesn't support it | ||
const cacheKey = getCanonicalPath(path) + suffixTrue; | ||
const cachedStats = statsCache.get(cacheKey); | ||
@@ -241,3 +247,4 @@ if (cachedStats) { | ||
path = fs.resolve(path); | ||
const cacheKey = getCanonicalPath(path); | ||
// force throwIfNoEntry, as this function doesn't support it | ||
const cacheKey = getCanonicalPath(path) + suffixTrue; | ||
const cachedStats = statsCache.get(cacheKey); | ||
@@ -244,0 +251,0 @@ if (cachedStats) { |
{ | ||
"name": "@file-services/cached", | ||
"description": "A file system wrapper that adds cache to any `IFileSystem` implementation.", | ||
"version": "5.4.0", | ||
"version": "5.7.0", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"test": "yarn test:node && yarn test:browser", | ||
"test": "npm run test:node && npm run test:browser", | ||
"test:node": "mocha \"./dist/test/*.{spec,nodespec}.js\"", | ||
@@ -12,4 +12,4 @@ "test:browser": "mocha-play \"./dist/test/**/*.spec.js\"" | ||
"dependencies": { | ||
"@file-services/types": "^5.4.0", | ||
"@file-services/utils": "^5.4.0" | ||
"@file-services/types": "^5.7.0", | ||
"@file-services/utils": "^5.7.0" | ||
}, | ||
@@ -16,0 +16,0 @@ "files": [ |
@@ -13,3 +13,3 @@ # @file-services/cached | ||
```sh | ||
yarn add @file-services/cached | ||
npm i @file-services/cached | ||
``` | ||
@@ -16,0 +16,0 @@ |
@@ -31,9 +31,14 @@ import type { IFileSystem, IFileSystemStats, CallbackFnVoid } from '@file-services/types'; | ||
const getCanonicalPath = fs.caseSensitive ? identity : toLowerCase; | ||
const statsCache = new Map<string, ISuccessCacheResult<IFileSystemStats> | IFailureCacheResult>(); | ||
const statsCache = new Map<string, ISuccessCacheResult<IFileSystemStats | undefined> | IFailureCacheResult>(); | ||
const realpathCache = new Map<string, string>(); | ||
const { promises } = fs; | ||
const { promises, delimiter } = fs; | ||
const suffixTrue = delimiter + 'true'; | ||
const suffixFalse = delimiter + 'false'; | ||
const invalidateAbsolute = (absolutePath: string) => { | ||
const cachePath = getCanonicalPath(absolutePath); | ||
realpathCache.delete(cachePath); | ||
statsCache.delete(cachePath); | ||
statsCache.delete(cachePath + suffixTrue); | ||
statsCache.delete(cachePath + suffixFalse); | ||
}; | ||
@@ -130,5 +135,6 @@ const invalidateAbsoluteByPrefix = (absolutePath: string) => { | ||
}, | ||
statSync(path) { | ||
statSync(path, options) { | ||
path = fs.resolve(path); | ||
const cacheKey = getCanonicalPath(path); | ||
const throwIfNoEntry = options?.throwIfNoEntry ?? true; | ||
const cacheKey = getCanonicalPath(path) + (throwIfNoEntry ? suffixTrue : suffixFalse); | ||
const cachedStats = statsCache.get(cacheKey); | ||
@@ -139,8 +145,8 @@ if (cachedStats) { | ||
} | ||
return cachedStats.value; | ||
return cachedStats.value as IFileSystemStats; | ||
} | ||
try { | ||
const stats = fs.statSync(path); | ||
const stats = fs.statSync(path, options); | ||
statsCache.set(cacheKey, { kind: 'success', value: stats }); | ||
return stats; | ||
return stats as IFileSystemStats; | ||
} catch (e) { | ||
@@ -153,3 +159,4 @@ statsCache.set(cacheKey, { kind: 'failure', error: e as Error }); | ||
path = fs.resolve(path); | ||
const cacheKey = getCanonicalPath(path); | ||
// force throwIfNoEntry, as callback version doesn't support it | ||
const cacheKey = getCanonicalPath(path) + suffixTrue; | ||
const cachedStats = statsCache.get(cacheKey); | ||
@@ -160,3 +167,3 @@ if (cachedStats) { | ||
} else if (cachedStats.kind === 'success') { | ||
callback(null, cachedStats.value); | ||
callback(null, cachedStats.value as IFileSystemStats); | ||
} | ||
@@ -259,5 +266,6 @@ } else { | ||
}, | ||
async stat(path: string) { | ||
async stat(path) { | ||
path = fs.resolve(path); | ||
const cacheKey = getCanonicalPath(path); | ||
// force throwIfNoEntry, as this function doesn't support it | ||
const cacheKey = getCanonicalPath(path) + suffixTrue; | ||
const cachedStats = statsCache.get(cacheKey); | ||
@@ -268,3 +276,3 @@ if (cachedStats) { | ||
} | ||
return cachedStats.value; | ||
return cachedStats.value as IFileSystemStats; | ||
} | ||
@@ -271,0 +279,0 @@ try { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35820
577
Updated@file-services/types@^5.7.0
Updated@file-services/utils@^5.7.0