diff --git a/streams/readable-byte-streams/bad-buffers-and-views.any.js b/streams/readable-byte-streams/bad-buffers-and-views.any.js index cc75dc46f9830c..121694fb168fa3 100644 --- a/streams/readable-byte-streams/bad-buffers-and-views.any.js +++ b/streams/readable-byte-streams/bad-buffers-and-views.any.js @@ -250,6 +250,23 @@ async_test(t => { }, 'ReadableStream with byte source: respondWithNewView() throws if the supplied view\'s buffer has a ' + 'different length (in the readable state)'); +async_test(t => { + // Tests https://github.com/nodejs/node/issues/41886 + const stream = new ReadableStream({ + pull: t.step_func_done(c => { + const view = new Uint8Array(new ArrayBuffer(11), 0, 3); + + assert_throws_js(RangeError, () => c.byobRequest.respondWithNewView(view)); + }), + type: 'bytes', + autoAllocateChunkSize: 10 + }); + const reader = stream.getReader(); + + reader.read(); +}, 'ReadableStream with byte source: respondWithNewView() throws if the supplied view\'s buffer has a ' + + 'different length (autoAllocateChunkSize)'); + async_test(t => { const stream = new ReadableStream({ pull: t.step_func_done(c => { diff --git a/streams/readable-byte-streams/general.any.js b/streams/readable-byte-streams/general.any.js index 3b88e8dd956220..1090e80786f5c0 100644 --- a/streams/readable-byte-streams/general.any.js +++ b/streams/readable-byte-streams/general.any.js @@ -2876,3 +2876,26 @@ promise_test(async t => { }, 'ReadableStream with byte source: read(view) with 1 element Uint16Array, respond(1), releaseLock(), read() on ' + 'second reader, enqueue()'); + +promise_test(async t => { + // Tests https://github.com/nodejs/node/issues/41886 + const stream = new ReadableStream({ + type: 'bytes', + autoAllocateChunkSize: 10, + pull: t.step_func((c) => { + const newView = new Uint8Array(c.byobRequest.view.buffer, 0, 3); + newView.set([20, 21, 22]); + c.byobRequest.respondWithNewView(newView); + }) + }); + + const reader = stream.getReader(); + const result = await reader.read(); + assert_false(result.done, 'result.done'); + + const view = result.value; + assert_equals(view.byteOffset, 0, 'result.value.byteOffset'); + assert_equals(view.byteLength, 3, 'result.value.byteLength'); + assert_equals(view.buffer.byteLength, 10, 'result.value.buffer.byteLength'); + assert_array_equals([...new Uint8Array(view)], [20, 21, 22], 'result.value'); +}, 'ReadableStream with byte source: autoAllocateChunkSize, read(), respondWithNewView()');