stream: fixup stream/iter to drop at most one entry per share call

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-11-13 17:02:15 +01:00
1 parent 7e1ae1757b
commit dd23e9e514
3 files changed
+66 -14

No files matched your search

+11 -3
View File
@@ -1441,13 +1441,21 @@ added: v24.20.0
* `source` {Iterable} The sync source to share.
* `options` {Object}
* `highWaterMark` {number} Must be >= 1; values below 1 are clamped
to 1. **Default:** `16`.
* `backpressure` {string} **Default:** `'strict'`.
* `budget` {number} Must be >= 16384.
**Default:** `65536`.
* `backpressure` {string} `'strict'`, `'drop-oldest'`, or `'drop-newest'`.
**Default:** `'strict'`.
* Returns: {SyncShare}
Synchronous version of [`share()`][].
Because there is no way to wait in a synchronous context, `'unbounded'` is not
supported and throws `ERR_INVALID_ARG_VALUE`. With `'drop-newest'`, a consumer
that reaches the end of the buffer while the budget is exhausted discards a
single entry from the source and then returns `{ done: true }` without a
value; the consumer is not detached, so it can resume once the slowest
consumer advances and releases budget.
### Class: `SyncShare`
#### Static method: `SyncShare.fromSync(input[, options])`
+11 -7
View File
@@ -576,6 +576,7 @@ class SyncShareImpl {
}
// Check buffer limit
let dropped = false;
if (self.#bufferedBytes >= self.#options.budget) {
switch (self.#options.backpressure) {
case 'strict':
@@ -598,17 +599,20 @@ class SyncShareImpl {
self.#recomputeMinCursor();
break;
case 'drop-newest':
while (self.#bufferedBytes >= self.#options.budget &&
!self.#sourceExhausted &&
!self.#cancelled &&
self.#sourceError === kNoShareError) {
self.#pullFromSource(true);
}
// Discarding does not reclaim budget, and the slowest
// consumer cannot advance while this synchronous next() is
// running, so at most one entry may be dropped per call.
// Looping here would spin forever on an unbounded source
// and drain a finite one in a single call.
self.#pullFromSource(true);
dropped = true;
break;
}
}
self.#pullFromSource();
if (!dropped) {
self.#pullFromSource();
}
if (self.#sourceError !== undefined) {
state.detached = true;
+44 -4
View File
@@ -149,7 +149,7 @@ function testShareSyncRejectsUnbounded() {
function testShareSyncDropNewest() {
let pulls = 0;
function* source() {
for (let i = 0; i < 3; i++) {
for (let i = 0; i < 4; i++) {
pulls++;
const chunk = new Uint8Array(16384);
chunk[0] = i;
@@ -165,11 +165,50 @@ function testShareSyncDropNewest() {
const slow = shared.pull()[Symbol.iterator]();
assert.strictEqual(fast.next().value[0][0], 0);
assert.strictEqual(fast.next().done, true);
assert.strictEqual(pulls, 3);
// The budget is exhausted and the slow consumer cannot advance while this
// call is running, so exactly one entry is dropped and no value is
// available. The consumer is not detached.
assert.strictEqual(fast.next().done, true);
assert.strictEqual(pulls, 2);
// The slow consumer still sees the buffered entry, which releases budget.
assert.strictEqual(slow.next().value[0][0], 0);
assert.strictEqual(slow.next().done, true);
// Entry 1 was dropped for every consumer, so both resume at entry 2.
assert.strictEqual(slow.next().value[0][0], 2);
assert.strictEqual(pulls, 3);
assert.strictEqual(fast.next().value[0][0], 2);
}
// Regression test: a full buffer must not spin pulling-and-discarding from an
// unbounded source, since discarding never reclaims budget.
function testShareSyncDropNewestUnboundedSource() {
let pulls = 0;
function* source() {
for (;;) {
pulls++;
yield [new Uint8Array(16384)];
}
}
const shared = shareSync(source(), {
budget: 16384,
backpressure: 'drop-newest',
});
const fast = shared.pull()[Symbol.iterator]();
shared.pull();
assert.strictEqual(fast.next().done, false);
assert.strictEqual(pulls, 1);
// Each blocked call drops at most one entry and returns without a value.
for (let i = 0; i < 3; i++) {
assert.strictEqual(fast.next().done, true);
assert.strictEqual(pulls, 2 + i);
}
shared.cancel();
}
// shareSync() accepts string source directly (normalized via fromSync())
@@ -209,6 +248,7 @@ Promise.all([
testShareSyncSourceError(),
testShareSyncRejectsUnbounded(),
testShareSyncDropNewest(),
testShareSyncDropNewestUnboundedSource(),
testShareSyncStringSource(),
testShareSyncRetainsBufferWhenAllConsumersDetach(),
]).then(common.mustCall());