Artifactspublic-v0

ply

main
ply4 git calls28 ms41 KiB3 cachedprotocol v2
126 lines9.1 KiBRaw
1// parseUploadPackRequest is the inverse of fetchBody / lsRefsBody (#120): every request ply encodes parses back to the
2// spec it was built from (seeded property over generated requests), every refusal is typed, and exchangeKind / isPureFetch
3// classify the result. The real-git side (requests git itself sends, verifyFetchAnswer, cachingFetch) is in
4// test/packcache.test.js.
5import { test } from 'vitest';
6import assert from 'node:assert/strict';
7import { te, concat, decodeBytes } from '../src/bytes.js';
8import { pkt, FLUSH, DELIM } from '../src/pktline.js';
9import { fetchBody as fetchBodyR } from '../src/fetch.js';
10import { lsRefsBody as lsRefsBodyR } from '../src/lsrefs.js';
11import { parseUploadPackRequest, exchangeKind, isPureFetch, isCacheableFetch } from '../src/packcache.js';
12import { unwrap } from './unwrap.js';
14const fetchBody = o => unwrap(fetchBodyR(o));
15const lsRefsBody = (p, o) => unwrap(lsRefsBodyR(p, o));
16const parse = b => unwrap(parseUploadPackRequest(b));
18// mulberry32: the same seed is the same 2,000 requests
19const rng = seed => () => { seed |= 0; seed = (seed + 0x6D2B79F5) | 0; let t = Math.imul(seed ^ (seed >>> 15), 1 | seed); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };
20const R = rng(20261104);
21const int = n => Math.floor(R() * n);
22const pick = xs => xs[int(xs.length)];
23const bool = () => R() < 0.5;
24const many = (n, f) => Array.from({ length: int(n + 1) }, f);
25const oid = () => Array.from({ length: 40 }, () => '0123456789abcdefABCDEF'[int(22)]).join('');
26// names as ply reads them: ASCII, non-ASCII, and an escaped non-UTF-8 byte (decodeBytes, #43)
27const name = () => pick(['refs/heads/main', 'refs/tags/v1.0', 'refs/heads/é', decodeBytes(new Uint8Array([0x72, 0x65, 0x66, 0x73, 0x2f, 0xff])), `refs/heads/b${int(1000)}`]);
29const fetchSpec = () => ({
30 wants: many(3, oid), haves: many(2, oid), shallows: many(2, oid),
31 filter: pick(['', 'tree:0', 'blob:none', 'blob:limit=1k', 'combine:tree:0+blob:none']),
32 deepen: pick([0, 0, 1, 7, 2 ** 31]), deepenSince: pick([0, 0, 1, 1761955200]), deepenNot: many(2, name),
33 deepenRelative: bool(), thinPack: bool(), ofsDelta: bool(), includeTag: bool(), noProgress: bool(), done: bool(),
34});
35// what the parser hands back for a spec: the spec with git's extra arguments absent
36const parsedFetch = s => ({ command: 'fetch', caps: [], wantRefs: [], sidebandAll: false, waitForDone: false, packfileUris: '', ...s });
38test('round trip: parseUploadPackRequest(fetchBody(x)) is x, over 2,000 generated fetch requests', () => {
39 for (let i = 0; i < 2000; i++) {
40 const s = fetchSpec();
41 // a want, a have and a shallow are each parsed once into an id, lowercased (git reads either case as one object, #156)
42 const lower = ids => ids.map(w => w.toLowerCase());
43 assert.deepEqual(parse(fetchBody(s)), parsedFetch({ ...s, wants: lower(s.wants), haves: lower(s.haves), shallows: lower(s.shallows) }), JSON.stringify(s));
44 }
45 // left-out arguments read as fetchBody's defaults (ofs-delta, no-progress and done on)
46 assert.deepEqual(parse(fetchBody({ wants: ['a'.repeat(40)] })), parsedFetch({ wants: ['a'.repeat(40)], haves: [], shallows: [], filter: '', deepen: 0, deepenSince: 0, deepenNot: [], deepenRelative: false, thinPack: false, ofsDelta: true, includeTag: false, noProgress: true, done: true }));
47});
49test('round trip: parseUploadPackRequest(lsRefsBody(p, o)) is (p, o), over 500 generated ls-refs requests', () => {
50 for (let i = 0; i < 500; i++) {
51 const prefixes = many(4, () => pick(['', 'HEAD', 'refs/heads/', 'refs/tags/', name()]));
52 const o = { peel: bool(), symrefs: bool(), unborn: bool() };
53 assert.deepEqual(parse(lsRefsBody(prefixes, o)), { command: 'ls-refs', caps: [], prefixes, ...o });
54 }
55});
57// a request framed by hand: lines (strings), DELIM and FLUSH markers, in order
58const raw = (...parts) => concat(parts.map(p => (p === DELIM || p === FLUSH ? p : unwrap(pkt(p)))));
59const W = 'b'.repeat(40);
61test('requests git itself writes: capability lines, chomped and unchomped lines, every fetch argument', () => {
62 const r = parse(raw('command=fetch\n', 'agent=git/2.50.0\n', 'object-format=sha1', DELIM, `want ${W}`, 'want-ref refs/heads/main\n', 'sideband-all\n', 'wait-for-done\n', 'packfile-uris https,http\n', 'thin-pack', FLUSH));
63 assert.deepEqual(r.caps, ['agent=git/2.50.0', 'object-format=sha1']);
64 assert.deepEqual(r.wants, [W]); assert.deepEqual(r.wantRefs, ['refs/heads/main']);
65 assert.equal(r.sidebandAll, true); assert.equal(r.waitForDone, true); assert.equal(r.packfileUris, 'https,http'); assert.equal(r.thinPack, true);
66 // a request with no arguments ends at the flush after its capabilities
67 assert.deepEqual(parse(raw('command=ls-refs\n', 'agent=x\n', FLUSH)), { command: 'ls-refs', caps: ['agent=x'], prefixes: [], peel: false, symrefs: false, unborn: false });
68});
70test('refusals are typed: framing, order, command, every argument rule', () => {
71 const refused = (body, re) => { const r = parseUploadPackRequest(body); assert.equal(r.ok, false); assert.equal(r.error.code, 'protocol'); assert.match(r.error.message, re); };
72 refused(te.encode('zzzz'), /bad pkt-line length/);
73 refused(te.encode('0010command=fe'), /trailing/);
74 refused(raw('command=fetch\n', DELIM, `want ${W}\n`), /no flush ends the request/);
75 refused(new Uint8Array(0), /no flush ends the request/);
76 refused(concat([raw('command=fetch\n', DELIM, FLUSH), FLUSH]), /flush after the flush/);
77 refused(concat([raw('command=fetch\n', DELIM), te.encode('0002'), FLUSH]), /response-end inside a request/);
78 refused(raw(DELIM, 'command=fetch\n', FLUSH), /delim before the command line/);
79 refused(raw('want x\n', FLUSH), /not command=<name>/);
80 refused(raw('command=object-info\n', FLUSH), /unsupported command "object-info"/);
81 refused(raw('command=fetch\n', DELIM, DELIM, FLUSH), /a second delim/);
82 const arg = (line, re, command = 'fetch') => refused(raw(`command=${command}\n`, DELIM, line, FLUSH), re);
83 arg('want xyz\n', /want must be a 40-hex oid/);
84 arg(`have ${W}0\n`, /have must be a 40-hex oid/);
85 arg('frobnicate\n', /unknown fetch argument "frobnicate"/);
86 arg('frobnicate 1\n', /unknown fetch argument "frobnicate"/);
87 arg('want-ref \n', /want-ref needs a value/);
88 arg('filter \n', /filter must be given once/);
89 refused(raw('command=fetch\n', DELIM, 'filter tree:0\n', 'filter blob:none\n', FLUSH), /filter must be given once/);
90 arg('deepen 0\n', /deepen must be a positive integer/);
91 arg('deepen -1\n', /deepen must be a positive integer/);
92 arg('deepen-since 1e3\n', /deepen-since must be a positive integer/);
93 arg('deepen 99999999999999999999\n', /deepen must be a positive integer/);
94 refused(raw('command=fetch\n', DELIM, 'deepen 1\n', 'deepen 2\n', FLUSH), /a second deepen/);
95 arg('ref-prefix\n', /unknown ls-refs argument "ref-prefix"/, 'ls-refs');
96 arg('peel 1\n', /unknown ls-refs argument "peel"/, 'ls-refs');
97});
99test('exchangeKind: a pure fetch is pack, ls-refs is refs, the advertisement is discover, the rest is null', () => {
100 const u = 'https://h/r.git/git-upload-pack';
101 const pure = fetchBody({ wants: [W], filter: 'tree:0', deepen: 1 });
102 assert.equal(exchangeKind('POST', u, pure), 'pack');
103 assert.equal(exchangeKind('post', new URL(u), fetchBody({ wants: [W] })), 'pack', 'a blob fetch with no filter is pure too');
104 assert.equal(exchangeKind('POST', u, decodeBytes(pure)), 'pack', 'a string body');
105 assert.equal(exchangeKind('POST', u, lsRefsBody(['refs/heads/'])), 'refs');
106 assert.equal(exchangeKind('GET', 'https://h/r.git/info/refs?service=git-upload-pack', null), 'discover');
107 assert.equal(exchangeKind('GET', 'https://h/r.git/info/refs?service=git-receive-pack', null), null);
108 assert.equal(exchangeKind('GET', 'https://h/r.git/info/refs', null), null);
109 assert.equal(exchangeKind('POST', 'https://h/r.git/git-receive-pack', pure), null);
110 assert.equal(exchangeKind('POST', u, null), null);
111 assert.equal(exchangeKind('POST', u, te.encode('garbage')), null);
112 assert.equal(exchangeKind('PUT', u, pure), null);
113 // each impurity on its own
114 for (const extra of [{ haves: [W] }, { shallows: [W] }, { deepenSince: 1 }, { deepenNot: ['refs/heads/x'] }, { deepenRelative: true }, { includeTag: true }, { done: false }]) {
115 assert.equal(exchangeKind('POST', u, fetchBody({ wants: [W], ...extra })), null, JSON.stringify(extra));
116 }
117 assert.equal(exchangeKind('POST', u, fetchBody({ wants: [] })), null, 'no want');
118 for (const line of ['want-ref refs/heads/main\n', 'packfile-uris https\n', 'sideband-all\n']) assert.equal(exchangeKind('POST', u, raw('command=fetch\n', DELIM, `want ${W}\n`, line, 'done\n', FLUSH)), null, line);
119 assert.equal(exchangeKind('POST', u, raw('command=fetch\n', 'server-option=x\n', DELIM, `want ${W}\n`, 'done\n', FLUSH)), null, 'a server option');
120 assert.equal(exchangeKind('POST', u, raw('command=fetch\n', 'agent=git/2.50.0\n', DELIM, `want ${W}\n`, 'done\n', FLUSH)), 'pack', 'an agent is not an impurity');
121 assert.equal(isPureFetch(parse(pure)), true);
122 // the default cache rule is unchanged: a pure fetch with a filter line
123 assert.equal(isCacheableFetch(pure), true);
124 assert.equal(isCacheableFetch(fetchBody({ wants: [W] })), false);
125 assert.equal(isCacheableFetch(lsRefsBody([])), false);
126});
Diagnostics4 git calls · 28 ms
CallStatusSizeTime028 ms
discover200152 B3 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 · 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=23
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=55
Open pack map
fetch2003.1 KiB · hit23 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=3200
git.response.cache=HIT
git.response.duration_ms=23
git.fetch.wants=ff9e27fd448dadcfcee72c2d29f6f6223e84bc27
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=3200
git.pkt.payload.max=3179
git.sideband.band1.bytes=3178
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
a5e1901a7d005b65
Request
7458990c…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format