Repository Public
1/**2 * Structured logs: one JSON object per line on stdout (PM2 collects them). Anything that looks like a3 * credential is redacted before it is written — by field name and by value shape — so a careless call4 * site cannot leak a session token, an enrolment link, a cookie or a CSRF token.5 */6const SENSITIVE_KEY = /(token|secret|cookie|authorization|password|passwd|csrf|challenge|jeton|public_?key|credential|signature|assertion|set-cookie)/i;7export const REDACTED = "[masqué]";89export function redact(value: unknown, depth = 0): unknown {10 if (typeof value === "string") {11 let s = value.length > 2000 ? `${value.slice(0, 2000)}…` : value;12 s = s.replace(/([?&](?:token|jeton|key|secret|signature)=)[^&\s"']+/gi, `$1${REDACTED}`) // secrets in query strings13 .replace(/\b(Bearer|Basic)\s+[A-Za-z0-9._~+/=-]{8,}/gi, `$1 ${REDACTED}`) // Authorization values14 .replace(/\b(atlas_studio|atlas_preauth)=[^;\s"']+/gi, `$1=${REDACTED}`) // cookie pairs15 .replace(/-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----/g, REDACTED);16 return s;17 }18 if (value instanceof Error) return { name: value.name, message: redact(value.message) };19 if (Array.isArray(value)) return depth > 5 ? "[…]" : value.slice(0, 50).map((v) => redact(v, depth + 1));20 if (value && typeof value === "object") {21 if (depth > 5) return "{…}";22 return Object.fromEntries(Object.entries(value as Record<string, unknown>).map(([k, v]) => [k, SENSITIVE_KEY.test(k) ? REDACTED : redact(v, depth + 1)]));23 }24 return value;25}2627export type Level = "debug" | "info" | "warn" | "error";28type Sink = (line: string) => void;29let sink: Sink = (line) => { process.stdout.write(`${line}\n`); };30/** Tests capture the output instead of printing it. */31export const setLogSink = (s: Sink | null) => { sink = s ?? ((line) => { process.stdout.write(`${line}\n`); }); };3233export function log(level: Level, msg: string, fields: Record<string, unknown> = {}): void {34 if (level === "debug" && process.env.ATLAS_LOG_LEVEL !== "debug") return;35 if (process.env.ATLAS_LOG_LEVEL === "silent") return;36 sink(JSON.stringify({ ts: new Date().toISOString(), level, msg, ...(redact(fields) as Record<string, unknown>) }));37}38