Skip to content

Commit e06ad5f

Browse files
Benjamin Gruenbaumlpinca
authored andcommitted
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: #19296 Fixes: #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>
1 parent 610dd79 commit e06ad5f

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/fs/promises.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ async function readFileHandle(filehandle, options) {
157157
chunks.push(buffer.slice(0, totalRead));
158158
} while (totalRead === chunkSize);
159159

160-
return Buffer.concat(chunks);
160+
const result = Buffer.concat(chunks);
161+
if (options.encoding) {
162+
return result.toString(options.encoding);
163+
} else {
164+
return result;
165+
}
161166
}
162167

163168
// All of the functions are defined as async in order to ensure that errors

test/parallel/test-fs-promises-writefile.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,15 @@ async function doRead() {
3535
assert.deepStrictEqual(buf, data);
3636
}
3737

38+
async function doReadWithEncoding() {
39+
const data = await fsPromises.readFile(dest, 'utf-8');
40+
const syncData = fs.readFileSync(dest, 'utf-8');
41+
assert.strictEqual(typeof data, 'string');
42+
assert.deepStrictEqual(data, syncData);
43+
}
44+
3845
doWrite()
3946
.then(doAppend)
4047
.then(doRead)
48+
.then(doReadWithEncoding)
4149
.then(common.mustCall());

0 commit comments

Comments
 (0)