Skip to content

Commit

Permalink
feat: make fs.read params optional
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lholmquist committed Jan 17, 2020
1 parent b4f5c9e commit 95f03ef
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
6 changes: 5 additions & 1 deletion doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2716,10 +2716,14 @@ Returns an integer representing the file descriptor.
For detailed information, see the documentation of the asynchronous version of
this API: [`fs.open()`][].

## `fs.read(fd, buffer, offset, length, position, callback)`
## `fs.read(fd, [buffer, [offset[, length[, position]]]], callback)`
<!-- YAML
added: v0.0.2
changes:
- version: REPLACEME
pr-url: REPLACEME
description: Buffer, offset, length and position parameters
are now optional
- version: v10.10.0
pr-url: https://github.com/nodejs/node/pull/22150
description: The `buffer` parameter can now be any `TypedArray`, or a
Expand Down
15 changes: 14 additions & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,20 @@ function openSync(path, flags, mode) {
return result;
}

function read(fd, buffer, offset, length, position, callback) {
// usage:
// fs.read(fd, [buffer, offset[, length[, position]]], callback);
function read(fd, ...args) {
let callback = args.pop();
// Not really thrilled with this, but done to satisfy the linter
// Maybe adding a linter exception here might be better?
const [buffer = Buffer.alloc(16384)] = args;
args.shift();

let [
offset = 0,
length = buffer.length,
position = null ] = args;

validateInt32(fd, 'fd', 0);
validateBuffer(buffer);
callback = maybeCallback(callback);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ assert.throws(() => new fs.Dir(), {
assert.throws(
() => fs.read(fd, Buffer.alloc(1), 0, 1, 0),
{
message: 'Callback must be a function. Received undefined',
message: 'Callback must be a function. Received 0',
code: 'ERR_INVALID_CALLBACK',
}
);
Expand Down

0 comments on commit 95f03ef

Please sign in to comment.