-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fs: filehandle read now accepts object as argument #34180
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and we need to add a test case, like this
diff --git a/test/parallel/test-fs-promises-file-handle-read.js b/test/parallel/test-fs-promises-file-handle-read.js
index 5ba7fc63e3..c9fc38d489 100644
--- a/test/parallel/test-fs-promises-file-handle-read.js
+++ b/test/parallel/test-fs-promises-file-handle-read.js
@@ -13,7 +13,24 @@ const tmpdir = require('../common/tmpdir');
const assert = require('assert');
const tmpDir = tmpdir.path;
-tmpdir.refresh();
+let useConf = false;
+(async () => {
+ for (const value of [false, true]) {
+ tmpdir.refresh();
+ useConf = value;
+
+ await validateRead()
+ .then(validateEmptyRead)
+ .then(validateLargeRead)
+ .then(common.mustCall());
+ }
+})();
+
+function read(fileHandle, buffer, offset, length, position) {
+ return useConf ?
+ fileHandle.read({ buffer, offset, length, position }) :
+ fileHandle.read(buffer, offset, length, position);
+}
async function validateRead() {
const filePath = path.resolve(tmpDir, 'tmp-read-file.txt');
@@ -23,7 +40,7 @@ async function validateRead() {
const fd = fs.openSync(filePath, 'w+');
fs.writeSync(fd, buffer, 0, buffer.length);
fs.closeSync(fd);
- const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0);
+ const readAsyncHandle = await read(fileHandle, Buffer.alloc(11), 0, 11, 0);
assert.deepStrictEqual(buffer.length, readAsyncHandle.bytesRead);
assert.deepStrictEqual(buffer, readAsyncHandle.buffer);
@@ -38,7 +55,7 @@ async function validateEmptyRead() {
const fd = fs.openSync(filePath, 'w+');
fs.writeSync(fd, buffer, 0, buffer.length);
fs.closeSync(fd);
- const readAsyncHandle = await fileHandle.read(Buffer.alloc(11), 0, 11, 0);
+ const readAsyncHandle = await read(fileHandle, Buffer.alloc(11), 0, 11, 0);
assert.deepStrictEqual(buffer.length, readAsyncHandle.bytesRead);
await fileHandle.close();
@@ -51,12 +68,7 @@ async function validateLargeRead() {
const filePath = fixtures.path('x.txt');
const fileHandle = await open(filePath, 'r');
const pos = 0xffffffff + 1; // max-uint32 + 1
- const readHandle = await fileHandle.read(Buffer.alloc(1), 0, 1, pos);
+ const readHandle = await read(fileHandle, Buffer.alloc(1), 0, 1, pos);
assert.strictEqual(readHandle.bytesRead, 0);
}
-
-validateRead()
- .then(validateEmptyRead)
- .then(validateLargeRead)
- .then(common.mustCall());
lib/internal/fs/promises.js
Outdated
read(buffer, offset, length, position) { | ||
return read(this, buffer, offset, length, position); | ||
read(...args) { | ||
return read(this, ...args); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would leave this signature alone. It's subtle, and not likely to break anyone, but changing this does not a small impact on the API and it's just not really necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I agree, it does look more nicer and cleaner the way it originally was. I would rather manually parse parameters. Your thoughts @himself65, @mscdex?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another implementation is to parse the parameters in this class function.
Whatever, I’m 👎 with the previous handle which uses filter
lib/internal/fs/promises.js
Outdated
read(buffer, offset, length, position) { | ||
return read(this, buffer, offset, length, position); | ||
read(...args) { | ||
return read(this, ...args); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this to here maybe better?
if (arguments.length < 3) {
// Assume filehandle.read(options)
({
buffer = Buffer.alloc(16384),
offset = 0,
length = buffer.length,
position
} = buffer);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, I agree, looks good here, I just did this, waiting for build to finish. Also, I changed arguments.length < 3
to arguments.length < 2
because we don't get filehandle reference argument in this method.
@branisha Can you rebase this to resolve the git conflits please? |
This issue/PR was marked as stalled, it will be automatically closed in 30 days. If it should remain open, please leave a comment explaining why it should remain open. |
I'll try to find some time in the next couple of days to rebase this and update |
36b23c0
to
ea1cc73
Compare
@aduh95 @jasnell @himself65 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
still LGTM
PR-URL: #34180 Fixes: #34176 Refs: https://nodejs.org/api/fs.html#fs_filehandle_read_options Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
Landed in d05c271 |
PR-URL: #34180 Fixes: #34176 Refs: https://nodejs.org/api/fs.html#fs_filehandle_read_options Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
PR-URL: #34180 Fixes: #34176 Refs: https://nodejs.org/api/fs.html#fs_filehandle_read_options Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
PR-URL: #34180 Fixes: #34176 Refs: https://nodejs.org/api/fs.html#fs_filehandle_read_options Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
PR-URL: #34180 Fixes: #34176 Refs: https://nodejs.org/api/fs.html#fs_filehandle_read_options Reviewed-By: Zeyu Yang <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
Fixes: #34176
Refs: https://nodejs.org/api/fs.html#fs_filehandle_read_options
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes