-
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
doc: add description for filehandle.write(string[, position[, encoding]]) #23224
Conversation
161f349
to
1350c67
Compare
cc @jasnell |
doc/api/fs.md
Outdated
the value will be coerced to one. | ||
|
||
`position` refers to the offset from the beginning of the file where this data | ||
should be written. If `typeof position !== 'number'` the data will be written at |
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.
Could this be simply written as, "If the type of position is not a number"?
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.
So I basically copied the same wording used here: https://github.com/nodejs/node/pull/23224/files#diff-acabf706a8aa070a8796e3573f7e4678R3910
This style is used in the callback APIs too. I figured I would use the same wording that's used in those other places. I'd be happy to clarify those other places also if you think that's a good idea.
Looks like you are correct. If possible, could you come up with a test case which will break because of this? cc @nodejs/fs |
Sure thing. I'll look to see how it's tested in the callback based API and see if I can get some inspiration from there. |
I believe so - yes, and work improving the promises fs API is always appreciated. (cc @jasnell ) Changes in this PR LGTM |
`encoding` is the expected string encoding. | ||
|
||
It is unsafe to use `filehandle.write()` multiple times on the same file | ||
without waiting for the `Promise` to be resolved (or rejected). For this |
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.
Nit: a rejected promise is still resolved (and so is a promise following another promise) - fulfilled
instead of resolved or "settled" instead of either would work here.
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.
Hey there, I've addressed some other feedback but I haven't addressed this one yet. I did that for two reasons.
- This is the same wording used in other parts of the documentation. See the docs for the filehandle.write(buffer)
- I would argue that explicitly using the words
resolved
andrejected
make sense in this context. If you're familiar with Promises, this makes sense. I would argue the same cannot exactly be said about the wordfulfilled
because it's not as commonly used when referring to Promises. (at least as far as I can tell). In my opinion that potentially introduces some ambiguity.
All that being said: I am happy to make the suggested changes here, as well as in the other parts of the same doc to ensure consistency.
Hey folks, sorry I was away for the past couple of days so I couldn't do any more work on this. I mentioned that I would try to come up with some test cases wrt to the encoding option. Still willing to do this, but I'm wondering should it be done as part of this PR or as a different one? Thanks! |
@darahayes I would say, you can add them as separate commits in this PR itself. Because without the code changes, this doc patch would not be correct. |
1350c67
to
160ddd6
Compare
160ddd6
to
6faeff1
Compare
Hey folks, it's been so long but I was finally able to get some more time with this issue. I've added a code change that ensures the same behaviour as the callback-based API. A default value of All that being said, there was a request to come up with a test case for this and I'm struggling with that a little bit. The callback based I'm not 100% sure how to test that the string contents written to a file is indeed |
6faeff1
to
30e241d
Compare
@thefourtheye Hey there. Sorry to bother you like this, I figured I would ping you as the original reviewer. I know you folks were probably really busy with all the PRs from the recent code and learn events 😃. Just wanted to bump for some visibility. Just a quick summary of where I'm at - I believe I have made the necessary changes but I'm not 100% sure how write a good test case for this. I was hoping I could get some pointers if anyone has the time. Thanks! |
@darahayes No problem at all. I am sorry I couldn't get to this on time. Let me take a look at this today. Thanks for being patient. |
Okay, looks like I was wrong.
I took a deeper look at the code and the default encoding value is actually set to UTF-8. Let me break this down. node/lib/internal/fs/promises.js Lines 255 to 256 in dde9b3a
which actually parses the encoding like this Line 1720 in 6ae6383
Lines 89 to 100 in fcd7a72
If the Lines 79 to 85 in fcd7a72
will take care of it. @darahayes Sorry, I misled you. Could you please drop the commit which introduces default value for encoding? |
Add missing docs for filehandle.write(string[, position[, encoding]]) In the fs.promises API. fixes: nodejs#20406
30e241d
to
8bc4de6
Compare
@thefourtheye thanks so much for your help! I'm kicking myself that I didn't figure that out sooner. I've dropped the commit you mentioned and I also addressed some of the feedback on the docs changes. |
@nodejs/fs PTAL once. |
Landed in 692b09f |
Add missing docs for filehandle.write(string[, position[, encoding]]) In the fs.promises API. Fixes: nodejs#20406 PR-URL: nodejs#23224 Reviewed-By: Sakthipriyan Vairamani <[email protected]>
Thanks @Trott. I am currently traveling, otherwise I would have landed this sooner. @darahayes Thanks for your patience in getting this done. |
Add missing docs for filehandle.write(string[, position[, encoding]]) In the fs.promises API. Fixes: #20406 PR-URL: #23224 Reviewed-By: Sakthipriyan Vairamani <[email protected]>
Add missing docs for filehandle.write(string[, position[, encoding]]) In the fs.promises API. Fixes: #20406 PR-URL: #23224 Reviewed-By: Sakthipriyan Vairamani <[email protected]>
Add missing docs for filehandle.write(string[, position[, encoding]]) In the fs.promises API. Fixes: #20406 PR-URL: #23224 Reviewed-By: Sakthipriyan Vairamani <[email protected]>
Add missing docs for filehandle.write(string[, position[, encoding]]) In the fs.promises API. Fixes: nodejs#20406 PR-URL: nodejs#23224 Reviewed-By: Sakthipriyan Vairamani <[email protected]>
Add missing docs for filehandle.write(string[, position[, encoding]]) In the fs.promises API. Fixes: #20406 PR-URL: #23224 Reviewed-By: Sakthipriyan Vairamani <[email protected]>
Hey folks, this PR adds missing docs for
filehandle.write(string[, position[, encoding]])
in thefs.promises
API.As per #20406, only the buffer version of this API is documented right now.
One thing I wanted to ask. I believe the intention is to reconcile the Promise based API as much as possible with the callback based one. In the docs submitted with this PR, I described the
encoding
option has having the default valueutf8
. This is the behaviour in the callback based API.Based off the code here. I don't think that's actually happening in the Promise based version.
I have two questions:
utf8
is used? I'd be delighted to provide a PR and some tests.Cheers!
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes