Skip to content

Commit 01bc753

Browse files
fix: types for cached input filesystem
1 parent d18b95c commit 01bc753

File tree

2 files changed

+52
-19
lines changed

2 files changed

+52
-19
lines changed

lib/CachedInputFileSystem.js

+40-17
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
const nextTick = require("process").nextTick;
99

1010
/** @typedef {import("./Resolver").FileSystem} FileSystem */
11+
/** @typedef {import("./Resolver").PathLike} PathLike */
12+
/** @typedef {import("./Resolver").PathOrFileDescriptor} PathOrFileDescriptor */
1113
/** @typedef {import("./Resolver").SyncFileSystem} SyncFileSystem */
12-
/** @typedef {any} BaseFileSystem */
14+
/** @typedef {FileSystem & SyncFileSystem} BaseFileSystem */
1315

1416
/**
1517
* @template T
@@ -58,8 +60,8 @@ const runCallbacks = (callbacks, err, result) => {
5860

5961
class OperationMergerBackend {
6062
/**
61-
* @param {function} provider async method in filesystem
62-
* @param {function} syncProvider sync method in filesystem
63+
* @param {Function | undefined} provider async method in filesystem
64+
* @param {Function | undefined} syncProvider sync method in filesystem
6365
* @param {BaseFileSystem} providerContext call context for the provider methods
6466
*/
6567
constructor(provider, syncProvider, providerContext) {
@@ -81,7 +83,7 @@ class OperationMergerBackend {
8183
options = undefined;
8284
}
8385
if (options) {
84-
return this._provider.call(
86+
return /** @type {Function} */ (this._provider).call(
8587
this._providerContext,
8688
path,
8789
options,
@@ -98,7 +100,8 @@ class OperationMergerBackend {
98100
return;
99101
}
100102
this._activeAsyncOperations.set(path, (callbacks = [callback]));
101-
provider(
103+
/** @type {Function} */
104+
(provider)(
102105
path,
103106
/**
104107
* @param {Error} err error
@@ -118,7 +121,11 @@ class OperationMergerBackend {
118121
* @returns {any} result
119122
*/
120123
(path, options) => {
121-
return this._syncProvider.call(this._providerContext, path, options);
124+
return /** @type {Function} */ (this._syncProvider).call(
125+
this._providerContext,
126+
path,
127+
options
128+
);
122129
}
123130
: null;
124131
}
@@ -151,11 +158,19 @@ const STORAGE_MODE_IDLE = 0;
151158
const STORAGE_MODE_SYNC = 1;
152159
const STORAGE_MODE_ASYNC = 2;
153160

161+
/**
162+
* @callback Provide
163+
* @param {PathLike | PathOrFileDescriptor} path path
164+
* @param {any} options options
165+
* @param {FileSystemCallback<any>} callback callback
166+
* @returns {void}
167+
*/
168+
154169
class CacheBackend {
155170
/**
156171
* @param {number} duration max cache duration of items
157-
* @param {function} provider async method
158-
* @param {function} syncProvider sync method
172+
* @param {function | undefined} provider async method
173+
* @param {function | undefined} syncProvider sync method
159174
* @param {BaseFileSystem} providerContext call context for the provider methods
160175
*/
161176
constructor(duration, provider, syncProvider, providerContext) {
@@ -188,7 +203,7 @@ class CacheBackend {
188203
}
189204

190205
/**
191-
* @param {string} path path
206+
* @param {PathLike | PathOrFileDescriptor} path path
192207
* @param {any} options options
193208
* @param {FileSystemCallback<any>} callback callback
194209
* @returns {void}
@@ -203,7 +218,7 @@ class CacheBackend {
203218
return;
204219
}
205220
if (options) {
206-
return this._provider.call(
221+
return /** @type {Function} */ (this._provider).call(
207222
this._providerContext,
208223
path,
209224
options,
@@ -232,7 +247,8 @@ class CacheBackend {
232247
this._activeAsyncOperations.set(path, (callbacks = [callback]));
233248

234249
// Run the operation
235-
this._provider.call(
250+
/** @type {Function} */
251+
(this._provider).call(
236252
this._providerContext,
237253
path,
238254
/**
@@ -256,7 +272,7 @@ class CacheBackend {
256272
}
257273

258274
/**
259-
* @param {string} path path
275+
* @param {PathLike | PathOrFileDescriptor} path path
260276
* @param {any} options options
261277
* @returns {any} result
262278
*/
@@ -265,7 +281,11 @@ class CacheBackend {
265281
throw new TypeError("path must be a string");
266282
}
267283
if (options) {
268-
return this._syncProvider.call(this._providerContext, path, options);
284+
return /** @type {Function} */ (this._syncProvider).call(
285+
this._providerContext,
286+
path,
287+
options
288+
);
269289
}
270290

271291
// In sync mode we may have to decay some cache items
@@ -289,7 +309,10 @@ class CacheBackend {
289309
// When in idle mode, we will enter sync mode
290310
let result;
291311
try {
292-
result = this._syncProvider.call(this._providerContext, path);
312+
result = /** @type {Function} */ (this._syncProvider).call(
313+
this._providerContext,
314+
path
315+
);
293316
} catch (err) {
294317
this._storeResult(path, /** @type {Error} */ (err), undefined);
295318
this._enterSyncModeWhenIdle();
@@ -449,9 +472,9 @@ class CacheBackend {
449472
* @template {function} AsyncProvider
450473
* @template FileSystem
451474
* @param {number} duration duration in ms files are cached
452-
* @param {Provider} provider provider
453-
* @param {AsyncProvider} syncProvider sync provider
454-
* @param {FileSystem} providerContext provider context
475+
* @param {Provider | undefined} provider provider
476+
* @param {AsyncProvider | undefined} syncProvider sync provider
477+
* @param {BaseFileSystem} providerContext provider context
455478
* @returns {OperationMergerBackend | CacheBackend} backend
456479
*/
457480
const createBackend = (duration, provider, syncProvider, providerContext) => {

types.d.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type AliasOptionNewRequest = string | false | string[];
2424
declare interface AliasOptions {
2525
[index: string]: AliasOptionNewRequest;
2626
}
27+
type BaseFileSystem = FileSystem & SyncFileSystem;
2728
declare interface BaseResolveRequest {
2829
path: string | false;
2930
context?: object;
@@ -52,8 +53,8 @@ type BufferEncoding =
5253
| "hex";
5354
type BufferEncodingOption = "buffer" | { encoding: "buffer" };
5455
declare class CachedInputFileSystem {
55-
constructor(fileSystem: any, duration: number);
56-
fileSystem: any;
56+
constructor(fileSystem: BaseFileSystem, duration: number);
57+
fileSystem: BaseFileSystem;
5758
lstat?: LStat;
5859
lstatSync?: LStatSync;
5960
stat: Stat;
@@ -1025,6 +1026,15 @@ declare interface StatSyncOptions {
10251026
bigint?: boolean;
10261027
throwIfNoEntry?: boolean;
10271028
}
1029+
declare interface SyncFileSystem {
1030+
readFileSync: ReadFileSync;
1031+
readdirSync: ReaddirSync;
1032+
readJsonSync?: (arg0: PathOrFileDescriptor) => JsonObject;
1033+
readlinkSync: ReadlinkSync;
1034+
lstatSync?: LStatSync;
1035+
statSync: StatSync;
1036+
realpathSync?: RealPathSync;
1037+
}
10281038

10291039
/**
10301040
* `URL` class is a global reference for `require('url').URL`

0 commit comments

Comments
 (0)