Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 44ef458

Browse filesBrowse files
bdistinaddaleax
authored andcommitted
fs: ensure options.flag defaults to 'r' in readFile
When passing {} or { encoding: 'utf8' } as options to readFile, the flag is not defaulted to 'r' unlike normal fs. This fix makes fs.promises.readFile() act consistent with fs.readFile(). It also fixes another issue with fs.promises.readfile() where it returned a Buffer instead of an empty string when encoding is provided. PR-URL: #20268 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ron Korving <ron@ronkorving.nl> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 04af697 commit 44ef458
Copy full SHA for 44ef458

File tree

Expand file treeCollapse file tree

3 files changed

+26
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+26
-2
lines changed
Open diff view settings
Collapse file

‎lib/internal/fs/promises.js‎

Copy file name to clipboardExpand all lines: lib/internal/fs/promises.js
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ async function readFileHandle(filehandle, options) {
137137
}
138138

139139
if (size === 0)
140-
return Buffer.alloc(0);
140+
return options.encoding ? '' : Buffer.alloc(0);
141141

142142
if (size > kMaxLength)
143143
throw new ERR_FS_FILE_TOO_LARGE(size);
@@ -459,11 +459,12 @@ async function appendFile(path, data, options) {
459459

460460
async function readFile(path, options) {
461461
options = getOptions(options, { flag: 'r' });
462+
const flag = options.flag || 'r';
462463

463464
if (path instanceof FileHandle)
464465
return readFileHandle(path, options);
465466

466-
const fd = await open(path, options.flag, 0o666);
467+
const fd = await open(path, flag, 0o666);
467468
return readFileHandle(fd, options).finally(fd.close.bind(fd));
468469
}
469470

Collapse file
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
const assert = require('assert');
5+
const { promises: fs } = require('fs');
6+
const fixtures = require('../common/fixtures');
7+
8+
const fn = fixtures.path('empty.txt');
9+
10+
common.crashOnUnhandledRejection();
11+
12+
fs.readFile(fn)
13+
.then(assert.ok);
14+
15+
fs.readFile(fn, 'utf8')
16+
.then(assert.strictEqual.bind(this, ''));
17+
18+
fs.readFile(fn, { encoding: 'utf8' })
19+
.then(assert.strictEqual.bind(this, ''));
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-fs-readfile-empty.js
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,9 @@ fs.readFile(fn, 'utf8', function(err, data) {
3838
assert.strictEqual('', data);
3939
});
4040

41+
fs.readFile(fn, { encoding: 'utf8' }, function(err, data) {
42+
assert.strictEqual('', data);
43+
});
44+
4145
assert.ok(fs.readFileSync(fn));
4246
assert.strictEqual('', fs.readFileSync(fn, 'utf8'));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.