From 01c554387e01695faea9d047b17ff45fe0930aa0 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sun, 4 Oct 2026 07:01:01 +0000 Subject: [PATCH] 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 PR-URL: https://github.com/nodejs/node/pull/66504 Reviewed-By: Matteo Collina Reviewed-By: Trivikram Kamat --- lib/internal/streams/iter/push.js | 4 ++-- test/parallel/test-stream-iter-push-basic.js | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/internal/streams/iter/push.js b/lib/internal/streams/iter/push.js index 6039ab08803..a5174ad7e55 100644 --- a/lib/internal/streams/iter/push.js +++ b/lib/internal/streams/iter/push.js @@ -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); diff --git a/test/parallel/test-stream-iter-push-basic.js b/test/parallel/test-stream-iter-push-basic.js index 9b1c044cac4..ad7eab5f53a 100644 --- a/test/parallel/test-stream-iter-push-basic.js +++ b/test/parallel/test-stream-iter-push-basic.js @@ -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());