diff options
Diffstat (limited to 'ext/node/polyfills/_fs')
-rw-r--r-- | ext/node/polyfills/_fs/_fs_open.ts | 24 | ||||
-rw-r--r-- | ext/node/polyfills/_fs/_fs_readFile.ts | 25 |
2 files changed, 30 insertions, 19 deletions
diff --git a/ext/node/polyfills/_fs/_fs_open.ts b/ext/node/polyfills/_fs/_fs_open.ts index 135520591..2e29f3df1 100644 --- a/ext/node/polyfills/_fs/_fs_open.ts +++ b/ext/node/polyfills/_fs/_fs_open.ts @@ -8,10 +8,10 @@ import { O_WRONLY, } from "ext:deno_node/_fs/_fs_constants.ts"; import { getOpenOptions } from "ext:deno_node/_fs/_fs_common.ts"; -import { promisify } from "ext:deno_node/internal/util.mjs"; import { parseFileMode } from "ext:deno_node/internal/validators.mjs"; import { ERR_INVALID_ARG_TYPE } from "ext:deno_node/internal/errors.ts"; import { getValidatedPath } from "ext:deno_node/internal/fs/utils.mjs"; +import { FileHandle } from "ext:deno_node/internal/fs/handle.ts"; import type { Buffer } from "ext:deno_node/buffer.ts"; function existsSync(filePath: string | URL): boolean { @@ -139,16 +139,18 @@ export function open( } } -export const openPromise = promisify(open) as ( - & ((path: string | Buffer | URL) => Promise<number>) - & ((path: string | Buffer | URL, flags: openFlags) => Promise<number>) - & ((path: string | Buffer | URL, mode?: number) => Promise<number>) - & (( - path: string | Buffer | URL, - flags?: openFlags, - mode?: number, - ) => Promise<number>) -); +export function openPromise( + path: string | Buffer | URL, + flags?: openFlags = "r", + mode? = 0o666, +): Promise<FileHandle> { + return new Promise((resolve, reject) => { + open(path, flags, mode, (err, fd) => { + if (err) reject(err); + else resolve(new FileHandle(fd)); + }); + }); +} export function openSync(path: string | Buffer | URL): number; export function openSync( diff --git a/ext/node/polyfills/_fs/_fs_readFile.ts b/ext/node/polyfills/_fs/_fs_readFile.ts index 0ff8a311a..b3bd5b94c 100644 --- a/ext/node/polyfills/_fs/_fs_readFile.ts +++ b/ext/node/polyfills/_fs/_fs_readFile.ts @@ -6,6 +6,8 @@ import { TextOptionsArgument, } from "ext:deno_node/_fs/_fs_common.ts"; import { Buffer } from "ext:deno_node/buffer.ts"; +import { readAll } from "ext:deno_io/12_io.js"; +import { FileHandle } from "ext:deno_node/internal/fs/handle.ts"; import { fromFileUrl } from "ext:deno_node/path.ts"; import { BinaryEncodings, @@ -32,25 +34,26 @@ type TextCallback = (err: Error | null, data?: string) => void; type BinaryCallback = (err: Error | null, data?: Buffer) => void; type GenericCallback = (err: Error | null, data?: string | Buffer) => void; type Callback = TextCallback | BinaryCallback | GenericCallback; +type Path = string | URL | FileHandle; export function readFile( - path: string | URL, + path: Path, options: TextOptionsArgument, callback: TextCallback, ): void; export function readFile( - path: string | URL, + path: Path, options: BinaryOptionsArgument, callback: BinaryCallback, ): void; export function readFile( - path: string | URL, + path: Path, options: null | undefined | FileOptionsArgument, callback: BinaryCallback, ): void; export function readFile(path: string | URL, callback: BinaryCallback): void; export function readFile( - path: string | URL, + path: Path, optOrCallback?: FileOptionsArgument | Callback | null | undefined, callback?: Callback, ) { @@ -64,7 +67,13 @@ export function readFile( const encoding = getEncoding(optOrCallback); - const p = Deno.readFile(path); + let p: Promise<Uint8Array>; + if (path instanceof FileHandle) { + const fsFile = new Deno.FsFile(path.fd); + p = readAll(fsFile); + } else { + p = Deno.readFile(path); + } if (cb) { p.then((data: Uint8Array) => { @@ -79,9 +88,9 @@ export function readFile( } export const readFilePromise = promisify(readFile) as ( - & ((path: string | URL, opt: TextOptionsArgument) => Promise<string>) - & ((path: string | URL, opt?: BinaryOptionsArgument) => Promise<Buffer>) - & ((path: string | URL, opt?: FileOptionsArgument) => Promise<Buffer>) + & ((path: Path, opt: TextOptionsArgument) => Promise<string>) + & ((path: Path, opt?: BinaryOptionsArgument) => Promise<Buffer>) + & ((path: Path, opt?: FileOptionsArgument) => Promise<Buffer>) ); export function readFileSync( |