Skip to content

Commit 95fa3c7

Browse files
committed
buffer: inspect extra properties
This makes sure extra properties on buffers are not ignored anymore when inspecting the buffer. PR-URL: #25150 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent e97acf7 commit 95fa3c7

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

lib/buffer.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ const {
3737
kMaxLength,
3838
kStringMaxLength
3939
} = internalBinding('buffer');
40+
const {
41+
getOwnNonIndexProperties,
42+
propertyFilter: {
43+
ALL_PROPERTIES,
44+
ONLY_ENUMERABLE
45+
}
46+
} = internalBinding('util');
4047
const {
4148
customInspectSymbol,
4249
isInsideNodeModules,
@@ -49,6 +56,11 @@ const {
4956
isUint8Array
5057
} = require('internal/util/types');
5158

59+
const {
60+
formatProperty,
61+
kObjectType
62+
} = require('internal/util/inspect');
63+
5264
const {
5365
ERR_BUFFER_OUT_OF_BOUNDS,
5466
ERR_OUT_OF_RANGE,
@@ -668,10 +680,20 @@ Buffer.prototype.equals = function equals(otherBuffer) {
668680
Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
669681
const max = exports.INSPECT_MAX_BYTES;
670682
const actualMax = Math.min(max, this.length);
671-
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
672683
const remaining = this.length - max;
684+
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
673685
if (remaining > 0)
674686
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
687+
// Inspect special properties as well, if possible.
688+
if (ctx) {
689+
const filter = ctx.showHidden ? ALL_PROPERTIES : ONLY_ENUMERABLE;
690+
str += getOwnNonIndexProperties(this, filter).reduce((str, key) => {
691+
// Using `formatProperty()` expects an indentationLvl to be set.
692+
ctx.indentationLvl = 0;
693+
str += `, ${formatProperty(ctx, this, recurseTimes, key, kObjectType)}`;
694+
return str;
695+
}, '');
696+
}
675697
return `<${this.constructor.name} ${str}>`;
676698
};
677699
Buffer.prototype.inspect = Buffer.prototype[customInspectSymbol];

lib/internal/util/inspect.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,5 +1236,7 @@ function reduceToSingleString(ctx, output, base, braces) {
12361236
}
12371237

12381238
module.exports = {
1239-
inspect
1239+
inspect,
1240+
formatProperty,
1241+
kObjectType
12401242
};

test/parallel/test-buffer-inspect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ assert.strictEqual(util.inspect(b), expected);
5555
assert.strictEqual(util.inspect(s), expected);
5656

5757
b.inspect = undefined;
58-
assert.strictEqual(util.inspect(b), expected);
58+
assert.strictEqual(util.inspect(b), '<Buffer 31 32, inspect: undefined>');

0 commit comments

Comments
 (0)