fs: 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
Fixes: https://github.com/nodejs/node/issues/19286
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Yuta Hiroto <hello@hiroppy.me>
This commit is contained in:
Benjamin Gruenbaum 2018-03-12 12:02:20 +02:00 committed by Luigi Pinca
parent 610dd79956
commit e06ad5faf9
2 changed files with 14 additions and 1 deletions

View File

@ -157,7 +157,12 @@ async function readFileHandle(filehandle, options) {
chunks.push(buffer.slice(0, totalRead));
} while (totalRead === chunkSize);
return Buffer.concat(chunks);
const result = Buffer.concat(chunks);
if (options.encoding) {
return result.toString(options.encoding);
} else {
return result;
}
}
// All of the functions are defined as async in order to ensure that errors

View File

@ -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());