2026-06-12 00:40:07 +05:30
|
|
|
// Small HTTP helpers shared across the server.
|
|
|
|
|
const now = () => Date.now();
|
|
|
|
|
|
|
|
|
|
const json = (res, code, body) => {
|
2026-06-24 17:58:23 +05:30
|
|
|
// no-store: API/JSON responses (and 404s) must never be cached — a cached 404 for an asset
|
|
|
|
|
// like /manifest.json would otherwise persist on a device even after the file is deployed.
|
|
|
|
|
res.writeHead(code, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' });
|
2026-06-12 00:40:07 +05:30
|
|
|
res.end(JSON.stringify(body));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function readBody(req) {
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
let data = '';
|
|
|
|
|
req.on('data', (c) => (data += c));
|
|
|
|
|
req.on('end', () => {
|
|
|
|
|
try { resolve(data ? JSON.parse(data) : {}); } catch { resolve({}); }
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function parseCookies(req) {
|
|
|
|
|
const out = {};
|
|
|
|
|
(req.headers.cookie || '').split(';').forEach((c) => {
|
|
|
|
|
const [k, ...v] = c.trim().split('=');
|
|
|
|
|
if (k) out[k] = decodeURIComponent(v.join('='));
|
|
|
|
|
});
|
|
|
|
|
return out;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = { now, json, readBody, parseCookies };
|