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 7e6ce28
Show file tree
Hide file tree
Showing 2 changed files with 7 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
2 changes: 2 additions & 0 deletions lib/string_decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ function normalizeEncoding(enc) {
function StringDecoder(encoding) {
this.encoding = normalizeEncoding(encoding);
this[kNativeDecoder] = Buffer.alloc(kSize);


this[kNativeDecoder][kEncodingField] = encodingsMap[this.encoding];
}

Expand Down

0 comments on commit 7e6ce28

Please sign in to comment.