Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: use .chunk when calling adapters' writev #42161

Merged
merged 1 commit into from Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/internal/webstreams/adapters.js
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
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();
}
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