mirror of
https://github.com/nodejs/node.git
synced 2026-11-29 14:27:57 -05:00
stream: preserve toReadableSync batch after backpressure
Keep the current batch and index across _read() calls so chunks that remain after push() returns false are emitted on later reads. Fixes: https://github.com/nodejs/node/issues/63275 Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> Assisted-by: openai:gpt-5.5 PR-URL: https://github.com/nodejs/node/pull/63276 Backport-PR-URL: https://github.com/nodejs/node/pull/64675 Fixes: https://github.com/nodejs/node/issues/63275 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
eb72fe94fc
commit
9a45ff5d8f
2 files changed
+34
-4
No files matched your search
@@ -363,23 +363,37 @@ function toReadableSync(source, options = kNullPrototype) {
|
||||
|
||||
const ReadableCtor = lazyReadable();
|
||||
const iterator = source[SymbolIterator]();
|
||||
let hasBatch = false;
|
||||
let batch;
|
||||
let batchIndex = 0;
|
||||
|
||||
return new ReadableCtor({
|
||||
__proto__: null,
|
||||
highWaterMark,
|
||||
read() {
|
||||
for (;;) {
|
||||
const { value: batch, done } = iterator.next();
|
||||
if (hasBatch) {
|
||||
while (batchIndex < batch.length) {
|
||||
if (!this.push(batch[batchIndex++])) return;
|
||||
}
|
||||
batch = undefined;
|
||||
hasBatch = false;
|
||||
batchIndex = 0;
|
||||
}
|
||||
|
||||
const result = iterator.next();
|
||||
const { done } = result;
|
||||
if (done) {
|
||||
this.push(null);
|
||||
return;
|
||||
}
|
||||
for (let i = 0; i < batch.length; i++) {
|
||||
if (!this.push(batch[i])) return;
|
||||
}
|
||||
batch = result.value;
|
||||
hasBatch = true;
|
||||
}
|
||||
},
|
||||
destroy(err, cb) {
|
||||
batch = undefined;
|
||||
hasBatch = false;
|
||||
if (typeof iterator.return === 'function') iterator.return();
|
||||
cb(err);
|
||||
},
|
||||
|
||||
@@ -439,6 +439,21 @@ async function testBackpressureSync() {
|
||||
assert.strictEqual(chunks.length, 9);
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// fromStreamIterSync: backpressure within a batch
|
||||
// =============================================================================
|
||||
|
||||
async function testBackpressureSyncMultiChunkBatch() {
|
||||
function* gen() {
|
||||
yield [Buffer.from('a'), Buffer.from('b'), Buffer.from('c')];
|
||||
}
|
||||
|
||||
const readable = toReadableSync(gen(), { highWaterMark: 1 });
|
||||
const result = await collect(readable);
|
||||
|
||||
assert.strictEqual(result.toString(), 'abc');
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// fromStreamIterSync: source error
|
||||
// =============================================================================
|
||||
@@ -613,6 +628,7 @@ Promise.all([
|
||||
testWithTransformAsync(),
|
||||
testBasicSync(),
|
||||
testBackpressureSync(),
|
||||
testBackpressureSyncMultiChunkBatch(),
|
||||
testErrorSync(),
|
||||
testDestroySync(),
|
||||
testRoundTrip(),
|
||||
|
||||
Reference in new issue
Block a user