| 1234567891011121314151617181920212223242526 |
- const SECRET_PATTERN = /(authorization|api[-_]?key|token|password|secret|dsn|database_url|connection_string)/i;
- export function redact(value: unknown, depth = 0): unknown {
- if (depth > 8) return "[DEPTH_LIMIT]";
- if (Array.isArray(value)) return value.map((item) => redact(item, depth + 1));
- if (value && typeof value === "object") {
- return Object.fromEntries(
- Object.entries(value as Record<string, unknown>).map(([key, item]) => [
- key,
- SECRET_PATTERN.test(key) ? "[REDACTED]" : redact(item, depth + 1)
- ])
- );
- }
- if (typeof value === "string") {
- return value
- .replace(/Bearer\s+[A-Za-z0-9._~+\/-]+/gi, "Bearer [REDACTED]")
- .replace(/(postgres(?:ql)?:\/\/)[^\s@]+@/gi, "$1[REDACTED]@");
- }
- return value;
- }
- export function pretty(value: unknown): string {
- if (value === undefined || value === null || value === "") return "无记录";
- if (typeof value === "string") return String(redact(value));
- return JSON.stringify(redact(value), null, 2);
- }
|