| 1 | import assert from "node:assert/strict"; |
| 2 | import { test } from "node:test"; |
| 3 | import { createHash } from "node:crypto"; |
| 4 | import { mkdtemp, mkdir, writeFile, rm } from "node:fs/promises"; |
| 5 | import { tmpdir } from "node:os"; |
| 6 | import { dirname, join, resolve } from "node:path"; |
| 7 | import { archiveAsset, archiveLiveAssets, assetReferences, publishWebAssets, readArchiveManifest, retryingFetch, verifyWebAssets } from "./web-assets.mjs"; |
| 8 | |
| 9 | test("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 | |
| 18 | test("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 | |
| 35 | test("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 | }); |
| 67 | |
| 68 | test("a missing live asset aborts the archive instead of saving an HTML error as CSS", async () => { |
| 69 | await assert.rejects( |
| 70 | archiveLiveAssets( |
| 71 | "https://example.com", |
| 72 | async () => assert.fail(), |
| 73 | async (url) => |
| 74 | new URL(url).pathname === "/" |
| 75 | ? new Response("/assets/app-12345678.css /assets/entry-12345678.js") |
| 76 | : new Response("not found", { headers: { "content-type": "text/html" } }), |
| 77 | ), |
| 78 | /unavailable/, |
| 79 | ); |
| 80 | }); |
| 81 | |
| 82 | test("a release manifest skips unchanged uploads without scanning deployment history", async () => { |
| 83 | const bytes = Buffer.from("body{}"); |
| 84 | const etag = createHash("md5").update(bytes).digest("hex"); |
| 85 | const index = await readArchiveManifest("https://provider/manifest", {}, async () => |
| 86 | Response.json({ version: 1, assets: [["web-assets/assets/app-12345678.css", `"${etag}"`]] }), |
| 87 | ); |
| 88 | await archiveAsset({ |
| 89 | name: "app-12345678.css", |
| 90 | bytes, |
| 91 | account: "a", |
| 92 | bucket: "b", |
| 93 | headers: {}, |
| 94 | index, |
| 95 | fetchImpl: async () => assert.fail("unchanged asset should not be uploaded"), |
| 96 | }); |
| 97 | await assert.rejects(archiveAsset({ name: "app-12345678.css", bytes: Buffer.from("different"), account: "a", bucket: "b", headers: {}, index }), /collision/); |
| 98 | }); |
| 99 | |
| 100 | test("provider rate limits wait and retry; persistent failure remains a failure", async () => { |
| 101 | const waits = []; |
| 102 | let calls = 0; |
| 103 | const response = await retryingFetch( |
| 104 | async () => new Response(null, { status: ++calls < 3 ? 429 : 200, headers: { "retry-after": "2" } }), |
| 105 | async (ms) => waits.push(ms), |
| 106 | )("https://provider"); |
| 107 | assert.equal(response.status, 200); |
| 108 | assert.deepEqual(waits, [2000, 2000]); |
| 109 | calls = 0; |
| 110 | assert.equal( |
| 111 | ( |
| 112 | await retryingFetch( |
| 113 | async () => { |
| 114 | calls++; |
| 115 | return new Response(null, { status: 503 }); |
| 116 | }, |
| 117 | async () => {}, |
| 118 | )("https://provider") |
| 119 | ).status, |
| 120 | 503, |
| 121 | ); |
| 122 | assert.equal(calls, 7); |
| 123 | }); |
| 124 | |
| 125 | test("cutover verifies old and new assets and rejects HTML fallback or wrong build", async () => { |
| 126 | const css = "body{}"; |
| 127 | const release = { |
| 128 | origin: "https://example.com", |
| 129 | manifest: "manifest-12345678.js", |
| 130 | samples: [["web-assets/assets/app-old12345.css", `"${createHash("md5").update(css).digest("hex")}"`]], |
| 131 | }; |
| 132 | const page = '<link href="/assets/app-12345678.css"><script src="/assets/manifest-12345678.js"></script>'; |
| 133 | await verifyWebAssets(release, async (url) => new Response(new URL(url).pathname === "/" ? page : css)); |
| 134 | await assert.rejects( |
| 135 | verifyWebAssets(release, async (url) => new Response(new URL(url).pathname === "/" ? page : "error", { headers: { "content-type": "text/html" } }), { |
| 136 | retryWaits: [], |
| 137 | }), |
| 138 | /asset check failed/, |
| 139 | ); |
| 140 | await assert.rejects( |
| 141 | verifyWebAssets(release, async () => new Response(page.replaceAll("12345678", "87654321")), { retryWaits: [] }), |
| 142 | /expected build/, |
| 143 | ); |
| 144 | }); |
| 145 | |
| 146 | test("cutover tolerates propagation delay but still requires the expected build and asset bytes", async () => { |
| 147 | const css = "body{}"; |
| 148 | const release = { |
| 149 | origin: "https://example.com", |
| 150 | manifest: "manifest-12345678.js", |
| 151 | samples: [["web-assets/assets/app-old12345.css", `"${createHash("md5").update(css).digest("hex")}"`]], |
| 152 | }; |
| 153 | const page = "/assets/app-12345678.css /assets/manifest-12345678.js"; |
| 154 | const waits = []; |
| 155 | const logs = []; |
| 156 | let pages = 0; |
| 157 | let assets = 0; |
| 158 | await verifyWebAssets( |
| 159 | release, |
| 160 | async (url) => { |
| 161 | if (new URL(url).pathname === "/") return new Response(++pages === 1 ? page.replaceAll("12345678", "87654321") : page); |
| 162 | return ++assets === 1 ? new Response("not propagated", { status: 404 }) : new Response(css); |
| 163 | }, |
| 164 | { |
| 165 | retryWaits: [1, 2], |
| 166 | wait: async (ms) => { |
| 167 | waits.push(ms); |
| 168 | }, |
| 169 | onLine: (line) => logs.push(line), |
| 170 | }, |
| 171 | ); |
| 172 | assert.deepEqual(waits, [1, 2]); |
| 173 | assert.equal(pages, 3); |
| 174 | assert.equal(assets, 2); |
| 175 | assert.match(logs[0], /expected manifest-12345678.js, observed manifest-87654321.js/); |
| 176 | assert.match(logs[1], /asset check failed/); |
| 177 | let attempts = 0; |
| 178 | await assert.rejects( |
| 179 | verifyWebAssets( |
| 180 | release, |
| 181 | async () => { |
| 182 | attempts++; |
| 183 | return new Response(page.replaceAll("12345678", "87654321")); |
| 184 | }, |
| 185 | { retryWaits: [1, 2], wait: async () => {} }, |
| 186 | ), |
| 187 | /expected build/, |
| 188 | ); |
| 189 | assert.equal(attempts, 3, "a persistently wrong build must fail after bounded retries"); |
| 190 | }); |
| 191 | |
| 192 | test("1000 successive releases keep old tabs' assets and perform bounded incremental archive work", async () => { |
| 193 | const cwd = await mkdtemp(join(tmpdir(), "g1t-web-releases-")); |
| 194 | const directory = join(cwd, "build/client/assets"); |
| 195 | await mkdir(directory, { recursive: true }); |
| 196 | await mkdir(join(cwd, "build/server"), { recursive: true }); |
| 197 | await writeFile( |
| 198 | join(cwd, "build/server/wrangler.json"), |
| 199 | JSON.stringify({ account_id: "a", r2_buckets: [{ binding: "DOWNLOADS", bucket_name: "b" }], routes: [{ custom_domain: true, pattern: "example.com" }] }), |
| 200 | ); |
| 201 | const objects = new Map(); |
| 202 | const shared = "export const shared = true;"; |
| 203 | let live = null; |
| 204 | let apiCalls = 0; |
| 205 | const fetchImpl = async (input, init = {}) => { |
| 206 | const url = new URL(input); |
| 207 | if (url.hostname === "example.com") { |
| 208 | assert.equal(url.pathname, "/", "already protected releases must not crawl all live chunks again"); |
| 209 | return new Response(`/assets/${live} /assets/app-12345678.css`); |
| 210 | } |
| 211 | apiCalls++; |
| 212 | assert.ok(url.pathname.includes("/objects/"), "must not list all previous releases"); |
| 213 | const key = url.pathname.split("/objects/")[1]; |
| 214 | if (init.method === "PUT") { |
| 215 | objects.set(key, Buffer.from(init.body)); |
| 216 | return Response.json({ success: true, result: { etag: createHash("md5").update(init.body).digest("hex") } }); |
| 217 | } |
| 218 | const bytes = objects.get(key); |
| 219 | return bytes ? new Response(bytes, { headers: { etag: `"${createHash("md5").update(bytes).digest("hex")}"` } }) : new Response(null, { status: 404 }); |
| 220 | }; |
| 221 | try { |
| 222 | await writeFile(join(directory, "app-12345678.css"), "body{}"); |
| 223 | await writeFile(join(directory, "shared-12345678.js"), shared); |
| 224 | for (let i = 0; i < 1000; i++) { |
| 225 | const manifest = `manifest-${i.toString(16).padStart(8, "0")}.js`; |
| 226 | if (live) await rm(join(directory, live)); |
| 227 | await writeFile(join(directory, manifest), `window.manifest=${i}`); |
| 228 | apiCalls = 0; |
| 229 | await publishWebAssets(cwd, { auth: { authorization: "test" }, fetchImpl, seedLive: i > 0, onLine: () => {} }); |
| 230 | assert.equal(apiCalls, i === 0 ? 7 : 4, `release ${i} should upload only its changed file and manifest`); |
| 231 | live = manifest; |
| 232 | } |
| 233 | assert.equal(objects.get("web-assets/assets/shared-12345678.js").toString(), shared); |
| 234 | assert.equal(objects.get("web-assets/assets/manifest-00000000.js").toString(), "window.manifest=0"); |
| 235 | assert.equal(objects.get("web-assets/assets/manifest-000003e7.js").toString(), "window.manifest=999"); |
| 236 | } finally { |
| 237 | assert.equal(dirname(resolve(cwd)), resolve(tmpdir())); |
| 238 | await rm(cwd, { recursive: true, force: true }); |
| 239 | } |
| 240 | }); |