/** * Whether the debugbar exists at all. * * Everything else in the package asks here first, and the answer in * production is no before any configuration is read. That ordering is the * guarantee: `enabled: true` in `config/debugbar.ts` cannot reach a * production process, because production is decided from the environment and * returns before the config is looked at. */ import type { DebugbarConfig } from '@stacksjs/types' import type { ResolvedDebugbarConfig } from './types' import process from 'node:process' export interface EnvironmentInputs { /** `process.env`, or a stand-in for it. */ env?: Record /** `config.app.env`, when config has loaded. */ appEnv?: string } const PRODUCTION_NAMES = new Set(['production', 'prod', 'live']) const DEVELOPMENT_NAMES = new Set(['local', 'development', 'dev']) function normalized(value: unknown): string { return typeof value === 'string' ? value.trim().toLowerCase() : '' } /** * True when anything says this process is production: `APP_ENV`, the loaded * config, or `NODE_ENV` when `APP_ENV` says nothing. * * Any one of them is enough. A process whose environment variable and config * disagree is treated as production, since being wrong in that direction * costs a developer a toolbar and being wrong in the other costs an app its * session tokens. */ export function isProductionEnvironment(inputs: EnvironmentInputs = {}): boolean { const env = inputs.env ?? process.env const appEnv = normalized(env.APP_ENV) if (PRODUCTION_NAMES.has(appEnv) || PRODUCTION_NAMES.has(normalized(inputs.appEnv))) return true return !appEnv && PRODUCTION_NAMES.has(normalized(env.NODE_ENV)) } /** The environment name the app reports, lowercased, or '' when it reports none. */ export function environmentName(inputs: EnvironmentInputs = {}): string { const env = inputs.env ?? process.env return normalized(env.APP_ENV) || normalized(inputs.appEnv) || normalized(env.NODE_ENV) } /** * `APP_DEBUG` set to a false value. Read from the environment and not from * `config.app.debug`: the app template defaults that to false, so it says * "nobody set it" far more often than "somebody turned debugging off". */ function debugDisabled(inputs: EnvironmentInputs): boolean { const raw = normalized((inputs.env ?? process.env).APP_DEBUG) return raw === 'false' || raw === '0' || raw === 'off' || raw === 'no' } /** * Whether the debugbar should run in this process. * * Production: never. Otherwise `enabled: false` turns it off, `enabled: true` * turns it on, and unset means on in `local` and `development` while * `APP_DEBUG` is not false. */ export function isDebugbarEnabled(config: DebugbarConfig | undefined, inputs: EnvironmentInputs = {}): boolean { if (isProductionEnvironment(inputs)) return false if (config?.enabled === false) return false if (config?.enabled === true) return true return DEVELOPMENT_NAMES.has(environmentName(inputs)) && !debugDisabled(inputs) } export function resolveDebugbarConfig(config: DebugbarConfig | undefined, defaultStoragePath: string): ResolvedDebugbarConfig { const queries = config?.queries ?? {} return { ...config, storage: { path: config?.storage?.path || defaultStoragePath, maxRequests: positive(config?.storage?.maxRequests, 200), }, except: Array.isArray(config?.except) ? config.except.filter(pattern => typeof pattern === 'string') : [], queries: { slowThresholdMs: nonNegative(queries.slowThresholdMs, 100), nPlusOneThreshold: Math.max(2, positive(queries.nPlusOneThreshold, 5)), backtrace: queries.backtrace !== false, maxQueries: positive(queries.maxQueries, 500), }, } } function positive(value: unknown, fallback: number): number { const number = Number(value) return Number.isFinite(number) && number >= 1 ? Math.floor(number) : fallback } function nonNegative(value: unknown, fallback: number): number { const number = Number(value) return Number.isFinite(number) && number >= 0 ? number : fallback } /** Paths the debugbar never collects, whatever the app configured. */ const BUILT_IN_EXCEPT = /^\/(?:__debugbar|_stx|__deps|__hmr|__stx|@|node_modules)(?:\/|$)|^\/favicon\.|\.(?:js|mjs|css|map|png|jpe?g|gif|svg|ico|webp|avif|woff2?|ttf|otf|eot|mp4|webm|mp3|wasm|txt|xml|webmanifest)$/i export function isExcludedPath(path: string, except: readonly string[]): boolean { if (BUILT_IN_EXCEPT.test(path)) return true for (const pattern of except) { if (pattern.endsWith('*') ? path.startsWith(pattern.slice(0, -1)) : path === pattern) return true } return false }