main
1// Throw ratchet. Errors are values (docs/STACK.md rule 1): the number of `throw` statements under src/ must match
2// scripts/throw-baseline.json exactly. A rise fails. A drop fails until the same change lowers the baseline, so the
3// count only moves down on purpose. What remains at the end is assertNever, `internal` invariants, and (until the
4// public Result cut-over) the throwing wrappers other modules still call.
5//
6// node scripts/throw-ratchet.mjs compare, exit 1 on any difference
7// node scripts/throw-ratchet.mjs --write rewrite the baseline when the count fell (a rise is refused)
8// The baseline is written only under --write. A missing baseline is a failure, not a reason to write one: deleting
9// the file would otherwise accept any number of new throws with exit 0.
10import { readFileSync, writeFileSync, readdirSync, existsSync } from 'node:fs';
11import { join } from 'node:path';
12import { pathToFileURL } from 'node:url';
14const BASELINE_PATH = 'scripts/throw-baseline.json';
16// A `throw` in code, not in prose: drop block comments, then line comments (a `//` preceded by a space or at the
17// line start, so `http://` in a string survives), then count the keyword where a statement can start.
18const stripComments = text => text
19 .replace(/\/\*[\s\S]*?\*\//g, '')
20 .split('\n')
21 .map(line => line.replace(/(^|\s)\/\/.*$/, '$1'))
22 .join('\n');
23export const countThrows = text => (stripComments(text).match(/(?:^|[;{}()]|\belse|=>|\?|:)\s*throw\b/gm) ?? []).length;
25const walk = dir => readdirSync(dir, { withFileTypes: true }).flatMap(entry => {
26 const path = join(dir, entry.name).split('\\').join('/');
27 if (entry.isDirectory()) return walk(path);
28 return /\.(?:ts|js|mjs|cjs)$/.test(entry.name) ? [path] : [];
29});
31export function countTree() {
32 const files = walk('src').map(file => ({ file, n: countThrows(readFileSync(file, 'utf8')) })).filter(f => f.n > 0);
33 files.sort((a, b) => b.n - a.n || a.file.localeCompare(b.file));
34 return { total: files.reduce((sum, f) => sum + f.n, 0), files };
35}
37function main() {
38 const { total, files } = countTree();
39 const write = process.argv.includes('--write');
40 const baseline = existsSync(BASELINE_PATH) ? JSON.parse(readFileSync(BASELINE_PATH, 'utf8')) : null;
41 if (baseline === null && !write) {
42 console.error(`throw-ratchet: no ${BASELINE_PATH} (src has ${total} throw statements); run with --write to create it`);
43 process.exit(1);
44 }
45 if (baseline === null || write) {
46 if (baseline !== null && total > baseline.throw) {
47 console.error(`throw-ratchet: throw rose ${baseline.throw} -> ${total}; a throw may only be removed`);
48 process.exit(1);
49 }
50 writeFileSync(BASELINE_PATH, `${JSON.stringify({ throw: total }, null, 2)}\n`);
51 console.log(`throw-ratchet: wrote ${BASELINE_PATH} (throw ${total})`);
52 return;
53 }
54 if (total === baseline.throw) { console.log(`throw-ratchet: throw ${total}`); return; }
55 const verb = total > baseline.throw ? 'rose' : 'fell';
56 const advice = total > baseline.throw ? 'return a Result instead (docs/STACK.md rule 1)' : `lower ${BASELINE_PATH} in this change (--write)`;
57 console.error(`throw-ratchet:\n throw ${verb} ${baseline.throw} -> ${total}; ${advice}`);
58 for (const f of files.slice(0, 15)) console.error(` ${f.file}: ${f.n}`);
59 process.exit(1);
60}
62if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) main();
Diagnostics4 git calls · 401 ms
CallStatusSizeTime0401 ms
discover200152 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200145 B · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=2 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=145 git.pkt.payload.max=76 git.ls_refs.count=2 git.ls_refs.head=refs/heads/main
fetchblob:none · deepen 1020038 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=38434 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=19912563f540228593df0c489082e5e2508e21e4 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=6 git.pkt.data=4 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=38434 git.pkt.payload.max=38339 git.sideband.band1.bytes=38338 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=63 git.pack.bytes=38338 git.pack.ofs_delta=0 git.pack.ref_delta=8 git.pack.chain.max=8 git.pack.trailer=0bc36ab7adf82d3109177f11f1ea93f1ce071f77 git.pack.full=55Open pack map
fetch2001.6 KiB397 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=397 git.response.bytes=1594 git.response.cache=MISS git.response.duration_ms=397 git.fetch.wants=25024017f9cdb04a487d9e774cb071667cedf8a7 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1594 git.pkt.payload.max=1573 git.sideband.band1.bytes=1572 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/ply.git
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e2cb4da8b27929
- Request
- 2577c1ca…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format