main
ply4 git calls42 ms52 KiB2 cachedprotocol v2
533 lines30 KiBRaw
1// Workers Cache for public git reads (never the Cache API). The default entrypoint has caching off and always runs: rate
2// limit, logs, scrubbing, traces. For an upload-pack exchange whose answer is public, it calls the cache-enabled
3// `GitCache` entrypoint over ctx.exports instead of the upstream; GitCache performs the upstream request and returns it
4// with a Cache-Control decided by what the exchange is:
5//
6// pack POST git-upload-pack, command=fetch, wants only (no have / shallow / want-ref / deepen-since / deepen-not):
7// a pure function of (url, body). Objects are named by their hash, so public, max-age=31536000, immutable.
8// refs POST git-upload-pack, command=ls-refs: refs move. public, max-age=REFS_MAX_AGE_S, swr=REFS_SWR_S
9// wrefs ls-refs of a public-namespace repo when the watched policy is on (createLoopbackFetch `watchedRefs`): stored
10// WATCHED_REFS_CACHE (minutes) and tagged refsTag(repo) when RefsWatch grants a lease, else the refs policy.
11// RefsWatch purges the tag when the repo's refs move (src/refs-watch.ts). Its own key kind, so a version
12// without the policy never reads a long-lived answer, and one with it never reads a short one.
13// discover GET info/refs?service=git-upload-pack: a v2 answer is the capability list (no refs), so it is long-lived;
14// a v0 answer carries the refs, so it gets the refs policy.
15//
16// What is public, and so may be cached (`cacheableUpstream`): the request carries no Authorization (a caller's Git-Token
17// or Git-Authorization always bypasses the cache), and the URL is either exactly a public-namespace repo endpoint
18// (publicUploadPackRepo) or an upload-pack endpoint on one of the viewer's built-in allowlisted forges.
19//
20// Key: /v<CACHE_VERSION>/<kind>/<sha256(kind, url, git-protocol, body)>. The upstream request rides in headers, which are
21// not part of a Workers Cache key; GitCache recomputes the hash from them and refuses a mismatch, so no request can
22// store an answer under another request's key. crossVersionCache is on, so CACHE_VERSION is the only partition: bump it
23// when what GitCache stores changes.
24//
25// ply's retry after a corrupt cached pack sends `x-ply-cache: bust`; the loopback then purges that key (GitCache, which
26// owns it) and fetches again.
27import { captureGroup, chainGet, dig, fromNullable, header } from './absent.ts';
28import { CACHE_VERSION } from './cache-version.ts';
29import { err, httpReply, isUnexpected, ok, responseOf, runRequest, type HttpReply, type Result, type UnexpectedFailure } from './http/machine.ts';
30import { unwrapOr } from './option.ts';
31import { extractRequestPath, rawPathHasUnsafeSegment } from './pathguard.ts';
32import { parseCfRay } from './rays.ts';
33import { isPublicRepoName, publicUploadPackRepo, scrubSecrets, type TokenSource, withPublicReadCredential, type FetchLike } from './public.ts';
34import { ALLOWED_HOSTS } from './viewer/url.ts';
35import { createTransport, fetchPack, Tracer } from 'ply';
36import { LEASE_TIMEOUT_MS, MAX_PURGE_TAGS, REFS_FILL_TIMEOUT_MS, WATCH_NAME, WATCHED_REFS_CACHE, parseLsRefsAnswer, refsTag, type LeaseAnswer } from './refs-watch.ts';
37import { VERIFY_CAPS, VERIFY_LIMITS } from './limits.ts';
39export type CacheKind = 'pack' | 'refs' | 'wrefs' | 'discover';
41export const REFS_MAX_AGE_S = 5;
42export const REFS_SWR_S = 10;
43export const IMMUTABLE = 'public, max-age=31536000, immutable';
44export const REFS_CACHE = `public, max-age=${REFS_MAX_AGE_S}, stale-while-revalidate=${REFS_SWR_S}`;
45export const DISCOVER_V2_CACHE = 'public, max-age=300, stale-while-revalidate=3600';
46export const NO_STORE = 'no-store';
47/**
48 * A server's refusal of a partial-clone filter (Artifacts: `400 … only blob:none is supported`) is a fact about that
49 * server, the same for every caller. Uncached, every fresh isolate paid it upstream, and Workers Caching does not
50 * collapse an uncacheable answer: 11 of 13 GitCache invocations in a burst of 20 concurrent cold requests were this 400
51 * (e021776d, 2026-10-31T02:01:45Z). Fresh for an hour, so a server that adds the filter is seen within the hour.
52 * Stale for a day after that, revalidated in the background: an expired refusal sent the first page of every new
53 * isolate on a quiet colo upstream for 1.0-1.6 s (read-token mint + the 400) before its blob:none retry (4 of 14
54 * first contacts in run sparse-ply, 2a4741c0; trace d149bcd8… 1647 ms MISS, c352756d… 1483 ms EXPIRED). A stale
55 * refusal costs at most one extra fallback if the server has since added the filter.
56 */
57export const REFUSAL_CACHE = 'public, max-age=3600, stale-while-revalidate=86400';
58// The text ply recognises as a filter refusal (fetch.js isFilterRejection).
59const FILTER_REFUSAL = /only blob:none is supported|unsupported (git )?fetch filter|filter .* not (supported|allowed)/i;
61/** The request body travels base64 in a header; a larger body (many wants) is not routed through the cache. */
62export const MAX_ROUTED_BODY = 4096;
63const MAX_ERROR_BODY = 64 * 1024;
64/** A pack is stored only after GitCache has verified it, and it buffers the pack to do so; bigger ones pass through unstored. */
65export const MAX_VERIFIED_PACK = VERIFY_CAPS.maxPackBytes;
67const wantsOf = (text: string) => [...text.matchAll(/[0-9a-f]{4}want ([0-9a-f]{40})\n/g)].map(m => m[1] as string);
69/**
70 * Verify a buffered upload-pack fetch answer with ply's own parser: frame it, parse the pack, resolve every delta and
71 * hash every object, and require every wanted oid among the results. ply's streamed blob read (openBlob) has no
72 * retry-past-the-cache, so an unverified pack must never be stored as immutable.
73 */
74export async function verifyPackResponse(url: string, body: Uint8Array, bytes: Uint8Array, contentType: string): Promise<{ ok: true; objects: number } | { ok: false; reason: string }> {
75 const text = latin1(body);
76 const wants = wantsOf(text);
77 if (!wants.length) return { ok: false, reason: 'no wants' };
78 const deepen = captureGroup(/[0-9a-f]{4}deepen (\d+)\n/.exec(text), 1);
79 const filter = captureGroup(/[0-9a-f]{4}filter ([^\n]+)\n/.exec(text), 1);
80 const replay = async () => new Response(bytes.slice(), { status: 200, headers: { 'content-type': contentType } });
81 try {
82 const made = createTransport(url, { tracer: new Tracer({ exporter: () => {}, maxEvents: 0, resource: {} }), fetchImpl: replay, userAgent: 'ply-gitcache', limits: VERIFY_LIMITS, signal: AbortSignal.timeout(30_000), quirks: {} });
83 if (!made.ok) return { ok: false, reason: failureText(made.error).slice(0, 120) };
84 const r = await fetchPack(made.value, { wants, ...(deepen.tag === 'some' ? { deepen: Number(deepen.value) } : {}), ...(filter.tag === 'some' ? { filter: filter.value } : {}), ofsDelta: true }, { resolve: true });
85 if (!r.ok) return { ok: false, reason: failureText(r.error).slice(0, 120) };
86 const objects = fromNullable(r.value.objects);
87 if (objects.tag === 'none') return { ok: false, reason: 'no pack in the answer' };
88 const missing = wants.filter(w => !objects.value.has(w));
89 return missing.length ? { ok: false, reason: `wanted ${missing.length} oid(s) not in the pack` } : { ok: true, objects: objects.value.size };
90 } catch (e) {
91 // A replayed body that errors mid-stream, or a ply invariant: still a pack that is not stored.
92 return { ok: false, reason: failureText(e).slice(0, 120) };
93 }
96function failureText(error: unknown): string {
97 const code = chainGet(error, 'code');
98 if (code.tag === 'some') return String(code.value);
99 const message = chainGet(error, 'message');
100 if (message.tag === 'some') return String(message.value);
101 return String(error);
104const H_URL = 'x-ply-gc-url';
105const H_BODY = 'x-ply-gc-body';
106const H_PROTO = 'x-ply-gc-proto';
107const H_UA = 'x-ply-gc-ua';
108/** On a refs or wrefs answer: when GitCache stored it (ms), so a hit's age is known. */
109export const H_FILLED = 'x-ply-gc-filled';
110/** On a wrefs answer: `watched;<reason>` or `short;<reason>` (the lease answer). */
111export const H_REFS_POLICY = 'x-ply-gc-refs';
112/** What the loopback reports for a refs exchange: `<policy>;age=<s>`. observeUpstream puts it on the span. */
113export const REFS_STATE_HEADER = 'x-ply-refs';
114/** Set by GitCache on its upstream request, so tests can tell a cached path from a direct one. Carries no secret. */
115export const VIA_HEADER = 'x-ply-via';
117const BUILTIN = new Set<string>(ALLOWED_HOSTS);
119function forgeUploadPack(method: string, raw: string): boolean {
120 if (typeof raw !== 'string' || raw.length > 2048 || raw.includes('\\') || raw.includes('#')) return false;
121 if (rawPathHasUnsafeSegment(extractRequestPath(raw))) return false;
122 let u: URL;
123 try { u = new URL(raw); } catch { return false; }
124 if (u.protocol !== 'https:' || u.port !== '' || u.username || u.password || !BUILTIN.has(u.hostname)) return false;
125 if (!raw.startsWith(`https://${u.hostname}/`)) return false;
126 const verb = method.toUpperCase();
127 if (u.pathname.endsWith('/info/refs')) return verb === 'GET' && u.search === '?service=git-upload-pack';
128 return u.pathname.endsWith('/git-upload-pack') && verb === 'POST' && u.search === '';
131/** Is (method, url) an upload-pack endpoint whose anonymous answer is public? (Authorization is checked by the caller.) */
132export function cacheableUpstream(method: string, url: string): boolean {
133 return publicUploadPackRepo(method, url) !== null || forgeUploadPack(method, url);
136// One char per byte, in chunks (a routed body is at most MAX_ROUTED_BODY): the old per-byte `s +=` built a rope of 4096
137// one-char strings per call, and the loopback built it twice per exchange (classify, then base64).
138const latin1 = (b: Uint8Array) => {
139 let s = '';
140 for (let i = 0; i < b.length; i += 1024) s += String.fromCharCode.apply(null, b.subarray(i, i + 1024) as unknown as number[]);
141 return s;
142};
144// pkt-lines: 4 hex length, then the payload; anchor to the length prefix so text inside a payload can't match.
145const LS_REFS = /[0-9a-f]{4}command=ls-refs\n/;
146const FETCH = /[0-9a-f]{4}command=fetch\n/;
147const NOT_PURE = /[0-9a-f]{4}(have [0-9a-f]{40}\n|want-ref |deepen-(since|not) |shallow [0-9a-f]{40}\n)/;
148const WANT = /[0-9a-f]{4}want [0-9a-f]{40}\n/;
150/** classify on a body already read as latin1 text (`null`: no body). */
151function classifyText(method: string, url: string, t: string | null): CacheKind | null {
152 if (method.toUpperCase() === 'GET') return url.endsWith('/info/refs?service=git-upload-pack') && t === null ? 'discover' : null;
153 if (t === null) return null;
154 if (LS_REFS.test(t)) return 'refs';
155 if (!FETCH.test(t)) return null;
156 if (NOT_PURE.test(t)) return null;
157 return WANT.test(t) ? 'pack' : null;
160/** Which cache policy an upload-pack exchange gets, or null when it is not a pure function of its request. */
161export function classify(method: string, url: string, body: Uint8Array | null): CacheKind | null {
162 return classifyText(method, url, body === null ? null : latin1(body));
165function fromBase64(s: string): Uint8Array { const bin = atob(s); const out = new Uint8Array(bin.length); for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i); return out; }
167const HEX = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));
168const ENCODER = new TextEncoder();
170export async function keyHash(kind: CacheKind, url: string, gitProtocol: string, body: Uint8Array | null): Promise<string> {
171 const head = `ply-gitcache\n${kind}\n${url}\n${gitProtocol}\n`;
172 // UTF-8 is at most 3 bytes per UTF-16 unit: one buffer for head and body, no second copy.
173 const all = new Uint8Array(head.length * 3 + (body === null ? 0 : body.length));
174 const { written } = ENCODER.encodeInto(head, all);
175 if (body) all.set(body, written);
176 const d = new Uint8Array(await crypto.subtle.digest('SHA-256', all.subarray(0, written + (body === null ? 0 : body.length))));
177 let hex = '';
178 for (let i = 0; i < d.length; i++) hex += HEX[d[i] as number];
179 return hex;
182export function keyPath(kind: CacheKind, hash: string, version: number = CACHE_VERSION): string {
183 return `/v${version}/${kind}/${hash}`;
186export type LoopbackBinding = { fetch(req: Request): Promise<Response> };
188export function isLoopback(value: unknown): value is LoopbackBinding {
189 return typeof value === 'object' && value !== null && 'fetch' in value;
192function requestUrl(input: RequestInfo | URL): string {
193 if (typeof input === 'string') return input;
194 if (input instanceof URL) return input.href;
195 return input.url;
198/**
199 * The fetch that sits under the redirect guards (createGuardedFetch, createViewerFetch). A public, cacheable exchange
200 * with no Authorization goes to GitCache; everything else goes to `direct` unchanged. `loopback` is null when the
201 * cache is off (PACK_CACHE=off) or there is no ctx.exports (node tests).
202 */
203export type LoopbackOptions = { readonly watchedRefs: boolean };
205/** The key kind of an exchange: a public-namespace ls-refs is `wrefs` when the watched policy is on. */
206export function routedKind(kind: CacheKind | null, method: string, url: string, options: LoopbackOptions): CacheKind | null {
207 if (kind === 'refs' && options.watchedRefs && publicUploadPackRepo(method, url) !== null) return 'wrefs';
208 return kind;
211/** `<policy>;age=<s>` for a refs answer: the policy GitCache stored it under and how long ago. */
212export function refsState(kind: CacheKind, headers: Headers, now: number): string | null {
213 if (kind !== 'refs' && kind !== 'wrefs') return null;
214 const filled = Number(headers.get(H_FILLED));
215 const age = Number.isFinite(filled) && filled > 0 ? Math.max(0, Math.round((now - filled) / 1000)) : -1;
216 const policy = kind === 'refs' ? 'short;ttl' : unwrapOr(header(headers, H_REFS_POLICY), 'none');
217 return `${policy};age=${age}`;
220export function createLoopbackFetch(direct: FetchLike, loopback: (() => LoopbackBinding | null) | null, options: LoopbackOptions = { watchedRefs: false }): FetchLike {
221 return async (input, init) => {
222 if (loopback === null) return direct(input, init);
223 const binding = loopback();
224 if (binding === null) return direct(input, init);
225 const url = requestUrl(input);
226 const methodField = chainGet(init, 'method');
227 const method = methodField.tag === 'some' && typeof methodField.value === 'string' ? methodField.value : 'GET';
228 const headerField = chainGet(init, 'headers');
229 // ply hands a Headers; read it in place rather than copying it per exchange.
230 const headers = headerField.tag === 'none' ? new Headers() : headerField.value instanceof Headers ? headerField.value : new Headers(headerField.value as HeadersInit);
231 if (headers.has('authorization')) return direct(input, init);
232 if (!cacheableUpstream(method, url)) return direct(input, init);
233 const bodyField = chainGet(init, 'body');
234 const body = bodyField.tag === 'some' && bodyField.value instanceof Uint8Array ? bodyField.value : null;
235 if (bodyField.tag === 'some' && bodyField.value && body === null) return direct(input, init);
236 if (body && body.length > MAX_ROUTED_BODY) return direct(input, init);
237 const text = body === null ? null : latin1(body);
238 const kind = routedKind(classifyText(method, url, text), method, url, options);
239 if (!kind) return direct(input, init);
240 const proto = unwrapOr(header(headers, 'git-protocol'), '');
241 const hash = await keyHash(kind, url, proto, body);
242 const carry = new Headers({ [H_URL]: url, [H_PROTO]: proto, [H_UA]: unwrapOr(header(headers, 'user-agent'), '') });
243 if (text !== null) carry.set(H_BODY, btoa(text));
244 const signalField = chainGet(init, 'signal');
245 const carried = { headers: carry, redirect: 'manual' as const, ...(signalField.tag === 'some' ? { signal: signalField.value as AbortSignal } : {}) };
246 if (headers.get('x-ply-cache') === 'bust') {
247 const purged = await binding.fetch(new Request(`https://gitcache.internal/v${CACHE_VERSION}/bust/${kind}/${hash}`, carried));
248 const purgedBody = fromNullable(purged.body);
249 if (purgedBody.tag === 'some') await purgedBody.value.cancel().catch(() => {});
250 }
251 // redirect: 'manual' — a 3xx from upstream must reach the guard above, which re-validates every hop. Followed here,
252 // a relative Location would resolve against gitcache.internal and skip that check.
253 const res = await binding.fetch(new Request(`https://gitcache.internal${keyPath(kind, hash)}`, carried));
254 // The cache tier, in the header ply turns into the `git.response.cache` span attribute.
255 const out = new Headers(res.headers);
256 out.set('x-ply-pack-cache', unwrapOr(header(res.headers, 'cf-cache-status'), 'NONE'));
257 const state = refsState(kind, res.headers, Date.now());
258 if (state !== null) out.set(REFS_STATE_HEADER, state);
259 return new Response(res.body, { status: res.status, statusText: res.statusText, headers: out });
260 };
263export type GitCacheEnv = { PUBLIC_REPOS: unknown };
264/** What GitCache needs of RefsWatch (src/refs-watch-entry.ts). */
265export type RefsWatchRpc = { lease(repo: string): Promise<LeaseAnswer>; seen(repo: string, refs: [string, string][]): Promise<boolean> };
266export type PurgeCtx = object;
268function plain(status: number, text: string): HttpReply {
269 return httpReply(status, text, new Headers({ 'content-type': 'text/plain; charset=utf-8', 'cache-control': NO_STORE }));
272type CacheRequest = {
273 readonly bust: boolean;
274 readonly cacheKind: CacheKind;
275 readonly hash: string;
276 readonly url: string;
277 readonly proto: string;
278 readonly body: Uint8Array | null;
279 readonly ua: string | null;
280};
282type GitCacheFailure =
283 | { readonly kind: 'method' }
284 | { readonly kind: 'not_key' }
285 | { readonly kind: 'bad_body' }
286 | { readonly kind: 'not_public' }
287 | { readonly kind: 'mismatch' }
288 | { readonly kind: 'upstream'; readonly status: number; readonly text: string };
290function decodeCacheBody(value: string): { readonly kind: 'text'; readonly value: Uint8Array } | { readonly kind: 'opaque' } {
291 try {
292 return { kind: 'text', value: fromBase64(value) };
293 } catch {
294 return { kind: 'opaque' };
295 }
298function parseGitCache(req: Request): Result<CacheRequest, GitCacheFailure> {
299 if (req.method !== 'GET' && req.method !== 'HEAD') return err({ kind: 'method' });
300 const path = new URL(req.url).pathname;
301 const match = /^\/v(\d+)\/(bust\/)?(pack|refs|wrefs|discover)\/([0-9a-f]{64})$/.exec(path);
302 const version = captureGroup(match, 1);
303 const kindText = captureGroup(match, 3);
304 const hashText = captureGroup(match, 4);
305 if (version.tag === 'none' || kindText.tag === 'none' || hashText.tag === 'none' || Number(version.value) !== CACHE_VERSION) return err({ kind: 'not_key' });
306 const encoded = req.headers.get(H_BODY);
307 let body: Uint8Array | null = null;
308 if (encoded !== null) {
309 const decoded = decodeCacheBody(encoded);
310 if (decoded.kind === 'opaque') return err({ kind: 'bad_body' });
311 body = decoded.value;
312 }
313 const cacheKind = kindText.value as CacheKind;
314 return ok({
315 bust: captureGroup(match, 2).tag === 'some',
316 cacheKind,
317 hash: hashText.value,
318 url: unwrapOr(header(req.headers, H_URL), ''),
319 proto: unwrapOr(header(req.headers, H_PROTO), ''),
320 body,
321 ua: req.headers.get(H_UA),
322 });
325async function authorizeGitCache(parsed: CacheRequest): Promise<Result<CacheRequest, GitCacheFailure>> {
326 const method = parsed.cacheKind === 'discover' ? 'GET' : 'POST';
327 const classified = classify(method, parsed.url, parsed.body);
328 const watched = parsed.cacheKind === 'wrefs' && classified === 'refs' && publicUploadPackRepo(method, parsed.url) !== null;
329 if (!cacheableUpstream(method, parsed.url) || (classified !== parsed.cacheKind && !watched)) {
330 return err({ kind: 'not_public' });
331 }
332 if ((await keyHash(parsed.cacheKind, parsed.url, parsed.proto, parsed.body)) !== parsed.hash) return err({ kind: 'mismatch' });
333 return ok(parsed);
336function gitCacheFailureReply(failure: GitCacheFailure): HttpReply {
337 switch (failure.kind) {
338 case 'method':
339 return plain(405, 'method not allowed');
340 case 'not_key':
341 return plain(404, 'not a cache key');
342 case 'bad_body':
343 return plain(400, 'bad body');
344 case 'not_public':
345 return plain(403, 'not a public upload-pack exchange');
346 case 'mismatch':
347 return plain(400, 'key does not match the request');
348 case 'upstream':
349 return plain(failure.status, failure.text);
350 default:
351 return assertGitCacheFailure(failure);
352 }
355function assertGitCacheFailure(failure: never): never {
356 throw new Error(`unhandled git cache failure ${JSON.stringify(failure)}`);
359/** A rebuilt response keeps the upstream ray only when it is a ray id. Anything else (a URL, a token) is dropped. */
360function copyCfRay(from: Headers, to: Headers): void {
361 const ray = parseCfRay(from.get('cf-ray'));
362 if (ray.kind === 'id') to.set('cf-ray', ray.id);
365/**
366 * GitCache's fetch: verify the key binds the carried request, re-check that it is public, fetch upstream (the public
367 * namespace's read token is attached here, by withPublicReadCredential), and label the answer.
368 */
369export async function serveGitCache(req: Request, tokens: TokenSource | null, ctx: PurgeCtx, upstream: FetchLike = (i, n) => fetch(i, n)): Promise<Response> {
370 return runRequest(
371 {
372 parse: () => parseGitCache(req),
373 authorize: (parsed: CacheRequest) => authorizeGitCache(parsed),
374 execute: (parsed: CacheRequest) => executeGitCache(req, parsed, tokens, ctx, upstream),
375 },
376 (result: Result<HttpReply, GitCacheFailure | UnexpectedFailure>) => {
377 if (!result.ok) {
378 if (isUnexpected(result.error)) {
379 console.log(JSON.stringify({ msg: 'ply.gitcache', error_kind: result.error.kind, stage: result.error.stage, error_name: result.error.name }));
380 return new Response('unexpected\n', {
381 status: 500,
382 headers: { 'content-type': 'text/plain; charset=utf-8', 'cache-control': NO_STORE, 'x-ply-error-kind': result.error.kind },
383 });
384 }
385 return responseOf(gitCacheFailureReply(result.error));
386 }
387 return responseOf(result.value);
388 },
389 );
392async function executeGitCache(req: Request, parsed: CacheRequest, tokens: TokenSource | null, ctx: PurgeCtx, upstream: FetchLike): Promise<Result<HttpReply, GitCacheFailure>> {
393 const { cacheKind, url, proto, body, ua } = parsed;
394 if (parsed.bust) {
395 const purge = dig(ctx, ['cache', 'purge']);
396 if (purge.tag === 'some') await (purge.value as (opts: { pathPrefixes: string[] }) => Promise<unknown>)({ pathPrefixes: [keyPath(cacheKind, parsed.hash)] });
397 return ok(httpReply(204, null, new Headers({ 'cache-control': NO_STORE })));
398 }
399 const method = cacheKind === 'discover' ? 'GET' : 'POST';
400 // A watched fill asks RefsWatch for the long policy while its upstream request is in flight.
401 const repo = cacheKind === 'wrefs' ? publicUploadPackRepo(method, url) : null;
402 const leased = repo === null ? null : askLease(ctx, repo);
403 const headers = new Headers({ [VIA_HEADER]: 'gitcache' });
404 if (proto) headers.set('git-protocol', proto);
405 if (ua) headers.set('user-agent', ua);
406 if (body) { headers.set('content-type', 'application/x-git-upload-pack-request'); headers.set('accept', 'application/x-git-upload-pack-result'); }
407 const fetchUp = withPublicReadCredential(upstream, tokens);
408 let res: Response;
409 try {
410 const signal = repo === null ? req.signal : AbortSignal.any([req.signal, AbortSignal.timeout(REFS_FILL_TIMEOUT_MS)]);
411 const upstreamInit: RequestInit = { method, headers, redirect: 'manual', signal };
412 if (body !== null) upstreamInit.body = body as BodyInit;
413 res = await fetchUp(url, upstreamInit);
414 } catch (e) {
415 // A failure before any upstream answer (the read-token mint: no such repo, binding down; the network). Answer with
416 // the HTTP status ply maps back to the same code (http.js httpCode): 404 → not_found, else 502 → upstream_5xx.
417 const code = chainGet(e, 'code');
418 const notFound = code.tag === 'some' && code.value === 'not_found';
419 return err({ kind: 'upstream', status: notFound ? 404 : 502, text: notFound ? 'repository not found' : 'upstream unavailable' });
420 }
421 const ct = unwrapOr(header(res.headers, 'content-type'), '');
422 const out = new Headers({ 'content-type': ct || 'application/octet-stream', 'x-ply-gc-kind': cacheKind, 'cache-tag': `ply-gc-v${CACHE_VERSION},ply-gc-${cacheKind}` });
423 copyCfRay(res.headers, out);
424 const location = res.headers.get('location');
425 if (location) out.set('location', location);
426 const answered = res.status === 200 && /^application\/x-git-upload-pack-(result|advertisement)/.test(ct) && res.body;
427 if (!answered) {
428 // Not stored. An error body is small; scrub it with this isolate's tokens before it leaves (the caller scrubs too,
429 // but may be another isolate that never saw this token).
430 const text = res.body ? (await res.text()).slice(0, MAX_ERROR_BODY) : '';
431 out.set('cache-control', cacheKind === 'pack' && res.status === 400 && FILTER_REFUSAL.test(text) ? REFUSAL_CACHE : NO_STORE);
432 return ok(httpReply(res.status, res.status >= 300 && res.status < 400 ? null : scrubSecrets(tokens === null ? [] : tokens.secrets())(text), out));
433 }
434 if (cacheKind === 'pack') {
435 // Buffer up to the cap, verify, and only then call it immutable. Over the cap, or unverified: passed on, not stored.
436 const reader = (res.body as ReadableStream<Uint8Array>).getReader();
437 const parts: Uint8Array[] = [];
438 let total = 0;
439 for (;;) {
440 const step = await reader.read();
441 if (step.done) break;
442 parts.push(step.value);
443 total += step.value.byteLength;
444 if (total > MAX_VERIFIED_PACK) {
445 out.set('cache-control', NO_STORE);
446 out.set('x-ply-gc-verified', 'too-big');
447 const rest = new ReadableStream<Uint8Array>({
448 start(c) { for (const p of parts) c.enqueue(p); },
449 async pull(c) { const n = await reader.read(); if (n.done) c.close(); else c.enqueue(n.value); },
450 cancel(r) { return reader.cancel(r); },
451 });
452 return ok(httpReply(200, rest, out));
453 }
454 }
455 const bytes = new Uint8Array(total);
456 let offset = 0;
457 for (const part of parts) { bytes.set(part, offset); offset += part.byteLength; }
458 const verified = await verifyPackResponse(url, body as Uint8Array, bytes, ct);
459 out.set('cache-control', verified.ok ? IMMUTABLE : NO_STORE);
460 out.set('x-ply-gc-verified', verified.ok ? `ok;objects=${verified.objects}` : `failed;${verified.reason.replace(/[^\x20-\x7e]/g, '?')}`);
461 return ok(httpReply(200, bytes, out));
462 }
463 if (cacheKind === 'refs') {
464 out.set('cache-control', REFS_CACHE);
465 out.set(H_FILLED, String(Date.now()));
466 return ok(httpReply(200, res.body, out));
467 }
468 if (cacheKind === 'wrefs' && repo !== null && leased !== null) {
469 // Buffered (a ref listing is small): the refs stored are reported to RefsWatch.
470 const bytes = new Uint8Array(await res.arrayBuffer());
471 const lease = await leased;
472 out.set('cache-control', lease.grant ? WATCHED_REFS_CACHE : REFS_CACHE);
473 out.set('cache-tag', `${unwrapOr(header(out, 'cache-tag'), '')},${refsTag(repo)}`);
474 out.set(H_REFS_POLICY, `${lease.grant ? 'watched' : 'short'};${lease.reason}`);
475 out.set(H_FILLED, String(Date.now()));
476 if (lease.grant) reportSeen(ctx, repo, parseLsRefsAnswer(bytes));
477 return ok(httpReply(200, bytes, out));
478 }
479 // discover: buffer the (small) advertisement to tell a v2 capability list from a v0 ref advertisement
480 const bytes = new Uint8Array(await res.arrayBuffer());
481 out.set('cache-control', /(^|[0-9a-f]{4})version 2\n/.test(latin1(bytes.subarray(0, 128))) ? DISCOVER_V2_CACHE : REFS_CACHE);
482 return ok(httpReply(200, bytes, out));
485function watchStub(ctx: PurgeCtx): RefsWatchRpc | null {
486 const ns = dig(ctx, ['exports', 'RefsWatch']);
487 if (ns.tag === 'none' || !ns.value) return null;
488 const space = ns.value as { idFromName(name: string): unknown; get(id: unknown): RefsWatchRpc };
489 if (typeof space.idFromName !== 'function') return null;
490 return space.get(space.idFromName(WATCH_NAME));
493/** RefsWatch's lease answer, or a refusal when it is absent, errs, or takes longer than LEASE_TIMEOUT_MS. */
494export async function askLease(ctx: PurgeCtx, repo: string): Promise<LeaseAnswer> {
495 const stub = watchStub(ctx);
496 if (stub === null) return { grant: false, reason: 'unpolled' };
497 const timers: ReturnType<typeof setTimeout>[] = [];
498 const timeout = new Promise<LeaseAnswer>(resolve => { timers.push(setTimeout(() => resolve({ grant: false, reason: 'stale' }), LEASE_TIMEOUT_MS)); });
499 try {
500 return await Promise.race([stub.lease(repo).catch((): LeaseAnswer => ({ grant: false, reason: 'stale' })), timeout]);
501 } finally {
502 for (const t of timers) clearTimeout(t);
503 }
506function reportSeen(ctx: PurgeCtx, repo: string, refs: [string, string][]): void {
507 const stub = watchStub(ctx);
508 if (stub === null || refs.length === 0) return;
509 const sent = stub.seen(repo, refs).catch(() => false);
510 const wait = dig(ctx, ['waitUntil']);
511 if (wait.tag === 'some' && typeof wait.value === 'function') (wait.value as (p: Promise<unknown>) => void).call(ctx, sent);
514/** GitCache.purgeRefs: purge the refs tag of each repo, inside GitCache (tags are scoped to the entrypoint that stored them). */
515export async function purgeRefsTags(ctx: PurgeCtx, repos: readonly string[]): Promise<{ ok: boolean; ms: number; error: string }> {
516 const names = repos.filter(isPublicRepoName);
517 if (names.length === 0) return { ok: true, ms: 0, error: '' };
518 if (names.length > MAX_PURGE_TAGS) return { ok: false, ms: 0, error: `more than ${MAX_PURGE_TAGS} tags` };
519 const cache = dig(ctx, ['cache']);
520 if (cache.tag === 'none' || !cache.value) return { ok: false, ms: 0, error: 'no ctx.cache' };
521 const t0 = Date.now();
522 try {
523 const result = await (cache.value as { purge(o: { tags: string[] }): Promise<unknown> }).purge({ tags: names.map(refsTag) });
524 const success = chainGet(result, 'success');
525 if (success.tag === 'some' && success.value === false) {
526 const errors = chainGet(result, 'errors');
527 return { ok: false, ms: Date.now() - t0, error: JSON.stringify(errors.tag === 'some' ? errors.value : null).slice(0, 200) };
528 }
529 return { ok: true, ms: Date.now() - t0, error: '' };
530 } catch (e) {
531 return { ok: false, ms: Date.now() - t0, error: failureText(e).slice(0, 200) };
532 }
Diagnostics4 git calls · 42 ms
CallStatusSizeTime042 ms
discover200152 B4 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=4
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 B7 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 1020042 KiB · hit3 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=42585
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=262b639aa1b258fc1777c3a66ba8f811df3a3da2
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=42585
git.pkt.payload.max=42490
git.sideband.band1.bytes=42489
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=61
git.pack.bytes=42489
git.pack.ofs_delta=0
git.pack.ref_delta=0
git.pack.chain.max=0
git.pack.trailer=7dbf576d97429022615f79aa46b440d9da603496
git.pack.full=61
Open pack map
fetch20010 KiB · hit32 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=32
git.response.bytes=10429
git.response.cache=HIT
git.response.duration_ms=32
git.fetch.wants=dcaa15277b20192a581dfe543677dc443b170a12
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=10429
git.pkt.payload.max=10408
git.sideband.band1.bytes=10407
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
262b639aa1b258fc1777c3a66ba8f811df3a3da2
cf-ray
a5e225472beba151
Request
6ffcde45…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format