8000 buffer: inspect extra properties by BridgeAR · Pull Request #25150 · nodejs/node · GitHub
[go: up one dir, main page]

Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ const {
kStringMaxLength
} = internalBinding('buffer');
const { isAnyArrayBuffer } = internalBinding('types');
const {
getOwnNonIndexProperties,
propertyFilter: {
ALL_PROPERTIES,
ONLY_ENUMERABLE
}
} = internalBinding('util');
const {
customInspectSymbol,
isInsideNodeModules,
Expand All @@ -48,6 +55,10 @@ const {
isArrayBufferView,
isUint8Array
} = require('inte 8000 rnal/util/types');
const {
formatProperty,
kObjectType
} = require('internal/util/inspect');

const {
ERR_BUFFER_OUT_OF_BOUNDS,
Expand Down Expand Up @@ -671,10 +682,20 @@ Buffer.prototype.equals = function equals(otherBuffer) {
Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
const max = exports.INSPECT_MAX_BYTES;
const actualMax = Math.min(max, this.length);
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
const remaining = this.length - max;
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
if (remaining > 0)
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
// Inspect special properties as well, if possible.
if (ctx) {
const filter = ctx.showHidden ? ALL_PROPERTIES : ONLY_ENUMERABLE;
str += getOwnNonIndexProperties(this, filter).reduce((str, key) => {
// Using `formatProperty()` expects an indentationLvl to be set.
ctx.indentationLvl = 0;
str += `, ${formatProperty(ctx, this, recurseTimes, key, kObjectType)}`;
return str;
}, '');
}
return `<${this.constructor.name} ${str}>`;
};
Buffer.prototype.inspect = Buffer.prototype[customInspectSymbol];
Expand Down
4 changes: 3 additions & 1 deletion lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1214,5 +1214,7 @@ function reduceToSingleString(ctx, output, base, braces) {
}

module.exports = {
inspect
inspect,
formatProperty,
kObjectType
};
2 changes: 1 addition & 1 deletion test/parallel/test-buffer-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ assert.strictEqual(util.inspect(b), expected);
assert.strictEqual(util.inspect(s), expected);

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