-
Notifications
You must be signed in to change notification settings - Fork 30.2k
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
fs: improve argument handling for ReadStream #19898
Changes from 17 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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would instead use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Made the change. |
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could save an unnecessary assignment if you move this condition to the else and make it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will do, sure. |
||
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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
throw new ERR_OUT_OF_RANGE('end', 'integer', this.end); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is now not specific enough anymore. E.g., |
||
} | ||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at this again I would also change this to: throw new ERR_OUT_OF_RANGE('start', `<= ${this.end}`, this.start); This ends up as:
instead of:
It just seems better to read for me. It is also the way how it is done in e.g. the But that is just a suggestion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I'm not as inclined regarding this one. Let's assume that the passed value of I bet there would be a section of developers, already coping with a huge language barrier, who would might to the conclusion that there's some magic value (4 in this case) which "start" needs to be smaller than. What about: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 that works as well. |
||
} | ||
} | ||
|
||
if (typeof this.fd !== 'number') | ||
this.open(); | ||
|
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.
NaN
is 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.end
below.As far as error codes go, I think it would be fair to just use
ERR_INVALID_ARG_TYPE
if not an integer, andERR_OUT_OF_RANGE
if < 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.
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,
NaN
and floats aren't the wrong type, are they? They're stillNumber
s.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_TYPE
in case the type is really a mismatch. As @ryzokuken pointed outNaN
and 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).