{"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../../src/api/server.ts"],"names":[],"mappings":"AACA,OAAO,EAAgB,KAAK,MAAM,EAAuB,MAAM,WAAW,CAAC;AAI3E,OAAO,KAAK,EAAgB,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEnE,MAAM,WAAW,iBAAiB;IACjC,KAAK,EAAE,iBAAiB,CAAC;IACzB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACb;AAiBD,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,iBAAiB,GAAG,MAAM,CA6CnE","sourcesContent":["import { readFileSync } from \"node:fs\";\nimport { createServer, type Server, type ServerResponse } from \"node:http\";\nimport { dirname, join } from \"node:path\";\nimport { fileURLToPath } from \"node:url\";\nimport { frontier, unverifiedResults } from \"../graph.js\";\nimport type { Contribution, ContributionStore } from \"../store.js\";\n\nexport interface AgoraServerConfig {\n\tstore: ContributionStore;\n\tprojectDir: string;\n\thost: string;\n\tport: number;\n}\n\nfunction sendJson(res: ServerResponse, status: number, body: unknown): void {\n\tconst payload = JSON.stringify(body);\n\tres.writeHead(status, {\n\t\t\"content-type\": \"application/json; charset=utf-8\",\n\t\t\"content-length\": Buffer.byteLength(payload),\n\t\t\"cache-control\": \"no-store\",\n\t});\n\tres.end(payload);\n}\n\nfunction readSpec(): unknown {\n\tconst here = dirname(fileURLToPath(import.meta.url));\n\treturn JSON.parse(readFileSync(join(here, \"..\", \"..\", \"openapi.json\"), \"utf8\")) as unknown;\n}\n\nexport function createAgoraServer(config: AgoraServerConfig): Server {\n\tconst startedAt = Date.now();\n\treturn createServer((req, res) => {\n\t\tconst url = new URL(req.url ?? \"/\", `http://${req.headers.host ?? \"localhost\"}`);\n\t\tif (req.method !== \"GET\") {\n\t\t\tsendJson(res, 405, { error: \"method not allowed\" });\n\t\t\treturn;\n\t\t}\n\t\ttry {\n\t\t\tif (url.pathname === \"/api/health\") {\n\t\t\t\tsendJson(res, 200, {\n\t\t\t\t\tstatus: \"ok\",\n\t\t\t\t\tprojectDir: config.projectDir,\n\t\t\t\t\tuptimeSec: (Date.now() - startedAt) / 1000,\n\t\t\t\t});\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (url.pathname === \"/api/contributions\") {\n\t\t\t\tconfig.store\n\t\t\t\t\t.query()\n\t\t\t\t\t.then((contributions) => sendJson(res, 200, contributions))\n\t\t\t\t\t.catch((error: unknown) => sendJson(res, 500, { error: String(error) }));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (url.pathname === \"/api/frontier\") {\n\t\t\t\tconfig.store\n\t\t\t\t\t.query()\n\t\t\t\t\t.then((contributions: Contribution[]) =>\n\t\t\t\t\t\tsendJson(res, 200, {\n\t\t\t\t\t\t\tleaves: frontier(contributions),\n\t\t\t\t\t\t\tunverifiedResults: unverifiedResults(contributions),\n\t\t\t\t\t\t}),\n\t\t\t\t\t)\n\t\t\t\t\t.catch((error: unknown) => sendJson(res, 500, { error: String(error) }));\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tif (url.pathname === \"/api/openapi.json\") {\n\t\t\t\tsendJson(res, 200, readSpec());\n\t\t\t\treturn;\n\t\t\t}\n\t\t\tsendJson(res, 404, { error: \"not found\" });\n\t\t} catch (error) {\n\t\t\tsendJson(res, 500, { error: error instanceof Error ? error.message : \"internal error\" });\n\t\t}\n\t});\n}\n"]}