diff --git a/lib/internal/webstreams/readablestream.js b/lib/internal/webstreams/readablestream.js index 3181355959b6..ffc48c74a6dd 100644 --- a/lib/internal/webstreams/readablestream.js +++ b/lib/internal/webstreams/readablestream.js @@ -3860,15 +3860,15 @@ function readableByteStreamControllerFillReadRequestFromQueue(controller, readRe } function readableByteStreamControllerProcessReadRequestsUsingQueue(controller) { - const { - stream, - queueTotalSize, - } = controller[kState]; + const state = controller[kState]; + const { stream } = state; const { reader } = stream[kState]; assert(isReadableStreamDefaultReader(reader)); while (reader[kState].readRequests.length > 0) { - if (queueTotalSize === 0) { + // Filling a read request dequeues a chunk, so re-read the queue size + // on every iteration. + if (state.queueTotalSize === 0) { return; } readableByteStreamControllerFillReadRequestFromQueue( diff --git a/test/parallel/test-webstreams-byte-enqueue-pending-read-requests.js b/test/parallel/test-webstreams-byte-enqueue-pending-read-requests.js new file mode 100644 index 000000000000..d067d23fac1b --- /dev/null +++ b/test/parallel/test-webstreams-byte-enqueue-pending-read-requests.js @@ -0,0 +1,57 @@ +'use strict'; + +// A released BYOB reader's partially filled pull-into is moved to the +// queue on the next enqueue(). If a default reader is waiting with more +// than one read request, the queue must only fill as many read requests +// as it has chunks for, and the newly enqueued chunk must go to the next +// read request. + +const common = require('../common'); +const assert = require('assert'); +const { ReadableStream } = require('stream/web'); + +async function test(numReads) { + let controller; + const rs = new ReadableStream({ + type: 'bytes', + start(c) { + controller = c; + }, + }); + + // Leave a pull-into with 1 of the required 2 bytes filled. + const byobReader = rs.getReader({ mode: 'byob' }); + const byobRead = byobReader.read(new Uint8Array(2), { min: 2 }); + await new Promise(setImmediate); + controller.enqueue(new Uint8Array([1])); + byobReader.releaseLock(); + await assert.rejects(byobRead, { name: 'TypeError' }); + + const reader = rs.getReader(); + const reads = []; + for (let i = 0; i < numReads; i++) { + reads.push(reader.read()); + } + + controller.enqueue(new Uint8Array([2])); + controller.enqueue(new Uint8Array([3])); + controller.close(); + + const results = await Promise.all(reads); + const expected = [[1], [2], [3]].slice(0, numReads); + assert.deepStrictEqual(results.map(({ value }) => [...value]), expected); + + // Chunks that were not read yet are still in the queue. + const rest = []; + for (let result = await reader.read(); !result.done; + result = await reader.read()) { + rest.push(...result.value); + } + assert.deepStrictEqual(rest, [1, 2, 3].slice(numReads)); +} + +(async () => { + for (const numReads of [1, 2, 3]) { + await test(numReads); + } +})().then(common.mustCall());