file-entry-cache 11.1.2 → 11.1.5

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.
@@ -0,0 +1,290 @@
1
+ import { FlatCache, FlatCacheOptions } from "flat-cache";
2
+ import { Buffer } from "node:buffer";
3
+
4
+ //#region src/index.d.ts
5
+ type ILogger = {
6
+ /** Current log level */level?: string; /** Trace level logging */
7
+ trace: (message: string | object, ...args: unknown[]) => void; /** Debug level logging */
8
+ debug: (message: string | object, ...args: unknown[]) => void; /** Info level logging */
9
+ info: (message: string | object, ...args: unknown[]) => void; /** Warning level logging */
10
+ warn: (message: string | object, ...args: unknown[]) => void; /** Error level logging */
11
+ error: (message: string | object, ...args: unknown[]) => void; /** Fatal level logging */
12
+ fatal: (message: string | object, ...args: unknown[]) => void;
13
+ };
14
+ type FileEntryCacheOptions = {
15
+ /** Whether to use file modified time for change detection (default: true) */useModifiedTime?: boolean; /** Whether to use checksum for change detection (default: false) */
16
+ useCheckSum?: boolean; /** Hash algorithm to use for checksum (default: 'md5') */
17
+ hashAlgorithm?: string; /** Current working directory for resolving relative paths (default: process.cwd()) */
18
+ cwd?: string; /** Restrict file access to within cwd boundaries (default: true) */
19
+ restrictAccessToCwd?: boolean; /** Whether to use absolute path as cache key (default: false) */
20
+ useAbsolutePathAsKey?: boolean; /** Logger instance for logging (default: undefined) */
21
+ logger?: ILogger; /** Options for the underlying flat cache */
22
+ cache?: FlatCacheOptions;
23
+ };
24
+ type GetFileDescriptorOptions = {
25
+ /** Whether to use checksum for this specific file check instead of modified time (mtime) (overrides instance setting) */useCheckSum?: boolean; /** Whether to use file modified time for change detection (default: true) */
26
+ useModifiedTime?: boolean;
27
+ };
28
+ type FileDescriptor = {
29
+ /** The cache key for this file (typically the file path) */key: string; /** Whether the file has changed since last cache check */
30
+ changed?: boolean; /** Metadata about the file */
31
+ meta: FileDescriptorMeta; /** Whether the file was not found */
32
+ notFound?: boolean; /** Error encountered when accessing the file */
33
+ err?: Error;
34
+ };
35
+ type FileDescriptorMeta = {
36
+ /** File size in bytes */size?: number; /** File modification time (timestamp in milliseconds) */
37
+ mtime?: number; /** File content hash (when useCheckSum is enabled) */
38
+ hash?: string; /** Custom data associated with the file (e.g., lint results, metadata) */
39
+ data?: unknown; /** Allow any additional custom properties */
40
+ [key: string]: unknown;
41
+ };
42
+ type AnalyzedFiles = {
43
+ /** Array of file paths that have changed since last cache */changedFiles: string[]; /** Array of file paths that were not found */
44
+ notFoundFiles: string[]; /** Array of file paths that have not changed since last cache */
45
+ notChangedFiles: string[];
46
+ };
47
+ /**
48
+ * Create a new FileEntryCache instance from a file path
49
+ * @param filePath - The path to the cache file
50
+ * @param options - create options such as useChecksum, cwd, and more
51
+ * @returns A new FileEntryCache instance
52
+ */
53
+ declare function createFromFile(filePath: string, options?: CreateOptions): FileEntryCache;
54
+ type CreateOptions = Omit<FileEntryCacheOptions, "cache">;
55
+ /**
56
+ * Create a new FileEntryCache instance
57
+ * @param cacheId - The cache file name
58
+ * @param cacheDirectory - The directory to store the cache file (default: undefined, cache won't be persisted)
59
+ * @param options - Whether to use checksum to detect file changes (default: false)
60
+ * @returns A new FileEntryCache instance
61
+ */
62
+ declare function create(cacheId: string, cacheDirectory?: string, options?: CreateOptions): FileEntryCache;
63
+ declare class FileEntryDefault {
64
+ static create: typeof create;
65
+ static createFromFile: typeof createFromFile;
66
+ }
67
+ declare class FileEntryCache {
68
+ private _cache;
69
+ private _useCheckSum;
70
+ private _hashAlgorithm;
71
+ private _cwd;
72
+ private _restrictAccessToCwd;
73
+ private _logger?;
74
+ private _useAbsolutePathAsKey;
75
+ private _useModifiedTime;
76
+ /**
77
+ * Snapshot of the persisted meta for each key as of the last load/reconcile.
78
+ * Change detection compares against this baseline (not the working cache) so
79
+ * that repeated `getFileDescriptor()` calls keep reporting a file as changed
80
+ * until the cache is reconciled. The set of keys also tracks which files were
81
+ * visited during the current session so that `reconcile()` only updates those.
82
+ */
83
+ private _originalMeta;
84
+ /**
85
+ * Create a new FileEntryCache instance
86
+ * @param options - The options for the FileEntryCache (all properties are optional with defaults)
87
+ */
88
+ constructor(options?: FileEntryCacheOptions);
89
+ /**
90
+ * Get the cache
91
+ * @returns {FlatCache} The cache
92
+ */
93
+ get cache(): FlatCache;
94
+ /**
95
+ * Set the cache
96
+ * @param {FlatCache} cache - The cache to set
97
+ */
98
+ set cache(cache: FlatCache);
99
+ /**
100
+ * Get the logger
101
+ * @returns {ILogger | undefined} The logger instance
102
+ */
103
+ get logger(): ILogger | undefined;
104
+ /**
105
+ * Set the logger
106
+ * @param {ILogger | undefined} logger - The logger to set
107
+ */
108
+ set logger(logger: ILogger | undefined);
109
+ /**
110
+ * Use the hash to check if the file has changed
111
+ * @returns {boolean} if the hash is used to check if the file has changed (default: false)
112
+ */
113
+ get useCheckSum(): boolean;
114
+ /**
115
+ * Set the useCheckSum value
116
+ * @param {boolean} value - The value to set
117
+ */
118
+ set useCheckSum(value: boolean);
119
+ /**
120
+ * Get the hash algorithm
121
+ * @returns {string} The hash algorithm (default: 'md5')
122
+ */
123
+ get hashAlgorithm(): string;
124
+ /**
125
+ * Set the hash algorithm
126
+ * @param {string} value - The value to set
127
+ */
128
+ set hashAlgorithm(value: string);
129
+ /**
130
+ * Get the current working directory
131
+ * @returns {string} The current working directory (default: process.cwd())
132
+ */
133
+ get cwd(): string;
134
+ /**
135
+ * Set the current working directory
136
+ *
137
+ * Note: when relative paths are used as cache keys (the default), `cwd` must
138
+ * stay stable across a `getFileDescriptor()` / `reconcile()` cycle. Relative
139
+ * keys are resolved against the *current* `cwd` each time, so changing it
140
+ * mid-run can cause `reconcile()` to resolve a key to a different (missing)
141
+ * path and drop the entry. Use absolute keys (`useAbsolutePathAsKey: true`)
142
+ * if `cwd` must change during a run.
143
+ * @param {string} value - The value to set
144
+ */
145
+ set cwd(value: string);
146
+ /**
147
+ * Get whether to use modified time for change detection
148
+ * @returns {boolean} Whether modified time (mtime) is used for change detection (default: true)
149
+ */
150
+ get useModifiedTime(): boolean;
151
+ /**
152
+ * Set whether to use modified time for change detection
153
+ * @param {boolean} value - The value to set
154
+ */
155
+ set useModifiedTime(value: boolean);
156
+ /**
157
+ * Get whether to restrict paths to cwd boundaries
158
+ * @returns {boolean} Whether strict path checking is enabled (default: true)
159
+ */
160
+ get restrictAccessToCwd(): boolean;
161
+ /**
162
+ * Set whether to restrict paths to cwd boundaries
163
+ * @param {boolean} value - The value to set
164
+ */
165
+ set restrictAccessToCwd(value: boolean);
166
+ /**
167
+ * Get whether to use absolute path as cache key
168
+ * @returns {boolean} Whether cache keys use absolute paths (default: false)
169
+ */
170
+ get useAbsolutePathAsKey(): boolean;
171
+ /**
172
+ * Set whether to use absolute path as cache key
173
+ * @param {boolean} value - The value to set
174
+ */
175
+ set useAbsolutePathAsKey(value: boolean);
176
+ /**
177
+ * Given a buffer, calculate md5 hash of its content.
178
+ * @method getHash
179
+ * @param {Buffer} buffer buffer to calculate hash on
180
+ * @return {String} content hash digest
181
+ */
182
+ getHash(buffer: Buffer): string;
183
+ /**
184
+ * Create the key for the file path used for caching.
185
+ * @method createFileKey
186
+ * @param {String} filePath
187
+ * @return {String}
188
+ */
189
+ createFileKey(filePath: string): string;
190
+ /**
191
+ * Check if the file path is a relative path
192
+ * @method isRelativePath
193
+ * @param filePath - The file path to check
194
+ * @returns {boolean} if the file path is a relative path, false otherwise
195
+ */
196
+ isRelativePath(filePath: string): boolean;
197
+ /**
198
+ * Delete the cache file from the disk
199
+ * @method deleteCacheFile
200
+ * @return {boolean} true if the file was deleted, false otherwise
201
+ */
202
+ deleteCacheFile(): boolean;
203
+ /**
204
+ * Remove the cache from the file and clear the memory cache
205
+ * @method destroy
206
+ */
207
+ destroy(): void;
208
+ /**
209
+ * Remove and Entry From the Cache
210
+ * @method removeEntry
211
+ * @param filePath - The file path to remove from the cache
212
+ */
213
+ removeEntry(filePath: string): void;
214
+ /**
215
+ * Reconcile the cache
216
+ * @method reconcile
217
+ */
218
+ reconcile(): void;
219
+ /**
220
+ * Check if the file has changed
221
+ * @method hasFileChanged
222
+ * @param filePath - The file path to check
223
+ * @returns {boolean} if the file has changed, false otherwise
224
+ */
225
+ hasFileChanged(filePath: string): boolean;
226
+ /**
227
+ * Get the file descriptor for the file path
228
+ * @method getFileDescriptor
229
+ * @param filePath - The file path to get the file descriptor for
230
+ * @param options - The options for getting the file descriptor
231
+ * @returns The file descriptor
232
+ */
233
+ getFileDescriptor(filePath: string, options?: GetFileDescriptorOptions): FileDescriptor;
234
+ /**
235
+ * Get the file descriptors for the files
236
+ * @method normalizeEntries
237
+ * @param files?: string[] - The files to get the file descriptors for
238
+ * @returns The file descriptors
239
+ */
240
+ normalizeEntries(files?: string[]): FileDescriptor[];
241
+ /**
242
+ * Analyze the files
243
+ * @method analyzeFiles
244
+ * @param files - The files to analyze
245
+ * @returns {AnalyzedFiles} The analysis of the files
246
+ */
247
+ analyzeFiles(files: string[]): AnalyzedFiles;
248
+ /**
249
+ * Get the updated files
250
+ * @method getUpdatedFiles
251
+ * @param files - The files to get the updated files for
252
+ * @returns {string[]} The updated files
253
+ */
254
+ getUpdatedFiles(files: string[]): string[];
255
+ /**
256
+ * Get the file descriptors by path prefix
257
+ * @method getFileDescriptorsByPath
258
+ * @param filePath - the path prefix to match
259
+ * @returns {FileDescriptor[]} The file descriptors
260
+ */
261
+ getFileDescriptorsByPath(filePath: string): FileDescriptor[];
262
+ /**
263
+ * Get the Absolute Path. If it is already absolute it will return the path as is.
264
+ * When restrictAccessToCwd is enabled, ensures the resolved path stays within cwd boundaries.
265
+ * @method getAbsolutePath
266
+ * @param filePath - The file path to get the absolute path for
267
+ * @returns {string}
268
+ * @throws {Error} When restrictAccessToCwd is true and path would resolve outside cwd
269
+ */
270
+ getAbsolutePath(filePath: string): string;
271
+ /**
272
+ * Get the Absolute Path with a custom working directory. If it is already absolute it will return the path as is.
273
+ * When restrictAccessToCwd is enabled, ensures the resolved path stays within the provided cwd boundaries.
274
+ * @method getAbsolutePathWithCwd
275
+ * @param filePath - The file path to get the absolute path for
276
+ * @param cwd - The custom working directory to resolve relative paths from
277
+ * @returns {string}
278
+ * @throws {Error} When restrictAccessToCwd is true and path would resolve outside the provided cwd
279
+ */
280
+ getAbsolutePathWithCwd(filePath: string, cwd: string): string;
281
+ /**
282
+ * Rename cache keys that start with a given path prefix.
283
+ * @method renameCacheKeys
284
+ * @param oldPath - The old path prefix to rename
285
+ * @param newPath - The new path prefix to rename to
286
+ */
287
+ renameCacheKeys(oldPath: string, newPath: string): void;
288
+ }
289
+ //#endregion
290
+ export { AnalyzedFiles, CreateOptions, FileDescriptor, FileDescriptorMeta, FileEntryCache, FileEntryCacheOptions, GetFileDescriptorOptions, ILogger, create, createFromFile, FileEntryDefault as default };