// src/telemetry.ts // The anonymous usage ping (fw#503): one switch, one notice, two events. // // What it answers: how many machines start the setup wizard (and which path // they pick), and how many machines run node9 on a given day. Nothing else. // // What a ping carries: the random install id from ~/.node9/machine-id, the OS, // the node9 version, the agents detected on the machine, the event name and, // for `wizard_started`, the path chosen (local or dashboard). Never a path, a // command, a repository name, a hostname or account data. // // When it is on: only after this machine has shown the first-run notice (or // the user ran `node9 telemetry on`). An install that predates the notice has // no recorded choice and stays off: the old stats question never stored its // answer, so treating it as off is the only way a "no" there stays a "no". // // Always off with NODE9_NO_TELEMETRY=1, DO_NOT_TRACK=1, under NODE9_TESTING, // and in CI. `node9 telemetry off|on` records the choice in // ~/.node9/telemetry.json (see telemetry-switch.ts for why it is not a key in // config.json); a project file or a cloud policy cannot turn it on. import { getMachineId } from './machine-id'; import { node9Version, detectAgents } from './setup'; import { isCI } from './cli/interactive'; import { sendScanRepoPing } from './ci-check/usage-ping'; import type { PingTarget } from './ci-check/usage-ping'; import { telemetryTarget } from './telemetry-env'; import { readTelemetryChoice, telemetryDisabled, telemetryStatus, writeTelemetryChoice, } from './telemetry-switch'; export { readTelemetryChoice, telemetryDisabled, telemetryStatus, writeTelemetryChoice, } from './telemetry-switch'; export type { TelemetryChoice, TelemetryStatus } from './telemetry-switch'; export { telemetryTarget } from './telemetry-env'; export type TelemetryEvent = 'wizard_started' | 'daily_active'; export type WizardChoice = 'local' | 'dashboard'; export interface TelemetryPing { event: TelemetryEvent; machine_id: string; os: string; node9_version: string; agents_detected: string[]; /** `wizard_started` only. */ choice?: WizardChoice; } /** Printed once, on the first run of a fresh machine. */ export const TELEMETRY_NOTICE = 'node9 sends anonymous usage counts when setup starts and once a day it runs (a random install ID, OS, version and detected agents; no code, commands or paths). Turn off: node9 telemetry off, or set NODE9_NO_TELEMETRY=1'; const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/; /** * The first-run notice. On a fresh machine (no config, no credentials before * this run) it prints the notice once and records `on`, which is what lets the * pings below go out. On any other machine it does nothing: an existing * install keeps whatever it had, which for one that never saw the notice is * off. Never throws. */ export function showTelemetryNoticeOnce( fresh: boolean, print: (line: string) => void = console.log, env: NodeJS.ProcessEnv = process.env ): void { try { if (!fresh || telemetryDisabled(env) || isCI(env)) return; if (readTelemetryChoice() !== null) return; writeTelemetryChoice('on'); print(TELEMETRY_NOTICE); } catch { /* a notice must never stop setup */ } } export function buildTelemetryPing(event: TelemetryEvent, choice?: WizardChoice): TelemetryPing { const detected = detectAgents(); const ping: TelemetryPing = { event, machine_id: getMachineId(), os: process.platform, node9_version: node9Version(), agents_detected: (Object.keys(detected) as Array).filter( (k) => detected[k] ), }; if (choice) ping.choice = choice; return ping; } /** * Send one ping if telemetry is on. Best effort: resolves within `timeoutMs` * whatever the network does, never throws, never writes to stdout. */ export async function sendTelemetry( event: TelemetryEvent, choice?: WizardChoice, opts: SendOptions = {} ): Promise { try { // Checked before the body is built: detecting agents reads the disk. if (!telemetryStatus().on) return; await post(buildTelemetryPing(event, choice), opts); } catch { /* best effort */ } } /** * POST a ping body to /api/v1/telemetry when telemetry is on. Shared by every * install-side event, so all of them follow the one switch. Never throws. */ export async function postTelemetry( body: { machine_id: string }, opts: SendOptions = {} ): Promise { try { if (!telemetryStatus().on) return; await post(body, opts); } catch { /* best effort */ } } interface SendOptions { timeoutMs?: number; target?: PingTarget; } /** No machine id, no ping. The scan-repo sender: cancellable DNS, a hard cap, no output. */ async function post(body: { machine_id: string }, opts: SendOptions): Promise { if (!UUID_RE.test(body.machine_id)) return; await sendScanRepoPing(body, opts.timeoutMs ?? 1500, opts.target ?? telemetryTarget()); }