1// Minimal long-lived dev server for the AWS StaticSite fixture: serves
2// ./site over HTTP on an ephemeral port and prints its own localhost URL,
3// which `Command.Dev` extracts from stdout and surfaces as the site's dev
4// `url`.
5//
6// `/__dev-env` echoes the DEV_MARKER environment variable so tests can
7// prove that `dev.env` reached the spawned child process.
8//
9// Plain .mjs (not .ts) so the test project's tsc never type-checks it —
10// it only ever runs under bun as a fixture child process.
11const server = Bun.serve({
12 port: 0,
13 fetch: async (request) => {
14 const { pathname } = new URL(request.url);
15 if (pathname === "/__dev-env") {
16 return Response.json({ marker: process.env.DEV_MARKER ?? null });
17 }
18 // Echoes exactly what the origin received, so a test can compare the
19 // request an emulated CloudFront edge forwarded against the request the
20 // TestFunction API says the same function produced.
21 if (pathname.endsWith("/__echo")) {
22 return Response.json({
23 marker: process.env.DEV_MARKER ?? null,
24 path: pathname,
25 headers: Object.fromEntries(request.headers),
26 });
27 }
28 // Directory-style paths resolve to their index page, so a site mounted
29 // under a Router path prefix (`/docs/`) serves like a real static host.
30 const file = Bun.file(
31 `${process.cwd()}/site${pathname.endsWith("/") ? `${pathname}index.html` : pathname}`,
32 );
33 return (await file.exists())
34 ? new Response(file)
35 : new Response("not found", { status: 404 });
36 },
37});
38
39console.log(`StaticSite fixture dev server: http://localhost:${server.port}`);