Skip to content

Commit 70f6898

Browse files
BridgeARrefack
authored andcommitted
buffer: inspect extra properties
This makes sure extra properties on buffers are not ignored anymore when inspecting the buffer. PR-URL: nodejs#25150 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent a9dc51d commit 70f6898

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

lib/buffer.js

Lines changed: 22 additions & 1 deletion
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,
@@ -48,6 +55,10 @@ const {
4855
isArrayBufferView,
4956
isUint8Array
5057
} = require('internal/util/types');
58+
const {
59+
formatProperty,
60+
kObjectType
61+
} = require('internal/util/inspect');
5162

5263
const {
5364
ERR_BUFFER_OUT_OF_BOUNDS,
@@ -671,10 +682,20 @@ Buffer.prototype.equals = function equals(otherBuffer) {
671682
Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
672683
const max = exports.INSPECT_MAX_BYTES;
673684
const actualMax = Math.min(max, this.length);
674-
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
675685
const remaining = this.length - max;
686+
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
676687
if (remaining > 0)
677688
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+
}
678699
return `<${this.constructor.name} ${str}>`;
679700
};
680701
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
@@ -1217,5 +1217,7 @@ function reduceToSingleString(ctx, output, base, braces) {
12171217
}
12181218

12191219
module.exports = {
1220-
inspect
1220+
inspect,
1221+
formatProperty,
1222+
kObjectType
12211223
};

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)