From d47147fb6ad229b1c039aff9d0959b6e281f4df5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Tue, 14 Feb 2023 17:38:45 +0100 Subject: feat(ext/node): embed std/node into the snapshot (#17724) This commit moves "deno_std/node" in "ext/node" crate. The code is transpiled and snapshotted during the build process. During the first pass a minimal amount of work was done to create the snapshot, a lot of code in "ext/node" depends on presence of "Deno" global. This code will be gradually fixed in the follow up PRs to migrate it to import relevant APIs from "internal:" modules. Currently the code from snapshot is not used in any way, and all Node/npm compatibility still uses code from "https://deno.land/std/node" (or from the location specified by "DENO_NODE_COMPAT_URL"). This will also be handled in a follow up PRs. --------- Co-authored-by: crowlkats Co-authored-by: Divy Srivastava Co-authored-by: Yoshiya Hinosawa --- ext/node/polyfills/internal/util/debuglog.ts | 121 +++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100644 ext/node/polyfills/internal/util/debuglog.ts (limited to 'ext/node/polyfills/internal/util/debuglog.ts') diff --git a/ext/node/polyfills/internal/util/debuglog.ts b/ext/node/polyfills/internal/util/debuglog.ts new file mode 100644 index 000000000..498facbd1 --- /dev/null +++ b/ext/node/polyfills/internal/util/debuglog.ts @@ -0,0 +1,121 @@ +// 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/polyfills/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 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 }; -- cgit v1.2.3