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

fs: improve argument handling for ReadStream #19898

Closed
wants to merge 26 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
e507f93
fs: improve argument handling for ReadStream
ryzokuken Apr 9, 2018
30c285f
test,fs: update tests for improvements in ReadStream
ryzokuken Apr 10, 2018
915d77c
Remove inconsistency
ryzokuken Apr 10, 2018
17d6d55
Separate testing for start and end
ryzokuken Apr 10, 2018
36574f4
Remove invalid check
ryzokuken Apr 10, 2018
e4415d5
Address comments regarding handling undefined
ryzokuken Apr 10, 2018
b946616
Refactor conditions
ryzokuken Apr 11, 2018
d00b46a
fs,doc: add documentation regarding changes to createReadStream
ryzokuken Apr 13, 2018
93b0915
Add a changes block and move new documentation towards the end
ryzokuken Apr 14, 2018
5d03199
Replace version by REPLACEME
ryzokuken Apr 14, 2018
047529b
Minimize comments and documentation
ryzokuken Apr 15, 2018
a90457b
Address nit by adding parens
ryzokuken Apr 15, 2018
c3be59a
Resolve merge conflict
ryzokuken Apr 16, 2018
41b3a2e
Thow RangeError on fractional values
ryzokuken Apr 20, 2018
3530baa
Cleanup fs.md
ryzokuken Apr 23, 2018
4863dba
Fix error
ryzokuken Apr 24, 2018
1cf1814
Fix failures
ryzokuken Apr 25, 2018
2c35628
Address nit
ryzokuken Apr 26, 2018
a24f115
Use isSafeInteger over isInteger
ryzokuken Apr 27, 2018
0368397
Change error message
ryzokuken Apr 29, 2018
b6fa515
Improve error handling
ryzokuken May 5, 2018
7bda63c
Refactor fs tests for brevity
ryzokuken May 5, 2018
c34ba43
Address nits
ryzokuken May 8, 2018
30e36c2
Update RangeError error message
ryzokuken May 9, 2018
9af225c
Update RangeError error message v2
ryzokuken May 9, 2018
c76e75a
Fix lint errors
ryzokuken May 9, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactor conditions
  • Loading branch information
ryzokuken committed Apr 16, 2018

Verified

This commit was signed with the committer’s verified signature. The key has expired.
addaleax Anna Henningsen
commit b9466160e870d215dd1b14392e636bf4bdc1d7cc
20 changes: 10 additions & 10 deletions lib/fs.js
Original file line number Diff line number Diff line change
@@ -2013,9 +2013,6 @@ function ReadStream(path, options) {
this.bytesRead = 0;
this.closed = false;

// Case 0: If end is undefined, set end to Infinity.
this.end = this.end === undefined ? Infinity : this.end;

if (this.start !== undefined) {
// Case 1: If the type of start is not 'number', throw ERR_INVALID_ARG_TYPE
// (TypeError).
@@ -2041,16 +2038,13 @@ function ReadStream(path, options) {

// Case 5: If start is a whole number, work perfectly.

// Case 6: If start is greater than end, throw ERR_OUT_OF_RANGE (RangeError)
if (this.start > this.end) {
const errVal = `{start: ${this.start}, end: ${this.end}}`;
throw new ERR_OUT_OF_RANGE('start', '<= "end"', errVal);
}

this.pos = this.start;
}

if (this.end !== undefined) {
// Case 0: If end is undefined, set end to Infinity.
if (this.end === undefined) {
this.end = Infinity;
} else {
// Case 1: If the type of end is not 'number', throw ERR_INVALID_ARG_TYPE
// (TypeError).
if (typeof this.end !== 'number') {
@@ -2074,6 +2068,12 @@ function ReadStream(path, options) {
}

// Case 5: If end is a whole number, work perfectly.

// Case 6: If start is greater than end, throw ERR_OUT_OF_RANGE (RangeError)
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);
Copy link
Member

Choose a reason for hiding this comment

The 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:

The value of "start" is out of range. It must be <= ${this.end}. Received ${this.start}

instead of:

The value of "start" is out of range. It must be <= "end". Received {start: ${this.start}, end: ${this.end}}

It just seems better to read for me. It is also the way how it is done in e.g. the buffer module.

But that is just a suggestion.

Copy link
Contributor Author

@ryzokuken ryzokuken May 5, 2018

Choose a reason for hiding this comment

The 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 end is 4, and start is 6. The error message would be: The value of "start" is out of range. It must be <= 4. Received 6.

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: The value of "start" is out of range. It must be <= "end" (here: 4). Received 6 instead?

Copy link
Member

Choose a reason for hiding this comment

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

👍 that works as well.

}
}

if (typeof this.fd !== 'number')