main
1// A proxy in front of the git http-backend server, for the cells about the wire rather than the repository.
2// The first path segment `~<mode>` picks what it does to the exchange and is stripped before git sees the path:
3// ~v0, ~v1 the Git-Protocol header removed, or set to version=1: a server that answers in protocol v0
4// ~delay-<ms> each response held <ms> before its status line (a slow server)
5// ~stall-<ms> each response's headers and first chunk sent, then <ms> of silence before the rest (a stalled body)
6// ~frame-<L> an upload-pack response's packfile section re-framed: every band-1 frame is a pkt-line of total
7// length L (the last one shorter). L = 4 inserts one empty "0004" pkt-line before the pack, L = 5
8// one band-only frame ("0005\x01"), each followed by the pack in git's own 65520-byte frames.
9// The same URL serves git's client, which is the oracle for the frame cells.
10import http from 'node:http';
12/** @param {number} n */
13const hex4 = n => n.toString(16).padStart(4, '0');
15/** @param {Buffer} body @param {number} L */
16export function reframe(body, L) {
17 /** @type {Buffer[]} */
18 const out = [];
19 let p = 0;
20 let inPack = false;
21 /** @type {Buffer[]} */
22 const band1 = [];
23 /** @type {Buffer[]} */
24 const others = [];
25 const flushPack = () => {
26 const data = Buffer.concat(band1);
27 const emit = (/** @type {Buffer} */ chunk) => out.push(Buffer.from(hex4(chunk.length + 5)), Buffer.from([1]), chunk);
28 if (L === 4) out.push(Buffer.from('0004'));
29 if (L === 5) out.push(Buffer.from('0005'), Buffer.from([1]));
30 const size = L >= 6 ? L - 5 : 65515;
31 for (let i = 0; i < data.length; i += size) emit(data.subarray(i, Math.min(data.length, i + size)));
32 out.push(...others);
33 };
34 while (p + 4 <= body.length) {
35 const len = parseInt(body.subarray(p, p + 4).toString('latin1'), 16);
36 if (!Number.isFinite(len)) throw new Error(`shim: bad pkt-line length at ${p}`);
37 if (len < 4) {
38 if (inPack && len === 0) { flushPack(); inPack = false; }
39 out.push(body.subarray(p, p + 4));
40 p += 4;
41 continue;
42 }
43 const pkt = body.subarray(p, p + len);
44 p += len;
45 if (inPack) {
46 if (pkt[4] === 1) band1.push(pkt.subarray(5));
47 else others.push(pkt);
48 continue;
49 }
50 out.push(pkt);
51 if (pkt.subarray(4).toString('latin1') === 'packfile\n') inPack = true;
52 }
53 if (inPack) flushPack();
54 out.push(body.subarray(p));
55 return Buffer.concat(out);
56}
58/** @param {string} upstreamUrl */
59export function startShim(upstreamUrl) {
60 const up = new URL(upstreamUrl);
61 const server = http.createServer((req, res) => {
62 const m = /^\/~([a-z0-9]+)(?:-(\d+))?(\/.*)$/.exec(req.url ?? '');
63 const mode = m ? m[1] : '';
64 const arg = m && m[2] ? Number(m[2]) : 0;
65 const path = m ? m[3] : (req.url ?? '/');
66 const headers = { ...req.headers, host: up.host };
67 if (mode === 'v0') delete headers['git-protocol'];
68 if (mode === 'v1') headers['git-protocol'] = 'version=1';
69 const reframing = mode === 'frame' && req.method === 'POST' && path.endsWith('/git-upload-pack');
70 const p = http.request({ host: up.hostname, port: up.port, path, method: req.method, headers }, r => {
71 const respond = () => {
72 if (reframing) {
73 /** @type {Buffer[]} */
74 const parts = [];
75 r.on('data', d => parts.push(d));
76 r.on('end', () => {
77 const body = reframe(Buffer.concat(parts), arg);
78 const h = { ...r.headers };
79 delete h['content-length'];
80 delete h['transfer-encoding'];
81 h['content-length'] = String(body.length);
82 res.writeHead(r.statusCode ?? 502, h);
83 res.end(body);
84 });
85 return;
86 }
87 res.writeHead(r.statusCode ?? 502, r.headers);
88 if (mode !== 'stall') { r.pipe(res); return; }
89 let first = true;
90 let waiting = false;
91 let ended = false;
92 r.on('data', d => {
93 if (!first) { res.write(d); return; }
94 first = false;
95 // the first bytes now; everything after them waits out the stall
96 res.write(d.subarray(0, Math.min(d.length, 64)));
97 r.pause();
98 waiting = true;
99 const t = setTimeout(() => {
100 waiting = false;
101 if (res.destroyed) return;
102 res.write(d.subarray(Math.min(d.length, 64)));
103 if (ended) res.end(); else r.resume();
104 }, arg);
105 res.on('close', () => clearTimeout(t));
106 });
107 r.on('end', () => { ended = true; if (!waiting) res.end(); });
108 };
109 if (mode === 'delay') {
110 const t = setTimeout(respond, arg);
111 res.on('close', () => clearTimeout(t));
112 } else respond();
113 });
114 p.on('error', e => { if (!res.headersSent) res.writeHead(502); res.end(String(e)); });
115 req.pipe(p);
116 });
117 server.keepAliveTimeout = 120_000;
118 server.headersTimeout = 121_000;
119 return new Promise(resolve => server.listen(0, '127.0.0.1', () => {
120 const addr = /** @type {import('node:net').AddressInfo} */ (server.address());
121 resolve({ url: `http://127.0.0.1:${addr.port}`, close: () => new Promise(r => { server.close(() => r(undefined)); server.closeAllConnections?.(); }) });
122 }));
123}
Diagnostics4 git calls · 43 ms
CallStatusSizeTime043 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=38 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.1 KiB · hit38 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2130 git.response.cache=HIT git.response.duration_ms=38 git.fetch.wants=9d07dd20d56983760586f392054275f75f72242f 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=2130 git.pkt.payload.max=2109 git.sideband.band1.bytes=2108 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
- a5e344e86fcef4d4
- Request
- c82e585b…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format