Skip to content

stream: fix isDetachedBuffer validations in ReadableStream - #44114

Merged
nodejs-github-bot merged 9 commits into
nodejs:mainfrom
daeyeon:main.use-same-validator-220728.Thu.7686
Aug 18, 2022
Merged

stream: fix isDetachedBuffer validations in ReadableStream#44114
nodejs-github-bot merged 9 commits into
nodejs:mainfrom
daeyeon:main.use-same-validator-220728.Thu.7686

Conversation

@daeyeon

Copy link
Copy Markdown
Member

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-botnodejs-github-bot added the needs-ci PRs that need a full CI run. label Aug 3, 2022
Comment threadlib/internal/webstreams/readablestream.js Outdated
Comment threadlib/internal/webstreams/readablestream.js
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
Comment threadlib/internal/webstreams/util.js
Comment threadlib/internal/webstreams/readablestream.js Outdated
Comment threadlib/internal/webstreams/readablestream.js
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
@daeyeondaeyeon 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
Comment threadlib/internal/webstreams/readablestream.js Outdated
Comment threadlib/internal/webstreams/util.js Outdated
Comment on lines +134 to +136
function isDetachedBuffer(buffer) {
if (!isArrayBuffer(buffer))
throw new ERR_INVALID_ARG_TYPE('buffer', 'ArrayBuffer', buffer);

Copy link
Copy Markdown
Member

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
Copy Markdown
MemberAuthor

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
@LiviaMedeirosLiviaMedeiros added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 8, 2022
@github-actionsgithub-actionsBot 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
Copy Markdown
Collaborator

@daeyeondaeyeon 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
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?

constvalidateBuffer=hideStackFrames((buffer,name='buffer')=>{
if(!isArrayBufferView(buffer)){
thrownewERR_INVALID_ARG_TYPE(name,
['Buffer','TypedArray','DataView'],
buffer);
}
});

Comment threadlib/internal/webstreams/util.js Outdated

function isArrayBufferDetached(buffer) {
function isDetachedBuffer(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
MemberAuthor

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.

Comment threadlib/internal/webstreams/util.js Outdated
}

function isViewedArrayBufferDetached(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));

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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
MemberAuthor

@aduh95 Applied the suggestions, PTAL.

@daeyeondaeyeon added the request-ci Add this label to start a Jenkins CI on a PR. label Aug 18, 2022
@github-actionsgithub-actionsBot removed the request-ci Add this label to start a Jenkins CI on a PR. label Aug 18, 2022
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@aduh95aduh95 added the commit-queue Add this label to land a pull request using GitHub Actions. label Aug 18, 2022
@nodejs-github-botnodejs-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:mainAug 18, 2022
@nodejs-github-bot

Copy link
Copy Markdown
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>
@ruyadornoruyadorno mentioned this pull request Aug 23, 2022
@juanarbol

juanarbol commented Sep 30, 2022

Copy link
Copy Markdown
Member

Depends on #43866

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author readyPRs that have at least one approval, no pending requests for changes, and a CI started.commit-queue-squashAdd this label to instruct the Commit Queue to squash all the PR commits into the first one.needs-ciPRs that need a full CI run.web streams

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@daeyeon@nodejs-github-bot@juanarbol@aduh95@LiviaMedeiros