From 6b7e4c331b5c6b5177a0bb9c76eaf4036e9ed847 Mon Sep 17 00:00:00 2001 From: Trevor Manz Date: Mon, 25 Nov 2024 01:02:38 -0500 Subject: [PATCH] fix(ext/node): add `fs.promises.fstat` and `FileHandle#stat` (#26719) Co-authored-by: Yoshiya Hinosawa --- ext/node/polyfills/_fs/_fs_fstat.ts | 21 +++++++++++++++++++++ ext/node/polyfills/fs.ts | 3 ++- ext/node/polyfills/fs/promises.ts | 1 + ext/node/polyfills/internal/fs/handle.ts | 10 +++++++++- tests/unit_node/_fs/_fs_handle_test.ts | 24 ++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 2 deletions(-) diff --git a/ext/node/polyfills/_fs/_fs_fstat.ts b/ext/node/polyfills/_fs/_fs_fstat.ts index c1722487eb..1a845dfff4 100644 --- a/ext/node/polyfills/_fs/_fs_fstat.ts +++ b/ext/node/polyfills/_fs/_fs_fstat.ts @@ -63,3 +63,24 @@ export function fstatSync( const origin = new FsFile(fd, Symbol.for("Deno.internal.FsFile")).statSync(); return CFISBIS(origin, options?.bigint || false); } + +export function fstatPromise(fd: number): Promise; +export function fstatPromise( + fd: number, + options: { bigint: false }, +): Promise; +export function fstatPromise( + fd: number, + options: { bigint: true }, +): Promise; +export function fstatPromise( + fd: number, + options?: statOptions, +): Stats | BigIntStats { + return new Promise((resolve, reject) => { + fstat(fd, options, (err, stats) => { + if (err) reject(err); + else resolve(stats); + }); + }); +} diff --git a/ext/node/polyfills/fs.ts b/ext/node/polyfills/fs.ts index 7a3cf4e67f..cbdc36afe5 100644 --- a/ext/node/polyfills/fs.ts +++ b/ext/node/polyfills/fs.ts @@ -23,7 +23,7 @@ import Dir from "ext:deno_node/_fs/_fs_dir.ts"; import Dirent from "ext:deno_node/_fs/_fs_dirent.ts"; import { exists, existsSync } from "ext:deno_node/_fs/_fs_exists.ts"; import { fdatasync, fdatasyncSync } from "ext:deno_node/_fs/_fs_fdatasync.ts"; -import { fstat, fstatSync } from "ext:deno_node/_fs/_fs_fstat.ts"; +import { fstat, fstatPromise, fstatSync } from "ext:deno_node/_fs/_fs_fstat.ts"; import { fsync, fsyncSync } from "ext:deno_node/_fs/_fs_fsync.ts"; import { ftruncate, ftruncateSync } from "ext:deno_node/_fs/_fs_ftruncate.ts"; import { futimes, futimesSync } from "ext:deno_node/_fs/_fs_futimes.ts"; @@ -174,6 +174,7 @@ const promises = { lstat: lstatPromise, stat: statPromise, statfs: statfsPromise, + fstat: fstatPromise, link: linkPromise, unlink: unlinkPromise, chmod: chmodPromise, diff --git a/ext/node/polyfills/fs/promises.ts b/ext/node/polyfills/fs/promises.ts index 3e5329dbbe..a5125dac8d 100644 --- a/ext/node/polyfills/fs/promises.ts +++ b/ext/node/polyfills/fs/promises.ts @@ -16,6 +16,7 @@ export const readlink = fsPromises.readlink; export const symlink = fsPromises.symlink; export const lstat = fsPromises.lstat; export const stat = fsPromises.stat; +export const fstat = fsPromises.fstat; export const link = fsPromises.link; export const unlink = fsPromises.unlink; export const chmod = fsPromises.chmod; diff --git a/ext/node/polyfills/internal/fs/handle.ts b/ext/node/polyfills/internal/fs/handle.ts index fc3a7ae205..9ec0fc97e2 100644 --- a/ext/node/polyfills/internal/fs/handle.ts +++ b/ext/node/polyfills/internal/fs/handle.ts @@ -6,6 +6,7 @@ import { EventEmitter } from "node:events"; import { Buffer } from "node:buffer"; import { promises, read, write } from "node:fs"; +export type { BigIntStats, Stats } from "ext:deno_node/_fs/_fs_stat.ts"; import { BinaryOptionsArgument, FileOptionsArgument, @@ -141,6 +142,13 @@ export class FileHandle extends EventEmitter { // Note that Deno.close is not async return Promise.resolve(core.close(this.fd)); } + + stat(): Promise; + stat(options: { bigint: false }): Promise; + stat(options: { bigint: true }): Promise; + stat(options?: { bigint: boolean }): Promise { + return fsCall(promises.fstat, this, options); + } } function fsCall(fn, handle, ...args) { @@ -152,7 +160,7 @@ function fsCall(fn, handle, ...args) { }); } - return fn(handle, ...args); + return fn(handle.fd, ...args); } export default { diff --git a/tests/unit_node/_fs/_fs_handle_test.ts b/tests/unit_node/_fs/_fs_handle_test.ts index 755e091fd7..e26b82aa06 100644 --- a/tests/unit_node/_fs/_fs_handle_test.ts +++ b/tests/unit_node/_fs/_fs_handle_test.ts @@ -93,3 +93,27 @@ Deno.test("[node/fs filehandle.write] Write from string", async function () { assertEquals(res.bytesWritten, 11); assertEquals(decoder.decode(data), "hello world"); }); + +Deno.test("[node/fs filehandle.stat] Get file status", async function () { + const fileHandle = await fs.open(testData); + const stat = await fileHandle.stat(); + + assertEquals(stat.isFile(), true); + assertEquals(stat.size, "hello world".length); + + await fileHandle.close(); +}); + +Deno.test("[node/fs filehandle.writeFile] Write to file", async function () { + const tempFile: string = await Deno.makeTempFile(); + const fileHandle = await fs.open(tempFile, "w"); + + const str = "hello world"; + await fileHandle.writeFile(str); + + const data = Deno.readFileSync(tempFile); + await Deno.remove(tempFile); + await fileHandle.close(); + + assertEquals(decoder.decode(data), "hello world"); +});