Skip to content

Commit

Permalink
fix(ext/node): fix no arg call of fs.promises.readFile (denoland#22030)
Browse files Browse the repository at this point in the history
  • Loading branch information
kt3k committed Jan 23, 2024
1 parent 7e4145d commit 2af0c0a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
7 changes: 6 additions & 1 deletion cli/tests/unit_node/_fs/_fs_readFile_test.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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) => {});
});
18 changes: 12 additions & 6 deletions ext/node/polyfills/_fs/_fs_readFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -91,11 +90,18 @@ export function readFile(
}
}

export const readFilePromise = promisify(readFile) as (
& ((path: Path, opt: TextOptionsArgument) => Promise<string>)
& ((path: Path, opt?: BinaryOptionsArgument) => Promise<Buffer>)
& ((path: Path, opt?: FileOptionsArgument) => Promise<Buffer>)
);
export function readFilePromise(
path: Path,
options?: FileOptionsArgument | null | undefined,
// deno-lint-ignore no-explicit-any
): Promise<any> {
return new Promise((resolve, reject) => {
readFile(path, options, (err, data) => {
if (err) reject(err);
else resolve(data);
});
});
}

export function readFileSync(
path: string | URL,
Expand Down

0 comments on commit 2af0c0a

Please sign in to comment.