Skip to content

Commit d18b95c

Browse files
fix: filesystem types
2 parents 90c511b + 13a3f1e commit d18b95c

10 files changed

+1119
-381
lines changed

.cspell.json

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"words": [
44
"abcxyz",
55
"addrs",
6+
"abortable",
67
"anotherhashishere",
78
"arcanis",
89
"Builtins",

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ jobs:
3939
run: |
4040
yarn upgrade typescript@^4 --ignore-engines
4141
yarn --frozen-lockfile
42-
if: matrix.node-version == '10.x'
42+
if: matrix.node-version == '10.x' || matrix.node-version == '12.x'
4343
- name: Install dependencies
4444
run: yarn --frozen-lockfile
4545
if: matrix.node-version != '10.x'

lib/CachedInputFileSystem.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const dirname = path => {
3535
/**
3636
* @template T
3737
* @param {FileSystemCallback<T>[]} callbacks callbacks
38-
* @param {Error | undefined} err error
38+
* @param {Error | null} err error
3939
* @param {T} result result
4040
*/
4141
const runCallbacks = (callbacks, err, result) => {
@@ -165,7 +165,7 @@ class CacheBackend {
165165
this._providerContext = providerContext;
166166
/** @type {Map<string, FileSystemCallback<any>[]>} */
167167
this._activeAsyncOperations = new Map();
168-
/** @type {Map<string, { err?: Error, result?: any, level: Set<string> }>} */
168+
/** @type {Map<string, { err: Error | null, result?: any, level: Set<string> }>} */
169169
this._data = new Map();
170170
/** @type {Set<string>[]} */
171171
this._levels = [];
@@ -236,7 +236,7 @@ class CacheBackend {
236236
this._providerContext,
237237
path,
238238
/**
239-
* @param {Error} [err] error
239+
* @param {Error | null} err error
240240
* @param {any} [result] result
241241
*/
242242
(err, result) => {
@@ -298,16 +298,16 @@ class CacheBackend {
298298
}
299299
throw err;
300300
}
301-
this._storeResult(path, undefined, result);
301+
this._storeResult(path, null, result);
302302
this._enterSyncModeWhenIdle();
303303
if (callbacks) {
304-
runCallbacks(callbacks, undefined, result);
304+
runCallbacks(callbacks, null, result);
305305
}
306306
return result;
307307
}
308308

309309
/**
310-
* @param {string|string[]|Set<string>} [what] what to purge
310+
* @param {string | string[] | Set<string>} [what] what to purge
311311
*/
312312
purge(what) {
313313
if (!what) {
@@ -363,7 +363,7 @@ class CacheBackend {
363363

364364
/**
365365
* @param {string} path path
366-
* @param {undefined | Error} err error
366+
* @param {Error | null} err error
367367
* @param {any} result result
368368
*/
369369
_storeResult(path, err, result) {

lib/DescriptionFileUtils.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ function loadDescriptionFile(
6666
if (resolver.fileSystem.readJson) {
6767
resolver.fileSystem.readJson(descriptionFilePath, (err, content) => {
6868
if (err) {
69-
if (typeof err.code !== "undefined") {
69+
if (
70+
typeof (/** @type {NodeJS.ErrnoException} */ (err).code) !==
71+
"undefined"
72+
) {
7073
if (resolveContext.missingDependencies) {
7174
resolveContext.missingDependencies.add(descriptionFilePath);
7275
}
@@ -80,7 +83,7 @@ function loadDescriptionFile(
8083
if (resolveContext.fileDependencies) {
8184
resolveContext.fileDependencies.add(descriptionFilePath);
8285
}
83-
onJson(null, /** @type {JsonObject} */ (content));
86+
onJson(null, content);
8487
});
8588
} else {
8689
resolver.fileSystem.readFile(descriptionFilePath, (err, content) => {

lib/Resolver.js

+225-32
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,6 @@ const {
2121

2222
/** @typedef {(err: ErrorWithDetail | null, res?: string | false, req?: ResolveRequest) => void} ResolveCallback */
2323

24-
/**
25-
* @typedef {Object} FileSystemStats
26-
* @property {function(): boolean} isDirectory
27-
* @property {function(): boolean} isFile
28-
*/
29-
30-
/**
31-
* @typedef {Object} FileSystemDirent
32-
* @property {Buffer | string} name
33-
* @property {function(): boolean} isDirectory
34-
* @property {function(): boolean} isFile
35-
*/
36-
3724
/**
3825
* @typedef {Object} PossibleFileSystemError
3926
* @property {string=} code
@@ -45,38 +32,244 @@ const {
4532
/**
4633
* @template T
4734
* @callback FileSystemCallback
48-
* @param {PossibleFileSystemError & Error | null | undefined} err
35+
* @param {PossibleFileSystemError & Error | null} err
4936
* @param {T=} result
5037
*/
5138

52-
/** @typedef {function((NodeJS.ErrnoException | null)=, (string | Buffer)[] | import("fs").Dirent[]=): void} DirentArrayCallback */
39+
/**
40+
* @typedef {string | Buffer | URL} PathLike
41+
*/
42+
43+
/**
44+
* @typedef {PathLike | number} PathOrFileDescriptor
45+
*/
46+
47+
/**
48+
* @typedef {Object} ObjectEncodingOptions
49+
* @property {BufferEncoding | null | undefined} [encoding]
50+
*/
51+
52+
/** @typedef {function(NodeJS.ErrnoException | null, string=): void} StringCallback */
53+
/** @typedef {function(NodeJS.ErrnoException | null, Buffer=): void} BufferCallback */
54+
/** @typedef {function(NodeJS.ErrnoException | null, (string | Buffer)=): void} StringOrBufferCallback */
55+
/** @typedef {function(NodeJS.ErrnoException | null, IStats=): void} StatsCallback */
56+
/** @typedef {function(NodeJS.ErrnoException | null, IBigIntStats=): void} BigIntStatsCallback */
57+
/** @typedef {function(NodeJS.ErrnoException | null, (IStats | IBigIntStats)=): void} StatsOrBigIntStatsCallback */
58+
/** @typedef {function(NodeJS.ErrnoException | Error | null, JsonObject=): void} ReadJsonCallback */
59+
/** @typedef {function(NodeJS.ErrnoException | null, string[]=): void} ReaddirStringCallback */
60+
/** @typedef {function(NodeJS.ErrnoException | null, Buffer[]=): void} ReaddirBufferCallback */
61+
/** @typedef {function(NodeJS.ErrnoException | null, (string[] | Buffer[])=): void} ReaddirStringOrBufferCallback */
62+
/** @typedef {function(NodeJS.ErrnoException | null, Dirent[]=): void} ReaddirDirentCallback */
63+
64+
/**
65+
* @template T
66+
* @typedef {Object} IStatsBase
67+
* @property {() => boolean} isFile
68+
* @property {() => boolean} isDirectory
69+
* @property {() => boolean} isBlockDevice
70+
* @property {() => boolean} isCharacterDevice
71+
* @property {() => boolean} isSymbolicLink
72+
* @property {() => boolean} isFIFO
73+
* @property {() => boolean} isSocket
74+
* @property {T} dev
75+
* @property {T} ino
76+
* @property {T} mode
77+
* @property {T} nlink
78+
* @property {T} uid
79+
* @property {T} gid
80+
* @property {T} rdev
81+
* @property {T} size
82+
* @property {T} blksize
83+
* @property {T} blocks
84+
* @property {T} atimeMs
85+
* @property {T} mtimeMs
86+
* @property {T} ctimeMs
87+
* @property {T} birthtimeMs
88+
* @property {Date} atime
89+
* @property {Date} mtime
90+
* @property {Date} ctime
91+
* @property {Date} birthtime
92+
*/
93+
94+
/**
95+
* @typedef {IStatsBase<number>} IStats
96+
*/
97+
98+
/**
99+
* @typedef {IStatsBase<bigint> & { atimeNs: bigint, mtimeNs: bigint, ctimeNs: bigint, birthtimeNs: bigint }} IBigIntStats
100+
*/
101+
102+
/**
103+
* @typedef {Object} Dirent
104+
* @property {() => boolean} isFile
105+
* @property {() => boolean} isDirectory
106+
* @property {() => boolean} isBlockDevice
107+
* @property {() => boolean} isCharacterDevice
108+
* @property {() => boolean} isSymbolicLink
109+
* @property {() => boolean} isFIFO
110+
* @property {() => boolean} isSocket
111+
* @property {string} name
112+
* @property {string} path
113+
*/
114+
115+
/**
116+
* @typedef {Object} StatOptions
117+
* @property {(boolean | undefined)=} bigint
118+
*/
119+
120+
/**
121+
* @typedef {Object} StatSyncOptions
122+
* @property {(boolean | undefined)=} bigint
123+
* @property {(boolean | undefined)=} throwIfNoEntry
124+
*/
125+
126+
/**
127+
* @typedef {{
128+
* (path: PathOrFileDescriptor, options: ({ encoding?: null | undefined, flag?: string | undefined } & import("events").Abortable) | undefined | null, callback: BufferCallback): void;
129+
* (path: PathOrFileDescriptor, options: ({ encoding: BufferEncoding, flag?: string | undefined } & import("events").Abortable) | BufferEncoding, callback: StringCallback): void;
130+
* (path: PathOrFileDescriptor, options: (ObjectEncodingOptions & { flag?: string | undefined } & import("events").Abortable) | BufferEncoding | undefined | null, callback: StringOrBufferCallback): void;
131+
* (path: PathOrFileDescriptor, callback: BufferCallback): void;
132+
* }} ReadFile
133+
*/
134+
135+
/**
136+
* @typedef {ObjectEncodingOptions | BufferEncoding | undefined | null} EncodingOption
137+
*/
138+
139+
/**
140+
* @typedef {'buffer'| { encoding: 'buffer' }} BufferEncodingOption
141+
*/
142+
143+
/**
144+
* @typedef {{
145+
* (path: PathOrFileDescriptor, options?: { encoding?: null | undefined, flag?: string | undefined } | null): Buffer;
146+
* (path: PathOrFileDescriptor, options: { encoding: BufferEncoding, flag?: string | undefined } | BufferEncoding): string;
147+
* (path: PathOrFileDescriptor, options?: (ObjectEncodingOptions & { flag?: string | undefined }) | BufferEncoding | null): string | Buffer;
148+
* }} ReadFileSync
149+
*/
150+
151+
/**
152+
* @typedef {{
153+
* (path: PathLike, options: { encoding: BufferEncoding | null, withFileTypes?: false | undefined, recursive?: boolean | undefined } | BufferEncoding | undefined | null, callback: ReaddirStringCallback): void;
154+
* (path: PathLike, options: { encoding: 'buffer', withFileTypes?: false | undefined, recursive?: boolean | undefined } | 'buffer', callback: ReaddirBufferCallback): void;
155+
* (path: PathLike, callback: ReaddirStringCallback): void;
156+
* (path: PathLike, options: (ObjectEncodingOptions & { withFileTypes?: false | undefined, recursive?: boolean | undefined }) | BufferEncoding | undefined | null, callback: ReaddirStringOrBufferCallback): void;
157+
* (path: PathLike, options: ObjectEncodingOptions & { withFileTypes: true, recursive?: boolean | undefined }, callback: ReaddirDirentCallback): void;
158+
* }} Readdir
159+
*/
160+
161+
/**
162+
* @typedef {{
163+
* (path: PathLike, options?: { encoding: BufferEncoding | null, withFileTypes?: false | undefined, recursive?: boolean | undefined } | BufferEncoding | null): string[];
164+
* (path: PathLike, options: { encoding: 'buffer', withFileTypes?: false | undefined, recursive?: boolean | undefined } | 'buffer'): Buffer[];
165+
* (path: PathLike, options?: (ObjectEncodingOptions & { withFileTypes?: false | undefined, recursive?: boolean | undefined }) | BufferEncoding | null): string[] | Buffer[];
166+
* (path: PathLike, options: ObjectEncodingOptions & { withFileTypes: true, recursive?: boolean | undefined }): Dirent[];
167+
* }} ReaddirSync
168+
169+
/**
170+
* @typedef {function(PathOrFileDescriptor, ReadJsonCallback): void} ReadJson
171+
*/
172+
173+
/**
174+
* @typedef {function(PathOrFileDescriptor): JsonObject} ReadJsonSync
175+
*/
176+
177+
/**
178+
* @typedef {{
179+
* (path: PathLike, options: EncodingOption, callback: StringCallback): void;
180+
* (path: PathLike, options: BufferEncodingOption, callback: BufferCallback): void;
181+
* (path: PathLike, options: EncodingOption, callback: StringOrBufferCallback): void;
182+
* (path: PathLike, callback: StringCallback): void;
183+
* }} Readlink
184+
*/
185+
186+
/**
187+
* @typedef {{
188+
* (path: PathLike, options?: EncodingOption): string;
189+
* (path: PathLike, options: BufferEncodingOption): Buffer;
190+
* (path: PathLike, options?: EncodingOption): string | Buffer;
191+
* }} ReadlinkSync
192+
*/
193+
194+
/**
195+
* @typedef {{
196+
* (path: PathLike, callback: StatsCallback): void;
197+
* (path: PathLike, options: (StatOptions & { bigint?: false | undefined }) | undefined, callback: StatsCallback): void;
198+
* (path: PathLike, options: StatOptions & { bigint: true }, callback: BigIntStatsCallback): void;
199+
* (path: PathLike, options: StatOptions | undefined, callback: StatsOrBigIntStatsCallback): void;
200+
* }} LStat
201+
*/
202+
203+
/**
204+
* @typedef {{
205+
* (path: PathLike, options?: undefined): IStats;
206+
* (path: PathLike, options?: StatSyncOptions & { bigint?: false | undefined, throwIfNoEntry: false }): IStats | undefined;
207+
* (path: PathLike, options: StatSyncOptions & { bigint: true, throwIfNoEntry: false }): IBigIntStats | undefined;
208+
* (path: PathLike, options?: StatSyncOptions & { bigint?: false | undefined }): IStats;
209+
* (path: PathLike, options: StatSyncOptions & { bigint: true }): IBigIntStats;
210+
* (path: PathLike, options: StatSyncOptions & { bigint: boolean, throwIfNoEntry?: false | undefined }): IStats | IBigIntStats;
211+
* (path: PathLike, options?: StatSyncOptions): IStats | IBigIntStats | undefined;
212+
* }} LStatSync
213+
*/
214+
215+
/**
216+
* @typedef {{
217+
* (path: PathLike, callback: StatsCallback): void;
218+
* (path: PathLike, options: (StatOptions & { bigint?: false | undefined }) | undefined, callback: StatsCallback): void;
219+
* (path: PathLike, options: StatOptions & { bigint: true }, callback: BigIntStatsCallback): void;
220+
* (path: PathLike, options: StatOptions | undefined, callback: StatsOrBigIntStatsCallback): void;
221+
* }} Stat
222+
*/
223+
224+
/**
225+
* @typedef {{
226+
* (path: PathLike, options?: undefined): IStats;
227+
* (path: PathLike, options?: StatSyncOptions & { bigint?: false | undefined, throwIfNoEntry: false }): IStats | undefined;
228+
* (path: PathLike, options: StatSyncOptions & { bigint: true, throwIfNoEntry: false }): IBigIntStats | undefined;
229+
* (path: PathLike, options?: StatSyncOptions & { bigint?: false | undefined }): IStats;
230+
* (path: PathLike, options: StatSyncOptions & { bigint: true }): IBigIntStats;
231+
* (path: PathLike, options: StatSyncOptions & { bigint: boolean, throwIfNoEntry?: false | undefined }): IStats | IBigIntStats;
232+
* (path: PathLike, options?: StatSyncOptions): IStats | IBigIntStats | undefined;
233+
* }} StatSync
234+
*/
235+
236+
/**
237+
* @typedef {{
238+
* (path: PathLike, options: EncodingOption, callback: StringCallback): void;
239+
* (path: PathLike, options: BufferEncodingOption, callback: BufferCallback): void;
240+
* (path: PathLike, options: EncodingOption, callback: StringOrBufferCallback): void;
241+
* (path: PathLike, callback: StringCallback): void;
242+
* }} RealPath
243+
*/
53244

54245
/**
55-
* @typedef {Object} ReaddirOptions
56-
* @property {BufferEncoding | null | 'buffer'} [encoding]
57-
* @property {boolean | undefined} [withFileTypes=false]
246+
* @typedef {{
247+
* (path: PathLike, options?: EncodingOption): string;
248+
* (path: PathLike, options: BufferEncodingOption): Buffer;
249+
* (path: PathLike, options?: EncodingOption): string | Buffer;
250+
* }} RealPathSync
58251
*/
59252

60253
/**
61254
* @typedef {Object} FileSystem
62-
* @property {(function(string, FileSystemCallback<Buffer | string>): void) & function(string, object, FileSystemCallback<Buffer | string>): void} readFile
63-
* @property {function(string, (ReaddirOptions | BufferEncoding | null | undefined | 'buffer' | DirentArrayCallback)=, DirentArrayCallback=): void} readdir
64-
* @property {((function(string, FileSystemCallback<object>): void) & function(string, object, FileSystemCallback<object>): void)=} readJson
65-
* @property {(function(string, FileSystemCallback<Buffer | string>): void) & function(string, object, FileSystemCallback<Buffer | string>): void} readlink
66-
* @property {(function(string, FileSystemCallback<FileSystemStats>): void) & function(string, object, FileSystemCallback<Buffer | string>): void=} lstat
67-
* @property {(function(string, FileSystemCallback<FileSystemStats>): void) & function(string, object, FileSystemCallback<Buffer | string>): void} stat
68-
* @property {(function(string, FileSystemCallback<Buffer | string>): void) & function(string, object, FileSystemCallback<Buffer | string>): void=} realpath
255+
* @property {ReadFile} readFile
256+
* @property {Readdir} readdir
257+
* @property {ReadJson=} readJson
258+
* @property {Readlink} readlink
259+
* @property {LStat=} lstat
260+
* @property {Stat} stat
261+
* @property {RealPath=} realpath
69262
*/
70263

71264
/**
72265
* @typedef {Object} SyncFileSystem
73-
* @property {function(string, object=): Buffer | string} readFileSync
74-
* @property {function(string, object=): (Buffer | string)[] | FileSystemDirent[]} readdirSync
75-
* @property {(function(string, object=): object)=} readJsonSync
76-
* @property {function(string, object=): Buffer | string} readlinkSync
77-
* @property {function(string, object=): FileSystemStats=} lstatSync
78-
* @property {function(string, object=): FileSystemStats} statSync
79-
* @property {function(string, object=): string | Buffer=} realpathSync
266+
* @property {ReadFileSync} readFileSync
267+
* @property {ReaddirSync} readdirSync
268+
* @property {ReadJsonSync=} readJsonSync
269+
* @property {ReadlinkSync} readlinkSync
270+
* @property {LStatSync=} lstatSync
271+
* @property {StatSync} statSync
272+
* @property {RealPathSync=} realpathSync
80273
*/
81274

82275
/**

0 commit comments

Comments
 (0)