Files
scriptc/tests/corpus/fs-write-file-sync-fd.ts
T
Malte UblandClaude Opus 5.5 071be693d9 Support writeFileSync(fd, data)
Node's writeFileSync accepts an open file descriptor where it accepts a
path: it writes the data at the descriptor's current offset and leaves the
descriptor open, since closeSync owns its lifetime. The static fs surface
declared only the path forms, so `fs.writeFileSync(fd, text)` failed type
mapping — the compiler was missing a documented overload, not a
conversion.

Declare the (fd, data) and (fd, data, "utf8" | "utf-8") forms in the
node-fallback typings, route the call by the first argument's static f64
type to a new fs.writeFileSyncFd libfn, and implement it as
scr_fs_write_file_fd: a write loop over the whole string through the
descriptor-validating scr_fs_write_bytes, resuming short writes, with the
descriptor operation's errno shape on failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 05:38:00 -07:00

15 lines
551 B
TypeScript

// writeFileSync(fd, data): Node accepts an open file descriptor where it
// accepts a path, writes the whole data from the descriptor's current
// offset, and leaves the descriptor open.
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
const scratch = path.join(os.tmpdir(), `scriptc-write-fd-${process.pid}.txt`);
const fd = fs.openSync(scratch, "w");
fs.writeFileSync(fd, "hello ");
fs.writeFileSync(fd, "world");
fs.closeSync(fd);
console.log(fs.readFileSync(scratch, "utf8"));
fs.rmSync(scratch);