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 @@ -59,6 +59,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 @@ -531,8 +538,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)) {
path = getValidatedPath(path);
}
Expand Down Expand Up @@ -2906,7 +2912,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)) {
path = getValidatedPath(path);
}
Expand Down Expand Up @@ -3197,7 +3203,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