Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: handle a pending BYOB pull request from a released reader #44702

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/api/webstreams.md
Expand Up @@ -651,6 +651,10 @@ Signals an error that causes the {ReadableStream} to error and close.

<!-- YAML
added: v16.5.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/44702
description: Support handling a BYOB pull request from a released reader.
-->

Every {ReadableStream} has a controller that is responsible for
Expand Down
82 changes: 82 additions & 0 deletions lib/internal/webstreams/readablestream.js
Expand Up @@ -139,6 +139,7 @@ const kClose = Symbol('kClose');
const kChunk = Symbol('kChunk');
const kError = Symbol('kError');
const kPull = Symbol('kPull');
const kRelease = Symbol('kRelease');

/**
* @typedef {import('../abort_controller').AbortSignal} AbortSignal
Expand Down Expand Up @@ -1019,6 +1020,8 @@ class ReadableStreamDefaultController {
readableStreamDefaultControllerPullSteps(this, readRequest);
}

[kRelease]() {}

[kInspect](depth, options) {
return customInspect(depth, options, this[kType], { });
}
Expand Down Expand Up @@ -1143,6 +1146,17 @@ class ReadableByteStreamController {
readableByteStreamControllerPullSteps(this, readRequest);
}

[kRelease]() {
const {
pendingPullIntos,
} = this[kState];
if (pendingPullIntos.length > 0) {
const firstPendingPullInto = pendingPullIntos[0];
firstPendingPullInto.type = 'none';
this[kState].pendingPullIntos = [firstPendingPullInto];
}
}

[kInspect](depth, options) {
return customInspect(depth, options, this[kType], { });
}
Expand Down Expand Up @@ -2060,6 +2074,9 @@ function readableStreamReaderGenericRelease(reader) {
};
}
setPromiseHandled(reader[kState].close.promise);

stream[kState].controller[kRelease]();

stream[kState].reader = undefined;
reader[kState].stream = undefined;
}
Expand Down Expand Up @@ -2365,6 +2382,8 @@ function readableByteStreamControllerClose(controller) {

function readableByteStreamControllerCommitPullIntoDescriptor(stream, desc) {
assert(stream[kState].state !== 'errored');
assert(desc.type !== 'none');

let done = false;
if (stream[kState].state === 'closed') {
desc.bytesFilled = 0;
Expand Down Expand Up @@ -2574,6 +2593,9 @@ function readableByteStreamControllerRespond(controller, bytesWritten) {

function readableByteStreamControllerRespondInClosedState(controller, desc) {
assert(!desc.bytesFilled);
if (desc.type === 'none') {
readableByteStreamControllerShiftPendingPullInto(controller);
}
const {
stream,
} = controller[kState];
Expand Down Expand Up @@ -2663,6 +2685,31 @@ function readableByteStreamControllerEnqueue(controller, chunk) {
readableByteStreamControllerCallPullIfNeeded(controller);
}

function readableByteStreamControllerEnqueueClonedChunkToQueue(
controller,
buffer,
byteOffset,
byteLength
) {
let cloneResult;
try {
cloneResult = ArrayBufferPrototypeSlice(
buffer,
byteOffset,
byteOffset + byteLength
);
} catch (error) {
readableByteStreamControllerError(controller, error);
throw error;
}
readableByteStreamControllerEnqueueChunkToQueue(
controller,
cloneResult,
0,
byteLength
);
}

function readableByteStreamControllerEnqueueChunkToQueue(
controller,
buffer,
Expand All @@ -2678,6 +2725,29 @@ function readableByteStreamControllerEnqueueChunkToQueue(
controller[kState].queueTotalSize += byteLength;
}

function readableByteStreamControllerEnqueueDetachedPullIntoToQueue(
controller,
desc
) {
const {
buffer,
byteOffset,
bytesFilled,
type,
} = desc;
assert(type === 'none');

if (bytesFilled > 0) {
readableByteStreamControllerEnqueueClonedChunkToQueue(
controller,
buffer,
byteOffset,
bytesFilled
);
}
readableByteStreamControllerShiftPendingPullInto(controller);
}

function readableByteStreamControllerFillPullIntoDescriptorFromQueue(
controller,
desc) {
Expand Down Expand Up @@ -2773,6 +2843,7 @@ function readableByteStreamControllerRespondInReadableState(
buffer,
bytesFilled,
byteLength,
type,
} = desc;

if (bytesFilled + bytesWritten > byteLength)
Expand All @@ -2783,6 +2854,17 @@ function readableByteStreamControllerRespondInReadableState(
bytesWritten,
desc);

if (type === 'none') {
readableByteStreamControllerEnqueueDetachedPullIntoToQueue(
controller,
desc
);
readableByteStreamControllerProcessPullIntoDescriptorsUsingQueue(
controller
);
return;
}

if (desc.bytesFilled < desc.elementSize)
return;

Expand Down
8 changes: 0 additions & 8 deletions test/wpt/status/streams.json
Expand Up @@ -16,17 +16,9 @@
"fail": {
"expected": [
"ReadableStream with byte source: enqueue() discards auto-allocated BYOB request",
"ReadableStream with byte source: releaseLock() with pending read(view), read(view) on second reader, respond()",
"ReadableStream with byte source: releaseLock() with pending read(view), read(view) on second reader with 1 element Uint16Array, respond(1)",
"ReadableStream with byte source: releaseLock() with pending read(view), read(view) on second reader with 2 element Uint8Array, respond(3)",
"ReadableStream with byte source: releaseLock() with pending read(view), read(view) on second reader, respondWithNewView()",
"ReadableStream with byte source: releaseLock() with pending read(view), read(view) on second reader, enqueue()",
"ReadableStream with byte source: releaseLock() with pending read(view), read(view) on second reader, close(), respond(0)",
"ReadableStream with byte source: autoAllocateChunkSize, releaseLock() with pending read(), read() on second reader, respond()",
"ReadableStream with byte source: autoAllocateChunkSize, releaseLock() with pending read(), read() on second reader, enqueue()",
"ReadableStream with byte source: autoAllocateChunkSize, releaseLock() with pending read(), read(view) on second reader, respond()",
"ReadableStream with byte source: autoAllocateChunkSize, releaseLock() with pending read(), read(view) on second reader, enqueue()",
"ReadableStream with byte source: read(view) with 1 element Uint16Array, respond(1), releaseLock(), read(view) on second reader with 1 element Uint16Array, respond(1)",
"ReadableStream with byte source: read(view) with 1 element Uint16Array, respond(1), releaseLock(), read() on second reader, enqueue()"
]
}
Expand Down