// Workers Cache for public git reads (never the Cache API). The default entrypoint has caching off and always runs: rate // limit, logs, scrubbing, traces. For an upload-pack exchange whose answer is public, it calls the cache-enabled // `GitCache` entrypoint over ctx.exports instead of the upstream; GitCache performs the upstream request and returns it // with a Cache-Control decided by what the exchange is: // // pack POST git-upload-pack, command=fetch, wants only (no have / shallow / want-ref / deepen-since / deepen-not): // a pure function of (url, body). Objects are named by their hash, so public, max-age=31536000, immutable. // refs POST git-upload-pack, command=ls-refs: refs move. public, max-age=REFS_MAX_AGE_S, swr=REFS_SWR_S // wrefs ls-refs of a public-namespace repo when the watched policy is on (createLoopbackFetch `watchedRefs`): stored // WATCHED_REFS_CACHE (minutes) and tagged refsTag(repo) when RefsWatch grants a lease, else the refs policy. // RefsWatch purges the tag when the repo's refs move (src/refs-watch.ts). Its own key kind, so a version // without the policy never reads a long-lived answer, and one with it never reads a short one. // discover GET info/refs?service=git-upload-pack: a v2 answer is the capability list (no refs), so it is long-lived; // a v0 answer carries the refs, so it gets the refs policy. // // What is public, and so may be cached (`cacheableUpstream`): the request carries no Authorization (a caller's Git-Token // or Git-Authorization always bypasses the cache), and the URL is either exactly a public-namespace repo endpoint // (publicUploadPackRepo) or an upload-pack endpoint on one of the viewer's built-in allowlisted forges. // // Key: /v//. The upstream request rides in headers, which are // not part of a Workers Cache key; GitCache recomputes the hash from them and refuses a mismatch, so no request can // store an answer under another request's key. crossVersionCache is on, so CACHE_VERSION is the only partition: bump it // when what GitCache stores changes. // // ply's retry after a corrupt cached pack sends `x-ply-cache: bust`; the loopback then purges that key (GitCache, which // owns it) and fetches again. import { captureGroup, chainGet, dig, fromNullable, header } from './absent.ts'; import { CACHE_VERSION } from './cache-version.ts'; import { err, httpReply, isUnexpected, ok, responseOf, runRequest, type HttpReply, type Result, type UnexpectedFailure } from './http/machine.ts'; import { unwrapOr } from './option.ts'; import { extractRequestPath, rawPathHasUnsafeSegment } from './pathguard.ts'; import { parseCfRay } from './rays.ts'; import { isPublicRepoName, publicUploadPackRepo, scrubSecrets, type TokenSource, withPublicReadCredential, type FetchLike } from './public.ts'; import { ALLOWED_HOSTS } from './viewer/url.ts'; import { createTransport, fetchPack, Tracer } from 'ply'; import { LEASE_TIMEOUT_MS, MAX_PURGE_TAGS, REFS_FILL_TIMEOUT_MS, WATCH_NAME, WATCHED_REFS_CACHE, parseLsRefsAnswer, refsTag, type LeaseAnswer } from './refs-watch.ts'; import { VERIFY_CAPS, VERIFY_LIMITS } from './limits.ts'; export type CacheKind = 'pack' | 'refs' | 'wrefs' | 'discover'; export const REFS_MAX_AGE_S = 5; export const REFS_SWR_S = 10; export const IMMUTABLE = 'public, max-age=31536000, immutable'; export const REFS_CACHE = `public, max-age=${REFS_MAX_AGE_S}, stale-while-revalidate=${REFS_SWR_S}`; export const DISCOVER_V2_CACHE = 'public, max-age=300, stale-while-revalidate=3600'; export const NO_STORE = 'no-store'; /** * A server's refusal of a partial-clone filter (Artifacts: `400 … only blob:none is supported`) is a fact about that * server, the same for every caller. Uncached, every fresh isolate paid it upstream, and Workers Caching does not * collapse an uncacheable answer: 11 of 13 GitCache invocations in a burst of 20 concurrent cold requests were this 400 * (e021776d, 2026-10-31T02:01:45Z). Fresh for an hour, so a server that adds the filter is seen within the hour. * Stale for a day after that, revalidated in the background: an expired refusal sent the first page of every new * 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 * first contacts in run sparse-ply, 2a4741c0; trace d149bcd8… 1647 ms MISS, c352756d… 1483 ms EXPIRED). A stale * refusal costs at most one extra fallback if the server has since added the filter. */ export const REFUSAL_CACHE = 'public, max-age=3600, stale-while-revalidate=86400'; // The text ply recognises as a filter refusal (fetch.js isFilterRejection). const FILTER_REFUSAL = /only blob:none is supported|unsupported (git )?fetch filter|filter .* not (supported|allowed)/i; /** The request body travels base64 in a header; a larger body (many wants) is not routed through the cache. */ export const MAX_ROUTED_BODY = 4096; const MAX_ERROR_BODY = 64 * 1024; /** A pack is stored only after GitCache has verified it, and it buffers the pack to do so; bigger ones pass through unstored. */ export const MAX_VERIFIED_PACK = VERIFY_CAPS.maxPackBytes; const wantsOf = (text: string) => [...text.matchAll(/[0-9a-f]{4}want ([0-9a-f]{40})\n/g)].map(m => m[1] as string); /** * Verify a buffered upload-pack fetch answer with ply's own parser: frame it, parse the pack, resolve every delta and * hash every object, and require every wanted oid among the results. ply's streamed blob read (openBlob) has no * retry-past-the-cache, so an unverified pack must never be stored as immutable. */ export async function verifyPackResponse(url: string, body: Uint8Array, bytes: Uint8Array, contentType: string): Promise<{ ok: true; objects: number } | { ok: false; reason: string }> { const text = latin1(body); const wants = wantsOf(text); if (!wants.length) return { ok: false, reason: 'no wants' }; const deepen = captureGroup(/[0-9a-f]{4}deepen (\d+)\n/.exec(text), 1); const filter = captureGroup(/[0-9a-f]{4}filter ([^\n]+)\n/.exec(text), 1); const replay = async () => new Response(bytes.slice(), { status: 200, headers: { 'content-type': contentType } }); try { 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: {} }); if (!made.ok) return { ok: false, reason: failureText(made.error).slice(0, 120) }; const r = await fetchPack(made.value, { wants, ...(deepen.tag === 'some' ? { deepen: Number(deepen.value) } : {}), ...(filter.tag === 'some' ? { filter: filter.value } : {}), ofsDelta: true }, { resolve: true }); if (!r.ok) return { ok: false, reason: failureText(r.error).slice(0, 120) }; const objects = fromNullable(r.value.objects); if (objects.tag === 'none') return { ok: false, reason: 'no pack in the answer' }; const missing = wants.filter(w => !objects.value.has(w)); return missing.length ? { ok: false, reason: `wanted ${missing.length} oid(s) not in the pack` } : { ok: true, objects: objects.value.size }; } catch (e) { // A replayed body that errors mid-stream, or a ply invariant: still a pack that is not stored. return { ok: false, reason: failureText(e).slice(0, 120) }; } } function failureText(error: unknown): string { const code = chainGet(error, 'code'); if (code.tag === 'some') return String(code.value); const message = chainGet(error, 'message'); if (message.tag === 'some') return String(message.value); return String(error); } const H_URL = 'x-ply-gc-url'; const H_BODY = 'x-ply-gc-body'; const H_PROTO = 'x-ply-gc-proto'; const H_UA = 'x-ply-gc-ua'; /** On a refs or wrefs answer: when GitCache stored it (ms), so a hit's age is known. */ export const H_FILLED = 'x-ply-gc-filled'; /** On a wrefs answer: `watched;` or `short;` (the lease answer). */ export const H_REFS_POLICY = 'x-ply-gc-refs'; /** What the loopback reports for a refs exchange: `;age=`. observeUpstream puts it on the span. */ export const REFS_STATE_HEADER = 'x-ply-refs'; /** Set by GitCache on its upstream request, so tests can tell a cached path from a direct one. Carries no secret. */ export const VIA_HEADER = 'x-ply-via'; const BUILTIN = new Set(ALLOWED_HOSTS); function forgeUploadPack(method: string, raw: string): boolean { if (typeof raw !== 'string' || raw.length > 2048 || raw.includes('\\') || raw.includes('#')) return false; if (rawPathHasUnsafeSegment(extractRequestPath(raw))) return false; let u: URL; try { u = new URL(raw); } catch { return false; } if (u.protocol !== 'https:' || u.port !== '' || u.username || u.password || !BUILTIN.has(u.hostname)) return false; if (!raw.startsWith(`https://${u.hostname}/`)) return false; const verb = method.toUpperCase(); if (u.pathname.endsWith('/info/refs')) return verb === 'GET' && u.search === '?service=git-upload-pack'; return u.pathname.endsWith('/git-upload-pack') && verb === 'POST' && u.search === ''; } /** Is (method, url) an upload-pack endpoint whose anonymous answer is public? (Authorization is checked by the caller.) */ export function cacheableUpstream(method: string, url: string): boolean { return publicUploadPackRepo(method, url) !== null || forgeUploadPack(method, url); } // 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 // one-char strings per call, and the loopback built it twice per exchange (classify, then base64). const latin1 = (b: Uint8Array) => { let s = ''; for (let i = 0; i < b.length; i += 1024) s += String.fromCharCode.apply(null, b.subarray(i, i + 1024) as unknown as number[]); return s; }; // pkt-lines: 4 hex length, then the payload; anchor to the length prefix so text inside a payload can't match. const LS_REFS = /[0-9a-f]{4}command=ls-refs\n/; const FETCH = /[0-9a-f]{4}command=fetch\n/; const NOT_PURE = /[0-9a-f]{4}(have [0-9a-f]{40}\n|want-ref |deepen-(since|not) |shallow [0-9a-f]{40}\n)/; const WANT = /[0-9a-f]{4}want [0-9a-f]{40}\n/; /** classify on a body already read as latin1 text (`null`: no body). */ function classifyText(method: string, url: string, t: string | null): CacheKind | null { if (method.toUpperCase() === 'GET') return url.endsWith('/info/refs?service=git-upload-pack') && t === null ? 'discover' : null; if (t === null) return null; if (LS_REFS.test(t)) return 'refs'; if (!FETCH.test(t)) return null; if (NOT_PURE.test(t)) return null; return WANT.test(t) ? 'pack' : null; } /** Which cache policy an upload-pack exchange gets, or null when it is not a pure function of its request. */ export function classify(method: string, url: string, body: Uint8Array | null): CacheKind | null { return classifyText(method, url, body === null ? null : latin1(body)); } function 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; } const HEX = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0')); const ENCODER = new TextEncoder(); export async function keyHash(kind: CacheKind, url: string, gitProtocol: string, body: Uint8Array | null): Promise { const head = `ply-gitcache\n${kind}\n${url}\n${gitProtocol}\n`; // UTF-8 is at most 3 bytes per UTF-16 unit: one buffer for head and body, no second copy. const all = new Uint8Array(head.length * 3 + (body === null ? 0 : body.length)); const { written } = ENCODER.encodeInto(head, all); if (body) all.set(body, written); const d = new Uint8Array(await crypto.subtle.digest('SHA-256', all.subarray(0, written + (body === null ? 0 : body.length)))); let hex = ''; for (let i = 0; i < d.length; i++) hex += HEX[d[i] as number]; return hex; } export function keyPath(kind: CacheKind, hash: string, version: number = CACHE_VERSION): string { return `/v${version}/${kind}/${hash}`; } export type LoopbackBinding = { fetch(req: Request): Promise }; export function isLoopback(value: unknown): value is LoopbackBinding { return typeof value === 'object' && value !== null && 'fetch' in value; } function requestUrl(input: RequestInfo | URL): string { if (typeof input === 'string') return input; if (input instanceof URL) return input.href; return input.url; } /** * The fetch that sits under the redirect guards (createGuardedFetch, createViewerFetch). A public, cacheable exchange * with no Authorization goes to GitCache; everything else goes to `direct` unchanged. `loopback` is null when the * cache is off (PACK_CACHE=off) or there is no ctx.exports (node tests). */ export type LoopbackOptions = { readonly watchedRefs: boolean }; /** The key kind of an exchange: a public-namespace ls-refs is `wrefs` when the watched policy is on. */ export function routedKind(kind: CacheKind | null, method: string, url: string, options: LoopbackOptions): CacheKind | null { if (kind === 'refs' && options.watchedRefs && publicUploadPackRepo(method, url) !== null) return 'wrefs'; return kind; } /** `;age=` for a refs answer: the policy GitCache stored it under and how long ago. */ export function refsState(kind: CacheKind, headers: Headers, now: number): string | null { if (kind !== 'refs' && kind !== 'wrefs') return null; const filled = Number(headers.get(H_FILLED)); const age = Number.isFinite(filled) && filled > 0 ? Math.max(0, Math.round((now - filled) / 1000)) : -1; const policy = kind === 'refs' ? 'short;ttl' : unwrapOr(header(headers, H_REFS_POLICY), 'none'); return `${policy};age=${age}`; } export function createLoopbackFetch(direct: FetchLike, loopback: (() => LoopbackBinding | null) | null, options: LoopbackOptions = { watchedRefs: false }): FetchLike { return async (input, init) => { if (loopback === null) return direct(input, init); const binding = loopback(); if (binding === null) return direct(input, init); const url = requestUrl(input); const methodField = chainGet(init, 'method'); const method = methodField.tag === 'some' && typeof methodField.value === 'string' ? methodField.value : 'GET'; const headerField = chainGet(init, 'headers'); // ply hands a Headers; read it in place rather than copying it per exchange. const headers = headerField.tag === 'none' ? new Headers() : headerField.value instanceof Headers ? headerField.value : new Headers(headerField.value as HeadersInit); if (headers.has('authorization')) return direct(input, init); if (!cacheableUpstream(method, url)) return direct(input, init); const bodyField = chainGet(init, 'body'); const body = bodyField.tag === 'some' && bodyField.value instanceof Uint8Array ? bodyField.value : null; if (bodyField.tag === 'some' && bodyField.value && body === null) return direct(input, init); if (body && body.length > MAX_ROUTED_BODY) return direct(input, init); const text = body === null ? null : latin1(body); const kind = routedKind(classifyText(method, url, text), method, url, options); if (!kind) return direct(input, init); const proto = unwrapOr(header(headers, 'git-protocol'), ''); const hash = await keyHash(kind, url, proto, body); const carry = new Headers({ [H_URL]: url, [H_PROTO]: proto, [H_UA]: unwrapOr(header(headers, 'user-agent'), '') }); if (text !== null) carry.set(H_BODY, btoa(text)); const signalField = chainGet(init, 'signal'); const carried = { headers: carry, redirect: 'manual' as const, ...(signalField.tag === 'some' ? { signal: signalField.value as AbortSignal } : {}) }; if (headers.get('x-ply-cache') === 'bust') { const purged = await binding.fetch(new Request(`https://gitcache.internal/v${CACHE_VERSION}/bust/${kind}/${hash}`, carried)); const purgedBody = fromNullable(purged.body); if (purgedBody.tag === 'some') await purgedBody.value.cancel().catch(() => {}); } // redirect: 'manual' — a 3xx from upstream must reach the guard above, which re-validates every hop. Followed here, // a relative Location would resolve against gitcache.internal and skip that check. const res = await binding.fetch(new Request(`https://gitcache.internal${keyPath(kind, hash)}`, carried)); // The cache tier, in the header ply turns into the `git.response.cache` span attribute. const out = new Headers(res.headers); out.set('x-ply-pack-cache', unwrapOr(header(res.headers, 'cf-cache-status'), 'NONE')); const state = refsState(kind, res.headers, Date.now()); if (state !== null) out.set(REFS_STATE_HEADER, state); return new Response(res.body, { status: res.status, statusText: res.statusText, headers: out }); }; } export type GitCacheEnv = { PUBLIC_REPOS: unknown }; /** What GitCache needs of RefsWatch (src/refs-watch-entry.ts). */ export type RefsWatchRpc = { lease(repo: string): Promise; seen(repo: string, refs: [string, string][]): Promise }; export type PurgeCtx = object; function plain(status: number, text: string): HttpReply { return httpReply(status, text, new Headers({ 'content-type': 'text/plain; charset=utf-8', 'cache-control': NO_STORE })); } type CacheRequest = { readonly bust: boolean; readonly cacheKind: CacheKind; readonly hash: string; readonly url: string; readonly proto: string; readonly body: Uint8Array | null; readonly ua: string | null; }; type GitCacheFailure = | { readonly kind: 'method' } | { readonly kind: 'not_key' } | { readonly kind: 'bad_body' } | { readonly kind: 'not_public' } | { readonly kind: 'mismatch' } | { readonly kind: 'upstream'; readonly status: number; readonly text: string }; function decodeCacheBody(value: string): { readonly kind: 'text'; readonly value: Uint8Array } | { readonly kind: 'opaque' } { try { return { kind: 'text', value: fromBase64(value) }; } catch { return { kind: 'opaque' }; } } function parseGitCache(req: Request): Result { if (req.method !== 'GET' && req.method !== 'HEAD') return err({ kind: 'method' }); const path = new URL(req.url).pathname; const match = /^\/v(\d+)\/(bust\/)?(pack|refs|wrefs|discover)\/([0-9a-f]{64})$/.exec(path); const version = captureGroup(match, 1); const kindText = captureGroup(match, 3); const hashText = captureGroup(match, 4); if (version.tag === 'none' || kindText.tag === 'none' || hashText.tag === 'none' || Number(version.value) !== CACHE_VERSION) return err({ kind: 'not_key' }); const encoded = req.headers.get(H_BODY); let body: Uint8Array | null = null; if (encoded !== null) { const decoded = decodeCacheBody(encoded); if (decoded.kind === 'opaque') return err({ kind: 'bad_body' }); body = decoded.value; } const cacheKind = kindText.value as CacheKind; return ok({ bust: captureGroup(match, 2).tag === 'some', cacheKind, hash: hashText.value, url: unwrapOr(header(req.headers, H_URL), ''), proto: unwrapOr(header(req.headers, H_PROTO), ''), body, ua: req.headers.get(H_UA), }); } async function authorizeGitCache(parsed: CacheRequest): Promise> { const method = parsed.cacheKind === 'discover' ? 'GET' : 'POST'; const classified = classify(method, parsed.url, parsed.body); const watched = parsed.cacheKind === 'wrefs' && classified === 'refs' && publicUploadPackRepo(method, parsed.url) !== null; if (!cacheableUpstream(method, parsed.url) || (classified !== parsed.cacheKind && !watched)) { return err({ kind: 'not_public' }); } if ((await keyHash(parsed.cacheKind, parsed.url, parsed.proto, parsed.body)) !== parsed.hash) return err({ kind: 'mismatch' }); return ok(parsed); } function gitCacheFailureReply(failure: GitCacheFailure): HttpReply { switch (failure.kind) { case 'method': return plain(405, 'method not allowed'); case 'not_key': return plain(404, 'not a cache key'); case 'bad_body': return plain(400, 'bad body'); case 'not_public': return plain(403, 'not a public upload-pack exchange'); case 'mismatch': return plain(400, 'key does not match the request'); case 'upstream': return plain(failure.status, failure.text); default: return assertGitCacheFailure(failure); } } function assertGitCacheFailure(failure: never): never { throw new Error(`unhandled git cache failure ${JSON.stringify(failure)}`); } /** A rebuilt response keeps the upstream ray only when it is a ray id. Anything else (a URL, a token) is dropped. */ function copyCfRay(from: Headers, to: Headers): void { const ray = parseCfRay(from.get('cf-ray')); if (ray.kind === 'id') to.set('cf-ray', ray.id); } /** * GitCache's fetch: verify the key binds the carried request, re-check that it is public, fetch upstream (the public * namespace's read token is attached here, by withPublicReadCredential), and label the answer. */ export async function serveGitCache(req: Request, tokens: TokenSource | null, ctx: PurgeCtx, upstream: FetchLike = (i, n) => fetch(i, n)): Promise { return runRequest( { parse: () => parseGitCache(req), authorize: (parsed: CacheRequest) => authorizeGitCache(parsed), execute: (parsed: CacheRequest) => executeGitCache(req, parsed, tokens, ctx, upstream), }, (result: Result) => { if (!result.ok) { if (isUnexpected(result.error)) { console.log(JSON.stringify({ msg: 'ply.gitcache', error_kind: result.error.kind, stage: result.error.stage, error_name: result.error.name })); return new Response('unexpected\n', { status: 500, headers: { 'content-type': 'text/plain; charset=utf-8', 'cache-control': NO_STORE, 'x-ply-error-kind': result.error.kind }, }); } return responseOf(gitCacheFailureReply(result.error)); } return responseOf(result.value); }, ); } async function executeGitCache(req: Request, parsed: CacheRequest, tokens: TokenSource | null, ctx: PurgeCtx, upstream: FetchLike): Promise> { const { cacheKind, url, proto, body, ua } = parsed; if (parsed.bust) { const purge = dig(ctx, ['cache', 'purge']); if (purge.tag === 'some') await (purge.value as (opts: { pathPrefixes: string[] }) => Promise)({ pathPrefixes: [keyPath(cacheKind, parsed.hash)] }); return ok(httpReply(204, null, new Headers({ 'cache-control': NO_STORE }))); } const method = cacheKind === 'discover' ? 'GET' : 'POST'; // A watched fill asks RefsWatch for the long policy while its upstream request is in flight. const repo = cacheKind === 'wrefs' ? publicUploadPackRepo(method, url) : null; const leased = repo === null ? null : askLease(ctx, repo); const headers = new Headers({ [VIA_HEADER]: 'gitcache' }); if (proto) headers.set('git-protocol', proto); if (ua) headers.set('user-agent', ua); if (body) { headers.set('content-type', 'application/x-git-upload-pack-request'); headers.set('accept', 'application/x-git-upload-pack-result'); } const fetchUp = withPublicReadCredential(upstream, tokens); let res: Response; try { const signal = repo === null ? req.signal : AbortSignal.any([req.signal, AbortSignal.timeout(REFS_FILL_TIMEOUT_MS)]); const upstreamInit: RequestInit = { method, headers, redirect: 'manual', signal }; if (body !== null) upstreamInit.body = body as BodyInit; res = await fetchUp(url, upstreamInit); } catch (e) { // A failure before any upstream answer (the read-token mint: no such repo, binding down; the network). Answer with // the HTTP status ply maps back to the same code (http.js httpCode): 404 → not_found, else 502 → upstream_5xx. const code = chainGet(e, 'code'); const notFound = code.tag === 'some' && code.value === 'not_found'; return err({ kind: 'upstream', status: notFound ? 404 : 502, text: notFound ? 'repository not found' : 'upstream unavailable' }); } const ct = unwrapOr(header(res.headers, 'content-type'), ''); 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}` }); copyCfRay(res.headers, out); const location = res.headers.get('location'); if (location) out.set('location', location); const answered = res.status === 200 && /^application\/x-git-upload-pack-(result|advertisement)/.test(ct) && res.body; if (!answered) { // Not stored. An error body is small; scrub it with this isolate's tokens before it leaves (the caller scrubs too, // but may be another isolate that never saw this token). const text = res.body ? (await res.text()).slice(0, MAX_ERROR_BODY) : ''; out.set('cache-control', cacheKind === 'pack' && res.status === 400 && FILTER_REFUSAL.test(text) ? REFUSAL_CACHE : NO_STORE); return ok(httpReply(res.status, res.status >= 300 && res.status < 400 ? null : scrubSecrets(tokens === null ? [] : tokens.secrets())(text), out)); } if (cacheKind === 'pack') { // Buffer up to the cap, verify, and only then call it immutable. Over the cap, or unverified: passed on, not stored. const reader = (res.body as ReadableStream).getReader(); const parts: Uint8Array[] = []; let total = 0; for (;;) { const step = await reader.read(); if (step.done) break; parts.push(step.value); total += step.value.byteLength; if (total > MAX_VERIFIED_PACK) { out.set('cache-control', NO_STORE); out.set('x-ply-gc-verified', 'too-big'); const rest = new ReadableStream({ start(c) { for (const p of parts) c.enqueue(p); }, async pull(c) { const n = await reader.read(); if (n.done) c.close(); else c.enqueue(n.value); }, cancel(r) { return reader.cancel(r); }, }); return ok(httpReply(200, rest, out)); } } const bytes = new Uint8Array(total); let offset = 0; for (const part of parts) { bytes.set(part, offset); offset += part.byteLength; } const verified = await verifyPackResponse(url, body as Uint8Array, bytes, ct); out.set('cache-control', verified.ok ? IMMUTABLE : NO_STORE); out.set('x-ply-gc-verified', verified.ok ? `ok;objects=${verified.objects}` : `failed;${verified.reason.replace(/[^\x20-\x7e]/g, '?')}`); return ok(httpReply(200, bytes, out)); } if (cacheKind === 'refs') { out.set('cache-control', REFS_CACHE); out.set(H_FILLED, String(Date.now())); return ok(httpReply(200, res.body, out)); } if (cacheKind === 'wrefs' && repo !== null && leased !== null) { // Buffered (a ref listing is small): the refs stored are reported to RefsWatch. const bytes = new Uint8Array(await res.arrayBuffer()); const lease = await leased; out.set('cache-control', lease.grant ? WATCHED_REFS_CACHE : REFS_CACHE); out.set('cache-tag', `${unwrapOr(header(out, 'cache-tag'), '')},${refsTag(repo)}`); out.set(H_REFS_POLICY, `${lease.grant ? 'watched' : 'short'};${lease.reason}`); out.set(H_FILLED, String(Date.now())); if (lease.grant) reportSeen(ctx, repo, parseLsRefsAnswer(bytes)); return ok(httpReply(200, bytes, out)); } // discover: buffer the (small) advertisement to tell a v2 capability list from a v0 ref advertisement const bytes = new Uint8Array(await res.arrayBuffer()); out.set('cache-control', /(^|[0-9a-f]{4})version 2\n/.test(latin1(bytes.subarray(0, 128))) ? DISCOVER_V2_CACHE : REFS_CACHE); return ok(httpReply(200, bytes, out)); } function watchStub(ctx: PurgeCtx): RefsWatchRpc | null { const ns = dig(ctx, ['exports', 'RefsWatch']); if (ns.tag === 'none' || !ns.value) return null; const space = ns.value as { idFromName(name: string): unknown; get(id: unknown): RefsWatchRpc }; if (typeof space.idFromName !== 'function') return null; return space.get(space.idFromName(WATCH_NAME)); } /** RefsWatch's lease answer, or a refusal when it is absent, errs, or takes longer than LEASE_TIMEOUT_MS. */ export async function askLease(ctx: PurgeCtx, repo: string): Promise { const stub = watchStub(ctx); if (stub === null) return { grant: false, reason: 'unpolled' }; const timers: ReturnType[] = []; const timeout = new Promise(resolve => { timers.push(setTimeout(() => resolve({ grant: false, reason: 'stale' }), LEASE_TIMEOUT_MS)); }); try { return await Promise.race([stub.lease(repo).catch((): LeaseAnswer => ({ grant: false, reason: 'stale' })), timeout]); } finally { for (const t of timers) clearTimeout(t); } } function reportSeen(ctx: PurgeCtx, repo: string, refs: [string, string][]): void { const stub = watchStub(ctx); if (stub === null || refs.length === 0) return; const sent = stub.seen(repo, refs).catch(() => false); const wait = dig(ctx, ['waitUntil']); if (wait.tag === 'some' && typeof wait.value === 'function') (wait.value as (p: Promise) => void).call(ctx, sent); } /** GitCache.purgeRefs: purge the refs tag of each repo, inside GitCache (tags are scoped to the entrypoint that stored them). */ export async function purgeRefsTags(ctx: PurgeCtx, repos: readonly string[]): Promise<{ ok: boolean; ms: number; error: string }> { const names = repos.filter(isPublicRepoName); if (names.length === 0) return { ok: true, ms: 0, error: '' }; if (names.length > MAX_PURGE_TAGS) return { ok: false, ms: 0, error: `more than ${MAX_PURGE_TAGS} tags` }; const cache = dig(ctx, ['cache']); if (cache.tag === 'none' || !cache.value) return { ok: false, ms: 0, error: 'no ctx.cache' }; const t0 = Date.now(); try { const result = await (cache.value as { purge(o: { tags: string[] }): Promise }).purge({ tags: names.map(refsTag) }); const success = chainGet(result, 'success'); if (success.tag === 'some' && success.value === false) { const errors = chainGet(result, 'errors'); return { ok: false, ms: Date.now() - t0, error: JSON.stringify(errors.tag === 'some' ? errors.value : null).slice(0, 200) }; } return { ok: true, ms: Date.now() - t0, error: '' }; } catch (e) { return { ok: false, ms: Date.now() - t0, error: failureText(e).slice(0, 200) }; } }