main
1// recordResponseHeaders (#123). Not a git-semantics change: a real git http-backend, with the response headers
2// stamped by the fetch the transport calls. Each cell is one exchange (discover, ls-refs, fetch), the tree:0 refusal
3// and its blob:none retry, a credential echoed in a header, and a pack-cache HIT of the same wants-only fetch.
4import { test, beforeAll as before, afterAll as after } from 'vitest';
5import assert from 'node:assert/strict';
6import { join } from 'node:path';
7import { mkdirSync, writeFileSync } from 'node:fs';
8import { git, tmpRoot, makeBareRepo, startGitServer, initRepo } from './server.js';
9import { Tracer, arrayExporter, flattenLine } from '../src/otel.js';
10import { createTransport as createTransportR } from '../src/http.js';
11import { discover as discoverR } from '../src/discover.js';
12import { lsRefs as lsRefsR } from '../src/lsrefs.js';
13import { fetchPack as fetchPackR, fetchPackTree0 as fetchPackTree0R } from '../src/fetchpack.js';
14import { cachingFetch, memoryCache } from '../src/packcache.js';
15import { td } from '../src/bytes.js';
17import { unwrap, unwrapAsync } from './unwrap.js';
18// Throwing adapters over the Result API (0.2.0): the assertions below are about the throw.
19const createTransport = (...a) => unwrap(createTransportR(...a));
20const discover = (...a) => unwrapAsync(discoverR(...a));
21const lsRefs = (...a) => unwrapAsync(lsRefsR(...a));
22const fetchPack = (...a) => unwrapAsync(fetchPackR(...a));
23const fetchPackTree0 = (...a) => unwrapAsync(fetchPackTree0R(...a));
24const KEY = /^(git|http|url|server|mem|error|ply|exception)\.[a-z0-9_.-]+$/;
25const CAP = 128;
26let root, cleanup, srv, n = 0;
27before(async () => { ({ root, cleanup } = tmpRoot()); srv = await startGitServer(root); });
28after(async () => { await srv?.close(); cleanup?.(); });
30const seeded = () => {
31 const name = `h${n++}`;
32 const bare = makeBareRepo(root, name);
33 const work = join(root, `w${name}`);
34 mkdirSync(work);
35 initRepo(work);
36 writeFileSync(join(work, 'a.txt'), 'a\n');
37 const g = ['-c', 'user.name=h', '-c', 'user.email=h@x', '-c', 'commit.gpgsign=false'];
38 git(work, ['add', 'a.txt']);
39 git(work, [...g, 'commit', '-q', '-m', 'seed']);
40 git(work, ['push', '-q', bare, 'HEAD:refs/heads/main']);
41 return { url: `${srv.url}/${name}.git`, oid: git(work, ['rev-parse', 'HEAD']).trim() };
42};
44const spansOf = ex => ex.lines.flatMap(flattenLine);
45const traced = () => { const ex = arrayExporter(); return { tracer: new Tracer({ exporter: ex }), spans: () => spansOf(ex) }; };
47test('each exchange span records the headers that response carried, and no others', async () => {
48 const r = seeded();
49 let i = 0;
50 const hint = [];
51 const fetchImpl = async (url, init) => {
52 hint.push(new Headers(init.headers).get('x-ply-record-headers'));
53 const res = await fetch(url, init);
54 i += 1;
55 const headers = new Headers(res.headers);
56 headers.set('cf-ray', `ray-${i}`);
57 headers.set('x-github-request-id', `gh-${i}`);
58 headers.set('x-request-id', `not-listed-${i}`);
59 return new Response(res.body, { status: res.status, headers });
60 };
61 const { tracer, spans } = traced();
62 const t = createTransport(r.url, { tracer, fetchImpl, recordResponseHeaders: ['CF-Ray', 'cf-ray', 'X-GitHub-Request-Id', 'x-missing'] });
63 await discover(t);
64 const { refs } = await lsRefs(t, ['refs/heads/main']);
65 assert.equal(refs.get('refs/heads/main').oid, r.oid);
66 await fetchPack(t, { wants: [r.oid], filter: 'blob:none', deepen: 1 });
67 const want = ['git.upload-pack discover', 'git.upload-pack ls-refs', 'git.upload-pack fetch'];
68 const got = spans().filter(s => want.includes(s.name));
69 assert.deepEqual(got.map(s => s.name), want);
70 got.forEach((s, k) => {
71 assert.equal(s.attrs['http.response.header.cf-ray'], `ray-${k + 1}`, s.name);
72 assert.equal(s.attrs['http.response.header.x-github-request-id'], `gh-${k + 1}`, s.name);
73 assert.equal(s.attrs['http.response.header.x-request-id'], undefined, 'a header not in the list is not recorded');
74 assert.equal(s.attrs['http.response.header.x-missing'], undefined, 'a listed header the response omitted is absent');
75 assert.equal(s.attrs['http.response.header.CF-Ray'], undefined, 'the key is the lowercased name');
76 assert.equal(s.attrs['http.response.headers.from_cache'], undefined, 'a live response is not a cache hit');
77 assert.equal(typeof s.attrs['http.response.status_code'], 'number');
78 assert.equal(typeof s.attrs['http.response.ttfb_ms'], 'number');
79 for (const key of Object.keys(s.attrs)) assert.match(key, KEY, key);
80 });
81 assert.deepEqual(hint, [null, null, null], 'a non-caching fetch is not sent the record hint');
82});
84test('a tree:0 refusal and the blob:none retry keep the ray of the response each one received', async () => {
85 const r = seeded();
86 const fetchImpl = async (url, init) => {
87 const body = init.body instanceof Uint8Array ? td.decode(init.body) : '';
88 if (String(url).endsWith('/git-upload-pack') && body.includes('filter tree:0')) {
89 return new Response('Unsupported Git fetch filter; only blob:none is supported.', {
90 status: 400, headers: { 'content-type': 'text/plain', 'cf-ray': 'ray-refused' },
91 });
92 }
93 const res = await fetch(url, init);
94 const headers = new Headers(res.headers);
95 headers.set('cf-ray', 'ray-wide');
96 return new Response(res.body, { status: res.status, headers });
97 };
98 const { tracer, spans } = traced();
99 const t = createTransport(r.url, { tracer, fetchImpl, recordResponseHeaders: ['cf-ray'], quirks: {} });
100 const res = await fetchPackTree0(t, { wants: [r.oid], filter: 'tree:0', deepen: 1 });
101 assert.equal(res.filter, 'blob:none');
102 const fetches = spans().filter(s => s.name === 'git.upload-pack fetch');
103 assert.equal(fetches.length, 2);
104 assert.equal(fetches[0].attrs['http.response.header.cf-ray'], 'ray-refused');
105 assert.equal(fetches[0].attrs['git.fetch.filter.fallback'], undefined);
106 assert.equal(fetches[1].attrs['http.response.header.cf-ray'], 'ray-wide');
107 assert.equal(fetches[1].attrs['git.fetch.filter.fallback'], 'blob:none');
108});
110test('a recorded value is scrubbed, then capped at 128, on the span that received it', async () => {
111 const token = `ghp_${'a1'.repeat(20)}`;
112 const hint = [];
113 const fetchImpl = async (_url, init) => {
114 hint.push(new Headers(init.headers).get('x-ply-record-headers'));
115 return new Response(Uint8Array.of(0), {
116 status: 200,
117 headers: { 'content-type': 'application/x-git-upload-pack-advertisement', 'cf-ray': `${token}-${'z'.repeat(200)}` },
118 });
119 };
120 const { tracer, spans } = traced();
121 const span = tracer.start('contract.harness');
122 const t = createTransport('http://127.0.0.1/r.git', { tracer, parent: span, auth: { bearer: token }, fetchImpl, recordResponseHeaders: ['cf-ray'] });
123 assert.equal((await unwrapAsync(t.advertise('git-upload-pack', span))).length, 1);
124 span.end();
125 const ray = spans().find(s => s.name === 'contract.harness').attrs['http.response.header.cf-ray'];
126 assert.equal(ray.length, CAP);
127 assert.equal(ray.startsWith('[redacted]'), true);
128 assert.equal(ray.includes(token), false);
129 assert.equal(ray.includes('ghp_'), false);
130 assert.deepEqual(hint, [null]);
131});
133test('a recorded value is kept at 0, 1, cap-1 and cap, and cut at cap+1 and beyond', async () => {
134 for (const n of [0, 1, CAP - 1, CAP, CAP + 1, CAP * 8]) {
135 const raw = 'v'.repeat(n);
136 const fetchImpl = async () => new Response(Uint8Array.of(0), {
137 status: 200,
138 headers: { 'content-type': 'application/x-git-upload-pack-advertisement', 'cf-ray': raw },
139 });
140 const { tracer, spans } = traced();
141 const span = tracer.start('contract.harness');
142 const t = createTransport('http://127.0.0.1/r.git', { tracer, parent: span, fetchImpl, recordResponseHeaders: ['cf-ray'] });
143 await unwrapAsync(t.advertise('git-upload-pack', span));
144 span.end();
145 const ray = spans().find(s => s.name === 'contract.harness').attrs['http.response.header.cf-ray'];
146 assert.equal(ray, raw.slice(0, CAP), `value length ${n}`);
147 }
148});
150test('a pack-cache HIT replays the stored ray and is marked from_cache; the origin never sees the hint', async () => {
151 const r = seeded();
152 const cache = memoryCache();
153 const hint = [];
154 const origin = async (url, init) => {
155 hint.push(new Headers(init.headers).get('x-ply-record-headers'));
156 const res = await fetch(url, init);
157 const headers = new Headers(res.headers);
158 headers.set('cf-ray', 'origin-ray');
159 headers.set('x-github-request-id', 'gh-origin');
160 return new Response(res.body, { status: res.status, headers });
161 };
162 const pending = [];
163 const fetchImpl = cachingFetch(origin, cache, { waitUntil: p => { pending.push(p); } });
164 const { tracer, spans } = traced();
165 const t = createTransport(r.url, { tracer, fetchImpl, recordResponseHeaders: ['cf-ray', 'x-github-request-id'] });
166 const want = { wants: [r.oid], filter: 'blob:none', deepen: 1 };
167 await fetchPack(t, want);
168 await Promise.all(pending);
169 await fetchPack(t, want);
170 const fetches = spans().filter(s => s.name === 'git.upload-pack fetch');
171 assert.equal(fetches.length, 2);
172 assert.equal(fetches[0].attrs['git.response.cache'], 'MISS');
173 assert.equal(fetches[0].attrs['http.response.header.cf-ray'], 'origin-ray');
174 assert.equal(fetches[0].attrs['http.response.header.x-github-request-id'], 'gh-origin');
175 assert.equal(fetches[0].attrs['http.response.headers.from_cache'], undefined);
176 assert.equal(fetches[1].attrs['git.response.cache'], 'HIT');
177 assert.equal(fetches[1].attrs['http.response.header.cf-ray'], 'origin-ray');
178 assert.equal(fetches[1].attrs['http.response.header.x-github-request-id'], 'gh-origin');
179 assert.equal(fetches[1].attrs['http.response.headers.from_cache'], true);
180 assert.deepEqual(hint, [null]);
181 const stored = [...cache.entries.values()];
182 assert.equal(stored.length, 1);
183 assert.equal(stored[0].headers['cf-ray'], 'origin-ray');
184 assert.equal(stored[0].headers['x-github-request-id'], 'gh-origin');
185 assert.equal(stored[0].headers['x-ply-record-headers'], undefined);
186});
Diagnostics4 git calls · 384 ms
CallStatusSizeTime0384 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=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 B2 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=145 git.response.cache=UPDATING git.response.duration_ms=2 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 · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=378 git.response.bytes=38434 git.response.cache=HIT git.response.duration_ms=3 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.9 KiB378 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=2951 git.response.cache=MISS git.response.duration_ms=378 git.fetch.wants=f47bbc093cc394484f7603993636226bd7ebdf00 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=2951 git.pkt.payload.max=2930 git.sideband.band1.bytes=2929 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
- a5e27231e912d806
- Request
- af161cbd…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format