Skip to content

Commit

Permalink
fs: reduce throwing unnecessary errors on glob
Browse files Browse the repository at this point in the history
PR-URL: #53632
Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
  • Loading branch information
anonrig authored and aduh95 committed Jul 16, 2024
1 parent 076e82c commit 628a539
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions lib/internal/fs/glob.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,30 @@ function lazyMinimatch() {
const isWindows = process.platform === 'win32';
const isOSX = process.platform === 'darwin';

/**
* @param {string} path
* @returns {Promise<DirentFromStats|null>}
*/
async function getDirent(path) {
return new DirentFromStats(basename(path), await lstat(path), path);
let stat;
try {
stat = await lstat(path);
} catch {
return null;
}
return new DirentFromStats(basename(path), stat, path);
}

/**
* @param {string} path
* @returns {DirentFromStats|null}
*/
function getDirentSync(path) {
return new DirentFromStats(basename(path), lstatSync(path), path);
const stat = lstatSync(path, { throwIfNoEntry: false });
if (stat === undefined) {
return null;
}
return new DirentFromStats(basename(path), stat, path);
}

class Cache {
Expand All @@ -56,7 +74,7 @@ class Cache {
if (cached) {
return cached;
}
const promise = PromisePrototypeThen(getDirent(path), null, () => null);
const promise = getDirent(path);
this.#statsCache.set(path, promise);
return promise;
}
Expand All @@ -65,12 +83,7 @@ class Cache {
if (cached) {
return cached;
}
let val;
try {
val = getDirentSync(path);
} catch {
val = null;
}
const val = getDirentSync(path);
this.#statsCache.set(path, val);
return val;
}
Expand Down

0 comments on commit 628a539

Please sign in to comment.