Skip to content
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

Merged

Conversation

daeyeon
Copy link
Member

@daeyeon daeyeon commented Aug 3, 2022

This fixes validations below related to isDetachedBuffer using a function introduced in #43866.

https://streams.spec.whatwg.org/#rs-byob-request-respond

The respond(bytesWritten) method steps are:

If this.[[controller]] is undefined, throw a TypeError exception.
If ! IsDetachedBuffer(this.[[view]].[[ArrayBuffer]]) is true, throw a TypeError exception.

https://streams.spec.whatwg.org/#byob-reader-read

The read(view) method steps are:

If view.[[ByteLength]] is 0, return a promise rejected with a TypeError exception.
If view.[[ViewedArrayBuffer]].[[ArrayBufferByteLength]] is 0, return a promise rejected with a TypeError exception.
If ! IsDetachedBuffer(view.[[ViewedArrayBuffer]]) is true, return a promise rejected with a TypeError exception.

https://streams.spec.whatwg.org/#readable-byte-stream-controller-enqueue

  1. If controller.[[pendingPullIntos]] is not empty,

8.1 Let firstPendingPullInto be controller.[[pendingPullIntos]][0].
8.2 If ! IsDetachedBuffer(firstPendingPullInto’s buffer) is true, throw a TypeError exception.

Refs: #43866 (review)

Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com

Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Aug 3, 2022
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
@daeyeon daeyeon added the commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. label Aug 6, 2022
lib/internal/webstreams/readablestream.js Outdated Show resolved Hide resolved
Comment on lines 134 to 136
function isDetachedBuffer(buffer) {
if (!isArrayBuffer(buffer))
throw new ERR_INVALID_ARG_TYPE('buffer', 'ArrayBuffer', buffer);
Copy link
Contributor

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.

Copy link
Member Author

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
@LiviaMedeiros LiviaMedeiros added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 8, 2022
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 8, 2022
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot
Copy link
Collaborator

@daeyeon daeyeon added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Aug 10, 2022
Comment on lines 687 to 693
if (!isArrayBufferView(view)) {
throw new ERR_INVALID_ARG_TYPE(
'view',
['Buffer', 'TypedArray', 'DataView'],
view,
);
}
Copy link
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);
}
});

@@ -129,21 +131,25 @@ function transferArrayBuffer(buffer) {
return res;
}

function isArrayBufferDetached(buffer) {
function isDetachedBuffer(buffer) {
assert(isArrayBuffer(buffer));
Copy link
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
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.

return true;
}
}
return false;
}

function isViewedArrayBufferDetached(view) {
assert(isArrayBufferView(view));
Copy link
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));

Comment on lines 63 to 73
reader
.read(view)
.then(common.mustNotCall())
.catch(
common.mustCall(
common.expectsError({
code: 'ERR_INVALID_STATE',
name: 'TypeError',
}),
),
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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());

Comment on lines 85 to 95
reader
.read(view)
.then(common.mustNotCall())
.catch(
common.mustCall(
common.expectsError({
code: 'ERR_INVALID_STATE',
name: 'TypeError',
}),
),
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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
@daeyeon
Copy link
Member Author

daeyeon commented Aug 18, 2022

@aduh95 Applied the suggestions, PTAL.

@daeyeon daeyeon added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 18, 2022
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 18, 2022
@nodejs-github-bot
Copy link
Collaborator

@aduh95 aduh95 added the commit-queue Add this label to land a pull request using GitHub Actions. label Aug 18, 2022
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Aug 18, 2022
@nodejs-github-bot nodejs-github-bot merged commit 937520a into nodejs:main Aug 18, 2022
@nodejs-github-bot
Copy link
Collaborator

Landed in 937520a

@daeyeon daeyeon deleted the main.use-same-validator-220728.Thu.7686 branch August 18, 2022 08:40
ruyadorno pushed a commit that referenced this pull request Aug 23, 2022
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
PR-URL: #44114
Refs: #43866
Reviewed-By: LiviaMedeiros <livia@cirno.name>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Fyko pushed a commit to Fyko/node that referenced this pull request Sep 15, 2022
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>
@juanarbol
Copy link
Member

juanarbol commented Sep 30, 2022

Depends on #43866

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. commit-queue-squash Add this label to instruct the Commit Queue to squash all the PR commits into the first one. needs-ci PRs that need a full CI run. web streams
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants