// src/telemetry-env.ts // The environment switches that turn off every ping node9 sends (fw#388, // fw#503), and where pings go. No runtime imports on purpose: the hook and // scan-repo both read it. import type { PingTarget } from './ci-check/usage-ping'; const TRUTHY = /^(1|true|yes)$/i; /** NODE9_NO_TELEMETRY, DO_NOT_TRACK or NODE9_TESTING: no ping of any kind. */ export function telemetryDisabled(env: NodeJS.ProcessEnv = process.env): boolean { return ( TRUTHY.test(env.NODE9_NO_TELEMETRY ?? '') || TRUTHY.test(env.DO_NOT_TRACK ?? '') || // Same rule as isTestingMode(), read from `env` so a test can pass its own. TRUTHY.test(env.NODE9_TESTING ?? '') ); } /** * Where pings go. Production unless NODE9_TELEMETRY_URL names a local server * (http://localhost:PORT or http://127.0.0.1:PORT): that override exists for * the end-to-end check against a local backend and accepts nothing else, so * an environment variable cannot send the ping to a third party. */ export function telemetryTarget(env: NodeJS.ProcessEnv = process.env): PingTarget | undefined { const raw = env.NODE9_TELEMETRY_URL; if (!raw) return undefined; try { const u = new URL(raw); if (u.protocol !== 'http:' || !['localhost', '127.0.0.1'].includes(u.hostname)) return undefined; return { hostname: u.hostname === 'localhost' ? '127.0.0.1' : u.hostname, port: u.port ? Number(u.port) : 80, protocol: 'http', }; } catch { return undefined; } }