mirror of
https://github.com/nodejs/node.git
synced 2026-10-14 10:26:26 -04:00
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:
1 parent
fadcb2d850
commit
d834275a48
3 files changed
+31
-11
No files matched your search
+18
-8
@@ -59,8 +59,7 @@ const {
|
||||
isUint8Array
|
||||
} = require('internal/util/types');
|
||||
const {
|
||||
formatProperty,
|
||||
kObjectType
|
||||
inspect: utilInspect
|
||||
} = require('internal/util/inspect');
|
||||
|
||||
const {
|
||||
@@ -665,13 +664,24 @@ Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
|
||||
str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`;
|
||||
// Inspect special properties as well, if possible.
|
||||
if (ctx) {
|
||||
let extras = false;
|
||||
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;
|
||||
}, '');
|
||||
const obj = getOwnNonIndexProperties(this, filter).reduce((obj, key) => {
|
||||
extras = true;
|
||||
obj[key] = this[key];
|
||||
return obj;
|
||||
}, 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}>`;
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user