summaryrefslogtreecommitdiff
path: root/std/node/_fs/_fs_common.ts
diff options
context:
space:
mode:
authorBenjamin Lupton <b@lupton.cc>2020-06-24 12:32:43 +1000
committerGitHub <noreply@github.com>2020-06-23 22:32:43 -0400
commit49c54c0805ab26410a62e0251fee3a28b98c0e13 (patch)
treeb9fcb2713622f2739557f2caffd51d8bb454300b /std/node/_fs/_fs_common.ts
parentd16337cc9c59732fe81655482e08b72d844472e6 (diff)
fix(std/node): fix readFile types, add encoding types (#6451)
Diffstat (limited to 'std/node/_fs/_fs_common.ts')
-rw-r--r--std/node/_fs/_fs_common.ts36
1 files changed, 29 insertions, 7 deletions
diff --git a/std/node/_fs/_fs_common.ts b/std/node/_fs/_fs_common.ts
index bf7c0f675..43d3b8064 100644
--- a/std/node/_fs/_fs_common.ts
+++ b/std/node/_fs/_fs_common.ts
@@ -4,11 +4,32 @@ import { notImplemented } from "../_utils.ts";
export type CallbackWithError = (err?: Error | null) => void;
+export type TextEncodings =
+ | "ascii"
+ | "utf8"
+ | "utf-8"
+ | "utf16le"
+ | "ucs2"
+ | "ucs-2"
+ | "base64"
+ | "latin1"
+ | "hex";
+export type BinaryEncodings = "binary";
+export type Encodings = TextEncodings | BinaryEncodings;
+
export interface FileOptions {
- encoding?: string;
+ encoding?: Encodings;
flag?: string;
}
+export type TextOptionsArgument =
+ | TextEncodings
+ | ({ encoding: TextEncodings } & FileOptions);
+export type BinaryOptionsArgument =
+ | BinaryEncodings
+ | ({ encoding: BinaryEncodings } & FileOptions);
+export type FileOptionsArgument = Encodings | FileOptions;
+
export interface WriteFileOptions extends FileOptions {
mode?: number;
}
@@ -26,8 +47,8 @@ export function isFileOptions(
}
export function getEncoding(
- optOrCallback?: FileOptions | WriteFileOptions | Function | string
-): string | null {
+ optOrCallback?: FileOptions | WriteFileOptions | Function | Encodings | null
+): Encodings | null {
if (!optOrCallback || typeof optOrCallback === "function") {
return null;
}
@@ -38,13 +59,15 @@ export function getEncoding(
return encoding;
}
-export function checkEncoding(encoding: string | null): string | null {
+export function checkEncoding(encoding: Encodings | null): Encodings | null {
if (!encoding) return null;
if (encoding === "utf8" || encoding === "utf-8") {
return "utf8";
}
- if (encoding === "buffer") {
- return "buffer";
+ if (encoding === "binary") {
+ return "binary";
+ // before this was buffer, however buffer is not used in Node
+ // node -e "require('fs').readFile('../world.txt', 'buffer', console.log)"
}
const notImplementedEncodings = [
@@ -53,7 +76,6 @@ export function checkEncoding(encoding: string | null): string | null {
"base64",
"hex",
"ascii",
- "binary",
"ucs2",
];