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 -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;