Skip to content

Commit a8fd01a

Browse files
pulkit-30richardlau
authored andcommitted
fs: make offset, position & length args in fh.read() optional
PR-URL: #51087 Fixes: #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

+8-5
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

+1-1
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

+18-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const assert = require('assert');
1414
const tmpDir = tmpdir.path;
1515

1616
async function read(fileHandle, buffer, offset, length, position, options) {
17-
return options.useConf ?
17+
return options?.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+
async function validateReadWithNoOptions(byte) {
100+
const buf = Buffer.alloc(byte);
101+
const filePath = fixtures.path('x.txt');
102+
const fileHandle = await open(filePath, 'r');
103+
let response = await fileHandle.read(buf);
104+
assert.strictEqual(response.bytesRead, byte);
105+
response = await read(fileHandle, buf, 0, undefined, 0);
106+
assert.strictEqual(response.bytesRead, byte);
107+
response = await read(fileHandle, buf, 0, null, 0);
108+
assert.strictEqual(response.bytesRead, byte);
109+
response = await read(fileHandle, buf, 0, undefined, 0, { useConf: true });
110+
assert.strictEqual(response.bytesRead, byte);
111+
response = await read(fileHandle, buf, 0, null, 0, { useConf: true });
112+
assert.strictEqual(response.bytesRead, byte);
113+
}
99114

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

0 commit comments

Comments
 (0)