diff options
Diffstat (limited to 'std/node/_fs')
-rw-r--r-- | std/node/_fs/_fs_common.ts | 5 | ||||
-rw-r--r-- | std/node/_fs/_fs_readFile.ts | 18 | ||||
-rw-r--r-- | std/node/_fs/_fs_writeFile.ts | 5 |
3 files changed, 17 insertions, 11 deletions
diff --git a/std/node/_fs/_fs_common.ts b/std/node/_fs/_fs_common.ts index 1f00bc481..bf7c0f675 100644 --- a/std/node/_fs/_fs_common.ts +++ b/std/node/_fs/_fs_common.ts @@ -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"; } 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)); } diff --git a/std/node/_fs/_fs_writeFile.ts b/std/node/_fs/_fs_writeFile.ts index 72e75c97a..4ede42638 100644 --- a/std/node/_fs/_fs_writeFile.ts +++ b/std/node/_fs/_fs_writeFile.ts @@ -7,6 +7,7 @@ import { CallbackWithError, isFileOptions, getEncoding, + checkEncoding, getOpenOptions, } from "./_fs_common.ts"; @@ -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") @@ -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") |