From b5166a998ab3c602ccdbdfec719d5c0965751f9e Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Wed, 18 Nov 2026 21:03:36 -0700 Subject: [PATCH] Make every opt-out fail closed (0.5.0) A switch that does not clearly mean "on" now turns reporting off, so an opt-out can never be read as consent (trace-client-java 0.8.0 counterpart): - TRACE_USAGE_REPORTING set to anything but blank or true/on/yes/1 now disables; only off/false/0/no did. - DO_NOT_TRACK set to anything but blank or 0/false/off/no now disables; only 1/true/yes did. - An environment that cannot be read (process.env or an env lookup throwing) now disables; it used to opt nothing out. A non-string env value disables too. No process / no process.env still opts nothing out. - The enabled option, when given, is read through the new TraceClient.allowsReporting(); only enabled: false turned it off, so "false", null, undefined or 0 reported. Left out, it defaults to on. - README examples use allowsReporting(process.env.USAGE_REPORTING_ENABLED ?? true) instead of !== "false", which read "off" or a typo as on. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 22 ++++++++ README.md | 38 ++++++++----- package-lock.json | 4 +- package.json | 2 +- tests/trace-client.test.ts | 93 ++++++++++++++++++++++++++------ trace-client.ts | 108 ++++++++++++++++++++++++------------- 6 files changed, 196 insertions(+), 71 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d3914c..57f6748 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,28 @@ against this file to see what a re-vendor would bring. ## [Unreleased] +Version 0.5.0 in the source. Every opt-out now fails closed, so an +operator's opt-out can never be read as consent (the counterpart of +trace-client-java 0.8.0): + +- `TRACE_USAGE_REPORTING` set to anything but blank, `true`, `on`, `yes` or + `1` now turns reporting off. Only `off`, `false`, `0` and `no` did before, + so `disabled` or a typo left reporting on. +- `DO_NOT_TRACK` set to anything but blank, `0`, `false`, `off` or `no` now + turns reporting off. Only `1`, `true` and `yes` did before. +- An environment that cannot be read (reading `process.env` throws, or an + `env` whose lookups throw) now counts as opting out; it used to opt + nothing out. So does an `env` value that is not a string. A runtime with + no `process` or no `process.env` still opts nothing out. +- The `enabled` option, when given, now keeps reporting on only for `true` + or a string `true`/`on`/`yes`/`1` (any case). Only `false` turned it off + before, so `enabled: "false"`, `null`, `undefined` or `0` from untyped code + or a config file left reporting on. Left out, it still defaults to on. +- New `TraceClient.allowsReporting(value)` reads a program's own setting the + same way. The README examples use it in place of + `process.env.USAGE_REPORTING_ENABLED !== "false"`, which read `off`, + `FALSE` or a typo as on; programs that copied that line should switch. + - A response whose body cannot be read no longer counts against the report, however the read fails. A custom `fetch` whose response had no `arrayBuffer`, or one that threw at once, used to log a delivered report diff --git a/README.md b/README.md index ac9c46e..3e97936 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ import { TraceClient } from "./trace-client"; const trace = new TraceClient("https://trace.danielstephenson.dev", "my-site", { version: "1.4.0", // the program's own version, required -- see below key: process.env.USAGE_REPORTING_KEY, - enabled: process.env.USAGE_REPORTING_ENABLED !== "false", + enabled: TraceClient.allowsReporting(process.env.USAGE_REPORTING_ENABLED ?? true), }); trace.report("startup"); // tagged version=1.4.0, like every other event trace.report("world-load", { value: 2.5, tags: { kind: "procedural" } }); @@ -122,7 +122,15 @@ regardless; pass the path as `installIdFile` to keep that guarantee.) Reporting is **opt-out**: `enabled: false`, or no key at all, yields a client that does nothing and costs nothing (`TraceClient.disabled()` is a ready-made -one). A program that runs on other people's machines should expose that switch +one). **Every switch fails closed:** a value that does not clearly mean "on" — +a typo, `"false"` or `"off"` as a string, an empty value, `null` — turns +reporting off, and nothing in this client ever turns a switch back on. +`enabled` keeps reporting on only for `true` or a string `true`/`on`/`yes`/`1` +(any case); left out, it defaults to on. Read the program's own setting with +`TraceClient.allowsReporting(value)`, which fails closed the same way — +`allowsReporting(process.env.USAGE_REPORTING_ENABLED ?? true)` keeps reporting +on when the variable is unset and turns it off for any value but a clear "on". +Avoid `value !== "false"`: it reads `off`, `FALSE` or a typo as on. A program that runs on other people's machines should expose that switch in its settings — and say so once, so whoever runs it knows it is on and where to turn it off. @@ -169,16 +177,18 @@ client — this one, trace-client-python and trace-client-java alike — and they are checked before the program's own `enabled` setting and before the key: -| Variable | Values that turn reporting off | +| Variable | Turns reporting off when set to | |---|---| -| `TRACE_USAGE_REPORTING` | `off`, `false`, `0`, `no` | -| `DO_NOT_TRACK` | `1`, `true`, `yes` ([consoledonottrack.com](https://consoledonottrack.com)) | +| `TRACE_USAGE_REPORTING` | anything but blank, `true`, `on`, `yes` or `1` — so `off`, `false`, `0`, `no`, `disabled`, a typo, … | +| `DO_NOT_TRACK` | anything but blank, `0`, `false`, `off` or `no` — so `1`, `true`, `yes`, … ([consoledonottrack.com](https://consoledonottrack.com)) | -Case and surrounding space are ignored; any other value, or none, leaves the -program's own setting in charge. The client reads them from `process.env` -when the runtime has one (by their literal names, so Next.js can inline them -into the Edge runtime); a runtime without `process`, or whose environment -cannot be read, simply opts nothing out. Pass `env` to use another source: +Both fail closed. Case and surrounding space are ignored; an unset or blank +variable leaves the program's own setting in charge. The client reads them +from `process.env` when the runtime has one (by their literal names, so +Next.js can inline them into the Edge runtime). A runtime without `process` +(or without `process.env`) has no environment and opts nothing out; one whose +environment cannot be read (reading it throws), or an `env` holding a value +that is not a string, counts as opting out. Pass `env` to use another source: ```ts // Cloudflare Workers and other runtimes that hand the environment to the handler @@ -191,7 +201,7 @@ this order — so a program can say so in its startup notice: | `disabledReason` | Cause | |---|---| | `"environment"` | `TRACE_USAGE_REPORTING` or `DO_NOT_TRACK` above | -| `"config"` | `enabled: false` | +| `"config"` | `enabled` given and not `true` or a string `true`/`on`/`yes`/`1` | | `"no key"` | the key was missing, blank or not a string | | `null` | the client reports | @@ -236,7 +246,7 @@ const trace = new TraceClient( version: "1.4.0", // The key shipped with the site is an identity, not a secret -- see "Keys". key: process.env.USAGE_REPORTING_KEY ?? "tk_my-site_xxxxxxxxxxxxxxxxxxxxxxxx", - enabled: process.env.USAGE_REPORTING_ENABLED !== "false", + enabled: TraceClient.allowsReporting(process.env.USAGE_REPORTING_ENABLED ?? true), }, ); @@ -275,7 +285,7 @@ reported as page views. | Variable | Default | Meaning | |---|---|---| -| `USAGE_REPORTING_ENABLED` | `true` | Set to `false` to turn reporting off entirely. | +| `USAGE_REPORTING_ENABLED` | `true` | Set to anything but `true`, `on`, `yes` or `1` (e.g. `false`) to turn reporting off entirely. | | `USAGE_REPORTING_ENDPOINT` | the shared trace server | Point at your own trace server instead. | | `USAGE_REPORTING_KEY` | the key shipped in the repo | Report as a different program. | @@ -286,7 +296,7 @@ site can flip `USAGE_REPORTING_ENABLED=false` and nothing is sent. ## The wire format `POST {baseUrl}/api/metrics` with `Content-Type: application/json`, -`Authorization: Bearer `, `User-Agent: trace-client-js/0.4.1 ()` +`Authorization: Bearer `, `User-Agent: trace-client-js/0.5.0 ()` and a body of ```json diff --git a/package-lock.json b/package-lock.json index c2d61f3..c0a75fc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "trace-client-js", - "version": "0.4.1", + "version": "0.5.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "trace-client-js", - "version": "0.4.1", + "version": "0.5.0", "license": "MIT", "devDependencies": { "typescript": "^5.6.0" diff --git a/package.json b/package.json index 48664f3..555e1db 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "trace-client-js", - "version": "0.4.1", + "version": "0.5.0", "private": true, "description": "One-file usage-reporting client for trace, for Node and edge runtimes (vendor it, bStats-style)", "type": "module", diff --git a/tests/trace-client.test.ts b/tests/trace-client.test.ts index 3439207..2705429 100644 --- a/tests/trace-client.test.ts +++ b/tests/trace-client.test.ts @@ -1286,7 +1286,7 @@ describe("API shape", () => { assert.equal(await client.flush(50), undefined); assert.equal(await client.close(50), undefined); assert.equal(TraceClient.disabled().enabled, false); - assert.equal(TRACE_CLIENT_VERSION, "0.4.1"); + assert.equal(TRACE_CLIENT_VERSION, "0.5.0"); }); it("keeps the documented values of its public constants, which the Python and Java clients share", () => { @@ -1327,23 +1327,82 @@ describe("environment opt-outs", () => { for (const value of ["1", "true", "yes", "TRUE", " Yes "]) { assert.equal(environmentOptsOut({ DO_NOT_TRACK: value }), true, `DO_NOT_TRACK=${value}`); } - for (const value of ["", "on", "true", "1", "yes", "disabled"]) { + for (const value of ["", " ", "on", "true", "1", "yes", "TRUE", " On "]) { assert.equal(environmentOptsOut({ TRACE_USAGE_REPORTING: value }), false, `TRACE_USAGE_REPORTING=${value}`); } - for (const value of ["", "0", "false", "no", "off"]) { + for (const value of ["", " ", "0", "false", "no", "off", " No ", "OFF"]) { assert.equal(environmentOptsOut({ DO_NOT_TRACK: value }), false, `DO_NOT_TRACK=${value}`); } assert.equal(environmentOptsOut({}), false); + assert.equal(environmentOptsOut({ TRACE_USAGE_REPORTING: undefined, DO_NOT_TRACK: undefined }), false); }); - it("ignores an opt-out value that is not a string, as untyped code may pass", async () => { - for (const value of [1, true, 0, false, null, ["off"], { toString: () => "off" }]) { - const env = { TRACE_USAGE_REPORTING: value, DO_NOT_TRACK: value } as unknown as Record; - assert.equal(environmentOptsOut(env), false, `a ${typeof value} is not one of the documented strings`); - const client = new TraceClient(baseUrl, "MyGame", { version: "1.2.3", key: "k", env }); - assert.equal(client.disabledReason, null, "the program's own setting stays in charge"); + it("fails closed: TRACE_USAGE_REPORTING set to anything but a clear on opts out", async () => { + for (const value of ["disabled", "of", "nope", "2", "-1", "ture", "none", "never", '"true"']) { + assert.equal(environmentOptsOut({ TRACE_USAGE_REPORTING: value }), true, `TRACE_USAGE_REPORTING=${value}`); + const client = new TraceClient(baseUrl, "MyGame", { version: "1.2.3", key: "k", env: { TRACE_USAGE_REPORTING: value } }); + assert.equal(client.disabledReason, REASON_ENVIRONMENT, `TRACE_USAGE_REPORTING=${value}`); + await client.report("startup"); + } + await sleep(100); + assert.deepEqual(capture.requests, []); + }); + + it("fails closed: DO_NOT_TRACK set to anything but a clear no opts out", async () => { + for (const value of ["on", "2", "y", "enabled", "please", "nein"]) { + assert.equal(environmentOptsOut({ DO_NOT_TRACK: value }), true, `DO_NOT_TRACK=${value}`); + const client = new TraceClient(baseUrl, "MyGame", { version: "1.2.3", key: "k", env: { DO_NOT_TRACK: value } }); + assert.equal(client.disabledReason, REASON_ENVIRONMENT, `DO_NOT_TRACK=${value}`); + await client.report("startup"); + } + await sleep(100); + assert.deepEqual(capture.requests, []); + }); + + it("an opt-out value that is not a string, as untyped code may pass, opts out", async () => { + for (const value of [1, true, 0, false, ["off"], { toString: () => "on" }]) { + for (const name of OPT_OUT_VARIABLES) { + const env = { [name]: value } as unknown as Record; + assert.equal(environmentOptsOut(env), true, `${name} as a ${typeof value} fails closed`); + const client = new TraceClient(baseUrl, "MyGame", { version: "1.2.3", key: "k", env }); + assert.equal(client.disabledReason, REASON_ENVIRONMENT); + await client.close(); + } + } + const nulls = { TRACE_USAGE_REPORTING: null, DO_NOT_TRACK: null } as unknown as Record; + assert.equal(environmentOptsOut(nulls), false, "null is unset"); + }); + + it("allowsReporting is true only for true or a clear on string", () => { + for (const value of [true, "true", "on", "yes", "1", "TRUE", " On ", "Yes\n"]) { + assert.equal(TraceClient.allowsReporting(value), true, JSON.stringify(value)); + } + for (const value of [false, null, undefined, "false", "off", "no", "0", "", " ", "ture", '"false"', '"true"', + "disabled", 1, 0, 1n, NaN, [], ["true"], { enabled: true }, { toString: () => "true" }, new String("true"), + new Boolean(true), Symbol("true")]) { + assert.equal(TraceClient.allowsReporting(value), false, String(typeof value === "symbol" ? "symbol" : value)); + } + }); + + it("reads the enabled option through allowsReporting when it is given", async () => { + for (const value of ["false", "off", "no", '"false"', "ture", null, undefined, 0, 1, "0"]) { + const options = { version: "1.2.3", key: "k", env: {}, enabled: value } as unknown as TraceClientOptions; + const client = new TraceClient(baseUrl, "MyGame", options); + assert.equal(client.enabled, false, `enabled: ${String(value)} must not report`); + assert.equal(client.disabledReason, REASON_CONFIG, String(value)); + await client.report("startup"); + } + await sleep(100); + assert.deepEqual(capture.requests, []); + for (const value of [true, "true", " ON ", "yes", "1"]) { + const options = { version: "1.2.3", key: "k", env: {}, enabled: value } as unknown as TraceClientOptions; + const client = new TraceClient(baseUrl, "MyGame", options); + assert.equal(client.disabledReason, null, `enabled: ${String(value)} reports`); await client.close(); } + const unsaid = new TraceClient(baseUrl, "MyGame", { version: "1.2.3", key: "k", env: {} }); + assert.equal(unsaid.disabledReason, null, "enabled left out defaults to on"); + await unsaid.close(); }); it("sends nothing and reports \"environment\" when TRACE_USAGE_REPORTING or DO_NOT_TRACK opts out", async () => { @@ -1428,24 +1487,26 @@ describe("environment opt-outs", () => { } finally { Object.defineProperty(globalThis, "process", descriptor); } - assert.equal(noProcess, false); - assert.equal(throwingEnv, false); + assert.equal(noProcess, false, "no process: no environment, nothing opted out"); + assert.equal(throwingEnv, true, "an environment that cannot be read may say DO_NOT_TRACK=1"); + assert.deepEqual(built.map((client) => client.disabledReason), [null, REASON_ENVIRONMENT, null]); for (const client of built) { - assert.equal(client.disabledReason, null); await client.report("startup"); await client.close(); } - assert.equal(capture.requests.length, built.length); + assert.equal(capture.requests.length, 2, "the client whose environment could not be read sent nothing"); }); - it("an env whose lookups throw opts nothing out and does not escape", () => { + it("an env whose lookups throw opts out and does not escape", () => { const hostile = new Proxy({}, { get(): never { throw new Error("no"); }, }); - assert.equal(environmentOptsOut(hostile), false); - assert.equal(new TraceClient(baseUrl, "MyGame", { version: "1.2.3", key: "k", env: hostile }).disabledReason, null); + assert.equal(environmentOptsOut(hostile), true); + const client = new TraceClient(baseUrl, "MyGame", { version: "1.2.3", key: "k", env: hostile }); + assert.equal(client.disabledReason, REASON_ENVIRONMENT); + assert.equal(client.enabled, false); }); }); diff --git a/trace-client.ts b/trace-client.ts index 9f3fe04..a35af1f 100644 --- a/trace-client.ts +++ b/trace-client.ts @@ -1,5 +1,5 @@ /** - * trace-client 0.4.1 -- https://github.com/Stephenson-Software/trace-client-js + * trace-client 0.5.0 -- https://github.com/Stephenson-Software/trace-client-js * * One call to report that a program was used. Copy this file into a project * as is; there is nothing else to add. Zero dependencies and no Node-only @@ -13,7 +13,7 @@ * MIT licensed. Keep this header when vendoring so the file can be found again. */ -export const TRACE_CLIENT_VERSION = "0.4.1"; +export const TRACE_CLIENT_VERSION = "0.5.0"; // Everything added since 0.1.0 hangs off TraceClient (static members) rather // than being a new top-level export, so the file's exported values stay @@ -26,8 +26,8 @@ export type DisabledReason = "environment" | "config" | "no key"; /** An environment to read the opt-outs from: `process.env`, or a stand-in. */ export type Environment = Readonly>; -const OFF_VALUES = ["off", "false", "0", "no"]; -const DO_NOT_TRACK_VALUES = ["1", "true", "yes"]; +const ON_VALUES = ["true", "on", "yes", "1"]; +const NEGATIVE_VALUES = ["false", "off", "no", "0"]; // Declared here rather than taken from @types/node so the file still // type-checks against ES2022 + DOM alone; at runtime `process` may simply not @@ -63,8 +63,14 @@ export interface TraceClientOptions { version: string; /** The program's write key. Blank, missing or not a string yields a client that does nothing. */ key?: string; - /** The opt-out switch. `false` yields a client that does nothing. Default `true`. */ - enabled?: boolean; + /** + * The program's own switch. Fails closed: only `true`, or a string reading + * `true`, `on`, `yes` or `1` (any case), leaves reporting on; anything else + * given -- `false`, `"false"`, `"off"`, a typo, a number, `null`, + * `undefined` -- yields a client that does nothing. Default (option left + * out) `true`. See {@link TraceClient.allowsReporting}. + */ + enabled?: boolean | string | null; /** * Per-request timeout in milliseconds. Default {@link TraceClient.TIMEOUT_MS}. * Held to 2147483647 (about 24.8 days), the longest a timer can wait. @@ -131,6 +137,8 @@ export interface ReportOptions { * nothing and costs nothing. So does the environment: the constructor checks * `TRACE_USAGE_REPORTING=off` and `DO_NOT_TRACK=1` before it looks at * `enabled`, so a user can switch off every trace-reporting program at once. + * Every switch fails closed: a value that does not clearly mean "on" turns + * reporting off, and nothing in this client turns a switch back on. * {@link disabledReason} says which of those applied (`"environment"`, * `"config"` or `"no key"`; `null` when on) so the program can say so in its * notice. Programs that run on other people's machines should expose that @@ -152,7 +160,7 @@ export interface ReportOptions { * const trace = new TraceClient("https://trace.example.org", "my-site", { * version: "1.4.0", * key: process.env.USAGE_REPORTING_KEY, - * enabled: process.env.USAGE_REPORTING_ENABLED !== "false", + * enabled: TraceClient.allowsReporting(process.env.USAGE_REPORTING_ENABLED ?? true), * }); * if (!trace.enabled) console.log(`Usage reporting is off (${trace.disabledReason}).`); * trace.report("startup"); // tagged version=1.4.0 @@ -175,39 +183,59 @@ export class TraceClient { /** * Environment variables that turn reporting off for every program using a - * trace client, checked before the program's own setting: - * `TRACE_USAGE_REPORTING=off` (also `false`, `0`, `no`; case and - * surrounding space do not matter) or `DO_NOT_TRACK=1` (also `true`, `yes`; - * see https://consoledonottrack.com). Any other value, including an empty - * one, leaves the program's own setting in charge. + * trace client, checked before the program's own setting. Both fail closed: + * `TRACE_USAGE_REPORTING` set to anything but blank, `true`, `on`, `yes` or + * `1` turns reporting off (so `off`, `false`, `0`, `no`, `disabled`, a + * typo, ...), and so does `DO_NOT_TRACK` set to anything but blank, `0`, + * `false`, `off` or `no` (so `1`, `true`, `yes`, ...; see + * https://consoledonottrack.com). Case and surrounding space do not + * matter. Unset or blank leaves the program's own setting in charge. */ static readonly ENV_TRACE_USAGE_REPORTING = "TRACE_USAGE_REPORTING"; static readonly ENV_DO_NOT_TRACK = "DO_NOT_TRACK"; /** {@link disabledReason} when `TRACE_USAGE_REPORTING` or `DO_NOT_TRACK` opted out. */ static readonly REASON_ENVIRONMENT = "environment"; - /** {@link disabledReason} when the program passed `enabled: false`. */ + /** {@link disabledReason} when the program's `enabled` was not clearly on. */ static readonly REASON_CONFIG = "config"; /** {@link disabledReason} when the key was missing, blank or not a string. */ static readonly REASON_NO_KEY = "no key"; /** - * Whether the environment asks for usage reporting to be off, via - * `TRACE_USAGE_REPORTING=off` or `DO_NOT_TRACK=1`. Reads `env` when given, - * otherwise `process.env` if the runtime has one; never throws, and a - * runtime without an environment opts nothing out. + * Whether the environment asks for usage reporting to be off: see + * {@link ENV_TRACE_USAGE_REPORTING}. Reads `env` when given, otherwise + * `process.env` if the runtime has one; a runtime with no environment at + * all opts nothing out. Never throws, and fails closed: an environment + * that cannot be read, or a value in it that is not a string, opts out -- + * it may hold `DO_NOT_TRACK=1`. */ static environmentOptsOut(env?: Environment): boolean { - let trace: unknown; - let doNotTrack: unknown; try { const source = env ?? processEnvironment(); - trace = source[TraceClient.ENV_TRACE_USAGE_REPORTING]; - doNotTrack = source[TraceClient.ENV_DO_NOT_TRACK]; + const trace = setting(source[TraceClient.ENV_TRACE_USAGE_REPORTING]); + if (trace !== "" && !ON_VALUES.includes(trace)) return true; + const doNotTrack = setting(source[TraceClient.ENV_DO_NOT_TRACK]); + return doNotTrack !== "" && !NEGATIVE_VALUES.includes(doNotTrack); } catch { - return false; // an environment that cannot be read opts nothing out + return true; // an environment that cannot be read may say DO_NOT_TRACK=1 } - return matches(trace, OFF_VALUES) || matches(doNotTrack, DO_NOT_TRACK_VALUES); + } + + /** + * Whether a program's own reporting setting, as read from its + * configuration, allows reporting -- for the `enabled` option. Fails + * closed: only `true`, or a string reading `true`, `on`, `yes` or `1` (any + * case, surrounding space ignored), allows it. Anything else turns + * reporting off: `false`, `"false"`, `"off"`, `"no"`, a typo, a number, + * `null`, `undefined` (a missing setting), a quoted `'"false"'`. + * + * Pass the raw value. A comparison such as `value !== "false"` reads a + * typo, `"off"` or `"FALSE"` as on. To keep reporting on when the setting + * is absent, say so: `allowsReporting(process.env.X ?? true)`. + */ + static allowsReporting(setting: unknown): boolean { + if (typeof setting === "boolean") return setting; + return typeof setting === "string" && ON_VALUES.includes(setting.trim().toLowerCase()); } private readonly endpoint: string; @@ -256,7 +284,7 @@ export class TraceClient { // Decided once, here: the environment, then the program's switch, then the key. if (TraceClient.environmentOptsOut(options.env)) { this.reason = TraceClient.REASON_ENVIRONMENT; - } else if (options.enabled === false) { + } else if ("enabled" in options && !TraceClient.allowsReporting(options.enabled)) { this.reason = TraceClient.REASON_CONFIG; } else if (this.key === "") { this.reason = TraceClient.REASON_NO_KEY; @@ -335,7 +363,7 @@ export class TraceClient { /** * Why this client reports nothing: `"environment"` (`TRACE_USAGE_REPORTING` - * / `DO_NOT_TRACK`), `"config"` (`enabled: false`) or `"no key"`; `null` + * / `DO_NOT_TRACK`), `"config"` (`enabled` not clearly on) or `"no key"`; `null` * when it reports. Fixed at construction; {@link close} does not change it. */ get disabledReason(): DisabledReason | null { @@ -511,24 +539,28 @@ export function isBot(userAgent: string | null | undefined): boolean { /** * The two opt-out variables from `process.env`, or an empty environment where - * there is no `process` (or reading it throws, as a permission-gated runtime - * may). Each is read by its literal name, which is what lets bundlers such as + * there is no `process` or it has no `env`. Throws when reading it throws (a + * permission-gated runtime), which environmentOptsOut takes as an opt-out. + * Each is read by its literal name, which is what lets bundlers such as * Next.js inline it for the Edge runtime. */ function processEnvironment(): Environment { - try { - if (typeof process === "undefined" || !process || !process.env) return {}; - return { - TRACE_USAGE_REPORTING: process.env.TRACE_USAGE_REPORTING, - DO_NOT_TRACK: process.env.DO_NOT_TRACK, - }; - } catch { - return {}; - } + if (typeof process === "undefined" || !process || !process.env) return {}; + return { + TRACE_USAGE_REPORTING: process.env.TRACE_USAGE_REPORTING, + DO_NOT_TRACK: process.env.DO_NOT_TRACK, + }; } -function matches(value: unknown, accepted: readonly string[]): boolean { - return typeof value === "string" && accepted.includes(value.trim().toLowerCase()); +/** + * An environment variable's value, trimmed and lower-cased; `""` when unset. + * Throws for a value that is not a string, which environmentOptsOut takes as + * an opt-out. + */ +function setting(value: unknown): string { + if (value === undefined || value === null) return ""; + if (typeof value !== "string") throw new TypeError("not a string"); + return value.trim().toLowerCase(); } /**