← back to Wallco Ai
lib/http.js
28 lines
// lib/http.js — HTTP helpers (304 negotiation, etc.).
//
// REFACTOR-1c (2026-05-23): extracted verbatim from server.js. Pure function,
// no state.
'use strict';
// Conditional-GET helper. Sets Last-Modified, checks If-Modified-Since, and
// 304s when the client's copy is current. Returns true if it sent the 304
// (caller should `return` after calling this).
function maybe304(req, res, lastModifiedDate) {
if (!lastModifiedDate || isNaN(lastModifiedDate.getTime())) return false;
// HTTP-date truncates to 1s; floor both sides to match.
const lastMs = Math.floor(lastModifiedDate.getTime() / 1000) * 1000;
res.setHeader('Last-Modified', new Date(lastMs).toUTCString());
const ims = req.headers['if-modified-since'];
if (ims) {
const imsMs = Date.parse(ims);
if (!isNaN(imsMs) && imsMs >= lastMs) {
res.status(304).end();
return true;
}
}
return false;
}
module.exports = { maybe304 };