From d72fb59ddc0db2ed007fcbf823edd3b960a8e2e6 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Sun, 30 Aug 2026 17:16:23 +0000 Subject: [PATCH] stream: make Broadcast.from() only return the Broadcast Signed-off-by: James M Snell Assisted-by: Opencode PR-URL: https://github.com/nodejs/node/pull/66030 Reviewed-By: Filip Skokan Reviewed-By: Trivikram Kamat --- doc/api/stream_iter.md | 7 ++++--- lib/internal/streams/iter/broadcast.js | 2 +- test/parallel/test-stream-iter-broadcast-from.js | 10 ++++++++++ test/parallel/test-stream-iter-property-access.js | 3 +-- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/doc/api/stream_iter.md b/doc/api/stream_iter.md index 80579fba982..60d2aa39737 100644 --- a/doc/api/stream_iter.md +++ b/doc/api/stream_iter.md @@ -1374,7 +1374,8 @@ added: * `input` {AsyncIterable|Iterable|BroadcastChannel} * `options` {Object} Same as `broadcast()`. -* Returns: {Object} `{ writer, broadcast }` +* Returns: {BroadcastChannel|Object} A `broadcastProtocol` input returns its + {BroadcastChannel} directly. Other inputs return `{ writer, broadcast }`. Create a {BroadcastChannel} from an existing source. The source is consumed automatically and pushed to all subscribers. @@ -1883,7 +1884,7 @@ class MessageBus { } const bus = new MessageBus(); -const { broadcast } = Broadcast.from(bus); +const broadcast = Broadcast.from(bus); const consumer = broadcast.push(); bus.send('hello'); bus.close(); @@ -1919,7 +1920,7 @@ class MessageBus { } const bus = new MessageBus(); -const { broadcast } = Broadcast.from(bus); +const broadcast = Broadcast.from(bus); const consumer = broadcast.push(); bus.send('hello'); bus.close(); diff --git a/lib/internal/streams/iter/broadcast.js b/lib/internal/streams/iter/broadcast.js index 4c191295a37..3b37a5b5f2a 100644 --- a/lib/internal/streams/iter/broadcast.js +++ b/lib/internal/streams/iter/broadcast.js @@ -923,7 +923,7 @@ const Broadcast = { throw new ERR_INVALID_RETURN_VALUE( 'an object', '[Symbol.for(\'Stream.broadcastProtocol\')]', bc); } - return { __proto__: null, writer: { __proto__: null }, broadcast: bc }; + return bc; } const source = from(input); diff --git a/test/parallel/test-stream-iter-broadcast-from.js b/test/parallel/test-stream-iter-broadcast-from.js index 6062f5244ec..cd76f3647b2 100644 --- a/test/parallel/test-stream-iter-broadcast-from.js +++ b/test/parallel/test-stream-iter-broadcast-from.js @@ -181,6 +181,15 @@ async function testBroadcastFromSourceError() { // Protocol validation // ============================================================================= +function testBroadcastProtocolReturnsBroadcast() { + const { broadcast: expected } = broadcast(); + const obj = { + [Symbol.for('Stream.broadcastProtocol')]() { return expected; }, + }; + assert.strictEqual(Broadcast.from(obj), expected); + expected.cancel(); +} + function testBroadcastProtocolReturnsNull() { const obj = { [Symbol.for('Stream.broadcastProtocol')]() { return null; }, @@ -223,6 +232,7 @@ Promise.all([ testAlreadyAbortedSignal(), testBroadcastFromCancelWhileBlocked(), testBroadcastFromSourceError(), + testBroadcastProtocolReturnsBroadcast(), testBroadcastProtocolReturnsNull(), testBroadcastProtocolReturnsString(), testBroadcastProtocolReturnsUndefined(), diff --git a/test/parallel/test-stream-iter-property-access.js b/test/parallel/test-stream-iter-property-access.js index 5ba1a5a587f..7c4b8ef3a7e 100644 --- a/test/parallel/test-stream-iter-property-access.js +++ b/test/parallel/test-stream-iter-property-access.js @@ -140,8 +140,7 @@ async function testMultiConsumerProtocolsSnapshotMethods() { const broadcastTarget = broadcast().broadcast; const broadcastFixture = protocolFixture( broadcastProtocol, broadcastTarget); - assert.strictEqual( - Broadcast.from(broadcastFixture.input).broadcast, broadcastTarget); + assert.strictEqual(Broadcast.from(broadcastFixture.input), broadcastTarget); assert.strictEqual(broadcastFixture.accesses, 1); const shareTarget = share('abc');