From 6469a62d7cee7f1d89d2a6a42806725708511e31 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Mon, 12 Mar 2018 12:02:20 +0200 Subject: [PATCH 1/2] promises: use encoding in readfile Use the encoding parameter passed to fsPromises.readFile if it is passed. Currently the encoding parameter is ignored in fsPromises PR-URL: https://github.com/nodejs/node/pull/19296 Reviewed-By: Sakthipriyan Vairamani Fixes: https://github.com/nodejs/node/issues/19286 --- lib/fs/promises.js | 7 ++++++- test/parallel/test-fs-promises-writefile.js | 8 ++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/fs/promises.js b/lib/fs/promises.js index 2252900e22aaf0..80a10816da5430 100644 --- a/lib/fs/promises.js +++ b/lib/fs/promises.js @@ -157,7 +157,12 @@ async function readFileHandle(filehandle, options) { chunks.push(buffer.slice(0, totalRead)); } while (totalRead === chunkSize); - return Buffer.concat(chunks); + var buffer = Buffer.concat(chunks); + if (options.encoding) { + return buffer.toString(options.encoding); + } else { + return buffer; + } } // All of the functions are defined as async in order to ensure that errors diff --git a/test/parallel/test-fs-promises-writefile.js b/test/parallel/test-fs-promises-writefile.js index e2ae289b180bc2..280bce864229ce 100644 --- a/test/parallel/test-fs-promises-writefile.js +++ b/test/parallel/test-fs-promises-writefile.js @@ -35,7 +35,15 @@ async function doRead() { assert.deepStrictEqual(buf, data); } +async function doReadWithEncoding() { + const data = await fsPromises.readFile(dest, 'utf-8'); + const syncData = fs.readFileSync(dest, 'utf-8'); + assert.strictEqual(typeof data, 'string'); + assert.deepStrictEqual(data, syncData); +} + doWrite() .then(doAppend) .then(doRead) + .then(doReadWithEncoding) .then(common.mustCall()); From 100257f68d3932b4bf24849ad8eead2352795680 Mon Sep 17 00:00:00 2001 From: Benjamin Gruenbaum Date: Mon, 12 Mar 2018 17:56:31 +0200 Subject: [PATCH 2/2] review nit --- lib/fs/promises.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/fs/promises.js b/lib/fs/promises.js index 80a10816da5430..95e83a5475cffc 100644 --- a/lib/fs/promises.js +++ b/lib/fs/promises.js @@ -157,11 +157,11 @@ async function readFileHandle(filehandle, options) { chunks.push(buffer.slice(0, totalRead)); } while (totalRead === chunkSize); - var buffer = Buffer.concat(chunks); + const result = Buffer.concat(chunks); if (options.encoding) { - return buffer.toString(options.encoding); + return result.toString(options.encoding); } else { - return buffer; + return result; } }