-
-
Notifications
You must be signed in to change notification settings - Fork 34.3k
fs: improve argument handling for ReadStream #19898
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
Changes from 15 commits
e507f93
30c285f
915d77c
17d6d55
36574f4
e4415d5
b946616
d00b46a
93b0915
5d03199
047529b
a90457b
c3be59a
41b3a2e
3530baa
4863dba
1cf1814
2c35628
a24f115
0368397
b6fa515
7bda63c
c34ba43
30e36c2
9af225c
c76e75a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2014,30 +2014,49 @@ function ReadStream(path, options) { | |
| this.closed = false; | ||
|
|
||
| if (this.start !== undefined) { | ||
| if (typeof this.start !== 'number' || Number.isNaN(this.start)) { | ||
| if (typeof this.start !== 'number') { | ||
| throw new ERR_INVALID_ARG_TYPE('start', 'number', this.start); | ||
| } | ||
| if (this.end === undefined) { | ||
| this.end = Infinity; | ||
| } else if (typeof this.end !== 'number' || Number.isNaN(this.end)) { | ||
| throw new ERR_INVALID_ARG_TYPE('end', 'number', this.end); | ||
|
|
||
| if (Number.isNaN(this.start)) { | ||
| throw new ERR_OUT_OF_RANGE('start', 'not NaN', this.start); | ||
| } | ||
|
|
||
| if (this.start > this.end) { | ||
| const errVal = `{start: ${this.start}, end: ${this.end}}`; | ||
| throw new ERR_OUT_OF_RANGE('start', '<= "end"', errVal); | ||
| if (this.start < 0) { | ||
| throw new ERR_OUT_OF_RANGE('start', 'positive', this.start); | ||
| } | ||
|
|
||
| if (!Number.isInteger(this.start)) { | ||
|
||
| throw new ERR_OUT_OF_RANGE('start', 'integer', this.start); | ||
| } | ||
|
|
||
| this.pos = this.start; | ||
| } | ||
|
|
||
| // Backwards compatibility: Make sure `end` is a number regardless of `start`. | ||
| // TODO(addaleax): Make the above typecheck not depend on `start` instead. | ||
| // (That is a semver-major change). | ||
| if (typeof this.end !== 'number') | ||
| if (this.end === undefined) { | ||
| this.end = Infinity; | ||
| else if (Number.isNaN(this.end)) | ||
| throw new ERR_INVALID_ARG_TYPE('end', 'number', this.end); | ||
| } else { | ||
| if (typeof this.end !== 'number') { | ||
| throw new ERR_INVALID_ARG_TYPE('end', 'number', this.end); | ||
| } | ||
|
|
||
| if (Number.isNaN(this.end)) { | ||
| throw new ERR_OUT_OF_RANGE('end', 'not NaN', this.end); | ||
| } | ||
|
|
||
| if (this.end < 0) { | ||
| throw new ERR_OUT_OF_RANGE('end', 'positive', this.end); | ||
| } | ||
|
|
||
| if (!Number.isInteger(this.end)) { | ||
|
||
| throw new ERR_OUT_OF_RANGE('start', 'integer', this.end); | ||
| } | ||
|
|
||
| if (this.start !== undefined && this.start > this.end) { | ||
| const errVal = `{start: ${this.start}, end: ${this.end}}`; | ||
| throw new ERR_OUT_OF_RANGE('start', '<= "end"', errVal); | ||
|
||
| } | ||
| } | ||
|
|
||
| if (typeof this.fd !== 'number') | ||
| this.open(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
With it throwing if not an integer, this check technically doesn't have to happen.
NaNis not an integer according toNumber.isInteger. Thetypeof this.start !== 'number'test can also be removed for the same reason. Perhaps that keeps it simpler?The same applies to
this.endbelow.As far as error codes go, I think it would be fair to just use
ERR_INVALID_ARG_TYPEif not an integer, andERR_OUT_OF_RANGEif < 0. Thoughts?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.
I would rather throw a little more descriptively, let the user know what went wrong.
That said, I'd love to hear everyone's opinions on this, and would love to change this accordingly.
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.
There are a couple PRs that consolidate validation functions. It is possible to improve the check here performance and code wise but functionality wise it is fine and I believe it is best to land it as is. It can still be improved later on.
Uh oh!
There was an error while loading. Please reload this page.
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.
@BridgeAR The only problem with "improving later" may be that some cases at one point may report out-of-range and after an update would report invalid-arg-type. For example, at this point I was suggesting to make the entire test the following:
So that changes NaN and floats to invalid-arg-type from the code as it is now. If that were to ever happen in the future, that would constitute another semver-major, wouldn't it?
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.
But if you think about it,
NaNand floats aren't the wrong type, are they? They're stillNumbers.They are invalid arguments because they lie outside of the rational input range.
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.
@ronkorving we only throw
ERR_INVALID_ARG_TYPEin case the type is really a mismatch. As @ryzokuken pointed outNaNand floats are of type number. So they should beERR_OUT_OF_RANGE.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.
I didn't realize the type was only allowed to be a JS type, not a type in the broader sense (like integer).