buffer: inspect extra properties
This makes sure extra properties on buffers are not ignored anymore when inspecting the buffer. PR-URL: https://github.com/nodejs/node/pull/25150 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
c9d08c7af2
commit
d385e2cc5a
@ -38,6 +38,13 @@ const {
|
|||||||
kStringMaxLength
|
kStringMaxLength
|
||||||
} = internalBinding('buffer');
|
} = internalBinding('buffer');
|
||||||
const { isAnyArrayBuffer } = internalBinding('types');
|
const { isAnyArrayBuffer } = internalBinding('types');
|
||||||
|
const {
|
||||||
|
getOwnNonIndexProperties,
|
||||||
|
propertyFilter: {
|
||||||
|
ALL_PROPERTIES,
|
||||||
|
ONLY_ENUMERABLE
|
||||||
|
}
|
||||||
|
} = internalBinding('util');
|
||||||
const {
|
const {
|
||||||
customInspectSymbol,
|
customInspectSymbol,
|
||||||
isInsideNodeModules,
|
isInsideNodeModules,
|
||||||
@ -48,6 +55,10 @@ const {
|
|||||||
isArrayBufferView,
|
isArrayBufferView,
|
||||||
isUint8Array
|
isUint8Array
|
||||||
} = require('internal/util/types');
|
} = require('internal/util/types');
|
||||||
|
const {
|
||||||
|
formatProperty,
|
||||||
|
kObjectType
|
||||||
|
} = require('internal/util/inspect');
|
||||||
|
|
||||||
const {
|
const {
|
||||||
ERR_BUFFER_OUT_OF_BOUNDS,
|
ERR_BUFFER_OUT_OF_BOUNDS,
|
||||||
@ -671,10 +682,20 @@ Buffer.prototype.equals = function equals(otherBuffer) {
|
|||||||
Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
|
Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
|
||||||
const max = exports.INSPECT_MAX_BYTES;
|
const max = exports.INSPECT_MAX_BYTES;
|
||||||
const actualMax = Math.min(max, this.length);
|
const actualMax = Math.min(max, this.length);
|
||||||
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
|
|
||||||
const remaining = this.length - max;
|
const remaining = this.length - max;
|
||||||
|
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
|
||||||
if (remaining > 0)
|
if (remaining > 0)
|
||||||
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
|
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}>`;
|
return `<${this.constructor.name} ${str}>`;
|
||||||
};
|
};
|
||||||
Buffer.prototype.inspect = Buffer.prototype[customInspectSymbol];
|
Buffer.prototype.inspect = Buffer.prototype[customInspectSymbol];
|
||||||
|
@ -1217,5 +1217,7 @@ function reduceToSingleString(ctx, output, base, braces) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
inspect
|
inspect,
|
||||||
|
formatProperty,
|
||||||
|
kObjectType
|
||||||
};
|
};
|
||||||
|
@ -55,4 +55,4 @@ assert.strictEqual(util.inspect(b), expected);
|
|||||||
assert.strictEqual(util.inspect(s), expected);
|
assert.strictEqual(util.inspect(s), expected);
|
||||||
|
|
||||||
b.inspect = undefined;
|
b.inspect = undefined;
|
||||||
assert.strictEqual(util.inspect(b), expected);
|
assert.strictEqual(util.inspect(b), '<Buffer 31 32, inspect: undefined>');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user