|
| 1 | +// fallow-ignore-file unused-file -- loaded at runtime (spawned/injected by local-docs.ts), never statically imported |
| 2 | +/** |
| 3 | + * Standalone docs content GraphQL API for `search_docs`. |
| 4 | + * |
| 5 | + * Serves the docs app's own route handler (apps/docs/app/api/graphql/route.ts |
| 6 | + * in a supabase/supabase checkout) over plain node:http — no Next server. |
| 7 | + * Launched by `pnpm local docs api` with the docs checkout's tsx so the |
| 8 | + * route's TS + tsconfig conditions resolve; DOCS_ROUTE_PATH points at the |
| 9 | + * checkout, PORT picks the listen port. |
| 10 | + */ |
| 11 | +import { createServer } from 'node:http'; |
| 12 | +import { pathToFileURL } from 'node:url'; |
| 13 | + |
| 14 | +const routePath = process.env.DOCS_ROUTE_PATH; |
| 15 | +if (!routePath) { |
| 16 | + console.error( |
| 17 | + 'DOCS_ROUTE_PATH not set — run this through `pnpm local docs api`' |
| 18 | + ); |
| 19 | + process.exit(1); |
| 20 | +} |
| 21 | +// The docs checkout location is user-supplied at runtime; a static import |
| 22 | +// cannot name it. |
| 23 | +const route = await import(pathToFileURL(routePath).href); |
| 24 | +const handlers: Record<string, (req: Request) => Promise<Response>> = { |
| 25 | + GET: route.GET, |
| 26 | + OPTIONS: route.OPTIONS, |
| 27 | + POST: route.POST, |
| 28 | +}; |
| 29 | +const port = Number(process.env.PORT ?? 3001); |
| 30 | + |
| 31 | +createServer(async (incoming, outgoing) => { |
| 32 | + const url = new URL( |
| 33 | + incoming.url ?? '/', |
| 34 | + `http://${incoming.headers.host ?? `127.0.0.1:${port}`}` |
| 35 | + ); |
| 36 | + const handler = handlers[incoming.method ?? '']; |
| 37 | + if (url.pathname !== '/docs/api/graphql' || !handler) { |
| 38 | + outgoing.writeHead(404).end(); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + const headers = new Headers(); |
| 43 | + for (const [name, value] of Object.entries(incoming.headers)) { |
| 44 | + if (Array.isArray(value)) |
| 45 | + for (const item of value) headers.append(name, item); |
| 46 | + else if (value !== undefined) headers.set(name, value); |
| 47 | + } |
| 48 | + |
| 49 | + const chunks: Buffer[] = []; |
| 50 | + for await (const chunk of incoming) chunks.push(Buffer.from(chunk)); |
| 51 | + const body = |
| 52 | + incoming.method === 'GET' || incoming.method === 'HEAD' |
| 53 | + ? undefined |
| 54 | + : Buffer.concat(chunks).toString('utf8'); |
| 55 | + const response = await handler( |
| 56 | + new Request(url, { method: incoming.method, headers, body }) |
| 57 | + ); |
| 58 | + |
| 59 | + outgoing.writeHead( |
| 60 | + response.status, |
| 61 | + Object.fromEntries(response.headers.entries()) |
| 62 | + ); |
| 63 | + outgoing.end(Buffer.from(await response.arrayBuffer())); |
| 64 | + // Bind every interface, advertise loopback — same rule as platform-lite in |
| 65 | + // tools mode (see run-eval.ts: sandboxed CLI agents run their MCP servers |
| 66 | + // INSIDE the container and reach host-side services via |
| 67 | + // host.docker.internal, which arrives on the host's bridge interface, not |
| 68 | + // loopback; a 127.0.0.1-only listener refuses those connections). |
| 69 | +}).listen(port, '0.0.0.0', () => { |
| 70 | + console.log(`Docs content API: http://127.0.0.1:${port}/docs/api/graphql`); |
| 71 | +}); |
0 commit comments