stream: resolve stream/iter push() return() with its value

The iterator of a push() readable resolved return(value) with an
undefined value, unlike async generators and the other stream/iter
iterators. Since push() readables are no longer wrapped by from(), this
also applied to from() and pipeTo().

Resolve it with `value`.

Assisted-by: OpenCode
Signed-off-by: James M Snell <jasnell@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/66504
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
James M Snell committed 2026-11-22 08:27:32 -08:00
1 parent 445323e5be
commit 01c554387e
2 files changed
+19 -3

No files matched your search

+2 -2
View File
@@ -745,9 +745,9 @@ function createReadable(queue) {
async next() {
return queue.read();
},
async return() {
async return(value) {
queue.consumerReturn();
return new IterResult(true, undefined);
return new IterResult(true, value);
},
async throw(error) {
queue.consumerThrow(error);
+17 -1
View File
@@ -3,7 +3,7 @@
const common = require('../common');
const assert = require('assert');
const { push, text } = require('stream/iter');
const { from, push, text } = require('stream/iter');
async function testBasicWriteRead() {
const { writer, readable } = push();
@@ -174,6 +174,21 @@ async function testInvalidBackpressure() {
}
}
async function testReturnValue() {
// return(value) resolves with `value`, read directly or through from().
for (const getIterator of [
(readable) => readable[Symbol.asyncIterator](),
(readable) => from(readable)[Symbol.asyncIterator](),
]) {
const { writer, readable } = push();
const iterator = getIterator(readable);
const result = await iterator.return('value');
assert.strictEqual(result.done, true);
assert.strictEqual(result.value, 'value');
assert.strictEqual(writer.canWrite, null);
}
}
Promise.all([
testBasicWriteRead(),
testMultipleWrites(),
@@ -187,4 +202,5 @@ Promise.all([
testConsumerBreakWriteSyncReturnsFalse(),
testPushWithTransforms(),
testInvalidBackpressure(),
testReturnValue(),
]).then(common.mustCall());