Skip to content

Commit bf5f3ac

Browse files
committed
util: allow wildcards in NODE_DEBUG variable
Fixes: #17605
1 parent 7bb2cc4 commit bf5f3ac

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

lib/util.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,14 +233,25 @@ function format(f) {
233233
var debugs = {};
234234
var debugEnviron;
235235

236+
const regExpSpecialChars = /[|\\{}()[\]^$+?.]/g;
237+
238+
function stringToRegExp(string) {
239+
// Escape special chars except wildcard.
240+
string = string.replace(regExpSpecialChars, '\\$&');
241+
// Transform wildcard to "match anything"
242+
string = string.replace(/\*/g, '.*');
243+
return new RegExp(`^${string}$`);
244+
}
245+
236246
function debuglog(set) {
237247
if (debugEnviron === undefined) {
238-
debugEnviron = new Set(
239-
(process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase()));
248+
debugEnviron = Array.from(new Set(
249+
(process.env.NODE_DEBUG || '').split(',').map((s) => s.toUpperCase())));
250+
debugEnviron = debugEnviron.map(stringToRegExp);
240251
}
241252
set = set.toUpperCase();
242253
if (!debugs[set]) {
243-
if (debugEnviron.has(set)) {
254+
if (debugEnviron.some((name) => name.test(set))) {
244255
var pid = process.pid;
245256
debugs[set] = function() {
246257
var msg = exports.format.apply(exports, arguments);

test/sequential/test-util-debug.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ function parent() {
4343
test('f$oo', true, 'f$oo');
4444
test('f$oo', false, 'f.oo');
4545
test('no-bar-at-all', false, 'bar');
46+
47+
test('test-abc', true, 'test-abc');
48+
test('test-a', false, 'test-abc');
49+
test('test-*', true, 'test-abc');
50+
test('test-*c', true, 'test-abc');
51+
test('test-*abc', true, 'test-abc');
52+
test('abc-test', true, 'abc-test');
53+
test('a*-test', true, 'abc-test');
54+
test('*-test', true, 'abc-test');
4655
}
4756

4857
function test(environ, shouldWrite, section) {

0 commit comments

Comments
 (0)