diff --git a/ext/node/polyfills/_fs/_fs_lstat.ts b/ext/node/polyfills/_fs/_fs_lstat.ts index c8cdfc4e490e4a..6ce401444f341c 100644 --- a/ext/node/polyfills/_fs/_fs_lstat.ts +++ b/ext/node/polyfills/_fs/_fs_lstat.ts @@ -3,6 +3,7 @@ // TODO(petamoriken): enable prefer-primordials for node polyfills // deno-lint-ignore-file prefer-primordials +import { denoErrorToNodeError } from "ext:deno_node/internal/errors.ts"; import { BigIntStats, CFISBIS, @@ -56,16 +57,31 @@ export const lstatPromise = promisify(lstat) as ( export function lstatSync(path: string | URL): Stats; export function lstatSync( path: string | URL, - options: { bigint: false }, + options: { bigint: false; throwIfNoEntry?: boolean }, ): Stats; export function lstatSync( path: string | URL, - options: { bigint: true }, + options: { bigint: true; throwIfNoEntry?: boolean }, ): BigIntStats; export function lstatSync( path: string | URL, options?: statOptions, ): Stats | BigIntStats { - const origin = Deno.lstatSync(path); - return CFISBIS(origin, options?.bigint || false); + try { + const origin = Deno.lstatSync(path); + return CFISBIS(origin, options?.bigint || false); + } catch (err) { + if ( + options?.throwIfNoEntry === false && + err instanceof Deno.errors.NotFound + ) { + return; + } + + if (err instanceof Error) { + throw denoErrorToNodeError(err, { syscall: "stat" }); + } else { + throw err; + } + } } diff --git a/tests/unit_node/fs_test.ts b/tests/unit_node/fs_test.ts index e62a246fade669..a241189a5eab68 100644 --- a/tests/unit_node/fs_test.ts +++ b/tests/unit_node/fs_test.ts @@ -7,6 +7,7 @@ import { constants, createWriteStream, existsSync, + lstatSync, mkdtempSync, promises, readFileSync, @@ -156,3 +157,11 @@ Deno.test("[node/fs createWriteStream", async () => { await Deno.remove(tempDir, { recursive: true }); } }); + +Deno.test( + "[node/fs lstatSync] supports throwIfNoEntry option", + () => { + const result = lstatSync("non-existing-path", { throwIfNoEntry: false }); + assertEquals(result, undefined); + }, +);