stream: update broadcast to retain buffered data with zero consumers

While the Broadcast is still open, maintain buffered data when
consumer count reaches zero

Signed-off-by: James M Snell <jasnell@gmail.com>
Assisted-by: Opencode
PR-URL: https://github.com/nodejs/node/pull/66028
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
This commit is contained in:
James M Snell authored and Antoine du Hamel committed 2026-09-18 14:52:02 +02:00
1 parent 02c2302c31
commit 3134ca6f8e
2 files changed
+15

No files matched your search

+2
View File
@@ -444,6 +444,8 @@ class BroadcastImpl {
}
#tryTrimBuffer() {
// Retain buffered data for consumers that attach while none are active.
if (this.#consumers.size === 0) return;
if (this.#cachedMinCursorConsumers === 0) {
this.#recomputeMinCursor();
}
@@ -350,6 +350,18 @@ async function testLateJoinerSeesBufferedData() {
assert.strictEqual(result, 'before-join');
}
async function testLateJoinerAfterDetachSeesBufferedData() {
const { writer, broadcast: bc } = broadcast({ budget: 16384 });
const first = bc.push()[Symbol.asyncIterator]();
writer.writeSync('before-detach');
await first.return();
const second = bc.push();
writer.endSync();
assert.strictEqual(await text(second), 'before-detach');
}
async function testOverlappingNextKeepsEarlierRead() {
const { writer, broadcast: bc } = broadcast();
const it = bc.push()[Symbol.asyncIterator]();
@@ -403,5 +415,6 @@ Promise.all([
testFailDetachesConsumers(),
testWriterFailIdempotent(),
testLateJoinerSeesBufferedData(),
testLateJoinerAfterDetachSeesBufferedData(),
testOverlappingNextKeepsEarlierRead(),
]).then(common.mustCall());