Skip to content

Commit 628a539

Browse files
anonrigaduh95
authored andcommitted
fs: reduce throwing unnecessary errors on glob
PR-URL: #53632 Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Moshe Atlow <[email protected]>
1 parent 076e82c commit 628a539

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

lib/internal/fs/glob.js

+22-9
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,30 @@ function lazyMinimatch() {
3838
const isWindows = process.platform === 'win32';
3939
const isOSX = process.platform === 'darwin';
4040

41+
/**
42+
* @param {string} path
43+
* @returns {Promise<DirentFromStats|null>}
44+
*/
4145
async function getDirent(path) {
42-
return new DirentFromStats(basename(path), await lstat(path), path);
46+
let stat;
47+
try {
48+
stat = await lstat(path);
49+
} catch {
50+
return null;
51+
}
52+
return new DirentFromStats(basename(path), stat, path);
4353
}
4454

55+
/**
56+
* @param {string} path
57+
* @returns {DirentFromStats|null}
58+
*/
4559
function getDirentSync(path) {
46-
return new DirentFromStats(basename(path), lstatSync(path), path);
60+
const stat = lstatSync(path, { throwIfNoEntry: false });
61+
if (stat === undefined) {
62+
return null;
63+
}
64+
return new DirentFromStats(basename(path), stat, path);
4765
}
4866

4967
class Cache {
@@ -56,7 +74,7 @@ class Cache {
5674
if (cached) {
5775
return cached;
5876
}
59-
const promise = PromisePrototypeThen(getDirent(path), null, () => null);
77+
const promise = getDirent(path);
6078
this.#statsCache.set(path, promise);
6179
return promise;
6280
}
@@ -65,12 +83,7 @@ class Cache {
6583
if (cached) {
6684
return cached;
6785
}
68-
let val;
69-
try {
70-
val = getDirentSync(path);
71-
} catch {
72-
val = null;
73-
}
86+
const val = getDirentSync(path);
7487
this.#statsCache.set(path, val);
7588
return val;
7689
}

0 commit comments

Comments
 (0)