From f5a462f4a8ba223af1e924577617431bae902c86 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 9 May 2018 11:48:30 -0500 Subject: [PATCH 1/4] 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(). --- lib/internal/fs/promises.js | 3 ++- .../parallel/test-fs-promises-readfile-empty.js | 17 +++++++++++++++++ test/parallel/test-fs-readfile-empty.js | 4 ++++ 3 files changed, 23 insertions(+), 1 deletion(-) 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 0857fd2ffff506..262aed8f31c36f 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -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)); } 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..ef15a2681123c6 --- /dev/null +++ b/test/parallel/test-fs-promises-readfile-empty.js @@ -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'); + +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')); From 26bce853b4a2ca643b1e80af4da80cdd067f0794 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 27 May 2018 09:51:46 -0500 Subject: [PATCH 2/4] test: requested change fail on unhandled rejection --- test/parallel/test-fs-promises-readfile-empty.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/parallel/test-fs-promises-readfile-empty.js b/test/parallel/test-fs-promises-readfile-empty.js index ef15a2681123c6..49e16aca153e65 100644 --- a/test/parallel/test-fs-promises-readfile-empty.js +++ b/test/parallel/test-fs-promises-readfile-empty.js @@ -7,6 +7,8 @@ const fixtures = require('../common/fixtures'); const fn = fixtures.path('empty.txt'); +common.crashOnUnhandledRejection(); + fs.readFile(fn) .then(assert.ok); From ca44fc29fd2b7efb11bd6801b2971df6146724a1 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 27 May 2018 11:07:11 -0500 Subject: [PATCH 3/4] test: fix requested change common was required, but not assigned to common in order for the requested change to work. --- test/parallel/test-fs-promises-readfile-empty.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-fs-promises-readfile-empty.js b/test/parallel/test-fs-promises-readfile-empty.js index 49e16aca153e65..24c17655c62135 100644 --- a/test/parallel/test-fs-promises-readfile-empty.js +++ b/test/parallel/test-fs-promises-readfile-empty.js @@ -1,5 +1,5 @@ 'use strict'; -require('../common'); +const common = require('../common'); const assert = require('assert'); const { promises: fs } = require('fs'); From 543f29729872befed736ce758fc1d3fff829dadc Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 28 May 2018 09:43:08 -0500 Subject: [PATCH 4/4] fs: return string on empty fs.promises.readFile() when encoding provided fs: return string on empty fs.promises.readfile when encoding provided fs.promises.readfile() would provide a buffer whenever an empty file is read, when it should have returned an empty string when encoding is provided. --- lib/internal/fs/promises.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 262aed8f31c36f..c08900289cc0d7 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -139,7 +139,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);