GeezerVison is showing github.com — buffer: inspect extra properties · nodejs/node@68014fb
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/buffer.js +23-1Lines changed: 23 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -38,6 +38,13 @@ const { | 38 | 38 | kStringMaxLength | 39 | 39 | } = internalBinding('buffer'); | 40 | 40 | const { isAnyArrayBuffer } = internalBinding('types'); | | 41 | +const { | | 42 | + getOwnNonIndexProperties, | | 43 | + propertyFilter: { | | 44 | + ALL_PROPERTIES, | | 45 | + ONLY_ENUMERABLE | | 46 | + } | | 47 | +} = internalBinding('util'); | 41 | 48 | const { | 42 | 49 | customInspectSymbol, | 43 | 50 | isInsideNodeModules, | @@ -51,6 +58,11 @@ const { | 51 | 58 | const { | 52 | 59 | pendingDeprecation | 53 | 60 | } = internalBinding('config'); | | 61 | +const { | | 62 | + formatProperty, | | 63 | + kObjectType | | 64 | +} = require('internal/util/inspect'); | | 65 | + | 54 | 66 | const { | 55 | 67 | ERR_BUFFER_OUT_OF_BOUNDS, | 56 | 68 | ERR_OUT_OF_RANGE, | @@ -670,10 +682,20 @@ Buffer.prototype.equals = function equals(otherBuffer) { | 670 | 682 | Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) { | 671 | 683 | const max = exports.INSPECT_MAX_BYTES; | 672 | 684 | const actualMax = Math.min(max, this.length); | 673 | | - let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim(); | 674 | 685 | const remaining = this.length - max; | | 686 | + let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim(); | 675 | 687 | if (remaining > 0) | 676 | 688 | str += ` ... ${remaining} more byte${remaining > 1 ? 's' : ''}`; | | 689 | + // Inspect special properties as well, if possible. | | 690 | + if (ctx) { | | 691 | + const filter = ctx.showHidden ? ALL_PROPERTIES : ONLY_ENUMERABLE; | | 692 | + str += getOwnNonIndexProperties(this, filter).reduce((str, key) => { | | 693 | + // Using `formatProperty()` expects an indentationLvl to be set. | | 694 | + ctx.indentationLvl = 0; | | 695 | + str += `, ${formatProperty(ctx, this, recurseTimes, key, kObjectType)}`; | | 696 | + return str; | | 697 | + }, ''); | | 698 | + } | 677 | 699 | return `<${this.constructor.name} ${str}>`; | 678 | 700 | }; | 679 | 701 | Buffer.prototype.inspect = Buffer.prototype[customInspectSymbol]; | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js +3-1Lines changed: 3 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1236,5 +1236,7 @@ function reduceToSingleString(ctx, output, base, braces) { | 1236 | 1236 | } | 1237 | 1237 |
| 1238 | 1238 | module.exports = { | 1239 | | - inspect | | 1239 | + inspect, | | 1240 | + formatProperty, | | 1241 | + kObjectType | 1240 | 1242 | }; |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-buffer-inspect.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,4 +55,4 @@ assert.strictEqual(util.inspect(b), expected); | 55 | 55 | assert.strictEqual(util.inspect(s), expected); | 56 | 56 |
| 57 | 57 | b.inspect = undefined; | 58 | | -assert.strictEqual(util.inspect(b), expected); | | 58 | +assert.strictEqual(util.inspect(b), '<Buffer 31 32, inspect: undefined>'); |
|