Skip to content

Commit 95f03ef

Browse files
committed
feat: make fs.read params optional
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
1 parent b4f5c9e commit 95f03ef

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

doc/api/fs.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -2716,10 +2716,14 @@ Returns an integer representing the file descriptor.
27162716
For detailed information, see the documentation of the asynchronous version of
27172717
this API: [`fs.open()`][].
27182718

2719-
## `fs.read(fd, buffer, offset, length, position, callback)`
2719+
## `fs.read(fd, [buffer, [offset[, length[, position]]]], callback)`
27202720
<!-- YAML
27212721
added: v0.0.2
27222722
changes:
2723+
- version: REPLACEME
2724+
pr-url: REPLACEME
2725+
description: Buffer, offset, length and position parameters
2726+
are now optional
27232727
- version: v10.10.0
27242728
pr-url: https://github.com/nodejs/node/pull/22150
27252729
description: The `buffer` parameter can now be any `TypedArray`, or a

lib/fs.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,20 @@ function openSync(path, flags, mode) {
454454
return result;
455455
}
456456

457-
function read(fd, buffer, offset, length, position, callback) {
457+
// usage:
458+
// fs.read(fd, [buffer, offset[, length[, position]]], callback);
459+
function read(fd, ...args) {
460+
let callback = args.pop();
461+
// Not really thrilled with this, but done to satisfy the linter
462+
// Maybe adding a linter exception here might be better?
463+
const [buffer = Buffer.alloc(16384)] = args;
464+
args.shift();
465+
466+
let [
467+
offset = 0,
468+
length = buffer.length,
469+
position = null ] = args;
470+
458471
validateInt32(fd, 'fd', 0);
459472
validateBuffer(buffer);
460473
callback = maybeCallback(callback);

test/parallel/test-fs-read.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ assert.throws(() => new fs.Dir(), {
7575
assert.throws(
7676
() => fs.read(fd, Buffer.alloc(1), 0, 1, 0),
7777
{
78-
message: 'Callback must be a function. Received undefined',
78+
message: 'Callback must be a function. Received 0',
7979
code: 'ERR_INVALID_CALLBACK',
8080
}
8181
);

0 commit comments

Comments
 (0)