Skip to content

Commit

Permalink
fix: types for cached input filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Mar 6, 2024
2 parents d18b95c + a1c6cd8 commit 2f51fb0
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 19 deletions.
57 changes: 40 additions & 17 deletions lib/CachedInputFileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
const nextTick = require("process").nextTick;

/** @typedef {import("./Resolver").FileSystem} FileSystem */
/** @typedef {import("./Resolver").PathLike} PathLike */
/** @typedef {import("./Resolver").PathOrFileDescriptor} PathOrFileDescriptor */
/** @typedef {import("./Resolver").SyncFileSystem} SyncFileSystem */
/** @typedef {any} BaseFileSystem */
/** @typedef {FileSystem & SyncFileSystem} BaseFileSystem */

/**
* @template T
Expand Down Expand Up @@ -58,8 +60,8 @@ const runCallbacks = (callbacks, err, result) => {

class OperationMergerBackend {
/**
* @param {function} provider async method in filesystem
* @param {function} syncProvider sync method in filesystem
* @param {Function | undefined} provider async method in filesystem
* @param {Function | undefined} syncProvider sync method in filesystem
* @param {BaseFileSystem} providerContext call context for the provider methods
*/
constructor(provider, syncProvider, providerContext) {
Expand All @@ -81,7 +83,7 @@ class OperationMergerBackend {
options = undefined;
}
if (options) {
return this._provider.call(
return /** @type {Function} */ (this._provider).call(
this._providerContext,
path,
options,
Expand All @@ -98,7 +100,8 @@ class OperationMergerBackend {
return;
}
this._activeAsyncOperations.set(path, (callbacks = [callback]));
provider(
/** @type {Function} */
(provider)(
path,
/**
* @param {Error} err error
Expand All @@ -118,7 +121,11 @@ class OperationMergerBackend {
* @returns {any} result
*/
(path, options) => {
return this._syncProvider.call(this._providerContext, path, options);
return /** @type {Function} */ (this._syncProvider).call(
this._providerContext,
path,
options
);
}
: null;
}
Expand Down Expand Up @@ -151,11 +158,19 @@ const STORAGE_MODE_IDLE = 0;
const STORAGE_MODE_SYNC = 1;
const STORAGE_MODE_ASYNC = 2;

/**
* @callback Provide
* @param {PathLike | PathOrFileDescriptor} path path
* @param {any} options options
* @param {FileSystemCallback<any>} callback callback
* @returns {void}
*/

class CacheBackend {
/**
* @param {number} duration max cache duration of items
* @param {function} provider async method
* @param {function} syncProvider sync method
* @param {function | undefined} provider async method
* @param {function | undefined} syncProvider sync method
* @param {BaseFileSystem} providerContext call context for the provider methods
*/
constructor(duration, provider, syncProvider, providerContext) {
Expand Down Expand Up @@ -188,7 +203,7 @@ class CacheBackend {
}

/**
* @param {string} path path
* @param {PathLike | PathOrFileDescriptor} path path
* @param {any} options options
* @param {FileSystemCallback<any>} callback callback
* @returns {void}
Expand All @@ -203,7 +218,7 @@ class CacheBackend {
return;
}
if (options) {
return this._provider.call(
return /** @type {Function} */ (this._provider).call(
this._providerContext,
path,
options,
Expand Down Expand Up @@ -232,7 +247,8 @@ class CacheBackend {
this._activeAsyncOperations.set(path, (callbacks = [callback]));

// Run the operation
this._provider.call(
/** @type {Function} */
(this._provider).call(
this._providerContext,
path,
/**
Expand All @@ -256,7 +272,7 @@ class CacheBackend {
}

/**
* @param {string} path path
* @param {PathLike | PathOrFileDescriptor} path path
* @param {any} options options
* @returns {any} result
*/
Expand All @@ -265,7 +281,11 @@ class CacheBackend {
throw new TypeError("path must be a string");
}
if (options) {
return this._syncProvider.call(this._providerContext, path, options);
return /** @type {Function} */ (this._syncProvider).call(
this._providerContext,
path,
options
);
}

// In sync mode we may have to decay some cache items
Expand All @@ -289,7 +309,10 @@ class CacheBackend {
// When in idle mode, we will enter sync mode
let result;
try {
result = this._syncProvider.call(this._providerContext, path);
result = /** @type {Function} */ (this._syncProvider).call(
this._providerContext,
path
);
} catch (err) {
this._storeResult(path, /** @type {Error} */ (err), undefined);
this._enterSyncModeWhenIdle();
Expand Down Expand Up @@ -449,9 +472,9 @@ class CacheBackend {
* @template {function} AsyncProvider
* @template FileSystem
* @param {number} duration duration in ms files are cached
* @param {Provider} provider provider
* @param {AsyncProvider} syncProvider sync provider
* @param {FileSystem} providerContext provider context
* @param {Provider | undefined} provider provider
* @param {AsyncProvider | undefined} syncProvider sync provider
* @param {BaseFileSystem} providerContext provider context
* @returns {OperationMergerBackend | CacheBackend} backend
*/
const createBackend = (duration, provider, syncProvider, providerContext) => {
Expand Down
4 changes: 4 additions & 0 deletions test/CachedInputFileSystem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe("CachedInputFileSystem OperationMergerBackend ('stat' and 'statSync')",
100
);
},
// @ts-ignore
statSync: function (path, options) {
return {
path,
Expand Down Expand Up @@ -101,6 +102,7 @@ describe("CachedInputFileSystem OperationMergerBackend ('lstat' and 'lstatSync')
100
);
},
// @ts-ignore
lstatSync: function (path, options) {
return {
path,
Expand Down Expand Up @@ -185,6 +187,7 @@ describe("CachedInputFileSystem OperationMergerBackend ('realpath' and 'realpath
100
);
},
// @ts-ignore
realpathSync: function (path, options) {
return {
path,
Expand Down Expand Up @@ -269,6 +272,7 @@ describe("CachedInputFileSystem CacheBackend", () => {
100
);
},
// @ts-ignore
statSync: function (path, options) {
return {
path,
Expand Down
1 change: 1 addition & 0 deletions test/pr-53.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ describe("pr-53", () => {
it("should allow to readJsonSync in CachedInputFileSystem", () => {
var cfs = new CachedInputFileSystem(
{
// @ts-ignore
readFileSync: function (path) {
return JSON.stringify("abc" + path);
}
Expand Down
14 changes: 12 additions & 2 deletions types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type AliasOptionNewRequest = string | false | string[];
declare interface AliasOptions {
[index: string]: AliasOptionNewRequest;
}
type BaseFileSystem = FileSystem & SyncFileSystem;
declare interface BaseResolveRequest {
path: string | false;
context?: object;
Expand Down Expand Up @@ -52,8 +53,8 @@ type BufferEncoding =
| "hex";
type BufferEncodingOption = "buffer" | { encoding: "buffer" };
declare class CachedInputFileSystem {
constructor(fileSystem: any, duration: number);
fileSystem: any;
constructor(fileSystem: BaseFileSystem, duration: number);
fileSystem: BaseFileSystem;
lstat?: LStat;
lstatSync?: LStatSync;
stat: Stat;
Expand Down Expand Up @@ -1025,6 +1026,15 @@ declare interface StatSyncOptions {
bigint?: boolean;
throwIfNoEntry?: boolean;
}
declare interface SyncFileSystem {
readFileSync: ReadFileSync;
readdirSync: ReaddirSync;
readJsonSync?: (arg0: PathOrFileDescriptor) => JsonObject;
readlinkSync: ReadlinkSync;
lstatSync?: LStatSync;
statSync: StatSync;
realpathSync?: RealPathSync;
}

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

0 comments on commit 2f51fb0

Please sign in to comment.