Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
stream: fix isDetachedBuffer validations
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
  • Loading branch information
daeyeon committed Aug 3, 2022
commit e5e32c096fa3e1885c8f1cc9fb7cb97c8250c63f
26 changes: 8 additions & 18 deletions lib/internal/webstreams/readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ const {
extractHighWaterMark,
extractSizeAlgorithm,
lazyTransfer,
isDetachedBuffer,
isViewedArrayBufferDetached,
isBrandCheck,
resetQueue,
Expand Down Expand Up @@ -654,13 +655,8 @@ class ReadableStreamBYOBRequest {
'This BYOB request has been invalidated');
}

const viewByteLength = ArrayBufferViewGetByteLength(view);
const viewBuffer = ArrayBufferViewGetBuffer(view);
const viewBufferByteLength = ArrayBufferGetByteLength(viewBuffer);

if (viewByteLength === 0 || viewBufferByteLength === 0) {
throw new ERR_INVALID_STATE.TypeError(
'View ArrayBuffer is zero-length or detached');
if (isViewedArrayBufferDetached(view)) {
throw new ERR_INVALID_STATE.TypeError('Viewed ArrayBuffer is detached');
Comment thread
LiviaMedeiros marked this conversation as resolved.
}

readableByteStreamControllerRespond(controller, bytesWritten);
Expand Down Expand Up @@ -894,14 +890,9 @@ class ReadableStreamBYOBReader {
],
view));
}
const viewByteLength = ArrayBufferViewGetByteLength(view);
const viewBuffer = ArrayBufferViewGetBuffer(view);
const viewBufferByteLength = ArrayBufferGetByteLength(viewBuffer);

if (viewByteLength === 0 || viewBufferByteLength === 0) {
return PromiseReject(
new ERR_INVALID_STATE.TypeError(
'View ArrayBuffer is zero-length or detached'));
if (isViewedArrayBufferDetached(view)) {
throw new ERR_INVALID_STATE.TypeError('Viewed ArrayBuffer is detached');
Comment thread
LiviaMedeiros marked this conversation as resolved.
Outdated
}
// Supposed to assert here that the view's buffer is not
// detached, but there's no API available to use to check that.
Expand Down Expand Up @@ -2298,11 +2289,10 @@ function readableByteStreamControllerEnqueue(
if (pendingPullIntos.length) {
const firstPendingPullInto = pendingPullIntos[0];

const pendingBufferByteLength =
ArrayBufferGetByteLength(firstPendingPullInto.buffer);
if (pendingBufferByteLength === 0) {
if (isDetachedBuffer(firstPendingPullInto.buffer)) {
throw new ERR_INVALID_STATE.TypeError(
'Destination ArrayBuffer is zero-length or detached');
'Destination ArrayBuffer is detached',
);
}

firstPendingPullInto.buffer =
Expand Down
5 changes: 3 additions & 2 deletions lib/internal/webstreams/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ function transferArrayBuffer(buffer) {
return res;
}

function isArrayBufferDetached(buffer) {
function isDetachedBuffer(buffer) {
if (ArrayBufferGetByteLength(buffer) === 0) {
Comment thread
LiviaMedeiros marked this conversation as resolved.
try {
new Uint8Array(buffer);
Expand All @@ -143,7 +143,7 @@ function isArrayBufferDetached(buffer) {
function isViewedArrayBufferDetached(view) {
return (
ArrayBufferViewGetByteLength(view) === 0 &&
isArrayBufferDetached(ArrayBufferViewGetBuffer(view))
isDetachedBuffer(ArrayBufferViewGetBuffer(view))
);
}

Expand Down Expand Up @@ -243,6 +243,7 @@ module.exports = {
extractSizeAlgorithm,
lazyTransfer,
isBrandCheck,
isDetachedBuffer,
isPromisePending,
isViewedArrayBufferDetached,
peekQueueValue,
Expand Down