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: make read parameters optional #31237

Closed
ronag opened this issue Jan 7, 2020 · 8 comments
Closed

fs: make read parameters optional #31237

ronag opened this issue Jan 7, 2020 · 8 comments

Comments

@ronag
Copy link
Member

ronag commented Jan 7, 2020

At least in the promisifed version the parameters after fd should be optional, i.e:

function read(fd, buffer = new Buffer(16384), offset = 0, length = buffer.length, position = null);

The non promisified is a bitter more tricky but should be doable as well.

@ronag
Copy link
Member Author

ronag commented Jan 7, 2020

Good first issue?

@lholmquist
Copy link
Contributor

@ronag I'd be willing to help out on this one. Just might need a little guidance to get started :)

@lholmquist
Copy link
Contributor

I've played around with this a little and i think fd and buffer are needed to keep the implementation simpler, but the other values can be optional. Similar to the fs.write and fs.writev

Link to my first pass commit: lholmquist@a012cdb

In my first round of testing, i create the function signature like this:

function read(fd, buffer = Buffer.alloc(16384), offset = 0, length = buffer.length, position = null, callback)

If i run this with an example like

fs.read(fd, /*buffer, offset, length, position,*/ function cb(err, bytesRead, buffer) {
    console.log(buffer.toString('utf8'));
  })

I'll get an error because at the moment, the callback is going into the buffer value. It seems like the implementation might be simplier and less error prone if the only optional values were offset, length and position. I'm going to give that a try

@ronag
Copy link
Member Author

ronag commented Jan 16, 2020

You can't use default parameters here since the last parameter is not optional. Instead you might have to do something like:

read(...args) {
  const callback = args.pop();
  const [ buffer = Buffer.alloc(16384), offset = 0, length = buffer.length, position = null ] = args;
}

Will need to check the performance implications as well.

@lholmquist
Copy link
Contributor

I have something working here: b139697 still using the default params(except for the callback). It is doing a little more typeof checking than i think is needed

I'll give the spread stuff a shot and see how that goes

@lholmquist
Copy link
Contributor

i'm not a performance expert, so i don't if this is an issue, but it seem like creating that an extra default buffer if we didn't need to might affect something? Maybe not making buffer optional might be a good thing? 🤷‍♂️

I'll send a PR in the next day or so for more discussion

Thanks @ronag for the help

@ronag
Copy link
Member Author

ronag commented Jan 16, 2020

but it seem like creating that an extra default buffer if we didn't need to might affect something

It would only be created if no buffer is provided. Default parameters are evaluated lazylily as far as I know. Might be worth to confirm though.

@lholmquist
Copy link
Contributor

Default parameters are evaluated lazylily as far as I know. Might be worth to confirm though.

👍

lholmquist added a commit to lholmquist/node that referenced this issue Jan 17, 2020
This makes all the parameters of the `fs.read` function, except
for `fd` and the callback(when not using as a promise) optional.

They will default to sensible defaults

fixes nodejs#31237
MylesBorins pushed a commit that referenced this issue Mar 11, 2020
This makes all the parameters of the `fs.read` function, except
for `fd` and the callback(when not using as a promise) optional.

They will default to sensible defaults.

Fixes: #31237

PR-URL: #31402
Reviewed-By: Robert Nagy <ronagy@icloud.com>
targos pushed a commit to targos/node that referenced this issue Apr 25, 2020
This makes all the parameters of the `fs.read` function, except
for `fd` and the callback(when not using as a promise) optional.

They will default to sensible defaults.

Fixes: nodejs#31237

PR-URL: nodejs#31402
Reviewed-By: Robert Nagy <ronagy@icloud.com>
targos pushed a commit that referenced this issue Apr 28, 2020
This makes all the parameters of the `fs.read` function, except
for `fd` and the callback(when not using as a promise) optional.

They will default to sensible defaults.

Fixes: #31237

PR-URL: #31402
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants