Skip to content

Commit f5a462f

Browse files
committed
fs/promises: ensure options.flag is defaulted 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 consistently with fs.readFile().
1 parent 5bb5d61 commit f5a462f

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

lib/internal/fs/promises.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,11 +463,12 @@ async function appendFile(path, data, options) {
463463

464464
async function readFile(path, options) {
465465
options = getOptions(options, { flag: 'r' });
466+
const flag = options.flag || 'r';
466467

467468
if (path instanceof FileHandle)
468469
return readFileHandle(path, options);
469470

470-
const fd = await open(path, options.flag, 0o666);
471+
const fd = await open(path, flag, 0o666);
471472
return readFileHandle(fd, options).finally(fd.close.bind(fd));
472473
}
473474

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
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+
fs.readFile(fn)
11+
.then(assert.ok);
12+
13+
fs.readFile(fn, 'utf8')
14+
.then(assert.strictEqual.bind(this, ''));
15+
16+
fs.readFile(fn, { encoding: 'utf8' })
17+
.then(assert.strictEqual.bind(this, ''));

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

Lines 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)