Skip to content

Commit c9cbbd1

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

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

lib/internal/fs/promises.js

Lines changed: 8 additions & 5 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
}
@@ -580,15 +581,17 @@ async function readFileHandle(filehandle, options) {
580581
result += decoder.end(buffer);
581582
return result;
582583
}
583-
584+
const writeBuffer = bytesRead !== buffer.length ?
585+
buffer.subarray(0, bytesRead) :
586+
buffer;
584587
if (encoding) {
585-
result += decoder.write(buffer);
588+
result += decoder.write(writeBuffer);
586589
} else if (size !== 0) {
587590
offset = totalRead;
588591
} else {
589592
buffers ??= [];
590593
// Unknown file size requires chunks.
591-
ArrayPrototypePush(buffers, buffer);
594+
ArrayPrototypePush(buffers, writeBuffer);
592595
buffer = Buffer.allocUnsafeSlow(kReadFileUnknownBufferLength);
593596
}
594597
}

test/parallel/test-fs-readfile-eof.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
if (common.isWindows || common.isAIX || common.isIBMi)
5+
common.skip(`No /dev/stdin on ${process.platform}.`);
6+
7+
const assert = require('assert');
8+
const fs = require('fs/promises');
9+
const childType = ['child-encoding', 'child-non-encoding'];
10+
11+
if (process.argv[2] === childType[0]) {
12+
fs.readFile('/dev/stdin', 'utf8').then((data) => {
13+
process.stdout.write(data);
14+
});
15+
return;
16+
} else if (process.argv[2] === childType[1]) {
17+
fs.readFile('/dev/stdin').then((data) => {
18+
process.stdout.write(data);
19+
});
20+
return;
21+
}
22+
23+
const data1 = 'Hello';
24+
const data2 = 'World';
25+
const expected = `${data1}\n${data2}\n`;
26+
27+
const exec = require('child_process').exec;
28+
const f = JSON.stringify(__filename);
29+
const node = JSON.stringify(process.execPath);
30+
31+
function test(child) {
32+
const cmd = `(echo ${data1}; sleep 0.5; echo ${data2}) | ${node} ${f} ${child}`;
33+
exec(cmd, common.mustSucceed((stdout, stderr) => {
34+
assert.strictEqual(
35+
stdout,
36+
expected,
37+
`expected to read(${child === childType[0] ? 'with' : 'without'} encoding): '${expected}' but got: '${stdout}'`);
38+
assert.strictEqual(
39+
stderr,
40+
'',
41+
`expected not to read anything from stderr but got: '${stderr}'`);
42+
}));
43+
}
44+
45+
test(childType[0]);
46+
test(childType[1]);

0 commit comments

Comments
 (0)