main
1// The workbench's own reading of the git wire, independent of ply's parsers: the pkt-lines of a request or an answer,
2// the refs and capabilities of an advertisement, the commands of a receive-pack request, the report-status a server
3// sends back (through side-band-64k when it was asked for), and what a v2 request asks. The verdict compares this
4// reading with ply's (src/bench/verdict.ts), so a disagreement between ply and the bytes is evidence, not a guess.
5// Pure: bytes in, values out. Object ids and ref names only; a credential is never in a git body.
6import { parseOid, type Oid } from 'ply';
8const FLUSH = 0;
9const DELIM = 1;
10const RESPONSE_END = 2;
11const HEADER_BYTES = 4;
12const MAX_LINES = 2_000;
13const TEXT_MAX = 400;
14const BAND_DATA = 1;
15const BAND_PROGRESS = 2;
16const BAND_ERROR = 3;
17const PACK_SIGNATURE = [0x50, 0x41, 0x43, 0x4b] as const;
19export type Pkt =
20 | { readonly tag: 'data'; readonly bytes: Uint8Array }
21 | { readonly tag: 'flush' }
22 | { readonly tag: 'delim' }
23 | { readonly tag: 'response-end' };
25// Where the framing stopped: the end of the body, a raw pack, a line cap, or bytes that are not a pkt-line.
26export type PktStop = 'end' | 'pack' | 'cap' | 'malformed' | 'truncated';
27export type PktRead = { readonly pkts: readonly Pkt[]; readonly stop: PktStop; readonly consumed: number };
29const HEX = /^[0-9a-f]{4}$/;
30const latin1 = new TextDecoder('latin1');
31const utf8 = new TextDecoder('utf-8', { fatal: false });
33function isPackAt(buf: Uint8Array, at: number): boolean {
34 return PACK_SIGNATURE.every((b, i) => buf[at + i] === b);
35}
37export function readPkts(buf: Uint8Array, limit: number = MAX_LINES): PktRead {
38 const pkts: Pkt[] = [];
39 let at = 0;
40 while (at < buf.length) {
41 if (pkts.length >= limit) return { pkts, stop: 'cap', consumed: at };
42 if (isPackAt(buf, at)) return { pkts, stop: 'pack', consumed: at };
43 if (at + HEADER_BYTES > buf.length) return { pkts, stop: 'truncated', consumed: at };
44 const head = latin1.decode(buf.subarray(at, at + HEADER_BYTES));
45 if (!HEX.test(head)) return { pkts, stop: 'malformed', consumed: at };
46 const n = parseInt(head, 16);
47 if (n === FLUSH) { pkts.push({ tag: 'flush' }); at += HEADER_BYTES; continue; }
48 if (n === DELIM) { pkts.push({ tag: 'delim' }); at += HEADER_BYTES; continue; }
49 if (n === RESPONSE_END) { pkts.push({ tag: 'response-end' }); at += HEADER_BYTES; continue; }
50 if (n < HEADER_BYTES) return { pkts, stop: 'malformed', consumed: at };
51 if (at + n > buf.length) return { pkts, stop: 'truncated', consumed: at };
52 pkts.push({ tag: 'data', bytes: buf.subarray(at + HEADER_BYTES, at + n) });
53 at += n;
54 }
55 return { pkts, stop: 'end', consumed: at };
56}
58const textOf = (bytes: Uint8Array): string => latin1.decode(bytes).replace(/\n$/, '');
59const dataLines = (pkts: readonly Pkt[]): string[] => pkts.flatMap(p => (p.tag === 'data' ? [textOf(p.bytes)] : []));
61// ---- advertisement (GET info/refs?service=...) ----
62export type AdvertisedRef = { readonly oid: Oid; readonly ref: string };
63export type Advertisement = {
64 readonly service: string | null;
65 readonly version: 'v0' | 'v2';
66 readonly refs: readonly AdvertisedRef[];
67 readonly capabilities: readonly string[];
68};
70function oidRef(line: string): AdvertisedRef | null {
71 const space = line.indexOf(' ');
72 if (space < 0) return null;
73 const oid = parseOid(line.slice(0, space));
74 if (!oid.ok) return null;
75 return { oid: oid.value, ref: line.slice(space + 1) };
76}
78export function readAdvertisement(body: Uint8Array): Advertisement {
79 const lines = dataLines(readPkts(body).pkts);
80 let service: string | null = null;
81 let version: 'v0' | 'v2' = 'v0';
82 const refs: AdvertisedRef[] = [];
83 let capabilities: string[] = [];
84 for (const line of lines) {
85 if (line.startsWith('# service=')) { service = line.slice('# service='.length); continue; }
86 if (line === 'version 2') { version = 'v2'; continue; }
87 if (version === 'v2') { capabilities.push(line); continue; }
88 const nul = line.indexOf('\0');
89 const head = nul < 0 ? line : line.slice(0, nul);
90 if (nul >= 0) capabilities = line.slice(nul + 1).split(' ').filter(c => c.length > 0);
91 const r = oidRef(head);
92 if (r !== null && r.ref !== 'capabilities^{}') refs.push(r);
93 }
94 return { service, version, refs, capabilities };
95}
97// ---- a receive-pack request: commands, then a pack ----
98export type Command = { readonly old: Oid; readonly new: Oid; readonly ref: string };
99export type ReceiveRequest = { readonly commands: readonly Command[]; readonly capabilities: readonly string[]; readonly shallow: readonly Oid[]; readonly packBytes: number };
101export function readReceiveRequest(body: Uint8Array): ReceiveRequest {
102 const read = readPkts(body);
103 const commands: Command[] = [];
104 const shallow: Oid[] = [];
105 let capabilities: string[] = [];
106 for (const line of dataLines(read.pkts)) {
107 if (line.startsWith('shallow ')) {
108 const s = parseOid(line.slice('shallow '.length));
109 if (s.ok) shallow.push(s.value);
110 continue;
111 }
112 const nul = line.indexOf('\0');
113 if (nul >= 0) capabilities = line.slice(nul + 1).split(' ').filter(c => c.length > 0);
114 const [old, next, ref] = (nul < 0 ? line : line.slice(0, nul)).split(' ');
115 const o = parseOid(String(old));
116 const n = parseOid(String(next));
117 if (o.ok && n.ok && typeof ref === 'string' && ref.length > 0) commands.push({ old: o.value, new: n.value, ref });
118 }
119 return { commands, capabilities, shallow, packBytes: read.stop === 'pack' ? body.length - read.consumed : 0 };
120}
122// ---- side-band-64k ----
123export type Sideband = {
124 readonly bands: { readonly data: number; readonly progress: number; readonly error: number };
125 readonly progress: string;
126 readonly error: string;
127};
128// The demultiplexed band-1 bytes beside the summary; only the summary is kept with an exchange.
129export type Demuxed = { readonly sideband: Sideband; readonly data: Uint8Array };
131export function demuxSideband(pkts: readonly Pkt[]): Demuxed {
132 const parts: Uint8Array[] = [];
133 let data = 0;
134 let progress = 0;
135 let error = 0;
136 let progressText = '';
137 let errorText = '';
138 for (const p of pkts) {
139 if (p.tag !== 'data' || p.bytes.length === 0) continue;
140 const band = p.bytes[0];
141 const payload = p.bytes.subarray(1);
142 if (band === BAND_DATA) { data += payload.length; parts.push(payload); }
143 else if (band === BAND_PROGRESS) { progress += payload.length; if (progressText.length < TEXT_MAX) progressText += utf8.decode(payload); }
144 else if (band === BAND_ERROR) { error += payload.length; if (errorText.length < TEXT_MAX) errorText += utf8.decode(payload); }
145 }
146 const joined = new Uint8Array(data);
147 let at = 0;
148 for (const part of parts) { joined.set(part, at); at += part.length; }
149 return { sideband: { bands: { data, progress, error }, progress: progressText.slice(0, TEXT_MAX), error: errorText.slice(0, TEXT_MAX) }, data: joined };
150}
152const isBanded = (pkts: readonly Pkt[]): boolean => {
153 const first = pkts.find(p => p.tag === 'data');
154 if (first === null || typeof first !== 'object' || first.tag !== 'data' || first.bytes.length === 0) return false;
155 const b = first.bytes[0];
156 return b === BAND_DATA || b === BAND_PROGRESS || b === BAND_ERROR;
157};
159// ---- report-status: what the server says it did with a push ----
160export type Unpack = { readonly tag: 'ok' } | { readonly tag: 'error'; readonly reason: string };
161export type RefStatus = { readonly tag: 'ok'; readonly ref: string } | { readonly tag: 'ng'; readonly ref: string; readonly reason: string };
162export type Report =
163 | { readonly tag: 'report'; readonly unpack: Unpack; readonly refs: readonly RefStatus[]; readonly sideband: Sideband | null }
164 // the answer carried no report-status (empty, cut, or only an error band)
165 | { readonly tag: 'none'; readonly why: string; readonly sideband: Sideband | null };
167export function readReport(body: Uint8Array): Report {
168 if (body.length === 0) return { tag: 'none', why: 'empty answer', sideband: null };
169 const outer = readPkts(body);
170 const banded = isBanded(outer.pkts);
171 const demuxed = banded ? demuxSideband(outer.pkts) : null;
172 const sideband = demuxed === null ? null : demuxed.sideband;
173 const lines = dataLines(demuxed === null ? outer.pkts : readPkts(demuxed.data).pkts);
174 const unpackLine = lines.find(l => l.startsWith('unpack '));
175 if (typeof unpackLine !== 'string') return { tag: 'none', why: sideband !== null && sideband.error.length > 0 ? `no report; band 3: ${sideband.error}` : `no report-status (${outer.stop})`, sideband };
176 const said = unpackLine.slice('unpack '.length);
177 const unpack: Unpack = said === 'ok' ? { tag: 'ok' } : { tag: 'error', reason: said };
178 const refs: RefStatus[] = [];
179 for (const l of lines) {
180 if (l.startsWith('ok ')) refs.push({ tag: 'ok', ref: l.slice(3) });
181 else if (l.startsWith('ng ')) {
182 const rest = l.slice(3);
183 const sp = rest.indexOf(' ');
184 refs.push(sp < 0 ? { tag: 'ng', ref: rest, reason: '' } : { tag: 'ng', ref: rest.slice(0, sp), reason: rest.slice(sp + 1) });
185 }
186 }
187 return { tag: 'report', unpack, refs, sideband };
188}
190// What the report says happened to the push as a whole: every ref ok, some, none, or no report at all.
191export type ReportVerdict = 'all-ok' | 'some-ok' | 'none-ok' | 'no-report';
192export function reportVerdict(r: Report): ReportVerdict {
193 if (r.tag === 'none') return 'no-report';
194 if (r.unpack.tag === 'error') return 'none-ok';
195 const ok = r.refs.filter(x => x.tag === 'ok').length;
196 if (r.refs.length > 0 && ok === r.refs.length) return 'all-ok';
197 return ok > 0 ? 'some-ok' : 'none-ok';
198}
200// ---- protocol v2 requests ----
201export type V2Request = { readonly command: string | null; readonly args: readonly string[] };
202export function readV2Request(body: Uint8Array): V2Request {
203 const lines = dataLines(readPkts(body).pkts);
204 const cmd = lines.find(l => l.startsWith('command='));
205 const args = lines.filter(l => !l.startsWith('command=') && !l.startsWith('agent=') && !l.startsWith('object-format=')).slice(0, 64);
206 return { command: typeof cmd === 'string' ? cmd.slice('command='.length) : null, args };
207}
209// The answer of a v2 fetch: its sections, and the side-band of its packfile section.
210export type FetchAnswer = { readonly sections: readonly string[]; readonly sideband: Sideband | null };
211export function readFetchAnswer(body: Uint8Array): FetchAnswer {
212 const pkts = readPkts(body).pkts;
213 const sections: string[] = [];
214 let at = -1;
215 for (const [i, p] of pkts.entries()) {
216 if (p.tag !== 'data') continue;
217 const t = textOf(p.bytes);
218 if (t === 'acknowledgments' || t === 'shallow-info' || t === 'wanted-refs' || t === 'packfile-uris' || t === 'packfile') { sections.push(t); if (t === 'packfile') at = i; }
219 }
220 return { sections, sideband: at < 0 ? null : demuxSideband(pkts.slice(at + 1)).sideband };
221}
Diagnostics4 git calls · 185 ms
CallStatusSizeTime0185 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 · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=145 git.response.cache=HIT git.response.duration_ms=3 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 1020040 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=41349 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=526b305930b8ef382f2d7a1f9c0238730a585921 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=41349 git.pkt.payload.max=41254 git.sideband.band1.bytes=41253 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=61 git.pack.bytes=41253 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c git.pack.full=61Open pack map
fetch2003.4 KiB180 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=180 git.response.bytes=3513 git.response.cache=MISS git.response.duration_ms=180 git.fetch.wants=15152cc8287d57666b11caa6d9375382dcb2de00 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=3513 git.pkt.payload.max=3492 git.sideband.band1.bytes=3491 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-web.git
- Ref
- refs/heads/main
- Commit
- 526b305930b8ef382f2d7a1f9c0238730a585921
- cf-ray
- a5e2f2e87c127089
- Request
- 8e10700c…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format