File size: 1,902 Bytes
5c0175e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// 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",
];