Skip to content

Commit 91ab769

Browse files
jasnellFishrock123
authored andcommitted
util: truncate inspect array and typed array
As an alternative to #5070, set the max length of Arrays/TypedArrays in util.inspect() to `100` and provide a `maxArrayLength` option to override. PR-URL: #6334 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Roman Reiss <[email protected]>
1 parent 33f24c8 commit 91ab769

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

doc/api/util.md

+4
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ formatted string:
183183
will be introspected to show their `target` and `hander` objects. Defaults to
184184
`false`.
185185

186+
- `maxArrayLength` - specifies the maximum number of Array and TypedArray
187+
elements to include when formatting. Defaults to `100`. Set to `null` to
188+
show all array elements. Set to `0` or negative to show no array elements.
189+
186190
Example of inspecting all properties of the `util` object:
187191

188192
```js

lib/util.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const internalUtil = require('internal/util');
66
const binding = process.binding('util');
77

88
const isError = internalUtil.isError;
9+
const kDefaultMaxLength = 100;
910

1011
var Debug;
1112

@@ -141,6 +142,8 @@ function inspect(obj, opts) {
141142
if (ctx.customInspect === undefined) ctx.customInspect = true;
142143
if (ctx.showProxy === undefined) ctx.showProxy = false;
143144
if (ctx.colors) ctx.stylize = stylizeWithColor;
145+
if (ctx.maxArrayLength === undefined) ctx.maxArrayLength = kDefaultMaxLength;
146+
if (ctx.maxArrayLength === null) ctx.maxArrayLength = Infinity;
144147
return formatValue(ctx, obj, ctx.depth);
145148
}
146149
exports.inspect = inspect;
@@ -579,14 +582,19 @@ function formatObject(ctx, value, recurseTimes, visibleKeys, keys) {
579582

580583
function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
581584
var output = [];
582-
for (var i = 0, l = value.length; i < l; ++i) {
585+
const maxLength = Math.min(Math.max(0, ctx.maxArrayLength), value.length);
586+
const remaining = value.length - maxLength;
587+
for (var i = 0; i < maxLength; ++i) {
583588
if (hasOwnProperty(value, String(i))) {
584589
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
585590
String(i), true));
586591
} else {
587592
output.push('');
588593
}
589594
}
595+
if (remaining > 0) {
596+
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
597+
}
590598
keys.forEach(function(key) {
591599
if (typeof key === 'symbol' || !key.match(/^\d+$/)) {
592600
output.push(formatProperty(ctx, value, recurseTimes, visibleKeys,
@@ -598,9 +606,14 @@ function formatArray(ctx, value, recurseTimes, visibleKeys, keys) {
598606

599607

600608
function formatTypedArray(ctx, value, recurseTimes, visibleKeys, keys) {
601-
var output = new Array(value.length);
602-
for (var i = 0, l = value.length; i < l; ++i)
609+
const maxLength = Math.min(Math.max(0, ctx.maxArrayLength), value.length);
610+
const remaining = value.length - maxLength;
611+
var output = new Array(maxLength);
612+
for (var i = 0; i < maxLength; ++i)
603613
output[i] = formatNumber(ctx, value[i]);
614+
if (remaining > 0) {
615+
output.push(`... ${remaining} more item${remaining > 1 ? 's' : ''}`);
616+
}
604617
for (const key of keys) {
605618
if (typeof key === 'symbol' || !key.match(/^\d+$/)) {
606619
output.push(

test/parallel/test-util-inspect.js

+55
Original file line numberDiff line numberDiff line change
@@ -648,3 +648,58 @@ checkAlignment(new Map(big_array.map(function(y) { return [y, null]; })));
648648
const x = Object.create(null);
649649
assert.equal(util.inspect(x), '{}');
650650
}
651+
652+
// The following maxArrayLength tests were introduced after v6.0.0 was released.
653+
// Do not backport to v5/v4 unless all of
654+
// https://github.com/nodejs/node/pull/6334 is backported.
655+
{
656+
const x = Array(101);
657+
assert(/1 more item/.test(util.inspect(x)));
658+
}
659+
660+
{
661+
const x = Array(101);
662+
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: 101})));
663+
}
664+
665+
{
666+
const x = Array(101);
667+
assert(/^\[ ... 101 more items \]$/.test(
668+
util.inspect(x, {maxArrayLength: 0})));
669+
}
670+
671+
{
672+
const x = new Uint8Array(101);
673+
assert(/1 more item/.test(util.inspect(x)));
674+
}
675+
676+
{
677+
const x = new Uint8Array(101);
678+
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: 101})));
679+
}
680+
681+
{
682+
const x = new Uint8Array(101);
683+
assert(/\[ ... 101 more items \]$/.test(
684+
util.inspect(x, {maxArrayLength: 0})));
685+
}
686+
687+
{
688+
const x = Array(101);
689+
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: null})));
690+
}
691+
692+
{
693+
const x = Array(101);
694+
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: Infinity})));
695+
}
696+
697+
{
698+
const x = new Uint8Array(101);
699+
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: null})));
700+
}
701+
702+
{
703+
const x = new Uint8Array(101);
704+
assert(!/1 more item/.test(util.inspect(x, {maxArrayLength: Infinity})));
705+
}

0 commit comments

Comments
 (0)