Skip to content

fix(ext/node): support throwIfNoEntry option in fs.lstatSync #24006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2024
Merged
Show file tree
Hide file tree
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
24 changes: 20 additions & 4 deletions ext/node/polyfills/_fs/_fs_lstat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
}
}
9 changes: 9 additions & 0 deletions tests/unit_node/fs_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
constants,
createWriteStream,
existsSync,
lstatSync,
mkdtempSync,
promises,
readFileSync,
Expand Down Expand Up @@ -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);
},
);