summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cli/tests/integration/node_unit_tests.rs1
-rw-r--r--cli/tests/unit_node/_fs/_fs_handle_test.ts20
-rw-r--r--ext/node/lib.rs1
-rw-r--r--ext/node/polyfills/_fs/_fs_open.ts24
-rw-r--r--ext/node/polyfills/_fs/_fs_readFile.ts25
-rw-r--r--ext/node/polyfills/internal/fs/handle.ts31
6 files changed, 83 insertions, 19 deletions
diff --git a/cli/tests/integration/node_unit_tests.rs b/cli/tests/integration/node_unit_tests.rs
index 1cd52f61d..f62c8761c 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 000000000..c1e5ef871
--- /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 6ac64eb2e..e77b3c089 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 135520591..2e29f3df1 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<number>)
- & ((path: string | Buffer | URL, flags: openFlags) => Promise<number>)
- & ((path: string | Buffer | URL, mode?: number) => Promise<number>)
- & ((
- path: string | Buffer | URL,
- flags?: openFlags,
- mode?: number,
- ) => Promise<number>)
-);
+export function openPromise(
+ path: string | Buffer | URL,
+ flags?: openFlags = "r",
+ mode? = 0o666,
+): Promise<FileHandle> {
+ 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 0ff8a311a..b3bd5b94c 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<Uint8Array>;
+ 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<string>)
- & ((path: string | URL, opt?: BinaryOptionsArgument) => Promise<Buffer>)
- & ((path: string | URL, opt?: FileOptionsArgument) => Promise<Buffer>)
+ & ((path: Path, opt: TextOptionsArgument) => Promise<string>)
+ & ((path: Path, opt?: BinaryOptionsArgument) => Promise<Buffer>)
+ & ((path: Path, opt?: FileOptionsArgument) => Promise<Buffer>)
);
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 000000000..a369a4a4d
--- /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<string | Buffer> {
+ return promises.readFile(this, opt);
+ }
+}
+
+export default {
+ FileHandle,
+};