From 44ef458d9cc060789bbb952f6e793c67f098b081 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 9 May 2018 11:48:30 -0500 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/20268 Reviewed-By: James M Snell Reviewed-By: Ron Korving Reviewed-By: Trivikram Kamat Reviewed-By: Ruben Bridgewater --- lib/internal/fs/promises.js | 5 +++-- .../test-fs-promises-readfile-empty.js | 19 +++++++++++++++++++ test/parallel/test-fs-readfile-empty.js | 4 ++++ 3 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-fs-promises-readfile-empty.js diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index a80a5283c52e3c..97ac4c6c495b3d 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -137,7 +137,7 @@ async function readFileHandle(filehandle, options) { } if (size === 0) - return Buffer.alloc(0); + return options.encoding ? '' : Buffer.alloc(0); if (size > kMaxLength) throw new ERR_FS_FILE_TOO_LARGE(size); @@ -459,11 +459,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)); } diff --git a/test/parallel/test-fs-promises-readfile-empty.js b/test/parallel/test-fs-promises-readfile-empty.js new file mode 100644 index 00000000000000..24c17655c62135 --- /dev/null +++ b/test/parallel/test-fs-promises-readfile-empty.js @@ -0,0 +1,19 @@ +'use strict'; +const common = require('../common'); + +const assert = require('assert'); +const { promises: fs } = require('fs'); +const fixtures = require('../common/fixtures'); + +const fn = fixtures.path('empty.txt'); + +common.crashOnUnhandledRejection(); + +fs.readFile(fn) + .then(assert.ok); + +fs.readFile(fn, 'utf8') + .then(assert.strictEqual.bind(this, '')); + +fs.readFile(fn, { encoding: 'utf8' }) + .then(assert.strictEqual.bind(this, '')); diff --git a/test/parallel/test-fs-readfile-empty.js b/test/parallel/test-fs-readfile-empty.js index 21f99fc6be24ba..36eccfb1162713 100644 --- a/test/parallel/test-fs-readfile-empty.js +++ b/test/parallel/test-fs-readfile-empty.js @@ -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'));