Skip to content

Commit 10272a9

Browse files
watildeaduh95
authored andcommitted
stream: check done before backpressure in stream reader
Signed-off-by: Daijiro Wachi <daijiro.wachi@gmail.com> PR-URL: #63699 Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 3d0097d commit 10272a9

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

lib/internal/streams/compose.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,12 @@ module.exports = function compose(...streams) {
204204
try {
205205
const { value, done } = await reader.read();
206206

207-
if (!d.push(value)) {
207+
if (done) {
208+
d.push(null);
208209
return;
209210
}
210211

211-
if (done) {
212-
d.push(null);
212+
if (!d.push(value)) {
213213
return;
214214
}
215215
} catch {

test/parallel/test-stream-compose.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,3 +537,24 @@ const assert = require('assert');
537537
assert.strictEqual(duplex.destroyed, true);
538538

539539
}
540+
541+
// Regression test: compose with a web TransformStream tail must always emit
542+
// null (EOF) when the source finishes. The done check must precede the
543+
// backpressure check in the reader.read() loop; otherwise push(null) can be
544+
// skipped if canPushMore() returns false on the final done:true read.
545+
{
546+
const { TransformStream } = globalThis;
547+
const { Readable } = require('stream');
548+
549+
// A web TransformStream as the tail exercises the isWebStream code path
550+
// in compose that loops over reader.read() results.
551+
const ts = new TransformStream();
552+
const src = Readable.from(['hello', ' ', 'world']);
553+
const composed = compose(src, ts);
554+
555+
let result = '';
556+
composed.on('data', (chunk) => { result += Buffer.from(chunk).toString(); });
557+
composed.on('end', common.mustCall(() => {
558+
assert.strictEqual(result, 'hello world');
559+
}));
560+
}

0 commit comments

Comments
 (0)