Skip to content

Commit

Permalink
stream: use .chunk when calling adapters's writev
Browse files Browse the repository at this point in the history
Fix: nodejs#42157

PR-URL: nodejs#42161
Fixes: nodejs#42157
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
meixg authored and sxa committed Mar 7, 2022
1 parent 9aeda47 commit 06e5c0e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
10 changes: 6 additions & 4 deletions lib/internal/webstreams/adapters.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ function newStreamWritableFromWritableStream(writableStream, options = {}) {

writev(chunks, callback) {
function done(error) {
error = error.filter((e) => e);
try {
callback(error);
callback(error.length === 0 ? undefined : error);
} catch (error) {
// In a next tick because this is happening within
// a promise context, and if there are any errors
Expand All @@ -247,7 +248,7 @@ function newStreamWritableFromWritableStream(writableStream, options = {}) {
PromiseAll(
ArrayPrototypeMap(
chunks,
(chunk) => writer.write(chunk))),
(data) => writer.write(data.chunk))),
done,
done);
},
Expand Down Expand Up @@ -633,8 +634,9 @@ function newStreamDuplexFromReadableWritablePair(pair = {}, options = {}) {

writev(chunks, callback) {
function done(error) {
error = error.filter((e) => e);
try {
callback(error);
callback(error.length === 0 ? undefined : error);
} catch (error) {
// In a next tick because this is happening within
// a promise context, and if there are any errors
Expand All @@ -652,7 +654,7 @@ function newStreamDuplexFromReadableWritablePair(pair = {}, options = {}) {
PromiseAll(
ArrayPrototypeMap(
chunks,
(chunk) => writer.write(chunk))),
(data) => writer.write(data.chunk))),
done,
done);
},
Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-whatwg-webstreams-adapters-to-streamduplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,20 @@ const {
finished(duplex, common.mustCall());
pipeline(readable, duplex, writable, common.mustCall());
}

{
const transform = new TransformStream();
const duplex = newStreamDuplexFromReadableWritablePair(transform);
duplex.setEncoding('utf-8');
duplex.on('data', common.mustCall((data) => {
assert.strictEqual(data, 'hello');
}, 5));

duplex.write(Buffer.from('hello'));
duplex.write(Buffer.from('hello'));
duplex.write(Buffer.from('hello'));
duplex.write(Buffer.from('hello'));
duplex.write(Buffer.from('hello'));

duplex.end();
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,17 @@ class TestSource {

{
const writableStream = new WritableStream({
write: common.mustCall(2),
write: common.mustCall(5),
close: common.mustCall(),
});
const writable = newStreamWritableFromWritableStream(writableStream);

finished(writable, common.mustCall());

writable.write('hello');
writable.write('hello');
writable.write('hello');
writable.write('world');
writable.write('world');
writable.end();
}
Expand Down

0 comments on commit 06e5c0e

Please sign in to comment.