Artifactspublic-v0

ply

main
ply4 git calls248 ms47 KiB1 cachedprotocol v2
621 lines35 KiBRaw
1// pushPack (#234): a pack the caller already has, as a stream, with push's commands, capability checks and report handling.
2// The oracle is `git http-backend`: packs written by stock git are pushed, the ref moves, fsck --strict is clean.
3// - known and unknown length; the body is a stream (duplex half, redirect error), read once, never buffered
4// - length wrong in either direction, an errored source, the cap mid-stream: ambiguous with the cause as `original`,
5// the final bytes withheld, the ref not moved
6// - a body that errors but looks complete to the server (workerd) is still not reported as landed
7// - refusals before the request leave the stream unread and unlocked
8// - a source the request did not finish is cancelled
9// - memory: a pack far larger than what is held (child process, --expose-gc)
10import { test, vi, beforeAll as before, afterAll as after } from 'vitest';
11import assert from 'node:assert/strict';
12import { spawn } from 'node:child_process';
13import { MEMORY_CELL_FLAGS } from './memcell.js';
14import { mkdirSync, writeFileSync } from 'node:fs';
15import { join } from 'node:path';
16import { randomFillSync } from 'node:crypto';
17import { git, gitBuf, tmpRoot, makeBareRepo, startGitServer, fsckProblems, initRepo } from './server.js';
18import { createTransport } from '../src/http.js';
19import { ZERO_OID, pushPackChecked, planPackPush } from '../src/push.js';
20import { pushPack, checkPushCaps } from '../src/receivecaps.js';
21import { GitHttp } from '../src/client.js';
22import { withLimits } from '../src/limits.js';
23import { Tracer, arrayExporter, flattenLine } from '../src/otel.js';
24import { pkt, FLUSH } from '../src/pktline.js';
25import { concat } from '../src/bytes.js';
26import { unwrap } from './unwrap.js';
28const REF = 'refs/heads/main';
29let root, cleanup, srv, n = 0, source;
30before(async () => {
31 ({ root, cleanup } = tmpRoot());
32 srv = await startGitServer(root);
33 const work = join(root, 'source');
34 mkdirSync(work);
35 initRepo(work);
36 const blob = new Uint8Array(300_000);
37 randomFillSync(blob);
38 writeFileSync(join(work, 'random.bin'), blob);
39 writeFileSync(join(work, 'text.txt'), 'line\n'.repeat(4000));
40 git(work, ['add', '.']);
41 git(work, ['-c', 'user.name=a', '-c', 'user.email=a@x', 'commit', '-q', '-m', 'one']);
42 writeFileSync(join(work, 'text.txt'), `${'line\n'.repeat(4000)}changed\n`);
43 git(work, ['add', '.']);
44 git(work, ['-c', 'user.name=a', '-c', 'user.email=a@x', 'commit', '-q', '-m', 'two']);
45 const oid = git(work, ['rev-parse', 'HEAD']).trim();
46 source = { oid, pack: new Uint8Array(gitBuf(work, ['pack-objects', '--revs', '--stdout', '-q'], `${oid}\n`)) };
47});
48after(async () => { await srv.close(); cleanup(); });
50const repo = () => { const name = `p${n++}`; return { bare: makeBareRepo(root, name), url: `${srv.url}/${name}.git` }; };
51const tipOf = bare => { try { return git(bare, ['rev-parse', '--verify', '-q', REF]).trim(); } catch { return null; } };
52const create = () => ({ ref: REF, oldOid: ZERO_OID, newOid: source.oid });
53// a stream of `bytes` cut every `size`, recording whether it was read and cancelled
54const streamOf = (bytes, size = 4096, end = c => c.close()) => {
55 const state = { pulls: 0, cancelled: 0 };
56 let at = 0;
57 const stream = new ReadableStream({
58 pull(c) {
59 state.pulls++;
60 if (at >= bytes.length) { end(c); return; }
61 c.enqueue(bytes.slice(at, at + size));
62 at += size;
63 },
64 cancel() { state.cancelled++; },
65 }, { highWaterMark: 0 });
66 return { stream, state };
67};
68// every receive-pack POST: what the body was, and the bytes the server was sent when it is drained here
69const capture = () => {
70 const seen = [];
71 const fetchImpl = async (url, init) => {
72 if (String(url).endsWith('/git-receive-pack')) seen.push({ streamed: typeof init.body?.getReader === 'function', duplex: init.duplex ?? null, redirect: init.redirect });
73 return fetch(url, init);
74 };
75 return { seen, fetchImpl };
76};
77const transport = (url, opts = {}) => unwrap(createTransport(url, opts));
78const report = () => concat([unwrap(pkt('unpack ok\n')), unwrap(pkt(`ok ${REF}\n`)), FLUSH]);
79const reportResponse = () => new Response(report(), { status: 200, headers: { 'content-type': 'application/x-git-receive-pack-result' } });
80// the POST goes to `post`; everything else (the advertisement GET) to the real server
81const routed = post => async (url, init) => (String(url).endsWith('/git-receive-pack') ? post(init) : fetch(url, init));
82const drain = async body => {
83 const reader = body.getReader();
84 let bytes = 0;
85 try { for (;;) { const r = await reader.read(); if (r.done) return { bytes, error: null }; bytes += r.value.length; } } catch (e) { return { bytes, error: e }; }
86};
87const assertAmbiguous = (r, original) => {
88 assert.equal(r.ok, false);
89 assert.equal(r.error.code, 'ambiguous', r.error.message);
90 assert.equal(r.error.original, original);
91};
93test('a stock git pack of known length: chunked stream, ref moves, fsck --strict clean, span counts the body', async () => {
94 const { bare, url } = repo();
95 const { seen, fetchImpl } = capture();
96 const ex = arrayExporter();
97 const { stream } = streamOf(source.pack);
98 const r = await pushPack(transport(url, { fetchImpl, tracer: new Tracer({ exporter: ex }) }), { ...create(), pack: stream, length: source.pack.length });
99 assert.equal(r.ok, true, r.error?.message);
100 assert.deepEqual(seen, [{ streamed: true, duplex: 'half', redirect: 'error' }]);
101 assert.equal(r.value.packBytes, source.pack.length);
102 assert.deepEqual(r.value.entries, []);
103 assert.equal(r.value.report.tag, 'statuses');
104 assert.equal(tipOf(bare), source.oid);
105 assert.deepEqual(fsckProblems(bare, { args: ['--strict'] }), []);
106 const span = ex.lines.flatMap(flattenLine).find(s => s.name === 'git.receive-pack');
107 assert.equal(span.attrs['git.push.pack.bytes'], source.pack.length);
108 const prefix = span.attrs['git.request.bytes'] - source.pack.length;
109 assert.ok(prefix > 100 && prefix < 400, String(prefix));
110 assert.equal(span.attrs['http.request.body.size'], span.attrs['git.request.bytes']);
111});
113test('an unknown length, cut into small chunks, through GitHttp: the same push', async () => {
114 const { bare, url } = repo();
115 const { stream } = streamOf(source.pack, 1000);
116 const r = await unwrap(GitHttp.create(url)).pushPack({ commands: [create()], pack: stream });
117 assert.equal(r.ok, true, r.error?.message);
118 assert.equal(r.value.packBytes, source.pack.length);
119 assert.equal(tipOf(bare), source.oid);
120 assert.deepEqual(fsckProblems(bare, { args: ['--strict'] }), []);
121});
123test('empty chunks in the stream are skipped; a chunk may be any Uint8Array', async () => {
124 const { bare, url } = repo();
125 const parts = [new Uint8Array(0), source.pack.subarray(0, 12), new Uint8Array(0), source.pack.subarray(12), new Uint8Array(0)];
126 const stream = new ReadableStream({ pull(c) { const p = parts.shift(); if (p === undefined) c.close(); else c.enqueue(p); } }, { highWaterMark: 0 });
127 const r = await pushPack(transport(url), { ...create(), pack: stream, length: source.pack.length });
128 assert.equal(r.ok, true, r.error?.message);
129 assert.equal(tipOf(bare), source.oid);
130});
132test('a source shorter than length: ambiguous (bad_request), its last chunk withheld, the ref does not move', async () => {
133 const { bare, url } = repo();
134 let got = null;
135 const fetchImpl = routed(async init => { got = await drain(init.body); return reportResponse(); });
136 const { stream } = streamOf(source.pack, 4096);
137 const r = await pushPack(transport(url, { fetchImpl }), { ...create(), pack: stream, length: source.pack.length + 1, sideband: false });
138 assertAmbiguous(r, 'bad_request');
139 assert.match(r.error.message, /yielded \d+ bytes where length said/u);
140 assert.equal(r.error.cause.got, source.pack.length);
141 assert.equal(r.error.cause.max, source.pack.length + 1);
142 // the server never got the last chunk, so it never got the trailer
143 assert.ok(got.error !== null);
144 assert.ok(got.bytes < source.pack.length, `${got.bytes}`);
145 assert.equal(tipOf(bare), null);
146});
148test('a source longer than length: refused at the chunk that goes over, which is never sent', async () => {
149 const { bare, url } = repo();
150 let got = null;
151 const fetchImpl = routed(async init => { got = await drain(init.body); return reportResponse(); });
152 const { stream, state } = streamOf(source.pack, 4096);
153 const length = 4096 * 3;
154 const r = await pushPack(transport(url, { fetchImpl }), { ...create(), pack: stream, length, sideband: false });
155 assertAmbiguous(r, 'bad_request');
156 assert.equal(r.error.cause.max, length);
157 assert.equal(r.error.cause.got, 4096 * 4);
158 assert.ok(got.bytes <= length + 400, `${got.bytes}`);
159 assert.equal(state.cancelled, 1);
160 assert.equal(tipOf(bare), null);
161});
163test('length equal to what the source yields, against a mismatch by one in each direction, is the only pass', async () => {
164 for (const [delta, landed] of [[0, true], [-1, false], [1, false]]) {
165 const { bare, url } = repo();
166 const { stream } = streamOf(source.pack);
167 const r = await pushPack(transport(url), { ...create(), pack: stream, length: source.pack.length + delta });
168 assert.equal(r.ok, landed, `${delta}: ${r.error?.message}`);
169 assert.equal(tipOf(bare), landed ? source.oid : null, String(delta));
170 }
171});
173test('an errored source: ambiguous (internal), its cause kept, the ref does not move', async () => {
174 const { bare, url } = repo();
175 const boom = new Error('source died');
176 const { stream } = streamOf(source.pack.subarray(0, 20_000), 4096, c => c.error(boom));
177 const r = await pushPack(transport(url), { ...create(), pack: stream });
178 assertAmbiguous(r, 'internal');
179 assert.equal(r.error.cause.cause, boom);
180 assert.equal(tipOf(bare), null);
181});
183test('a runtime that ends an errored body as if complete: a clean report is still not "landed"', async () => {
184 const { url } = repo();
185 const fetchImpl = routed(async init => { await drain(init.body); return reportResponse(); });
186 const { stream } = streamOf(source.pack.subarray(0, 20_000), 4096, c => c.error(new Error('source died')));
187 const r = await pushPack(transport(url, { fetchImpl }), { ...create(), pack: stream, sideband: false });
188 assertAmbiguous(r, 'internal');
189});
191test('a chunk that is not bytes: ambiguous (bad_request), nothing after it sent', async () => {
192 const { bare, url } = repo();
193 const stream = new ReadableStream({ start(c) { c.enqueue('not bytes'); c.close(); } });
194 const r = await pushPack(transport(url), { ...create(), pack: stream });
195 assertAmbiguous(r, 'bad_request');
196 assert.equal(r.error.cause.what, 'push pack chunk');
197 assert.equal(tipOf(bare), null);
198});
200test('over maxPackBytes with a known length: limit_exceeded before any request, the stream unread and unlocked', async () => {
201 const { url } = repo();
202 let requests = 0;
203 const fetchImpl = async (u, init) => { requests++; return fetch(u, init); };
204 const { stream, state } = streamOf(source.pack);
205 const r = await pushPack(transport(url, { fetchImpl, limits: withLimits({ maxPackBytes: 100 }) }), { ...create(), pack: stream, length: 101 });
206 assert.equal(r.ok, false);
207 assert.equal(r.error.code, 'limit_exceeded');
208 assert.equal(r.error.got, 101);
209 assert.equal(r.error.max, 100);
210 assert.equal(requests, 0);
211 assert.equal(state.pulls, 0);
212 assert.equal(stream.locked, false);
213});
215test('exactly maxPackBytes passes, with a known length and an unknown one', async () => {
216 for (const length of [source.pack.length, null]) {
217 const { bare, url } = repo();
218 const { stream } = streamOf(source.pack);
219 const r = await pushPack(transport(url, { limits: withLimits({ maxPackBytes: source.pack.length }) }), { ...create(), pack: stream, length });
220 assert.equal(r.ok, true, r.error?.message);
221 assert.equal(tipOf(bare), source.oid);
222 }
223});
225test('over maxPackBytes with no length: refused mid-stream at the chunk that crosses, never sent; the source is cancelled', async () => {
226 const { bare, url } = repo();
227 let got = null;
228 const fetchImpl = routed(async init => { got = await drain(init.body); return reportResponse(); });
229 const { stream, state } = streamOf(source.pack, 4096);
230 const max = 4096 * 2 + 1;
231 const r = await pushPack(transport(url, { fetchImpl, limits: withLimits({ maxPackBytes: max }) }), { ...create(), pack: stream, sideband: false });
232 assertAmbiguous(r, 'limit_exceeded');
233 assert.equal(r.error.cause.got, 4096 * 3);
234 assert.equal(r.error.cause.max, max);
235 assert.ok(got.bytes < max + 400, `${got.bytes}`);
236 assert.equal(state.cancelled, 1);
237 assert.equal(tipOf(bare), null);
238});
240// #246: a pack the server cannot unpack is a definite refusal, never ambiguous; nothing moves
241test('a corrupt pack against stock git: push_rejected (git\'s unpack failure), the ref not moved, nothing read back', async () => {
242 const { createHash } = await import('node:crypto');
243 const withTrailer = body => concat([body, new Uint8Array(createHash('sha1').update(body).digest())]);
244 const body = source.pack.subarray(0, source.pack.length - 20);
245 // a zlib stream damaged inside the first object, and a first entry of type 0 (invalid), both with a valid trailer
246 const damaged = new Uint8Array(body); damaged[40] ^= 0xff;
247 const type0 = new Uint8Array(body); type0[12] &= 0x8f;
248 for (const pack of [withTrailer(damaged), withTrailer(type0)]) {
249 const { bare, url } = repo();
250 const posts = [];
251 const fetchImpl = async (u, init) => { posts.push(String(u).split('/').at(-1)); return fetch(u, init); };
252 const r = await pushPack(transport(url, { fetchImpl }), { ...create(), pack: streamOf(pack).stream, length: pack.length });
253 assert.equal(r.ok, false);
254 assert.equal(r.error.code, 'push_rejected', r.error.message);
255 assert.equal(r.error.report.tag, 'unpackFailed');
256 assert.equal(tipOf(bare), null);
257 assert.equal(posts.filter(p => p === 'git-upload-pack').length, 0, 'a definite answer reads nothing back');
258 }
259});
261// #246: Artifacts' unpack failure (observed bytes, 2026-11-10) through the real transport: push_rejected with its reason
262test('a host\'s echoed unpack failure (Cloudflare Artifacts\' report bytes) is push_rejected with the host\'s reason', async () => {
263 const { url } = repo();
264 const reason = 'invalid pack entry type 0';
265 const inner = concat([unwrap(pkt(`unpack ${reason}\n`)), unwrap(pkt(`ng ${REF} ${reason}\n`)), FLUSH]);
266 const sideband = concat([unwrap(pkt(concat([Uint8Array.of(1), inner]))), FLUSH]);
267 const answer = () => new Response(sideband, { status: 200, headers: { 'content-type': 'application/x-git-receive-pack-result' } });
268 const fetchImpl = routed(async init => { await drain(init.body); return answer(); });
269 const r = await pushPack(transport(url, { fetchImpl }), { ...create(), pack: streamOf(source.pack).stream, length: source.pack.length });
270 assert.equal(r.ok, false);
271 assert.equal(r.error.code, 'push_rejected', r.error.message);
272 assert.equal(r.error.message, `push rejected: ${reason}`);
273 assert.equal(r.error.report.status, reason);
274});
276test('a stale old oid is cas_conflict, as push reports it', async () => {
277 const { bare, url } = repo();
278 const first = await pushPack(transport(url), { ...create(), pack: streamOf(source.pack).stream });
279 assert.equal(first.ok, true, first.error?.message);
280 const again = await pushPack(transport(url), { ...create(), pack: streamOf(source.pack).stream });
281 assert.equal(again.ok, false);
282 assert.equal(again.error.code, 'cas_conflict', again.error.message);
283 assert.equal(tipOf(bare), source.oid);
284});
286test('a lost response after the body went out is ambiguous, and the unfinished source is cancelled', async () => {
287 const { url } = repo();
288 // the body went out: the transport took its first chunk (the command list) before the connection was lost. A
289 // failure before that first pull is the definite one (test/push-pack-preabort.test.js).
290 let taken = 0;
291 const fetchImpl = routed(async init => { taken += (await init.body.getReader().read()).value.length; throw new TypeError('network gone'); });
292 const { stream, state } = streamOf(source.pack);
293 const r = await pushPack(transport(url, { fetchImpl }), { ...create(), pack: stream });
294 assert.ok(taken > 0);
295 assertAmbiguous(r, 'network');
296 assert.equal(state.cancelled, 1);
297});
299test('a server that answers without reading the pack: its report decides, and the source is cancelled', async () => {
300 const { url } = repo();
301 const { stream, state } = streamOf(source.pack);
302 const r = await pushPack(transport(url, { fetchImpl: routed(async () => reportResponse()) }), { ...create(), pack: stream, sideband: false });
303 assert.equal(r.ok, true, r.error?.message);
304 assert.equal(r.value.packBytes, 0);
305 assert.equal(state.cancelled, 1);
306});
308test('a source that fails to cancel changes nothing', async () => {
309 const { url } = repo();
310 const stream = new ReadableStream({ pull() { return new Promise(() => {}); }, cancel() { throw new Error('cannot cancel'); } }, { highWaterMark: 0 });
311 const r = await pushPack(transport(url, { fetchImpl: routed(async () => reportResponse()) }), { ...create(), pack: stream, sideband: false });
312 assert.equal(r.ok, true, r.error?.message);
313});
315// The reader is the caller's too: its cancel() may throw where it stands, or answer with something that is not a
316// promise. The server reported the push as landed (reportResponse), so the outcome is ok either way: expected from the
317// protocol, not from the code. Bounded by event-loop turns (settledWithin, below), so a defect fails and does not hang.
318for (const [name, answer] of [['throws where it stands', () => { throw new Error('sync cancel'); }], ['returns undefined', () => undefined]]) {
319 test(`a reader whose cancel() ${name} changes nothing`, async () => {
320 const { url } = repo();
321 let cancels = 0;
322 class Odd extends ReadableStream {
323 getReader() { return { read: () => new Promise(() => {}), cancel(why) { cancels++; return answer(why); }, releaseLock() {}, closed: new Promise(() => {}) }; }
324 }
325 const pushed = pushPack(transport(url, { fetchImpl: routed(async () => reportResponse()) }), { ...create(), pack: new Odd(), sideband: false });
326 assert.equal(await settledWithin(pushed), 'settled');
327 const r = await pushed;
328 assert.equal(r.ok, true, r.error?.message);
329 assert.equal(cancels, 1, 'asked to cancel once');
330 });
333// The pack stream is the caller's: its read() and its cancel() may never settle. pushPack's outcome waits for neither;
334// the source is asked to cancel once and not waited for. A hang is proved by a step counter, not a clock: `turns`
335// event-loop turns pass while the promise is still pending (nothing here does I/O: the transport is in process).
336const settledWithin = async (promise, turns = 200) => {
337 let state = 'pending';
338 promise.then(() => { state = 'settled'; }, () => { state = 'settled'; });
339 for (let i = 0; i < turns && state === 'pending'; i++) await new Promise(r => setImmediate(r));
340 return state;
341};
342// `native`: a ReadableStream that serves `serve` bytes, then a pull that never returns, and a cancel() that never
343// settles. `deaf`: a stream whose reader answers nothing at all, not even a read() that was pending when it is cancelled.
344const stuck = (kind, bytes, serve) => {
345 const state = { reads: 0, cancelled: 0 };
346 let reached;
347 const parked = new Promise(r => { reached = r; });
348 const never = () => new Promise(() => {});
349 if (kind === 'native') {
350 const stream = new ReadableStream({
351 pull(c) { if (state.reads++ === 0 && serve > 0) { c.enqueue(bytes.subarray(0, serve)); return undefined; } reached(); return never(); },
352 cancel() { state.cancelled++; return never(); },
353 }, { highWaterMark: 0 });
354 return { stream, state, parked };
355 }
356 class Deaf extends ReadableStream {
357 getReader() { return { read() { state.reads++; reached(); return never(); }, cancel() { state.cancelled++; return never(); }, releaseLock() {}, closed: never() }; }
358 }
359 return { stream: new Deaf(), state, parked };
360};
361// A transport that reads the request body as a server would and never answers. Like fetch it rejects when its signal
362// aborts; unlike undici it does not cancel the body it was given, so only pushPack's own release reaches the source.
363const silent = () => (url, init) => {
364 const reader = init.body.getReader();
365 (async () => { for (;;) { const r = await reader.read(); if (r.done) return; } })().catch(() => null);
366 return new Promise((resolve, reject) => { init.signal.addEventListener('abort', () => { reject(init.signal.reason); }); });
367};
368const packPush = (stream, length) => ({ ...create(), pack: stream, length, sideband: false });
370for (const kind of ['native', 'deaf']) {
371 test(`a source whose read() and cancel() never settle (${kind}): the server's report is the outcome, not waited past`, async () => {
372 const src = stuck(kind, source.pack, 0);
373 const t = transport('https://h/r', { fetchImpl: async () => reportResponse() });
374 const pushed = pushPackChecked(t, packPush(src.stream, null));
375 assert.equal(await settledWithin(pushed), 'settled', 'pushPack with the report in hand');
376 const { result: r } = await pushed;
377 // the server's report is `unpack ok`, `ok refs/heads/main` (reportResponse): by the protocol the push landed
378 assert.equal(r.ok, true, r.error?.message);
379 assert.equal(src.state.cancelled, 1, 'the source is asked to cancel, once');
380 });
382 test(`a source whose read() and cancel() never settle (${kind}): the caller's abort returns ambiguous`, async () => {
383 const src = stuck(kind, source.pack, 1000);
384 const ac = new AbortController();
385 const pushed = pushPackChecked(transport('https://h/r', { fetchImpl: silent(), signal: ac.signal }), packPush(src.stream, source.pack.length));
386 await src.parked;
387 assert.equal(await settledWithin(pushed, 20), 'pending', 'the request is under way, waiting for the source');
388 ac.abort(new Error('caller gave up'));
389 assert.equal(await settledWithin(pushed), 'settled', 'the abort ends it');
390 // request bytes were on the wire and no report came back: what the server did is not known to the client
391 assertAmbiguous((await pushed).result, 'aborted');
392 assert.equal(src.state.cancelled, 1, 'the source is asked to cancel, once');
393 });
395 test(`a source whose read() and cancel() never settle (${kind}): the idle timeout returns ambiguous`, async () => {
396 // the exchange's clock is the fake one: 1000 ms of no bytes pass by advancing it, not by waiting
397 vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'Date'] });
398 try {
399 const src = stuck(kind, source.pack, 1000);
400 const limits = withLimits({ firstByteTimeoutMs: 5000, idleTimeoutMs: 1000 });
401 const pushed = pushPackChecked(transport('https://h/r', { fetchImpl: silent(), limits }), packPush(src.stream, source.pack.length));
402 await src.parked;
403 await vi.advanceTimersByTimeAsync(999);
404 assert.equal(await settledWithin(pushed, 20), 'pending', 'one millisecond short of limits.idleTimeoutMs');
405 await vi.advanceTimersByTimeAsync(1);
406 assert.equal(await settledWithin(pushed), 'settled', 'limits.idleTimeoutMs ends it');
407 const { result: r } = await pushed;
408 assertAmbiguous(r, 'timeout');
409 assert.match(r.error.message, /1000 ms \(limits\.idleTimeoutMs\)/);
410 assert.equal(src.state.cancelled, 1, 'the source is asked to cancel, once');
411 } finally { vi.useRealTimers(); }
412 });
415test('the body cancelled by the transport cancels the source', async () => {
416 const { url } = repo();
417 const fetchImpl = routed(async init => { await init.body.cancel('gone'); return reportResponse(); });
418 const { stream, state } = streamOf(source.pack);
419 const r = await pushPack(transport(url, { fetchImpl }), { ...create(), pack: stream, sideband: false });
420 assert.equal(r.ok, true, r.error?.message);
421 assert.equal(state.cancelled, 1);
422});
424test('atomic: checked against the advertisement, then sent; two refs land together', async () => {
425 const { bare, url } = repo();
426 const { stream } = streamOf(source.pack);
427 const commands = [create(), { ref: 'refs/heads/other', oldOid: ZERO_OID, newOid: source.oid }];
428 const r = await pushPack(transport(url), { commands, pack: stream, length: source.pack.length, atomic: true });
429 assert.equal(r.ok, true, r.error?.message);
430 assert.equal(tipOf(bare), source.oid);
431 assert.equal(git(bare, ['rev-parse', 'refs/heads/other']).trim(), source.oid);
432});
434test('atomic the server does not advertise: unsupported, no POST, the pack unread', async () => {
435 const { url } = repo();
436 const advert = concat([unwrap(pkt('# service=git-receive-pack\n')), FLUSH, unwrap(pkt(`${ZERO_OID} capabilities^{}\0report-status\n`)), FLUSH]);
437 let posts = 0;
438 const fetchImpl = async u => {
439 if (String(u).endsWith('/git-receive-pack')) { posts++; return reportResponse(); }
440 return new Response(advert, { status: 200, headers: { 'content-type': 'application/x-git-receive-pack-advertisement' } });
441 };
442 const { stream, state } = streamOf(source.pack);
443 const r = await pushPack(transport(url, { fetchImpl }), { ...create(), pack: stream, atomic: true });
444 assert.equal(r.ok, false);
445 assert.equal(r.error.code, 'unsupported');
446 assert.equal(posts, 0);
447 assert.equal(state.pulls, 0);
448 assert.equal(stream.locked, false);
449});
451test('push options: refused when not advertised, sent when they are', async () => {
452 const { url } = repo();
453 const { stream } = streamOf(source.pack);
454 const r = await pushPack(transport(url), { ...create(), pack: stream, pushOptions: ['ci.skip'] });
455 assert.equal(r.ok, false);
456 assert.equal(r.error.code, 'unsupported');
457 // the checked form writes the options after the commands' flush
458 const checked = unwrap(checkPushCaps(new Set(['report-status', 'push-options', 'object-format=sha1']), { pushOptions: ['ci.skip'] }));
459 let body = null;
460 const fetchImpl = routed(async init => { const chunks = []; const reader = init.body.getReader(); for (;;) { const c = await reader.read(); if (c.done) break; chunks.push(c.value); } body = concat(chunks); return reportResponse(); });
461 const { result: sent } = await pushPackChecked(transport(url, { fetchImpl }), { ...create(), pack: streamOf(new Uint8Array([1, 2, 3])).stream, checked, sideband: false });
462 assert.equal(sent.ok, true, sent.error?.message);
463 const text = new TextDecoder('latin1').decode(body);
464 assert.ok(text.includes('000cci.skip\n0000'), text.slice(0, 300));
465 assert.ok(body.subarray(-3).every((b, i) => b === i + 1));
466});
468test('refusals with nothing read: a missing, locked or non-stream pack; objects; only deletes; a bad length; a bad ref', async () => {
469 const { url } = repo();
470 const t = transport(url);
471 const stream = () => streamOf(source.pack).stream;
472 const locked = stream(); locked.getReader();
473 const cases = [
474 [{ ...create() }, 'pack'],
475 [{ ...create(), pack: 'bytes' }, 'pack'],
476 [{ ...create(), pack: source.pack }, 'pack'],
477 [{ ...create(), pack: locked }, 'pack'],
478 [{ ...create(), pack: stream(), objects: [] }, 'objects'],
479 [{ ref: REF, oldOid: source.oid, newOid: ZERO_OID, pack: stream() }, 'pack'],
480 ];
481 for (const [opts, what] of cases) {
482 const r = await pushPack(t, opts);
483 assert.equal(r.ok, false);
484 assert.equal(r.error.code, 'bad_request', r.error.message);
485 assert.equal(r.error.what, what);
486 }
487 for (const length of [-1, 1.5, '12', Number.NaN, Number.MAX_SAFE_INTEGER + 1, {}]) {
488 const r = await pushPack(t, { ...create(), pack: stream(), length });
489 assert.equal(r.ok, false, String(length));
490 assert.equal(r.error.code, 'bad_request', String(length));
491 assert.equal(r.error.what, 'length');
492 }
493 const badRef = await pushPack(t, { ref: 'not a ref', oldOid: ZERO_OID, newOid: source.oid, pack: stream() });
494 assert.equal(badRef.error.code, 'bad_request');
495 const { result: noWants } = await pushPackChecked(t, { ...create(), pack: stream(), atomic: true });
496 assert.equal(noWants.error.code, 'bad_request', 'unchecked atomic is refused, not dropped');
497});
499test('planPackPush: length 0 and null are lengths; the cap is on the pack', () => {
500 const pack = () => streamOf(source.pack).stream;
501 const zero = unwrap(planPackPush({ ...create(), pack: pack(), length: 0 }, withLimits({ maxPackBytes: 0 })));
502 assert.equal(zero.length, 0);
503 assert.equal(unwrap(planPackPush({ ...create(), pack: pack() }, null)).length, null);
504 assert.equal(unwrap(planPackPush({ ...create(), pack: pack(), length: null }, null)).length, null);
505 assert.equal(planPackPush({ ...create(), pack: pack(), length: 2 }, withLimits({ maxPackBytes: 1 })).error.code, 'limit_exceeded');
506});
508test('memory: a pack far larger than held memory holds about two chunks', async () => {
509 const chunk = 1 << 20;
510 const chunks = 256; // 256 MiB
511 const out = await new Promise((resolve, reject) => {
512 const child = spawn(process.execPath, [...MEMORY_CELL_FLAGS, '--import', './test/ts-resolve.mjs', 'test/push-pack-child.mjs', String(chunk), String(chunks)], { stdio: ['ignore', 'pipe', 'inherit'] });
513 let text = '';
514 child.stdout.on('data', d => { text += d; });
515 child.on('error', reject);
516 child.on('close', code => (code === 0 ? resolve(JSON.parse(text)) : reject(new Error(`child exited ${code}`))));
517 });
518 assert.equal(out.ok, true, out.error);
519 assert.equal(out.packBytes, chunk * chunks);
520 assert.equal(out.bytesSeen > chunk * chunks, true);
521 console.log(JSON.stringify(out));
522 // Two chunks: the one being written and the one the source made next (2,097,288 B on node 24, 2,097,292 B on node 22).
523 // Before #249 node 22 read 11.5 MiB here: the backing stores of dropped chunks still counted until V8's background
524 // sweep reached them, which MEMORY_CELL_FLAGS now does with the gc. A third chunk is a copy or a tee.
525 assert.ok(out.samples >= 10, JSON.stringify(out));
526 assert.ok(out.liveDelta < 3 * chunk, JSON.stringify(out));
527}, 180_000);
529// A transport that keeps the request body of a push that failed unsent, and pulls it afterwards. Expected values from
530// the README (pushPack): a `fetchImpl` that rejects without reading the body is that failure itself (a rejected
531// fetch() is a network error), the server received no command list, and the stream stays unread and unlocked: the
532// caller still owns it. So the late pull must hand over nothing (not the command list, no byte of the pack): the read
533// rejects, as ply's own fault-free refusal of a use outside the contract ('internal'), and the caller's stream is
534// still unpulled, uncancelled, unlocked and readable from its first byte; git shows no ref.
535test('a transport that rejects without reading the body and pulls it afterwards is handed nothing; the pack is still the caller\'s', async () => {
536 const { url, bare } = repo();
537 let kept = null;
538 const fetchImpl = routed(async init => { kept = init.body; throw new TypeError('fetch failed'); });
539 const { stream, state } = streamOf(source.pack);
540 const r = await pushPack(transport(url, { fetchImpl }), { ...create(), pack: stream, length: source.pack.length });
541 assert.equal(r.ok, false);
542 assert.equal(r.error.code, 'network', r.error.message);
543 assert.deepEqual({ ...state, locked: stream.locked }, { pulls: 0, cancelled: 0, locked: false });
544 const late = await kept.getReader().read().then(chunk => ({ chunk }), error => ({ error }));
545 assert.equal('chunk' in late, false, 'the late pull was handed a chunk');
546 assert.equal(late.error.code, 'internal');
547 assert.deepEqual({ ...state, locked: stream.locked }, { pulls: 0, cancelled: 0, locked: false });
548 assert.equal(tipOf(bare), null);
549 const first = await stream.getReader().read();
550 assert.deepEqual([...first.value], [...source.pack.slice(0, 4096)]);
551});
553// #272: the outcome says whether receive-pack got a request. README (pushPack, Resumable transfer): a refusal, a fired
554// signal and a fetch that rejects before pulling the body send nothing; from the first pull the request went out.
555test('pushPackChecked says whether the request was sent: not for a refusal, a fired signal or an unpulled body; yes from the first pull', async () => {
556 const { url } = repo();
557 const stream = () => streamOf(source.pack).stream;
558 const unpulled = routed(async () => { throw new TypeError('connect failed'); });
559 const pulled = routed(async init => { await init.body.getReader().read(); throw new TypeError('network gone'); });
560 const fired = new AbortController(); fired.abort();
561 const cases = [
562 ['refused', transport(url), { ...create(), pack: stream(), length: -1 }, 'no', 'bad_request'],
563 ['signal fired', transport(url, { signal: fired.signal }), { ...create(), pack: stream() }, 'no', 'aborted'],
564 ['body never pulled', transport(url, { fetchImpl: unpulled }), { ...create(), pack: stream() }, 'no', 'network'],
565 ['lost after the first pull', transport(url, { fetchImpl: pulled }), { ...create(), pack: stream() }, 'yes', 'ambiguous'],
566 ['landed', transport(url), { ...create(), pack: stream() }, 'yes', null],
567 ];
568 for (const [what, t, opts, sent, code] of cases) {
569 const o = await pushPackChecked(t, { ...opts, sideband: false });
570 assert.equal(o.sent, sent, what);
571 assert.equal(o.result.ok ? null : o.result.error.code, code, what);
572 }
573});
575// #272: a failed pushPack's error is its git.receive-pack span's error (OTel semconv: status ERROR = code 2, OK = 1;
576// error.type, an 'exception' event), whether or not the body was pulled; a landed push's span is not an error.
577test('pushPackChecked records the outcome\'s error on its git.receive-pack span, sent or not', async () => {
578 const { url } = repo();
579 const unpulled = routed(async () => { throw new TypeError('connect failed'); });
580 const pulled = routed(async init => { await init.body.getReader().read(); throw new TypeError('network gone'); });
581 const cases = [
582 ['body never pulled', unpulled, 'network'],
583 ['lost after the first pull', pulled, 'ambiguous'],
584 ['landed', undefined, null],
585 ];
586 for (const [what, fetchImpl, code] of cases) {
587 const ex = arrayExporter();
588 const o = await pushPackChecked(transport(url, { fetchImpl, tracer: new Tracer({ exporter: ex }) }), { ...create(), pack: streamOf(source.pack).stream, sideband: false });
589 assert.equal(o.result.ok ? null : o.result.error.code, code, what);
590 const spans = ex.lines.flatMap(flattenLine).filter(s => s.name === 'git.receive-pack');
591 assert.equal(spans.length, 1, what);
592 const [span] = spans;
593 const exceptions = span.events.filter(e => e.name === 'exception');
594 assert.deepEqual(
595 { status: span.status.code, type: span.attrs['error.type'] ?? null, exceptions: exceptions.length },
596 code === null ? { status: 1, type: null, exceptions: 0 } : { status: 2, type: code, exceptions: 1 },
597 what,
598 );
599 }
600});
602// #272: a raise (a tracer or transport the caller handed in that throws) is 'protocol' and counts as sent: nothing says
603// the body was not pulled, so runTransfer settles it rather than treat a request that may have reached git as unsent.
604test('pushPackChecked: a raise is protocol and sent, before the first pull or after it', async () => {
605 const { url } = repo();
606 const raising = when => {
607 const tracer = new Tracer();
608 const start = tracer.start.bind(tracer);
609 tracer.start = (name, options) => {
610 if (when === 'start' && name === 'git.receive-pack') throw new TypeError('tracer raised');
611 const span = start(name, options);
612 if (when === 'set') { const set = span.set.bind(span); span.set = attrs => { if ('git.push.pack.bytes' in attrs) throw new TypeError('span raised'); return set(attrs); }; }
613 return span;
614 };
615 return tracer;
616 };
617 for (const when of ['start', 'set']) {
618 const o = await pushPackChecked(transport(url, { tracer: raising(when) }), { ...create(), pack: streamOf(source.pack).stream, sideband: false });
619 assert.deepEqual({ sent: o.sent, code: o.result.ok ? null : o.result.error.code }, { sent: 'yes', code: 'protocol' }, when);
620 }
621});
Diagnostics4 git calls · 248 ms
CallStatusSizeTime0248 ms
discover200152 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=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 B5 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=UPDATING
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
fetch2009.2 KiB240 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=240
git.response.bytes=9427
git.response.cache=MISS
git.response.duration_ms=240
git.fetch.wants=6497176a207b8e026c58773be8d30713857c65ad
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=9427
git.pkt.payload.max=9406
git.sideband.band1.bytes=9405
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
a5e28d7b4ec124d3
Request
d4da99bc…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format