Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fs: reduce throwing unnecessary errors on glob
  • Loading branch information
anonrig committed Jun 28, 2024
commit 94c39c9eb36e8632857799255484a41a7fe2d808
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