import "reflect-metadata"; import { plainToInstance } from "class-transformer"; import { IsBoolean, IsEnum, IsInt, IsString, Max, Min, validateSync, } from "class-validator"; import { appPackage } from "../common/app-package"; import { durationSeconds } from "../common/interval"; export enum TraceMode { On = "on", RetainOnFailure = "retain-on-failure", Off = "off", } export enum LogMode { Console = "console", Debug = "debug", } export class AppConfig { /** `production` in the Docker image; also selects the default CORS origins. */ @IsString() NODE_ENV: string = "development"; /** Comma-separated origins allowed to call the API cross-origin; empty = default for NODE_ENV. */ @IsString() CORS_ORIGINS: string = ""; @IsInt() @Min(1) @Max(65535) PORT: number = 3000; @IsString() APP_NAME: string = appPackage().name; @IsString() APP_VERSION: string = appPackage().version; /** Route application diagnostics through `debug` instead of the console. */ @IsEnum(LogMode) LOG_MODE: LogMode = LogMode.Console; @IsString() DB_PATH: string = "data/liqa.db"; @IsInt() @Min(1) SESSION_IDLE_TIMEOUT_MINUTES: number = 30; @IsInt() @Min(1) SESSION_DELETE_CLOSED_DAYS: number = 7; @IsString() FILES_DIR: string = "data/files"; @IsInt() @Min(1) FILE_RUN_EXPIRATION_DAYS: number = 7; @IsString() FILE_CLEANUP_CRON: string = "0 3 * * *"; /** Directory with the built client to serve at `/`; empty to not serve it. */ @IsString() CLIENT_DIR: string = ""; @IsEnum(TraceMode) // Off by default: traces with screenshots and snapshots easily reach ~10 MB per run PLAYWRIGHT_TRACE_MODE: TraceMode = TraceMode.Off; /** Run log data blocks over this many bytes (pretty-printed) start collapsed in the UI. */ @IsInt() @Min(0) RUN_LOG_DATA_COLLAPSE_BYTES: number = 256; /** Run log data blocks over this many lines (pretty-printed) start collapsed in the UI. */ @IsInt() @Min(0) RUN_LOG_DATA_COLLAPSE_LINES: number = 5; /** Authentication is opt-in so existing deployments remain unchanged. */ @IsBoolean() AUTH_ENABLED: boolean = false; /** Required only when AUTH_ENABLED is true. */ @IsString() AUTH_JWT_SECRET: string = ""; /** Cookie name used for browser sessions. */ @IsString() AUTH_COOKIE_NAME: string = "liqa_auth"; @IsString() AUTH_JWT_EXPIRES_IN: string = "24h"; @IsString() AUTH_REVOKED_TOKEN_CLEANUP_CRON: string = "0 3 * * *"; } export function validateAppConfig(config: Record): AppConfig { const authEnabled = config.AUTH_ENABLED; if ( authEnabled !== undefined && authEnabled !== true && authEnabled !== false && authEnabled !== "true" && authEnabled !== "false" ) { throw new Error("AUTH_ENABLED must be either true or false."); } const normalized = { ...config, AUTH_ENABLED: authEnabled === true || authEnabled === "true", }; const validated = plainToInstance(AppConfig, normalized, { enableImplicitConversion: true, }); const errors = validateSync(validated, { skipMissingProperties: false }); if (errors.length > 0) { throw new Error(errors.toString()); } if (validated.AUTH_ENABLED && !validated.AUTH_JWT_SECRET.trim()) { throw new Error("AUTH_JWT_SECRET must be set when AUTH_ENABLED=true."); } if ( validated.AUTH_ENABLED && durationSeconds(validated.AUTH_JWT_EXPIRES_IN) === null ) { throw new Error( "AUTH_JWT_EXPIRES_IN must be a positive duration such as 30m, 1h, or 24h.", ); } if ( validated.AUTH_ENABLED && !validated.AUTH_REVOKED_TOKEN_CLEANUP_CRON.trim() ) { throw new Error( "AUTH_REVOKED_TOKEN_CLEANUP_CRON must be set when AUTH_ENABLED=true.", ); } return validated; }