Artifactspublic-v0

ply

main
ply4 git calls32 ms41 KiB3 cachedprotocol v2
186 lines8.9 KiBRaw
1// Decoding a v2 upload-pack request: the inverse of fetchBody (fetch.ts) and lsRefsBody (lsrefs.ts), for the cache
2// that has to say what a request asks for (packcache.ts exchangeKind, verifyFetchAnswer). A pkt-line step machine
3// over the frames pktStep reads, one line at a time (no pattern over the whole body), as git's serve.c process_request
4// reads it: `command=<name>`, capability lines, a delim, the command's arguments, a flush, and nothing after it.
5// Every argument upload-pack.c process_args (fetch) and ls-refs.c (ls-refs) accepts is decoded; anything else is
6// refused, so a request the cache cannot read is never judged pure.
7/* oxlint-disable require-unicode-regexp -- ASCII wire text; `u` costs a byte per literal */
8import type { ErrorOf } from './errors.js';
9import type { PktFrame } from './pktline.js';
10import type { Result } from './result.js';
11import { fail } from './errors.js';
12import { decodeBytes } from './bytes.js';
13import { pktInit, pktStep, pktFinish } from './pktline.js';
14import { OID_HEX_CHARS } from './format.js';
15import { assertNever, ok } from './result.js';
17export type LsRefsRequest = {
18 readonly command: 'ls-refs';
19 readonly caps: readonly string[];
20 readonly prefixes: readonly string[];
21 readonly peel: boolean;
22 readonly symrefs: boolean;
23 readonly unborn: boolean;
24};
25// The fields of FetchConfig (fetch.ts) with their absent values (an empty filter, a zero deepen and deepen-since), so
26// parseUploadPackRequest(fetchBody(x)) is x with its defaults filled; plus what git sends and ply does not.
27export type FetchRequest = {
28 readonly command: 'fetch';
29 readonly caps: readonly string[];
30 readonly wants: readonly string[];
31 readonly wantRefs: readonly string[];
32 readonly haves: readonly string[];
33 readonly shallows: readonly string[];
34 readonly filter: string;
35 readonly deepen: number;
36 readonly deepenSince: number;
37 readonly deepenNot: readonly string[];
38 readonly deepenRelative: boolean;
39 readonly thinPack: boolean;
40 readonly ofsDelta: boolean;
41 readonly includeTag: boolean;
42 readonly noProgress: boolean;
43 readonly done: boolean;
44 readonly sidebandAll: boolean;
45 readonly waitForDone: boolean;
46 readonly packfileUris: string;
47};
48export type UploadPackRequest = LsRefsRequest | FetchRequest;
50type Bad = ErrorOf<'protocol'>;
51const bad = (msg: string): Result<never, Bad> => fail('protocol', `upload-pack request: ${msg}`);
53const OID = new RegExp(`^[0-9a-fA-F]{${OID_HEX_CHARS}}$`);
54const DIGITS = /^[0-9]+$/;
55type Mutable<T> = { -readonly [K in keyof T]: T[K] extends ReadonlyArray<infer U> ? U[] : T[K] };
57const lsRefsInit = (caps: string[]): Mutable<LsRefsRequest> => ({ command: 'ls-refs', caps, prefixes: [], peel: false, symrefs: false, unborn: false });
58const fetchInit = (caps: string[]): Mutable<FetchRequest> => ({
59 command: 'fetch', caps, wants: [], wantRefs: [], haves: [], shallows: [], filter: '', deepen: 0, deepenSince: 0, deepenNot: [],
60 deepenRelative: false, thinPack: false, ofsDelta: false, includeTag: false, noProgress: false, done: false, sidebandAll: false, waitForDone: false, packfileUris: '',
61});
63// A positive count: git's upload-pack dies on a deepen or deepen-since it cannot read as one (fetchBody leaves 0 out).
64function count(value: string, what: string): Result<number, Bad> {
65 const n = Number(value);
66 if (!DIGITS.test(value) || !Number.isSafeInteger(n) || n === 0) return bad(`${what} must be a positive integer`);
67 return ok(n);
70const FETCH_FLAGS = {
71 'done': 'done', 'thin-pack': 'thinPack', 'ofs-delta': 'ofsDelta', 'include-tag': 'includeTag', 'no-progress': 'noProgress',
72 'deepen-relative': 'deepenRelative', 'sideband-all': 'sidebandAll', 'wait-for-done': 'waitForDone',
73} as const;
74const FETCH_OIDS = { want: 'wants', have: 'haves', shallow: 'shallows' } as const;
75const FETCH_NAMES = { 'want-ref': 'wantRefs', 'deepen-not': 'deepenNot' } as const;
76const isKey = <O extends object>(o: O, k: string): k is Extract<keyof O, string> => Object.hasOwn(o, k);
78// The single-valued arguments: a second one is refused, as git dies on a second filter.
79const FETCH_ONCE = { 'filter': 'filter', 'packfile-uris': 'packfileUris' } as const;
80const FETCH_COUNTS = { 'deepen': 'deepen', 'deepen-since': 'deepenSince' } as const;
82// One fetch argument: a flag, an oid, a name, or one of the single-valued arguments.
83function fetchArg(r: Mutable<FetchRequest>, key: string, value: string | null): Result<null, Bad> {
84 if (value === null) {
85 if (!isKey(FETCH_FLAGS, key)) return bad(`unknown fetch argument ${JSON.stringify(key)}`);
86 r[FETCH_FLAGS[key]] = true;
87 return ok(null);
88 }
89 if (isKey(FETCH_OIDS, key)) {
90 if (!OID.test(value)) return bad(`${key} must be a ${OID_HEX_CHARS}-hex oid`);
91 r[FETCH_OIDS[key]].push(value);
92 return ok(null);
93 }
94 if (isKey(FETCH_NAMES, key)) {
95 if (value === '') return bad(`${key} needs a value`);
96 r[FETCH_NAMES[key]].push(value);
97 return ok(null);
98 }
99 if (isKey(FETCH_ONCE, key)) {
100 const field = FETCH_ONCE[key];
101 if (r[field] !== '' || value === '') return bad(`${key} must be given once, with a value`);
102 r[field] = value;
103 return ok(null);
104 }
105 if (isKey(FETCH_COUNTS, key)) {
106 const field = FETCH_COUNTS[key];
107 const n = count(value, key);
108 if (!n.ok) return n;
109 if (r[field] !== 0) return bad(`a second ${key}`);
110 r[field] = n.value;
111 return ok(null);
112 }
113 return bad(`unknown fetch argument ${JSON.stringify(key)}`);
116function lsRefsArg(r: Mutable<LsRefsRequest>, key: string, value: string | null): Result<null, Bad> {
117 if (value === null && (key === 'peel' || key === 'symrefs' || key === 'unborn')) { r[key] = true; return ok(null); }
118 // an empty prefix is a prefix (lsRefsBody writes one, git matches every ref with it)
119 if (key === 'ref-prefix' && value !== null) { r.prefixes.push(value); return ok(null); }
120 return bad(`unknown ls-refs argument ${JSON.stringify(key)}`);
123// The machine: `command` (the first line), `caps` (until the delim, or a flush when there are no arguments), `args`
124// (until the flush), `ended` (nothing may follow).
125type ReqState =
126 | { readonly tag: 'command' }
127 | { readonly tag: 'caps'; readonly command: 'fetch' | 'ls-refs'; readonly caps: string[] }
128 | { readonly tag: 'args'; readonly request: Mutable<UploadPackRequest> }
129 | { readonly tag: 'ended'; readonly request: Mutable<UploadPackRequest> };
131// git's packet reader chomps one trailing LF (PACKET_READ_CHOMP_NEWLINE): `want x` and `want x\n` are one line.
132const lineOf = (payload: Uint8Array): string => { const s = decodeBytes(payload); return s.endsWith('\n') ? s.slice(0, -1) : s; };
133const COMMAND = 'command=';
134const start = (command: 'fetch' | 'ls-refs', caps: string[]): Mutable<UploadPackRequest> => (command === 'fetch' ? fetchInit(caps) : lsRefsInit(caps));
136function commandLine(f: PktFrame): Result<ReqState, Bad> {
137 if (f.kind !== 'data') return bad(`${f.kind} before the command line`);
138 const line = lineOf(f.payload);
139 if (!line.startsWith(COMMAND)) return bad('the first line is not command=<name>');
140 const command = line.slice(COMMAND.length);
141 if (command !== 'fetch' && command !== 'ls-refs') return bad(`unsupported command ${JSON.stringify(command)}`);
142 return ok({ tag: 'caps', command, caps: [] });
145function argLine(st: Extract<ReqState, { readonly tag: 'args' }>, f: PktFrame): Result<ReqState, Bad> {
146 if (f.kind === 'flush') return ok({ tag: 'ended', request: st.request });
147 if (f.kind !== 'data') return bad(`a second ${f.kind}`);
148 const line = lineOf(f.payload);
149 const sp = line.indexOf(' ');
150 const key = sp < 0 ? line : line.slice(0, sp);
151 const value = sp < 0 ? null : line.slice(sp + 1);
152 const r = st.request.command === 'fetch' ? fetchArg(st.request, key, value) : lsRefsArg(st.request, key, value);
153 return r.ok ? ok(st) : r;
156function reqStep(st: ReqState, f: PktFrame): Result<ReqState, Bad> {
157 if (f.kind === 'end') return bad('response-end inside a request');
158 switch (st.tag) {
159 case 'ended': return bad(`${f.kind} after the flush that ends the request`);
160 case 'command': return commandLine(f);
161 case 'caps': {
162 if (f.kind === 'data') { st.caps.push(lineOf(f.payload)); return ok(st); }
163 const request = start(st.command, st.caps);
164 return ok(f.kind === 'delim' ? { tag: 'args', request } : { tag: 'ended', request });
165 }
166 case 'args': return argLine(st, f);
167 }
168 // Stryker disable next-line ConditionalExpression: unreachable, the compiler's exhaustiveness check
169 return assertNever(st);
172// @decoder parseUploadPackRequest prefix=upload-pack-request
173export function parseUploadPackRequest(body: Uint8Array): Result<UploadPackRequest, Bad> {
174 const read = pktStep(pktInit(), body);
175 if (!read.ok) return read;
176 const fin = pktFinish(read.value.state);
177 if (!fin.ok) return fin;
178 let st: ReqState = { tag: 'command' };
179 for (const f of read.value.frames) {
180 const next = reqStep(st, f);
181 if (!next.ok) return next;
182 st = next.value;
183 }
184 if (st.tag !== 'ended') return bad('no flush ends the request');
185 return ok(st.request);
Diagnostics4 git calls · 32 ms
CallStatusSizeTime032 ms
discover200152 B4 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
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 · hit4 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=4
git.response.bytes=145
git.response.cache=HIT
git.response.duration_ms=4
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 · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=25
git.response.bytes=38434
git.response.cache=HIT
git.response.duration_ms=3
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=55
Open pack map
fetch2003.1 KiB · hit25 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=3212
git.response.cache=HIT
git.response.duration_ms=25
git.fetch.wants=477d384f2c417c864439f563b9263e579135e639
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=3212
git.pkt.payload.max=3191
git.sideband.band1.bytes=3190
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
a5e1a22f08173cd0
Request
1eb63ab2…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format