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: fix respondWithNewView() errors when view.byteOffset != 0 #46465

Merged
merged 1 commit into from
Feb 24, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/webstreams/readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2965,7 +2965,7 @@ function readableByteStreamControllerRespondWithNewView(controller, view) {
if (byteOffset + bytesFilled !== viewByteOffset)
throw new ERR_INVALID_ARG_VALUE.RangeError('view', view);

if (bytesFilled + viewByteOffset > byteLength)
if (bytesFilled + viewByteLength > byteLength)
throw new ERR_INVALID_ARG_VALUE.RangeError('view', view);

if (bufferByteLength !== viewBufferByteLength)
Expand Down
30 changes: 30 additions & 0 deletions test/parallel/test-whatwg-readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -1657,3 +1657,33 @@ class Source {
reader.read(new DataView(buffer))
.then(common.mustCall());
}

{
const stream = new ReadableStream({
type: 'bytes',
autoAllocateChunkSize: 128,
pull: common.mustCall((controller) => {
const view = controller.byobRequest.view;
const dest = new Uint8Array(
view.buffer,
view.byteOffset,
view.byteLength
);
dest.fill(1);
controller.byobRequest.respondWithNewView(dest);
}),
});

const reader = stream.getReader({ mode: 'byob' });

const buffer = new ArrayBuffer(10);
const view = new Uint8Array(
buffer,
1,
3
);

reader.read(view).then(common.mustCall(({ value }) => {
assert.deepStrictEqual(value, new Uint8Array([1, 1, 1]));
}));
}