fs: keep fs.promises.readFile read until EOF is reached

PR-URL: https://github.com/nodejs/node/pull/52178
Fixes: https://github.com/nodejs/node/issues/52155
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
Zhenwei Jin authored and Michaël Zasso committed 2024-05-11 18:55:56 +02:00
1 parent 2c7d7caa8a
commit f076e721cb
2 files changed
+54 -5

No files matched your search

+8 -5
View File
@@ -538,6 +538,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;
@@ -560,7 +561,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;
@@ -570,7 +571,7 @@ async function readFileHandle(filehandle, options) {
}
if (!encoding) {
if (size === 0 && !singleRead) {
if (noSize && !singleRead) {
ArrayPrototypePush(buffers, buffer);
return Buffer.concat(buffers, totalRead);
}
@@ -583,15 +584,17 @@ async function readFileHandle(filehandle, options) {
result += decoder.end(buffer);
return result;
}
const readBuffer = bytesRead !== buffer.length ?
buffer.subarray(0, bytesRead) :
buffer;
if (encoding) {
result += decoder.write(buffer);
result += decoder.write(readBuffer);
} else if (size !== 0) {
offset = totalRead;
} else {
buffers ??= [];
// Unknown file size requires chunks.
ArrayPrototypePush(buffers, buffer);
ArrayPrototypePush(buffers, readBuffer);
buffer = Buffer.allocUnsafeSlow(kReadFileUnknownBufferLength);
}
}
+46
View File
@@ -0,0 +1,46 @@
'use strict';
const common = require('../common');
if (common.isWindows || common.isAIX || common.isIBMi)
common.skip(`No /dev/stdin on ${process.platform}.`);
const assert = require('assert');
const fs = require('fs/promises');
const childType = ['child-encoding', 'child-non-encoding'];
if (process.argv[2] === childType[0]) {
fs.readFile('/dev/stdin', 'utf8').then((data) => {
process.stdout.write(data);
});
return;
} else if (process.argv[2] === childType[1]) {
fs.readFile('/dev/stdin').then((data) => {
process.stdout.write(data);
});
return;
}
const data1 = 'Hello';
const data2 = 'World';
const expected = `${data1}\n${data2}\n`;
const exec = require('child_process').exec;
const f = JSON.stringify(__filename);
const node = JSON.stringify(process.execPath);
function test(child) {
const cmd = `(echo ${data1}; sleep 0.5; echo ${data2}) | ${node} ${f} ${child}`;
exec(cmd, common.mustSucceed((stdout, stderr) => {
assert.strictEqual(
stdout,
expected,
`expected to read(${child === childType[0] ? 'with' : 'without'} encoding): '${expected}' but got: '${stdout}'`);
assert.strictEqual(
stderr,
'',
`expected not to read anything from stderr but got: '${stderr}'`);
}));
}
test(childType[0]);
test(childType[1]);