Skip to content

Commit b5f890a

Browse files
authored
Merge pull request #32 from browser-use/feat/embed-lmnr-key
feat: embed Laminar project key into release binaries
2 parents 6cbf6a7 + 70e6fd1 commit b5f890a

4 files changed

Lines changed: 59 additions & 0 deletions

File tree

.github/workflows/release.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ jobs:
8484
OPENCODE_CHANNEL: latest
8585
GH_REPO: ${{ github.repository }}
8686
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
87+
# Embedded into the binary by build.ts via Bun `define`. Read at
88+
# runtime in @browser-use/bcode-browser/telemetry, gated by
89+
# DO_NOT_TRACK and any user-supplied LMNR_PROJECT_API_KEY.
90+
BCODE_DEFAULT_LMNR_KEY: ${{ secrets.LMNR_PROJECT_API_KEY_OSS }}
8791
run: |
8892
./packages/opencode/script/build.ts
8993
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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"

packages/opencode/script/build.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ for (const item of targets) {
235235
OPENCODE_WORKER_PATH: workerPath,
236236
OPENCODE_CHANNEL: `'${Script.channel}'`,
237237
OPENCODE_LIBC: item.os === "linux" ? `'${item.abi ?? "glibc"}'` : "",
238+
// Build-time-embedded Laminar project key. Populated by release CI from
239+
// the LMNR_PROJECT_API_KEY_OSS secret; empty for local builds. Runtime
240+
// use is gated in @browser-use/bcode-browser/src/telemetry.ts.
241+
BCODE_DEFAULT_LMNR_KEY: JSON.stringify(process.env.BCODE_DEFAULT_LMNR_KEY ?? ""),
238242
},
239243
})
240244

packages/opencode/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
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"
6+
17
import yargs from "yargs"
28
import { hideBin } from "yargs/helpers"
39
import { RunCommand } from "./cli/cmd/run"

0 commit comments

Comments
 (0)