Skip to content

Commit b6da55f

Browse files
lholmquistaddaleax
authored andcommitted
fs: 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 PR-URL: nodejs#31402 Reviewed-By: Robert Nagy <[email protected]>
1 parent 4f87b4f commit b6da55f

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

Diff for: doc/api/fs.md

+34
Original file line numberDiff line numberDiff line change
@@ -2764,6 +2764,29 @@ The callback is given the three arguments, `(err, bytesRead, buffer)`.
27642764
If this method is invoked as its [`util.promisify()`][]ed version, it returns
27652765
a `Promise` for an `Object` with `bytesRead` and `buffer` properties.
27662766

2767+
## `fs.read(fd, [options,] callback)`
2768+
<!-- YAML
2769+
added: REPLACEME
2770+
changes:
2771+
- version: REPLACEME
2772+
pr-url: https://github.com/nodejs/node/pull/31402
2773+
description: Options object can be passed in
2774+
to make Buffer, offset, length and position optional
2775+
-->
2776+
* `fd` {integer}
2777+
* `options` {Object}
2778+
* `buffer` {Buffer|TypedArray|DataView} **Default:** `Buffer.alloc(16384)`
2779+
* `offset` {integer} **Default:** `0`
2780+
* `length` {integer} **Default:** `buffer.length`
2781+
* `position` {integer} **Default:** `null`
2782+
* `callback` {Function}
2783+
* `err` {Error}
2784+
* `bytesRead` {integer}
2785+
* `buffer` {Buffer}
2786+
2787+
Similar to the above `fs.read` function, this version takes an optional `options` object.
2788+
If no `options` object is specified, it will default with the above values.
2789+
27672790
## `fs.readdir(path[, options], callback)`
27682791
<!-- YAML
27692792
added: v0.1.8
@@ -4377,6 +4400,17 @@ Following successful read, the `Promise` is resolved with an object with a
43774400
`bytesRead` property specifying the number of bytes read, and a `buffer`
43784401
property that is a reference to the passed in `buffer` argument.
43794402

4403+
#### `filehandle.read(options)`
4404+
<!-- YAML
4405+
added: REPLACEME
4406+
-->
4407+
* `options` {Object}
4408+
* `buffer` {Buffer|Uint8Array} **Default:** `Buffer.alloc(16384)`
4409+
* `offset` {integer} **Default:** `0`
4410+
* `length` {integer} **Default:** `buffer.length`
4411+
* `position` {integer} **Default:** `null`
4412+
* Returns: {Promise}
4413+
43804414
#### `filehandle.readFile(options)`
43814415
<!-- YAML
43824416
added: v10.0.0

Diff for: lib/fs.js

+26
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,34 @@ function openSync(path, flags, mode) {
462462
return result;
463463
}
464464

465+
// usage:
466+
// fs.read(fd, buffer, offset, length, position, callback);
467+
// OR
468+
// fs.read(fd, {}, callback)
465469
function read(fd, buffer, offset, length, position, callback) {
466470
validateInt32(fd, 'fd', 0);
471+
472+
if (arguments.length <= 3) {
473+
// Assume fs.read(fd, options, callback)
474+
let options = {};
475+
if (arguments.length < 3) {
476+
// This is fs.read(fd, callback)
477+
// buffer will be the callback
478+
callback = buffer;
479+
} else {
480+
// This is fs.read(fd, {}, callback)
481+
// buffer will be the options object
482+
// offset is the callback
483+
options = buffer;
484+
callback = offset;
485+
}
486+
487+
buffer = options.buffer || Buffer.alloc(16384);
488+
offset = options.offset || 0;
489+
length = options.length || buffer.length;
490+
position = options.position;
491+
}
492+
467493
validateBuffer(buffer);
468494
callback = maybeCallback(callback);
469495

Diff for: test/parallel/test-fs-read-optional-params.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fixtures = require('../common/fixtures');
5+
const fs = require('fs');
6+
const assert = require('assert');
7+
const filepath = fixtures.path('x.txt');
8+
const fd = fs.openSync(filepath, 'r');
9+
10+
const expected = Buffer.from('xyz\n');
11+
const defaultBufferAsync = Buffer.alloc(16384);
12+
const bufferAsOption = Buffer.allocUnsafe(expected.length);
13+
14+
// Test passing in an empty options object
15+
fs.read(fd, { position: 0 }, common.mustCall((err, bytesRead, buffer) => {
16+
assert.strictEqual(bytesRead, expected.length);
17+
assert.deepStrictEqual(defaultBufferAsync.length, buffer.length);
18+
}));
19+
20+
// Test not passing in any options object
21+
fs.read(fd, common.mustCall((err, bytesRead, buffer) => {
22+
assert.strictEqual(bytesRead, expected.length);
23+
assert.deepStrictEqual(defaultBufferAsync.length, buffer.length);
24+
}));
25+
26+
// Test passing in options
27+
fs.read(fd, {
28+
buffer: bufferAsOption,
29+
offset: 0,
30+
lenght: bufferAsOption.length,
31+
position: 0
32+
},
33+
common.mustCall((err, bytesRead, buffer) => {
34+
assert.strictEqual(bytesRead, expected.length);
35+
assert.deepStrictEqual(bufferAsOption.length, buffer.length);
36+
}));
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const fixtures = require('../common/fixtures');
5+
const fs = require('fs');
6+
const read = require('util').promisify(fs.read);
7+
const assert = require('assert');
8+
const filepath = fixtures.path('x.txt');
9+
const fd = fs.openSync(filepath, 'r');
10+
11+
const expected = Buffer.from('xyz\n');
12+
const defaultBufferAsync = Buffer.alloc(16384);
13+
14+
read(fd, {})
15+
.then(function({ bytesRead, buffer }) {
16+
assert.strictEqual(bytesRead, expected.length);
17+
assert.deepStrictEqual(defaultBufferAsync.length, buffer.length);
18+
})
19+
.then(common.mustCall());

0 commit comments

Comments
 (0)