Skip to content
Open
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
14 changes: 10 additions & 4 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ const {
O_SYMLINK,
} = constants;

function isUtf8Encoding(encoding) {
return encoding === 'utf8' ||
encoding === 'utf-8' ||
encoding === 'UTF8' ||
encoding === 'UTF-8';
}

const pathModule = require('path');
const { isArrayBufferView } = require('internal/util/types');

Expand Down Expand Up @@ -611,8 +618,7 @@ function readFileSync(path, options) {
validateReadFileBufferOptions(options);
const hasUserBuffer = options.buffer !== undefined;

if ((options.encoding === 'utf8' || options.encoding === 'utf-8') &&
!hasUserBuffer) {
if (isUtf8Encoding(options.encoding) && !hasUserBuffer) {
if (isInt32(path)) {
// V8 does not report -0 as an int32, so it would reach the binding as a
// path instead of a file descriptor.
Expand Down Expand Up @@ -3015,7 +3021,7 @@ function writeFileSync(path, data, options) {
const flag = options.flag || 'w';

// C++ fast path for string data and UTF8 encoding
if (typeof data === 'string' && (options.encoding === 'utf8' || options.encoding === 'utf-8')) {
if (typeof data === 'string' && isUtf8Encoding(options.encoding)) {
if (isInt32(path)) {
// V8 does not report -0 as an int32, so it would reach the binding as a
// path instead of a file descriptor.
Expand Down Expand Up @@ -3320,7 +3326,7 @@ if (isWindows) {
}

function encodeRealpathResult(result, options) {
if (!options || !options.encoding || options.encoding === 'utf8')
if (!options || !options.encoding || isUtf8Encoding(options.encoding))
return result;
const asBuffer = Buffer.from(result);
if (options.encoding === 'buffer') {
Expand Down
Loading