export function errorMessage(err) {
    return err instanceof Error ? err.message : String(err);
}
export function toArgs(body) {
    if (body != null && typeof body === "object" && !Array.isArray(body)) {
        return body;
    }
    return {};
}
import { MAX_BODY_BYTES } from "./constants.js";
export function parseJsonBody(req) {
    return new Promise((resolve, reject) => {
        const chunks = [];
        let bytes = 0;
        req.on("data", (chunk) => {
            bytes += chunk.length;
            if (bytes > MAX_BODY_BYTES) {
                req.destroy?.();
                reject(new Error("Request body too large"));
                return;
            }
            chunks.push(chunk);
        });
        req.on("end", () => {
            if (chunks.length === 0)
                return resolve({});
            try {
                resolve(JSON.parse(Buffer.concat(chunks).toString()));
            }
            catch {
                reject(new Error("Invalid JSON body"));
            }
        });
        req.on("error", reject);
    });
}
