Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ const {
ObjectSetPrototypeOf,
Promise,
PromisePrototype,
Proxy,
ReflectApply,
ReflectOwnKeys,
ReflectSet,
RegExp,
RegExpPrototype,
RegExpPrototypeExec,
Expand Down Expand Up @@ -184,8 +186,10 @@ const assert = require('internal/assert');

const { BuiltinModule } = require('internal/bootstrap/realm');
const {
validateArray,
validateObject,
validateString,
validateNumber,
kValidateObjectAllowArray,
} = require('internal/validators');

Expand Down Expand Up @@ -421,7 +425,7 @@ ObjectDefineProperty(inspect, 'defaultOptions', {
// reset code as second entry.
const defaultFG = 39;
const defaultBG = 49;
inspect.colors = {
const defaultInspectColor = {
__proto__: null,
reset: [0, 0],
bold: [1, 22],
Expand Down Expand Up @@ -470,6 +474,20 @@ inspect.colors = {
bgWhiteBright: [107, defaultBG],
};

inspect.colors = new Proxy(
defaultInspectColor,
{
__proto__: null,
set(target, prop, value) {
validateString(prop, 'color name');
validateArray(value, 'value', { length: 2 });
validateNumber(value[0], 'color code');
validateNumber(value[1], 'reset code');
return ReflectSet(target, prop, value);
},
},
);
Copy link
Member

@jasnell jasnell Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What kind of performance impact will this have? inspect.colors is accessed in a for loop inside styleText and accessing via a Proxy (even without a get trap) will have a definite performance impact in such cases, so we should be careful here.


function defineColorAlias(target, alias) {
ObjectDefineProperty(inspect.colors, alias, {
__proto__: null,
Expand Down
Loading