Skip to content

Commit 555bcfb

Browse files
LiviaMedeirosaduh95
authored andcommitted
util: use internal hexSlice directly in util.inspect()
Signed-off-by: LiviaMedeiros <livia@cirno.name> PR-URL: #66974 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jordan Harband <ljharb@gmail.com>
1 parent 356b720 commit 555bcfb

3 files changed

Lines changed: 15 additions & 4 deletions

File tree

‎lib/internal/buffer.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,7 @@ module.exports = {
11481148
createUnsafeAlignedBuffer,
11491149
readUInt16BE,
11501150
readUInt32BE,
1151+
hexSlice,
11511152
asciiWrite,
11521153
latin1Write,
11531154
utf8Write,

‎lib/internal/util/inspect.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ const {
130130
WeakSet,
131131
WeakSetPrototype,
132132
globalThis,
133-
uncurryThis,
134133
} = primordials;
135134

136135
const {
@@ -195,7 +194,10 @@ const {
195194
kValidateObjectAllowArray,
196195
} = require('internal/validators');
197196

198-
let hexSlice;
197+
const {
198+
hexSlice,
199+
} = require('internal/buffer');
200+
199201
let internalUrl;
200202

201203
function pathToFileUrlHref(filepath) {
@@ -2347,8 +2349,6 @@ function formatArrayBuffer(ctx, value) {
23472349
} catch {
23482350
return [ctx.stylize('(detached)', 'special')];
23492351
}
2350-
if (hexSlice === undefined)
2351-
hexSlice = uncurryThis(require('buffer').Buffer.prototype.hexSlice);
23522352
const rawString = hexSlice(buffer, 0, MathMin(ctx.maxArrayLength, buffer.length));
23532353
let str = '';
23542354
let i = 0;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { mustNotCall } from '../common/index.mjs';
2+
import assert from 'node:assert';
3+
import { inspect } from 'node:util';
4+
5+
// util.inspect must not be affected by userland mutations of Buffer.prototype
6+
Buffer.prototype.hexSlice = mustNotCall();
7+
8+
const { buffer } = new Uint8Array([0x04, 0x38, 0x73, 0xE6]);
9+
const expected = 'ArrayBuffer { [Uint8Contents]: <04 38 73 e6>, [byteLength]: 4 }';
10+
assert.strictEqual(inspect(buffer), expected);

0 commit comments

Comments
 (0)