import assert from "node:assert/strict"; import { readdir, readFile } from "node:fs/promises"; import path from "node:path"; const baseUrl = (process.env.PUBLIC_BASE_URL ?? "https://wechat.app.que01.top").replace(/\/+$/, ""); const root = path.resolve(process.argv[2] ?? "."); const contentTypes = new Map([ [".avif", "image/avif"], [".css", "text/css"], [".gif", "image/gif"], [".ico", "image/"], [".jpeg", "image/jpeg"], [".jpg", "image/jpeg"], [".js", "javascript"], [".json", "application/json"], [".png", "image/png"], [".svg", "image/svg+xml"], [".webp", "image/webp"], [".woff", "font/woff"], [".woff2", "font/woff2"], ]); async function checkResource(url, expectedType, label) { const response = await fetch(url, { signal: AbortSignal.timeout(10000) }); const contentType = response.headers.get("content-type") ?? ""; assert.equal(response.status, 200, label + " returned " + response.status); assert.ok(contentType.includes(expectedType), label + " has content type " + contentType); await response.body?.cancel(); } async function checkHtmlAssets(relativeEntry, localPath, publicPath) { const html = await readFile(localPath, "utf8"); const checks = []; for (const match of html.matchAll(/(?:src|href)=["']([^"']+)["']/g)) { const asset = match[1]; if (!/\.(?:js|css)(?:[?#]|$)/.test(asset)) continue; const url = new URL(asset, baseUrl + publicPath); const expectedType = /\.css(?:[?#]|$)/.test(url.pathname) ? "text/css" : "javascript"; checks.push(checkResource(url, expectedType, relativeEntry + " -> " + asset)); } await Promise.all(checks); } async function listFiles(directory, prefix = "") { const files = []; for (const entry of await readdir(directory, { withFileTypes: true })) { const relative = path.posix.join(prefix, entry.name); if (entry.isDirectory()) files.push(...(await listFiles(path.join(directory, entry.name), relative))); else if (entry.isFile()) files.push(relative); } return files; } const apiHealthResponse = await fetch(baseUrl + "/api/health", { signal: AbortSignal.timeout(10000), }); assert.equal(apiHealthResponse.status, 200, "public API health check failed"); assert.match(apiHealthResponse.headers.get("content-type") ?? "", /application\/json/); assert.equal((await apiHealthResponse.json()).status, "ok"); for (const [entry, localFile, publicPath] of [ ["admin", path.join(root, "apps/admin/dist/index.html"), "/admin/"], [ "template preview", path.join(root, "apps/admin/dist/template-preview/index.html"), "/template-preview/", ], ]) { await checkResource(baseUrl + publicPath, "text/html", entry); await checkHtmlAssets(entry, localFile, publicPath); } const assetRoot = path.join(root, "apps/admin/dist/assets"); await Promise.all( (await listFiles(assetRoot)).map(async (asset) => { const extension = path.extname(asset).toLowerCase(); const expectedType = contentTypes.get(extension); if (!expectedType) return; await checkResource( baseUrl + "/assets/" + asset.split("/").map(encodeURIComponent).join("/"), expectedType, "static asset " + asset, ); }), ); await Promise.all( (await readdir(path.join(root, "seed-media"))).map((entry) => checkResource( baseUrl + "/media/seed/" + encodeURIComponent(entry), "image/jpeg", "seed media " + entry, ), ), ); console.log("public release verified: admin, preview, assets, and seed media");