-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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 stream option and set encoding on fs.WritableStream #1412
fs: check stream option and set encoding on fs.WritableStream #1412
Conversation
@@ -1615,6 +1615,11 @@ function ReadStream(path, options) { | |||
if (!(this instanceof ReadStream)) | |||
return new ReadStream(path, options); | |||
|
|||
if (typeof options === 'string') | |||
options = { encoding: options }; | |||
else if (options && 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.
You probably should be more strict with this check. For example, options = 0
would slip through.
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.
Thank you, I will fix it.
LGTM. No strong opinion on the error message. |
@bnoordhuis @cjihrig I fixed!! Thank you for your review. |
@@ -1615,8 +1615,15 @@ function ReadStream(path, options) { | |||
if (!(this instanceof ReadStream)) | |||
return new ReadStream(path, options); | |||
|
|||
if (options === undefined || options === 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.
You can probably drop the null
check, as it will work fine with Object.create()
.
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!
FWIW, this can be interpreted as a breaking change - something was silently failing before could now throw an exception. |
Yes, but currently |
If the "is breaking?" is controversial we can just tag it for semver-major and land it in the upcoming 2.0 anyways. |
We already break the compatible in |
oh i didn't even know you could set options as a string. never saw that anywhere. i don't consider this "breaking" because 1) it's not documented and 2) it wasn't tested prior. LGTM! |
Could I land it on master ?? |
+1 for patch, not major, because #635 got through on a patch - @yosuke-furukawa I think so. |
OK i will land this. Thanks for your remind @brendanashworth |
Closing in favor of the two new ones. |
Reviewer: @bnoordhuis
Problem 1
On Node.js v0.12
On io.js v1.6.4
Bug found modules
Cause
I found this pull request #635 . this PR uses
Object.create
in createReadStream.Object.create
does not acceptstring
.Of course, the above scripts and modules are weird. we have never written such options in our api document.
BUT I have 2 opinions.
TypeError: Object prototype may only be an Object or null: utf8
) is not so easy to understand. We should returnBad Arguments
like other functions.fs.createReadStream
andfs.createWriteStream
should accept 2nd string argument as an encoding type. our existing functions likefs.readFile
,fs.writeFile
, have same behavior like here. the behavior misleads some developers.Fix
Problem2
According to the createWriteStream api docs,
encoding
option could be found in the default.But I set the encoding option to
fs.createWriteStream('foo.txt', {encoding: 'utf8'})
, the encoding option is ignored.Fix
This pull request includes the fix both problem1 and problem2. comments and discussions are welcome.