Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

10.8 KB
Raw Blame
/**
 * Pure env-string coercion helpers shared by the whole config layer (derive.ts,
 * env.schema.ts). Kept free of imports so units can test them in isolation.
 *
 * Each helper reproduces a coercion family that already exists in the codebase.
 * Parity is law: do NOT "fix" a family's quirks here (e.g. `numberOr` treating
 * "0" as unset) — call sites were written against the current semantics.
 */

const TRUE_VALUES = new Set(['true', '1', 'on', 'yes']);
const FALSE_VALUES = new Set(['false', '0', 'off', 'no']);

/**
 * Unified boolean coercion for env switches — the ONE deliberate departure from
 * legacy parity: historically each site accepted a different literal ('true'
 * vs '1' vs 'on'), so operators guessed. Now every boolean-like variable
 * accepts true/1/on/yes and false/0/off/no (any casing, padded ok) and derives
 * to a real boolean. Anything else — including unset and blank — returns
 * undefined so the field's default applies; out-of-family values are rejected
 * at boot by env.schema.ts anyway (undefined here is the safe fallback for
 * paths that skip validation, e.g. tests).
 */
export function parseBool(raw: string | undefined): boolean | undefined {
  if (raw === undefined) return undefined;
  const v = raw.trim().toLowerCase();
  if (TRUE_VALUES.has(v)) return true;
  if (FALSE_VALUES.has(v)) return false;
  return undefined;
}

/** `Number(raw) || fallback` — NaN, 0 and '' all fall back (PORT, plugin limits). */
export function numberOr(raw: string | undefined, fallback: number): number {
  return Number(raw) || fallback;
}

/** Finite and > 0, else fallback (IDEMPOTENCY_TTL_SECONDS, OVERPASS_TIMEOUT_MS, BACKUP_*). */
export function positiveNumberOr(raw: string | undefined, fallback: number): number {
  const n = Number(raw);
  return Number.isFinite(n) && n > 0 ? n : fallback;
}

/** `Number.parseInt` finite and > 0, else fallback (MCP_MAX_SESSION_PER_USER, MCP_RATE_LIMIT). */
export function positiveIntOr(raw: string | undefined, fallback: number): number {
  const n = Number.parseInt(raw ?? '');
  return Number.isFinite(n) && n > 0 ? n : fallback;
}

/** Integer ≥ 0 (zero is meaningful — e.g. retries disabled), else the fallback. */
export function nonNegativeIntOr(raw: string | undefined, fallback: number): number {
  if (raw === undefined || raw.trim() === '') return fallback;
  const n = Number.parseInt(raw, 10);
  return Number.isFinite(n) && n >= 0 ? n : fallback;
}

/** Comma-split + trim, null when unset — the websocket ALLOWED_ORIGINS variant (no empty-entry filter). */
export function csvList(raw: string | undefined): string[] | null {
  return raw ? raw.split(',').map((o) => o.trim()) : null;
}

/** Comma-split + trim + drop empties — the CORS ALLOWED_ORIGINS variant (globalMiddleware). */
export function csvListFiltered(raw: string | undefined): string[] | null {
  return raw
    ? raw
        .split(',')
        .map((o) => o.trim())
        .filter(Boolean)
    : null;
}

/** Strip ALL trailing slashes (`/\/+$/`) — app-url.getAppUrl / TRANSIT_API_URL variant.
 *  Scanned rather than replaced: `/\/+$/` re-walks the slash run from every start
 *  position, which is quadratic on a slash-heavy value. Same result, same quirk. */
export function stripTrailingSlashes(value: string): string {
  let end = value.length;
  while (end > 0 && value.charCodeAt(end - 1) === 47 /* '/' */) end--;
  return value.slice(0, end);
}

const DURATION_UNITS_MS: Record<string, number> = {
  ms: 1,
  s: 1000,
  m: 60_000,
  h: 3_600_000,
  d: 86_400_000,
  w: 604_800_000,
  y: 31_557_600_000,
};

/**
 * ms-style duration strings ('1h', '7d', '30d', …) → milliseconds, null when
 * invalid. Same grammar as the SESSION_DURATION parsing in src/config.ts.
 */
