From 09ee9a828009ab1fbe59f611da64f48f9e9e573b Mon Sep 17 00:00:00 2001 From: Nikolai Vavilov Date: Sat, 6 Jun 2020 22:56:49 +0300 Subject: feat(std/node): Buffer (#5925) --- std/node/_fs/_fs_readFile.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'std/node/_fs/_fs_readFile.ts') diff --git a/std/node/_fs/_fs_readFile.ts b/std/node/_fs/_fs_readFile.ts index ccea71cd6..448045fd2 100644 --- a/std/node/_fs/_fs_readFile.ts +++ b/std/node/_fs/_fs_readFile.ts @@ -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, - data: MaybeEmpty + data: MaybeEmpty ) => 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( @@ -37,9 +37,9 @@ export function readFile( const encoding = getEncoding(optOrCallback); - intoCallbackAPIWithIntercept( + intoCallbackAPIWithIntercept( denoReadFile, - (data: Uint8Array): string | Uint8Array => maybeDecode(data, encoding), + (data: Uint8Array): string | Buffer => maybeDecode(data, encoding), cb, path ); @@ -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)); } -- cgit v1.2.3