// src/telemetry-switch.ts // The on/off switch for the anonymous usage ping (fw#503), and the once-a-day // check the hook runs. Kept apart from src/telemetry.ts on purpose: `check` // imports this on every tool call, so it must not load setup.ts, the HTTP // client or anything else heavy. See src/telemetry.ts for what is sent. import fs from 'fs'; import os from 'os'; import path from 'path'; import { spawn } from 'child_process'; import { atomicWriteSync } from './utils/atomic-write'; import { isCI } from './cli/interactive'; import { ownLauncher, INJECTING_ENV } from './cli/daemon-starter'; export type TelemetryChoice = 'on' | 'off'; import { telemetryDisabled } from './telemetry-env'; export { telemetryDisabled }; /** * Where the choice lives: ~/.node9/telemetry.json, `{"choice":"on"|"off"}`. * Its own file, not a key in config.json, because setup treats a machine with * no config.json as fresh (and picks its default protection from that): the * notice records its choice before setup writes the config, and must not * change what setup sees. Only the home directory is read, so a project file * or a cloud policy cannot turn the ping on. */ export function telemetryChoiceFile(): string { return path.join(os.homedir(), '.node9', 'telemetry.json'); } /** The recorded choice, or null when none was ever made. */ export function readTelemetryChoice(): TelemetryChoice | null { try { const raw = JSON.parse(fs.readFileSync(telemetryChoiceFile(), 'utf8')) as unknown; const v = raw && typeof raw === 'object' ? (raw as Record).choice : null; return v === 'on' || v === 'off' ? v : null; } catch { // Missing or unreadable is not a yes. return null; } } export function writeTelemetryChoice(choice: TelemetryChoice): void { const file = telemetryChoiceFile(); fs.mkdirSync(path.dirname(file), { recursive: true }); atomicWriteSync(file, JSON.stringify({ choice }) + '\n', { mode: 0o600 }); } export type TelemetryStatus = { on: true } | { on: false; reason: 'env' | 'ci' | 'off' | 'never-shown' }; /** Whether a ping may be sent from this process, and if not, why. */ export function telemetryStatus(env: NodeJS.ProcessEnv = process.env): TelemetryStatus { if (telemetryDisabled(env)) return { on: false, reason: 'env' }; if (isCI(env)) return { on: false, reason: 'ci' }; const choice = readTelemetryChoice(); if (choice === 'on') return { on: true }; return { on: false, reason: choice === 'off' ? 'off' : 'never-shown' }; } // daily_active, from the hook const DAY_STAMP = 'telemetry-day'; function today(now: Date): string { return now.toISOString().slice(0, 10); } /** * Called from `check` on every tool call. With telemetry off by env it costs * nothing; otherwise one read of a 10-byte stamp file. On the first call of a * UTC day it writes the stamp FIRST (so a failure can never turn into a retry * on every call), then, if telemetry is on, starts `node9 telemetry ping` * detached and returns at once. The hook never waits for the network. * Never throws. */ export function maybeSendDailyActive( now: Date = new Date(), env: NodeJS.ProcessEnv = process.env ): void { try { if (telemetryDisabled(env) || isCI(env)) return; const dir = path.join(os.homedir(), '.node9'); const stamp = path.join(dir, DAY_STAMP); const day = today(now); try { if (fs.readFileSync(stamp, 'utf8').trim() === day) return; } catch { /* no stamp yet */ } if (!fs.existsSync(dir)) return; // not set up: nothing to count fs.writeFileSync(stamp, day + '\n', { mode: 0o600 }); if (readTelemetryChoice() !== 'on') return; const script = ownLauncher(); if (!script) return; const childEnv = { ...env }; for (const key of INJECTING_ENV) delete childEnv[key]; const child = spawn(process.execPath, [script, 'telemetry', 'ping'], { detached: true, stdio: 'ignore', // Without it Windows opens a console window for the detached child. windowsHide: true, env: childEnv, cwd: os.homedir(), }); // spawn() reports exec failures on this event; without a listener it // would be an uncaught exception in the hook. child.on('error', () => {}); child.unref(); } catch { /* never fail a tool call over a usage count */ } }