Skip to content

Commit

Permalink
fs: keep fs.promises.readFile read until EOF is reached
Browse files Browse the repository at this point in the history
  • Loading branch information
kylo5aby committed Mar 22, 2024
1 parent 6dd1c75 commit 212c845
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,7 @@ async function readFileHandle(filehandle, options) {
throw new ERR_FS_FILE_TOO_LARGE(size);

let totalRead = 0;
const noSize = size === 0;
let buffer = Buffer.allocUnsafeSlow(length);
let result = '';
let offset = 0;
Expand All @@ -557,7 +558,7 @@ async function readFileHandle(filehandle, options) {

if (bytesRead === 0 ||
totalRead === size ||
(bytesRead !== buffer.length && !chunkedRead)) {
(bytesRead !== buffer.length && !chunkedRead && !noSize)) {
const singleRead = bytesRead === totalRead;

const bytesToCheck = chunkedRead ? totalRead : bytesRead;
Expand All @@ -567,7 +568,7 @@ async function readFileHandle(filehandle, options) {
}

if (!encoding) {
if (size === 0 && !singleRead) {
if (noSize && !singleRead) {
ArrayPrototypePush(buffers, buffer);
return Buffer.concat(buffers, totalRead);
}
Expand All @@ -582,7 +583,8 @@ async function readFileHandle(filehandle, options) {
}

if (encoding) {
result += decoder.write(buffer);
result += decoder.write(noSize && bytesRead !== kReadFileUnknownBufferLength ?
buffer.subarray(0, bytesRead) : buffer);
} else if (size !== 0) {
offset = totalRead;
} else {
Expand Down

0 comments on commit 212c845

Please sign in to comment.