main
ply4 git calls27 ms43 KiB3 cachedprotocol v2
172 lines9.3 KiBRaw
1// Mirror sync (src/mirror.ts) against real git: a local http-backend plays both GitHub (the source) and the Artifacts
2// host (the mirror). Checked: the first sync, in-sync, an incremental update that sends only the new objects, a
3// rewritten upstream, the write token's scope / TTL / revocation, and that no token appears in a result.
4import { test } from 'node:test';
5import assert from 'node:assert/strict';
6import { mkdirSync, writeFileSync } from 'node:fs';
7import { join } from 'node:path';
8import { git, makeBareRepo, startGitServer, tmpRoot, commitFiles } from './server.js';
9import { syncMirror, WRITE_TTL_S, type Mirror } from '../src/mirror.ts';
10import type { ArtifactsNamespace, TokenSource } from '../src/public.ts';
12/** Null and a missing value take the fallback. An empty string and 0 stay. */
13function fill<T>(value: unknown, fallback: T): T {
14 return value === null || value === void 0 ? fallback : value as T;
17/** One property. A null receiver and a missing key are holes. A stored null stays null. */
18function slot(receiver: unknown, key: string): unknown {
19 if (receiver === null || receiver === void 0) return void 0;
20 if (typeof receiver !== 'object' && typeof receiver !== 'function') return void 0;
21 return (receiver as Record<string, unknown>)[key];
24const READ = `art_v1_${'re4d'.repeat(10)}?expires=4102444800`;
25const WRITE = `art_v1_${'wr17'.repeat(10)}?expires=4102444800`;
27test('syncMirror: first sync, in-sync, incremental, rewritten upstream; a 60 s write token, revoked, never in the result', async () => {
28 const { root, cleanup } = tmpRoot();
29 const srv = await startGitServer(root);
30 try {
31 const source = makeBareRepo(root, 'src');
32 mkdirSync(join(root, 'git', 'public-v0'), { recursive: true });
33 const mirrorBare = makeBareRepo(root, 'git/public-v0/ply');
34 const work = join(root, 'w-src');
35 mkdirSync(work);
36 git(work, ['init', '-q', '-b', 'main']);
37 const c1 = commitFiles(work, source, { 'README.md': 'one\n', 'src/a.js': 'a\n' }, 'one');
38 const mints: { scope: string; ttl: number }[] = [];
39 const revoked: string[] = [];
40 const binding: ArtifactsNamespace = {
41 async get() {
42 return Object.assign({ async createToken(scope: 'read' | 'write', ttl: number) { mints.push({ scope, ttl }); return { plaintext: WRITE }; } },
43 { async revokeToken(t: string) { revoked.push(t); return true; } });
44 },
45 };
46 const tokens: TokenSource = { readToken: async () => READ, secrets: () => [READ] };
47 const auths: string[] = [];
48 // github.com/ivarvong/ply.git → the local source repo; the mirror origin is the local server itself
49 const fetchImpl: typeof globalThis.fetch = async (input, init) => {
50 auths.push(fill(new Headers(slot(init, 'headers') as HeadersInit).get('authorization'), ''));
51 const u = String(input).replace('https://github.com/ivarvong/ply.git', `${srv.url}/src.git`);
52 return fetch(u, init);
53 };
54 const m: Mirror = { repo: 'ply', source: 'https://github.com/ivarvong/ply.git', branch: 'main' };
55 const deps = { binding, tokens, fetchImpl, origin: srv.url };
56 const mirrorTip = () => git(mirrorBare, ['rev-parse', 'refs/heads/main']).trim();
58 const first = await syncMirror(m, deps);
59 assert.equal(first.outcome, 'updated', JSON.stringify(first));
60 assert.equal(first.from, null);
61 assert.equal(first.to, c1);
62 assert.equal(mirrorTip(), c1);
63 git(mirrorBare, ['fsck', '--strict', '--no-dangling']);
65 const again = await syncMirror(m, deps);
66 assert.equal(again.outcome, 'in-sync');
67 assert.equal(mints.length, 1, 'no write token when nothing changed');
69 git(work, ['-c', 'user.name=seed', '-c', 'user.email=seed@x', 'commit', '--allow-empty', '-q', '-m', 'two']);
70 git(work, ['push', '-q', source, 'HEAD:refs/heads/main']);
71 const c2 = git(work, ['rev-parse', 'HEAD']).trim();
72 const inc = await syncMirror(m, deps);
73 assert.equal(inc.outcome, 'updated');
74 assert.equal(inc.from, c1);
75 assert.equal(inc.to, c2);
76 assert.equal(inc.objects, 1, 'only the new commit (its tree is already there)');
77 assert.equal(mirrorTip(), c2);
79 // upstream rewrites history (force push): followed, with a CAS from the mirror's tip
80 git(work, ['reset', '-q', '--hard', c1]);
81 writeFileSync(join(work, 'README.md'), 'rewritten\n');
82 git(work, ['add', '-A']);
83 git(work, ['-c', 'user.name=seed', '-c', 'user.email=seed@x', 'commit', '-q', '-m', 'rewritten']);
84 git(work, ['push', '-q', '-f', source, 'HEAD:refs/heads/main']);
85 const c3 = git(work, ['rev-parse', 'HEAD']).trim();
86 const rw = await syncMirror(m, deps);
87 assert.equal(rw.outcome, 'updated', JSON.stringify(rw));
88 assert.equal(mirrorTip(), c3);
89 git(mirrorBare, ['fsck', '--strict', '--no-dangling']);
91 assert.deepEqual(mints.map(x => x.scope), ['write', 'write', 'write']);
92 assert.ok(mints.every(x => x.ttl === WRITE_TTL_S && WRITE_TTL_S === 60));
93 assert.deepEqual(revoked, [WRITE, WRITE, WRITE], 'every write token is revoked after its push');
94 // the source is read anonymously; the mirror with the read token (ls-refs) and the write token (receive-pack)
95 assert.ok(auths.includes(''));
96 assert.ok(auths.includes(`Bearer ${READ}`) && auths.includes(`Bearer ${WRITE}`));
97 for (const r of [first, again, inc, rw]) assert.equal(JSON.stringify(r).includes('art_v1_'), false);
99 // a missing source branch is an error result, not a throw, and pushes nothing
100 const missing = await syncMirror({ ...m, branch: 'nope' }, deps);
101 assert.equal(missing.outcome, 'error');
102 assert.equal(mirrorTip(), c3);
103 } finally {
104 await srv.close();
105 cleanup();
106 }
107});
109// A pack past ply's held size (INFLATE_CHUNK, 32 KiB) is sent as a ReadableStream (ply#172's streamed push): the guarded
110// fetch must forward it, not refuse it as unreplayable. 64 KiB of random bytes deflates to more than 32 KiB.
111test('syncMirror: a pack larger than 32 KiB is streamed through the guarded fetch', async () => {
112 const { root, cleanup } = tmpRoot();
113 const srv = await startGitServer(root);
114 try {
115 const source = makeBareRepo(root, 'src');
116 mkdirSync(join(root, 'git', 'public-v0'), { recursive: true });
117 const mirrorBare = makeBareRepo(root, 'git/public-v0/ply');
118 const work = join(root, 'w-src');
119 mkdirSync(work);
120 git(work, ['init', '-q', '-b', 'main']);
121 const big = Buffer.from(crypto.getRandomValues(new Uint8Array(64 * 1024))).toString('base64');
122 const c1 = commitFiles(work, source, { 'big.txt': big }, 'big');
123 const binding: ArtifactsNamespace = {
124 async get() {
125 return Object.assign({ async createToken() { return { plaintext: WRITE }; } }, { async revokeToken() { return true; } });
126 },
127 };
128 const tokens: TokenSource = { readToken: async () => READ, secrets: () => [READ] };
129 const fetchImpl: typeof globalThis.fetch = async (input, init) => fetch(String(input).replace('https://github.com/ivarvong/ply.git', `${srv.url}/src.git`), init);
130 const r = await syncMirror({ repo: 'ply', source: 'https://github.com/ivarvong/ply.git', branch: 'main' }, { binding, tokens, fetchImpl, origin: srv.url });
131 assert.equal(r.outcome, 'updated', JSON.stringify(r));
132 assert.equal(git(mirrorBare, ['rev-parse', 'refs/heads/main']).trim(), c1);
133 git(mirrorBare, ['fsck', '--strict', '--no-dangling']);
134 } finally {
135 await srv.close();
136 cleanup();
137 }
138});
140// 2026-11-06: every mirror sync failed for ~6 h with `push: an ofs base must name an earlier object`. A resolved fetch
141// keeps each object's wire base ({ tag: 'ofs', offset } in the SOURCE pack); push reads `base` as its own input
142// ({ tag: 'ofs', index } into the objects it is given) and refused it. A source whose pack carries ofs deltas reproduces it.
143test('syncMirror: a source pack with ofs deltas is pushed (whole objects, not the fetched wire bases)', async () => {
144 const { root, cleanup } = tmpRoot();
145 const srv = await startGitServer(root);
146 try {
147 const source = makeBareRepo(root, 'src');
148 mkdirSync(join(root, 'git', 'public-v0'), { recursive: true });
149 const mirrorBare = makeBareRepo(root, 'git/public-v0/ply');
150 const work = join(root, 'w-src');
151 mkdirSync(work);
152 git(work, ['init', '-q', '-b', 'main']);
153 const text = Array.from({ length: 400 }, (_, i) => `line ${i} of a file that deltas well\n`).join('');
154 commitFiles(work, source, { 'a.txt': text }, 'one');
155 const c2 = commitFiles(work, source, { 'a.txt': text.replace('line 7 ', 'line seven ') }, 'two');
156 git(source, ['repack', '-q', '-a', '-d', '-f', '--window=10']);
157 const binding: ArtifactsNamespace = {
158 async get() {
159 return Object.assign({ async createToken() { return { plaintext: WRITE }; } }, { async revokeToken() { return true; } });
160 },
161 };
162 const tokens: TokenSource = { readToken: async () => READ, secrets: () => [READ] };
163 const fetchImpl: typeof globalThis.fetch = async (input, init) => fetch(String(input).replace('https://github.com/ivarvong/ply.git', `${srv.url}/src.git`), init);
164 const r = await syncMirror({ repo: 'ply', source: 'https://github.com/ivarvong/ply.git', branch: 'main' }, { binding, tokens, fetchImpl, origin: srv.url });
165 assert.equal(r.outcome, 'updated', JSON.stringify(r));
166 assert.equal(git(mirrorBare, ['rev-parse', 'refs/heads/main']).trim(), c2);
167 git(mirrorBare, ['fsck', '--strict', '--no-dangling']);
168 } finally {
169 await srv.close();
170 cleanup();
171 }
172});
Diagnostics4 git calls · 27 ms
CallStatusSizeTime027 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 · hit3 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 1020040 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=41349
git.response.cache=HIT
git.response.duration_ms=2
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=526b305930b8ef382f2d7a1f9c0238730a585921
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=41349
git.pkt.payload.max=41254
git.sideband.band1.bytes=41253
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=61
git.pack.bytes=41253
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=ab60efef4b7243678c75de64a4386986ee885c1c
git.pack.full=61
Open pack map
fetch2002.8 KiB · hit22 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=22
git.response.bytes=2881
git.response.cache=HIT
git.response.duration_ms=22
git.fetch.wants=58056444eda7a26aaaf7779ac322f90806155552
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=2881
git.pkt.payload.max=2860
git.sideband.band1.bytes=2859
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-web.git
Ref
refs/heads/main
Commit
526b305930b8ef382f2d7a1f9c0238730a585921
cf-ray
a5e2eb7a0d694bc2
Request
ff9dbf48…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format