From 1be44125308d3e71bf8f3e775d3da23cccf17669 Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Thu, 20 Dec 2018 03:43:02 +0100 Subject: [PATCH] buffer: inspect extra properties This makes sure extra properties on buffers are not ignored anymore when inspecting the buffer. --- lib/buffer.js | 23 ++++++++++++++++++++++- lib/internal/util/inspect.js | 4 +++- test/parallel/test-buffer-inspect.js | 2 +- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/buffer.js b/lib/buffer.js index ed3928b1b4a537..0521375cf53867 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -38,6 +38,13 @@ const { kStringMaxLength } = internalBinding('buffer'); const { isAnyArrayBuffer } = internalBinding('types'); +const { + getOwnNonIndexProperties, + propertyFilter: { + ALL_PROPERTIES, + ONLY_ENUMERABLE + } +} = internalBinding('util'); const { customInspectSymbol, isInsideNodeModules, @@ -48,6 +55,10 @@ const { isArrayBufferView, isUint8Array } = require('internal/util/types'); +const { + formatProperty, + kObjectType +} = require('internal/util/inspect'); const { ERR_BUFFER_OUT_OF_BOUNDS, @@ -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]; diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index 256a4a8b06904b..355c57a50590da 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -1214,5 +1214,7 @@ function reduceToSingleString(ctx, output, base, braces) { } module.exports = { - inspect + inspect, + formatProperty, + kObjectType }; diff --git a/test/parallel/test-buffer-inspect.js b/test/parallel/test-buffer-inspect.js index 9f91de700c7878..9230d7b089dd16 100644 --- a/test/parallel/test-buffer-inspect.js +++ b/test/parallel/test-buffer-inspect.js @@ -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), '');