Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 56 additions & 3 deletions lib/internal/streams/iter/broadcast.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
const {
ArrayIsArray,
ArrayPrototypePush,
ArrayPrototypeShift,
MathMax,
PromisePrototypeThen,
PromiseReject,
Expand Down Expand Up @@ -146,6 +147,7 @@ class BroadcastImpl {
cursor: this.#bufferStart,
resolve: null,
reject: null,
pending: [],
detached: false,
};

Expand All @@ -165,8 +167,10 @@ class BroadcastImpl {

function detach() {
state.detached = true;
state.resolve = null;
state.reject = null;
if (state.resolve) {
state.resolve({ __proto__: null, done: true, value: undefined });
}
self.#resolvePendingDone(state);
if (self.#deleteConsumer(state)) {
self.#tryTrimBuffer();
}
Expand Down Expand Up @@ -207,6 +211,13 @@ class BroadcastImpl {
return kDone;
}

if (state.resolve) {
const { promise, resolve, reject } = PromiseWithResolvers();
ArrayPrototypePush(state.pending,
{ __proto__: null, resolve, reject });
return promise;
}

const { promise, resolve, reject } = PromiseWithResolvers();
state.resolve = resolve;
state.reject = reject;
Expand Down Expand Up @@ -250,6 +261,11 @@ class BroadcastImpl {
consumer.resolve = null;
consumer.reject = null;
}
if (reason !== undefined) {
this.#rejectPending(consumer, reason);
} else {
this.#resolvePendingDone(consumer);
}
consumer.detached = true;
}
this.#consumers.clear();
Expand Down Expand Up @@ -296,7 +312,7 @@ class BroadcastImpl {
this.#ended = true;

for (const consumer of this.#consumers) {
if (consumer.resolve) {
while (consumer.resolve) {
const bufferIndex = consumer.cursor - this.#bufferStart;
if (bufferIndex < this.#buffer.length) {
const chunk = this.#buffer.get(bufferIndex);
Expand All @@ -309,9 +325,15 @@ class BroadcastImpl {
consumer.resolve({ __proto__: null, done: false, value: chunk });
} else {
consumer.resolve({ __proto__: null, done: true, value: undefined });
this.#resolvePendingDone(consumer);
consumer.detached = true;
}
consumer.resolve = null;
consumer.reject = null;
if (consumer.detached && this.#deleteConsumer(consumer)) {
this.#tryTrimBuffer();
break;
}
}
}
}
Expand All @@ -328,6 +350,7 @@ class BroadcastImpl {
consumer.resolve = null;
consumer.reject = null;
}
this.#rejectPending(consumer, reason);
consumer.detached = true;
}
this.#consumers.clear();
Expand Down Expand Up @@ -396,6 +419,11 @@ class BroadcastImpl {
consumer.resolve = null;
consumer.reject = null;
resolve({ __proto__: null, done: false, value: chunk });
if (consumer.detached && this.#deleteConsumer(consumer)) {
this.#tryTrimBuffer();
} else if (this.#promotePending(consumer)) {
ArrayPrototypePush(this.#waiters, consumer);
}
} else {
// Still waiting -- put back
ArrayPrototypePush(this.#waiters, consumer);
Expand All @@ -418,6 +446,31 @@ class BroadcastImpl {
}
return false;
}

#promotePending(consumer) {
const next = ArrayPrototypeShift(consumer.pending);
if (next === undefined) return false;
consumer.resolve = next.resolve;
consumer.reject = next.reject;
return true;
}

#resolvePendingDone(consumer) {
if (consumer.resolve) {
consumer.resolve = null;
consumer.reject = null;
}
while (consumer.pending.length > 0) {
ArrayPrototypeShift(consumer.pending).resolve(
{ __proto__: null, done: true, value: undefined });
}
}

#rejectPending(consumer, reason) {
while (consumer.pending.length > 0) {
ArrayPrototypeShift(consumer.pending).reject(reason);
}
}
}

// =============================================================================
Expand Down
34 changes: 34 additions & 0 deletions test/parallel/test-stream-iter-broadcast-basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

const common = require('../common');
const assert = require('assert');
const { setTimeout } = require('timers/promises');
const { broadcast, text } = require('stream/iter');

// =============================================================================
Expand Down Expand Up @@ -243,6 +244,38 @@ async function testLateJoinerSeesBufferedData() {
assert.strictEqual(result, 'before-join');
}

async function testOverlappingNextKeepsEarlierRead() {
const { writer, broadcast: bc } = broadcast();
const it = bc.push()[Symbol.asyncIterator]();

const first = it.next();
const second = it.next();

await writer.write('x');

const secondResult = await Promise.race([
second.then((value) => ({ __proto__: null, settled: true, value })),
setTimeout(common.platformTimeout(50),
{ __proto__: null, settled: false }),
]);
assert.deepStrictEqual(secondResult, {
__proto__: null,
settled: false,
});

const result = await first;
assert.strictEqual(result.done, false);
assert.strictEqual(Buffer.concat(result.value).toString(), 'x');

writer.endSync();
assert.deepStrictEqual(await second, {
__proto__: null,
done: true,
value: undefined,
});
assert.strictEqual(bc.consumerCount, 0);
}

Promise.all([
testBasicBroadcast(),
testMultipleWrites(),
Expand All @@ -257,4 +290,5 @@ Promise.all([
testFailDetachesConsumers(),
testWriterFailIdempotent(),
testLateJoinerSeesBufferedData(),
testOverlappingNextKeepsEarlierRead(),
]).then(common.mustCall());
Loading