Skip to content

Commit

Permalink
lib: refactor debuglog init
Browse files Browse the repository at this point in the history
PR-URL: #38838
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Anto Aravinth <anto.aravinth.cse@gmail.com>
  • Loading branch information
aduh95 authored and danielleadams committed Jun 17, 2021
1 parent d685b5b commit addc304
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions lib/internal/util/debuglog.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
'use strict';

const {
FunctionPrototypeBind,
ObjectCreate,
ObjectDefineProperty,
RegExp,
RegExpPrototypeTest,
RegExpPrototypeExec,
SafeArrayIterator,
StringPrototypeToLowerCase,
StringPrototypeToUpperCase,
} = primordials;

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
Expand Down

0 comments on commit addc304

Please sign in to comment.