// Leveled logger. Outputs to console with [AutistMask] prefix. // Level is DEBUG when the compile-time DEBUG constant is true or the runtime // debugMode state flag is enabled. The runtime flag is checked lazily so it // responds immediately when toggled in settings. const { DEBUG } = require("./constants"); const LEVELS = { debug: 0, info: 1, warn: 2, error: 3 }; // Runtime debug mode flag — set by settings.js when the user toggles debug // mode via the easter egg. Kept here as a simple mutable reference so it can // be updated without circular dependency issues with state.js. let _runtimeDebug = false; function setRuntimeDebug(enabled) { _runtimeDebug = enabled; } function isDebug() { return DEBUG || _runtimeDebug; } function emit(level, method, args) { const threshold = isDebug() ? LEVELS.debug : LEVELS.info; if (LEVELS[level] >= threshold) { console[method]("[AutistMask]", ...args); } } const log = { debugf(...args) { emit("debug", "log", args); }, infof(...args) { emit("info", "log", args); }, warnf(...args) { emit("warn", "warn", args); }, errorf(...args) { emit("error", "error", args); }, }; // The origin (scheme, host and port) of a URL, for logging in place of the // URL: RPC providers put API keys in the path or the query string, and a URL // can carry a user name and password, which the origin leaves out. A URL that // does not parse gives "", so logging never stops a request. function urlOrigin(url) { try { return new URL(url).origin; } catch { return ""; } } // Fetch wrapper that debug-logs every request and response. It logs the // URL's origin and, for a JSON-RPC body, the method name: never the full URL // or body, which can carry an API key or a signed transaction. async function debugFetch(url, opts) { const method = (opts && opts.method) || "GET"; const origin = urlOrigin(url); let rpcMethod = ""; try { rpcMethod = JSON.parse(opts.body).method || ""; } catch { // no body, or a body that is not JSON } log.debugf("fetch →", method, origin, rpcMethod); const resp = await fetch(url, opts); log.debugf("fetch ←", resp.status, origin); return resp; } module.exports = { log, debugFetch, urlOrigin, setRuntimeDebug, isDebug };