diff --git a/scripts/cloud-source-verification.test.mjs b/scripts/cloud-source-verification.test.mjs index 6e1cf174714..612c9644832 100644 --- a/scripts/cloud-source-verification.test.mjs +++ b/scripts/cloud-source-verification.test.mjs @@ -741,3 +741,52 @@ describe("release candidate admission", () => { assert.equal(calls[0].options.redirect, "error"); }); }); + +function etagServer(resources) { + const requests = []; + const fetchImpl = async (url, init) => { + const path = url.replace("https://api.github.com", ""); + const sent = init.headers["If-None-Match"] ?? null; + requests.push({ path, sent }); + const { etag, body } = resources[path]; + const notModified = sent === etag; + return { + ok: !notModified, status: notModified ? 304 : 200, + headers: { get: (name) => (name.toLowerCase() === "etag" ? etag : null) }, + json: async () => (notModified ? assert.fail("a 304 has no body") : structuredClone(body)), + }; + }; + return { requests, fetchImpl }; +} + +test("a repeated read sends its ETag and reuses the body when GitHub answers 304", async () => { + const resources = { "/repos/x": { etag: '"v1"', body: { id: 1 } } }; + const { requests, fetchImpl } = etagServer(resources); + const api = createActionsReader({ token: "t", fetchImpl, log: () => {} }); + + assert.deepEqual(await api("/repos/x"), { id: 1 }); + assert.deepEqual(await api("/repos/x"), { id: 1 }); + assert.deepEqual(requests, [{ path: "/repos/x", sent: null }, { path: "/repos/x", sent: '"v1"' }]); + + resources["/repos/x"] = { etag: '"v2"', body: { id: 2 } }; + assert.deepEqual(await api("/repos/x"), { id: 2 }, "a changed resource replaces the cached body"); + assert.deepEqual(await api("/repos/x"), { id: 2 }); + assert.equal(requests.at(-1).sent, '"v2"'); +}); + +test("a cached body cannot be changed by the caller that received it", async () => { + const { fetchImpl } = etagServer({ "/repos/x": { etag: '"v1"', body: { jobs: [1] } } }); + const api = createActionsReader({ token: "t", fetchImpl, log: () => {} }); + (await api("/repos/x")).jobs.push(2); + assert.deepEqual(await api("/repos/x"), { jobs: [1] }); +}); + +test("ETags are kept per path", async () => { + const { requests, fetchImpl } = etagServer({ + "/repos/a": { etag: '"a"', body: { id: "a" } }, "/repos/b": { etag: '"b"', body: { id: "b" } }, + }); + const api = createActionsReader({ token: "t", fetchImpl, log: () => {} }); + await api("/repos/a"); + assert.deepEqual(await api("/repos/b"), { id: "b" }); + assert.deepEqual(requests.map((r) => r.sent), [null, null]); +}); diff --git a/scripts/github-actions-reader.mjs b/scripts/github-actions-reader.mjs index 1898164792a..a171fbc02b7 100644 --- a/scripts/github-actions-reader.mjs +++ b/scripts/github-actions-reader.mjs @@ -32,6 +32,7 @@ export function createActionsReader({ attempts = 4, backoffMs = 1_000, log = console.log, now = Date.now, deadlineAt = () => Infinity, maxRetryAfterMs = 60_000, } = {}) { + const cached = new Map(); return async (path) => { for (let attempt = 1; ; attempt += 1) { // Retry only while both the attempt budget and the caller's deadline @@ -41,8 +42,12 @@ export function createActionsReader({ const pause = (response) => sleep(retryAfterMs(response, maxRetryAfterMs) ?? waitMs); let response; try { + const etag = cached.get(path)?.etag; response = await fetchImpl(`https://api.github.com${path}`, { - headers: { Authorization: `Bearer ${token}`, Accept: "application/vnd.github+json", "X-GitHub-Api-Version": "2022-11-28" }, + headers: { + Authorization: `Bearer ${token}`, Accept: "application/vnd.github+json", "X-GitHub-Api-Version": "2022-11-28", + ...(etag ? { "If-None-Match": etag } : {}), + }, signal: AbortSignal.timeout(30_000), redirect: "error", }); } catch (cause) { @@ -51,11 +56,16 @@ export function createActionsReader({ await pause(); continue; } + if (response.status === 304 && cached.has(path)) return structuredClone(cached.get(path).body); if (response.ok) { try { // A 200 whose body is truncated or undecodable is a transport // failure like any other, so it belongs inside the retry. - return await response.json(); + const body = await response.json(); + const etag = response.headers?.get?.("etag"); + if (etag) cached.set(path, { etag, body: structuredClone(body) }); + else cached.delete(path); + return body; } catch (cause) { if (!retryable) throw new Error(`GitHub Actions read failed: ${cause.message}`); log(`GitHub Actions read body failed (${cause.message}); retrying (${attempt}/${attempts - 1}).`);