GitHubivarvong
ply
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();
Diagnostics6 git calls · 199 ms
CallStatusSizeTime0199 ms
discover200191 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=191 git.server.agent=git/github-9922ec520325-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=191 git.pkt.payload.max=36
ls-refs20012 KiB95 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=95 git.response.bytes=11990 git.response.cache=EXPIRED git.response.duration_ms=95 git.protocol.version=2 git.pkt.frames=152 git.pkt.data=151 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=11990 git.pkt.payload.max=122 git.ls_refs.count=151 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 102004.2 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=4347 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 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=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4347 git.pkt.payload.max=4247 git.sideband.band1.bytes=4247 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=10 git.pack.bytes=4247 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=a6caf8e129bea82c9bae4cbc7c130c9205add400 git.pack.full=10Open pack map
fetchtree:0200919 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=919 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=800d30ba8923860bfc460ca884b7f9ba22083588 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=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=919 git.pkt.payload.max=892 git.sideband.band1.bytes=892 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=892 git.pack.trailer=5323b9266eea5ded637285d1c5320699f12be76eOpen pack map
fetchtree:0200831 B · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=831 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=b9290e370bd091fd3272931655a175dd1a7af82d 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=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=831 git.pkt.payload.max=804 git.sideband.band1.bytes=804 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=804 git.pack.trailer=81ddc1f73214935b4ee953ad84f959db955ce0d1Open pack map
fetch2001.6 KiB95 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=95 git.response.bytes=1607 git.response.cache=MISS git.response.duration_ms=95 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=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=1607 git.pkt.payload.max=1580 git.sideband.band1.bytes=1580 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://github.com/ivarvong/ply
- Ref
- refs/heads/main
- Commit
- 19912563f540228593df0c489082e5e2508e21e4
- cf-ray
- a5e17f3228023014
- Request
- 368400d4…
- Protocol
- v2 · sha1
- Agent
- git/github-9922ec520325-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format