Skip to content

Commit 212c845

Browse files
committed
fs: keep fs.promises.readFile read until EOF is reached
1 parent 6dd1c75 commit 212c845

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

lib/internal/fs/promises.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ async function readFileHandle(filehandle, options) {
535535
throw new ERR_FS_FILE_TOO_LARGE(size);
536536

537537
let totalRead = 0;
538+
const noSize = size === 0;
538539
let buffer = Buffer.allocUnsafeSlow(length);
539540
let result = '';
540541
let offset = 0;
@@ -557,7 +558,7 @@ async function readFileHandle(filehandle, options) {
557558

558559
if (bytesRead === 0 ||
559560
totalRead === size ||
560-
(bytesRead !== buffer.length && !chunkedRead)) {
561+
(bytesRead !== buffer.length && !chunkedRead && !noSize)) {
561562
const singleRead = bytesRead === totalRead;
562563

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

569570
if (!encoding) {
570-
if (size === 0 && !singleRead) {
571+
if (noSize && !singleRead) {
571572
ArrayPrototypePush(buffers, buffer);
572573
return Buffer.concat(buffers, totalRead);
573574
}
@@ -582,7 +583,8 @@ async function readFileHandle(filehandle, options) {
582583
}
583584

584585
if (encoding) {
585-
result += decoder.write(buffer);
586+
result += decoder.write(noSize && bytesRead !== kReadFileUnknownBufferLength ?
587+
buffer.subarray(0, bytesRead) : buffer);
586588
} else if (size !== 0) {
587589
offset = totalRead;
588590
} else {

0 commit comments

Comments
 (0)