|
| 1 | +import { debug } from './debug-logger'; |
| 2 | +import { envToBool } from './envToBool'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Spotlight configuration option type. |
| 6 | + * - `undefined` - not configured |
| 7 | + * - `false` - explicitly disabled |
| 8 | + * - `true` - enabled with default URL (http://localhost:8969/stream) |
| 9 | + * - `string` - enabled with custom URL |
| 10 | + */ |
| 11 | +export type SpotlightConnectionOptions = boolean | string | undefined; |
| 12 | + |
| 13 | +/** |
| 14 | + * Parses a SENTRY_SPOTLIGHT environment variable value. |
| 15 | + * |
| 16 | + * Per the Spotlight spec: |
| 17 | + * - Truthy values ("true", "t", "y", "yes", "on", "1") -> true |
| 18 | + * - Falsy values ("false", "f", "n", "no", "off", "0") -> false |
| 19 | + * - Any other non-empty string -> treated as URL |
| 20 | + * - Empty string or undefined -> undefined |
| 21 | + * |
| 22 | + * @see https://develop.sentry.dev/sdk/expected-features/spotlight.md |
| 23 | + */ |
| 24 | +export function parseSpotlightEnvValue(envValue: string | undefined): SpotlightConnectionOptions { |
| 25 | + if (envValue === undefined || envValue === '') { |
| 26 | + return undefined; |
| 27 | + } |
| 28 | + |
| 29 | + // Try strict boolean parsing first |
| 30 | + const boolValue = envToBool(envValue, { strict: true }); |
| 31 | + if (boolValue !== null) { |
| 32 | + return boolValue; |
| 33 | + } |
| 34 | + |
| 35 | + // Not a boolean - treat as URL |
| 36 | + return envValue; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Resolves the final Spotlight configuration value based on the config option and environment variable. |
| 41 | + * |
| 42 | + * Precedence rules (per spec): |
| 43 | + * 1. Config `false` -> DISABLED (ignore env var, log warning) |
| 44 | + * 2. Config URL string -> USE CONFIG URL (log warning if env var also set to URL) |
| 45 | + * 3. Config `true` + Env URL -> USE ENV VAR URL (this is the key case!) |
| 46 | + * 4. Config `true` + Env bool/undefined -> USE DEFAULT URL (true) |
| 47 | + * 5. Config `undefined` -> USE ENV VAR VALUE |
| 48 | + * |
| 49 | + * @see https://develop.sentry.dev/sdk/expected-features/spotlight.md |
| 50 | + */ |
| 51 | +export function resolveSpotlightValue( |
| 52 | + optionValue: SpotlightConnectionOptions, |
| 53 | + envValue: SpotlightConnectionOptions, |
| 54 | +): SpotlightConnectionOptions { |
| 55 | + // Case 1: Config explicitly disables Spotlight |
| 56 | + if (optionValue === false) { |
| 57 | + if (envValue !== undefined) { |
| 58 | + // Per spec: MUST warn when config false ignores env var |
| 59 | + debug.warn('Spotlight disabled via config, ignoring SENTRY_SPOTLIGHT environment variable'); |
| 60 | + } |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + // Case 2: Config provides explicit URL |
| 65 | + if (typeof optionValue === 'string') { |
| 66 | + if (typeof envValue === 'string') { |
| 67 | + // Per spec: MUST warn when config URL overrides env var URL |
| 68 | + debug.warn('Spotlight config URL takes precedence over SENTRY_SPOTLIGHT environment variable'); |
| 69 | + } |
| 70 | + return optionValue; |
| 71 | + } |
| 72 | + |
| 73 | + // Case 3 & 4: Config is true - enable Spotlight |
| 74 | + if (optionValue === true) { |
| 75 | + // Per spec: If config true AND env var is URL, MUST use env var URL |
| 76 | + // This enables `spotlight: true` in code while `spotlight run` provides the URL |
| 77 | + return typeof envValue === 'string' ? envValue : true; |
| 78 | + } |
| 79 | + |
| 80 | + // Case 5: Config undefined - fully defer to env var |
| 81 | + return envValue; |
| 82 | +} |
0 commit comments