import path from "node:path"; const PLACEHOLDER_PATTERN = /(replace-with|change-me|changeme|^your[-_ ]|example|placeholder)/i; /** 判断值是否仍是示例占位符,而不是真实随机密钥。 */ export function isPlaceholderSecret(value) { return PLACEHOLDER_PATTERN.test(value.trim()); } /** * 切流前校验共享生产环境文件。 * 除必填项外,必须拒绝示例密钥,避免上线后才在 API 启动时失败。 */ export function assertReleaseEnvironment(environment) { if ( environment.NODE_ENV !== "production" || !environment.DATABASE_URL || !environment.SESSION_SECRET || !environment.DEFAULT_BRAND_ID || !environment.LOCAL_MEDIA_ROOT ) { throw new Error("Production environment is missing required values"); } if (environment.SESSION_SECRET.trim().length < 32) { throw new Error("SESSION_SECRET must contain at least 32 characters"); } if (isPlaceholderSecret(environment.SESSION_SECRET)) { throw new Error("SESSION_SECRET must not use an example placeholder"); } if (!path.isAbsolute(environment.LOCAL_MEDIA_ROOT)) { throw new Error("LOCAL_MEDIA_ROOT must be absolute"); } if (environment.API_PREFIX) { throw new Error("API_PREFIX must be empty when Traefik strips /api"); } const throttleStore = environment.CLIENT_ERROR_THROTTLE_STORE ?? "memory"; if (!["memory", "postgres"].includes(throttleStore)) { throw new Error("CLIENT_ERROR_THROTTLE_STORE must be memory or postgres"); } return new URL(environment.DATABASE_URL); }