diff options
author | Benjamin Lupton <b@lupton.cc> | 2020-06-24 12:32:43 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-23 22:32:43 -0400 |
commit | 49c54c0805ab26410a62e0251fee3a28b98c0e13 (patch) | |
tree | b9fcb2713622f2739557f2caffd51d8bb454300b /std/node/_fs/promises | |
parent | d16337cc9c59732fe81655482e08b72d844472e6 (diff) |
fix(std/node): fix readFile types, add encoding types (#6451)
Diffstat (limited to 'std/node/_fs/promises')
-rw-r--r-- | std/node/_fs/promises/_fs_readFile.ts | 22 | ||||
-rw-r--r-- | std/node/_fs/promises/_fs_readFile_test.ts | 30 | ||||
-rw-r--r-- | std/node/_fs/promises/_fs_writeFile.ts | 4 | ||||
-rw-r--r-- | std/node/_fs/promises/_fs_writeFile_test.ts | 2 |
4 files changed, 43 insertions, 15 deletions
diff --git a/std/node/_fs/promises/_fs_readFile.ts b/std/node/_fs/promises/_fs_readFile.ts index 9e4a4ed43..83ef9ac50 100644 --- a/std/node/_fs/promises/_fs_readFile.ts +++ b/std/node/_fs/promises/_fs_readFile.ts @@ -1,16 +1,28 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { FileOptions } from "../_fs_common.ts"; -import { MaybeEmpty } from "../../_utils.ts"; - +import { + FileOptionsArgument, + BinaryOptionsArgument, + TextOptionsArgument, +} from "../_fs_common.ts"; import { readFile as readFileCallback } from "../_fs_readFile.ts"; export function readFile( path: string | URL, - options?: FileOptions | string -): Promise<MaybeEmpty<string | Uint8Array>> { + options: TextOptionsArgument +): Promise<string>; +export function readFile( + path: string | URL, + options?: BinaryOptionsArgument +): Promise<Uint8Array>; +export function readFile( + path: string | URL, + options?: FileOptionsArgument +): Promise<string | Uint8Array> { return new Promise((resolve, reject) => { readFileCallback(path, options, (err, data): void => { if (err) return reject(err); + if (data == null) + return reject(new Error("Invalid state: data missing, but no error")); resolve(data); }); }); diff --git a/std/node/_fs/promises/_fs_readFile_test.ts b/std/node/_fs/promises/_fs_readFile_test.ts index c92907fec..f5be2318b 100644 --- a/std/node/_fs/promises/_fs_readFile_test.ts +++ b/std/node/_fs/promises/_fs_readFile_test.ts @@ -7,24 +7,38 @@ const testData = path.resolve( ); Deno.test("readFileSuccess", async function () { - const data = await readFile(testData); + const data: Uint8Array = await readFile(testData); assert(data instanceof Uint8Array); - assertEquals(new TextDecoder().decode(data as Uint8Array), "hello world"); + assertEquals(new TextDecoder().decode(data), "hello world"); }); -Deno.test("readFileEncodeUtf8Success", async function () { - const data = await readFile(testData, { encoding: "utf8" }); +Deno.test("readFileBinarySuccess", async function () { + const data: Uint8Array = await readFile(testData, "binary"); + + assert(data instanceof Uint8Array); + assertEquals(new TextDecoder().decode(data), "hello world"); +}); + +Deno.test("readFileBinaryObjectSuccess", async function () { + const data: Uint8Array = await readFile(testData, { encoding: "binary" }); + + assert(data instanceof Uint8Array); + assertEquals(new TextDecoder().decode(data), "hello world"); +}); + +Deno.test("readFileStringObjectSuccess", async function () { + const data: string = await readFile(testData, { encoding: "utf8" }); assertEquals(typeof data, "string"); - assertEquals(data as string, "hello world"); + assertEquals(data, "hello world"); }); -Deno.test("readFileEncodingAsString", async function () { - const data = await readFile(testData, "utf8"); +Deno.test("readFileStringSuccess", async function () { + const data: string = await readFile(testData, "utf8"); assertEquals(typeof data, "string"); - assertEquals(data as string, "hello world"); + assertEquals(data, "hello world"); }); Deno.test("readFileError", async function () { diff --git a/std/node/_fs/promises/_fs_writeFile.ts b/std/node/_fs/promises/_fs_writeFile.ts index e89276c97..4f7c39a6a 100644 --- a/std/node/_fs/promises/_fs_writeFile.ts +++ b/std/node/_fs/promises/_fs_writeFile.ts @@ -1,12 +1,12 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -import { WriteFileOptions } from "../_fs_common.ts"; +import { Encodings, WriteFileOptions } from "../_fs_common.ts"; import { writeFile as writeFileCallback } from "../_fs_writeFile.ts"; export function writeFile( pathOrRid: string | number | URL, data: string | Uint8Array, - options?: string | WriteFileOptions + options?: Encodings | WriteFileOptions ): Promise<void> { return new Promise((resolve, reject) => { writeFileCallback(pathOrRid, data, options, (err?: Error | null) => { diff --git a/std/node/_fs/promises/_fs_writeFile_test.ts b/std/node/_fs/promises/_fs_writeFile_test.ts index 574bbfc35..777971046 100644 --- a/std/node/_fs/promises/_fs_writeFile_test.ts +++ b/std/node/_fs/promises/_fs_writeFile_test.ts @@ -12,6 +12,7 @@ const decoder = new TextDecoder("utf-8"); Deno.test("Invalid encoding results in error()", function testEncodingErrors() { assertThrowsAsync( async () => { + // @ts-expect-error Type '"made-up-encoding"' is not assignable to type await writeFile("some/path", "some data", "made-up-encoding"); }, Error, @@ -20,6 +21,7 @@ Deno.test("Invalid encoding results in error()", function testEncodingErrors() { assertThrowsAsync( async () => { await writeFile("some/path", "some data", { + // @ts-expect-error Type '"made-up-encoding"' is not assignable to type encoding: "made-up-encoding", }); }, |