-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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: keep fs.promises.readFile read until EOF is reached #52178
Conversation
7e6ce28
to
212c845
Compare
The PR looks great, can you add a test just to ensure the issue is fixed? |
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.
This is looking good. I guess we can improve the conditions a tad with my suggestions and I wonder if we have to do the same for the non-encoding situation. Is that also impacted? Let's add regression tests for both to be safe.
@@ -557,7 +558,7 @@ async function readFileHandle(filehandle, options) { | |||
|
|||
if (bytesRead === 0 || | |||
totalRead === size || | |||
(bytesRead !== buffer.length && !chunkedRead)) { | |||
(bytesRead !== buffer.length && !chunkedRead && !noSize)) { |
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.
What about changing the chunkedRead above instead:
const chunkedRead = length > kReadFileBufferLength || size === 0;
That would automatically check for it and it's one boolean check in the loop less.
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 believe the premise here for chunkedRead
is already knowing the size
of the file, meaning size === 0
cannot imply the need for chunkedRead
, have I misunderstand something?
lib/internal/fs/promises.js
Outdated
result += decoder.write(noSize && bytesRead !== kReadFileUnknownBufferLength ? | ||
buffer.subarray(0, bytesRead) : 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.
result += decoder.write(noSize && bytesRead !== kReadFileUnknownBufferLength ? | |
buffer.subarray(0, bytesRead) : buffer); | |
const writeBuffer = bytesRead !== buffer.length ? | |
buffer.subarray(0, bytesRead) : | |
buffer; | |
result += decoder.write(writeBuffer); |
|
Thanks for the reminder :) |
could you take a look? @legendecas |
lib/internal/fs/promises.js
Outdated
@@ -580,15 +581,17 @@ async function readFileHandle(filehandle, options) { | |||
result += decoder.end(buffer); | |||
return result; | |||
} | |||
|
|||
const writeBuffer = bytesRead !== buffer.length ? |
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.
If I understand correctly, this is not a buffer for a "write" operation. Instead, it is a result of "read".
const writeBuffer = bytesRead !== buffer.length ? | |
const readBuffer = bytesRead !== buffer.length ? |
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.
Resolved. Thanks!
Landed in ff7910b |
Can this be backported to v20? |
PR-URL: nodejs#52178 Fixes: nodejs#52155 Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
PR-URL: nodejs#52178 Fixes: nodejs#52155 Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Fixes: #52155