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); 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); } async function checkHtmlAssets(relativeEntry, localPath, publicPath) { const html = await readFile(localPath, "utf8"); 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"; await checkResource(url, expectedType, relativeEntry + " -> " + asset); } } 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"); 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"); for (const asset of await listFiles(assetRoot)) { const extension = path.extname(asset).toLowerCase(); const expectedType = contentTypes.get(extension); if (!expectedType) continue; await checkResource( baseUrl + "/assets/" + asset.split("/").map(encodeURIComponent).join("/"), expectedType, "static asset " + asset, ); } for (const entry of await readdir(path.join(root, "seed-media"))) { await checkResource( baseUrl + "/media/seed/" + encodeURIComponent(entry), "image/jpeg", "seed media " + entry, ); } console.log("public release verified: admin, preview, assets, and seed media");