Skip to content

Commit aacbb79

Browse files
committed
perf(typst): overlap artifact download with producer trust checks
1 parent b1169e0 commit aacbb79

3 files changed

Lines changed: 122 additions & 32 deletions

File tree

‎packages/xl-typst-compiler/README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ old local outputs rebuild or restore, v2 remote artifacts are not requested,
8989
and old shared-cache entries may be removed manually. A source build removes
9090
leftover `.build-hash` metadata from `pkg/`.
9191

92+
For each matching artifact, the producer trust check and archive download run
93+
in parallel. An untrusted producer aborts/discards the speculative download;
94+
no archive is extracted or installed before trust is established. Only one
95+
candidate is downloaded at a time.
9296
Downloads are staged beside `pkg/` and checksum-verified before being renamed
9397
into place without a second output copy. The checksums
9498
detect corruption; trust comes from the restricted workflow producer, not the

‎packages/xl-typst-compiler/scripts/wasm-cache.mjs‎

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,30 @@ export function unpackArtifact(bytes, dir, hash) {
319319
: { error: "artifact checksum mismatch" };
320320
}
321321

322+
async function downloadArchive(base, id, token, fetcher) {
323+
const archive = await request(`${base}/artifacts/${id}/zip`, token, fetcher);
324+
if (archive.error) {
325+
return archive;
326+
}
327+
const location = archive.response.headers.get("location");
328+
if (
329+
archive.response.status !== 302 ||
330+
!location ||
331+
!URL.canParse(location) ||
332+
new URL(location).protocol !== "https:"
333+
) {
334+
return { error: "artifact download unavailable" };
335+
}
336+
// Signed storage URL: never forward the GitHub token to another host.
337+
const download = await request(location, undefined, fetcher);
338+
if (download.error) {
339+
return download;
340+
}
341+
return download.response.ok
342+
? readBody(download.response)
343+
: { error: "artifact download unavailable" };
344+
}
345+
322346
export async function downloadOutput(
323347
hash,
324348
destination,
@@ -355,40 +379,31 @@ export async function downloadOutput(
355379
) {
356380
continue;
357381
}
358-
const run = await jsonRequest(
359-
`${base}/runs/${artifact.workflow_run.id}`,
360-
token,
361-
fetcher,
362-
);
363-
if (run.error || !trustedRun(run.value)) {
382+
// Speculate on one candidate at a time. Bytes may arrive before trust is
383+
// known, but nothing is extracted or installed until both checks pass.
384+
const controller = new AbortController();
385+
const candidateFetch = (url, options) =>
386+
fetcher(url, {
387+
...options,
388+
signal: AbortSignal.any([controller.signal, options.signal]),
389+
});
390+
const [trusted, body] = await Promise.all([
391+
jsonRequest(
392+
`${base}/runs/${artifact.workflow_run.id}`,
393+
token,
394+
candidateFetch,
395+
).then((run) => {
396+
const trusted = !run.error && trustedRun(run.value);
397+
if (!trusted) {
398+
controller.abort();
399+
}
400+
return trusted;
401+
}),
402+
downloadArchive(base, artifact.id, token, candidateFetch),
403+
]).finally(() => controller.abort());
404+
if (!trusted) {
364405
continue;
365406
}
366-
const archive = await request(
367-
`${base}/artifacts/${artifact.id}/zip`,
368-
token,
369-
fetcher,
370-
);
371-
if (archive.error) {
372-
return archive;
373-
}
374-
const location = archive.response.headers.get("location");
375-
if (
376-
archive.response.status !== 302 ||
377-
!location ||
378-
!URL.canParse(location) ||
379-
new URL(location).protocol !== "https:"
380-
) {
381-
return { error: "artifact download unavailable" };
382-
}
383-
// Signed storage URL: never forward the GitHub token to another host.
384-
const download = await request(location, undefined, fetcher);
385-
if (download.error) {
386-
return download;
387-
}
388-
if (!download.response.ok) {
389-
return { error: "artifact download unavailable" };
390-
}
391-
const body = await readBody(download.response);
392407
if (body.error) {
393408
return body;
394409
}

‎packages/xl-typst-compiler/scripts/wasm-cache.test.mjs‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,77 @@ describe("WASM fingerprint and verification", () => {
187187
});
188188

189189
describe("GitHub artifacts", () => {
190+
it.each([true, false])(
191+
"downloads while trust is pending, then gates installation (trusted: %s)",
192+
async (trusted) => {
193+
const source = temp();
194+
const destination = join(temp(), "pkg");
195+
output(source, "hash");
196+
output(destination, "previous");
197+
const zip = zipOutput(source);
198+
const metadata = Promise.withResolvers();
199+
let storageSignal;
200+
let storageStarted = false;
201+
const pending = downloadOutput("hash", destination, {
202+
token: "token",
203+
fetcher: async (url, options) => {
204+
if (url.includes("/artifacts?")) {
205+
return Response.json({
206+
artifacts: [
207+
{ name: artifactName("hash"), id: 1, workflow_run: { id: 2 } },
208+
],
209+
});
210+
}
211+
if (url.endsWith("/runs/2")) {
212+
return metadata.promise;
213+
}
214+
if (url.endsWith("/artifacts/1/zip")) {
215+
return new Response(null, {
216+
status: 302,
217+
headers: { location: "https://storage.example/artifact" },
218+
});
219+
}
220+
expect(url).toBe("https://storage.example/artifact");
221+
expect(options.headers.Authorization).toBeUndefined();
222+
storageStarted = true;
223+
storageSignal = options.signal;
224+
if (trusted) {
225+
return new Response(zip);
226+
}
227+
return new Promise((resolve, reject) => {
228+
options.signal.throwIfAborted();
229+
options.signal.addEventListener(
230+
"abort",
231+
() => reject(options.signal.reason),
232+
{ once: true },
233+
);
234+
});
235+
},
236+
});
237+
try {
238+
// Drain the immediate mock HTTP work, without resolving producer trust.
239+
await new Promise(setImmediate);
240+
expect(storageStarted).toBe(true);
241+
expect(validateOutput(destination, "previous")).toBe(true);
242+
expect(readdirSync(dirname(destination))).toEqual(["pkg"]);
243+
} finally {
244+
metadata.resolve(
245+
Response.json(trusted ? run : { ...run, event: "pull_request" }),
246+
);
247+
const result = await pending;
248+
expect(result).toEqual(
249+
trusted
250+
? { restored: true }
251+
: { error: "no matching artifact from a successful main build" },
252+
);
253+
}
254+
expect(storageSignal.aborted).toBe(true);
255+
expect(validateOutput(destination, trusted ? "hash" : "previous")).toBe(
256+
true,
257+
);
258+
},
259+
);
260+
190261
it.each([
191262
{ event: "pull_request" },
192263
{ head_branch: "feature" },

0 commit comments

Comments
 (0)