mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
fs: make offset, position & length args in fh.read() optional
PR-URL: https://github.com/nodejs/node/pull/51087 Fixes: https://github.com/nodejs/node/issues/47183 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Vinícius Lourenço Claro Cardoso <contact@viniciusl.com.br> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
1 parent
dc3d70c040
commit
a8fd01a5a2
3 files changed
+27
-7
No files matched your search
+8
-5
@@ -385,11 +385,14 @@ added: v10.0.0
|
||||
* `buffer` {Buffer|TypedArray|DataView} A buffer that will be filled with the
|
||||
file data read.
|
||||
* `offset` {integer} The location in the buffer at which to start filling.
|
||||
* `length` {integer} The number of bytes to read.
|
||||
* `position` {integer|null} The location where to begin reading data from the
|
||||
file. If `null`, data will be read from the current file position, and
|
||||
the position will be updated. If `position` is an integer, the current
|
||||
file position will remain unchanged.
|
||||
**Default:** `0`
|
||||
* `length` {integer} The number of bytes to read. **Default:**
|
||||
`buffer.byteLength - offset`
|
||||
* `position` {integer|bigint|null} The location where to begin reading data
|
||||
from the file. If `null` or `-1`, data will be read from the current file
|
||||
position, and the position will be updated. If `position` is a non-negative
|
||||
integer, the current file position will remain unchanged.
|
||||
**Default:**: `null`
|
||||
* Returns: {Promise} Fulfills upon success with an object with two properties:
|
||||
* `bytesRead` {integer} The number of bytes read
|
||||
* `buffer` {Buffer|TypedArray|DataView} A reference to the passed in `buffer`
|
||||
|
||||
@@ -642,7 +642,7 @@ async function read(handle, bufferOrParams, offset, length, position) {
|
||||
validateInteger(offset, 'offset', 0);
|
||||
}
|
||||
|
||||
length |= 0;
|
||||
length ??= buffer.byteLength - offset;
|
||||
|
||||
if (length === 0)
|
||||
return { __proto__: null, bytesRead: length, buffer };
|
||||
|
||||
@@ -14,7 +14,7 @@ const assert = require('assert');
|
||||
const tmpDir = tmpdir.path;
|
||||
|
||||
async function read(fileHandle, buffer, offset, length, position, options) {
|
||||
return options.useConf ?
|
||||
return options?.useConf ?
|
||||
fileHandle.read({ buffer, offset, length, position }) :
|
||||
fileHandle.read(buffer, offset, length, position);
|
||||
}
|
||||
@@ -96,6 +96,21 @@ async function validateReadLength(len) {
|
||||
assert.strictEqual(bytesRead, len);
|
||||
}
|
||||
|
||||
async function validateReadWithNoOptions(byte) {
|
||||
const buf = Buffer.alloc(byte);
|
||||
const filePath = fixtures.path('x.txt');
|
||||
const fileHandle = await open(filePath, 'r');
|
||||
let response = await fileHandle.read(buf);
|
||||
assert.strictEqual(response.bytesRead, byte);
|
||||
response = await read(fileHandle, buf, 0, undefined, 0);
|
||||
assert.strictEqual(response.bytesRead, byte);
|
||||
response = await read(fileHandle, buf, 0, null, 0);
|
||||
assert.strictEqual(response.bytesRead, byte);
|
||||
response = await read(fileHandle, buf, 0, undefined, 0, { useConf: true });
|
||||
assert.strictEqual(response.bytesRead, byte);
|
||||
response = await read(fileHandle, buf, 0, null, 0, { useConf: true });
|
||||
assert.strictEqual(response.bytesRead, byte);
|
||||
}
|
||||
|
||||
(async function() {
|
||||
tmpdir.refresh();
|
||||
@@ -109,4 +124,6 @@ async function validateReadLength(len) {
|
||||
await validateReadWithPositionZero();
|
||||
await validateReadLength(0);
|
||||
await validateReadLength(1);
|
||||
await validateReadWithNoOptions(0);
|
||||
await validateReadWithNoOptions(1);
|
||||
})().then(common.mustCall());
|
||||
Reference in new issue
Block a user