Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

905 B
Raw Blame
/**
 * Strip trailing slash characters from a string without using a regex
 * quantifier on uncontrolled input (avoids CodeQL `js/polynomial-redos`).
 *
 * Equivalent to `value.replace(/\/+$/, "")` but runs in O(n) guaranteed
 * time with no backtracking risk.
 */
export function stripTrailingSlashes(value: string): string {
  let end = value.length;
  while (end > 0 && value.charCodeAt(end - 1) === 0x2f /* '/' */) {
    end--;
  }
  return end === value.length ? value : value.slice(0, end);
}

/**
 * Normalize a base URL by trimming whitespace and stripping trailing slashes.
 * Handles non-string inputs gracefully (returns empty string).
 * Single source of truth — replaces per-file inline copies in config/*.ts.
 */
export function normalizeBaseUrl(value: string | null | undefined): string {
  const str = typeof value === "string" ? value : "";
  return stripTrailingSlashes(str.trim());
}