From 2af0c0a3c6ea017bef2481c1a1ce0806457d5427 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Tue, 23 Jan 2024 12:59:39 +0900 Subject: [PATCH] fix(ext/node): fix no arg call of fs.promises.readFile (#22030) --- cli/tests/unit_node/_fs/_fs_readFile_test.ts | 7 ++++++- ext/node/polyfills/_fs/_fs_readFile.ts | 18 ++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/cli/tests/unit_node/_fs/_fs_readFile_test.ts b/cli/tests/unit_node/_fs/_fs_readFile_test.ts index 7d9e117cea..05cb909747 100644 --- a/cli/tests/unit_node/_fs/_fs_readFile_test.ts +++ b/cli/tests/unit_node/_fs/_fs_readFile_test.ts @@ -1,6 +1,6 @@ // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. import { assertCallbackErrorUncaught } from "../_test_utils.ts"; -import { readFile, readFileSync } from "node:fs"; +import { promises, readFile, readFileSync } from "node:fs"; import * as path from "../../../../test_util/std/path/mod.ts"; import { assert, assertEquals } from "../../../../test_util/std/assert/mod.ts"; @@ -116,3 +116,8 @@ Deno.test("[std/node/fs] readFile callback isn't called twice if error is thrown }, }); }); + +Deno.test("fs.promises.readFile with no arg call rejects with error correctly", async () => { + // @ts-ignore no arg call needs to be supported + await promises.readFile().catch((_e) => {}); +}); diff --git a/ext/node/polyfills/_fs/_fs_readFile.ts b/ext/node/polyfills/_fs/_fs_readFile.ts index 415d81a5bb..7e231d8ecd 100644 --- a/ext/node/polyfills/_fs/_fs_readFile.ts +++ b/ext/node/polyfills/_fs/_fs_readFile.ts @@ -18,7 +18,6 @@ import { Encodings, TextEncodings, } from "ext:deno_node/_utils.ts"; -import { promisify } from "ext:deno_node/internal/util.mjs"; function maybeDecode(data: Uint8Array, encoding: TextEncodings): string; function maybeDecode( @@ -91,11 +90,18 @@ export function readFile( } } -export const readFilePromise = promisify(readFile) as ( - & ((path: Path, opt: TextOptionsArgument) => Promise) - & ((path: Path, opt?: BinaryOptionsArgument) => Promise) - & ((path: Path, opt?: FileOptionsArgument) => Promise) -); +export function readFilePromise( + path: Path, + options?: FileOptionsArgument | null | undefined, + // deno-lint-ignore no-explicit-any +): Promise { + return new Promise((resolve, reject) => { + readFile(path, options, (err, data) => { + if (err) reject(err); + else resolve(data); + }); + }); +} export function readFileSync( path: string | URL,