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
7 changes: 6 additions & 1 deletion lib/internal/streams/iter/broadcast.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,12 @@ class BroadcastImpl {
[kWrite](chunk) {
if (this.#ended || this.#cancelled) return false;

const batchSize = this.#batchByteSize(chunk);

// Skip empty chunks -- zero-byte writes would accumulate infinitely
// without ever triggering backpressure under a byte-budget model.
if (batchSize === 0) return true;

if (this.#bufferedBytes >= this.#options.budget) {
switch (this.#options.backpressure) {
case 'strict':
Expand All @@ -299,7 +305,6 @@ class BroadcastImpl {
}
}

const batchSize = this.#batchByteSize(chunk);
this.#buffer.push(chunk);
this.#bufferedBytes += batchSize;
this.#notifyConsumers();
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-stream-iter-broadcast-backpressure.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,26 @@ async function testWritevAsync() {
assert.strictEqual(data, 'hello world');
}

// Zero-byte writes do not consume buffer entries.
async function testZeroByteWrites() {
const { writer, broadcast: bc } = broadcast({ budget: 16384 });
const consumer = bc.push();

for (let i = 0; i < 1000; i++) {
assert.strictEqual(writer.writeSync(''), true);
assert.strictEqual(writer.writevSync([]), true);
}
await writer.write('');
await writer.writev([]);
assert.strictEqual(writer.canWrite, true);
writer.endSync();

let entries = 0;
const iterator = consumer[Symbol.asyncIterator]();
while (!(await iterator.next()).done) entries++;
assert.strictEqual(entries, 0);
}

// endSync returns the total byte count
async function testEndSyncReturnValue() {
const { writer, broadcast: bc } = broadcast({ budget: 16384 });
Expand All @@ -165,5 +185,6 @@ Promise.all([
testBlockBackpressureContent(),
testStrictBackpressureOverflow(),
testWritevAsync(),
testZeroByteWrites(),
testEndSyncReturnValue(),
]).then(common.mustCall());
Loading