Skip to content
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

Closed
wants to merge 2 commits into from

Conversation

branisha
Copy link
Contributor

@branisha branisha commented Jul 3, 2020

Fixes: #34176
Refs: https://nodejs.org/api/fs.html#fs_filehandle_read_options

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added the fs Issues and PRs related to the fs subsystem / file system. label Jul 3, 2020
lib/internal/fs/promises.js Outdated Show resolved Hide resolved
Copy link
Member

@himself65 himself65 left a 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());

read(buffer, offset, length, position) {
return read(this, buffer, offset, length, position);
read(...args) {
return read(this, ...args);
}
Copy link
Member

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.

Copy link
Contributor Author

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?

Copy link
Member

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

@branisha branisha requested review from mscdex and himself65 July 7, 2020 06:31
read(buffer, offset, length, position) {
return read(this, buffer, offset, length, position);
read(...args) {
return read(this, ...args);
Copy link
Member

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);
  }

Copy link
Contributor Author

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.

@aduh95
Copy link
Contributor

aduh95 commented Nov 8, 2020

@branisha Can you rebase this to resolve the git conflits please?

@aduh95 aduh95 added the stalled Issues and PRs that are stalled. label Nov 8, 2020
@github-actions
Copy link
Contributor

github-actions bot commented Nov 8, 2020

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.

@branisha
Copy link
Contributor Author

branisha commented Nov 8, 2020

@branisha Can you rebase this to resolve the git conflits please?

I'll try to find some time in the next couple of days to rebase this and update

@branisha
Copy link
Contributor Author

@aduh95 @jasnell @himself65
Since I last worked on this, quite has changed in FS operations. I've tried to make it sensible, and I hope it's up to standard. If it's not, please let me know, and lay some wrath on me 😅.

lib/internal/fs/promises.js Outdated Show resolved Hide resolved
Copy link
Member

@himself65 himself65 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still LGTM

@aduh95 aduh95 added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Nov 17, 2020
@aduh95 aduh95 added the request-ci Add this label to start a Jenkins CI on a PR. label Nov 17, 2020
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Nov 17, 2020
@nodejs-github-bot
Copy link
Collaborator

aduh95 pushed a commit that referenced this pull request Nov 18, 2020
PR-URL: #34180
Fixes: #34176
Refs: https://nodejs.org/api/fs.html#fs_filehandle_read_options
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
@aduh95
Copy link
Contributor

aduh95 commented Nov 18, 2020

Landed in d05c271

@aduh95 aduh95 closed this Nov 18, 2020
codebytere pushed a commit that referenced this pull request Nov 22, 2020
PR-URL: #34180
Fixes: #34176
Refs: https://nodejs.org/api/fs.html#fs_filehandle_read_options
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
@codebytere codebytere mentioned this pull request Nov 22, 2020
BethGriggs pushed a commit that referenced this pull request Dec 10, 2020
PR-URL: #34180
Fixes: #34176
Refs: https://nodejs.org/api/fs.html#fs_filehandle_read_options
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
BethGriggs pushed a commit that referenced this pull request Dec 10, 2020
PR-URL: #34180
Fixes: #34176
Refs: https://nodejs.org/api/fs.html#fs_filehandle_read_options
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
@BethGriggs BethGriggs mentioned this pull request Dec 10, 2020
BethGriggs pushed a commit that referenced this pull request Dec 15, 2020
PR-URL: #34180
Fixes: #34176
Refs: https://nodejs.org/api/fs.html#fs_filehandle_read_options
Reviewed-By: Zeyu Yang <himself65@outlook.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. fs Issues and PRs related to the fs subsystem / file system. stalled Issues and PRs that are stalled.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fs.promises FileHandle.read() not working with object argument as stated in docs.
6 participants