overrides.ts58 lines · main
1import enabledFeaturesRaw from './enabled-features.json' with { type: 'json' }
2
3const knownFeatureKeys = Object.keys(enabledFeaturesRaw).filter((key) => key !== '$schema')
4
5const ENV_PREFIX = 'ENABLED_FEATURES_'
6
7// Server-only env var that short-circuits feature resolution; handled by
8// isFeatureEnabled directly, not by this parser.
9const RESERVED_ENV_NAMES = new Set<string>(['ENABLED_FEATURES_OVERRIDE_DISABLE_ALL'])
10
11function featureKeyToEnvName(feature: string): string {
12 return ENV_PREFIX + feature.toUpperCase().replace(/[^A-Z0-9]/g, '_')
13}
14
15function parseBooleanEnv(raw: string): boolean | null {
16 const normalized = raw.trim().toLowerCase()
17 if (normalized === 'true') return true
18 if (normalized === 'false') return false
19 return null
20}
21
22/**
23 * Returns the list of feature keys disabled by ENABLED_FEATURES_* env vars.
24 * Server-only — these are not NEXT_PUBLIC_* and must be read at request time.
25 * Invalid values and prefixed env vars that don't match a known feature are
26 * logged and ignored.
27 */
28export function getEnabledFeaturesOverrideDisabledList(
29 env: Record<string, string | undefined>
30): string[] {
31 const expected = new Map<string, string>()
32 for (const key of knownFeatureKeys) {
33 expected.set(featureKeyToEnvName(key), key)
34 }
35
36 const disabled: string[] = []
37 for (const [envName, featureKey] of expected) {
38 const raw = env[envName]
39 if (raw === undefined || raw === '') continue
40 const parsed = parseBooleanEnv(raw)
41 if (parsed === null) {
42 console.warn(
43 `[enabled-features] ${envName} must be "true" or "false" (got "${raw}"); ignoring.`
44 )
45 continue
46 }
47 if (parsed === false) disabled.push(featureKey)
48 }
49
50 for (const envName of Object.keys(env)) {
51 if (!envName.startsWith(ENV_PREFIX)) continue
52 if (expected.has(envName)) continue
53 if (RESERVED_ENV_NAMES.has(envName)) continue
54 console.warn(`[enabled-features] ${envName} does not match any known feature; ignoring.`)
55 }
56
57 return disabled
58}