Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 24 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" } });
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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
Expand All @@ -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 |

Expand Down Expand Up @@ -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),
},
);

Expand Down Expand Up @@ -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. |

Expand All @@ -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 <key>`, `User-Agent: trace-client-js/0.4.1 (<application>)`
`Authorization: Bearer <key>`, `User-Agent: trace-client-js/0.5.0 (<application>)`
and a body of

```json
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
93 changes: 77 additions & 16 deletions tests/trace-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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<string, string>;
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<string, string>;
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<string, string>;
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 () => {
Expand Down Expand Up @@ -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);
});
});

Expand Down
Loading
Loading