Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions src/runtime/node/node_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2832,7 +2832,7 @@
}
pub fn from_js(ctx: &JSGlobalObject, arguments: &mut ArgumentsSlice) -> JsResult<Self> {
let fd = FD::from_js_required(ctx, arguments)?;
let mut buffers = VectorArrayBuffer::from_js(
let buffers = VectorArrayBuffer::from_js(
ctx,
arguments.protect_eat_next().ok_or_else(|| {
ctx.throw_invalid_arguments(format_args!("Expected an ArrayBufferView[]"))
Expand All @@ -2841,19 +2841,16 @@
// each element and pin its backing store until completion.
arguments.will_be_async,
)?;
// Node: `typeof position !== 'number'` is coerced to null; libuv
// treats offset < 0 as non-positional readv/writev. Only a
// non-negative number selects preadv/pwritev.
let mut position: Option<u64> = None;
if let Some(pos_value) = arguments.next_eat() {
if !pos_value.is_undefined_or_null() {
if pos_value.is_number() {
position = Some(pos_value.to_int64() as u64);
} else {
// `buffers` never reaches the Unprotect hook on this
// path; drop its element roots and pins here.
buffers.release();
return Err(
ctx.throw_invalid_arguments(format_args!("position must be a number"))
);
if pos_value.is_number() {
let pos = pos_value.to_int64();
if pos >= 0 {
position = Some(pos as u64);
}

Check warning on line 2853 in src/runtime/node/node_fs.rs

View check run for this annotation

Claude / Claude Code Review

NaN/Infinity position still diverges from Node

Nit: `NaN` and `+Infinity` still diverge from Node here — `is_number()` is true for both, and `to_int64()` maps NaN→0 and +Infinity→`i64::MAX`, so `writevSync(fd, bufs, NaN)` does `pwritev` at offset 0 while Node (via `IsSafeJsInt` → -1) writes at the current offset. Pre-existing behavior, not a regression, but since this branch is being rewritten specifically to match Node/libuv semantics it's worth gating on a finite/safe-integer check and adding `NaN`/`Infinity` to the `describe.each` matrix.
Comment thread
robobun marked this conversation as resolved.
Resolved
}
}
Ok(Self {
Expand Down
130 changes: 126 additions & 4 deletions test/js/node/fs/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1812,6 +1812,123 @@
expect(buffers[2]).toEqual(new Uint8Array([10, 11, 12]));
});

// Node: `typeof position !== 'number'` is coerced to null, and libuv treats any
// offset < 0 as non-positional. Both select the current file offset.
describe.each([-1, -5, "3", {}, true, 3n] as any[])(
"readv/writev with position=%p uses the current file offset",
position => {
it("writevSync", () => {
const p = join(tmpdir(), `writev-pos-sync-${process.pid}.txt`);
writeFileSync(p, "0123456789");
const fd = openSync(p, "r+");
try {
// Advance the current position to 4.
readSync(fd, Buffer.alloc(4), 0, 4, null);
const n = writevSync(fd, [Buffer.from("AB"), Buffer.from("CD")], position);
expect(n).toBe(4);
} finally {
closeSync(fd);
}
expect(readFileSync(p, "utf8")).toBe("0123ABCD89");
});

it("readvSync", () => {
const p = join(tmpdir(), `readv-pos-sync-${process.pid}.txt`);
writeFileSync(p, "0123456789");
const fd = openSync(p, "r");
try {
readSync(fd, Buffer.alloc(4), 0, 4, null);
const b1 = Buffer.alloc(2);
const b2 = Buffer.alloc(2);
const n = readvSync(fd, [b1, b2], position);
expect({ n, b1: b1.toString(), b2: b2.toString() }).toEqual({ n: 4, b1: "45", b2: "67" });
// A second readv should continue from where the first left off.
const b3 = Buffer.alloc(2);
expect(readvSync(fd, [b3], position)).toBe(2);
expect(b3.toString()).toBe("89");
} finally {
closeSync(fd);
}
});

it("fs.writev (callback)", async () => {
const p = join(tmpdir(), `writev-pos-cb-${process.pid}.txt`);
writeFileSync(p, "0123456789");
const fd = openSync(p, "r+");
try {
readSync(fd, Buffer.alloc(4), 0, 4, null);
const { promise, resolve, reject } = Promise.withResolvers<number>();
fs.writev(fd, [Buffer.from("AB"), Buffer.from("CD")], position, (err, n) =>
err ? reject(err) : resolve(n),
);
expect(await promise).toBe(4);
} finally {
closeSync(fd);
}
expect(readFileSync(p, "utf8")).toBe("0123ABCD89");
});

it("fs.readv (callback)", async () => {
const p = join(tmpdir(), `readv-pos-cb-${process.pid}.txt`);
writeFileSync(p, "0123456789");
const fd = openSync(p, "r");
try {
readSync(fd, Buffer.alloc(4), 0, 4, null);
const b1 = Buffer.alloc(2);
const b2 = Buffer.alloc(2);
const { promise, resolve, reject } = Promise.withResolvers<number>();
fs.readv(fd, [b1, b2], position, (err, n) => (err ? reject(err) : resolve(n)));
const n = await promise;
expect({ n, b1: b1.toString(), b2: b2.toString() }).toEqual({ n: 4, b1: "45", b2: "67" });
} finally {
closeSync(fd);
}
});

it("FileHandle.writev", async () => {
const p = join(tmpdir(), `writev-pos-fh-${process.pid}.txt`);
writeFileSync(p, "0123456789");
const fh = await promises.open(p, "r+");
try {
await fh.read(Buffer.alloc(4), 0, 4, null);
const { bytesWritten } = await fh.writev([Buffer.from("AB"), Buffer.from("CD")], position);
expect(bytesWritten).toBe(4);
} finally {
await fh.close();
}
expect(readFileSync(p, "utf8")).toBe("0123ABCD89");
});

it("FileHandle.readv", async () => {
const p = join(tmpdir(), `readv-pos-fh-${process.pid}.txt`);
writeFileSync(p, "0123456789");
const fh = await promises.open(p, "r");
try {
await fh.read(Buffer.alloc(4), 0, 4, null);
const b1 = Buffer.alloc(2);
const b2 = Buffer.alloc(2);
const { bytesRead } = await fh.readv([b1, b2], position);
expect({ bytesRead, b1: b1.toString(), b2: b2.toString() }).toEqual({ bytesRead: 4, b1: "45", b2: "67" });
} finally {
await fh.close();
}
});
},
);

it("writevSync with a non-negative number position is still positional", () => {
const p = join(tmpdir(), `writev-pos-number-${process.pid}.txt`);
writeFileSync(p, "0123456789");
const fd = openSync(p, "r+");
try {
readSync(fd, Buffer.alloc(4), 0, 4, null);
expect(writevSync(fd, [Buffer.from("XY")], 8)).toBe(2);
} finally {
closeSync(fd);
}
expect(readFileSync(p, "utf8")).toBe("01234567XY");
});

describe("writeSync", () => {
it("works with bigint", () => {
const dest = join(tmpdir(), "writeSync-large-file-bigint.txt");
Expand Down Expand Up @@ -5229,15 +5346,20 @@
buf.buffer.transfer();
expect(buf.buffer.detached).toBe(true);

// A rejected call must not leave the buffers held either.
const other = new Uint8Array(new ArrayBuffer(8));
expect(() => fs.writev(fd, [other], "not a position" as any, () => {})).toThrow();
// A non-number position is treated as null (Node.js behaviour); the call
// succeeds and the pin is released after completion.
const other = new Uint8Array(new ArrayBuffer(8)).fill(0x44);
const second = Promise.withResolvers<number>();
fs.writev(fd, [other], "not a position" as any, (err, n) => (err ? second.reject(err) : second.resolve(n)));
other.buffer.transfer();
expect(other.buffer.detached).toBe(false);
expect(await second.promise).toBe(8);
other.buffer.transfer();
expect(other.buffer.detached).toBe(true);
} finally {
closeSync(fd);
}
expect(readFileSync(file, "latin1")).toBe("CCCCCCCC");
expect(readFileSync(file, "latin1")).toBe("DDDDDDDD");

Check warning on line 5362 in test/js/node/fs/fs.test.ts

View check run for this annotation

Claude / Claude Code Review

Buffer-pin test drops disk verification for the first (pwritev) write

The second (non-positional) writev now lands at offset 0 — pwritev doesn't advance the fd offset — so its 8 `D` bytes overwrite the first write's 8 `C` bytes before `readFileSync` runs, and the first (pwritev-path) write's disk content is no longer verified. Add `expect(readFileSync(file, "latin1")).toBe("CCCCCCCC")` right after `expect(await promise).toBe(8)` so both writes' bytes are checked and the original safety-net assertion is preserved.
Comment thread
robobun marked this conversation as resolved.
Resolved
});

it("fs.write keeps the source buffer attached while the write is in flight", async () => {
Expand Down
Loading