|
| 1 | +#!/usr/bin/env -S node --import tsx |
| 2 | +// Regenerates ui/config/control-ui-boot-modules.json: the measured module set |
| 3 | +// the default Control UI boot flow loads lazily. Boots the built dist bundle |
| 4 | +// against the mocked Gateway, records every JS chunk fetched through chat |
| 5 | +// readiness, and unions their sourcemap sources into canonical manifest keys. |
| 6 | +// Requires a current `pnpm ui:build` output in dist/control-ui. |
| 7 | +import fs from "node:fs"; |
| 8 | +import http from "node:http"; |
| 9 | +import path from "node:path"; |
| 10 | +import { fileURLToPath } from "node:url"; |
| 11 | +import { chromium } from "playwright"; |
| 12 | +import { controlUiBootManifestKey } from "../ui/config/control-ui-chunking.ts"; |
| 13 | +import { installMockGateway } from "../ui/src/test-helpers/control-ui-e2e.ts"; |
| 14 | + |
| 15 | +const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); |
| 16 | +const distDir = path.join(repoRoot, "dist", "control-ui"); |
| 17 | +const manifestPath = path.join(repoRoot, "ui", "config", "control-ui-boot-modules.json"); |
| 18 | +const SETTLE_MS = 3_000; |
| 19 | +const READY_TIMEOUT_MS = 60_000; |
| 20 | + |
| 21 | +const mime: Record<string, string> = { |
| 22 | + ".html": "text/html", |
| 23 | + ".js": "text/javascript", |
| 24 | + ".css": "text/css", |
| 25 | + ".json": "application/json", |
| 26 | + ".svg": "image/svg+xml", |
| 27 | + ".map": "application/json", |
| 28 | + ".webmanifest": "application/manifest+json", |
| 29 | +}; |
| 30 | + |
| 31 | +function serveDist(): Promise<{ baseUrl: string; close: () => void }> { |
| 32 | + const server = http.createServer((req, res) => { |
| 33 | + const urlPath = new URL(req.url ?? "/", "http://localhost").pathname; |
| 34 | + if (urlPath === "/control-ui-config.json") { |
| 35 | + res.setHeader("Content-Type", "application/json"); |
| 36 | + res.end(JSON.stringify({ basePath: "/", assistantName: "", assistantAvatar: "" })); |
| 37 | + return; |
| 38 | + } |
| 39 | + let filePath = path.join(distDir, urlPath === "/" ? "index.html" : urlPath.slice(1)); |
| 40 | + if (!fs.existsSync(filePath) || fs.statSync(filePath).isDirectory()) { |
| 41 | + filePath = path.join(distDir, "index.html"); |
| 42 | + } |
| 43 | + res.setHeader("Content-Type", mime[path.extname(filePath)] ?? "application/octet-stream"); |
| 44 | + res.end(fs.readFileSync(filePath)); |
| 45 | + }); |
| 46 | + return new Promise((resolve) => { |
| 47 | + server.listen(0, "127.0.0.1", () => { |
| 48 | + const address = server.address(); |
| 49 | + if (address === null || typeof address !== "object") { |
| 50 | + throw new Error("Control UI boot manifest server has no port"); |
| 51 | + } |
| 52 | + resolve({ |
| 53 | + baseUrl: `http://127.0.0.1:${address.port}`, |
| 54 | + close: () => server.close(), |
| 55 | + }); |
| 56 | + }); |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +function readDistBuildId(): string { |
| 61 | + const swSource = fs.readFileSync(path.join(distDir, "sw.js"), "utf8"); |
| 62 | + const buildId = /EMBEDDED_CACHE_VERSION = "([^"]+)"/.exec(swSource)?.[1]; |
| 63 | + if (!buildId) { |
| 64 | + throw new Error("Control UI boot manifest cannot read the dist build id from sw.js"); |
| 65 | + } |
| 66 | + return buildId; |
| 67 | +} |
| 68 | + |
| 69 | +async function collectBootChunkPaths(baseUrl: string): Promise<Set<string>> { |
| 70 | + const browser = await chromium.launch(); |
| 71 | + try { |
| 72 | + const page = await browser.newPage(); |
| 73 | + const chunkPaths = new Set<string>(); |
| 74 | + page.on("request", (request) => { |
| 75 | + const { pathname } = new URL(request.url()); |
| 76 | + if (pathname.startsWith("/assets/") && pathname.endsWith(".js")) { |
| 77 | + chunkPaths.add(pathname); |
| 78 | + } |
| 79 | + }); |
| 80 | + await installMockGateway(page, { serverBuildId: readDistBuildId() }); |
| 81 | + await page.goto(`${baseUrl}/chat`, { waitUntil: "commit" }); |
| 82 | + // Chat readiness proves the boot flow completed instead of stalling on an |
| 83 | + // error surface; a manifest captured from a broken boot would be garbage. |
| 84 | + await page |
| 85 | + .locator(".agent-chat__composer-combobox textarea") |
| 86 | + .waitFor({ timeout: READY_TIMEOUT_MS }); |
| 87 | + await page.waitForTimeout(SETTLE_MS); |
| 88 | + return chunkPaths; |
| 89 | + } finally { |
| 90 | + await browser.close(); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +function manifestKeysForChunks(chunkPaths: Iterable<string>): string[] { |
| 95 | + const keys = new Set<string>(); |
| 96 | + for (const chunkPath of chunkPaths) { |
| 97 | + const mapPath = path.join(distDir, `${chunkPath}.map`); |
| 98 | + if (!fs.existsSync(mapPath)) { |
| 99 | + // Facade chunks for dynamic entries can omit maps; their modules are |
| 100 | + // covered by the chunks that carry the actual code. |
| 101 | + continue; |
| 102 | + } |
| 103 | + const map = JSON.parse(fs.readFileSync(mapPath, "utf8")) as { sources?: string[] }; |
| 104 | + for (const source of map.sources ?? []) { |
| 105 | + keys.add(controlUiBootManifestKey(path.resolve(path.join(distDir, "assets"), source))); |
| 106 | + } |
| 107 | + } |
| 108 | + return [...keys].toSorted(); |
| 109 | +} |
| 110 | + |
| 111 | +async function main(): Promise<void> { |
| 112 | + if (!fs.existsSync(path.join(distDir, "index.html"))) { |
| 113 | + throw new Error(`No Control UI build at ${distDir}; run \`pnpm ui:build\` first`); |
| 114 | + } |
| 115 | + const server = await serveDist(); |
| 116 | + try { |
| 117 | + const chunkPaths = await collectBootChunkPaths(server.baseUrl); |
| 118 | + const keys = manifestKeysForChunks(chunkPaths); |
| 119 | + if (keys.length < 100) { |
| 120 | + throw new Error(`Boot capture looks truncated: only ${keys.length} modules recorded`); |
| 121 | + } |
| 122 | + fs.writeFileSync(manifestPath, `${JSON.stringify(keys, null, 1)}\n`); |
| 123 | + console.log( |
| 124 | + `control-ui-boot-manifest: ${chunkPaths.size} boot chunks -> ${keys.length} modules -> ${path.relative(repoRoot, manifestPath)}`, |
| 125 | + ); |
| 126 | + } finally { |
| 127 | + server.close(); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +main().catch((error: unknown) => { |
| 132 | + console.error(error); |
| 133 | + console.error("[control-ui-boot-manifest] FAILED (exit 1)"); |
| 134 | + process.exit(1); |
| 135 | +}); |
0 commit comments