-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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: check argument type on createStream method #1845
Conversation
Please re-review for document and test. |
@@ -1617,8 +1617,15 @@ function ReadStream(path, options) { | |||
if (!(this instanceof ReadStream)) | |||
return new ReadStream(path, options); | |||
|
|||
if (options === undefined) |
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.
Perhaps use loose equality here to grab null
also? It'll avoid an Object.create(null)
call too.
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.
It might be better to change the check on line 1624 to else if (options === null || typeof options !== 'object')
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.
latest node and io.js accept null
on fs.createReadStream.
current pull request does not change the behavior on fs.createReadStream, accepts string
and throws proper exception.
if we don't accept null
, this api may break the compatibility.
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.
Current versions accept null
and any other falsey value so this is still a breaking change. If this were to land, it would make sense to throw on null
, since this is already a breaking change and null
is not a useful value to support here. This needs to be given a lot of consideration, especially after the events of this past weekend.
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.
Why is this already a breaking change (disregarding null
)?
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.
Because fs.createReadStream()
currently allows any falsey value (false
, NaN
, etc.). Under this PR, passing in false
, for example, would throw.
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.
options = options || {}
used to allow other falsy values as well.
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.
hm. OK. I will disallow null
.
our current spec is to allow object
only.
And under this PR, we can use string
as an encoding.
So, this function allows object
and string
. the other types are not allowed.
If we found disallowed type, we should throw TypeError
.
But null is unclear type.
I think null and undefined are not easy to use properly for beginner.
So my first impression, we should allow null just like undefined.
But I will follow @cjihrig suggestion. his spec is clear.
options = {}; | ||
else if (typeof options === 'string') | ||
options = { encoding: options }; | ||
else if (options === null || typeof options !== 'object') |
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.
fixed.
Fixed styles. |
I will land this. |
Add string encoding option for fs.createReadStream and fs.createWriteStream. and check argument type more strictly PR-URL: #1845 Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
landed 353e26e |
@yosuke-furukawa looks like this wasn't properly signed-off on. It's best to wait for an explicit |
hm,,, I got some review and gets I just separate the pull request. But I should be careful more and more. |
separeted #1412
On Node.js v0.12
On io.js v1.6.4
I added document and fixed test to avoid linter errors.