file-entry-cache 11.0.0 → 11.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.
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var k=Object.create;var u=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var v=Object.getPrototypeOf,z=Object.prototype.hasOwnProperty;var D=(o,e)=>{for(var s in e)u(o,s,{get:e[s],enumerable:!0})},F=(o,e,s,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let t of A(e))!z.call(o,t)&&t!==s&&u(o,t,{get:()=>e[t],enumerable:!(i=w(e,t))||i.enumerable});return o};var p=(o,e,s)=>(s=o!=null?k(v(o)):{},F(e||!o||!o.__esModule?u(s,"default",{value:o,enumerable:!0}):s,o)),T=o=>F(u({},"__esModule",{value:!0}),o);var K={};D(K,{FileEntryCache:()=>m,create:()=>f,createFromFile:()=>C,default:()=>d});module.exports=T(K);var _=p(require("crypto"),1),g=p(require("fs"),1),c=p(require("path"),1),h=require("flat-cache");function C(o,e){let s=c.default.basename(o),i=c.default.dirname(o);return f(s,i,e)}function f(o,e,s){let i={...s,cache:{cacheId:o,cacheDir:e}},t=new m(i);if(e){let r=`${e}/${o}`;g.default.existsSync(r)&&(t.cache=(0,h.createFromFile)(r,i.cache))}return t}var d=class{static create=f;static createFromFile=C},m=class{_cache=new h.FlatCache({useClone:!1});_useCheckSum=!1;_hashAlgorithm="md5";_cwd=process.cwd();_restrictAccessToCwd=!1;_logger;_useAbsolutePathAsKey=!1;_useModifiedTime=!0;constructor(e){e?.cache&&(this._cache=new h.FlatCache(e.cache)),e?.useCheckSum&&(this._useCheckSum=e.useCheckSum),e?.hashAlgorithm&&(this._hashAlgorithm=e.hashAlgorithm),e?.cwd&&(this._cwd=e.cwd),e?.useModifiedTime!==void 0&&(this._useModifiedTime=e.useModifiedTime),e?.restrictAccessToCwd!==void 0&&(this._restrictAccessToCwd=e.restrictAccessToCwd),e?.useAbsolutePathAsKey!==void 0&&(this._useAbsolutePathAsKey=e.useAbsolutePathAsKey),e?.logger&&(this._logger=e.logger)}get cache(){return this._cache}set cache(e){this._cache=e}get logger(){return this._logger}set logger(e){this._logger=e}get useCheckSum(){return this._useCheckSum}set useCheckSum(e){this._useCheckSum=e}get hashAlgorithm(){return this._hashAlgorithm}set hashAlgorithm(e){this._hashAlgorithm=e}get cwd(){return this._cwd}set cwd(e){this._cwd=e}get useModifiedTime(){return this._useModifiedTime}set useModifiedTime(e){this._useModifiedTime=e}get restrictAccessToCwd(){return this._restrictAccessToCwd}set restrictAccessToCwd(e){this._restrictAccessToCwd=e}get useAbsolutePathAsKey(){return this._useAbsolutePathAsKey}set useAbsolutePathAsKey(e){this._useAbsolutePathAsKey=e}getHash(e){return _.default.createHash(this._hashAlgorithm).update(e).digest("hex")}createFileKey(e){let s=e;return this._useAbsolutePathAsKey&&this.isRelativePath(e)&&(s=this.getAbsolutePathWithCwd(e,this._cwd)),s}isRelativePath(e){return!c.default.isAbsolute(e)}deleteCacheFile(){return this._cache.removeCacheFile()}destroy(){this._cache.destroy()}removeEntry(e){let s=this.createFileKey(e);this._cache.removeKey(s)}reconcile(){let{items:e}=this._cache;for(let s of e)this.getFileDescriptor(s.key).notFound&&this._cache.removeKey(s.key);this._cache.save()}hasFileChanged(e){let s=!1,i=this.getFileDescriptor(e);return(!i.err||!i.notFound)&&i.changed&&(s=!0),s}getFileDescriptor(e,s){this._logger?.debug({filePath:e,options:s},"Getting file descriptor");let i,t={key:this.createFileKey(e),changed:!1,meta:{}};this._logger?.trace({key:t.key},"Created file key");let r=this._cache.getKey(t.key);r?this._logger?.trace({metaCache:r},"Found cached meta"):this._logger?.trace("No cached meta found"),t.meta=r?{...r}:{};let n=this.getAbsolutePath(e);this._logger?.trace({absolutePath:n},"Resolved absolute path");let l=s?.useCheckSum??this._useCheckSum;this._logger?.debug({useCheckSum:l},"Using checksum setting");let b=s?.useModifiedTime??this.useModifiedTime;this._logger?.debug({useModifiedTime:b},"Using modified time (mtime) setting");try{if(i=g.default.statSync(n),t.meta.size=i.size,t.meta.mtime=i.mtime.getTime(),this._logger?.trace({size:t.meta.size,mtime:t.meta.mtime},"Read file stats"),l){let a=g.default.readFileSync(n);t.meta.hash=this.getHash(a),this._logger?.trace({hash:t.meta.hash},"Calculated file hash")}}catch(a){this._logger?.error({filePath:e,error:a},"Error reading file"),this.removeEntry(e);let y=!1;return a.message.includes("ENOENT")&&(y=!0,this._logger?.debug({filePath:e},"File not found")),{key:t.key,err:a,notFound:y,meta:{}}}return r?(b&&r?.mtime!==t.meta?.mtime&&(t.changed=!0,this._logger?.debug({filePath:e,oldMtime:r.mtime,newMtime:t.meta.mtime},"File changed: mtime differs")),r?.size!==t.meta?.size&&(t.changed=!0,this._logger?.debug({filePath:e,oldSize:r.size,newSize:t.meta.size},"File changed: size differs")),l&&r?.hash!==t.meta?.hash&&(t.changed=!0,this._logger?.debug({filePath:e,oldHash:r.hash,newHash:t.meta.hash},"File changed: hash differs")),this._cache.setKey(t.key,t.meta),t.changed?this._logger?.info({filePath:e},"File has changed"):this._logger?.debug({filePath:e},"File unchanged"),t):(t.changed=!0,this._cache.setKey(t.key,t.meta),this._logger?.debug({filePath:e},"File not in cache, marked as changed"),t)}normalizeEntries(e){let s=[];if(e){for(let t of e){let r=this.getFileDescriptor(t);s.push(r)}return s}let i=this.cache.keys();for(let t of i){let r=this.getFileDescriptor(t);!r.notFound&&!r.err&&s.push(r)}return s}analyzeFiles(e){let s={changedFiles:[],notFoundFiles:[],notChangedFiles:[]},i=this.normalizeEntries(e);for(let t of i)t.notFound?s.notFoundFiles.push(t.key):t.changed?s.changedFiles.push(t.key):s.notChangedFiles.push(t.key);return s}getUpdatedFiles(e){let s=[],i=this.normalizeEntries(e);for(let t of i)t.changed&&s.push(t.key);return s}getFileDescriptorsByPath(e){let s=[],i=this._cache.keys();for(let t of i)if(t.startsWith(e)){let r=this.getFileDescriptor(t);s.push(r)}return s}getAbsolutePath(e){if(this.isRelativePath(e)){let s=e.replace(/\0/g,""),i=c.default.resolve(this._cwd,s);if(this._restrictAccessToCwd){let t=c.default.normalize(i),r=c.default.normalize(this._cwd);if(!(t===r||t.startsWith(r+c.default.sep)))throw new Error(`Path traversal attempt blocked: "${e}" resolves outside of working directory "${this._cwd}"`)}return i}return e}getAbsolutePathWithCwd(e,s){if(this.isRelativePath(e)){let i=e.replace(/\0/g,""),t=c.default.resolve(s,i);if(this._restrictAccessToCwd){let r=c.default.normalize(t),n=c.default.normalize(s);if(!(r===n||r.startsWith(n+c.default.sep)))throw new Error(`Path traversal attempt blocked: "${e}" resolves outside of working directory "${s}"`)}return t}return e}renameCacheKeys(e,s){let i=this._cache.keys();for(let t of i)if(t.startsWith(e)){let r=t.replace(e,s),n=this._cache.getKey(t);this._cache.removeKey(t),this._cache.setKey(r,n)}}};0&&(module.exports={FileEntryCache,create,createFromFile});
|
package/dist/index.d.cts
CHANGED
|
@@ -38,6 +38,8 @@ type FileEntryCacheOptions = {
|
|
|
38
38
|
type GetFileDescriptorOptions = {
|
|
39
39
|
/** Whether to use checksum for this specific file check instead of modified time (mtime) (overrides instance setting) */
|
|
40
40
|
useCheckSum?: boolean;
|
|
41
|
+
/** Whether to use file modified time for change detection (default: true) */
|
|
42
|
+
useModifiedTime?: boolean;
|
|
41
43
|
};
|
|
42
44
|
type FileDescriptor = {
|
|
43
45
|
/** The cache key for this file (typically the file path) */
|
|
@@ -99,6 +101,7 @@ declare class FileEntryCache {
|
|
|
99
101
|
private _restrictAccessToCwd;
|
|
100
102
|
private _logger?;
|
|
101
103
|
private _useAbsolutePathAsKey;
|
|
104
|
+
private _useModifiedTime;
|
|
102
105
|
/**
|
|
103
106
|
* Create a new FileEntryCache instance
|
|
104
107
|
* @param options - The options for the FileEntryCache (all properties are optional with defaults)
|
|
@@ -154,6 +157,16 @@ declare class FileEntryCache {
|
|
|
154
157
|
* @param {string} value - The value to set
|
|
155
158
|
*/
|
|
156
159
|
set cwd(value: string);
|
|
160
|
+
/**
|
|
161
|
+
* Get whether to use modified time for change detection
|
|
162
|
+
* @returns {boolean} Whether modified time (mtime) is used for change detection (default: true)
|
|
163
|
+
*/
|
|
164
|
+
get useModifiedTime(): boolean;
|
|
165
|
+
/**
|
|
166
|
+
* Set whether to use modified time for change detection
|
|
167
|
+
* @param {boolean} value - The value to set
|
|
168
|
+
*/
|
|
169
|
+
set useModifiedTime(value: boolean);
|
|
157
170
|
/**
|
|
158
171
|
* Get whether to restrict paths to cwd boundaries
|
|
159
172
|
* @returns {boolean} Whether strict path checking is enabled (default: true)
|
package/dist/index.d.ts
CHANGED
|
@@ -38,6 +38,8 @@ type FileEntryCacheOptions = {
|
|
|
38
38
|
type GetFileDescriptorOptions = {
|
|
39
39
|
/** Whether to use checksum for this specific file check instead of modified time (mtime) (overrides instance setting) */
|
|
40
40
|
useCheckSum?: boolean;
|
|
41
|
+
/** Whether to use file modified time for change detection (default: true) */
|
|
42
|
+
useModifiedTime?: boolean;
|
|
41
43
|
};
|
|
42
44
|
type FileDescriptor = {
|
|
43
45
|
/** The cache key for this file (typically the file path) */
|
|
@@ -99,6 +101,7 @@ declare class FileEntryCache {
|
|
|
99
101
|
private _restrictAccessToCwd;
|
|
100
102
|
private _logger?;
|
|
101
103
|
private _useAbsolutePathAsKey;
|
|
104
|
+
private _useModifiedTime;
|
|
102
105
|
/**
|
|
103
106
|
* Create a new FileEntryCache instance
|
|
104
107
|
* @param options - The options for the FileEntryCache (all properties are optional with defaults)
|
|
@@ -154,6 +157,16 @@ declare class FileEntryCache {
|
|
|
154
157
|
* @param {string} value - The value to set
|
|
155
158
|
*/
|
|
156
159
|
set cwd(value: string);
|
|
160
|
+
/**
|
|
161
|
+
* Get whether to use modified time for change detection
|
|
162
|
+
* @returns {boolean} Whether modified time (mtime) is used for change detection (default: true)
|
|
163
|
+
*/
|
|
164
|
+
get useModifiedTime(): boolean;
|
|
165
|
+
/**
|
|
166
|
+
* Set whether to use modified time for change detection
|
|
167
|
+
* @param {boolean} value - The value to set
|
|
168
|
+
*/
|
|
169
|
+
set useModifiedTime(value: boolean);
|
|
157
170
|
/**
|
|
158
171
|
* Get whether to restrict paths to cwd boundaries
|
|
159
172
|
* @returns {boolean} Whether strict path checking is enabled (default: true)
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import
|
|
1
|
+
import b from"crypto";import l from"fs";import o from"path";import{createFromFile as y,FlatCache as p}from"flat-cache";function F(n,e){let s=o.basename(n),r=o.dirname(n);return f(s,r,e)}function f(n,e,s){let r={...s,cache:{cacheId:n,cacheDir:e}},t=new g(r);if(e){let i=`${e}/${n}`;l.existsSync(i)&&(t.cache=y(i,r.cache))}return t}var u=class{static create=f;static createFromFile=F},g=class{_cache=new p({useClone:!1});_useCheckSum=!1;_hashAlgorithm="md5";_cwd=process.cwd();_restrictAccessToCwd=!1;_logger;_useAbsolutePathAsKey=!1;_useModifiedTime=!0;constructor(e){e?.cache&&(this._cache=new p(e.cache)),e?.useCheckSum&&(this._useCheckSum=e.useCheckSum),e?.hashAlgorithm&&(this._hashAlgorithm=e.hashAlgorithm),e?.cwd&&(this._cwd=e.cwd),e?.useModifiedTime!==void 0&&(this._useModifiedTime=e.useModifiedTime),e?.restrictAccessToCwd!==void 0&&(this._restrictAccessToCwd=e.restrictAccessToCwd),e?.useAbsolutePathAsKey!==void 0&&(this._useAbsolutePathAsKey=e.useAbsolutePathAsKey),e?.logger&&(this._logger=e.logger)}get cache(){return this._cache}set cache(e){this._cache=e}get logger(){return this._logger}set logger(e){this._logger=e}get useCheckSum(){return this._useCheckSum}set useCheckSum(e){this._useCheckSum=e}get hashAlgorithm(){return this._hashAlgorithm}set hashAlgorithm(e){this._hashAlgorithm=e}get cwd(){return this._cwd}set cwd(e){this._cwd=e}get useModifiedTime(){return this._useModifiedTime}set useModifiedTime(e){this._useModifiedTime=e}get restrictAccessToCwd(){return this._restrictAccessToCwd}set restrictAccessToCwd(e){this._restrictAccessToCwd=e}get useAbsolutePathAsKey(){return this._useAbsolutePathAsKey}set useAbsolutePathAsKey(e){this._useAbsolutePathAsKey=e}getHash(e){return b.createHash(this._hashAlgorithm).update(e).digest("hex")}createFileKey(e){let s=e;return this._useAbsolutePathAsKey&&this.isRelativePath(e)&&(s=this.getAbsolutePathWithCwd(e,this._cwd)),s}isRelativePath(e){return!o.isAbsolute(e)}deleteCacheFile(){return this._cache.removeCacheFile()}destroy(){this._cache.destroy()}removeEntry(e){let s=this.createFileKey(e);this._cache.removeKey(s)}reconcile(){let{items:e}=this._cache;for(let s of e)this.getFileDescriptor(s.key).notFound&&this._cache.removeKey(s.key);this._cache.save()}hasFileChanged(e){let s=!1,r=this.getFileDescriptor(e);return(!r.err||!r.notFound)&&r.changed&&(s=!0),s}getFileDescriptor(e,s){this._logger?.debug({filePath:e,options:s},"Getting file descriptor");let r,t={key:this.createFileKey(e),changed:!1,meta:{}};this._logger?.trace({key:t.key},"Created file key");let i=this._cache.getKey(t.key);i?this._logger?.trace({metaCache:i},"Found cached meta"):this._logger?.trace("No cached meta found"),t.meta=i?{...i}:{};let c=this.getAbsolutePath(e);this._logger?.trace({absolutePath:c},"Resolved absolute path");let h=s?.useCheckSum??this._useCheckSum;this._logger?.debug({useCheckSum:h},"Using checksum setting");let d=s?.useModifiedTime??this.useModifiedTime;this._logger?.debug({useModifiedTime:d},"Using modified time (mtime) setting");try{if(r=l.statSync(c),t.meta.size=r.size,t.meta.mtime=r.mtime.getTime(),this._logger?.trace({size:t.meta.size,mtime:t.meta.mtime},"Read file stats"),h){let a=l.readFileSync(c);t.meta.hash=this.getHash(a),this._logger?.trace({hash:t.meta.hash},"Calculated file hash")}}catch(a){this._logger?.error({filePath:e,error:a},"Error reading file"),this.removeEntry(e);let m=!1;return a.message.includes("ENOENT")&&(m=!0,this._logger?.debug({filePath:e},"File not found")),{key:t.key,err:a,notFound:m,meta:{}}}return i?(d&&i?.mtime!==t.meta?.mtime&&(t.changed=!0,this._logger?.debug({filePath:e,oldMtime:i.mtime,newMtime:t.meta.mtime},"File changed: mtime differs")),i?.size!==t.meta?.size&&(t.changed=!0,this._logger?.debug({filePath:e,oldSize:i.size,newSize:t.meta.size},"File changed: size differs")),h&&i?.hash!==t.meta?.hash&&(t.changed=!0,this._logger?.debug({filePath:e,oldHash:i.hash,newHash:t.meta.hash},"File changed: hash differs")),this._cache.setKey(t.key,t.meta),t.changed?this._logger?.info({filePath:e},"File has changed"):this._logger?.debug({filePath:e},"File unchanged"),t):(t.changed=!0,this._cache.setKey(t.key,t.meta),this._logger?.debug({filePath:e},"File not in cache, marked as changed"),t)}normalizeEntries(e){let s=[];if(e){for(let t of e){let i=this.getFileDescriptor(t);s.push(i)}return s}let r=this.cache.keys();for(let t of r){let i=this.getFileDescriptor(t);!i.notFound&&!i.err&&s.push(i)}return s}analyzeFiles(e){let s={changedFiles:[],notFoundFiles:[],notChangedFiles:[]},r=this.normalizeEntries(e);for(let t of r)t.notFound?s.notFoundFiles.push(t.key):t.changed?s.changedFiles.push(t.key):s.notChangedFiles.push(t.key);return s}getUpdatedFiles(e){let s=[],r=this.normalizeEntries(e);for(let t of r)t.changed&&s.push(t.key);return s}getFileDescriptorsByPath(e){let s=[],r=this._cache.keys();for(let t of r)if(t.startsWith(e)){let i=this.getFileDescriptor(t);s.push(i)}return s}getAbsolutePath(e){if(this.isRelativePath(e)){let s=e.replace(/\0/g,""),r=o.resolve(this._cwd,s);if(this._restrictAccessToCwd){let t=o.normalize(r),i=o.normalize(this._cwd);if(!(t===i||t.startsWith(i+o.sep)))throw new Error(`Path traversal attempt blocked: "${e}" resolves outside of working directory "${this._cwd}"`)}return r}return e}getAbsolutePathWithCwd(e,s){if(this.isRelativePath(e)){let r=e.replace(/\0/g,""),t=o.resolve(s,r);if(this._restrictAccessToCwd){let i=o.normalize(t),c=o.normalize(s);if(!(i===c||i.startsWith(c+o.sep)))throw new Error(`Path traversal attempt blocked: "${e}" resolves outside of working directory "${s}"`)}return t}return e}renameCacheKeys(e,s){let r=this._cache.keys();for(let t of r)if(t.startsWith(e)){let i=t.replace(e,s),c=this._cache.getKey(t);this._cache.removeKey(t),this._cache.setKey(i,c)}}};export{g as FileEntryCache,f as create,F as createFromFile,u as default};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "file-entry-cache",
|
|
3
|
-
"version": "11.
|
|
3
|
+
"version": "11.1.0",
|
|
4
4
|
"description": "A lightweight cache for file metadata, ideal for processes that work on a specific set of files and only need to reprocess files that have changed since the last run",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|