Skip to content
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

feat(ext/node): Promisify fstat and implement FileHandle#stat #26719

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions ext/node/polyfills/_fs/_fs_common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,5 @@ export function makeCallback(

return (...args: unknown[]) => Reflect.apply(cb!, this, args);
}

export type { BigIntStats, Stats } from "ext:deno_node/_fs/_fs_stat.ts";
21 changes: 21 additions & 0 deletions ext/node/polyfills/_fs/_fs_fstat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Stats>;
export function fstatPromise(
fd: number,
options: { bigint: false },
): Promise<Stats>;
export function fstatPromise(
fd: number,
options: { bigint: true },
): Promise<BigIntStats>;
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);
});
});
}
3 changes: 2 additions & 1 deletion ext/node/polyfills/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -174,6 +174,7 @@ const promises = {
lstat: lstatPromise,
stat: statPromise,
statfs: statfsPromise,
fstat: fstatPromise,
link: linkPromise,
unlink: unlinkPromise,
chmod: chmodPromise,
Expand Down
1 change: 1 addition & 0 deletions ext/node/polyfills/fs/promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 10 additions & 1 deletion ext/node/polyfills/internal/fs/handle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import { EventEmitter } from "node:events";
import { Buffer } from "node:buffer";
import { promises, read, write } from "node:fs";
import {
BigIntStats,
BinaryOptionsArgument,
FileOptionsArgument,
ReadOptions,
Stats,
TextOptionsArgument,
} from "ext:deno_node/_fs/_fs_common.ts";
import { core } from "ext:core/mod.js";
Expand Down Expand Up @@ -141,6 +143,13 @@ export class FileHandle extends EventEmitter {
// Note that Deno.close is not async
return Promise.resolve(core.close(this.fd));
}

stat(): Promise<Stats>;
stat(options: { bigint: false }): Promise<Stats>;
stat(options: { bigint: true }): Promise<BigIntStats>;
stat(options?: { bigint: boolean }): Promise<Stats | BigIntStats> {
return fsCall(promises.fstat, this, options);
}
}

function fsCall(fn, handle, ...args) {
Expand All @@ -152,7 +161,7 @@ function fsCall(fn, handle, ...args) {
});
}

return fn(handle, ...args);
return fn(handle.fd, ...args);
Copy link
Contributor Author

@manzt manzt Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test as well for fh.writeFile() to make sure this change is OK.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should rather let fstat accept FileHandle, which improves the compatibility further (See this change for reference #25555 )

This fsCall util comes from this part in Node.js ( https://github.com/nodejs/node/blob/ccac4ee19d508abaf38bbabb87288ddeec7fcc21/lib/internal/fs/promises.js#L452-L470 ). I think it would be better to keep the code as aligned to node.js as possible.

}

export default {
Expand Down
24 changes: 24 additions & 0 deletions tests/unit_node/_fs/_fs_handle_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Loading