Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -560,7 +561,7 @@ async function readFileHandle(filehandle, options) {

if (bytesRead === 0 ||
totalRead === size ||
(bytesRead !== buffer.length && !chunkedRead)) {
(bytesRead !== buffer.length && !chunkedRead && !noSize)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about changing the chunkedRead above instead:

const chunkedRead = length > kReadFileBufferLength || size === 0;

That would automatically check for it and it's one boolean check in the loop less.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the premise here for chunkedRead is already knowing the size of the file, meaning size === 0 cannot imply the need for chunkedRead, have I misunderstand something?

const singleRead = bytesRead === totalRead;

const bytesToCheck = chunkedRead ? totalRead : bytesRead;
Expand All @@ -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);
}
Expand All @@ -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);
}
}
Expand Down
46 changes: 46 additions & 0 deletions test/parallel/test-fs-readfile-eof.js
Original file line number Diff line number Diff line change
@@ -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]);