Skip to content

Commit

Permalink
fs: use encoding in readFile
Browse files Browse the repository at this point in the history
Use the encoding parameter passed to fsPromises.readFile if it is
passed. Currently the encoding parameter is ignored in fsPromises.

PR-URL: nodejs#19296
Fixes: nodejs#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>
  • Loading branch information
Benjamin Gruenbaum authored and lpinca committed Mar 24, 2018
1 parent 610dd79 commit e06ad5f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-fs-promises-writefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());

0 comments on commit e06ad5f

Please sign in to comment.