diff options
Diffstat (limited to 'std/node/fs.ts')
-rwxr-xr-x[-rw-r--r--] | std/node/fs.ts | 57 |
1 files changed, 56 insertions, 1 deletions
diff --git a/std/node/fs.ts b/std/node/fs.ts index 539916c9e..eb6830acb 100644..100755 --- a/std/node/fs.ts +++ b/std/node/fs.ts @@ -3,7 +3,12 @@ import { intoCallbackAPIWithIntercept, MaybeEmpty } from "./_utils.ts"; -const { readFile: denoReadFile, readFileSync: denoReadFileSync } = Deno; +const { + readFile: denoReadFile, + readFileSync: denoReadFileSync, + readlink: denoReadlink, + readlinkSync: denoReadlinkSync +} = Deno; type ReadFileCallback = ( err: MaybeEmpty<Error>, @@ -15,6 +20,15 @@ interface ReadFileOptions { flag?: string; } +type ReadlinkCallback = ( + err: MaybeEmpty<Error>, + linkString: MaybeEmpty<string | Uint8Array> +) => void; + +interface ReadlinkOptions { + encoding?: string | null; +} + function getEncoding( optOrCallback?: ReadFileOptions | ReadFileCallback ): string | null { @@ -27,6 +41,8 @@ function getEncoding( optOrCallback.encoding === "utf-8" ) { return "utf8"; + } else if (optOrCallback.encoding === "buffer") { + return "buffer"; } else { notImplemented(); } @@ -45,6 +61,16 @@ function maybeDecode( return data; } +function maybeEncode( + data: string, + encoding: string | null +): string | Uint8Array { + if (encoding === "buffer") { + return new TextEncoder().encode(data); + } + return data; +} + export function readFile( path: string, optOrCallback: ReadFileCallback | ReadFileOptions, @@ -73,3 +99,32 @@ export function readFileSync( ): string | Uint8Array { return maybeDecode(denoReadFileSync(path), getEncoding(opt)); } + +export function readlink( + path: string, + optOrCallback: ReadlinkCallback | ReadlinkOptions, + callback?: ReadlinkCallback +): void { + let cb: ReadlinkCallback | undefined; + if (typeof optOrCallback === "function") { + cb = optOrCallback; + } else { + cb = callback; + } + + const encoding = getEncoding(optOrCallback); + + intoCallbackAPIWithIntercept<string, Uint8Array | string>( + denoReadlink, + (data: string): string | Uint8Array => maybeEncode(data, encoding), + cb, + path + ); +} + +export function readlinkSync( + path: string, + opt?: ReadlinkOptions +): string | Uint8Array { + return maybeEncode(denoReadlinkSync(path), getEncoding(opt)); +} |