Files
note2any/server/dist/http.js
2026-04-21 17:55:41 +08:00

32 lines
1019 B
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.readJsonBody = exports.sendJson = void 0;
function sendJson(res, statusCode, payload) {
const body = JSON.stringify(payload);
res.writeHead(statusCode, {
"Content-Type": "application/json; charset=utf-8",
"Cache-Control": "no-store",
"Content-Length": Buffer.byteLength(body),
});
res.end(body);
}
exports.sendJson = sendJson;
async function readJsonBody(req, limitBytes = 10 * 1024 * 1024) {
const chunks = [];
let size = 0;
for await (const chunk of req) {
const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
size += buffer.length;
if (size > limitBytes) {
throw new Error("Request body too large");
}
chunks.push(buffer);
}
const raw = Buffer.concat(chunks).toString("utf8").trim();
if (raw.length === 0) {
return {};
}
return JSON.parse(raw);
}
exports.readJsonBody = readJsonBody;