Skip to content

Commit

Permalink
feat(std/node): Buffer (denoland#5925)
Browse files Browse the repository at this point in the history
  • Loading branch information
seishun committed Jun 6, 2020
1 parent 26287ef commit 09ee9a8
Show file tree
Hide file tree
Showing 5 changed files with 436 additions and 11 deletions.
5 changes: 5 additions & 0 deletions std/node/_fs/_fs_common.ts
Expand Up @@ -35,6 +35,11 @@ export function getEncoding(
const encoding =
typeof optOrCallback === "string" ? optOrCallback : optOrCallback.encoding;
if (!encoding) return null;
return encoding;
}

export function checkEncoding(encoding: string | null): string | null {
if (!encoding) return null;
if (encoding === "utf8" || encoding === "utf-8") {
return "utf8";
}
Expand Down
18 changes: 9 additions & 9 deletions std/node/_fs/_fs_readFile.ts
Expand Up @@ -3,23 +3,23 @@
import { intoCallbackAPIWithIntercept, MaybeEmpty } from "../_utils.ts";

import { getEncoding, FileOptions } from "./_fs_common.ts";
import { Buffer } from "../buffer.ts";
import { fromFileUrl } from "../path.ts";

const { readFile: denoReadFile, readFileSync: denoReadFileSync } = Deno;

type ReadFileCallback = (
err: MaybeEmpty<Error>,
data: MaybeEmpty<string | Uint8Array>
data: MaybeEmpty<string | Buffer>
) => void;

function maybeDecode(
data: Uint8Array,
encoding: string | null
): string | Uint8Array {
if (encoding === "utf8") {
return new TextDecoder().decode(data);
}
return data;
): string | Buffer {
const buffer = new Buffer(data.buffer, data.byteOffset, data.byteLength);
if (encoding) return buffer.toString(encoding);
return buffer;
}

export function readFile(
Expand All @@ -37,9 +37,9 @@ export function readFile(

const encoding = getEncoding(optOrCallback);

intoCallbackAPIWithIntercept<Uint8Array, string | Uint8Array>(
intoCallbackAPIWithIntercept<Uint8Array, string | Buffer>(
denoReadFile,
(data: Uint8Array): string | Uint8Array => maybeDecode(data, encoding),
(data: Uint8Array): string | Buffer => maybeDecode(data, encoding),
cb,
path
);
Expand All @@ -48,7 +48,7 @@ export function readFile(
export function readFileSync(
path: string | URL,
opt?: FileOptions | string
): string | Uint8Array {
): string | Buffer {
path = path instanceof URL ? fromFileUrl(path) : path;
return maybeDecode(denoReadFileSync(path), getEncoding(opt));
}
5 changes: 3 additions & 2 deletions std/node/_fs/_fs_writeFile.ts
Expand Up @@ -7,6 +7,7 @@ import {
CallbackWithError,
isFileOptions,
getEncoding,
checkEncoding,
getOpenOptions,
} from "./_fs_common.ts";

Expand Down Expand Up @@ -35,7 +36,7 @@ export function writeFile(
? options.mode
: undefined;

const encoding = getEncoding(options) || "utf8";
const encoding = checkEncoding(getEncoding(options)) || "utf8";
const openOptions = getOpenOptions(flag || "w");

if (typeof data === "string" && encoding === "utf8")
Expand Down Expand Up @@ -82,7 +83,7 @@ export function writeFileSync(
? options.mode
: undefined;

const encoding = getEncoding(options) || "utf8";
const encoding = checkEncoding(getEncoding(options)) || "utf8";
const openOptions = getOpenOptions(flag || "w");

if (typeof data === "string" && encoding === "utf8")
Expand Down

0 comments on commit 09ee9a8

Please sign in to comment.