@file-services/cached
Advanced tools
Comparing version 2.2.0 to 3.0.0
@@ -1,2 +0,2 @@ | ||
import { IFileSystem, IFileSystemStats } from '@file-services/types'; | ||
import type { IFileSystem, IFileSystemStats } from '@file-services/types'; | ||
export interface ICachedFileSystem extends IFileSystem { | ||
@@ -3,0 +3,0 @@ /** |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createCachedFs = void 0; | ||
const utils_1 = require("@file-services/utils"); | ||
@@ -93,5 +94,5 @@ const identity = (val) => val; | ||
} | ||
catch (ex) { | ||
statsCache.set(cacheKey, { kind: 'failure', error: ex }); | ||
throw ex; | ||
catch (e) { | ||
statsCache.set(cacheKey, { kind: 'failure', error: e }); | ||
throw e; | ||
} | ||
@@ -122,3 +123,3 @@ }, | ||
}); | ||
} | ||
}, | ||
}), | ||
@@ -180,8 +181,8 @@ invalidate(path) { | ||
} | ||
catch (ex) { | ||
statsCache.set(cacheKey, { kind: 'failure', error: ex }); | ||
throw ex; | ||
catch (e) { | ||
statsCache.set(cacheKey, { kind: 'failure', error: e }); | ||
throw e; | ||
} | ||
} | ||
} | ||
}, | ||
}, | ||
}; | ||
@@ -188,0 +189,0 @@ } |
"use strict"; | ||
function __export(m) { | ||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; | ||
} | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __exportStar = (this && this.__exportStar) || function(m, exports) { | ||
for (var p in m) if (p !== "default" && !exports.hasOwnProperty(p)) __createBinding(exports, m, p); | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__export(require("./cached-fs")); | ||
__exportStar(require("./cached-fs"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
import { IFileSystem, IFileSystemStats } from '@file-services/types'; | ||
import type { IFileSystem, IFileSystemStats } from '@file-services/types'; | ||
export interface ICachedFileSystem extends IFileSystem { | ||
@@ -3,0 +3,0 @@ /** |
@@ -91,5 +91,5 @@ import { createFileSystem } from '@file-services/utils'; | ||
} | ||
catch (ex) { | ||
statsCache.set(cacheKey, { kind: 'failure', error: ex }); | ||
throw ex; | ||
catch (e) { | ||
statsCache.set(cacheKey, { kind: 'failure', error: e }); | ||
throw e; | ||
} | ||
@@ -120,3 +120,3 @@ }, | ||
}); | ||
} | ||
}, | ||
}), | ||
@@ -178,10 +178,10 @@ invalidate(path) { | ||
} | ||
catch (ex) { | ||
statsCache.set(cacheKey, { kind: 'failure', error: ex }); | ||
throw ex; | ||
catch (e) { | ||
statsCache.set(cacheKey, { kind: 'failure', error: e }); | ||
throw e; | ||
} | ||
} | ||
} | ||
}, | ||
}, | ||
}; | ||
} | ||
//# sourceMappingURL=cached-fs.js.map |
{ | ||
"name": "@file-services/cached", | ||
"description": "A file system wrapper that adds cache to any `IFileSystem` implementation.", | ||
"version": "2.2.0", | ||
"version": "3.0.0", | ||
"main": "cjs/index.js", | ||
@@ -17,4 +17,4 @@ "module": "esm/index.js", | ||
"dependencies": { | ||
"@file-services/types": "^2.2.0", | ||
"@file-services/utils": "^2.2.0" | ||
"@file-services/types": "^3.0.0", | ||
"@file-services/utils": "^3.0.0" | ||
}, | ||
@@ -21,0 +21,0 @@ "files": [ |
@@ -1,2 +0,2 @@ | ||
import { IFileSystem, IFileSystemStats, CallbackFnVoid } from '@file-services/types'; | ||
import type { IFileSystem, IFileSystemStats, CallbackFnVoid } from '@file-services/types'; | ||
import { createFileSystem } from '@file-services/utils'; | ||
@@ -8,200 +8,200 @@ | ||
export interface ICachedFileSystem extends IFileSystem { | ||
/** | ||
* | ||
* @param path the file path to clear from the cache | ||
*/ | ||
invalidate(path: string): void; | ||
/** | ||
* invalidates all files | ||
*/ | ||
invalidateAll(): void; | ||
/** | ||
* | ||
* @param path the file path to clear from the cache | ||
*/ | ||
invalidate(path: string): void; | ||
/** | ||
* invalidates all files | ||
*/ | ||
invalidateAll(): void; | ||
} | ||
export interface ISuccessCacheResult { | ||
kind: 'success'; | ||
stats: IFileSystemStats; | ||
kind: 'success'; | ||
stats: IFileSystemStats; | ||
} | ||
export interface IFailureCacheResult { | ||
kind: 'failure'; | ||
error: Error; | ||
kind: 'failure'; | ||
error: Error; | ||
} | ||
export function createCachedFs(fs: IFileSystem): ICachedFileSystem { | ||
const getCanonicalPath = fs.caseSensitive ? identity : toLowerCase; | ||
const statsCache = new Map<string, ISuccessCacheResult | IFailureCacheResult>(); | ||
const { promises } = fs; | ||
const invalidateAbsolute = (absolutePath: string) => statsCache.delete(getCanonicalPath(absolutePath)); | ||
const getCanonicalPath = fs.caseSensitive ? identity : toLowerCase; | ||
const statsCache = new Map<string, ISuccessCacheResult | IFailureCacheResult>(); | ||
const { promises } = fs; | ||
const invalidateAbsolute = (absolutePath: string) => statsCache.delete(getCanonicalPath(absolutePath)); | ||
return { | ||
...createFileSystem({ | ||
...fs, | ||
copyFile: function copyFile(sourcePath: string, destinationPath: string, ...args: [CallbackFnVoid]) { | ||
destinationPath = fs.resolve(destinationPath); | ||
invalidateAbsolute(destinationPath); | ||
return fs.copyFile(sourcePath, destinationPath, ...args); | ||
} as IFileSystem['copyFile'], | ||
copyFileSync(sourcePath, destinationPath, ...args) { | ||
destinationPath = fs.resolve(destinationPath); | ||
invalidateAbsolute(destinationPath); | ||
return fs.copyFileSync(sourcePath, destinationPath, ...args); | ||
}, | ||
mkdir(directoryPath, callback) { | ||
directoryPath = fs.resolve(directoryPath); | ||
invalidateAbsolute(directoryPath); | ||
return fs.mkdir(directoryPath, callback); | ||
}, | ||
mkdirSync(directoryPath) { | ||
directoryPath = fs.resolve(directoryPath); | ||
invalidateAbsolute(directoryPath); | ||
return fs.mkdirSync(directoryPath); | ||
}, | ||
rename(sourcePath, destinationPath, callback) { | ||
sourcePath = fs.resolve(sourcePath); | ||
destinationPath = fs.resolve(destinationPath); | ||
invalidateAbsolute(sourcePath); | ||
invalidateAbsolute(destinationPath); | ||
return fs.rename(sourcePath, destinationPath, callback); | ||
}, | ||
renameSync(sourcePath, destinationPath) { | ||
sourcePath = fs.resolve(sourcePath); | ||
destinationPath = fs.resolve(destinationPath); | ||
invalidateAbsolute(sourcePath); | ||
invalidateAbsolute(destinationPath); | ||
return fs.renameSync(sourcePath, destinationPath); | ||
}, | ||
rmdir(directoryPath, callback) { | ||
directoryPath = fs.resolve(directoryPath); | ||
invalidateAbsolute(directoryPath); | ||
return fs.rmdir(directoryPath, callback); | ||
}, | ||
rmdirSync(directoryPath) { | ||
directoryPath = fs.resolve(directoryPath); | ||
invalidateAbsolute(directoryPath); | ||
return fs.rmdirSync(directoryPath); | ||
}, | ||
unlink(filePath, callback) { | ||
filePath = fs.resolve(filePath); | ||
invalidateAbsolute(filePath); | ||
return fs.unlink(filePath, callback); | ||
}, | ||
unlinkSync(filePath) { | ||
filePath = fs.resolve(filePath); | ||
invalidateAbsolute(filePath); | ||
return fs.unlinkSync(filePath); | ||
}, | ||
writeFile: function writeFile(filePath: string, ...args: [string, CallbackFnVoid]) { | ||
filePath = fs.resolve(filePath); | ||
invalidateAbsolute(filePath); | ||
return fs.writeFile(filePath, ...args); | ||
} as IFileSystem['writeFile'], | ||
writeFileSync(filePath, ...args) { | ||
filePath = fs.resolve(filePath); | ||
invalidateAbsolute(filePath); | ||
return fs.writeFileSync(filePath, ...args); | ||
}, | ||
statSync(path) { | ||
path = fs.resolve(path); | ||
const cacheKey = getCanonicalPath(path); | ||
const cachedStats = statsCache.get(cacheKey); | ||
if (cachedStats) { | ||
if (cachedStats.kind === 'failure') { | ||
throw cachedStats.error; | ||
} | ||
return cachedStats.stats; | ||
} | ||
try { | ||
const stats = fs.statSync(path); | ||
statsCache.set(cacheKey, { kind: 'success', stats }); | ||
return stats; | ||
} catch (ex) { | ||
statsCache.set(cacheKey, { kind: 'failure', error: ex }); | ||
throw ex; | ||
} | ||
}, | ||
stat(path, callback) { | ||
path = fs.resolve(path); | ||
const cacheKey = getCanonicalPath(path); | ||
const cachedStats = statsCache.get(cacheKey); | ||
if (cachedStats) { | ||
if (cachedStats.kind === 'failure') { | ||
(callback as (e: Error) => void)(cachedStats.error); | ||
} else if (cachedStats.kind === 'success') { | ||
callback(undefined, cachedStats.stats); | ||
} | ||
return; | ||
} | ||
fs.stat(path, (error, stats) => { | ||
if (error) { | ||
statsCache.set(cacheKey, { kind: 'failure', error }); | ||
} else if (stats) { | ||
statsCache.set(cacheKey, { kind: 'success', stats }); | ||
} | ||
return { | ||
...createFileSystem({ | ||
...fs, | ||
copyFile: function copyFile(sourcePath: string, destinationPath: string, ...args: [CallbackFnVoid]) { | ||
destinationPath = fs.resolve(destinationPath); | ||
invalidateAbsolute(destinationPath); | ||
return fs.copyFile(sourcePath, destinationPath, ...args); | ||
} as IFileSystem['copyFile'], | ||
copyFileSync(sourcePath, destinationPath, ...args) { | ||
destinationPath = fs.resolve(destinationPath); | ||
invalidateAbsolute(destinationPath); | ||
return fs.copyFileSync(sourcePath, destinationPath, ...args); | ||
}, | ||
mkdir(directoryPath, callback) { | ||
directoryPath = fs.resolve(directoryPath); | ||
invalidateAbsolute(directoryPath); | ||
return fs.mkdir(directoryPath, callback); | ||
}, | ||
mkdirSync(directoryPath) { | ||
directoryPath = fs.resolve(directoryPath); | ||
invalidateAbsolute(directoryPath); | ||
return fs.mkdirSync(directoryPath); | ||
}, | ||
rename(sourcePath, destinationPath, callback) { | ||
sourcePath = fs.resolve(sourcePath); | ||
destinationPath = fs.resolve(destinationPath); | ||
invalidateAbsolute(sourcePath); | ||
invalidateAbsolute(destinationPath); | ||
return fs.rename(sourcePath, destinationPath, callback); | ||
}, | ||
renameSync(sourcePath, destinationPath) { | ||
sourcePath = fs.resolve(sourcePath); | ||
destinationPath = fs.resolve(destinationPath); | ||
invalidateAbsolute(sourcePath); | ||
invalidateAbsolute(destinationPath); | ||
return fs.renameSync(sourcePath, destinationPath); | ||
}, | ||
rmdir(directoryPath, callback) { | ||
directoryPath = fs.resolve(directoryPath); | ||
invalidateAbsolute(directoryPath); | ||
return fs.rmdir(directoryPath, callback); | ||
}, | ||
rmdirSync(directoryPath) { | ||
directoryPath = fs.resolve(directoryPath); | ||
invalidateAbsolute(directoryPath); | ||
return fs.rmdirSync(directoryPath); | ||
}, | ||
unlink(filePath, callback) { | ||
filePath = fs.resolve(filePath); | ||
invalidateAbsolute(filePath); | ||
return fs.unlink(filePath, callback); | ||
}, | ||
unlinkSync(filePath) { | ||
filePath = fs.resolve(filePath); | ||
invalidateAbsolute(filePath); | ||
return fs.unlinkSync(filePath); | ||
}, | ||
writeFile: function writeFile(filePath: string, ...args: [string, CallbackFnVoid]) { | ||
filePath = fs.resolve(filePath); | ||
invalidateAbsolute(filePath); | ||
return fs.writeFile(filePath, ...args); | ||
} as IFileSystem['writeFile'], | ||
writeFileSync(filePath, ...args) { | ||
filePath = fs.resolve(filePath); | ||
invalidateAbsolute(filePath); | ||
return fs.writeFileSync(filePath, ...args); | ||
}, | ||
statSync(path) { | ||
path = fs.resolve(path); | ||
const cacheKey = getCanonicalPath(path); | ||
const cachedStats = statsCache.get(cacheKey); | ||
if (cachedStats) { | ||
if (cachedStats.kind === 'failure') { | ||
throw cachedStats.error; | ||
} | ||
return cachedStats.stats; | ||
} | ||
try { | ||
const stats = fs.statSync(path); | ||
statsCache.set(cacheKey, { kind: 'success', stats }); | ||
return stats; | ||
} catch (e) { | ||
statsCache.set(cacheKey, { kind: 'failure', error: e as Error }); | ||
throw e; | ||
} | ||
}, | ||
stat(path, callback) { | ||
path = fs.resolve(path); | ||
const cacheKey = getCanonicalPath(path); | ||
const cachedStats = statsCache.get(cacheKey); | ||
if (cachedStats) { | ||
if (cachedStats.kind === 'failure') { | ||
(callback as (e: Error) => void)(cachedStats.error); | ||
} else if (cachedStats.kind === 'success') { | ||
callback(undefined, cachedStats.stats); | ||
} | ||
return; | ||
} | ||
fs.stat(path, (error, stats) => { | ||
if (error) { | ||
statsCache.set(cacheKey, { kind: 'failure', error }); | ||
} else if (stats) { | ||
statsCache.set(cacheKey, { kind: 'success', stats }); | ||
} | ||
callback(error, stats); | ||
return; | ||
}); | ||
} | ||
}), | ||
invalidate(path) { | ||
return invalidateAbsolute(fs.resolve(path)); | ||
}, | ||
invalidateAll() { | ||
statsCache.clear(); | ||
}, | ||
promises: { | ||
...promises, | ||
copyFile(sourcePath, destinationPath, ...args) { | ||
destinationPath = fs.resolve(destinationPath); | ||
invalidateAbsolute(destinationPath); | ||
return promises.copyFile(sourcePath, destinationPath, ...args); | ||
}, | ||
mkdir(directoryPath) { | ||
directoryPath = fs.resolve(directoryPath); | ||
invalidateAbsolute(directoryPath); | ||
return promises.mkdir(directoryPath); | ||
}, | ||
rename(sourcePath, destinationPath) { | ||
sourcePath = fs.resolve(sourcePath); | ||
destinationPath = fs.resolve(destinationPath); | ||
invalidateAbsolute(sourcePath); | ||
invalidateAbsolute(destinationPath); | ||
return promises.rename(sourcePath, destinationPath); | ||
}, | ||
rmdir(directoryPath) { | ||
directoryPath = fs.resolve(directoryPath); | ||
invalidateAbsolute(directoryPath); | ||
return promises.rmdir(directoryPath); | ||
}, | ||
unlink(filePath) { | ||
filePath = fs.resolve(filePath); | ||
invalidateAbsolute(filePath); | ||
return promises.unlink(filePath); | ||
}, | ||
writeFile(filePath, ...args) { | ||
filePath = fs.resolve(filePath); | ||
invalidateAbsolute(filePath); | ||
return promises.writeFile(filePath, ...args); | ||
}, | ||
async stat(path: string) { | ||
path = fs.resolve(path); | ||
const cacheKey = getCanonicalPath(path); | ||
const cachedStats = statsCache.get(cacheKey); | ||
if (cachedStats) { | ||
if (cachedStats.kind === 'failure') { | ||
throw cachedStats.error; | ||
} | ||
return cachedStats.stats; | ||
} | ||
try { | ||
const stats = await promises.stat(path); | ||
statsCache.set(cacheKey, { kind: 'success', stats }); | ||
return stats; | ||
} catch (ex) { | ||
statsCache.set(cacheKey, { kind: 'failure', error: ex }); | ||
throw ex; | ||
} | ||
} | ||
callback(error, stats); | ||
return; | ||
}); | ||
}, | ||
}), | ||
invalidate(path) { | ||
return invalidateAbsolute(fs.resolve(path)); | ||
}, | ||
invalidateAll() { | ||
statsCache.clear(); | ||
}, | ||
promises: { | ||
...promises, | ||
copyFile(sourcePath, destinationPath, ...args) { | ||
destinationPath = fs.resolve(destinationPath); | ||
invalidateAbsolute(destinationPath); | ||
return promises.copyFile(sourcePath, destinationPath, ...args); | ||
}, | ||
mkdir(directoryPath) { | ||
directoryPath = fs.resolve(directoryPath); | ||
invalidateAbsolute(directoryPath); | ||
return promises.mkdir(directoryPath); | ||
}, | ||
rename(sourcePath, destinationPath) { | ||
sourcePath = fs.resolve(sourcePath); | ||
destinationPath = fs.resolve(destinationPath); | ||
invalidateAbsolute(sourcePath); | ||
invalidateAbsolute(destinationPath); | ||
return promises.rename(sourcePath, destinationPath); | ||
}, | ||
rmdir(directoryPath) { | ||
directoryPath = fs.resolve(directoryPath); | ||
invalidateAbsolute(directoryPath); | ||
return promises.rmdir(directoryPath); | ||
}, | ||
unlink(filePath) { | ||
filePath = fs.resolve(filePath); | ||
invalidateAbsolute(filePath); | ||
return promises.unlink(filePath); | ||
}, | ||
writeFile(filePath, ...args) { | ||
filePath = fs.resolve(filePath); | ||
invalidateAbsolute(filePath); | ||
return promises.writeFile(filePath, ...args); | ||
}, | ||
async stat(path: string) { | ||
path = fs.resolve(path); | ||
const cacheKey = getCanonicalPath(path); | ||
const cachedStats = statsCache.get(cacheKey); | ||
if (cachedStats) { | ||
if (cachedStats.kind === 'failure') { | ||
throw cachedStats.error; | ||
} | ||
return cachedStats.stats; | ||
} | ||
}; | ||
try { | ||
const stats = await promises.stat(path); | ||
statsCache.set(cacheKey, { kind: 'success', stats }); | ||
return stats; | ||
} catch (e) { | ||
statsCache.set(cacheKey, { kind: 'failure', error: e as Error }); | ||
throw e; | ||
} | ||
}, | ||
}, | ||
}; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
628
0
42381
+ Added@file-services/types@3.1.1(transitive)
+ Added@file-services/utils@3.1.1(transitive)
- Removed@file-services/types@2.2.0(transitive)
- Removed@file-services/utils@2.2.0(transitive)
Updated@file-services/types@^3.0.0
Updated@file-services/utils@^3.0.0