Skip to content

Commit

Permalink
promises: 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: #19296
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Fixes: #19286
  • Loading branch information
Benjamin Gruenbaum committed Mar 12, 2018
1 parent 8403f00 commit 6469a62
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);
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
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 6469a62

Please sign in to comment.