function safeStringify(obj) { const seen = new WeakSet(); return JSON.stringify( obj, (k, v) => { if (typeof v === "object" && v !== null) { if (seen.has(v)) return "[Circular]"; seen.add(v); } if (typeof v === "function") return `[Function: ${v.name || "anonymous"}]`; if (typeof v === "bigint") return v.toString(); return v; }, 2 ); } function pickEnvSample() { try { const env = (typeof process !== "undefined" && process && process.env) ? process.env : null; if (!env) return null; // only show safe-ish keys, no full dump const keys = Object.keys(env).sort(); const filtered = keys.filter(k => k.toLowerCase().includes("sub") || k.toLowerCase().includes("store") || k.toLowerCase().includes("script") || k.toLowerCase().includes("url") || k.toLowerCase().includes("option") || k.toLowerCase().includes("param") ); const sample = {}; for (const k of filtered.slice(0, 50)) sample[k] = env[k]; return { keysCount: keys.length, filteredKeys: filtered.slice(0, 100), sample }; } catch (e) { return { error: String(e) }; } } function getGlobalDollarKeys() { try { return Object.getOwnPropertyNames(globalThis).filter(k => k.startsWith("$")).sort(); } catch { return []; } } // Safe "typeof" probes: never throws even if variable doesn't exist const probes = { $content: typeof $content, $files: typeof $files, $options: typeof $options, $params: typeof $params, $args: typeof $args, $arguments: typeof $arguments, $argument: typeof $argument, $argv: typeof $argv, $ctx: typeof $ctx, $context: typeof $context, $request: typeof $request, $req: typeof $req, $url: typeof $url, $scriptUrl: typeof $scriptUrl, $script_url: typeof $script_url, ProxyUtils: typeof ProxyUtils, produceArtifact: typeof produceArtifact, process: typeof process, }; const values = {}; function maybeSet(name, getter) { try { const v = getter(); // Avoid huge outputs if (typeof v === "string") values[name] = v.length > 800 ? v.slice(0, 800) + "…(truncated)" : v; else values[name] = v; } catch (e) { values[name] = { error: String(e) }; } } maybeSet("$options", () => (typeof $options !== "undefined" ? $options : null)); maybeSet("$params", () => (typeof $params !== "undefined" ? $params : null)); maybeSet("$args", () => (typeof $args !== "undefined" ? $args : null)); maybeSet("$arguments", () => (typeof $arguments !== "undefined" ? $arguments : null)); maybeSet("$argument", () => (typeof $argument !== "undefined" ? $argument : null)); maybeSet("$ctx", () => (typeof $ctx !== "undefined" ? $ctx : null)); maybeSet("$request", () => (typeof $request !== "undefined" ? $request : null)); maybeSet("$url", () => (typeof $url !== "undefined" ? $url : null)); maybeSet("$scriptUrl", () => (typeof $scriptUrl !== "undefined" ? $scriptUrl : null)); maybeSet("$script_url", () => (typeof $script_url !== "undefined" ? $script_url : null)); maybeSet("$contentPreview", () => (typeof $content === "string" ? $content.slice(0, 300) : $content)); maybeSet("$contentLength", () => (typeof $content === "string" ? $content.length : null)); maybeSet("$files", () => (typeof $files !== "undefined" ? $files : null)); const report = { probes, values, globalDollarKeys: getGlobalDollarKeys(), envSample: pickEnvSample(), }; $content = safeStringify(report);