/** Shared helpers for the sites e2e scripts. */ import { request as httpRequest } from "node:http"; import { gzipSync } from "node:zlib"; export const HTTP_OK = 200; const HTTP_CREATED = 201; /** Prints a progress line; e2e scripts report on stdout. */ export function log(line: string): void { process.stdout.write(`${line}\n`); } export const FORGE = new URL(process.env.SITES_E2E_URL ?? "http://localhost:3000"); export const DOMAIN = process.env.SITES_DOMAIN ?? "gitforstatic.localhost"; export type Reply = { status: number; headers: Record; body: string }; export function send( host: string, path: string, init: { method?: string; headers?: Record; body?: Uint8Array } = {}, ): Promise { return new Promise((resolve, reject) => { const req = httpRequest( { host: FORGE.hostname, port: FORGE.port || 80, path, method: init.method ?? "GET", headers: { host: `${host}:${FORGE.port}`, ...init.headers }, }, (res) => { const chunks: Buffer[] = []; res.on("data", (chunk: Buffer) => chunks.push(chunk)); res.on("end", () => resolve({ status: res.statusCode ?? 0, headers: res.headers, body: Buffer.concat(chunks).toString() }), ); }, ); req.on("error", reject); if (init.body) req.write(init.body); req.end(); }); } export function tar(files: Record): Uint8Array { const blocks: Uint8Array[] = []; const encoder = new TextEncoder(); for (const [name, content] of Object.entries(files)) { const data = encoder.encode(content); const header = new Uint8Array(512); const put = (value: string, offset: number) => header.set(encoder.encode(value), offset); put(name, 0); put("0000644\0", 100); put("0000000\0", 108); put("0000000\0", 116); put(`${data.byteLength.toString(8).padStart(11, "0")}\0`, 124); put( `${Math.floor(Date.now() / 1000) .toString(8) .padStart(11, "0")}\0`, 136, ); put(" ", 148); put("0", 156); put("ustar\0", 257); put("00", 263); const sum = header.reduce((total, byte) => total + byte, 0); put(`${sum.toString(8).padStart(6, "0")}\0 `, 148); blocks.push(header, data, new Uint8Array((512 - (data.byteLength % 512)) % 512)); } blocks.push(new Uint8Array(1024)); return gzipSync(Buffer.concat(blocks)); } export const WORKER = ` export default { async fetch(request, env) { const url = new URL(request.url); if (url.pathname === "/echo") { return Response.json({ cookie: request.headers.get("cookie"), user: request.headers.get("x-gitfor-user"), env: Object.keys(env) }); } if (url.pathname === "/set") { const headers = new Headers(); headers.append("set-cookie", "site_token=" + url.hostname + "; Path=/"); headers.append("set-cookie", "__Host-gitfor_site=forged; Path=/; Secure"); return new Response("ok", { headers }); } if (url.pathname === "/fetch") { try { const response = await fetch(url.searchParams.get("u")); return new Response("fetched " + response.status); } catch (error) { return new Response("blocked", { status: 599 }); } } return env.ASSETS.fetch(request); }, };`; export async function deployScratch(marker: string): Promise<{ host: string; claimUrl: string; claimToken: string }> { const body = tar({ "index.html": `

${marker}

`, "_worker.js": WORKER }); const reply = await send(DOMAIN, "/__scratch", { method: "POST", headers: { "content-type": "application/gzip", "content-length": String(body.byteLength) }, body, }); if (reply.status !== HTTP_CREATED) throw new Error(`scratch deploy failed: ${reply.status} ${reply.body}`); const json = JSON.parse(reply.body) as { url: string; claimUrl: string; claimToken: string }; return { host: new URL(json.url).hostname, claimUrl: json.claimUrl, claimToken: json.claimToken }; }