Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

promises: use encoding in readFile #19296

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert.strictEqual() was probably better here, but I've already landed this and it's not a biggie so I will not force push.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lpinca why?

Copy link
Member

@lpinca lpinca Mar 24, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a string no? No need to be deep.

}

doWrite()
.then(doAppend)
.then(doRead)
.then(doReadWithEncoding)
.then(common.mustCall());