main
1// Runs test/probes/wire-plans.ts against a target with each sender (raw socket, curl, stock git, ply) and records what
2// came back and what stock git, the judge, sees afterwards. Impure on purpose; what an answer means is in classOf and
3// meets below, the same for every target and sender.
4//
5// A wire target: { name, url (the repository's smart-HTTP URL, reached directly: no proxy between the sender and the
6// server, since a proxy would reframe the request), judgeUrl (where stock git reads the end state), auth }, where auth
7// supplies a credential to each sender without handing it to a child process's argv or environment:
8// headers(kind, h) adds the Authorization header for `kind` to a header object (raw socket, ply's fetch)
9// curlFile(kind) a 0600 file of headers for curl's `-H @file`, or null; removed by the caller
10// gitFile(kind) a 0600 git config file (http.extraHeader) for `-c include.path=`, or null; removed by the caller
11// url(kind) the URL to ask (`missing-repo`: a sibling path where no repository is)
12// For the reference all of these add nothing.
13import net from 'node:net';
14import tls from 'node:tls';
15import { gzipSync } from 'node:zlib';
16import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
17import { tmpdir } from 'node:os';
18import { dirname, join } from 'node:path';
19import { performance } from 'node:perf_hooks';
20import { createHash } from 'node:crypto';
21import { TYPE_ID } from '../../src/objects.js';
22import { buildSeed, gitEnv, observeEnd, run } from './runner.mjs';
23import { GitHttp } from '../../src/client.js';
25const ZERO = '0'.repeat(40);
26const CHUNK = 8 * 1024;
27const EXPECT_WAIT_MS = 1500;
28const ANSWER_TIMEOUT_MS = 30_000;
29const NOISE_BYTES = 64 * 1024;
30// The reference refuses every filter but blob:none in git's own words (upload-pack), as Artifacts does.
31export const WIRE_REFERENCE_CONFIG = [['uploadpackfilter.allow', 'false'], ['uploadpackfilter.blob:none.allow', 'true']];
33export const WIRE_SEED = {
34 objects: [
35 { tag: 'commit', name: 'base', parents: [], files: [{ path: 'a.txt', content: { tag: 'text', text: 'wire probe base\n' } }], message: 'base' },
36 { tag: 'commit', name: 'next', parents: ['base'], files: [{ path: 'b.bin', content: { tag: 'noise', seed: 'wire-next', bytes: NOISE_BYTES } }], message: 'next' },
37 ],
38 refs: [{ ref: 'refs/heads/main', at: 'base' }],
39};
41const must = async (p, what) => { const r = await p; if (r.status !== 0) throw new Error(`${what}: exit ${r.status}: ${r.stderr.trim().slice(0, 300)}`); return r; };
43// The seed store, its ids, base's a.txt and its size, and next's objects as a pack (what a create of next sends).
44export async function wireSeed(root) {
45 const seed = await buildSeed(root, WIRE_SEED);
46 const { dir, env } = seed;
47 const base = seed.names.base, next = seed.names.next;
48 const blob = (await must(run('git', ['rev-parse', `${base}:a.txt`], { cwd: dir, env }), 'rev-parse a.txt')).stdout.trim();
49 const blobSize = Number((await must(run('git', ['cat-file', '-s', blob], { cwd: dir, env }), 'cat-file -s')).stdout.trim());
50 const pack = await new Promise((resolve, reject) => {
51 import('node:child_process').then(({ spawn }) => {
52 const p = spawn('git', ['pack-objects', '--revs', '--stdout', '-q'], { cwd: dir, env });
53 const out = [];
54 p.stdout.on('data', d => out.push(d));
55 p.on('error', reject);
56 p.on('close', code => (code === 0 ? resolve(Buffer.concat(out)) : reject(new Error(`pack-objects exit ${code}`))));
57 p.stdin.end(`${next}\n^${base}\n`);
58 }, reject);
59 });
60 const list = (await must(run('git', ['rev-list', '--objects', '--no-object-names', next, '--not', base], { cwd: dir, env }), 'rev-list next')).stdout.trim().split('\n').filter(Boolean);
61 const batch = (await must(run('git', ['cat-file', '--batch'], { cwd: dir, env, input: `${list.join('\n')}\n` }), 'cat-file next')).out;
62 const objects = [];
63 for (let o = 0; o < batch.length;) {
64 const nl = batch.indexOf(10, o);
65 const [oid, type, size] = batch.subarray(o, nl).toString('latin1').split(' ');
66 const n = Number(size);
67 objects.push({ oid, type: TYPE_ID[type], data: new Uint8Array(batch.subarray(nl + 1, nl + 1 + n)) });
68 o = nl + 1 + n + 1;
69 }
70 return { seed, dir, env, base, next, blob, blobSize, pack, objects, many: null };
71}
73// The many-refs seed (wire-plans.ts WireSeedForm): branches alternately at base and next, tags at base, as refs in the
74// seed store so one glob push writes them all. Returns name -> id for every ref the target should then list.
75export async function manyRefs(ws, form) {
76 if (ws.many !== null && ws.many.form === `${form.heads}/${form.tags}`) return ws.many.refs;
77 const refs = { 'refs/heads/main': ws.base };
78 for (let i = 0; i < form.heads; i++) refs[`refs/heads/many/b${String(i).padStart(4, '0')}`] = i % 2 === 0 ? ws.base : ws.next;
79 for (let i = 0; i < form.tags; i++) refs[`refs/tags/many/t${String(i).padStart(4, '0')}`] = ws.base;
80 const input = Object.entries(refs).filter(([r]) => r !== 'refs/heads/main').map(([r, oid]) => `create ${r} ${oid}\n`).join('');
81 await must(run('git', ['update-ref', '--stdin'], { cwd: ws.dir, env: ws.env, input }), 'many refs');
82 ws.many = { form: `${form.heads}/${form.tags}`, refs };
83 return refs;
84}
85// The refspecs that seed a target for a plan, pushed from the seed store, and the refs it then holds.
86export async function seedOf(plan, ws) {
87 switch (plan.seed.tag) {
88 case 'base': return { specs: [`${ws.base}:refs/heads/main`], refs: { 'refs/heads/main': ws.base } };
89 case 'many-refs': return { specs: [`${ws.base}:refs/heads/main`, 'refs/heads/many/*:refs/heads/many/*', 'refs/tags/many/*:refs/tags/many/*'], refs: await manyRefs(ws, plan.seed) };
90 }
91 throw new Error(`unknown wire seed ${plan.seed.tag}`);
92}
94// ---- request bodies ----
95const pkt = s => { const b = Buffer.isBuffer(s) ? s : Buffer.from(s, 'latin1'); return Buffer.concat([Buffer.from((b.length + 4).toString(16).padStart(4, '0')), b]); };
96const FLUSH = Buffer.from('0000'), DELIM = Buffer.from('0001');
97export const wireRef = corner => `refs/heads/wire-${corner}`;
98export const refFor = (template, corner) => template.split('<c>').join(corner);
99const idOf = (ws, x) => (x === 'zero' ? ZERO : x === 'base' ? ws.base : ws.next);
100// A pack of no objects (a create or update to an object the server already holds still sends a pack).
101const EMPTY_PACK = (() => { const h = Buffer.from('PACK\0\0\0\x02\0\0\0\0', 'latin1'); return Buffer.concat([h, createHash('sha1').update(h).digest()]); })();
102export function bodyOf(body, ws, corner) {
103 switch (body.tag) {
104 case 'ls-refs': return Buffer.concat([pkt('command=ls-refs\n'), pkt('agent=ply-wire-probe\n'), DELIM, pkt('ref-prefix refs/heads/\n'), FLUSH]);
105 case 'ls-refs-no-flush': return Buffer.concat([pkt('command=ls-refs\n'), pkt('agent=ply-wire-probe\n'), DELIM, pkt('ref-prefix refs/heads/\n')]);
106 case 'object-info': return Buffer.concat([pkt('command=object-info\n'), DELIM, pkt('size\n'), pkt(`oid ${ws.blob}\n`), FLUSH]);
107 case 'fetch-filter': return Buffer.concat([pkt('command=fetch\n'), pkt('agent=ply-wire-probe\n'), DELIM, pkt(`want ${ws.base}\n`), pkt(`filter ${body.filter}=${ws.blob}\n`), pkt('no-progress\n'), pkt('done\n'), FLUSH]);
108 case 'unknown-command': return Buffer.concat([pkt('command=frobnicate\n'), FLUSH]);
109 case 'push-create': return Buffer.concat([pkt(`${ZERO} ${ws.next} ${wireRef(corner)}\0report-status agent=ply-wire-probe\n`), FLUSH, ws.pack]);
110 case 'bytes': return Buffer.from(body.latin1, 'latin1');
111 case 'push-ref': {
112 const cmd = pkt(Buffer.concat([Buffer.from(`${idOf(ws, body.old)} ${idOf(ws, body.new)} `, 'latin1'), Buffer.from(refFor(body.ref, corner), 'utf8'), Buffer.from(`\0${body.caps} agent=ply-wire-probe\n`, 'latin1')]));
113 return Buffer.concat([cmd, FLUSH, ...(body.new === 'zero' ? [] : [body.new === 'next' ? ws.pack : EMPTY_PACK])]);
114 }
115 case 'v2': return Buffer.concat([pkt(`command=${body.command}\n`), ...body.capLines.map(l => pkt(`${l}\n`)), DELIM, ...body.argLines.map(l => pkt(`${l.split('<base>').join(ws.base)}\n`)), FLUSH]);
116 case 'ls-refs-all': return Buffer.concat([pkt('command=ls-refs\n'), pkt('agent=ply-wire-probe\n'), DELIM, pkt('peel\n'), pkt('symrefs\n'), FLUSH]);
117 }
118 throw new Error(`unknown wire body ${body.tag}`);
119}
120export function encodeBody(buf, encoding) {
121 switch (encoding.tag) {
122 case 'identity': return buf;
123 case 'gzip': return gzipSync(buf);
124 case 'gzip-corrupt': {
125 const z = Buffer.from(gzipSync(buf));
126 const mid = 10 + Math.floor((z.length - 18) / 2); // inside the deflate stream (10-byte header, 8-byte trailer)
127 for (let i = 0; i < 8; i++) z[mid + i] ^= 0xff;
128 return z;
129 }
130 }
131 throw new Error(`unknown encoding ${encoding.tag}`);
132}
133const contentType = (req, kind) => (req.contentType.tag === 'other' ? req.contentType.value : `application/x-${req.service}-${kind}`);
135// The request as method, path suffix, headers and body (before framing).
136export function requestOf(plan, ws, corner) {
137 const r = plan.request;
138 switch (r.tag) {
139 case 'advertise': return { method: 'GET', suffix: `/info/refs?service=${r.service}`, headers: r.protocol === 'v2' ? { 'git-protocol': 'version=2' } : {}, body: null, framing: { tag: 'length' } };
140 case 'credential': return { method: 'GET', suffix: '/info/refs?service=git-upload-pack', headers: { 'git-protocol': 'version=2' }, body: null, framing: { tag: 'length' } };
141 case 'get': return { method: 'GET', suffix: r.path, headers: {}, body: null, framing: { tag: 'length' } };
142 case 'rpc': {
143 const headers = { 'content-type': contentType(r, 'request'), accept: `application/x-${r.service}-result` };
144 if (r.protocol === 'v2') headers['git-protocol'] = 'version=2';
145 if (r.encoding.tag !== 'identity') headers['content-encoding'] = 'gzip';
146 return { method: 'POST', suffix: `/${r.service}`, headers, body: encodeBody(bodyOf(r.body, ws, corner), r.encoding), framing: r.framing };
147 }
148 }
149 throw new Error(`unknown request ${r.tag}`);
150}
151const credentialOf = plan => (plan.request.tag === 'credential' ? plan.request.credential : 'write');
153// ---- a raw HTTP/1.1 exchange on a socket ----
154function parseResponses(buf) {
155 let at = 0, interim100 = false;
156 for (;;) {
157 const end = buf.indexOf('\r\n\r\n', at);
158 if (end < 0) return { status: 0, headers: {}, body: Buffer.alloc(0), interim100, partial: true };
159 const lines = buf.subarray(at, end).toString('latin1').split('\r\n');
160 const status = Number(/^HTTP\/1\.[01] (\d{3})/.exec(lines[0])?.[1] ?? 0);
161 const headers = {};
162 for (const l of lines.slice(1)) { const i = l.indexOf(':'); if (i > 0) headers[l.slice(0, i).trim().toLowerCase()] = l.slice(i + 1).trim(); }
163 at = end + 4;
164 if (status >= 100 && status < 200) { if (status === 100) interim100 = true; continue; }
165 let body = buf.subarray(at);
166 if (/chunked/i.test(headers['transfer-encoding'] ?? '')) {
167 const parts = [];
168 for (let o = 0; o < body.length;) {
169 const le = body.indexOf('\r\n', o);
170 if (le < 0) break;
171 const n = parseInt(body.subarray(o, le).toString('latin1'), 16);
172 if (!(n > 0)) break;
173 parts.push(body.subarray(le + 2, le + 2 + n));
174 o = le + 2 + n + 2;
175 }
176 body = Buffer.concat(parts);
177 } else if (headers['content-length'] !== undefined) body = body.subarray(0, Number(headers['content-length']));
178 return { status, headers, body, interim100, partial: false };
179 }
180}
181export function rawExchange(url, req, extraHeaders) {
182 const u = new URL(url + req.suffix);
183 const secure = u.protocol === 'https:';
184 const port = Number(u.port || (secure ? 443 : 80));
185 const head = [`${req.method} ${u.pathname}${u.search} HTTP/1.1`, `Host: ${u.host}`, 'User-Agent: ply-wire-probe/1', 'Connection: close'];
186 for (const [k, v] of Object.entries({ ...req.headers, ...extraHeaders })) head.push(`${k}: ${v}`);
187 const body = req.body;
188 const f = req.framing;
189 if (body !== null) {
190 if (f.tag === 'chunked') head.push('Transfer-Encoding: chunked');
191 else head.push(`Content-Length: ${body.length + (f.tag === 'length-off' ? f.by : 0)}`);
192 if (f.tag === 'expect-continue') head.push('Expect: 100-continue');
193 }
194 const t0 = performance.now();
195 return new Promise(resolve => {
196 const chunks = [];
197 let done = false, sawContinue = false, onContinue = null;
198 const finish = error => {
199 if (done) return;
200 done = true;
201 clearTimeout(timer);
202 sock.destroy();
203 const r = parseResponses(Buffer.concat(chunks));
204 resolve({ ...r, error, ms: +(performance.now() - t0).toFixed(1) });
205 };
206 const timer = setTimeout(() => finish('timeout'), ANSWER_TIMEOUT_MS);
207 const sock = secure ? tls.connect({ host: u.hostname, port, servername: u.hostname }) : net.connect({ host: u.hostname, port });
208 sock.on('data', d => { chunks.push(d); if (!sawContinue && Buffer.concat(chunks).toString('latin1').startsWith('HTTP/1.1 100')) { sawContinue = true; onContinue?.(); } });
209 sock.on('end', () => finish(null));
210 sock.on('close', () => finish(null));
211 sock.on('error', e => finish(String(e.code ?? e.message)));
212 sock.once(secure ? 'secureConnect' : 'connect', async () => {
213 sock.write(`${head.join('\r\n')}\r\n\r\n`);
214 if (body === null) return;
215 if (f.tag === 'expect-continue') await new Promise(r => { onContinue = r; if (sawContinue) r(); setTimeout(r, EXPECT_WAIT_MS); });
216 if (f.tag === 'chunked') {
217 for (let o = 0; o < body.length; o += CHUNK) { const c = body.subarray(o, o + CHUNK); sock.write(`${c.length.toString(16)}\r\n`); sock.write(c); sock.write('\r\n'); }
218 sock.write('0\r\n\r\n');
219 } else if (f.tag === 'length-off' && f.by < 0) sock.write(body);
220 else sock.write(body);
221 if (f.tag === 'length-off' && f.by > 0) sock.end(); // half-close: the promised bytes never come
222 });
223 });
224}
226// ---- curl ----
227export async function curlExchange(url, req, authFile, root) {
228 const dir = mkdtempSync(join(root, 'curl-'));
229 const args = ['-sS', '--http1.1', '--max-time', String(ANSWER_TIMEOUT_MS / 1000), '-o', join(dir, 'body'), '-D', join(dir, 'head'), '-w', '%{http_code}', '-A', 'ply-wire-probe/1', '-X', req.method];
230 for (const [k, v] of Object.entries(req.headers)) args.push('-H', `${k}: ${v}`);
231 if (req.body !== null) {
232 writeFileSync(join(dir, 'req'), req.body);
233 args.push('--data-binary', `@${join(dir, 'req')}`);
234 if (req.framing.tag === 'chunked') args.push('-H', 'Transfer-Encoding: chunked');
235 if (req.framing.tag === 'expect-continue') args.push('-H', 'Expect: 100-continue', '--expect100-timeout', String(EXPECT_WAIT_MS / 1000));
236 else args.push('-H', 'Expect:');
237 }
238 if (authFile !== null) args.push('-H', `@${authFile}`);
239 args.push(url + req.suffix);
240 const t0 = performance.now();
241 const r = await run('curl', args, { cwd: dir, env: { PATH: process.env.PATH, HOME: dir }, timeoutMs: ANSWER_TIMEOUT_MS + 5000 });
242 const ms = +(performance.now() - t0).toFixed(1);
243 const read = f => { try { return readFileSync(join(dir, f)); } catch { return Buffer.alloc(0); } };
244 const head = read('head').toString('latin1');
245 const blocks = head.split(/\r\n\r\n/).filter(b => b.startsWith('HTTP/'));
246 const last = blocks[blocks.length - 1] ?? '';
247 const headers = {};
248 for (const l of last.split('\r\n').slice(1)) { const i = l.indexOf(':'); if (i > 0) headers[l.slice(0, i).trim().toLowerCase()] = l.slice(i + 1).trim(); }
249 const out = { status: Number(r.stdout.trim()) || 0, headers, body: read('body'), interim100: blocks.some(b => b.startsWith('HTTP/1.1 100')), error: r.status === 0 ? null : `curl exit ${r.status}: ${r.stderr.trim().slice(0, 160)}`, ms };
250 rmSync(dir, { recursive: true, force: true });
251 return out;
252}
254// ---- what an answer is ----
255function pktLinesOf(buf, limit = 400) {
256 const out = [];
257 for (let o = 0; o + 4 <= buf.length && out.length < limit;) {
258 const h = buf.subarray(o, o + 4).toString('latin1');
259 if (!/^[0-9a-f]{4}$/.test(h)) return { lines: out, clean: false };
260 const n = parseInt(h, 16);
261 if (n < 4) { out.push(n === 0 ? '<flush>' : n === 1 ? '<delim>' : '<end>'); o += 4; continue; }
262 if (o + n > buf.length) return { lines: out, clean: false };
263 out.push(buf.subarray(o + 4, o + n).toString('latin1'));
264 o += n;
265 }
266 return { lines: out, clean: true };
267}
268const firstLine = buf => buf.toString('latin1').split('\n')[0].replace(/[^\x20-\x7e]/g, '?').slice(0, 120);
269// An answer as one class: v0-adv:<main id> | v2-adv | ls-refs:<main id> | object-info:<size> | err:<text> | report:<lines>
270// | pack | empty | status:<n> | text:<first line>. ERR may come on band 3 of a side-band answer or bare.
271export function classOf(status, body, ws) {
272 if (status >= 400) return `status:${status}`;
273 if (status === 0) return 'no-answer';
274 if (body.length === 0) return 'empty';
275 const { lines } = pktLinesOf(body);
276 const err = lines.find(l => l.startsWith('ERR ') || l.startsWith('\x03'));
277 if (err !== undefined) return `err:${(err.startsWith('\u0003') ? err.slice(1) : err).replace(/^ERR /, '').trim().slice(0, 120)}`;
278 if (lines[0]?.startsWith('# service=')) {
279 const main = lines.find(l => / refs\/heads\/main/.test(l));
280 return lines.includes('version 2\n') ? 'v2-adv' : `v0-adv:${main ? main.slice(0, 40) : 'no-main'}`;
281 }
282 if (lines[0] === 'version 2\n') return 'v2-adv';
283 const lsMain = lines.find(l => /^[0-9a-f]{40} refs\/heads\/main/.test(l));
284 if (lsMain) return `ls-refs:${lsMain.slice(0, 40)}`;
285 const size = lines.find(l => l.startsWith(`${ws.blob} `));
286 if (lines[0] === 'size\n' || size !== undefined) return `object-info:${size ? size.trim().split(' ')[1] : '?'}`;
287 if (lines.some(l => l.startsWith('unpack '))) return `report:${lines.filter(l => !l.startsWith('<')).map(l => l.trim()).join(';').slice(0, 160)}`;
288 if (lines.includes('packfile\n')) return 'pack';
289 return `text:${firstLine(body)}`;
290}
291const refusedClass = c => /^(status:4|status:5|err:|empty$|no-answer$)/.test(c) || (c.startsWith('report:') && !c.startsWith('report:unpack ok;ok ')) || c === 'refused' || c === 'client-refused';
293// A listing as one class: `refs:<count>:<digest>` over the sorted `<id> <name>` pairs (HEAD and peeled lines left out),
294// from a v0 advertisement, a v2 ls-refs answer, git ls-remote's or ply's list.
295export const listingOf = pairs => {
296 const lines = pairs.filter(([, n]) => n !== 'HEAD' && !n.endsWith('^{}')).map(([o, n]) => `${o} ${n}`).sort();
297 return `refs:${lines.length}:${createHash('sha1').update(lines.join('\n')).digest('hex').slice(0, 12)}`;
298};
299const listingOfBody = (status, body) => {
300 if (status >= 400) return `status:${status}`;
301 if (status === 0) return 'no-answer';
302 const { lines } = pktLinesOf(body, Infinity);
303 return listingOf(lines.map(l => /^([0-9a-f]{40}) ([^\0\s]+)/.exec(l)).filter(m => m !== null).map(m => [m[1], m[2]]));
304};
305// A dumb-HTTP file as one class: dumb-refs:<main id> | dumb-head:<target> | dumb-packs:<n> | status:<n> | text:<first line>.
306const dumbOf = (path, status, body) => {
307 if (status >= 400) return `status:${status}`;
308 if (status === 0) return 'no-answer';
309 const t = body.toString('latin1');
310 const lines = t.split('\n').filter(Boolean);
311 switch (path) {
312 case '/info/refs': { const m = lines.map(l => /^([0-9a-f]{40})\trefs\/heads\/main$/.exec(l)).find(x => x !== null); return m ? `dumb-refs:${m[1]}` : `text:${firstLine(body)}`; }
313 case '/HEAD': return /^ref: (\S+)\n?$/.test(t) ? `dumb-head:${/^ref: (\S+)/.exec(t)[1]}` : `text:${firstLine(body)}`;
314 case '/objects/info/packs': return lines.every(l => /^P pack-[0-9a-f]{40}\.pack$/.test(l)) ? `dumb-packs:${lines.length}` : `text:${firstLine(body)}`;
315 }
316 return `text:${firstLine(body)}`;
317};
318// The class of a raw or curl answer for this plan.
319const answerClass = (plan, x, ws) => (plan.expect.tag === 'listing' ? listingOfBody(x.status, x.body) : plan.request.tag === 'get' ? dumbOf(plan.request.path, x.status, x.body) : fold(classOf(x.status, x.body, ws)));
321// The v2 advertisement's capabilities on the target (object-info's expectation depends on it).
322export async function v2Caps(url, auth) {
323 const h = {};
324 auth.headers('write', h);
325 const r = await rawExchange(url, { method: 'GET', suffix: '/info/refs?service=git-upload-pack', headers: { 'git-protocol': 'version=2' }, body: null, framing: { tag: 'length' } }, h);
326 return { caps: pktLinesOf(r.body).lines.filter(l => !l.startsWith('<') && !l.startsWith('#')).map(l => l.trim()), ray: r.headers['cf-ray'] ?? null };
327}
329// Whether one sender's result meets the plan's stated answer. `end` is the judge's view after the plan.
330export function meets(plan, res, end, ws, caps, seeded = { 'refs/heads/main': ws.base }) {
331 const ref = end.refs[wireRef(res.corner)] ?? null;
332 const clean = end.fsck.length === 0 && end.refs['refs/heads/main'] === ws.base;
333 const a = plan.expect;
334 const asSeeded = () => JSON.stringify(Object.entries(end.refs).sort()) === JSON.stringify(Object.entries(seeded).sort()) && end.fsck.length === 0;
335 const landedRef = r => res.cls === 'landed' && end.refs[refFor(r, res.corner)] === ws.next && clean;
336 const refusedRef = r => refusedClass(res.cls) && !(refFor(r, res.corner) in end.refs) && clean;
337 switch (a.tag) {
338 case 'ref-landed': return landedRef(a.ref);
339 case 'ref-refused': return refusedClass(res.cls) && asSeeded();
340 case 'ref-either': return landedRef(a.ref) || refusedRef(a.ref);
341 case 'dumb': return res.cls.startsWith('status:4') || res.cls === (a.path === '/info/refs' ? `dumb-refs:${ws.base}` : a.path === '/HEAD' ? 'dumb-head:refs/heads/main' : res.cls.startsWith('dumb-packs:') ? res.cls : 'dumb-packs');
342 case 'listing': return res.cls === listingOf(Object.entries(seeded).map(([n, o]) => [o, n])) && asSeeded();
343 case 'v0-advertisement': return res.cls === `v0-adv:${ws.base}`;
344 case 'ls-refs-main': return res.cls === `ls-refs:${ws.base}` && clean;
345 case 'landed': return res.cls === 'landed' && ref === ws.next && end.fsck.length === 0;
346 case 'refused': return refusedClass(res.cls) && ref === null && clean;
347 case 'object-info': return caps.some(c => c === 'object-info' || c.startsWith('object-info=')) ? res.cls === `object-info:${ws.blobSize}` : refusedClass(res.cls);
348 case 'status': return a.statuses.some(st => res.cls === `status:${st}` || res.cls === `class:${st === 404 ? 'not_found' : 'auth'}`);
349 }
350 return false;
351}
352// A landed push's report class is folded to `landed` so senders compare; a plain refusal keeps its words.
353const fold = c => (c.startsWith('report:unpack ok;ok ') && !c.includes(';ng ') ? 'landed' : c);
355// ---- stock git and ply ----
356async function gitCorner(plan, target, ws, root) {
357 const kind = credentialOf(plan);
358 const cfg = target.auth.gitFile(kind);
359 const env = { ...gitEnv(root), GIT_TERMINAL_PROMPT: '0', GIT_TRACE_CURL: join(root, `trace-${Date.now()}`), GIT_TRACE_CURL_NO_DATA: '1' };
360 const pre = cfg === null ? [] : ['-c', `include.path=${cfg}`];
361 const url = target.auth.url(kind);
362 const t0 = performance.now();
363 let r, cls;
364 try {
365 switch (plan.git.tag) {
366 case 'ls-remote-v0': {
367 r = await run('git', [...pre, '-c', 'protocol.version=0', 'ls-remote', url], { cwd: root, env });
368 const main = r.stdout.split('\n').find(l => l.endsWith('\trefs/heads/main'));
369 cls = r.status === 0 ? `v0-adv:${main ? main.slice(0, 40) : 'no-main'}` : `git-exit:${r.status}`;
370 break;
371 }
372 case 'push-chunked': {
373 r = await run('git', [...pre, '-c', 'http.postBuffer=4096', 'push', '--porcelain', url, `${ws.next}:${wireRef('git')}`], { cwd: ws.dir, env });
374 cls = r.status === 0 ? 'landed' : `refused`;
375 break;
376 }
377 case 'fetch-filter': {
378 const dir = join(root, `filter-${Date.now()}.git`);
379 r = await run('git', [...pre, 'clone', '-q', '--bare', `--filter=sparse:oid=${ws.blob}`, url, dir], { cwd: root, env });
380 cls = r.status === 0 ? 'landed' : 'refused';
381 rmSync(dir, { recursive: true, force: true });
382 break;
383 }
384 case 'ls-remote': {
385 r = await run('git', [...pre, 'ls-remote', url], { cwd: root, env });
386 const s = r.stderr;
387 cls = r.status === 0 ? 'listed' : /returned error: (\d{3})/.test(s) ? `status:${/returned error: (\d{3})/.exec(s)[1]}` : /could not read Username|Authentication failed|terminal prompts disabled/.test(s) ? 'status:401' : /not found/i.test(s) ? 'status:404' : `git-exit:${r.status}`;
388 break;
389 }
390 case 'push-ref': {
391 const ref = refFor(plan.expect.ref, 'git');
392 r = await run('git', [...pre, 'push', '--porcelain', url, `${ws.next}:${ref}`], { cwd: ws.dir, env });
393 let tr = '';
394 try { tr = readFileSync(env.GIT_TRACE_CURL, 'latin1'); } catch { tr = ''; }
395 const posted = /=> Send header: POST \S*git-receive-pack/.test(tr);
396 cls = r.status === 0 ? 'landed' : posted ? 'refused' : 'client-refused';
397 break;
398 }
399 case 'ls-remote-dumb': {
400 r = await run('git', [...pre, 'ls-remote', url], { cwd: root, env: { ...env, GIT_SMART_HTTP: '0' } });
401 const main = r.stdout.split('\n').find(l => l.endsWith('\trefs/heads/main'));
402 const s = r.stderr;
403 cls = r.status === 0 ? (main ? `dumb-refs:${main.slice(0, 40)}` : 'dumb-refs:no-main') : /returned error: (\d{3})/.test(s) ? `status:${/returned error: (\d{3})/.exec(s)[1]}` : /not found/i.test(s) ? 'status:404' : `git-exit:${r.status}`;
404 break;
405 }
406 case 'ls-remote-all': {
407 r = await run('git', [...pre, '-c', `protocol.version=${plan.git.protocol === 'v0' ? 0 : 2}`, 'ls-remote', url], { cwd: root, env });
408 cls = r.status === 0 ? listingOf(r.stdout.split('\n').filter(Boolean).map(l => l.split('\t'))) : `git-exit:${r.status}`;
409 break;
410 }
411 default: throw new Error(`no git form ${plan.git.tag}`);
412 }
413 let trace = '';
414 try { trace = readFileSync(env.GIT_TRACE_CURL, 'latin1'); } catch { trace = ''; }
415 rmSync(env.GIT_TRACE_CURL, { force: true });
416 const rays = [...trace.matchAll(/<= Recv header: cf-ray: (\S+)/gi)].map(m => m[1]);
417 const chunked = /=> Send header: Transfer-Encoding: chunked/i.test(trace);
418 return { corner: 'git', cls, status: null, interim100: null, ms: +(performance.now() - t0).toFixed(1), rays, detail: `${r.stderr.trim().split('\n').slice(-2).join(' | ').slice(0, 200)}${plan.git.tag === 'push-chunked' ? ` [chunked=${chunked}]` : ''}`, error: null };
419 } finally {
420 if (cfg !== null) rmSync(dirname(cfg), { recursive: true, force: true });
421 }
422}
423async function plyCorner(plan, target, ws, tracer) {
424 const kind = credentialOf(plan);
425 const rays = [];
426 let posts = 0;
427 const fetchImpl = async (u, init = {}) => {
428 if (String(init.method ?? 'GET').toUpperCase() === 'POST' && String(u).endsWith('git-receive-pack')) posts++;
429 const headers = new Headers(init.headers);
430 const h = {};
431 target.auth.headers(kind, h);
432 for (const [k, v] of Object.entries(h)) headers.set(k, v);
433 const res = await fetch(u, { ...init, headers });
434 const ray = res.headers.get('cf-ray');
435 if (ray !== null) rays.push(ray);
436 return res;
437 };
438 const t0 = performance.now();
439 const client = GitHttp.create(target.auth.url(kind), { tracer, fetchImpl, quirks: {} });
440 if (!client.ok) return { corner: 'ply', cls: `ply:${client.error.code}`, status: null, interim100: null, ms: 0, rays, detail: String(client.error.message).slice(0, 200), error: null };
441 let r;
442 switch (plan.ply.tag) {
443 case 'ls-refs': r = await client.value.lsRefs(); break;
444 case 'fetch-filter': r = await client.value.fetchPack({ wants: [ws.base], filter: `sparse:oid=${ws.blob}` }); break;
445 case 'push-ref': r = await client.value.push({ commands: [{ ref: refFor(plan.expect.ref, 'ply'), oldOid: ZERO, newOid: ws.next }], objects: ws.objects }); break;
446 case 'ls-refs-all': {
447 r = await client.value.lsRefs();
448 const ms = +(performance.now() - t0).toFixed(1);
449 if (r.ok) return { corner: 'ply', cls: listingOf([...r.value.refs.values()].map(x => [x.oid, x.name])), status: null, interim100: null, ms, rays, detail: `${r.value.refs.size} refs`, error: null };
450 return { corner: 'ply', cls: `ply:${r.error.code}`, status: null, interim100: null, ms, rays, detail: String(r.error.message).slice(0, 160), error: null };
451 }
452 default: throw new Error(`no ply form ${plan.ply.tag}`);
453 }
454 const ms = +(performance.now() - t0).toFixed(1);
455 if (plan.ply.tag === 'push-ref') return { corner: 'ply', cls: r.ok ? 'landed' : posts === 0 ? 'client-refused' : 'refused', status: null, interim100: null, ms, rays, detail: r.ok ? 'ok' : `${r.error.code}: ${String(r.error.message).slice(0, 160)}`, error: null };
456 if (r.ok) return { corner: 'ply', cls: plan.ply.tag === 'ls-refs' ? 'listed' : 'landed', status: null, interim100: null, ms, rays, detail: 'ok', error: null };
457 const st = /"status":(\d{3})/.exec(JSON.stringify(r.error))?.[1] ?? null;
458 const code = r.error.code;
459 const cls = plan.ply.tag === 'ls-refs' ? (code === 'auth' || code === 'not_found' ? `class:${code}` : `ply:${code}`) : 'refused';
460 return { corner: 'ply', cls, status: st === null ? null : Number(st), interim100: null, ms, rays, detail: `${code}: ${String(r.error.message).slice(0, 160)}`, error: null };
461}
463// One plan on one target: every corner in order, then the judge. `caps`: the target's v2 capabilities.
464export async function runWirePlan(plan, corners, target, ws, { root, tracer, caps }) {
465 const results = [];
466 for (const corner of corners) {
467 const req = requestOf(plan, ws, corner);
468 const kind = credentialOf(plan);
469 const url = target.auth.url(kind);
470 if (corner === 'raw' || corner === 'curl') {
471 let x;
472 if (corner === 'raw') { const h = {}; target.auth.headers(kind, h); x = await rawExchange(url, req, h); } else {
473 const f = target.auth.curlFile(kind);
474 try { x = await curlExchange(url, req, f, root); } finally { if (f !== null) rmSync(dirname(f), { recursive: true, force: true }); }
475 }
476 const ray = x.headers['cf-ray'] ?? null;
477 results.push({ corner, cls: answerClass(plan, x, ws), status: x.status, interim100: x.interim100, ms: x.ms, rays: ray === null ? [] : [ray], detail: x.error ?? firstLine(x.body), error: x.error });
478 } else if (corner === 'git') results.push(await gitCorner(plan, target, ws, root));
479 else results.push(await plyCorner(plan, target, ws, tracer));
480 }
481 const end = await judgeEnd(target.judgeUrl, root, ws.env);
482 const seeded = (await seedOf(plan, ws)).refs;
483 const many = Object.keys(end.refs).length > 50;
484 return { results: results.map(r => ({ ...r, meets: meets(plan, r, end, ws, caps, seeded) })), end: { refs: many ? { count: Object.keys(end.refs).length, main: end.refs['refs/heads/main'] ?? null } : end.refs, fsck: end.fsck, head: end.head } };
485}
486// The judge's view; a repository stock git cannot mirror (a ref name git refuses to write, say) is itself a problem.
487async function judgeEnd(url, root, env) {
488 try { return await observeEnd(url, root, env); } catch (e) {
489 const ls = await run('git', ['ls-remote', url], { cwd: root, env });
490 const refs = Object.fromEntries(ls.stdout.split('\n').filter(Boolean).map(l => l.split('\t')).filter(([, n]) => n !== 'HEAD' && !n.endsWith('^{}')).map(([o, n]) => [n, o]));
491 return { head: null, refs, fsck: [`judge failed: ${String(e?.message ?? e).slice(0, 200)}`] };
492 }
493}
495// Differences that matter, between two targets for the same sender, and between senders on one target.
496export function wireDiffs(planId, byTarget) {
497 const out = [];
498 const [ref, art] = [byTarget.ref, byTarget.art];
499 if (ref && art) for (const a of art.results) {
500 const r = ref.results.find(x => x.corner === a.corner);
501 if (r && r.meets !== a.meets) out.push({ plan: planId, pair: `art-${a.corner}~ref-${a.corner}`, field: 'meets', left: `${a.meets} (${a.cls})`, right: `${r.meets} (${r.cls})` });
502 else if (r && r.cls !== a.cls) out.push({ plan: planId, pair: `art-${a.corner}~ref-${a.corner}`, field: 'class', left: a.cls, right: r.cls, minor: true });
503 }
504 for (const [name, t] of Object.entries(byTarget)) {
505 if (!t) continue;
506 const ms = new Set(t.results.map(x => x.meets));
507 if (ms.size > 1) out.push({ plan: planId, pair: `${name}:senders`, field: 'meets', left: t.results.map(x => `${x.corner}=${x.meets}`).join(' '), right: '' });
508 }
509 return out;
510}
512export function referenceAuth(url) {
513 return { headers: () => {}, curlFile: () => null, gitFile: () => null, url: () => url, hasRevoked: () => false };
514}
515export const tempRoot = () => mkdtempSync(join(tmpdir(), 'ply-wire-'));
Diagnostics4 git calls · 485 ms
CallStatusSizeTime0485 ms
discover200152 B2 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 B · 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=145 git.response.cache=HIT 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=480 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
fetch20010 KiB480 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=10391 git.response.cache=MISS git.response.duration_ms=480 git.fetch.wants=0bd5b85589d16fed471abcc68aff84d7431049a5 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=10391 git.pkt.payload.max=10370 git.sideband.band1.bytes=10369 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
- a5e307634b1f15db
- Request
- c3b3bd8c…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format