26 lines · 1.2 KBRaw
| 1 | import { branchChanges } from "../diff.ts"; |
| 2 | import type { ChangeSet } from "../contract/views.ts"; |
| 3 | |
| 4 | export interface Deps { |
| 5 | env: Env; |
| 6 | ctx: ExecutionContext; |
| 7 | origin: string; |
| 8 | } |
| 9 | |
| 10 | /** |
| 11 | * A change set depends only on its two commits, so it is cached by them in the colo's cache. Callers check |
| 12 | * access before asking; the key is never reachable from outside the Worker. |
| 13 | */ |
| 14 | export async function cachedChanges(deps: Deps, repoId: string, handle: ArtifactsRepo, baseTip: string, headTip: string): Promise<ChangeSet> { |
| 15 | const key = new Request(`${deps.origin}/__refs-cache/changes/v4/${repoId}/${baseTip}/${headTip}`); |
| 16 | const cache = await caches.open("refs-changes").catch(() => null); |
| 17 | const hit = await cache?.match(key).catch(() => undefined); |
| 18 | if (hit) return hit.json(); |
| 19 | const changes = await branchChanges(handle, baseTip, headTip); |
| 20 | if (cache) deps.ctx.waitUntil(cache.put(key, new Response(JSON.stringify(changes), { headers: { "content-type": "application/json", "cache-control": "max-age=2592000" } })).catch(() => undefined)); |
| 21 | return changes; |
| 22 | } |
| 23 | |
| 24 | export function adminEmails(env: Env): Set<string> { |
| 25 | return new Set(env.ADMIN_EMAILS.split(",").map((email) => email.trim().toLowerCase()).filter(Boolean)); |
| 26 | } |