GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fs: keep fs.promises.readFile read until EOF is reached · nodejs/node@f076e72

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/fs
    • promises.js
  • test/parallel
    • test-fs-readfile-eof.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/fs/promises.js‎

Copy file name to clipboardExpand all lines: lib/internal/fs/promises.js
+8-5Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@ async function readFileHandle(filehandle, options) {
538538
throw new ERR_FS_FILE_TOO_LARGE(size);
539539

540540
let totalRead = 0;
541+
const noSize = size === 0;
541542
let buffer = Buffer.allocUnsafeSlow(length);
542543
let result = '';
543544
let offset = 0;
@@ -560,7 +561,7 @@ async function readFileHandle(filehandle, options) {
560561

561562
if (bytesRead === 0 ||
562563
totalRead === size ||
563-
(bytesRead !== buffer.length && !chunkedRead)) {
564+
(bytesRead !== buffer.length && !chunkedRead && !noSize)) {
564565
const singleRead = bytesRead === totalRead;
565566

566567
const bytesToCheck = chunkedRead ? totalRead : bytesRead;
@@ -570,7 +571,7 @@ async function readFileHandle(filehandle, options) {
570571
}
571572

572573
if (!encoding) {
573-
if (size === 0 && !singleRead) {
574+
if (noSize && !singleRead) {
574575
ArrayPrototypePush(buffers, buffer);
575576
return Buffer.concat(buffers, totalRead);
576577
}
@@ -583,15 +584,17 @@ async function readFileHandle(filehandle, options) {
583584
result += decoder.end(buffer);
584585
return result;
585586
}
586-
587+
const readBuffer = bytesRead !== buffer.length ?
588+
buffer.subarray(0, bytesRead) :
589+
buffer;
587590
if (encoding) {
588-
result += decoder.write(buffer);
591+
result += decoder.write(readBuffer);
589592
} else if (size !== 0) {
590593
offset = totalRead;
591594
} else {
592595
buffers ??= [];
593596
// Unknown file size requires chunks.
594-
ArrayPrototypePush(buffers, buffer);
597+
ArrayPrototypePush(buffers, readBuffer);
595598
buffer = Buffer.allocUnsafeSlow(kReadFileUnknownBufferLength);
596599
}
597600
}
Collapse file

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

Copy file name to clipboard
+46Lines 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 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.