Skip to content

Commit 70e6fd1

Browse files
author
bcode
committed
fix(telemetry): address cubic review
- Move applyTelemetryKey() invocation into the telemetry module itself as an import side effect. ESM evaluates module-import side-effects before any subsequent module-load code in the importing file, so this is unambiguously ordered before the rest of opencode's imports — sidestepping the static-import-hoisting concern. - Use `!== undefined` for LMNR_PROJECT_API_KEY presence check so an explicitly-set empty string is respected as 'no key please' instead of being clobbered by the embedded default. - DO_NOT_TRACK truthiness check is intentional and unchanged: the de-facto standard (consoledonottrack.com) defines opt-out as presence with any non-empty value.
1 parent 2ba97e8 commit 70e6fd1

2 files changed

Lines changed: 26 additions & 13 deletions

File tree

packages/bcode-browser/src/telemetry.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,38 @@
88
//
99
// At runtime we set `LMNR_PROJECT_API_KEY` from the embedded default if and
1010
// only if:
11-
// - DO_NOT_TRACK is not set, AND
12-
// - the user has not already set LMNR_PROJECT_API_KEY (BYO key wins), AND
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
1315
// - the embedded default is non-empty.
1416
//
15-
// No `if (telemetryEnabled)` branches downstream — the future Laminar wiring
16-
// reads `LMNR_PROJECT_API_KEY` and initializes only when present, so the gate
17-
// here decides everything.
18-
//
19-
// Must run before any code that loads Laminar / reads LMNR_PROJECT_API_KEY.
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.
2023

2124
declare const BCODE_DEFAULT_LMNR_KEY: string
2225

2326
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).
2429
if (process.env.DO_NOT_TRACK) return
25-
if (process.env.LMNR_PROJECT_API_KEY) 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
2633
// `typeof` first: in dev (no Bun `define` substitution) the identifier is
2734
// undeclared and a direct read throws ReferenceError.
2835
if (typeof BCODE_DEFAULT_LMNR_KEY === "undefined" || !BCODE_DEFAULT_LMNR_KEY) return
2936
process.env.LMNR_PROJECT_API_KEY = BCODE_DEFAULT_LMNR_KEY
3037
}
3138

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+
3245
export * as Telemetry from "./telemetry"

packages/opencode/src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// Telemetry import sits above all others so the key gate runs before any
2-
// downstream code reads LMNR_PROJECT_API_KEY. ESM hoists imports, but the
3-
// `applyTelemetryKey()` call is the first non-import statement to execute.
4-
import { Telemetry } from "@browser-use/bcode-browser/telemetry"
5-
Telemetry.applyTelemetryKey()
1+
// Telemetry key injection runs as an import side effect of this module,
2+
// before any subsequent import is evaluated. Keep this as the FIRST import
3+
// so the LMNR_PROJECT_API_KEY env var is settled before any downstream
4+
// module-load code reads it.
5+
import "@browser-use/bcode-browser/telemetry"
66

77
import yargs from "yargs"
88
import { hideBin } from "yargs/helpers"

0 commit comments

Comments
 (0)