VR-MEDIA / src /lib /env.ts
Zero124's picture
VR Media Server 2.0 — latest build (custom Blomp thumbnail auto-detection + full pipeline)
5c0175e
Raw History Blame Contribute Delete
1.9 kB
// Runtime environment access.
//
// WHY THIS FILE EXISTS
// --------------------
// Next.js replaces `process.env.SOME_NAME` with a literal string at BUILD time
// for any code that ends up in the Edge / middleware bundle. Hugging Face Space
// secrets are NOT available during the Docker build - HF only injects them into
// the container at runtime - so that build-time replacement freezes them to
// "undefined", and middleware then behaves as if no key had ever been set.
//
// Reading through a computed key (`bag[name]`) is documented by Next.js as a
// pattern that is NOT inlined, so every call looks the value up in the live
// process environment. That is exactly what we need: one image, many runtimes.
const bag = process.env as unknown as Record<string, string | undefined>;
/** Trimmed value, or null when unset / blank. */
export function readEnv(name: string): string | null {
const raw = bag[name];
if (typeof raw !== "string") return null;
const trimmed = raw.trim();
return trimmed.length > 0 ? trimmed : null;
}
/** Boolean flag: 1/true/yes/on are true, 0/false/no/off are false. */
export function readEnvFlag(name: string, fallback = false): boolean {
const raw = readEnv(name);
if (raw === null) return fallback;
const value = raw.toLowerCase();
if (value === "1" || value === "true" || value === "yes" || value === "on") return true;
if (value === "0" || value === "false" || value === "no" || value === "off") return false;
return fallback;
}
/** True when this variable is visible to the runtime executing this code. */
export function envIsVisible(name: string): boolean {
return readEnv(name) !== null;
}
/** Names checked by the sign-in diagnostics panel. */
export const AUTH_ENV_NAMES = [
"APP_API_KEY",
"APP_PASSWORD",
"OFFCLOUD_API_KEY",
"AUTH_SECRET",
"ALLOW_OFFCLOUD_KEY_LOGIN",
];