Skip to content

Commit a8fd01a

Browse files
pulkit-30richardlau
authored andcommitted
fs: make offset, position & length args in fh.read() optional
PR-URL: #51087Fixes: #47183 Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Vinícius Lourenço Claro Cardoso <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent dc3d70c commit a8fd01a

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

‎doc/api/fs.md‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,14 @@ added: v10.0.0
385385
* `buffer`{Buffer|TypedArray|DataView} A buffer that will be filled with the
386386
file data read.
387387
* `offset`{integer} The location in the buffer at which to start filling.
388-
* `length`{integer} The number of bytes to read.
389-
* `position`{integer|null} The location where to begin reading data from the
390-
file. If `null`, data will be read from the current file position, and
391-
the position will be updated. If `position` is an integer, the current
392-
file position will remain unchanged.
388+
**Default:** `0`
389+
* `length`{integer} The number of bytes to read. **Default:**
390+
`buffer.byteLength- offset`
391+
* `position`{integer|bigint|null} The location where to begin reading data
392+
from the file. If `null` or `-1`, data will be read from the current file
393+
position, and the position will be updated. If `position` is a non-negative
394+
integer, the current file position will remain unchanged.
395+
**Default:**: `null`
393396
* Returns:{Promise} Fulfills upon success with an object with two properties:
394397
* `bytesRead`{integer} The number of bytes read
395398
* `buffer`{Buffer|TypedArray|DataView} A reference to the passed in `buffer`

‎lib/internal/fs/promises.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ async function read(handle, bufferOrParams, offset, length, position){
642642
validateInteger(offset,'offset',0);
643643
}
644644

645-
length|=0;
645+
length??=buffer.byteLength-offset;
646646

647647
if(length===0)
648648
return{__proto__: null,bytesRead: length, buffer };

‎test/parallel/test-fs-promises-file-handle-read.js‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const assert = require('assert');
1414
consttmpDir=tmpdir.path;
1515

1616
asyncfunctionread(fileHandle,buffer,offset,length,position,options){
17-
returnoptions.useConf ?
17+
returnoptions?.useConf ?
1818
fileHandle.read({ buffer, offset, length, position }) :
1919
fileHandle.read(buffer,offset,length,position);
2020
}
@@ -96,6 +96,21 @@ async function validateReadLength(len){
9696
assert.strictEqual(bytesRead,len);
9797
}
9898

99+
asyncfunctionvalidateReadWithNoOptions(byte){
100+
constbuf=Buffer.alloc(byte);
101+
constfilePath=fixtures.path('x.txt');
102+
constfileHandle=awaitopen(filePath,'r');
103+
letresponse=awaitfileHandle.read(buf);
104+
assert.strictEqual(response.bytesRead,byte);
105+
response=awaitread(fileHandle,buf,0,undefined,0);
106+
assert.strictEqual(response.bytesRead,byte);
107+
response=awaitread(fileHandle,buf,0,null,0);
108+
assert.strictEqual(response.bytesRead,byte);
109+
response=awaitread(fileHandle,buf,0,undefined,0,{useConf: true});
110+
assert.strictEqual(response.bytesRead,byte);
111+
response=awaitread(fileHandle,buf,0,null,0,{useConf: true});
112+
assert.strictEqual(response.bytesRead,byte);
113+
}
99114

100115
(asyncfunction(){
101116
tmpdir.refresh();
@@ -109,4 +124,6 @@ async function validateReadLength(len){
109124
awaitvalidateReadWithPositionZero();
110125
awaitvalidateReadLength(0);
111126
awaitvalidateReadLength(1);
127+
awaitvalidateReadWithNoOptions(0);
128+
awaitvalidateReadWithNoOptions(1);
112129
})().then(common.mustCall());

0 commit comments

Comments
(0)