GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — buffer: inspect extra properties · nodejs/node@68014fb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib
    • buffer.js
    • internal/util
      • inspect.js
  • test/parallel
    • test-buffer-inspect.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/buffer.js‎

Copy file name to clipboardExpand all lines: lib/buffer.js
+23-1Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ const {
3838
kStringMaxLength
3939
} = internalBinding('buffer');
4040
const { isAnyArrayBuffer } = internalBinding('types');
41+
const {
42+
getOwnNonIndexProperties,
43+
propertyFilter: {
44+
ALL_PROPERTIES,
45+
ONLY_ENUMERABLE
46+
}
47+
} = internalBinding('util');
4148
const {
4249
customInspectSymbol,
4350
isInsideNodeModules,
@@ -51,6 +58,11 @@ const {
5158
const {
5259
pendingDeprecation
5360
} = internalBinding('config');
61+
const {
62+
formatProperty,
63+
kObjectType
64+
} = require('internal/util/inspect');
65+
5466
const {
5567
ERR_BUFFER_OUT_OF_BOUNDS,
5668
ERR_OUT_OF_RANGE,
@@ -670,10 +682,20 @@ Buffer.prototype.equals = function equals(otherBuffer) {
670682
Buffer.prototype[customInspectSymbol] = function inspect(recurseTimes, ctx) {
671683
const max = exports.INSPECT_MAX_BYTES;
672684
const actualMax = Math.min(max, this.length);
673-
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
674685
const remaining = this.length - max;
686+
let str = this.hexSlice(0, actualMax).replace(/(.{2})/g, '$1 ').trim();
675687
if (remaining > 0)
676688
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+
}
677699
return `<${this.constructor.name} ${str}>`;
678700
};
679701
Buffer.prototype.inspect = Buffer.prototype[customInspectSymbol];
Collapse file

‎lib/internal/util/inspect.js‎

Copy file name to clipboardExpand all lines: lib/internal/util/inspect.js
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1236,5 +1236,7 @@ function reduceToSingleString(ctx, output, base, braces) {
12361236
}
12371237

12381238
module.exports = {
1239-
inspect
1239+
inspect,
1240+
formatProperty,
1241+
kObjectType
12401242
};
Collapse file

‎test/parallel/test-buffer-inspect.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-buffer-inspect.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@ assert.strictEqual(util.inspect(b), expected);
5555
assert.strictEqual(util.inspect(s), expected);
5656

5757
b.inspect = undefined;
58-
assert.strictEqual(util.inspect(b), expected);
58+
assert.strictEqual(util.inspect(b), '<Buffer 31 32, inspect: undefined>');

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.