export function parseDurationMs(value: string): number | null {
  const m = /^(\d+(?:\.\d+)?)\s*(ms|s|m|h|d|w|y)?$/i.exec(value.trim());
  if (!m) return null;
  const n = Number.parseFloat(m[1]);
  if (!Number.isFinite(n) || n <= 0) return null;
  return n * DURATION_UNITS_MS[(m[2] || 'ms').toLowerCase()];
}

/**
 * Session idle TTL in SECONDS via MCP_SESSION_TTL, default 1 hour, clamped to
 * 24h so a milliseconds-value typo can't produce a 1000-hour session.
 */
export function resolveSessionTtlMs(raw: string | undefined): number {
  const parsed = Number.parseInt(raw ?? '');
  return Number.isFinite(parsed) && parsed > 0 ? Math.min(parsed, 24 * 60 * 60) * 1000 : 60 * 60 * 1000;
}

/**
 * SSE keep-alive interval in SECONDS via MCP_SSE_KEEPALIVE, default 25s
 * (below common proxy idle timeouts like nginx-ingress's 60s). 0 disables.
 */
export function resolveKeepaliveMs(raw: string | undefined): number {
  const parsed = Number.parseInt(raw ?? '');
  return Number.isFinite(parsed) && parsed >= 0 ? parsed * 1000 : 25_000;
}

/** SQLite's journal_mode vocabulary. WAL is the default and the right choice on local disk; DELETE and TRUNCATE are the ones that survive network storage (Azure App Service, SMB/NFS volumes), where WAL's shared-memory coordination is not reliable. */
const JOURNAL_MODES = ['DELETE', 'TRUNCATE', 'PERSIST', 'MEMORY', 'WAL', 'OFF'];

/** PRAGMA synchronous levels, in the order SQLite numbers them (0-3). */
const SYNCHRONOUS_LEVELS = ['OFF', 'NORMAL', 'FULL', 'EXTRA'];

export const DEFAULT_JOURNAL_MODE = 'WAL';

export interface Durability {
  journalMode: string;
  synchronous: string;
  /** Rejected input, phrased for the log. Resolution never throws — a typo in a pragma must not lock an operator out of their instance — so the process that opens the file prints these instead. */
  warnings: string[];
}

/**
 * TREK_DB_JOURNAL_MODE / TREK_DB_SYNCHRONOUS → the pragmas to apply.
 *
 * journal_mode is persisted in the database file header, so it outlives the
 * process that set it and every entry point opening the file read-write has to
 * agree on it (see src/db/durability.ts). Defaults reproduce what an existing
 * install runs today: WAL, and the synchronous=NORMAL that SQLite itself picks
 * for a WAL database. Anything other than WAL keeps a rollback journal, where
 * NORMAL means a power cut can lose the last transactions — those default to
 * FULL, otherwise moving off WAL for safety would only trade one risk for
 * another.
 */
export function resolveDurability(rawJournalMode: string | undefined, rawSynchronous: string | undefined): Durability {
  const warnings: string[] = [];

  let journalMode = DEFAULT_JOURNAL_MODE;
  const wanted = rawJournalMode?.trim().toUpperCase();
  if (wanted) {
    if (JOURNAL_MODES.includes(wanted)) {
      journalMode = wanted;
    } else {
      warnings.push(
        `TREK_DB_JOURNAL_MODE="${rawJournalMode}" is not a SQLite journal mode ` +
          `(${JOURNAL_MODES.join(', ')}) — using ${DEFAULT_JOURNAL_MODE}.`,
      );
    }
  }

  const defaultSynchronous = journalMode === DEFAULT_JOURNAL_MODE ? 'NORMAL' : 'FULL';
  let synchronous = defaultSynchronous;
  const wantedSync = rawSynchronous?.trim().toUpperCase();
  if (wantedSync) {
    if (SYNCHRONOUS_LEVELS.includes(wantedSync)) {
      synchronous = wantedSync;
    } else {
      warnings.push(
        `TREK_DB_SYNCHRONOUS="${rawSynchronous}" is not a SQLite synchronous level ` +
          `(${SYNCHRONOUS_LEVELS.join(', ')}) — using ${defaultSynchronous}.`,
      );
    }
  }

  return { journalMode, synchronous, warnings };
}

