-
Notifications
You must be signed in to change notification settings - Fork 640
/
Copy pathdebuglog.ts
121 lines (103 loc) · 3.17 KB
/
debuglog.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
import { sprintf } from "../../../fmt/printf.ts";
import { inspect } from "./inspect.mjs";
// `debugImpls` and `testEnabled` are deliberately not initialized so any call
// to `debuglog()` before `initializeDebugEnv()` is called will throw.
let debugImpls: Record<string, (...args: unknown[]) => void>;
let testEnabled: (str: string) => boolean;
// `debugEnv` is initial value of process.env.NODE_DEBUG
function initializeDebugEnv(debugEnv: string) {
debugImpls = Object.create(null);
if (debugEnv) {
// This is run before any user code, it's OK not to use primordials.
debugEnv = debugEnv.replace(/[|\\{}()[\]^$+?.]/g, "\\$&")
.replaceAll("*", ".*")
.replaceAll(",", "$|^");
const debugEnvRegex = new RegExp(`^${debugEnv}$`, "i");
testEnabled = (str) => debugEnvRegex.exec(str) !== null;
} else {
testEnabled = () => false;
}
}
// Emits warning when user sets
// NODE_DEBUG=http or NODE_DEBUG=http2.
function emitWarningIfNeeded(set: string) {
if ("HTTP" === set || "HTTP2" === set) {
console.warn(
"Setting the NODE_DEBUG environment variable " +
"to '" + set.toLowerCase() + "' can expose sensitive " +
"data (such as passwords, tokens and authentication headers) " +
"in the resulting log.",
);
}
}
const noop = () => {};
function debuglogImpl(
enabled: boolean,
set: string,
): (...args: unknown[]) => void {
if (debugImpls[set] === undefined) {
if (enabled) {
emitWarningIfNeeded(set);
debugImpls[set] = function debug(...args: unknown[]) {
const msg = args.map((arg) => inspect(arg)).join(" ");
console.error(sprintf("%s %s: %s", set, String(Deno.pid), msg));
};
} else {
debugImpls[set] = noop;
}
}
return debugImpls[set];
}
// debuglogImpl depends on process.pid and process.env.NODE_DEBUG,
// so it needs to be called lazily in top scopes of internal modules
// that may be loaded before these run time states are allowed to
// be accessed.
export function debuglog(
set: string,
cb: (debug: (...args: unknown[]) => void) => void,
) {
function init() {
set = set.toUpperCase();
enabled = testEnabled(set);
}
let debug = (...args: unknown[]): void => {
init();
// Only invokes debuglogImpl() when the debug function is
// called for the first time.
debug = debuglogImpl(enabled, set);
if (typeof cb === "function") {
cb(debug);
}
return debug(...args);
};
let enabled: boolean;
let test = () => {
init();
test = () => enabled;
return enabled;
};
const logger = (...args: unknown[]) => debug(...args);
Object.defineProperty(logger, "enabled", {
get() {
return test();
},
configurable: true,
enumerable: true,
});
return logger;
}
let state = "";
if (Deno.permissions) {
state = (await Deno.permissions.query({
name: "env",
variable: "NODE_DEBUG",
})).state;
}
if (state === "granted") {
initializeDebugEnv(Deno.env.get("NODE_DEBUG") ?? "");
} else {
initializeDebugEnv("");
}
export default { debuglog };