From 07ecf953c3fddcfa6e6beff1677d0bcc936c6e84 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Thu, 17 Jul 2025 12:43:10 -0700 Subject: [PATCH] util: ensure `inspect.colors` --- lib/internal/util/inspect.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/internal/util/inspect.js b/lib/internal/util/inspect.js index d1b1965d95bebc..f5044220445cc0 100644 --- a/lib/internal/util/inspect.js +++ b/lib/internal/util/inspect.js @@ -74,8 +74,10 @@ const { ObjectSetPrototypeOf, Promise, PromisePrototype, + Proxy, ReflectApply, ReflectOwnKeys, + ReflectSet, RegExp, RegExpPrototype, RegExpPrototypeExec, @@ -184,8 +186,10 @@ const assert = require('internal/assert'); const { BuiltinModule } = require('internal/bootstrap/realm'); const { + validateArray, validateObject, validateString, + validateNumber, kValidateObjectAllowArray, } = require('internal/validators'); @@ -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], @@ -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); + }, + }, +); + function defineColorAlias(target, alias) { ObjectDefineProperty(inspect.colors, alias, { __proto__: null,