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
10 changes: 5 additions & 5 deletions lib/internal/webstreams/readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
Loading