Refactor test-options.js; rename globalKeysSample to pickEnvSample and enhance environment variable sampling logic for improved clarity and safety

This commit is contained in:
2026-01-05 19:55:39 +03:00
parent c140b08e12
commit 2fce3a192a

View File

@@ -15,32 +15,96 @@ function safeStringify(obj) {
); );
} }
function globalKeysSample(limit = 200) { function pickEnvSample() {
try { try {
const keys = Object.getOwnPropertyNames(globalThis).sort(); const env = (typeof process !== "undefined" && process && process.env) ? process.env : null;
return keys.slice(0, limit); 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 { } catch {
return []; return [];
} }
} }
const report = { // Safe "typeof" probes: never throws even if variable doesn't exist
types: { const probes = {
$content: typeof $content, $content: typeof $content,
$files: typeof $files, $files: typeof $files,
$options: typeof $options, $options: typeof $options,
ProxyUtils: typeof ProxyUtils,
produceArtifact: typeof produceArtifact, $params: typeof $params,
}, $args: typeof $args,
values: { $arguments: typeof $arguments,
$contentPreview: (typeof $content === "string" ? $content.slice(0, 300) : $content), $argument: typeof $argument,
$contentLength: (typeof $content === "string" ? $content.length : null), $argv: typeof $argv,
$files: $files ?? null,
$options: $options ?? null, $ctx: typeof $ctx,
}, $context: typeof $context,
globals: { $request: typeof $request,
globalThisKeysSample: globalKeysSample(), $req: typeof $req,
} $url: typeof $url,
$scriptUrl: typeof $scriptUrl,
$script_url: typeof $script_url,
ProxyUtils: typeof ProxyUtils,
produceArtifact: typeof produceArtifact,
process: typeof process,
}; };
$content = safeStringify(report); 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);