diff options
Diffstat (limited to 'ext/node/polyfills')
-rw-r--r-- | ext/node/polyfills/_fs/_fs_writeFile.ts | 4 | ||||
-rw-r--r-- | ext/node/polyfills/internal/fs/handle.ts | 16 |
2 files changed, 19 insertions, 1 deletions
diff --git a/ext/node/polyfills/_fs/_fs_writeFile.ts b/ext/node/polyfills/_fs/_fs_writeFile.ts index f0014c36d..dd324815b 100644 --- a/ext/node/polyfills/_fs/_fs_writeFile.ts +++ b/ext/node/polyfills/_fs/_fs_writeFile.ts @@ -23,6 +23,7 @@ import { validateStringAfterArrayBufferView, } from "ext:deno_node/internal/fs/utils.mjs"; import { promisify } from "ext:deno_node/internal/util.mjs"; +import { FileHandle } from "ext:deno_node/internal/fs/handle.ts"; import { FsFile } from "ext:deno_fs/30_fs.js"; interface Writer { @@ -30,7 +31,7 @@ interface Writer { } export function writeFile( - pathOrRid: string | number | URL, + pathOrRid: string | number | URL | FileHandle, data: string | Uint8Array, optOrCallback: Encodings | CallbackWithError | WriteFileOptions | undefined, callback?: CallbackWithError, @@ -45,6 +46,7 @@ export function writeFile( } pathOrRid = pathOrRid instanceof URL ? pathFromURL(pathOrRid) : pathOrRid; + pathOrRid = pathOrRid instanceof FileHandle ? pathOrRid.fd : pathOrRid; const flag: string | undefined = isFileOptions(options) ? options.flag diff --git a/ext/node/polyfills/internal/fs/handle.ts b/ext/node/polyfills/internal/fs/handle.ts index e422e2ba0..fc3a7ae20 100644 --- a/ext/node/polyfills/internal/fs/handle.ts +++ b/ext/node/polyfills/internal/fs/handle.ts @@ -133,12 +133,28 @@ export class FileHandle extends EventEmitter { } } + writeFile(data, options): Promise<void> { + return fsCall(promises.writeFile, this, data, options); + } + close(): Promise<void> { // Note that Deno.close is not async return Promise.resolve(core.close(this.fd)); } } +function fsCall(fn, handle, ...args) { + if (handle.fd === -1) { + const err = new Error("file closed"); + throw Object.assign(err, { + code: "EBADF", + syscall: fn.name, + }); + } + + return fn(handle, ...args); +} + export default { FileHandle, }; |