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
19 changes: 18 additions & 1 deletion 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,13 +618,18 @@ function readFileSync(path, options) {
validateReadFileBufferOptions(options);
const hasUserBuffer = options.buffer !== undefined;

<<<<<<< HEAD
if ((options.encoding === 'utf8' || options.encoding === 'utf-8') &&
!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.
path |= 0;
} else {
=======
if (isUtf8Encoding(options.encoding) && !hasUserBuffer) {
if (!isInt32(path)) {
>>>>>>> fccaa919c21 (fs: accept all valid utf8 values in fast paths)
path = getValidatedPath(path);
}
return binding.readFileUtf8(path, stringToFlags(options.flag));
Expand Down Expand Up @@ -3015,12 +3027,17 @@ function writeFileSync(path, data, options) {
const flag = options.flag || 'w';

// C++ fast path for string data and UTF8 encoding
<<<<<<< HEAD
if (typeof data === 'string' && (options.encoding === 'utf8' || options.encoding === 'utf-8')) {
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.
path |= 0;
} else {
=======
if (typeof data === 'string' && isUtf8Encoding(options.encoding)) {
if (!isInt32(path)) {
>>>>>>> fccaa919c21 (fs: accept all valid utf8 values in fast paths)
path = getValidatedPath(path);
}

Expand Down Expand Up @@ -3320,7 +3337,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