main
1// The v2 capability advertisement as git's client reads it (connect.c): the lines, and git's lookups over them. Its own
2// module so the fetch path can read one bit of it (does `fetch` take `filter`, fetch.ts) without discover's canary.
3// discover.ts re-exports both; the space of advertisements and ply's answer to each is test/space/advertisement.space.js.
4import type { ErrorOf, Extra } from './errors.js';
5import type { Frame, Scrub } from './transport.js';
6import type { Option, Result } from './result.js';
7import { fail, remoteQuoted, remoteText, REMOTE_TEXT_MAX } from './errors.js';
8import { flatMapOption, getOrElse, mapOption, none, ok, some } from './result.js';
9import { at, atOr, fromNullable } from './absent.js';
10import { td } from './bytes.js';
11import { proto } from './transport.js';
13const SERVICE = '# service=';
14const ERR = 'ERR ';
15const VERSION_2 = 'version 2';
16// parse_feature_value: features are separated by isspace()
17// oxlint-disable-next-line require-unicode-regexp -- ASCII whitespace; `u` costs a byte
18const SPACE = /[ \t\n\v\f\r]+/;
20// The capability lines of a v2 advertisement, as connect.c reads them: an optional "# service=…" pkt and its flush
21// (remote-curl's smart-HTTP preamble; http-backend omits it for v2), "version 2" (discover_version), then capability
22// lines up to a flush and nothing after it (process_capabilities_v2: "expected flush after capabilities"; over HTTP
23// a pkt after that flush is read as the next response and refused). Returns the lines in order, or the error.
24type DataFrame = Extract<Frame, { readonly kind: 'data' }>;
25// What a v2 advertisement can be refused with: a broken shape, the server's ERR line, an answer that is not v2.
26export type CapsV2Error = ErrorOf<'protocol' | 'remote_error' | 'unsupported'>;
27// oxlint-disable-next-line require-unicode-regexp -- ASCII text
28const text = (f: DataFrame): string => td.decode(f.payload).replace(/\n$/, '');
29// The data frame at `i`, if the frame there is one.
30const dataAt = (frames: readonly Frame[], i: number): Option<DataFrame> => flatMapOption(at(frames, i), (f): Option<DataFrame> => (f.kind === 'data' ? some(f) : none));
31// The kind of the frame at `i`, or 'end of stream' past the last one.
32const kindAt = (frames: readonly Frame[], i: number): string => getOrElse(mapOption(at(frames, i), f => f.kind), 'end of stream');
33// scrub: the transport's credential scrubber (or null), applied to an ERR line's text before it is cut (errors.ts remoteText).
34// @decoder parseCapabilitiesV2 prefix=caps
35export function parseCapabilitiesV2(frames: readonly Frame[], scrub: Scrub | null = null): Result<string[], CapsV2Error> {
36 let i = 0;
37 const preamble = dataAt(frames, 0);
38 if (preamble.tag === 'some' && text(preamble.value).startsWith(SERVICE)) {
39 // check_smart_http: "The header can include additional metadata lines, up until a packet flush marker"
40 i = frames.findIndex(f => f.kind !== 'data');
41 if (kindAt(frames, i) !== 'flush') return proto('advertisement: no flush after the service line');
42 i++;
43 // a header and nothing after it: the v2 lines, if any, were read as metadata (a preamble with no flush of its own),
44 // and remote-curl refuses that response
45 if (i >= frames.length) return proto('advertisement: nothing after the service line');
46 }
47 const head = dataAt(frames, i);
48 const first = head.tag === 'some' ? text(head.value) : '';
49 if (first.startsWith(ERR)) return fail('remote_error', `remote: ${remoteText(first.slice(ERR.length), REMOTE_TEXT_MAX, scrub)}`);
50 // oxlint-disable-next-line typescript/no-unsafe-type-assertion -- an ad-hoc error field (errors.ts Extra); A3 moves it into Detail
51 if (first !== VERSION_2) return fail('unsupported', `server did not answer protocol v2 (first line ${remoteQuoted(first)})`, { lines: [first] } as Extra);
52 const lines: string[] = [];
53 for (i++; ; i++) {
54 const line = dataAt(frames, i);
55 if (line.tag === 'none') break;
56 lines.push(text(line.value));
57 }
58 if (kindAt(frames, i) !== 'flush') return proto(`advertisement: expected flush after capabilities, got ${kindAt(frames, i)}`);
59 if (i !== frames.length - 1) return proto(`advertisement: ${kindAt(frames, i + 1)} after the capabilities' flush`);
60 return ok(lines);
61}
63// Lookups with git's semantics over the lines (first match wins, as connect.c walks server_capabilities_v2 in order):
64// has(key) server_supports_v2: a line `key` or `key=…`
65// value(key) server_feature_v2: the first line `key=…` (a bare `key` has no value: `object-format` alone is sha1)
66// features(key) server_supports_feature: the first line `key` or `key=…`, its value split on whitespace, each name
67// without its `=value` (parse_feature_value)
68export type CapLookup = {
69 readonly has: (key: string) => boolean;
70 readonly value: (key: string) => string | null;
71 readonly features: (key: string) => string[];
72};
73export const capLookup = (lines: readonly string[]): CapLookup => {
74 const has = (key: string): boolean => lines.some(l => l === key || l.startsWith(`${key}=`));
75 const value = (key: string): string | null => getOrElse(mapOption(fromNullable(lines.find(x => x.startsWith(`${key}=`))), l => l.slice(key.length + 1)), null);
76 const features = (key: string): string[] => {
77 const l = fromNullable(lines.find(x => x === key || x.startsWith(`${key}=`)));
78 // split() returns at least one element
79 return getOrElse(mapOption(l, x => x.slice(key.length + 1).split(SPACE).filter(Boolean).map(f => atOr(f.split('='), 0, ''))), []);
80 };
81 return { has, value, features };
82};
Diagnostics4 git calls · 285 ms
CallStatusSizeTime0285 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 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 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
fetch2002.3 KiB280 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=280 git.response.bytes=2305 git.response.cache=MISS git.response.duration_ms=280 git.fetch.wants=38d3a946c252d7fcf3f0439a3dbe6be98ae588f5 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=2305 git.pkt.payload.max=2284 git.sideband.band1.bytes=2283 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
- a5e2bb9f5ac8cfc3
- Request
- ab544814…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format