refactor: move editor page and types to conventional folders

This commit is contained in:
2026-04-28 09:29:18 +02:00
parent bfe8c49323
commit 7b38f04a0d
12 changed files with 426 additions and 606 deletions
+37 -60
View File
@@ -3,74 +3,51 @@ import react from "@vitejs/plugin-react";
import path from "node:path";
import fs from "node:fs";
import { fileURLToPath } from "node:url";
import type { ViteDevServer } from "vite";
import type { IncomingMessage, ServerResponse } from "http";
import type { Plugin } from "vite";
const __dirname = fileURLToPath(new URL(".", import.meta.url));
const MAX_MAP_PAYLOAD_BYTES = 1024 * 1024; // 1MB limit
const MAX_MAP_PAYLOAD_BYTES = 1024 * 1024;
const saveMapPlugin = () => ({
const saveMapPlugin = (): Plugin => ({
name: "save-map-api",
configureServer(server: ViteDevServer) {
server.middlewares.use(
"/api/save-map",
async (req: IncomingMessage, res: ServerResponse) => {
if (req.method !== "POST") {
res.writeHead(405, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Method not allowed" }));
configureServer(server) {
server.middlewares.use("/api/save-map", async (req, res) => {
if (req.method !== "POST") {
res.writeHead(405).end(JSON.stringify({ error: "Method not allowed" }));
return;
}
const chunks: Buffer[] = [];
let size = 0;
for await (const chunk of req) {
size += chunk.length;
if (size > MAX_MAP_PAYLOAD_BYTES) {
res
.writeHead(413)
.end(JSON.stringify({ error: "Payload too large" }));
req.destroy();
return;
}
chunks.push(chunk);
}
let body = "";
let bodySize = 0;
let requestAborted = false;
req.on("data", (chunk: Buffer) => {
if (requestAborted) return;
bodySize += chunk.length;
if (bodySize > MAX_MAP_PAYLOAD_BYTES) {
requestAborted = true;
res.writeHead(413, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Payload too large" }));
req.destroy();
return;
}
body += chunk.toString();
});
req.on("error", (err: Error) => {
if (!res.headersSent) {
res.writeHead(400, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: err.message }));
}
});
req.on("end", async () => {
if (requestAborted) return;
try {
const parsedBody = JSON.parse(body);
const mapPath = path.resolve(__dirname, "public/map.json");
await fs.promises.writeFile(
mapPath,
JSON.stringify(parsedBody, null, 2),
"utf8",
);
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify({ success: true }));
} catch (err) {
const statusCode = err instanceof SyntaxError ? 400 : 500;
res.writeHead(statusCode, { "Content-Type": "application/json" });
res.end(
JSON.stringify({
error: err instanceof Error ? err.message : "Unknown error",
}),
);
}
});
},
);
try {
const data = JSON.parse(Buffer.concat(chunks).toString());
const mapPath = path.resolve(__dirname, "public/map.json");
await fs.promises.writeFile(
mapPath,
JSON.stringify(data, null, 2),
"utf8",
);
res.writeHead(200).end(JSON.stringify({ success: true }));
} catch (err) {
const status = err instanceof SyntaxError ? 400 : 500;
const message = err instanceof Error ? err.message : "Unknown error";
res.writeHead(status).end(JSON.stringify({ error: message }));
}
});
},
});