From addc3045ed91dd6b47edc9a355d284ac2d26fef3 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 29 May 2021 10:35:41 +0200 Subject: [PATCH] lib: refactor debuglog init PR-URL: https://github.com/nodejs/node/pull/38838 Reviewed-By: Darshan Sen Reviewed-By: Anto Aravinth --- lib/internal/util/debuglog.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/internal/util/debuglog.js b/lib/internal/util/debuglog.js index b1f82b957b162a..93c741e753525f 100644 --- a/lib/internal/util/debuglog.js +++ b/lib/internal/util/debuglog.js @@ -1,11 +1,10 @@ 'use strict'; const { - FunctionPrototypeBind, ObjectCreate, ObjectDefineProperty, RegExp, - RegExpPrototypeTest, + RegExpPrototypeExec, SafeArrayIterator, StringPrototypeToLowerCase, StringPrototypeToUpperCase, @@ -13,24 +12,25 @@ const { const { inspect, format, formatWithOptions } = require('internal/util/inspect'); -// `debugs` is deliberately initialized to undefined so any call to -// debuglog() before initializeDebugEnv() is called will throw. +// `debugImpls` and `testEnabled` are deliberately not initialized so any call +// to `debuglog()` before `initializeDebugEnv()` is called will throw. let debugImpls; - -let debugEnvRegex = /^$/; let testEnabled; + // `debugEnv` is initial value of process.env.NODE_DEBUG function initializeDebugEnv(debugEnv) { debugImpls = ObjectCreate(null); if (debugEnv) { + // This is run before any user code, it's OK not to use primordials. debugEnv = debugEnv.replace(/[|\\{}()[\]^$+?.]/g, '\\$&') - .replace(/\*/g, '.*') - .replace(/,/g, '$|^') - .toUpperCase(); - debugEnvRegex = new RegExp(`^${debugEnv}$`, 'i'); + .replaceAll('*', '.*') + .replaceAll(',', '$|^'); + const debugEnvRegex = new RegExp(`^${debugEnv}$`, 'i'); + testEnabled = (str) => RegExpPrototypeExec(debugEnvRegex, str) !== null; + } else { + testEnabled = () => false; } - testEnabled = FunctionPrototypeBind(RegExpPrototypeTest, null, debugEnvRegex); } // Emits warning when user sets