All files index.ts

100% Statements 150/150
100% Branches 37/37
100% Functions 8/8
100% Lines 150/150

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 1511x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 27x 27x 27x 246x 27x 27x 1x 1x 4x 4x 1x 1x 18x 18x 18x 18x 18x 18x 18x 18x 1x 1x 5x 3x 3x 3x 3x 3x 1x 2x 3x 3x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 1x 4x 4x 1x 1x 1x 4x 4x 4x 1x 15x 15x 1x 1x 15x 15x 5x 4x 4x 5x 5x 1x 15x 15x 1x 1x 15x 15x 8x 8x 8x 18x 18x 8x 8x 15x  
import type { LogLevel } from "@workspace/shared"
 
export type LogMetadata = Record<string, unknown>
 
export type Logger = {
  debug: (message: string, metadata?: LogMetadata) => void
  info: (message: string, metadata?: LogMetadata) => void
  warn: (message: string, metadata?: LogMetadata) => void
  error: (message: string, metadata?: LogMetadata) => void
}
 
const SENSITIVE_KEY_PARTS = [
  "api_key",
  "apikey",
  "authorization",
  "authheader",
  "cookie",
  "credential",
  "email",
  "jwt",
  "password",
  "refresh_token",
  "secret",
  "session",
  "token",
] as const
 
const REDACTED = "[redacted]"
const AUTHORIZATION_PATTERN =
  /\b(authorization\s*[:=]\s*)(["']?)(bearer\s+)?[^"'\s,;)]+\2/gi
const COOKIE_PATTERN =
  /\b(cookie|set-cookie)\s*[:=]\s*[^,\n\r]+?(?=\s+[a-z][a-z0-9+.-]*:\/\/|\s+\w+\s*[:=]|$|,)/gi
const SENSITIVE_QUERY_PARAM_PATTERN =
  /([?&](?:api[_-]?key|authorization|auth|cookie|credential|jwt|password|refresh[_-]?token|secret|session|(?:[a-z0-9]+[_-])+token|token)=)[^&#\s)]+/gi
const SENSITIVE_ASSIGNMENT_PATTERN =
  /(["']?)(api[_-]?key|authorization|auth|cookie|credential|jwt|password|refresh[_-]?token|secret|session|(?:[a-z0-9]+[_-])+token|token)\1(\s*[:=]\s*)(["']?)(?:bearer\s+)?[^"'\s,;)}[\]]+\4/gi
const URL_CREDENTIAL_PATTERN =
  /\b([a-z][a-z0-9+.-]*:\/\/)([^/@\s:]+):([^/@\s]+)@/gi
const EMAIL_PATTERN = /\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/gi
 
export function isSensitiveLogKey(key: string): boolean {
  const normalized = key.replaceAll(/[^a-zA-Z0-9]/g, "").toLowerCase()
 
  return SENSITIVE_KEY_PARTS.some((part) =>
    normalized.includes(part.replaceAll("_", ""))
  )
}
 
export function redactLogMetadata<T>(value: T): T {
  return redactValue(value, new WeakSet()) as T
}
 
export function redactLogText(value: string): string {
  return value
    .replaceAll(URL_CREDENTIAL_PATTERN, "$1[redacted]@")
    .replaceAll(SENSITIVE_ASSIGNMENT_PATTERN, "$1$2$1$3$4[redacted]$4")
    .replaceAll(AUTHORIZATION_PATTERN, "$1$2$3[redacted]$2")
    .replaceAll(COOKIE_PATTERN, "$1=[redacted]")
    .replaceAll(SENSITIVE_QUERY_PARAM_PATTERN, "$1[redacted]")
    .replaceAll(EMAIL_PATTERN, REDACTED)
}
 
export function serializeLogError(error: unknown): LogMetadata {
  if (error instanceof Error) {
    return {
      name: error.name,
      message: redactLogText(error.message),
      ...(error.stack ? { stack: redactLogText(error.stack) } : {}),
      ...(error.cause !== undefined
        ? { cause: redactLogMetadata(error.cause) }
        : {}),
    }
  }
 
  if (error && typeof error === "object") {
    return {
      value: redactLogMetadata(error),
    }
  }
 
  return {
    value: redactLogText(String(error)),
  }
}
 
export function createLogger(namespace: string): Logger {
  return {
    debug: (message, metadata) =>
      writeLog("debug", namespace, message, metadata),
    info: (message, metadata) => writeLog("info", namespace, message, metadata),
    warn: (message, metadata) => writeLog("warn", namespace, message, metadata),
    error: (message, metadata) =>
      writeLog("error", namespace, message, metadata),
  }
}
 
function writeLog(
  level: LogLevel,
  namespace: string,
  message: string,
  metadata?: LogMetadata
) {
  const payload = {
    level,
    namespace,
    message: redactLogText(message),
    ...(metadata ? { metadata: redactLogMetadata(metadata) } : {}),
  }
 
  const line = JSON.stringify(payload)
 
  if (level === "error") {
    console.error(line)
    return
  }
 
  if (level === "warn") {
    console.warn(line)
    return
  }
 
  console.log(line)
}
 
function redactValue(value: unknown, seen: WeakSet<object>): unknown {
  if (Array.isArray(value)) {
    return value.map((item) => redactValue(item, seen))
  }
 
  if (!value || typeof value !== "object") {
    if (typeof value === "string") {
      return redactLogText(value)
    }
 
    return value
  }
 
  if (seen.has(value)) {
    return "[circular]"
  }
 
  seen.add(value)
 
  return Object.fromEntries(
    Object.entries(value).map(([key, nested]) => [
      key,
      isSensitiveLogKey(key) ? REDACTED : redactValue(nested, seen),
    ])
  )
}