import { createHash } from "node:crypto"; import { readFile } from "node:fs/promises"; import { createServer } from "node:http"; import { pathToFileURL } from "node:url"; import yazl from "yazl"; /** 显式启动的开发 fixture 服务,不由生产 App 自动开启。 */ export async function createCloudContentMockServer({ port = 0 } = {}) { const video = Buffer.from( await readFile( new URL("../../ui/src/assets/cloud-content/preview.webm.base64", import.meta.url), "utf8", ), "base64", ); const lottie = { v: "5.13.0", fr: 30, ip: 0, op: 60, w: 400, h: 300, nm: "Cloud content demo", ddd: 0, assets: [], layers: [ { ddd: 0, ind: 1, ty: 4, nm: "Rotating square", sr: 1, ks: { o: { a: 0, k: 100 }, r: { a: 1, k: [ { t: 0, s: [0], e: [360], i: { x: [0.67], y: [1] }, o: { x: [0.33], y: [0] } }, { t: 60, s: [360] }, ], }, p: { a: 0, k: [200, 150, 0] }, a: { a: 0, k: [0, 0, 0] }, s: { a: 0, k: [100, 100, 100] }, }, ao: 0, shapes: [ { ty: "rc", d: 1, s: { a: 0, k: [110, 110] }, p: { a: 0, k: [0, 0] }, r: { a: 0, k: 14 }, }, { ty: "fl", c: { a: 0, k: [0.2, 0.6, 1, 1] }, o: { a: 0, k: 100 }, r: 1 }, ], ip: 0, op: 60, st: 0, bm: 0, }, ], }; const hero = await readFile( new URL("../../ui/src/assets/cloud-content/weekend-plan-hero.html", import.meta.url), ); const fixture = JSON.parse( await readFile( new URL( "../../ui/src/components/cloud-content-dialog/mocks/weekendPlanResultDialog.zh-CN.json", import.meta.url, ), "utf8", ), ); const zip = new yazl.ZipFile(); zip.addBuffer(hero, "index.html", { mtime: new Date("2026-01-01T00:00:00Z") }); zip.end(); const chunks = []; for await (const chunk of zip.outputStream) chunks.push(chunk); const bytes = Buffer.concat(chunks); const sha256 = createHash("sha256").update(bytes).digest("hex"); const requests = []; let baseUrl; const server = createServer((req, res) => { requests.push(req.url ?? "/"); res.setHeader("Access-Control-Allow-Origin", "*"); res.setHeader("Cache-Control", "no-store"); res.setHeader("X-Content-Type-Options", "nosniff"); if (req.method !== "GET") { res.writeHead(405).end(); return; } const url = new URL(req.url ?? "/", baseUrl); if (url.pathname === "/preview.webm") { const range = req.headers.range?.match(/^bytes=(\d+)-(\d*)$/u); if (range) { const start = Number(range[1]); const end = Math.min(video.length - 1, range[2] ? Number(range[2]) : video.length - 1); if (start > end || start >= video.length) { res.writeHead(416).end(); return; } res .writeHead(206, { "Content-Type": "video/webm", "Accept-Ranges": "bytes", "Content-Range": `bytes ${start}-${end}/${video.length}`, "Content-Length": end - start + 1, }) .end(video.subarray(start, end + 1)); } else res .writeHead(200, { "Content-Type": "video/webm", "Content-Length": video.length, "Accept-Ranges": "bytes", }) .end(video); return; } if (url.pathname === "/preview.json") { res.writeHead(200, { "Content-Type": "application/json" }).end(JSON.stringify(lottie)); return; } if (["/preview-light.svg", "/preview-dark.svg"].includes(url.pathname)) { const dark = url.pathname.includes("dark"); res .writeHead(200, { "Content-Type": "image/svg+xml", "Content-Security-Policy": "default-src 'none'; style-src 'unsafe-inline'", }) .end( ``, ); return; } if (url.pathname === `/bundles/${sha256}.zip`) { res .writeHead(200, { "Content-Type": "application/zip", "Content-Length": bytes.length }) .end(bytes); return; } if (url.pathname === "/dialog") { const payload = structuredClone(fixture); payload.dialog.hero.bundle = { format: "zip", url: `${baseUrl}/bundles/${sha256}.zip`, entry: "index.html", sha256, sizeBytes: bytes.length, }; payload.dialog.description = { format: "html", text: "
GLM-5.3-Flash 已可使用。
这是本地 HTTP mock,不会领取真实套餐。
", }; if (url.searchParams.get("locale") === "en-US") { payload.locale = "en-US"; payload.dialog.title = "Your Weekend Plan is ready"; payload.dialog.description.text = "GLM-5.3-Flash is ready to use.
This local HTTP mock does not claim a real plan.
"; payload.dialog.buttons[0].label = "Model settings"; payload.dialog.buttons[1].label = "Copy & share"; payload.dialog.hero.data.endsAtPrefix = "Valid until"; payload.dialog.hero.data.replayLabel = "Replay"; payload.dialog.hero.data.benefits = ["GLM-5.3-Flash daily quota"]; payload.actions["copy-share"].text = "Try the new ZCode experience."; } const english = payload.locale === "en-US"; const scenario = url.searchParams.get("scenario"); if (scenario === "feature") { payload.id = "new-feature-dialog"; payload.kind = "feature"; payload.dialog.title = english ? "Meet the new feature" : "认识新功能"; payload.dialog.description.text = english ? "A new feature is ready.
新功能已准备就绪。