diff --git a/doc/api/stream_iter.md b/doc/api/stream_iter.md index 94b50135ee2..62cbf79650e 100644 --- a/doc/api/stream_iter.md +++ b/doc/api/stream_iter.md @@ -1316,7 +1316,8 @@ added: v24.20.0 * `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. @@ -1801,7 +1802,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(); @@ -1837,7 +1838,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 65988a8d19b..c3f2e868e6f 100644 --- a/lib/internal/streams/iter/broadcast.js +++ b/lib/internal/streams/iter/broadcast.js @@ -934,7 +934,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(),