From b6bc4f5206e37faa02697085b00bb476059020e1 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Thu, 11 Jul 2024 14:51:35 -0400 Subject: [PATCH] fs: reduce throwing unnecessary errors on glob MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/53632 Reviewed-By: Vinícius Lourenço Claro Cardoso Reviewed-By: Chemi Atlow Reviewed-By: James M Snell Reviewed-By: Moshe Atlow --- lib/internal/fs/glob.js | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/internal/fs/glob.js b/lib/internal/fs/glob.js index 4dd46ed0b6cb67..3c225900301309 100644 --- a/lib/internal/fs/glob.js +++ b/lib/internal/fs/glob.js @@ -38,12 +38,30 @@ function lazyMinimatch() { const isWindows = process.platform === 'win32'; const isOSX = process.platform === 'darwin'; +/** + * @param {string} path + * @returns {Promise} + */ 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 { @@ -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; } @@ -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; }