Skip to content

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

122 linesCodeBlameRaw
1import assert from "node:assert/strict";
2import { test } from "node:test";
3import { createHash } from "node:crypto";
4import { mkdtemp, mkdir, writeFile, rm } from "node:fs/promises";
5import { tmpdir } from "node:os";
6import { dirname, join, resolve } from "node:path";
7import { archiveAsset, archiveLiveAssets, assetReferences, publishWebAssets, readArchiveManifest, retryingFetch, verifyWebAssets } from "./web-assets.mjs";
8
9test("discover actual hashed assets without archiving icons, source maps or library defaults", () => {
10 assert.deepEqual(assetReferences('<link href="/assets/app-12345678.css"><img src="/apple-touch-icon.png"> import "./chunk-abcdefgh.js"; import "./elk-worker.min.js"; url(/assets/font-12345678.woff2)'), ["app-12345678.css", "chunk-abcdefgh.js", "font-12345678.woff2"]);
11});
12
13test("archive the old release's transitive chunks before replacing it", async () => {
14 const responses = {
15 "/": '<link href="/assets/app-12345678.css"><script src="/assets/entry-12345678.js"></script>',
16 "/assets/app-12345678.css": "body{color:red}",
17 "/assets/entry-12345678.js": 'import "./lazy-12345678.js"; import "./lazy-12345678.js"',
18 "/assets/lazy-12345678.js": 'import "./entry-12345678.js"',
19 };
20 const saved = [];
21 const total = await archiveLiveAssets("https://example.com", async (name) => saved.push(name), async (url) => new Response(responses[new URL(url).pathname]));
22 assert.equal(total, 3);
23 assert.deepEqual(saved.sort(), ["app-12345678.css", "entry-12345678.js", "lazy-12345678.js"]);
24});
25
26test("archive failures block cutover and immutable filename collisions are refused", async () => {
27 const args = { name: "app-12345678.css", bytes: Buffer.from("body{}"), account: "account", bucket: "bucket", headers: {} };
28 const calls = [];
29 await assert.rejects(archiveAsset({ ...args, fetchImpl: async (_url, init) => {
30 calls.push(init.method ?? "GET");
31 return new Response(null, { status: init.method === "PUT" ? 503 : 404 });
32 } }), /upload failed/);
33 assert.deepEqual(calls, ["GET", "PUT"]);
34 await assert.rejects(archiveAsset({ ...args, fetchImpl: async (_url, init) => init.method === "PUT" ? Response.json({ success: true, result: { etag: "incorrect" } }) : new Response(null, { status: 404 }) }), /checksum mismatch/);
35 await assert.rejects(archiveAsset({ ...args, fetchImpl: async () => new Response(null, { headers: { etag: '"different"' } }) }), /collision/);
36 const etag = `"${createHash("md5").update(args.bytes).digest("hex")}"`;
37 await archiveAsset({ ...args, fetchImpl: async (_url, init) => {
38 assert.notEqual(init.method, "PUT");
39 return new Response(null, { headers: { etag } });
40 } });
41});
42
43test("a missing live asset aborts the archive instead of saving an HTML error as CSS", async () => {
44 await assert.rejects(archiveLiveAssets("https://example.com", async () => assert.fail(), async (url) => new URL(url).pathname === "/"
45 ? new Response('/assets/app-12345678.css /assets/entry-12345678.js')
46 : new Response("not found", { headers: { "content-type": "text/html" } })), /unavailable/);
47});
48
49test("a release manifest skips unchanged uploads without scanning deployment history", async () => {
50 const bytes = Buffer.from("body{}");
51 const etag = createHash("md5").update(bytes).digest("hex");
52 const index = await readArchiveManifest("https://provider/manifest", {}, async () => Response.json({ version: 1, assets: [["web-assets/assets/app-12345678.css", `"${etag}"`]] }));
53 await archiveAsset({ name: "app-12345678.css", bytes, account: "a", bucket: "b", headers: {}, index, fetchImpl: async () => assert.fail("unchanged asset should not be uploaded") });
54 await assert.rejects(archiveAsset({ name: "app-12345678.css", bytes: Buffer.from("different"), account: "a", bucket: "b", headers: {}, index }), /collision/);
55});
56
57test("provider rate limits wait and retry; persistent failure remains a failure", async () => {
58 const waits = [];
59 let calls = 0;
60 const response = await retryingFetch(async () => new Response(null, { status: ++calls < 3 ? 429 : 200, headers: { "retry-after": "2" } }), async (ms) => waits.push(ms))("https://provider");
61 assert.equal(response.status, 200);
62 assert.deepEqual(waits, [2000, 2000]);
63 calls = 0;
64 assert.equal((await retryingFetch(async () => { calls++; return new Response(null, { status: 503 }); }, async () => {})("https://provider")).status, 503);
65 assert.equal(calls, 7);
66});
67
68test("cutover verifies old and new assets and rejects HTML fallback or wrong build", async () => {
69 const css = "body{}";
70 const release = { origin: "https://example.com", manifest: "manifest-12345678.js", samples: [["web-assets/assets/app-old12345.css", `"${createHash("md5").update(css).digest("hex")}"`]] };
71 const page = '<link href="/assets/app-12345678.css"><script src="/assets/manifest-12345678.js"></script>';
72 await verifyWebAssets(release, async (url) => new Response(new URL(url).pathname === "/" ? page : css));
73 await assert.rejects(verifyWebAssets(release, async (url) => new Response(new URL(url).pathname === "/" ? page : "error", { headers: { "content-type": "text/html" } })), /asset check failed/);
74 await assert.rejects(verifyWebAssets(release, async () => new Response(page.replaceAll("12345678", "87654321"))), /expected build/);
75});
76
77test("1000 successive releases keep old tabs' assets and perform bounded incremental archive work", async () => {
78 const cwd = await mkdtemp(join(tmpdir(), "g1t-web-releases-"));
79 const directory = join(cwd, "build/client/assets");
80 await mkdir(directory, { recursive: true });
81 await mkdir(join(cwd, "build/server"), { recursive: true });
82 await writeFile(join(cwd, "build/server/wrangler.json"), JSON.stringify({ account_id: "a", r2_buckets: [{ binding: "DOWNLOADS", bucket_name: "b" }], routes: [{ custom_domain: true, pattern: "example.com" }] }));
83 const objects = new Map();
84 const shared = 'export const shared = true;';
85 let live = null;
86 let apiCalls = 0;
87 const fetchImpl = async (input, init = {}) => {
88 const url = new URL(input);
89 if (url.hostname === "example.com") {
90 assert.equal(url.pathname, "/", "already protected releases must not crawl all live chunks again");
91 return new Response(`/assets/${live} /assets/app-12345678.css`);
92 }
93 apiCalls++;
94 assert.ok(url.pathname.includes("/objects/"), "must not list all previous releases");
95 const key = url.pathname.split("/objects/")[1];
96 if (init.method === "PUT") {
97 objects.set(key, Buffer.from(init.body));
98 return Response.json({ success: true, result: { etag: createHash("md5").update(init.body).digest("hex") } });
99 }
100 const bytes = objects.get(key);
101 return bytes ? new Response(bytes, { headers: { etag: `"${createHash("md5").update(bytes).digest("hex")}"` } }) : new Response(null, { status: 404 });
102 };
103 try {
104 await writeFile(join(directory, "app-12345678.css"), "body{}");
105 await writeFile(join(directory, "shared-12345678.js"), shared);
106 for (let i = 0; i < 1000; i++) {
107 const manifest = `manifest-${i.toString(16).padStart(8, "0")}.js`;
108 if (live) await rm(join(directory, live));
109 await writeFile(join(directory, manifest), `window.manifest=${i}`);
110 apiCalls = 0;
111 await publishWebAssets(cwd, { auth: { authorization: "test" }, fetchImpl, seedLive: i > 0, onLine: () => {} });
112 assert.equal(apiCalls, i === 0 ? 7 : 4, `release ${i} should upload only its changed file and manifest`);
113 live = manifest;
114 }
115 assert.equal(objects.get("web-assets/assets/shared-12345678.js").toString(), shared);
116 assert.equal(objects.get("web-assets/assets/manifest-00000000.js").toString(), "window.manifest=0");
117 assert.equal(objects.get("web-assets/assets/manifest-000003e7.js").toString(), "window.manifest=999");
118 } finally {
119 assert.equal(dirname(resolve(cwd)), resolve(tmpdir()));
120 await rm(cwd, { recursive: true, force: true });
121 }
122});