From 93e84a7998c9a5e7f65710b9e6f6d3bccb5a54c0 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Tue, 10 Mar 2026 13:20:47 -0700 Subject: [PATCH] util: fix styleText to accept format aliases The optimization in #61792 introduced a style cache using ObjectKeys(inspect.colors), which only returns enumerable properties. Color aliases (e.g. 'grey', 'blackBright', 'faint') are defined as non-enumerable via defineColorAlias(), so they were excluded from the cache, causing ERR_INVALID_ARG_VALUE when used with styleText(). Replace ObjectKeys with ObjectGetOwnPropertyNames to include non-enumerable alias properties in both the style cache and the validation error message. Fixes: https://github.com/nodejs/node/issues/62177 --- lib/util.js | 5 +++-- test/parallel/test-util-styletext.js | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lib/util.js b/lib/util.js index 5d3c0c75a28fc5..54b7e2f9a442f7 100644 --- a/lib/util.js +++ b/lib/util.js @@ -32,6 +32,7 @@ const { ObjectDefineProperties, ObjectDefineProperty, ObjectGetOwnPropertyDescriptors, + ObjectGetOwnPropertyNames, ObjectKeys, ObjectSetPrototypeOf, ObjectValues, @@ -115,7 +116,7 @@ function getStyleCache() { if (styleCache === undefined) { styleCache = { __proto__: null }; const colors = inspect.colors; - for (const key of ObjectKeys(colors)) { + for (const key of ObjectGetOwnPropertyNames(colors)) { const codes = colors[key]; if (codes) { const openNum = codes[0]; @@ -206,7 +207,7 @@ function styleText(format, text, options) { if (key === 'none') continue; const style = cache[key]; if (style === undefined) { - validateOneOf(key, 'format', ObjectKeys(inspect.colors)); + validateOneOf(key, 'format', ObjectGetOwnPropertyNames(inspect.colors)); } openCodes += style.openSeq; closeCodes = style.closeSeq + closeCodes; diff --git a/test/parallel/test-util-styletext.js b/test/parallel/test-util-styletext.js index 4fdf419143453c..8b0dc07b1e83c2 100644 --- a/test/parallel/test-util-styletext.js +++ b/test/parallel/test-util-styletext.js @@ -138,6 +138,29 @@ assert.throws(() => { code: 'ERR_INVALID_ARG_VALUE', }); +// Color aliases should be accepted (e.g. 'grey' is an alias for 'gray') +// See https://github.com/nodejs/node/issues/62177 +assert.strictEqual( + util.styleText('grey', 'test', { validateStream: false }), + util.styleText('gray', 'test', { validateStream: false }), +); +assert.strictEqual( + util.styleText('bgGrey', 'test', { validateStream: false }), + util.styleText('bgGray', 'test', { validateStream: false }), +); +assert.strictEqual( + util.styleText('blackBright', 'test', { validateStream: false }), + util.styleText('gray', 'test', { validateStream: false }), +); +assert.strictEqual( + util.styleText('faint', 'test', { validateStream: false }), + util.styleText('dim', 'test', { validateStream: false }), +); +assert.strictEqual( + util.styleText(['grey', 'bold'], 'test', { validateStream: false }), + util.styleText(['gray', 'bold'], 'test', { validateStream: false }), +); + assert.throws(() => { util.styleText('red', 'text', { stream: {} }); }, {