buffer: fix custom inspection with extra properties

This broke due to a recent change that prevents exposing inspect
internals. It now relies on the public API instead and should be a
bit more robust due to that.

PR-URL: https://github.com/nodejs/node/pull/27074
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
This commit is contained in:
Ruben Bridgewater 2019-04-03 19:54:37 +02:00 committed by Daniel Bevenius
parent fadcb2d850
commit d834275a48
3 changed files with 31 additions and 11 deletions

View File

@ -59,8 +59,7 @@ const {
isUint8Array isUint8Array
} = require('internal/util/types'); } = require('internal/util/types');
const { const {
formatProperty, inspect: utilInspect
kObjectType
} = require('internal/util/inspect'); } = require('internal/util/inspect');
const { const {
@ -665,13 +664,24 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`; str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
// Inspect special properties as well, if possible. // Inspect special properties as well, if possible.
if (ctx) { if (ctx) {
let extras = false;
const filter = ctx.showHidden ? ALL_PROPERTIES : ONLY_ENUMERABLE; const filter = ctx.showHidden ? ALL_PROPERTIES : ONLY_ENUMERABLE;
str += getOwnNonIndexProperties(this, filter).reduce((str, key) => { const obj = getOwnNonIndexProperties(this, filter).reduce((obj, key) => {
// Using `formatProperty()` expects an indentationLvl to be set. extras = true;
ctx.indentationLvl = 0; obj[key] = this[key];
str += `, ${formatProperty(ctx, this, recurseTimes, key, kObjectType)}`; return obj;
return str; }, Object.create(null));
}, ''); if (extras) {
if (this.length !== 0)
str += ', ';
// '[Object: null prototype] {'.length === 26
// This is guarded with a test.
str += utilInspect(obj, {
...ctx,
breakLength: Infinity,
compact: true
}).slice(27, -2);
}
} }
return `<${this.constructor.name} ${str}>`; return `<${this.constructor.name} ${str}>`;
}; };

View File

@ -1546,8 +1546,6 @@ function formatWithOptions(inspectOptions, ...args) {
module.exports = { module.exports = {
inspect, inspect,
formatProperty,
kObjectType,
format, format,
formatWithOptions formatWithOptions
}; };

View File

@ -55,4 +55,16 @@ 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), '<Buffer 31 32, inspect: undefined>'); b.prop = new Uint8Array(0);
assert.strictEqual(
util.inspect(b),
'<Buffer 31 32, inspect: undefined, prop: Uint8Array []>'
);
b = Buffer.alloc(0);
b.prop = 123;
assert.strictEqual(
util.inspect(b),
'<Buffer prop: 123>'
);