Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
stream: fix TypeError in writev when writer.ready rejects
The `done` callback in the `writev` handlers of both
`newStreamWritableFromWritableStream` and
`newStreamDuplexFromReadableWritablePair` unconditionally called
`error.filter()`, assuming `error` is always an array from
`SafePromiseAll`. However, `done` is also used as the rejection
handler for `writer.ready`, which passes a single error value,
not an array. This caused a `TypeError: error.filter is not a
function` which became an unhandled rejection, crashing the
process.

Fix by checking whether `error` is an array before filtering,
and also use `ArrayIsArray`/`ArrayPrototypeFilter` from primordials
to follow Node.js internal conventions.

Fixes: #62199
  • Loading branch information
easonysliu committed Mar 17, 2026
commit 07360bc89eaba94904f9ca179ac4ce917292cc00
21 changes: 17 additions & 4 deletions lib/internal/webstreams/adapters.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const {
ArrayIsArray,
ArrayPrototypeFilter,
ArrayPrototypeMap,
Boolean,
Expand Down Expand Up @@ -313,9 +314,15 @@ function newStreamWritableFromWritableStream(writableStream, options = kEmptyObj

writev(chunks, callback) {
function done(error) {
error = error.filter((e) => e);
// When error comes from SafePromiseAll, it is an array of
// errors (one per chunk). When it comes from writer.ready
// rejection, it is a single error. Normalize to handle both.
if (ArrayIsArray(error)) {
error = ArrayPrototypeFilter(error, Boolean);
error = error.length === 0 ? undefined : error;
}
try {
callback(error.length === 0 ? undefined : error);
callback(error);
} catch (error) {
// In a next tick because this is happening within
// a promise context, and if there are any errors
Expand Down Expand Up @@ -775,9 +782,15 @@ function newStreamDuplexFromReadableWritablePair(pair = kEmptyObject, options =

writev(chunks, callback) {
function done(error) {
error = error.filter((e) => e);
// When error comes from SafePromiseAll, it is an array of
// errors (one per chunk). When it comes from writer.ready
// rejection, it is a single error. Normalize to handle both.
if (ArrayIsArray(error)) {
error = ArrayPrototypeFilter(error, Boolean);
error = error.length === 0 ? undefined : error;
}
try {
callback(error.length === 0 ? undefined : error);
callback(error);
} catch (error) {
// In a next tick because this is happening within
// a promise context, and if there are any errors
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Flags: --no-warnings --expose-internals
'use strict';

// Regression test for https://github.com/nodejs/node/issues/62199
// Verifies that the writev done callback in webstream adapters does not
// throw a TypeError when the error comes from writer.ready rejection
// (a single error) rather than SafePromiseAll (an array of errors).

const common = require('../common');

const {
TransformStream,
WritableStream,
} = require('stream/web');

const {
newStreamWritableFromWritableStream,
newStreamDuplexFromReadableWritablePair,
} = require('internal/webstreams/adapters');

const { Duplex } = require('stream');

// Test 1: Duplex.fromWeb writev path should not cause unhandled rejection
{
const output = Duplex.fromWeb(new TransformStream());
output.on('error', common.mustCall());
output.cork();
output.write('test');
output.write('test');
output.uncork();
output.destroy();
}

// Test 2: newStreamDuplexFromReadableWritablePair writev path
{
const transform = new TransformStream();
const duplex = newStreamDuplexFromReadableWritablePair(transform);
duplex.on('error', common.mustCall());
duplex.cork();
duplex.write('test');
duplex.write('test');
duplex.uncork();
duplex.destroy();
}

// Test 3: newStreamWritableFromWritableStream writev path
{
const writableStream = new WritableStream();
const writable = newStreamWritableFromWritableStream(writableStream);
writable.on('error', common.mustCall());
writable.cork();
writable.write('test');
writable.write('test');
writable.uncork();
writable.destroy();
}