ai generated solutions to our ai generated problems
This commit is contained in:
+33
-3
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user