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.

260 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(
11 assetReferences(
12 '<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)',
13 ),
14 ["app-12345678.css", "chunk-abcdefgh.js", "font-12345678.woff2"],
15 );
16});
17
18test("archive the old release's transitive chunks before replacing it", async () => {
19 const responses = {
20 "/": '<link href="/assets/app-12345678.css"><script src="/assets/entry-12345678.js"></script>',
21 "/assets/app-12345678.css": "body{color:red}",
22 "/assets/entry-12345678.js": 'import "./lazy-12345678.js"; import "./lazy-12345678.js"',
23 "/assets/lazy-12345678.js": 'import "./entry-12345678.js"',
24 };
25 const saved = [];
26 const total = await archiveLiveAssets(
27 "https://example.com",
28 async (name) => saved.push(name),
29 async (url) => new Response(responses[new URL(url).pathname]),
30 );
31 assert.equal(total, 3);
32 assert.deepEqual(saved.sort(), ["app-12345678.css", "entry-12345678.js", "lazy-12345678.js"]);
33});
34
35test("archive failures block cutover and immutable filename collisions are refused", async () => {
36 const args = { name: "app-12345678.css", bytes: Buffer.from("body{}"), account: "account", bucket: "bucket", headers: {} };
37 const calls = [];
38 await assert.rejects(
39 archiveAsset({
40 ...args,
41 fetchImpl: async (_url, init) => {
42 calls.push(init.method ?? "GET");
43 return new Response(null, { status: init.method === "PUT" ? 503 : 404 });
44 },
45 }),
46 /upload failed/,
47 );
48 assert.deepEqual(calls, ["GET", "PUT"]);
49 await assert.rejects(
50 archiveAsset({
51 ...args,
52 fetchImpl: async (_url, init) =>
53 init.method === "PUT" ? Response.json({ success: true, result: { etag: "incorrect" } }) : new Response(null, { status: 404 }),
54 }),
55 /checksum mismatch/,
56 );
57 await assert.rejects(archiveAsset({ ...args, fetchImpl: async () => new Response(null, { headers: { etag: '"different"' } }) }), /collision/);
58 const etag = `"${createHash("md5").update(args.bytes).digest("hex")}"`;
59 await archiveAsset({
60 ...args,
61 fetchImpl: async (_url, init) => {
62 assert.notEqual(init.method, "PUT");
63 return new Response(null, { headers: { etag } });
64 },
65 });
66 // R2's REST gateway answers a GET with a weak ETag: the same object.
67 await archiveAsset({ ...args, fetchImpl: async () => new Response(null, { headers: { etag: `W/${etag}` } }) });
68 await assert.rejects(archiveAsset({ ...args, fetchImpl: async () => new Response(null, { headers: { etag: 'W/"different"' } }) }), /collision/);
69});
70
71test("an upload whose receipt can't be read is confirmed by the stored object", async () => {
72 const args = { name: "app-12345678.css", bytes: Buffer.from("body{}"), account: "account", bucket: "bucket", headers: {} };
73 const etag = `W/"${createHash("md5").update(args.bytes).digest("hex")}"`;
74 const landed = (stored) => {
75 let put = false;
76 return async (_url, init) => {
77 if (init.method === "PUT") {
78 put = true;
79 return new Response("", { status: 200 });
80 }
81 return put ? new Response(null, { headers: { etag: stored } }) : new Response(null, { status: 404 });
82 };
83 };
84 await archiveAsset({ ...args, fetchImpl: landed(etag) });
85 await assert.rejects(archiveAsset({ ...args, fetchImpl: landed('W/"different"') }), /Invalid asset upload receipt/);
86});
87
88test("a missing live asset aborts the archive instead of saving an HTML error as CSS", async () => {
89 await assert.rejects(
90 archiveLiveAssets(
91 "https://example.com",
92 async () => assert.fail(),
93 async (url) =>
94 new URL(url).pathname === "/"
95 ? new Response("/assets/app-12345678.css /assets/entry-12345678.js")
96 : new Response("not found", { headers: { "content-type": "text/html" } }),
97 ),
98 /unavailable/,
99 );
100});
101
102test("a release manifest skips unchanged uploads without scanning deployment history", async () => {
103 const bytes = Buffer.from("body{}");
104 const etag = createHash("md5").update(bytes).digest("hex");
105 const index = await readArchiveManifest("https://provider/manifest", {}, async () =>
106 Response.json({ version: 1, assets: [["web-assets/assets/app-12345678.css", `"${etag}"`]] }),
107 );
108 await archiveAsset({
109 name: "app-12345678.css",
110 bytes,
111 account: "a",
112 bucket: "b",
113 headers: {},
114 index,
115 fetchImpl: async () => assert.fail("unchanged asset should not be uploaded"),
116 });
117 await assert.rejects(archiveAsset({ name: "app-12345678.css", bytes: Buffer.from("different"), account: "a", bucket: "b", headers: {}, index }), /collision/);
118});
119
120test("provider rate limits wait and retry; persistent failure remains a failure", async () => {
121 const waits = [];
122 let calls = 0;
123 const response = await retryingFetch(
124 async () => new Response(null, { status: ++calls < 3 ? 429 : 200, headers: { "retry-after": "2" } }),
125 async (ms) => waits.push(ms),
126 )("https://provider");
127 assert.equal(response.status, 200);
128 assert.deepEqual(waits, [2000, 2000]);
129 calls = 0;
130 assert.equal(
131 (
132 await retryingFetch(
133 async () => {
134 calls++;
135 return new Response(null, { status: 503 });
136 },
137 async () => {},
138 )("https://provider")
139 ).status,
140 503,
141 );
142 assert.equal(calls, 7);
143});
144
145test("cutover verifies old and new assets and rejects HTML fallback or wrong build", async () => {
146 const css = "body{}";
147 const release = {
148 origin: "https://example.com",
149 manifest: "manifest-12345678.js",
150 samples: [["web-assets/assets/app-old12345.css", `"${createHash("md5").update(css).digest("hex")}"`]],
151 };
152 const page = '<link href="/assets/app-12345678.css"><script src="/assets/manifest-12345678.js"></script>';
153 await verifyWebAssets(release, async (url) => new Response(new URL(url).pathname === "/" ? page : css));
154 await assert.rejects(
155 verifyWebAssets(release, async (url) => new Response(new URL(url).pathname === "/" ? page : "error", { headers: { "content-type": "text/html" } }), {
156 retryWaits: [],
157 }),
158 /asset check failed/,
159 );
160 await assert.rejects(
161 verifyWebAssets(release, async () => new Response(page.replaceAll("12345678", "87654321")), { retryWaits: [] }),
162 /expected build/,
163 );
164});
165
166test("cutover tolerates propagation delay but still requires the expected build and asset bytes", async () => {
167 const css = "body{}";
168 const release = {
169 origin: "https://example.com",
170 manifest: "manifest-12345678.js",
171 samples: [["web-assets/assets/app-old12345.css", `"${createHash("md5").update(css).digest("hex")}"`]],
172 };
173 const page = "/assets/app-12345678.css /assets/manifest-12345678.js";
174 const waits = [];
175 const logs = [];
176 let pages = 0;
177 let assets = 0;
178 await verifyWebAssets(
179 release,
180 async (url) => {
181 if (new URL(url).pathname === "/") return new Response(++pages === 1 ? page.replaceAll("12345678", "87654321") : page);
182 return ++assets === 1 ? new Response("not propagated", { status: 404 }) : new Response(css);
183 },
184 {
185 retryWaits: [1, 2],
186 wait: async (ms) => {
187 waits.push(ms);
188 },
189 onLine: (line) => logs.push(line),
190 },
191 );
192 assert.deepEqual(waits, [1, 2]);
193 assert.equal(pages, 3);
194 assert.equal(assets, 2);
195 assert.match(logs[0], /expected manifest-12345678.js, observed manifest-87654321.js/);
196 assert.match(logs[1], /asset check failed/);
197 let attempts = 0;
198 await assert.rejects(
199 verifyWebAssets(
200 release,
201 async () => {
202 attempts++;
203 return new Response(page.replaceAll("12345678", "87654321"));
204 },
205 { retryWaits: [1, 2], wait: async () => {} },
206 ),
207 /expected build/,
208 );
209 assert.equal(attempts, 3, "a persistently wrong build must fail after bounded retries");
210});
211
212test("1000 successive releases keep old tabs' assets and perform bounded incremental archive work", async () => {
213 const cwd = await mkdtemp(join(tmpdir(), "g1t-web-releases-"));
214 const directory = join(cwd, "build/client/assets");
215 await mkdir(directory, { recursive: true });
216 await mkdir(join(cwd, "build/server"), { recursive: true });
217 await writeFile(
218 join(cwd, "build/server/wrangler.json"),
219 JSON.stringify({ account_id: "a", r2_buckets: [{ binding: "DOWNLOADS", bucket_name: "b" }], routes: [{ custom_domain: true, pattern: "example.com" }] }),
220 );
221 const objects = new Map();
222 const shared = "export const shared = true;";
223 let live = null;
224 let apiCalls = 0;
225 const fetchImpl = async (input, init = {}) => {
226 const url = new URL(input);
227 if (url.hostname === "example.com") {
228 assert.equal(url.pathname, "/", "already protected releases must not crawl all live chunks again");
229 return new Response(`/assets/${live} /assets/app-12345678.css`);
230 }
231 apiCalls++;
232 assert.ok(url.pathname.includes("/objects/"), "must not list all previous releases");
233 const key = url.pathname.split("/objects/")[1];
234 if (init.method === "PUT") {
235 objects.set(key, Buffer.from(init.body));
236 return Response.json({ success: true, result: { etag: createHash("md5").update(init.body).digest("hex") } });
237 }
238 const bytes = objects.get(key);
239 return bytes ? new Response(bytes, { headers: { etag: `"${createHash("md5").update(bytes).digest("hex")}"` } }) : new Response(null, { status: 404 });
240 };
241 try {
242 await writeFile(join(directory, "app-12345678.css"), "body{}");
243 await writeFile(join(directory, "shared-12345678.js"), shared);
244 for (let i = 0; i < 1000; i++) {
245 const manifest = `manifest-${i.toString(16).padStart(8, "0")}.js`;
246 if (live) await rm(join(directory, live));
247 await writeFile(join(directory, manifest), `window.manifest=${i}`);
248 apiCalls = 0;
249 await publishWebAssets(cwd, { auth: { authorization: "test" }, fetchImpl, seedLive: i > 0, onLine: () => {} });
250 assert.equal(apiCalls, i === 0 ? 7 : 4, `release ${i} should upload only its changed file and manifest`);
251 live = manifest;
252 }
253 assert.equal(objects.get("web-assets/assets/shared-12345678.js").toString(), shared);
254 assert.equal(objects.get("web-assets/assets/manifest-00000000.js").toString(), "window.manifest=0");
255 assert.equal(objects.get("web-assets/assets/manifest-000003e7.js").toString(), "window.manifest=999");
256 } finally {
257 assert.equal(dirname(resolve(cwd)), resolve(tmpdir()));
258 await rm(cwd, { recursive: true, force: true });
259 }
260});