Skip to content

Commit 68014fb

Browse files
BridgeARaddaleax
authored andcommitted
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 3350230 commit 68014fb

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

lib/buffer.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ const {
3838
kStringMaxLength
3939
} = internalBinding('buffer');
4040
const { isAnyArrayBuffer } = internalBinding('types');
41+
const {
42+
getOwnNonIndexProperties,
43+
propertyFilter: {
44+
ALL_PROPERTIES,
45+
ONLY_ENUMERABLE
46+
}
47+
} = internalBinding('util');
4148
const {
4249
customInspectSymbol,
4350
isInsideNodeModules,
@@ -51,6 +58,11 @@ const {
5158
const {
5259
pendingDeprecation
5360
} = internalBinding('config');
61+
const {
62+
formatProperty,
63+
kObjectType
64+
} = require('internal/util/inspect');
65+
5466
const {
5567
ERR_BUFFER_OUT_OF_BOUNDS,
5668
ERR_OUT_OF_RANGE,
@@ -670,10 +682,20 @@ Buffer.prototype.equals = function equals(otherBuffer) {
670682
Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
671683
const max = exports.INSPECT_MAX_BYTES;
672684
const actualMax = Math.min(max, this.length);
673-
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
674685
const remaining = this.length - max;
686+
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
675687
if (remaining > 0)
676688
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
689+
// Inspect special properties as well, if possible.
690+
if (ctx) {
691+
const filter = ctx.showHidden ? ALL_PROPERTIES : ONLY_ENUMERABLE;
692+
str += getOwnNonIndexProperties(this, filter).reduce((str, key) => {
693+
// Using `formatProperty()` expects an indentationLvl to be set.
694+
ctx.indentationLvl = 0;
695+
str += `, ${formatProperty(ctx, this, recurseTimes, key, kObjectType)}`;
696+
return str;
697+
}, '');
698+
}
677699
return `<${this.constructor.name} ${str}>`;
678700
};
679701
Buffer.prototype.inspect = Buffer.prototype[customInspectSymbol];

lib/internal/util/inspect.js

+3-1
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

+1-1
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)