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
Prev Previous commit
Next Next commit
fixup: use assert for isDetachedBuffer
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
  • Loading branch information
daeyeon committed Aug 7, 2022
commit fe816cdff06c68be3b17e6b87fc6f41346e7f406
18 changes: 8 additions & 10 deletions lib/internal/webstreams/readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,14 @@ class ReadableStreamBYOBRequest {
'This BYOB request has been invalidated');
}

if (!isArrayBufferView(view)) {
throw new ERR_INVALID_ARG_TYPE(
'view',
['Buffer', 'TypedArray', 'DataView'],
view,
);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you use validateBuffer instead?

const validateBuffer = hideStackFrames((buffer, name = 'buffer') => {
if (!isArrayBufferView(buffer)) {
throw new ERR_INVALID_ARG_TYPE(name,
['Buffer', 'TypedArray', 'DataView'],
buffer);
}
});


if (isViewedArrayBufferDetached(view)) {
throw new ERR_INVALID_STATE.TypeError('Viewed ArrayBuffer is detached');
}
Expand Down Expand Up @@ -2507,16 +2515,6 @@ function readableByteStreamControllerRespondWithNewView(controller, view) {
const desc = pendingPullIntos[0];
assert(stream[kState].state !== 'errored');

if (!isArrayBufferView(view)) {
throw new ERR_INVALID_ARG_TYPE(
'view',
[
'Buffer',
'TypedArray',
'DataView',
],
view);
}
const viewByteLength = ArrayBufferViewGetByteLength(view);
const viewByteOffset = ArrayBufferViewGetByteOffset(view);
const viewBuffer = ArrayBufferViewGetBuffer(view);
Expand Down
11 changes: 2 additions & 9 deletions lib/internal/webstreams/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,7 @@ function transferArrayBuffer(buffer) {
}

function isDetachedBuffer(buffer) {
if (!isArrayBuffer(buffer))
throw new ERR_INVALID_ARG_TYPE('buffer', 'ArrayBuffer', buffer);
assert(isArrayBuffer(buffer));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this useful? I think the error thrown by V8 enough (and clearer).

Suggested change
assert(isArrayBuffer(buffer));

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, seemingly it can be deleted in the current version of this PR. Fixed.

if (ArrayBufferGetByteLength(buffer) === 0) {
Comment thread
LiviaMedeiros marked this conversation as resolved.
// TODO(daeyeon): Consider using C++ builtin to improve performance.
try {
Expand All @@ -147,13 +146,7 @@ function isDetachedBuffer(buffer) {
}

function isViewedArrayBufferDetached(view) {
if (!isArrayBufferView(view)) {
throw new ERR_INVALID_ARG_TYPE(
'view',
['Buffer', 'TypedArray', 'DataView'],
view,
);
}
assert(isArrayBufferView(view));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Suggested change
assert(isArrayBufferView(view));

return (
ArrayBufferViewGetByteLength(view) === 0 &&
isDetachedBuffer(ArrayBufferViewGetBuffer(view))
Expand Down