diff --git a/cli/tests/integration/node_unit_tests.rs b/cli/tests/integration/node_unit_tests.rs index 1cd52f61da..f62c8761cf 100644 --- a/cli/tests/integration/node_unit_tests.rs +++ b/cli/tests/integration/node_unit_tests.rs @@ -25,6 +25,7 @@ util::unit_test_factory!( _fs_fsync_test = _fs / _fs_fsync_test, _fs_ftruncate_test = _fs / _fs_ftruncate_test, _fs_futimes_test = _fs / _fs_futimes_test, + _fs_handle_test = _fs / _fs_handle_test, _fs_link_test = _fs / _fs_link_test, _fs_lstat_test = _fs / _fs_lstat_test, _fs_mkdir_test = _fs / _fs_mkdir_test, diff --git a/cli/tests/unit_node/_fs/_fs_handle_test.ts b/cli/tests/unit_node/_fs/_fs_handle_test.ts new file mode 100644 index 0000000000..c1e5ef8713 --- /dev/null +++ b/cli/tests/unit_node/_fs/_fs_handle_test.ts @@ -0,0 +1,20 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +import * as path from "../../../../test_util/std/path/mod.ts"; +import { + assert, + assertEquals, +} from "../../../../test_util/std/testing/asserts.ts"; + +const moduleDir = path.dirname(path.fromFileUrl(import.meta.url)); +const testData = path.resolve(moduleDir, "testdata", "hello.txt"); + +Deno.test("readFileSuccess", async function () { + const fs = await import("node:fs/promises"); + const fileHandle = await fs.open(testData); + const data = await fileHandle.readFile(); + + assert(data instanceof Uint8Array); + assertEquals(new TextDecoder().decode(data as Uint8Array), "hello world"); + + Deno.close(fileHandle.fd); +}); diff --git a/ext/node/lib.rs b/ext/node/lib.rs index 6ac64eb2eb..e77b3c0896 100644 --- a/ext/node/lib.rs +++ b/ext/node/lib.rs @@ -392,6 +392,7 @@ deno_core::extension!(deno_node, "internal/fixed_queue.ts", "internal/fs/streams.mjs", "internal/fs/utils.mjs", + "internal/fs/handle.ts", "internal/hide_stack_frames.ts", "internal/http.ts", "internal/idna.ts", diff --git a/ext/node/polyfills/_fs/_fs_open.ts b/ext/node/polyfills/_fs/_fs_open.ts index 135520591d..2e29f3df10 100644 --- a/ext/node/polyfills/_fs/_fs_open.ts +++ b/ext/node/polyfills/_fs/_fs_open.ts @@ -8,10 +8,10 @@ import { O_WRONLY, } from "ext:deno_node/_fs/_fs_constants.ts"; import { getOpenOptions } from "ext:deno_node/_fs/_fs_common.ts"; -import { promisify } from "ext:deno_node/internal/util.mjs"; import { parseFileMode } from "ext:deno_node/internal/validators.mjs"; import { ERR_INVALID_ARG_TYPE } from "ext:deno_node/internal/errors.ts"; import { getValidatedPath } from "ext:deno_node/internal/fs/utils.mjs"; +import { FileHandle } from "ext:deno_node/internal/fs/handle.ts"; import type { Buffer } from "ext:deno_node/buffer.ts"; function existsSync(filePath: string | URL): boolean { @@ -139,16 +139,18 @@ export function open( } } -export const openPromise = promisify(open) as ( - & ((path: string | Buffer | URL) => Promise) - & ((path: string | Buffer | URL, flags: openFlags) => Promise) - & ((path: string | Buffer | URL, mode?: number) => Promise) - & (( - path: string | Buffer | URL, - flags?: openFlags, - mode?: number, - ) => Promise) -); +export function openPromise( + path: string | Buffer | URL, + flags?: openFlags = "r", + mode? = 0o666, +): Promise { + return new Promise((resolve, reject) => { + open(path, flags, mode, (err, fd) => { + if (err) reject(err); + else resolve(new FileHandle(fd)); + }); + }); +} export function openSync(path: string | Buffer | URL): number; export function openSync( diff --git a/ext/node/polyfills/_fs/_fs_readFile.ts b/ext/node/polyfills/_fs/_fs_readFile.ts index 0ff8a311ad..b3bd5b94c7 100644 --- a/ext/node/polyfills/_fs/_fs_readFile.ts +++ b/ext/node/polyfills/_fs/_fs_readFile.ts @@ -6,6 +6,8 @@ import { TextOptionsArgument, } from "ext:deno_node/_fs/_fs_common.ts"; import { Buffer } from "ext:deno_node/buffer.ts"; +import { readAll } from "ext:deno_io/12_io.js"; +import { FileHandle } from "ext:deno_node/internal/fs/handle.ts"; import { fromFileUrl } from "ext:deno_node/path.ts"; import { BinaryEncodings, @@ -32,25 +34,26 @@ type TextCallback = (err: Error | null, data?: string) => void; type BinaryCallback = (err: Error | null, data?: Buffer) => void; type GenericCallback = (err: Error | null, data?: string | Buffer) => void; type Callback = TextCallback | BinaryCallback | GenericCallback; +type Path = string | URL | FileHandle; export function readFile( - path: string | URL, + path: Path, options: TextOptionsArgument, callback: TextCallback, ): void; export function readFile( - path: string | URL, + path: Path, options: BinaryOptionsArgument, callback: BinaryCallback, ): void; export function readFile( - path: string | URL, + path: Path, options: null | undefined | FileOptionsArgument, callback: BinaryCallback, ): void; export function readFile(path: string | URL, callback: BinaryCallback): void; export function readFile( - path: string | URL, + path: Path, optOrCallback?: FileOptionsArgument | Callback | null | undefined, callback?: Callback, ) { @@ -64,7 +67,13 @@ export function readFile( const encoding = getEncoding(optOrCallback); - const p = Deno.readFile(path); + let p: Promise; + if (path instanceof FileHandle) { + const fsFile = new Deno.FsFile(path.fd); + p = readAll(fsFile); + } else { + p = Deno.readFile(path); + } if (cb) { p.then((data: Uint8Array) => { @@ -79,9 +88,9 @@ export function readFile( } export const readFilePromise = promisify(readFile) as ( - & ((path: string | URL, opt: TextOptionsArgument) => Promise) - & ((path: string | URL, opt?: BinaryOptionsArgument) => Promise) - & ((path: string | URL, opt?: FileOptionsArgument) => Promise) + & ((path: Path, opt: TextOptionsArgument) => Promise) + & ((path: Path, opt?: BinaryOptionsArgument) => Promise) + & ((path: Path, opt?: FileOptionsArgument) => Promise) ); export function readFileSync( diff --git a/ext/node/polyfills/internal/fs/handle.ts b/ext/node/polyfills/internal/fs/handle.ts new file mode 100644 index 0000000000..a369a4a4d7 --- /dev/null +++ b/ext/node/polyfills/internal/fs/handle.ts @@ -0,0 +1,31 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +import { EventEmitter } from "ext:deno_node/events.ts"; +import { Buffer } from "ext:deno_node/buffer.ts"; +import { promises } from "ext:deno_node/fs.ts"; +import { + BinaryOptionsArgument, + FileOptionsArgument, + TextOptionsArgument, +} from "ext:deno_node/_fs/_fs_common.ts"; + +export class FileHandle extends EventEmitter { + #rid: number; + constructor(rid: number) { + super(); + this.rid = rid; + } + + get fd() { + return this.rid; + } + + readFile( + opt?: TextOptionsArgument | BinaryOptionsArgument | FileOptionsArgument, + ): Promise { + return promises.readFile(this, opt); + } +} + +export default { + FileHandle, +};