|
| 1 | +// Telemetry key injection. |
| 2 | +// |
| 3 | +// At build time, `packages/opencode/script/build.ts` substitutes |
| 4 | +// `BCODE_DEFAULT_LMNR_KEY` with a string literal via Bun's `define`. The |
| 5 | +// release workflow sources that value from a GitHub Actions secret; local |
| 6 | +// `bun run build` invocations leave it empty, so self-builds never emit |
| 7 | +// telemetry. |
| 8 | +// |
| 9 | +// At runtime we set `LMNR_PROJECT_API_KEY` from the embedded default if and |
| 10 | +// only if: |
| 11 | +// - DO_NOT_TRACK is not set (any non-empty value opts out — DO_NOT_TRACK |
| 12 | +// standard convention), AND |
| 13 | +// - LMNR_PROJECT_API_KEY is not already set in the environment (BYO key |
| 14 | +// wins; explicit empty string is respected as "no key please"), AND |
| 15 | +// - the embedded default is non-empty. |
| 16 | +// |
| 17 | +// `applyTelemetryKey()` is invoked as a side effect on module import (last |
| 18 | +// statement of this file). Because `packages/opencode/src/index.ts` imports |
| 19 | +// this module before any other module that might consume the env var, the |
| 20 | +// gate is guaranteed to run before any downstream module-load code can |
| 21 | +// observe `LMNR_PROJECT_API_KEY` — sidestepping ESM static-import hoisting |
| 22 | +// entirely. |
| 23 | + |
| 24 | +declare const BCODE_DEFAULT_LMNR_KEY: string |
| 25 | + |
| 26 | +export const applyTelemetryKey = () => { |
| 27 | + // DO_NOT_TRACK: presence with any non-empty value opts out, per the |
| 28 | + // de-facto standard (consoledonottrack.com, Astro, Homebrew, npm). |
| 29 | + if (process.env.DO_NOT_TRACK) return |
| 30 | + // LMNR_PROJECT_API_KEY: presence (not truthiness) wins so users who |
| 31 | + // explicitly set it to an empty string get exactly that — no key. |
| 32 | + if (process.env.LMNR_PROJECT_API_KEY !== undefined) return |
| 33 | + // `typeof` first: in dev (no Bun `define` substitution) the identifier is |
| 34 | + // undeclared and a direct read throws ReferenceError. |
| 35 | + if (typeof BCODE_DEFAULT_LMNR_KEY === "undefined" || !BCODE_DEFAULT_LMNR_KEY) return |
| 36 | + process.env.LMNR_PROJECT_API_KEY = BCODE_DEFAULT_LMNR_KEY |
| 37 | +} |
| 38 | + |
| 39 | +// Run as an import side effect: this module is imported as the very first |
| 40 | +// import of `packages/opencode/src/index.ts`, so by the time any other |
| 41 | +// module's top-level code reads `LMNR_PROJECT_API_KEY` the gate has already |
| 42 | +// resolved. |
| 43 | +applyTelemetryKey() |
| 44 | + |
| 45 | +export * as Telemetry from "./telemetry" |
0 commit comments