Skip to content

Commit 7d38c2e

Browse files
kylo5abymarco-ippolito
authored andcommittedJun 17, 2024
fs: keep fs.promises.readFile read until EOF is reached
PR-URL: #52178 Fixes: #52155 Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent 184cfe5 commit 7d38c2e

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed
 

‎lib/internal/fs/promises.js

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

538538
let totalRead = 0;
539+
const noSize = size === 0;
539540
let buffer = Buffer.allocUnsafeSlow(length);
540541
let result = '';
541542
let offset = 0;
@@ -558,7 +559,7 @@ async function readFileHandle(filehandle, options) {
558559

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

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

570571
if (!encoding) {
571-
if (size === 0 && !singleRead) {
572+
if (noSize && !singleRead) {
572573
ArrayPrototypePush(buffers, buffer);
573574
return Buffer.concat(buffers, totalRead);
574575
}
@@ -581,15 +582,17 @@ async function readFileHandle(filehandle, options) {
581582
result += decoder.end(buffer);
582583
return result;
583584
}
584-
585+
const readBuffer = bytesRead !== buffer.length ?
586+
buffer.subarray(0, bytesRead) :
587+
buffer;
585588
if (encoding) {
586-
result += decoder.write(buffer);
589+
result += decoder.write(readBuffer);
587590
} else if (size !== 0) {
588591
offset = totalRead;
589592
} else {
590593
buffers ??= [];
591594
// Unknown file size requires chunks.
592-
ArrayPrototypePush(buffers, buffer);
595+
ArrayPrototypePush(buffers, readBuffer);
593596
buffer = Buffer.allocUnsafeSlow(kReadFileUnknownBufferLength);
594597
}
595598
}

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

+46
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)
Please sign in to comment.