import { createServer as createHttpServer } from "node:http"; import { mkdir, readFile, rename, writeFile } from "node:fs/promises"; import { existsSync } from "node:fs"; import { dirname, join } from "node:path"; import { fileURLToPath, pathToFileURL } from "node:url"; const projectDir = dirname(fileURLToPath(import.meta.url)); function sendJson(response, statusCode, value) { response.writeHead(statusCode, { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" }); response.end(JSON.stringify(value)); } function sendText(response, statusCode, text, contentType = "text/plain; charset=utf-8") { response.writeHead(statusCode, { "content-type": contentType, "cache-control": "no-store" }); response.end(text); } async function readRequestBody(request, limitBytes = 5 * 1024 * 1024) { const chunks = []; let size = 0; for await (const chunk of request) { size += chunk.length; if (size > limitBytes) { throw new Error("Payload zu gross"); } chunks.push(chunk); } return Buffer.concat(chunks).toString("utf8"); } function validateMapData(value) { if (!value || typeof value !== "object") { throw new Error("Karte muss ein Objekt sein"); } if (!Array.isArray(value.systems) || !Array.isArray(value.connections)) { throw new Error("systems und connections muessen Arrays sein"); } const ids = new Set(); for (const system of value.systems) { if (!system || typeof system !== "object") { throw new Error("Ungueltiges System"); } if (typeof system.id !== "string" || typeof system.name !== "string") { throw new Error("System braucht id und name"); } if (!Number.isFinite(system.x) || !Number.isFinite(system.y)) { throw new Error("System braucht x/y Koordinaten"); } ids.add(system.id); } for (const connection of value.connections) { if (!connection || typeof connection !== "object") { throw new Error("Ungueltige Verbindung"); } if (typeof connection.id !== "string" || typeof connection.from !== "string" || typeof connection.to !== "string") { throw new Error("Verbindung braucht id/from/to"); } if (!ids.has(connection.from) || !ids.has(connection.to)) { throw new Error("Verbindung verweist auf unbekanntes System"); } } return { source: typeof value.source === "string" ? value.source : undefined, background: typeof value.background === "string" ? value.background : "#05080f", grid: value.grid && typeof value.grid === "object" ? value.grid : undefined, viewBox: value.viewBox && typeof value.viewBox === "object" ? value.viewBox : undefined, systems: value.systems, connections: value.connections }; } async function readInitialMap(htmlPath) { const html = await readFile(htmlPath, "utf8"); const match = html.match(/