Skip to content

Commit 2266a4b

Browse files
daeyeondanielleadams
authored andcommitted
stream: improve respondWithNewView()
This fixes validating an ArrayBufferView given to ReadableStreamBYOBRequest.respondWithNewView() to improve the web streams compatibility. Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: #43866 Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 81a2194 commit 2266a4b

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

lib/internal/webstreams/readablestream.js

+5
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ const {
102102
extractHighWaterMark,
103103
extractSizeAlgorithm,
104104
lazyTransfer,
105+
isViewedArrayBufferDetached,
105106
isBrandCheck,
106107
resetQueue,
107108
setPromiseHandled,
@@ -681,6 +682,10 @@ class ReadableStreamBYOBRequest {
681682
'This BYOB request has been invalidated');
682683
}
683684

685+
if (isViewedArrayBufferDetached(view)) {
686+
throw new ERR_INVALID_STATE.TypeError('Viewed ArrayBuffer is detached');
687+
}
688+
684689
readableByteStreamControllerRespondWithNewView(controller, view);
685690
}
686691

lib/internal/webstreams/util.js

+20
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const {
1414
PromiseReject,
1515
ReflectGet,
1616
Symbol,
17+
Uint8Array,
1718
} = primordials;
1819

1920
const {
@@ -128,6 +129,24 @@ function transferArrayBuffer(buffer) {
128129
return res;
129130
}
130131

132+
function isArrayBufferDetached(buffer) {
133+
if (ArrayBufferGetByteLength(buffer) === 0) {
134+
try {
135+
new Uint8Array(buffer);
136+
} catch {
137+
return true;
138+
}
139+
}
140+
return false;
141+
}
142+
143+
function isViewedArrayBufferDetached(view) {
144+
return (
145+
ArrayBufferViewGetByteLength(view) === 0 &&
146+
isArrayBufferDetached(ArrayBufferViewGetBuffer(view))
147+
);
148+
}
149+
131150
function dequeueValue(controller) {
132151
assert(controller[kState].queue !== undefined);
133152
assert(controller[kState].queueTotalSize !== undefined);
@@ -225,6 +244,7 @@ module.exports = {
225244
lazyTransfer,
226245
isBrandCheck,
227246
isPromisePending,
247+
isViewedArrayBufferDetached,
228248
peekQueueValue,
229249
resetQueue,
230250
setPromiseHandled,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('node:assert');
5+
6+
let pass = 0;
7+
8+
{
9+
// ReadableStream with byte source: respondWithNewView() throws if the
10+
// supplied view's buffer has a different length (in the closed state)
11+
const stream = new ReadableStream({
12+
pull: common.mustCall(async (c) => {
13+
const view = new Uint8Array(new ArrayBuffer(10), 0, 0);
14+
15+
c.close();
16+
17+
assert.throws(() => c.byobRequest.respondWithNewView(view), {
18+
code: 'ERR_INVALID_ARG_VALUE',
19+
name: 'RangeError',
20+
});
21+
pass++;
22+
}),
23+
type: 'bytes',
24+
});
25+
26+
const reader = stream.getReader({ mode: 'byob' });
27+
reader.read(new Uint8Array([4, 5, 6]));
28+
}
29+
30+
{
31+
// ReadableStream with byte source: respondWithNewView() throws if the
32+
// supplied view's buffer has been detached (in the closed state)
33+
const stream = new ReadableStream({
34+
pull: common.mustCall((c) => {
35+
c.close();
36+
37+
// Detach it by reading into it
38+
const view = new Uint8Array([1, 2, 3]);
39+
reader.read(view);
40+
41+
assert.throws(() => c.byobRequest.respondWithNewView(view), {
42+
code: 'ERR_INVALID_STATE',
43+
name: 'TypeError',
44+
});
45+
pass++;
46+
}),
47+
type: 'bytes',
48+
});
49+
50+
const reader = stream.getReader({ mode: 'byob' });
51+
reader.read(new Uint8Array([4, 5, 6]));
52+
}
53+
54+
process.on('exit', () => assert.strictEqual(pass, 2));

0 commit comments

Comments
 (0)