From f631ea12a823913218b4b071152083f1afd158b8 Mon Sep 17 00:00:00 2001 From: Heidi Date: Sat, 20 Jun 2026 00:27:30 +0100 Subject: [PATCH] ai generated solutions to our ai generated problems --- server.cjs | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/server.cjs b/server.cjs index 732efac..69b75f5 100644 --- a/server.cjs +++ b/server.cjs @@ -357,6 +357,25 @@ function sendPublicDataFile(req, res, filePath, status = 200, extraHeaders = {}) }) } +function sendPublicDataHead(req, res, filePath, status = 200, extraHeaders = {}) { + fs.stat(filePath, (error, stat) => { + if (error || !stat.isFile()) { + send(res, 404, '', { + 'content-type': 'application/json; charset=utf-8', + 'cache-control': 'no-store', + }) + return + } + + send(res, status, '', { + 'content-type': 'application/json; charset=utf-8', + 'content-length': String(stat.size), + 'cache-control': `public, max-age=${PUBLIC_DATA_CACHE_MAX_AGE_SECONDS}, stale-while-revalidate=${PUBLIC_DATA_STALE_REVALIDATE_SECONDS}`, + ...extraHeaders, + }) + }) +} + async function servePublicData(req, res, pathname) { const filePath = safePublicDataPath(pathname) const relativePath = publicDataRelativePath(pathname) @@ -367,7 +386,8 @@ async function servePublicData(req, res, pathname) { const current = cachedPublicData(filePath) if (current?.fresh) { - sendPublicDataFile(req, res, filePath, 200, { 'x-tssbot-cache': 'data-hit' }) + const sender = req.method === 'HEAD' ? sendPublicDataHead : sendPublicDataFile + sender(req, res, filePath, 200, { 'x-tssbot-cache': 'data-hit' }) return } @@ -379,7 +399,17 @@ async function servePublicData(req, res, pathname) { if (current) { refreshPublicData(filePath, new URL(apiPath, API_UPSTREAM)) - sendPublicDataFile(req, res, filePath, 200, { 'x-tssbot-cache': 'data-stale' }) + const sender = req.method === 'HEAD' ? sendPublicDataHead : sendPublicDataFile + sender(req, res, filePath, 200, { 'x-tssbot-cache': 'data-stale' }) + return + } + + if (req.method === 'HEAD') { + send(res, 404, '', { + 'content-type': 'application/json; charset=utf-8', + 'cache-control': 'no-store', + 'x-tssbot-cache': 'data-miss', + }) return } @@ -2765,7 +2795,7 @@ const server = http.createServer((req, res) => { return } - if (req.method === 'GET') { + if (req.method === 'GET' || req.method === 'HEAD') { let pathname = '' try { pathname = new URL(req.url, `http://${req.headers.host || 'localhost'}`).pathname