-
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
feat: make fs.read params optional #31402
Closed
lholmquist
wants to merge
17
commits into
nodejs:master
from
lholmquist:31237-fs-read-params-optional
Closed
Changes from 4 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
95f03ef
feat: make fs.read params optional
lholmquist 478d2f0
squash: doc updates
lholmquist 10d3902
squash: adding in test
lholmquist dfdd3b4
squash: better use of destructoring
lholmquist cf320a0
squash: refactor.
lholmquist 418b6fd
squash: Update doc/api/fs.md
lholmquist e3835c2
squash: Update doc/api/fs.md
lholmquist 128aeab
squash: reverting some doc updates
lholmquist 2c5d3e7
squash: Update doc/api/fs.md
lholmquist 6433727
squash: Update doc/api/fs.md
lholmquist 3923975
squash: add test for promise based
lholmquist f6f01b6
squash: add defaults to filehandler.read docs
lholmquist 55a2356
squash: add the use case for an optional options object
lholmquist f86bdc1
squash: add test for optional options option
lholmquist a4c3763
squash: a little more doc update
lholmquist e321b88
squash: doc linting
lholmquist cb8c0ef
squash: combine tests and add another test that passes options
lholmquist File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const fixtures = require('../common/fixtures'); | ||
const fs = require('fs'); | ||
const assert = require('assert'); | ||
const filepath = fixtures.path('x.txt'); | ||
const fd = fs.openSync(filepath, 'r'); | ||
|
||
const expected = Buffer.from('xyz\n'); | ||
const defaultBufferAsync = Buffer.alloc(16384); | ||
|
||
// Optional buffer, offset, length, position | ||
// fs.read(fd, callback); | ||
fs.read(fd, common.mustCall((err, bytesRead, buffer) => { | ||
assert.strictEqual(bytesRead, expected.length); | ||
assert.deepStrictEqual(defaultBufferAsync.length, buffer.length); | ||
})); | ||
ronag marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
As much as I dislike polymorphic signatures, I'd much prefer the approach of moving to an
options
object as an alternative here. That is:It accomplishes the same goal with a much cleaner API and implementation, without the ambiguity of which argument was meant to be passed in.
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.
@jasnell I think i see what you are saying. Just for my own clarification, we keep the current signature of
fs.read(fd, buffer, offset, lenght, position, callback)
, but then add another signature offs.read(fd, options, callback)
where theoptions
is the buffer, offset, length, position paramsSo then if a user only wanted to specify some of the params, they would have to use the "options" object signature.
Does that sound correct?
One thing that jumps out at me here, is when checking to see if that second parameter is the options object instead of the buffer object, i don't think we can use
typeof
since both would returnobject
. Or am i overthinking 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.
@jasnell Something else that i thought of was that the
fs.write
method is similar, in that it allows for optional parameters to be passed without the use of an options object.Perhaps we don't go the options object route? Or if we do, maybe that function and others(not sure if there are)should be updated also?
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 think moving that direction is ideal but doesn't have to be done all at once
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.
@jasnell @ronag does what i said here, #31402 (comment) makes sense?