diff options
Diffstat (limited to 'std/node')
-rw-r--r-- | std/node/_fs/_fs_readFile.ts | 2 | ||||
-rw-r--r-- | std/node/_fs/promises/_fs_readFile.ts | 17 | ||||
-rw-r--r-- | std/node/_fs/promises/_fs_readFile_test.ts | 37 | ||||
-rw-r--r-- | std/node/_fs/promises/mod.ts | 1 |
4 files changed, 56 insertions, 1 deletions
diff --git a/std/node/_fs/_fs_readFile.ts b/std/node/_fs/_fs_readFile.ts index 900d69f89..ccea71cd6 100644 --- a/std/node/_fs/_fs_readFile.ts +++ b/std/node/_fs/_fs_readFile.ts @@ -24,7 +24,7 @@ function maybeDecode( export function readFile( path: string | URL, - optOrCallback: ReadFileCallback | FileOptions | string, + optOrCallback: ReadFileCallback | FileOptions | string | undefined, callback?: ReadFileCallback ): void { path = path instanceof URL ? fromFileUrl(path) : path; diff --git a/std/node/_fs/promises/_fs_readFile.ts b/std/node/_fs/promises/_fs_readFile.ts new file mode 100644 index 000000000..9e4a4ed43 --- /dev/null +++ b/std/node/_fs/promises/_fs_readFile.ts @@ -0,0 +1,17 @@ +// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { FileOptions } from "../_fs_common.ts"; +import { MaybeEmpty } from "../../_utils.ts"; + +import { readFile as readFileCallback } from "../_fs_readFile.ts"; + +export function readFile( + path: string | URL, + options?: FileOptions | string +): Promise<MaybeEmpty<string | Uint8Array>> { + return new Promise((resolve, reject) => { + readFileCallback(path, options, (err, data): void => { + if (err) return reject(err); + resolve(data); + }); + }); +} diff --git a/std/node/_fs/promises/_fs_readFile_test.ts b/std/node/_fs/promises/_fs_readFile_test.ts new file mode 100644 index 000000000..ac3c8fdda --- /dev/null +++ b/std/node/_fs/promises/_fs_readFile_test.ts @@ -0,0 +1,37 @@ +const { test } = Deno; +import { readFile } from "./_fs_readFile.ts"; +import * as path from "../../../path/mod.ts"; +import { assertEquals, assert } from "../../../testing/asserts.ts"; + +const testData = path.resolve( + path.join("node", "_fs", "testdata", "hello.txt") +); + +test("readFileSuccess", async function () { + const data = await readFile(testData); + + assert(data instanceof Uint8Array); + assertEquals(new TextDecoder().decode(data as Uint8Array), "hello world"); +}); + +test("readFileEncodeUtf8Success", async function () { + const data = await readFile(testData, { encoding: "utf8" }); + + assertEquals(typeof data, "string"); + assertEquals(data as string, "hello world"); +}); + +test("readFileEncodingAsString", async function () { + const data = await readFile(testData, "utf8"); + + assertEquals(typeof data, "string"); + assertEquals(data as string, "hello world"); +}); + +test("readFileError", async function () { + try { + await readFile("invalid-file", "utf8"); + } catch (e) { + assert(e instanceof Deno.errors.NotFound); + } +}); diff --git a/std/node/_fs/promises/mod.ts b/std/node/_fs/promises/mod.ts index 3cb81a422..4cc6462b9 100644 --- a/std/node/_fs/promises/mod.ts +++ b/std/node/_fs/promises/mod.ts @@ -1 +1,2 @@ export { writeFile } from "./_fs_writeFile.ts"; +export { readFile } from "./_fs_readFile.ts"; |