export async function readJsonBody(req, maxBytes = 2 * 1024 * 1024) {
    return await new Promise((resolve) => {
        const chunks = [];
        let total = 0;
        req.on("data", (chunk) => {
            total += chunk.length;
            if (total > maxBytes) {
                resolve(null);
                req.destroy();
                return;
            }
            chunks.push(chunk);
        });
        req.on("end", () => {
            try {
                resolve(JSON.parse(Buffer.concat(chunks).toString("utf-8")));
            }
            catch {
                resolve(null);
            }
        });
        req.on("error", () => resolve(null));
    });
}
