From a3384959bad4987320387e7ac261089b4cf2af37 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Wed, 3 Apr 2019 19:54:37 +0200 Subject: [PATCH] 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. --- lib/buffer.js | 26 ++++++++++++++++++-------- lib/internal/util/inspect.js | 2 -- test/parallel/test-buffer-inspect.js | 14 +++++++++++++- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index b5d89625b2bc69..110ee78cce7209 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -57,8 +57,7 @@ const { isUint8Array } = require('internal/util/types'); const { - formatProperty, - kObjectType + inspect: utilInspect } = require('internal/util/inspect'); const { @@ -663,13 +662,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}>`; }; diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 87e53d62f64f05..7642c294ac0eaa 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1531,8 +1531,6 @@ function formatWithOptions(inspectOptions, ...args) { module.exports = { inspect, - formatProperty, - kObjectType, format, formatWithOptions }; diff --git a/test/parallel/test-buffer-inspect.js b/test/parallel/test-buffer-inspect.js index 9230d7b089dd16..d6ecf6b7fc5c38 100644 --- a/test/parallel/test-buffer-inspect.js +++ b/test/parallel/test-buffer-inspect.js @@ -55,4 +55,16 @@ assert.strictEqual(util.inspect(b), expected); assert.strictEqual(util.inspect(s), expected); b.inspect = undefined; -assert.strictEqual(util.inspect(b), ''); +b.prop = new Uint8Array(0); +assert.strictEqual( + util.inspect(b), + '' +); + +b = Buffer.alloc(0); +b.prop = 123; + +assert.strictEqual( + util.inspect(b), + '' +);