Artifactspublic-v0

ply

main
ply4 git calls640 ms43 KiB2 cachedprotocol v2
284 lines17 KiBRaw
1// A pushPack whose caller's signal had already fired sent nothing, so its outcome is known. The counters are the
2// test's own: pulls and cancels of the pack stream it built, requests seen by its server. None is a number ply reports.
3import { test } from 'vitest';
4import assert from 'node:assert/strict';
5import http from 'node:http';
6import { execFileSync, spawn } from 'node:child_process';
7import { mkdtempSync, rmSync } from 'node:fs';
8import { tmpdir } from 'node:os';
9import { join } from 'node:path';
10import { createTransport } from '../src/http.js';
11import { ZERO_OID, push } from '../src/push.js';
12import { pushPack } from '../src/receivecaps.js';
13import { streamSource } from '../src/packwrite.js';
14import { Tracer, arrayExporter, flattenLine } from '../src/otel.js';
15import { unwrap } from './unwrap.js';
17test('pushPack under an already-aborted signal: a definite aborted, no request, the pack never read, locked or cancelled', async () => {
18 let requests = 0;
19 const server = http.createServer((req, res) => { requests++; req.resume(); res.end(); });
20 await new Promise(r => { server.listen(0, '127.0.0.1', r); });
21 try {
22 const ac = new AbortController();
23 ac.abort();
24 const pack = { pulls: 0, cancels: 0 };
25 const stream = new ReadableStream({ pull(c) { pack.pulls++; c.enqueue(new Uint8Array(1024)); c.close(); }, cancel() { pack.cancels++; } }, { highWaterMark: 0 });
26 const t = unwrap(createTransport(`http://127.0.0.1:${server.address().port}/repo.git`, { signal: ac.signal }));
27 const r = await pushPack(t, { ref: 'refs/heads/main', oldOid: ZERO_OID, newOid: 'a'.repeat(40), pack: stream, length: 1024 });
28 assert.equal(r.ok, false);
29 assert.equal(r.error.code, 'aborted', `nothing was sent, so nothing is unknown: ${r.error.message}`);
30 assert.deepEqual({ ...pack, locked: stream.locked, requests }, { pulls: 0, cancels: 0, locked: false, requests: 0 });
31 // the pack is still the caller's: it can be read (or sent again) afterwards
32 const first = await stream.getReader().read();
33 assert.equal(first.value.length, 1024);
34 } finally { server.close(); server.closeAllConnections(); }
35});
37// Decision table: a failure before the transport's first pull of the request body is definite; the same failure after
38// one pull is 'ambiguous'. The repository is a real bare one served by git http-backend; "no ref moved" is git's own
39// answer (for-each-ref before and after). Expected codes come from the fetch contract, not from ply: a rejected
40// fetch() is a network error (a TypeError), an abort is the signal's AbortError.
41const dnsError = () => new TypeError('fetch failed', { cause: Object.assign(new Error('getaddrinfo ENOTFOUND git.invalid'), { code: 'ENOTFOUND', syscall: 'getaddrinfo', hostname: 'git.invalid' }) });
42const refused = () => new TypeError('fetch failed', { cause: Object.assign(new Error('connect ECONNREFUSED 127.0.0.1:9'), { code: 'ECONNREFUSED' }) });
43const isReceive = url => String(url).endsWith('/git-receive-pack');
44// each row: how the POST fails, and the code the fetch contract gives that failure
45const FAILURES = [
46 ['a signal fired before the call', { preAbort: true, reject: null }, 'aborted'],
47 ['fetch rejects before reading the body', { preAbort: false, reject: refused }, 'network'],
48 ['a DNS failure (getaddrinfo ENOTFOUND)', { preAbort: false, reject: dnsError }, 'network'],
49];
51const gitServer = async () => {
52 const dir = mkdtempSync(join(tmpdir(), 'ply-unsent-'));
53 const repo = join(dir, 'repo.git');
54 const git = (args, input = '') => execFileSync('git', args, { cwd: dir, encoding: 'utf8', input, env: { ...process.env, GIT_AUTHOR_NAME: 'a', GIT_AUTHOR_EMAIL: 'a@a', GIT_COMMITTER_NAME: 'a', GIT_COMMITTER_EMAIL: 'a@a', GIT_AUTHOR_DATE: '1700000000 +0000', GIT_COMMITTER_DATE: '1700000000 +0000' } }).trim();
55 git(['init', '-q', '--bare', repo]);
56 git(['-C', repo, 'config', 'http.receivepack', 'true']);
57 const blob = git(['-C', repo, 'hash-object', '-w', '--stdin'], 'x\n');
58 const tree = git(['-C', repo, 'mktree'], `100644 blob ${blob}\tf\n`);
59 const commit = git(['-C', repo, 'commit-tree', tree, '-m', 'base']);
60 git(['-C', repo, 'update-ref', 'refs/heads/main', commit]);
61 const server = http.createServer((req, res) => {
62 const [path, query = ''] = req.url.split('?');
63 const cgi = spawn('git', ['http-backend'], { env: { ...process.env, GIT_PROJECT_ROOT: dir, GIT_HTTP_EXPORT_ALL: '1', REQUEST_METHOD: req.method, PATH_INFO: path, QUERY_STRING: query, CONTENT_TYPE: req.headers['content-type'] ?? '', REMOTE_USER: 'u', REMOTE_ADDR: '127.0.0.1' } });
64 req.pipe(cgi.stdin);
65 const out = [];
66 cgi.stdout.on('data', d => out.push(d));
67 cgi.on('close', () => {
68 const all = Buffer.concat(out), cut = all.indexOf('\r\n\r\n');
69 for (const h of all.subarray(0, cut).toString().split('\r\n')) {
70 const k = h.indexOf(':');
71 if (h.slice(0, k).toLowerCase() === 'status') res.statusCode = Number(h.slice(k + 1).trim().split(' ')[0]); else res.setHeader(h.slice(0, k), h.slice(k + 1).trim());
72 }
73 res.end(all.subarray(cut + 4));
74 });
75 });
76 await new Promise(r => { server.listen(0, '127.0.0.1', r); });
77 return { url: `http://127.0.0.1:${server.address().port}/repo.git`, commit, refs: () => git(['-C', repo, 'for-each-ref']), close: () => { server.close(); server.closeAllConnections(); rmSync(dir, { recursive: true, force: true }); } };
78};
80const packOf = () => {
81 const seen = { pulls: 0, cancels: 0 };
82 const stream = new ReadableStream({ pull(c) { seen.pulls++; c.enqueue(new Uint8Array(1024)); c.close(); }, cancel() { seen.cancels++; } }, { highWaterMark: 0 });
83 return { seen, stream };
84};
86for (const [name, how, code] of FAILURES) {
87 test(`pushPack, ${name}: before the first body pull it is the definite ${code}, the pack is never read, and git shows no ref moved`, async () => {
88 const g = await gitServer();
89 try {
90 const before = g.refs();
91 assert.match(before, new RegExp(`^${g.commit} commit\\trefs/heads/main$`, 'u'));
92 const ac = new AbortController();
93 if (how.preAbort) ac.abort();
94 let posts = 0;
95 const fetchImpl = async (url, init) => { if (isReceive(url) && how.reject !== null) { posts++; throw how.reject(); } return fetch(url, init); };
96 const { seen, stream } = packOf();
97 const t = unwrap(createTransport(g.url, { signal: ac.signal, fetchImpl }));
98 const r = await pushPack(t, { ref: 'refs/heads/main', oldOid: g.commit, newOid: 'a'.repeat(40), pack: stream, length: 1024 });
99 assert.equal(r.ok, false);
100 assert.equal(r.error.code, code, r.error.message);
101 assert.deepEqual({ ...seen, locked: stream.locked }, { pulls: 0, cancels: 0, locked: false });
102 if (how.reject !== null) assert.equal(posts, 1, 'the POST was attempted once and not retried');
103 assert.equal(g.refs(), before, 'git for-each-ref is unchanged');
104 // the pack is still the caller's
105 assert.equal((await stream.getReader().read()).value.length, 1024);
106 } finally { g.close(); }
107 });
109 test(`pushPack, ${name}: after one body pull the same failure is ambiguous`, async () => {
110 const g = await gitServer();
111 try {
112 const ac = new AbortController();
113 let pulled = 0;
114 // the transport takes one chunk of the body (the command list), then the same failure happens
115 const fetchImpl = async (url, init) => {
116 if (!isReceive(url)) return fetch(url, init);
117 const first = await init.body.getReader().read();
118 pulled += first.value.length;
119 if (how.reject === null) { ac.abort(); throw ac.signal.reason; }
120 throw how.reject();
121 };
122 const { stream } = packOf();
123 const t = unwrap(createTransport(g.url, { signal: ac.signal, fetchImpl }));
124 const r = await pushPack(t, { ref: 'refs/heads/main', oldOid: g.commit, newOid: 'a'.repeat(40), pack: stream, length: 1024 });
125 assert.ok(pulled > 0, 'the double read the first chunk');
126 assert.equal(r.ok, false);
127 assert.equal(r.error.code, 'ambiguous', r.error.message);
128 } finally { g.close(); }
129 });
132// push() with objects (the same rule, for the entry that writes the pack itself). 40 blobs of 60,000 random bytes do not
133// deflate, so the pack is 2.4 MB and its request body is a stream; the double sees that for itself (init.body is a
134// ReadableStream). Expected codes are the fetch contract's, as above; "no ref moved" is git's for-each-ref.
135const blobs = () => Array.from({ length: 40 }, () => ({ type: 3, data: crypto.getRandomValues(new Uint8Array(60000)) }));
136for (const [name, how, code] of FAILURES) {
137 test(`push with a streamed pack, ${name}: before the first body pull it is the definite ${code}, and git shows no ref moved`, async () => {
138 const g = await gitServer();
139 try {
140 const before = g.refs();
141 const ac = new AbortController();
142 if (how.preAbort) ac.abort();
143 const seen = { posts: 0, streamed: 0 };
144 const fetchImpl = async (url, init) => {
145 if (!isReceive(url)) return fetch(url, init);
146 seen.posts++;
147 if (init.body instanceof ReadableStream) seen.streamed++;
148 if (how.reject !== null) throw how.reject();
149 return fetch(url, init);
150 };
151 const t = unwrap(createTransport(g.url, { signal: ac.signal, fetchImpl }));
152 const r = await push(t, { ref: 'refs/heads/main', oldOid: g.commit, newOid: 'a'.repeat(40), objects: blobs() });
153 assert.equal(r.ok, false);
154 assert.equal(r.error.code, code, r.error.message);
155 // a signal that had fired: no request is made; a rejected fetch: one POST with a stream body, not retried
156 assert.deepEqual(seen, how.preAbort ? { posts: 0, streamed: 0 } : { posts: 1, streamed: 1 });
157 assert.equal(g.refs(), before, 'git for-each-ref is unchanged');
158 } finally { g.close(); }
159 });
161 test(`push with a streamed pack, ${name}: after one body pull the same failure is ambiguous`, async () => {
162 const g = await gitServer();
163 try {
164 const ac = new AbortController();
165 let pulled = 0;
166 const fetchImpl = async (url, init) => {
167 if (!isReceive(url)) return fetch(url, init);
168 const first = await init.body.getReader().read();
169 pulled += first.value.length;
170 if (how.reject === null) { ac.abort(); throw ac.signal.reason; }
171 throw how.reject();
172 };
173 const t = unwrap(createTransport(g.url, { signal: ac.signal, fetchImpl }));
174 const r = await push(t, { ref: 'refs/heads/main', oldOid: g.commit, newOid: 'a'.repeat(40), objects: blobs() });
175 assert.ok(pulled > 0, 'the double read the first chunk');
176 assert.equal(r.ok, false);
177 assert.equal(r.error.code, 'ambiguous', r.error.message);
178 assert.equal(r.error.original, code);
179 } finally { g.close(); }
180 });
183// A pack small enough to go as bytes: a signal that had fired before the request is still the definite 'aborted' with
184// no request made. A fetch that rejects is not: bytes handed to fetch() cannot be seen leaving or not, so that stays
185// 'ambiguous' (the near miss of the streamed rows above).
186test('push with a pack sent as bytes: a signal already fired is the definite aborted with no request; a rejected fetch stays ambiguous', async () => {
187 const g = await gitServer();
188 try {
189 const before = g.refs();
190 const small = () => ({ ref: 'refs/heads/main', oldOid: g.commit, newOid: 'a'.repeat(40), objects: [{ type: 3, data: new TextEncoder().encode('hi\n') }] });
191 const ac = new AbortController();
192 ac.abort();
193 let posts = 0;
194 const counting = async (url, init) => { if (isReceive(url)) posts++; return fetch(url, init); };
195 const fired = await push(unwrap(createTransport(g.url, { signal: ac.signal, fetchImpl: counting })), small());
196 assert.deepEqual({ ok: fired.ok, code: fired.error.code, posts }, { ok: false, code: 'aborted', posts: 0 });
198 const kinds = [];
199 const rejecting = async (url, init) => { if (!isReceive(url)) return fetch(url, init); kinds.push(init.body instanceof ReadableStream ? 'stream' : 'bytes'); throw refused(); };
200 const lost = await push(unwrap(createTransport(g.url, { fetchImpl: rejecting })), small());
201 assert.deepEqual({ ok: lost.ok, code: lost.error.code, original: lost.error.original, kinds }, { ok: false, code: 'ambiguous', original: 'network', kinds: ['bytes'] });
202 assert.equal(g.refs(), before, 'git for-each-ref is unchanged');
203 } finally { g.close(); }
204});
206// The HTTP half of the same rule: an answer that arrives while the body is still unpulled. The server cannot have
207// read a command list nobody handed over, so the answer's own code is the outcome and the pack is still the caller's.
208// Expected codes are the status classes as HTTP defines them and README names them (401 auth, 404 not_found,
209// 429 rate_limited, 5xx upstream_5xx, an unfollowed redirect http, a 200 that is not a receive-pack result not_git),
210// written here by hand. The double answers without touching init.body; pulls and cancels are the test's own counters.
211const ANSWERS = [
212 ['401', () => new Response('no', { status: 401 }), 'auth'],
213 ['404', () => new Response('no', { status: 404 }), 'not_found'],
214 ['429', () => new Response('no', { status: 429 }), 'rate_limited'],
215 ['500', () => new Response('no', { status: 500 }), 'upstream_5xx'],
216 ['a 302 redirect', () => new Response('', { status: 302, headers: { location: 'http://127.0.0.1:1/elsewhere/git-receive-pack' } }), 'http'],
217 ['200 with content-type text/plain', () => new Response('hello', { status: 200, headers: { 'content-type': 'text/plain' } }), 'not_git'],
218];
219for (const [name, answer, code] of ANSWERS) {
220 test(`pushPack, the POST answered ${name} with the body never pulled: the definite ${code}, the pack never read, locked or cancelled`, async () => {
221 const g = await gitServer();
222 try {
223 const before = g.refs();
224 let posts = 0;
225 const fetchImpl = async (url, init) => { if (!isReceive(url)) return fetch(url, init); posts++; return answer(); };
226 const { seen, stream } = packOf();
227 const exporter = arrayExporter();
228 const t = unwrap(createTransport(g.url, { fetchImpl, tracer: new Tracer({ exporter }) }));
229 const r = await pushPack(t, { ref: 'refs/heads/main', oldOid: g.commit, newOid: 'a'.repeat(40), pack: stream, length: 1024 });
230 assert.equal(r.ok, false);
231 assert.equal(r.error.code, code, r.error.message);
232 assert.deepEqual({ ...seen, locked: stream.locked, posts }, { pulls: 0, cancels: 0, locked: false, posts: 1 });
233 assert.equal(g.refs(), before, 'git for-each-ref is unchanged');
234 assert.equal((await stream.getReader().read()).value.length, 1024, 'the pack is still the caller\'s');
235 // the span says no more than the error it carries: no attribute outside the documented ones
236 const receive = exporter.lines.flatMap(flattenLine).filter(s => s.name === 'git.receive-pack');
237 assert.equal(receive.length, 1);
238 assert.equal('git.push.unsent' in receive[0].attrs, false);
239 } finally { g.close(); }
240 });
242 test(`pushPack, the POST answered ${name} after one body pull: ambiguous, with ${code} as the original`, async () => {
243 const g = await gitServer();
244 try {
245 let pulled = 0;
246 const fetchImpl = async (url, init) => {
247 if (!isReceive(url)) return fetch(url, init);
248 const first = await init.body.getReader().read();
249 pulled += first.value.length;
250 return answer();
251 };
252 const { stream } = packOf();
253 const t = unwrap(createTransport(g.url, { fetchImpl }));
254 const r = await pushPack(t, { ref: 'refs/heads/main', oldOid: g.commit, newOid: 'a'.repeat(40), pack: stream, length: 1024 });
255 assert.ok(pulled > 0, 'the double read the first chunk');
256 assert.equal(r.ok, false);
257 assert.deepEqual({ code: r.error.code, original: r.error.original }, { code: 'ambiguous', original: code });
258 } finally { g.close(); }
259 });
262// The body itself: `pulled` turns true at the first pull and not before; a source given back is unlocked and uncancelled;
263// a body given back and pulled after all errors without handing over the command list; after a pull giveBack does nothing.
264test('streamSource: pulled() is the first pull; giveBack() before it unlocks the source and the body then sends nothing', async () => {
265 const prefix = new TextEncoder().encode('0000');
266 const a = packOf();
267 const body = streamSource(a.stream, prefix, 1024, 4096);
268 assert.deepEqual({ pulled: body.pulled(), locked: a.stream.locked }, { pulled: false, locked: true });
269 body.giveBack();
270 assert.deepEqual({ pulled: body.pulled(), locked: a.stream.locked, ...a.seen }, { pulled: false, locked: false, pulls: 0, cancels: 0 });
271 const late = await body.stream.getReader().read().then(() => null, e => e);
272 assert.equal(late.code, 'internal');
273 assert.deepEqual({ pulled: body.pulled(), sent: body.sent(), ...a.seen }, { pulled: false, sent: 0, pulls: 0, cancels: 0 });
275 const b = packOf();
276 const sent = streamSource(b.stream, prefix, 1024, 4096);
277 const first = await sent.stream.getReader().read();
278 assert.deepEqual([...first.value], [...prefix]);
279 assert.equal(sent.pulled(), true);
280 sent.giveBack();
281 assert.equal(b.stream.locked, true, 'a pulled body keeps its source: only release() ends it, by cancelling');
282 await sent.release();
283 assert.equal(b.seen.cancels, 1);
284});
Diagnostics4 git calls · 640 ms
CallStatusSizeTime0640 ms
discover200152 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=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 · hit6 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 · 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=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=55
Open pack map
fetch2004.8 KiB631 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=631
git.response.bytes=4911
git.response.cache=MISS
git.response.duration_ms=631
git.fetch.wants=8cc62d2f519417a7762629ae204b296d30ac086a
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=4911
git.pkt.payload.max=4890
git.sideband.band1.bytes=4889
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
a5e1a51ecaa85e44
Request
15bb6dc7…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format