-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stream: fix isDetachedBuffer
validations in ReadableStream
#44114
stream: fix isDetachedBuffer
validations in ReadableStream
#44114
Conversation
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
This reverts commit 6f6ba3e. Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
lib/internal/webstreams/util.js
Outdated
function isDetachedBuffer(buffer) { | ||
if (!isArrayBuffer(buffer)) | ||
throw new ERR_INVALID_ARG_TYPE('buffer', 'ArrayBuffer', buffer); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking comment: this change is fine in context of this PR, but it probably should be rewritten in a follow-up.
It would be better if is*
functions always returned boolean without throwing. If we want to throw, validate*
function is preferable.
Since either true
or false
won't make sense if provided value is not a buffer, it looks like an acceptable temporal solution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, it looks better to follow the coding convention.
I updated the util using assert
instead, since, as you mentioned, either true
or false
won't make sense if a provided value is not a buffer. We can guarantee a passing value is a buffer since it's internal util. PTAL.
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
This comment was marked as outdated.
This comment was marked as outdated.
if (!isArrayBufferView(view)) { | ||
throw new ERR_INVALID_ARG_TYPE( | ||
'view', | ||
['Buffer', 'TypedArray', 'DataView'], | ||
view, | ||
); | ||
} |
There was a problem hiding this comment.
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?
node/lib/internal/validators.js
Lines 200 to 206 in d83446b
const validateBuffer = hideStackFrames((buffer, name = 'buffer') => { | |
if (!isArrayBufferView(buffer)) { | |
throw new ERR_INVALID_ARG_TYPE(name, | |
['Buffer', 'TypedArray', 'DataView'], | |
buffer); | |
} | |
}); |
lib/internal/webstreams/util.js
Outdated
@@ -129,21 +131,25 @@ function transferArrayBuffer(buffer) { | |||
return res; | |||
} | |||
|
|||
function isArrayBufferDetached(buffer) { | |||
function isDetachedBuffer(buffer) { | |||
assert(isArrayBuffer(buffer)); |
There was a problem hiding this comment.
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).
assert(isArrayBuffer(buffer)); |
There was a problem hiding this comment.
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.
lib/internal/webstreams/util.js
Outdated
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
function isViewedArrayBufferDetached(view) { | ||
assert(isArrayBufferView(view)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
assert(isArrayBufferView(view)); |
reader | ||
.read(view) | ||
.then(common.mustNotCall()) | ||
.catch( | ||
common.mustCall( | ||
common.expectsError({ | ||
code: 'ERR_INVALID_STATE', | ||
name: 'TypeError', | ||
}), | ||
), | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reader | |
.read(view) | |
.then(common.mustNotCall()) | |
.catch( | |
common.mustCall( | |
common.expectsError({ | |
code: 'ERR_INVALID_STATE', | |
name: 'TypeError', | |
}), | |
), | |
); | |
assert.rejects( | |
reader.read(view), | |
{ | |
code: 'ERR_INVALID_STATE', | |
name: 'TypeError', | |
} | |
).then(common.mustCall()); |
reader | ||
.read(view) | ||
.then(common.mustNotCall()) | ||
.catch( | ||
common.mustCall( | ||
common.expectsError({ | ||
code: 'ERR_INVALID_STATE', | ||
name: 'TypeError', | ||
}), | ||
), | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reader | |
.read(view) | |
.then(common.mustNotCall()) | |
.catch( | |
common.mustCall( | |
common.expectsError({ | |
code: 'ERR_INVALID_STATE', | |
name: 'TypeError', | |
}), | |
), | |
); | |
assert.rejects( | |
reader.read(view), | |
{ | |
code: 'ERR_INVALID_STATE', | |
name: 'TypeError', | |
} | |
).then(common.mustCall()); |
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
@aduh95 Applied the suggestions, PTAL. |
Landed in 937520a |
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com PR-URL: nodejs#44114 Refs: nodejs#43866 Reviewed-By: LiviaMedeiros <livia@cirno.name> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Depends on #43866 |
This fixes validations below related to
isDetachedBuffer
using a function introduced in #43866.https://streams.spec.whatwg.org/#rs-byob-request-respond
https://streams.spec.whatwg.org/#byob-reader-read
https://streams.spec.whatwg.org/#readable-byte-stream-controller-enqueue
Refs: #43866 (review)
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com