1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-14 19:37:59 -05:00
denoland-deno/ext/node/polyfills/internal/util/debuglog.ts
Bartek Iwańczuk ce75e31625 refactor(core): include_js_files! 'dir' option doesn't change specifiers (#18019)
This commit changes "include_js_files!" macro from "deno_core"
in a way that "dir" option doesn't cause specifiers to be rewritten 
to include it.

Example:
```
include_js_files! {
  dir "js",
  "hello.js",
}
```

The above definition required embedders to use:
`import ... from "internal:<ext_name>/js/hello.js"`. 
But with this change, the "js" directory in which the files are stored
is an implementation detail, which for embedders results in: 
`import ... from "internal:<ext_name>/hello.js"`.

The directory the files are stored in, is an implementation detail and 
in some cases might result in a significant size difference for the 
snapshot. As an example, in "deno_node" extension, we store the 
source code in "polyfills" directory; which resulted in each specifier 
to look like "internal:deno_node/polyfills/<module_name>", but with 
this change it's "internal:deno_node/<module_name>". 

Given that "deno_node" has over 100 files, many of them having 
several import specifiers to the same extension, this change removes
10 characters from each import specifier.
2023-03-10 12:47:26 +09:00

121 lines
3.2 KiB
TypeScript

// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
import { inspect } from "internal:deno_node/internal/util/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("%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 debugEnv;
/* TODO(kt3k): enable initializing debugEnv.
It's not possible to access env var when snapshotting.
try {
debugEnv = Deno.env.get("NODE_DEBUG") ?? "";
} catch (error) {
if (error instanceof Deno.errors.PermissionDenied) {
debugEnv = "";
} else {
throw error;
}
}
*/
initializeDebugEnv(debugEnv);
export default { debuglog };