GitHubivarvong
ply
main
1// Fault-injection proxy in front of the real git server. The fault decision is a pure function of the
2// exchange context: fault(ctx) → spec. ctx = { stage: 'ls-refs'|'fetch'|'receive-pack', n (index within stage),
3// i (global index), status, headers, bytes (upstream body), reqBody }.
4// spec: { type: 'pass' } | { type: 'bytes', bytes, chunks? } | { type: 'status', status, body?, contentType? }
5// | { type: 'stall' } | { type: 'drop', after? } | { type: 'slow', chunk, delayMs }
6import http from 'node:http';
8export function startProxy(upstreamUrl, fault) {
9 const up = new URL(upstreamUrl);
10 const counts = {};
11 let i = 0;
12 const log = [];
13 const server = http.createServer((req, res) => {
14 const reqChunks = [];
15 req.on('data', d => reqChunks.push(d));
16 req.on('end', () => {
17 const reqBody = Buffer.concat(reqChunks);
18 const stage = req.url.endsWith('git-receive-pack') ? 'receive-pack' : /command=ls-refs/.test(reqBody.subarray(0, 40).toString()) ? 'ls-refs' : 'fetch';
19 const n = counts[stage] = (counts[stage] ?? 0) + 1;
20 const ctx = { stage, n: n - 1, i: i++, reqBody, headers: req.headers };
21 // a fault may answer without forwarding anything upstream (server never saw the request)
22 let pre = null;
23 try { pre = fault({ ...ctx, phase: 'request', bytes: new Uint8Array(0) }); } catch { pre = null; } // faults that need the upstream bytes are called later
24 if (pre && pre.type === 'bytes' && pre.noForward) {
25 log.push({ stage, n: ctx.n, spec: 'bytes(noForward)', detail: pre.name ?? '' });
26 Promise.resolve(pre.bytes).then(b => { res.writeHead(pre.status ?? 200, { 'content-type': pre.contentType ?? `application/x-${stage === 'receive-pack' ? 'git-receive-pack' : 'git-upload-pack'}-result` }); res.end(Buffer.from(b)); });
27 return;
28 }
29 const upReq = http.request({ host: up.hostname, port: up.port, path: req.url, method: req.method, headers: { ...req.headers, host: up.host } }, upRes => {
30 const chunks = [];
31 upRes.on('data', d => chunks.push(d));
32 upRes.on('end', async () => {
33 const bytes = new Uint8Array(Buffer.concat(chunks));
34 const spec = fault({ ...ctx, status: upRes.statusCode, headers: upRes.headers, bytes }) ?? { type: 'pass' };
35 if (spec.bytes && typeof spec.bytes.then === 'function') spec.bytes = await spec.bytes;
36 log.push({ stage, n: ctx.n, spec: spec.type, detail: spec.name ?? '' });
37 const hdrs = { 'content-type': upRes.headers['content-type'] ?? 'application/octet-stream', 'cache-control': 'no-cache' };
38 switch (spec.type) {
39 case 'pass': res.writeHead(upRes.statusCode, hdrs); res.end(Buffer.from(bytes)); break;
40 case 'bytes': {
41 res.writeHead(spec.status ?? upRes.statusCode, { ...hdrs, ...(spec.contentType ? { 'content-type': spec.contentType } : {}) });
42 const b = Buffer.from(spec.bytes);
43 if (spec.chunks) { let o = 0; for (const c of spec.chunks) { if (o >= b.length) break; res.write(b.subarray(o, o + c)); o += c; await new Promise(r => setTimeout(r, spec.delayMs ?? 0)); } res.write(b.subarray(o)); res.end(); }
44 else res.end(b);
45 break;
46 }
47 case 'status': res.writeHead(spec.status, { 'content-type': spec.contentType ?? 'text/html' }); res.end(spec.body ?? '<html>login</html>'); break;
48 case 'stall': res.writeHead(upRes.statusCode, hdrs); if (spec.partial) res.write(Buffer.from(bytes.subarray(0, spec.partial))); /* never end */ break;
49 case 'drop': { res.writeHead(upRes.statusCode, hdrs); if (spec.after) res.write(Buffer.from(bytes.subarray(0, spec.after))); res.destroy(); break; }
50 case 'slow': { res.writeHead(upRes.statusCode, hdrs); const b = Buffer.from(bytes); for (let o = 0; o < b.length; o += spec.chunk) { res.write(b.subarray(o, o + spec.chunk)); await new Promise(r => setTimeout(r, spec.delayMs)); } res.end(); break; }
51 default: throw new Error(`bad fault spec ${spec.type}`);
52 }
53 });
54 });
55 upReq.on('error', () => { res.writeHead(502); res.end(); });
56 upReq.end(reqBody);
57 });
58 });
59 return new Promise(resolve => server.listen(0, '127.0.0.1', () => resolve({
60 url: `http://127.0.0.1:${server.address().port}`, log, counts,
61 close: () => new Promise(r => { server.closeAllConnections?.(); server.close(r); }),
62 })));
63}
Diagnostics6 git calls · 28 ms
CallStatusSizeTime028 ms
discover200191 B2 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 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 KiB · hit2 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=2 git.response.bytes=11990 git.response.cache=HIT git.response.duration_ms=2 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 · 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=4347 git.response.cache=HIT git.response.duration_ms=2 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=10Open 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=5323b9266eea5ded637285d1c5320699f12be76eOpen pack map
fetchtree:02005.6 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=5724 git.response.cache=HIT git.response.duration_ms=2 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=be7e0453974a5fe832f9065a33bb7e3434c10e9bOpen pack map
fetch2001.7 KiB · hit20 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=20 git.response.bytes=1726 git.response.cache=HIT git.response.duration_ms=20 git.fetch.wants=2300d9f854d813a5464d351fc99e54254a99958f 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=1726 git.pkt.payload.max=1699 git.sideband.band1.bytes=1699 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
- a5e0f82d3937332c
- Request
- d614a06b…
- 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