Skip to content

Commit 16a913f

Browse files
lholmquisttargos
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: #31237 PR-URL: #31402 Reviewed-By: Robert Nagy <[email protected]>
1 parent 40c5d58 commit 16a913f

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

doc/api/fs.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2775,6 +2775,29 @@ The callback is given the three arguments, `(err, bytesRead, buffer)`.
27752775
If this method is invoked as its [`util.promisify()`][]ed version, it returns
27762776
a `Promise` for an `Object` with `bytesRead` and `buffer` properties.
27772777

2778+
## `fs.read(fd, [options,] callback)`
2779+
<!-- YAML
2780+
added: REPLACEME
2781+
changes:
2782+
- version: REPLACEME
2783+
pr-url: https://github.com/nodejs/node/pull/31402
2784+
description: Options object can be passed in
2785+
to make Buffer, offset, length and position optional
2786+
-->
2787+
* `fd` {integer}
2788+
* `options` {Object}
2789+
* `buffer` {Buffer|TypedArray|DataView} **Default:** `Buffer.alloc(16384)`
2790+
* `offset` {integer} **Default:** `0`
2791+
* `length` {integer} **Default:** `buffer.length`
2792+
* `position` {integer} **Default:** `null`
2793+
* `callback` {Function}
2794+
* `err` {Error}
2795+
* `bytesRead` {integer}
2796+
* `buffer` {Buffer}
2797+
2798+
Similar to the above `fs.read` function, this version takes an optional `options` object.
2799+
If no `options` object is specified, it will default with the above values.
2800+
27782801
## `fs.readdir(path[, options], callback)`
27792802
<!-- YAML
27802803
added: v0.1.8
@@ -4365,6 +4388,17 @@ Following successful read, the `Promise` is resolved with an object with a
43654388
`bytesRead` property specifying the number of bytes read, and a `buffer`
43664389
property that is a reference to the passed in `buffer` argument.
43674390

4391+
#### `filehandle.read(options)`
4392+
<!-- YAML
4393+
added: REPLACEME
4394+
-->
4395+
* `options` {Object}
4396+
* `buffer` {Buffer|Uint8Array} **Default:** `Buffer.alloc(16384)`
4397+
* `offset` {integer} **Default:** `0`
4398+
* `length` {integer} **Default:** `buffer.length`
4399+
* `position` {integer} **Default:** `null`
4400+
* Returns: {Promise}
4401+
43684402
#### `filehandle.readFile(options)`
43694403
<!-- YAML
43704404
added: v10.0.0

lib/fs.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,8 +459,34 @@ function openSync(path, flags, mode) {
459459
return result;
460460
}
461461

462+
// usage:
463+
// fs.read(fd, buffer, offset, length, position, callback);
464+
// OR
465+
// fs.read(fd, {}, callback)
462466
function read(fd, buffer, offset, length, position, callback) {
463467
validateInt32(fd, 'fd', 0);
468+
469+
if (arguments.length <= 3) {
470+
// Assume fs.read(fd, options, callback)
471+
let options = {};
472+
if (arguments.length < 3) {
473+
// This is fs.read(fd, callback)
474+
// buffer will be the callback
475+
callback = buffer;
476+
} else {
477+
// This is fs.read(fd, {}, callback)
478+
// buffer will be the options object
479+
// offset is the callback
480+
options = buffer;
481+
callback = offset;
482+
}
483+
484+
buffer = options.buffer || Buffer.alloc(16384);
485+
offset = options.offset || 0;
486+
length = options.length || buffer.length;
487+
position = options.position;
488+
}
489+
464490
validateBuffer(buffer);
465491
callback = maybeCallback(callback);
466492

Lines changed: 36 additions & 0 deletions
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+
}));
Lines changed: 19 additions & 0 deletions
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)