diff options
author | Nikolai Vavilov <vvnicholas@gmail.com> | 2020-06-06 22:56:49 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-06 15:56:49 -0400 |
commit | 09ee9a828009ab1fbe59f611da64f48f9e9e573b (patch) | |
tree | 9333c9878682afe2f473b0203afd9a63dc8fd820 /std/node/_fs/_fs_readFile.ts | |
parent | 26287ef87b2b238e25a1ab44a43cde24eea15bfc (diff) |
feat(std/node): Buffer (#5925)
Diffstat (limited to 'std/node/_fs/_fs_readFile.ts')
-rw-r--r-- | std/node/_fs/_fs_readFile.ts | 18 |
1 files changed, 9 insertions, 9 deletions
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<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( @@ -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 ); @@ -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)); } |