From f2f8e63c01be0e3aaf6eda263fc17dc684c328fa Mon Sep 17 00:00:00 2001 From: pulkit-30 Date: Thu, 7 Dec 2023 22:16:46 +0530 Subject: [PATCH 1/3] fs: make offset, position & length args in fh.read() optional --- doc/api/fs.md | 5 ++++- lib/internal/fs/promises.js | 2 +- .../test-fs-promises-file-handle-read.js | 19 ++++++++++++++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 95afa01cbdec7b..5c79709c4c3c90 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -393,11 +393,14 @@ changes: * `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. + **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` diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 43d4ad7c0e9f63..0f1db035629c42 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -670,7 +670,7 @@ async function read(handle, bufferOrParams, offset, length, position) { validateInteger(offset, 'offset', 0); } - length |= 0; + length = length ?? buffer.byteLength - offset; if (length === 0) return { __proto__: null, bytesRead: length, buffer }; diff --git a/test/parallel/test-fs-promises-file-handle-read.js b/test/parallel/test-fs-promises-file-handle-read.js index 34a70c61d7e445..6d0bdfded1c651 100644 --- a/test/parallel/test-fs-promises-file-handle-read.js +++ b/test/parallel/test-fs-promises-file-handle-read.js @@ -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, 0); +} (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()); From 35ee64e2dcc9939305a01662828a1a562b0f4f8e Mon Sep 17 00:00:00 2001 From: pulkit-30 Date: Thu, 7 Dec 2023 22:17:44 +0530 Subject: [PATCH 2/3] fix test --- test/parallel/test-fs-promises-file-handle-read.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-fs-promises-file-handle-read.js b/test/parallel/test-fs-promises-file-handle-read.js index 6d0bdfded1c651..2e9534c3989906 100644 --- a/test/parallel/test-fs-promises-file-handle-read.js +++ b/test/parallel/test-fs-promises-file-handle-read.js @@ -109,7 +109,7 @@ async function validateReadWithNoOptions(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, 0); + assert.strictEqual(response.bytesRead, byte); } (async function() { From 1de21b8736677e4bd8ecbe353613cba871857509 Mon Sep 17 00:00:00 2001 From: pulkit-30 Date: Fri, 8 Dec 2023 00:38:07 +0530 Subject: [PATCH 3/3] fix suggestions --- lib/internal/fs/promises.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 0f1db035629c42..1e9573d0385fd0 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -670,7 +670,7 @@ async function read(handle, bufferOrParams, offset, length, position) { validateInteger(offset, 'offset', 0); } - length = length ?? buffer.byteLength - offset; + length ??= buffer.byteLength - offset; if (length === 0) return { __proto__: null, bytesRead: length, buffer };