Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
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().
  • Loading branch information
bdistin committed May 9, 2018
commit f5a462f4a8ba223af1e924577617431bae902c86
3 changes: 2 additions & 1 deletion lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,11 +463,12 @@ async function appendFile(path, data, options) {

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

if (path instanceof FileHandle)
return readFileHandle(path, options);

const fd = await open(path, options.flag, 0o666);
const fd = await open(path, flag, 0o666);
return readFileHandle(fd, options).finally(fd.close.bind(fd));
}

Expand Down
17 changes: 17 additions & 0 deletions test/parallel/test-fs-promises-readfile-empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';
require('../common');

const assert = require('assert');
const { promises: fs } = require('fs');
const fixtures = require('../common/fixtures');

const fn = fixtures.path('empty.txt');

This comment was marked as resolved.


fs.readFile(fn)
.then(assert.ok);

fs.readFile(fn, 'utf8')
.then(assert.strictEqual.bind(this, ''));

This comment was marked as resolved.


fs.readFile(fn, { encoding: 'utf8' })
.then(assert.strictEqual.bind(this, ''));
4 changes: 4 additions & 0 deletions test/parallel/test-fs-readfile-empty.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,9 @@ fs.readFile(fn, 'utf8', function(err, data) {
assert.strictEqual('', data);
});

fs.readFile(fn, { encoding: 'utf8' }, function(err, data) {
assert.strictEqual('', data);
});

assert.ok(fs.readFileSync(fn));
assert.strictEqual('', fs.readFileSync(fn, 'utf8'));