Skip to content

Commit 7e6ce28

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

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

lib/internal/fs/promises.js

Lines changed: 5 additions & 3 deletions
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 {

lib/string_decoder.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ function normalizeEncoding(enc) {
8181
function StringDecoder(encoding) {
8282
this.encoding = normalizeEncoding(encoding);
8383
this[kNativeDecoder] = Buffer.alloc(kSize);
84+
85+
8486
this[kNativeDecoder][kEncodingField] = encodingsMap[this.encoding];
8587
}
8688

0 commit comments

Comments
 (0)