Skip to content

Commit

Permalink
stream: fix respondWithNewView() errors when view.byteOffset != 0
Browse files Browse the repository at this point in the history
Fixes: #42851
Refs: https://github.com/whatwg/streams/blob/f894acdd417926a2121710803cef593e15127964/reference-implementation/lib/abstract-ops/readable-streams.js#L1756
PR-URL: #46465
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
  • Loading branch information
debadree25 authored and danielleadams committed Apr 5, 2023
1 parent ea71a2a commit ed2faa7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
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]));
}));
}

0 comments on commit ed2faa7

Please sign in to comment.