main
1// v2 negotiation: send haves in rounds without `done` until the server says ready (or we run out), then done.
2// Returns the final fetchPack result plus the rounds' acks. `external` supplies thin-pack bases.
3import type { Transport } from './transport.js';
4import type { FetchOpts } from './fetch.js';
5import type { FetchPackExtra, FetchPackResult } from './fetchpack.js';
6import type { ErrorOf, PlyError } from './errors.js';
7import type { Input } from './absent.js';
8import type { Result } from './result.js';
9import { fail, remoteQuoted } from './errors.js';
10import { checkLimit } from './limits.js';
11import { fetchPack } from './fetchpack.js';
12import { fetchRequest } from './fetch.js';
13import { parseOid } from './oid.js';
14import type { Oid } from './oid.js';
15import { getOrElse, ok } from './result.js';
16import { fromNullable, withDefaults } from './absent.js';
18const NEGOTIATE_BATCH = 16;
19const NEGOTIATE_ROUNDS = 32;
20// characters of a bad acknowledgment quoted in the refusal
21const ACK_SHOWN = 60;
23// The fetch arguments, plus the haves to offer in rounds of `batch` (at most `maxRounds` rounds).
24export type NegotiateConfig = { readonly batch: number; readonly maxRounds: number };
25export type NegotiateOpts = Omit<FetchOpts, 'done'> & Input<NegotiateConfig>;
26export type Round = { readonly sent: number; readonly acks: readonly string[]; readonly nak: boolean; readonly ready: boolean; readonly pack: boolean };
27export type NegotiateResult = FetchPackResult & { readonly rounds: Round[]; readonly common: string[] };
29// The round count and batch size, checked before any request: finite non-negative numbers, the batch at least one.
30type Caps = { readonly rounds: number; readonly batch: number };
31function negotiateCaps(maxRounds: unknown, batch: unknown): Result<Caps, ErrorOf<'bad_request'>> {
32 const rounds = checkLimit(maxRounds, 'maxNegotiateRounds');
33 if (!rounds.ok) return rounds;
34 const size = checkLimit(batch, 'negotiateBatch');
35 if (!size.ok) return size;
36 if (!Number.isInteger(rounds.value)) return fail('bad_request', 'negotiate rounds must be an integer', { what: 'maxNegotiateRounds' });
37 if (!Number.isInteger(size.value) || size.value < 1) return fail('bad_request', 'negotiate batch must be a positive integer', { what: 'negotiateBatch' });
38 return ok({ rounds: rounds.value, batch: size.value });
39}
41export async function negotiate(t: Transport, opts: NegotiateOpts, extra: Pick<FetchPackExtra, 'external' | 'resolve'> = {}): Promise<Result<NegotiateResult, PlyError>> {
42 const { wants, haves: givenHaves, batch: _batch, maxRounds: _maxRounds, ...rest } = opts;
43 const { batch, maxRounds } = withDefaults(opts, { batch: NEGOTIATE_BATCH, maxRounds: NEGOTIATE_ROUNDS });
44 const { external, resolve } = extra;
45 const checked = negotiateCaps(maxRounds, batch);
46 if (!checked.ok) return checked;
47 const caps = checked.value;
48 // The whole command parsed once, every have with it (#156): a bad one is refused before the first round, not in the
49 // round that would have sent it. The rounds send the parsed ids.
50 const parsed = fetchRequest({ ...rest, wants, haves: getOrElse(fromNullable(givenHaves), []) });
51 if (!parsed.ok) return parsed;
52 const { haves } = parsed.value;
53 // what the server acknowledged: each an id, offered again as a have in every later round
54 const acked = new Set<Oid>();
55 const rounds: Round[] = [];
56 let i = 0, ready = false;
57 for (let round = 0; round < caps.rounds && i < haves.length && !ready; round++) {
58 const sent = haves.slice(i, i + caps.batch); i += caps.batch;
59 // oxlint-disable-next-line no-await-in-loop -- each round's haves depend on the last round's acks
60 const fetched = await fetchPack(t, { ...rest, wants, haves: [...acked, ...sent], done: false }, { external, resolve });
61 if (!fetched.ok) return fetched;
62 const r = fetched.value;
63 for (const a of r.acks) {
64 const id = parseOid(a);
65 if (!id.ok) return fail('protocol', `negotiate: the server acknowledged ${remoteQuoted(a, ACK_SHOWN)}, not an object id`);
66 acked.add(id.value);
67 }
68 rounds.push({ sent: sent.length, acks: r.acks, nak: r.nak, ready: r.ready, pack: Boolean(r.parsed) });
69 ready = r.ready;
70 if (r.parsed) return ok({ ...r, rounds, common: [...acked] }); // server went straight to packfile
71 }
72 const r = await fetchPack(t, { ...rest, wants, haves: [...acked], done: true }, { external, resolve });
73 return r.ok ? ok({ ...r.value, rounds, common: [...acked] }) : r;
74}
Diagnostics4 git calls · 40 ms
CallStatusSizeTime040 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 · 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 · 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.7 KiB · hit34 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=34 git.response.bytes=1735 git.response.cache=HIT git.response.duration_ms=34 git.fetch.wants=7cc18509235e07332af0e94f534043bd806c737e 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=1735 git.pkt.payload.max=1714 git.sideband.band1.bytes=1713 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
- a5e1e6a35af3d96e
- Request
- 98461aa0…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format