Skip to content
Open
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
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
8 changes: 8 additions & 0 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2624,6 +2624,14 @@ function isBelowBreakLength(ctx, output, start, base) {
// TODO(BridgeAR): Add unicode support. Use the readline getStringWidth
// function. Check the performance overhead and make it an opt-in in case it's
// significant.
// allow the single-line format if the length limit is infinite and no items have newlines
if (ctx.breakLength === Infinity) {
if (base !== '' && StringPrototypeIncludes(base, '\n')) return false;
for (let i = 0; i < output.length; i++) {
if (typeof output[i] === 'string' && StringPrototypeIncludes(output[i], '\n')) return false;
}
return true;
}
let totalLength = output.length + start;
if (totalLength + output.length > ctx.breakLength)
return false;
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -4058,3 +4058,9 @@ ${error.stack.split('\n').slice(1).join('\n')}`,
assert.match(inspect(DOMException.prototype), /^\[object DOMException\] \{/);
delete Error[Symbol.hasInstance];
}

{
const obj = { a: 'short string', b: [1, 2], c: { d: true } };
const expected = "{ a: 'short string', b: [ 1, 2 ], c: { d: true } }";
assert.strictEqual(util.inspect(obj, { breakLength: Infinity }), expected);
}
Loading