All files logger.js

100% Statements 116/116
91.52% Branches 54/59
100% Functions 14/14
100% Lines 116/116

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 1178x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 172x 172x 8x 8x 8x 4x 1x 1x 4x 4x 4x 3x 2x 1x 4x 3x 4x 8x 8x 8x 5x 4x 5x 8x 8x 8x 8x 8x 8x 8x 232x 232x 8x 8x 8x 3x 3x 19x 19x 15x 12x 19x 3x 3x 8x 8x 8x 8x 174x 174x 174x 174x 8x 8x 1607x 1607x 8x 8x 27x 27x 27x 27x 8x 8x 1498x 27x 1498x 1498x 1x 1x 1498x 8x 8x 9x 9x 8x 8x 160x 160x 8x 8x 366x 366x 8x 8x 963x 963x 8x 8x 8x 8x 965x 555x 947x 501x 501x 945x 6x 6x 965x  
// Copyright 2026 优谷雅言 open.shengzhiai.com. Licensed under the Apache License, Version 2.0.
// Logging with levels and redaction (DESIGN 2.7). Never logs secretKey, signature, token or audio.
 
/** Log levels, from quiet to verbose. */
export const LogLevel = Object.freeze({
  OFF: 'OFF',
  ERROR: 'ERROR',
  WARN: 'WARN',
  INFO: 'INFO',
  DEBUG: 'DEBUG',
});
 
const RANK = { OFF: 0, ERROR: 1, WARN: 2, INFO: 3, DEBUG: 4 };
 
/** True when level is one of LogLevel. */
export function isLogLevel(level) {
  return typeof level === 'string' && Object.prototype.hasOwnProperty.call(RANK, level);
}
 
/** Default sink: the platform console. */
export function consoleLogSink(level, tag, message, error) {
  const c = typeof console !== 'undefined' ? console : null;
  /* c8 ignore next */
  if (!c) return;
  const line = `[yugu-web-sdk] ${level} ${tag}: ${message}`;
  let fn;
  if (level === LogLevel.ERROR) fn = c.error;
  else if (level === LogLevel.WARN) fn = c.warn;
  else if (level === LogLevel.INFO) fn = c.info || c.log;
  else fn = c.debug || c.log;
  if (error != null) fn.call(c, line, error);
  else fn.call(c, line);
}
 
/** appKey as its first 4 characters plus ***. */
export function maskAppKey(appKey) {
  if (appKey == null || appKey === '') return '';
  return String(appKey).slice(0, 4) + '***';
}
 
const SECRET_QUERY = /([?&](?:signature|sig|token|access_token|secretKey)=)[^&#\s"']*/gi;
const APPKEY_QUERY = /([?&]appKey=)([^&#\s"']{0,4})[^&#\s"']*/gi;
const BEARER = /(Bearer\s+)[A-Za-z0-9\-._~+/]+=*/g;
 
/** Mask credentials inside a URL or any text that may contain one. */
export function redactUrl(text) {
  return String(text).replace(SECRET_QUERY, '$1***').replace(APPKEY_QUERY, '$1$2***').replace(BEARER, '$1***');
}
 
/** Copy of a header map with credentials masked. */
export function redactHeaders(headers) {
  const out = {};
  for (const [k, v] of Object.entries(headers || {})) {
    const lk = k.toLowerCase();
    if (lk === 'x-signature' || lk === 'authorization') out[k] = lk === 'authorization' ? 'Bearer ***' : '***';
    else if (lk === 'x-app-key') out[k] = maskAppKey(v);
    else out[k] = v;
  }
  return out;
}
 
/** Internal logger. Sink failures never reach the caller. */
export class Logger {
  constructor(level, sink, secrets) {
    this.level = level || LogLevel.WARN;
    this.sink = typeof sink === 'function' ? sink : consoleLogSink;
    this.secrets = (secrets || []).filter((s) => typeof s === 'string' && s.length >= 4);
  }
 
  enabled(level) {
    return RANK[level] > 0 && RANK[level] <= RANK[this.level];
  }
 
  scrub(message) {
    let m = redactUrl(message);
    for (const s of this.secrets) m = m.split(s).join('***');
    return m;
  }
 
  log(level, tag, message, error) {
    if (!this.enabled(level)) return;
    try {
      this.sink(level, tag, this.scrub(message), error == null ? null : error);
    } catch (_) {
      /* a failing sink must not break the SDK */
    }
  }
 
  error(tag, message, error) {
    this.log(LogLevel.ERROR, tag, message, error);
  }
 
  warn(tag, message, error) {
    this.log(LogLevel.WARN, tag, message, error);
  }
 
  info(tag, message, error) {
    this.log(LogLevel.INFO, tag, message, error);
  }
 
  debug(tag, message, error) {
    this.log(LogLevel.DEBUG, tag, message, error);
  }
}
 
/** Calls an optional hook on the metrics EventListener; failures are logged, never thrown. */
export function callHook(target, name, args, logger) {
  if (!target) return;
  const fn = target[name];
  if (typeof fn !== 'function') return;
  try {
    fn.apply(target, args);
  } catch (e) {
    if (logger) logger.error('event', `eventListener.${name} threw`, e);
  }
}