/** `PRAGMA synchronous` reads back as a number — name it, so the boot log says FULL instead of 2. Unrecognized values pass through unchanged. */
export function synchronousName(level: unknown): string {
  return SYNCHRONOUS_LEVELS[Number(level)] ?? String(level);
}

/**
 * ALLOW_LINK_LOCAL_IPS: single IPv4 addresses out of 169.254.0.0/16 that outbound
 * requests may reach after all. The case it exists for is the host gateway of a
 * rootless Podman container, 169.254.1.2, with an identity provider or another
 * service behind it (#2400).
 *
 * 169.254.169.0/24 and 169.254.170.0/24 can never be listed: AWS, GCP, Azure,
 * OpenStack and the container runtimes on top of them serve instance metadata and
 * credentials there, which is what the link-local block is for. An entry is taken
 * only in the form a resolver answers with (no leading zeros, no ranges), because
 * it is compared as a string with the resolved address.
 *
 * `invalid` lists what was refused, for the boot check; `ips` is what may be used,
 * so a bad entry allows nothing.
 */
export function parseLinkLocalAllowList(raw: string | undefined): { ips: string[]; invalid: string[] } {
  const ips: string[] = [];
  const invalid: string[] = [];
  for (const entry of (raw ?? '')
    .split(',')
    .map((e) => e.trim())
    .filter(Boolean)) {
    const m = /^169\.254\.(\d{1,3})\.(\d{1,3})$/.exec(entry);
    const canonical = m !== null && [m[1], m[2]].every((o) => String(Number(o)) === o && Number(o) <= 255);
    if (canonical && m[1] !== '169' && m[1] !== '170') ips.push(entry);
    else invalid.push(entry);
  }
  return { ips, invalid };
}

// ── Web Push key material ──────────────────────────────────────────────────

const BASE64URL = /^[A-Za-z0-9_-]+={0,2}$/;

/**
 * Strict base64url decode, null for anything that is not base64url. Web Push
 * keys arrive in this alphabet from the browser and from the VAPID_* variables.
 * Buffer.from(value, 'base64url') alone skips characters it does not know, so a
 * mangled key would decode to fewer bytes instead of being refused, and the size
 * checks below would then report the wrong problem.
 */
export function decodeBase64Url(value: string): Buffer | null {
  const trimmed = value.trim();
  if (!BASE64URL.test(trimmed)) return null;
  return Buffer.from(trimmed, 'base64url');
}

/** An uncompressed P-256 point: 65 bytes, the first one 0x04 (VAPID public key, subscription p256dh). */
export function isUncompressedP256Key(value: string): boolean {
  const bytes = decodeBase64Url(value);
  return bytes?.length === 65 && bytes[0] === 0x04;
}

/** A P-256 private scalar as the Web Push tools print it: 32 bytes. */
export function isP256PrivateKey(value: string): boolean {
  return decodeBase64Url(value)?.length === 32;
}

/**
 * The VAPID `sub` claim (RFC 8292 section 2.1): a mailto: address or an https:
 * URL. Apple refuses anything else, and so does this check, so a subject that
 * boots is one every push service accepts.
 */
export function isVapidSubject(value: string): boolean {
  const trimmed = value.trim();
  if (/^mailto:[^\s@]+@[^\s@]+$/i.test(trimmed)) return true;
  try {
    return new URL(trimmed).protocol === 'https:';
  } catch {
    return false;
  }
}

const BASE64URL_SEGMENT = /^[A-Za-z0-9_-]+$/;

/** A TREK services key: `tk1.<kid>.<payload>.<signature>`, the last three base64url. */
export function isServiceKeyShape(value: string): boolean {
  const [version, ...parts] = value.trim().split('.');
  return version === 'tk1' && parts.length === 3 && parts.every((part) => BASE64URL_SEGMENT.test(part));
}