stream: validate writevSync chunks before queuing

Ensure writevSync() validates every chunk before handing the batch to
the push queue. Invalid chunks now throw ERR_INVALID_ARG_TYPE without
leaving data readable from the failed write.

Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com>
Assisted-by: openai:gpt-5.5
PR-URL: https://github.com/nodejs/node/pull/64300
Fixes: https://github.com/nodejs/node/issues/64299
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Trivikram Kamat authored and Antoine du Hamel committed 2026-08-06 15:35:03 +02:00
1 parent b57350fe27
commit a5e7d89f9e
2 files changed
+26 -4

No files matched your search

+3 -4
View File
@@ -190,15 +190,14 @@ function toUint8Array(chunk) {
}
/**
* Check if all chunks in an array are already Uint8Array (no strings).
* Short-circuits on the first string found.
* Check if all chunks in an array are already Uint8Array.
* Short-circuits on the first non-Uint8Array chunk found.
* @param {Array<Uint8Array|string>} chunks
* @returns {boolean}
*/
function allUint8Array(chunks) {
// Ok, well, kind of. This is more a check for "no strings"...
for (let i = 0; i < chunks.length; i++) {
if (typeof chunks[i] === 'string') return false;
if (!isUint8Array(chunks[i])) return false;
}
return true;
}
@@ -171,6 +171,28 @@ async function testWritevSync() {
assert.strictEqual(result, 'hello');
}
async function testWritevSyncInvalidChunkDoesNotQueue() {
const { writer, readable } = push({ highWaterMark: 10 });
assert.throws(
() => writer.writevSync([1]),
{ code: 'ERR_INVALID_ARG_TYPE' },
);
const iter = readable[Symbol.asyncIterator]();
const next = iter.next();
const result = await Promise.race([
next.then(() => 'resolved'),
new Promise((resolve) => setImmediate(resolve, 'pending')),
]);
assert.strictEqual(result, 'pending');
writer.endSync();
const end = await next;
assert.strictEqual(end.value, undefined);
assert.strictEqual(end.done, true);
}
async function testWritevMixedTypes() {
const { writer, readable } = push({ highWaterMark: 10 });
// Mix strings and Uint8Arrays
@@ -494,6 +516,7 @@ Promise.all([
testOndrainRejectsOnConsumerThrow(),
testWritev(),
testWritevSync(),
testWritevSyncInvalidChunkDoesNotQueue(),
testWritevMixedTypes(),
testWriteAfterEnd(),
testWriteAfterFail(),