diff options
author | Yoshiya Hinosawa <stibium121@gmail.com> | 2024-09-11 18:08:40 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-11 11:08:40 +0200 |
commit | 8bdd364dd568f93097ecee41e66c74d16d75c015 (patch) | |
tree | ae124042299ed91ee81c0a9b455e1f972be1a9e9 /ext/node | |
parent | 87bc47b3bf9afe26e33450757b13c3b14b8703d8 (diff) |
fix(ext/node): add `FileHandle#writeFile` (#25555)
This PR adds `writeFile` methods of `FileHandle` class
https://nodejs.org/api/fs.html#filehandlewritefiledata-options
Diffstat (limited to 'ext/node')
-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, }; |