Skip to content

Commit 94f3eed

Browse files
lholmquistMylesBorins
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 <ronagy@icloud.com>
1 parent 35d0569 commit 94f3eed

4 files changed

Lines changed: 115 additions & 0 deletions

File tree

‎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
@@ -4362,6 +4385,17 @@ Following successful read, the `Promise` is resolved with an object with a
43624385
`bytesRead` property specifying the number of bytes read, and a `buffer`
43634386
property that is a reference to the passed in `buffer` argument.
43644387

4388+
#### `filehandle.read(options)`
4389+
<!-- YAML
4390+
added: REPLACEME
4391+
-->
4392+
*`options` {Object}
4393+
*`buffer` {Buffer|Uint8Array} **Default:**`Buffer.alloc(16384)`
4394+
*`offset` {integer} **Default:**`0`
4395+
*`length` {integer} **Default:**`buffer.length`
4396+
*`position` {integer} **Default:**`null`
4397+
* Returns: {Promise}
4398+
43654399
#### `filehandle.readFile(options)`
43664400
<!-- YAML
43674401
added: v10.0.0

‎lib/fs.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,34 @@ function openSync(path, flags, mode) {
463463
returnresult;
464464
}
465465

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
constfixtures=require('../common/fixtures');
5+
constfs=require('fs');
6+
constassert=require('assert');
7+
constfilepath=fixtures.path('x.txt');
8+
constfd=fs.openSync(filepath,'r');
9+
10+
constexpected=Buffer.from('xyz\n');
11+
constdefaultBufferAsync=Buffer.alloc(16384);
12+
constbufferAsOption=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+
constcommon=require('../common');
4+
constfixtures=require('../common/fixtures');
5+
constfs=require('fs');
6+
constread=require('util').promisify(fs.read);
7+
constassert=require('assert');
8+
constfilepath=fixtures.path('x.txt');
9+
constfd=fs.openSync(filepath,'r');
10+
11+
constexpected=Buffer.from('xyz\n');
12+
constdefaultBufferAsync=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)