Skip to content

Commit d834275

Browse files
BridgeARdanbev
authored andcommitted
buffer: fix custom inspection with extra properties
This broke due to a recent change that prevents exposing inspect internals. It now relies on the public API instead and should be a bit more robust due to that. PR-URL: #27074 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Yongsheng Zhang <[email protected]>
1 parent fadcb2d commit d834275

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

lib/buffer.js

+18-8
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ const {
5959
isUint8Array
6060
} = require('internal/util/types');
6161
const {
62-
formatProperty,
63-
kObjectType
62+
inspect: utilInspect
6463
} = require('internal/util/inspect');
6564

6665
const {
@@ -665,13 +664,24 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
665664
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
666665
// Inspect special properties as well, if possible.
667666
if (ctx) {
667+
let extras = false;
668668
const filter = ctx.showHidden ? ALL_PROPERTIES : ONLY_ENUMERABLE;
669-
str += getOwnNonIndexProperties(this, filter).reduce((str, key) => {
670-
// Using `formatProperty()` expects an indentationLvl to be set.
671-
ctx.indentationLvl = 0;
672-
str += `, ${formatProperty(ctx, this, recurseTimes, key, kObjectType)}`;
673-
return str;
674-
}, '');
669+
const obj = getOwnNonIndexProperties(this, filter).reduce((obj, key) => {
670+
extras = true;
671+
obj[key] = this[key];
672+
return obj;
673+
}, Object.create(null));
674+
if (extras) {
675+
if (this.length !== 0)
676+
str += ', ';
677+
// '[Object: null prototype] {'.length === 26
678+
// This is guarded with a test.
679+
str += utilInspect(obj, {
680+
...ctx,
681+
breakLength: Infinity,
682+
compact: true
683+
}).slice(27, -2);
684+
}
675685
}
676686
return `<${this.constructor.name} ${str}>`;
677687
};

lib/internal/util/inspect.js

-2
Original file line numberDiff line numberDiff line change
@@ -1546,8 +1546,6 @@ function formatWithOptions(inspectOptions, ...args) {
15461546

15471547
module.exports = {
15481548
inspect,
1549-
formatProperty,
1550-
kObjectType,
15511549
format,
15521550
formatWithOptions
15531551
};

test/parallel/test-buffer-inspect.js

+13-1
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,16 @@ assert.strictEqual(util.inspect(b), expected);
5555
assert.strictEqual(util.inspect(s), expected);
5656

5757
b.inspect = undefined;
58-
assert.strictEqual(util.inspect(b), '<Buffer 31 32, inspect: undefined>');
58+
b.prop = new Uint8Array(0);
59+
assert.strictEqual(
60+
util.inspect(b),
61+
'<Buffer 31 32, inspect: undefined, prop: Uint8Array []>'
62+
);
63+
64+
b = Buffer.alloc(0);
65+
b.prop = 123;
66+
67+
assert.strictEqual(
68+
util.inspect(b),
69+
'<Buffer prop: 123>'
70+
);

0 commit comments

Comments
 (0)