import { Buffer } from "node:buffer"; export function sendJson(res, status, payload) { const body = JSON.stringify(payload, null, 2); res.writeHead(status, { "Content-Type": "application/json; charset=utf-8", "Content-Length": Buffer.byteLength(body), "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET,POST,PUT,DELETE,OPTIONS", "Access-Control-Allow-Headers": "Content-Type", }); res.end(body); } export function sendText(res, status, text) { res.writeHead(status, { "Content-Type": "text/plain; charset=utf-8", "Content-Length": Buffer.byteLength(text), "Access-Control-Allow-Origin": "*", }); res.end(text); } export async function readJsonBody(req) { const chunks = []; for await (const chunk of req) chunks.push(chunk); const raw = Buffer.concat(chunks).toString("utf-8").trim(); return raw ? JSON.parse(raw) : {}; }