GitHubivarvong

ply

main
ply7 git calls110 ms25 KiB4 cachedprotocol v2
123 lines5.2 KiBRaw
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);
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 }));
Diagnostics7 git calls · 110 ms
CallStatusSizeTime0110 ms
discover200191 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=191
git.server.agent=git/github-9922ec520325-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=191
git.pkt.payload.max=36
ls-refs20012 KiB3 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=11990
git.response.cache=UPDATING
git.response.duration_ms=3
git.protocol.version=2
git.pkt.frames=152
git.pkt.data=151
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=11990
git.pkt.payload.max=122
git.ls_refs.count=151
git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 102004.2 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=4347
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
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=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4347
git.pkt.payload.max=4247
git.sideband.band1.bytes=4247
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=10
git.pack.bytes=4247
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=a6caf8e129bea82c9bae4cbc7c130c9205add400
git.pack.full=10
Open pack map
fetchtree:0200919 B · 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=919
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=800d30ba8923860bfc460ca884b7f9ba22083588
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=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=919
git.pkt.payload.max=892
git.sideband.band1.bytes=892
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=892
git.pack.trailer=5323b9266eea5ded637285d1c5320699f12be76e
Open pack map
fetchtree:02005.6 KiB · hit3 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=3
git.response.bytes=5724
git.response.cache=HIT
git.response.duration_ms=3
git.fetch.filter=tree:0
git.fetch.wants=58fabb6768b3f737ee9cd8bac647bdc27405fb4d
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=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=5724
git.pkt.payload.max=5697
git.sideband.band1.bytes=5697
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=5697
git.pack.trailer=be7e0453974a5fe832f9065a33bb7e3434c10e9b
Open pack map
fetchtree:0200381 B · 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=381
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=294ee88709910bdf98d58eeb6898cd838de94a3a
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=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=381
git.pkt.payload.max=354
git.sideband.band1.bytes=354
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=354
git.pack.trailer=2c7127c2c53a7ca2957296d420246ab2358e3775
Open pack map
fetch2002.1 KiB97 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=97
git.response.bytes=2139
git.response.cache=MISS
git.response.duration_ms=97
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=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=2139
git.pkt.payload.max=2112
git.sideband.band1.bytes=2112
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://github.com/ivarvong/ply
Ref
refs/heads/main
Commit
19912563f540228593df0c489082e5e2508e21e4
cf-ray
a5e30a4499ffee28
Request
7cebcbdd…
Protocol
v2 · sha1
Agent
git/github-9922ec520325-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format