export function sendJson(res, statusCode, payload) {
const body = JSON.stringify(payload);
res.writeHead(statusCode, {
"Content-Type": "application/json; charset=utf-8",
"Content-Length": Buffer.byteLength(body),
"Cache-Control": "no-store",
});
res.end(body);
}
export function sendText(res, statusCode, body, contentType = "text/plain; charset=utf-8") {
res.writeHead(statusCode, {
"Content-Type": contentType,
"Content-Length": Buffer.byteLength(body),
"Cache-Control": "no-store",
});
res.end(body);
}
export async function fileExists(fsModule, filePath) {
try {
await fsModule.access(filePath);
return true;
} catch {
return false;
}
}
export async function readBody(req) {
const chunks = [];
for await (const chunk of req) chunks.push(chunk);
if (!chunks.length) return {};
const body = Buffer.concat(chunks).toString("utf8");
return body ? JSON.parse(body) : {};
}
export function escapeHtml(value) {
return String(value)
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">");
}