From 52f4e2c40944692944a7957016b70626a5f2bd94 Mon Sep 17 00:00:00 2001 From: "GBra 4.669" <35969802+GBra-4-669@users.noreply.github.com> Date: Mon, 23 Nov 2026 01:14:55 +0100 Subject: [PATCH] util: require validateStream === false for the styleText fast path Signed-off-by: GBra 4.669 <35969802+GBra-4-669@users.noreply.github.com> --- benchmark/util/style-text.js | 2 +- lib/util.js | 4 ++-- test/parallel/test-util-styletext.js | 13 +++++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/benchmark/util/style-text.js b/benchmark/util/style-text.js index c50a225fd393..a7545ab42731 100644 --- a/benchmark/util/style-text.js +++ b/benchmark/util/style-text.js @@ -21,7 +21,7 @@ const bench = common.createBenchmark(main, { // '#rotating' cycles through kHexColorCount distinct colors to simulate // the high-miss-rate / large-cache scenario (e.g. user-randomised colors). format: ['red', 'italic', 'invalid', '#ff0000', '#rotating'], - validateStream: [1, 0], + validateStream: [true, false], n: [1e3], }); diff --git a/lib/util.js b/lib/util.js index 02e205c8e16c..d2857f0c5570 100644 --- a/lib/util.js +++ b/lib/util.js @@ -247,8 +247,8 @@ function rgbToAnsi24Bit(r, g, b) { function styleText(format, text, options) { const validateStream = options?.validateStream ?? true; - // Fast path: single format string with validateStream=false - if (!validateStream && typeof format === 'string' && typeof text === 'string') { + // Fast path: single format string with validateStream === false + if (validateStream === false && typeof format === 'string' && typeof text === 'string') { const cache = getStyleCache(); if (format === 'none') return text; const style = cache[format]; diff --git a/test/parallel/test-util-styletext.js b/test/parallel/test-util-styletext.js index ba81aae06d83..b1518d7a3a73 100644 --- a/test/parallel/test-util-styletext.js +++ b/test/parallel/test-util-styletext.js @@ -36,6 +36,19 @@ assert.throws(() => { code: 'ERR_INVALID_ARG_VALUE', }); +for (const validateStream of [0, 0n, '', NaN, 1, 'false', {}, []]) { + assert.throws(() => { + util.styleText('red', 'test', { validateStream }); + }, { + code: 'ERR_INVALID_ARG_TYPE', + }, `validateStream=${String(validateStream)}`); +} + +assert.strictEqual( + util.styleText('red', 'test', { validateStream: false, stream: {} }), + '\u001b[31mtest\u001b[39m', +); + assert.strictEqual( util.styleText('red', 'test', { validateStream: false }), '\u001b[31mtest\u001b[39m',