Skip to content

Commit 06e5c0e

Browse files
meixgsxa
authored andcommitted
stream: use .chunk when calling adapters's writev
Fix: #42157 PR-URL: #42161 Fixes: #42157 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 9aeda47 commit 06e5c0e

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

lib/internal/webstreams/adapters.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,9 @@ function newStreamWritableFromWritableStream(writableStream, options = {}) {
228228

229229
writev(chunks, callback) {
230230
function done(error) {
231+
error = error.filter((e) => e);
231232
try {
232-
callback(error);
233+
callback(error.length === 0 ? undefined : error);
233234
} catch (error) {
234235
// In a next tick because this is happening within
235236
// a promise context, and if there are any errors
@@ -247,7 +248,7 @@ function newStreamWritableFromWritableStream(writableStream, options = {}) {
247248
PromiseAll(
248249
ArrayPrototypeMap(
249250
chunks,
250-
(chunk) => writer.write(chunk))),
251+
(data) => writer.write(data.chunk))),
251252
done,
252253
done);
253254
},
@@ -633,8 +634,9 @@ function newStreamDuplexFromReadableWritablePair(pair = {}, options = {}) {
633634

634635
writev(chunks, callback) {
635636
function done(error) {
637+
error = error.filter((e) => e);
636638
try {
637-
callback(error);
639+
callback(error.length === 0 ? undefined : error);
638640
} catch (error) {
639641
// In a next tick because this is happening within
640642
// a promise context, and if there are any errors
@@ -652,7 +654,7 @@ function newStreamDuplexFromReadableWritablePair(pair = {}, options = {}) {
652654
PromiseAll(
653655
ArrayPrototypeMap(
654656
chunks,
655-
(chunk) => writer.write(chunk))),
657+
(data) => writer.write(data.chunk))),
656658
done,
657659
done);
658660
},

test/parallel/test-whatwg-webstreams-adapters-to-streamduplex.js

+17
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,20 @@ const {
147147
finished(duplex, common.mustCall());
148148
pipeline(readable, duplex, writable, common.mustCall());
149149
}
150+
151+
{
152+
const transform = new TransformStream();
153+
const duplex = newStreamDuplexFromReadableWritablePair(transform);
154+
duplex.setEncoding('utf-8');
155+
duplex.on('data', common.mustCall((data) => {
156+
assert.strictEqual(data, 'hello');
157+
}, 5));
158+
159+
duplex.write(Buffer.from('hello'));
160+
duplex.write(Buffer.from('hello'));
161+
duplex.write(Buffer.from('hello'));
162+
duplex.write(Buffer.from('hello'));
163+
duplex.write(Buffer.from('hello'));
164+
165+
duplex.end();
166+
}

test/parallel/test-whatwg-webstreams-adapters-to-streamwritable.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -200,14 +200,17 @@ class TestSource {
200200

201201
{
202202
const writableStream = new WritableStream({
203-
write: common.mustCall(2),
203+
write: common.mustCall(5),
204204
close: common.mustCall(),
205205
});
206206
const writable = newStreamWritableFromWritableStream(writableStream);
207207

208208
finished(writable, common.mustCall());
209209

210210
writable.write('hello');
211+
writable.write('hello');
212+
writable.write('hello');
213+
writable.write('world');
211214
writable.write('world');
212215
writable.end();
213216
}

0 commit comments

Comments
 (0)