This commit is contained in:
2026-05-15 00:38:23 +01:00
parent 4a0a00cbb8
commit 0f57bcdfc4
5 changed files with 158 additions and 6 deletions
+24 -2
View File
@@ -793,6 +793,23 @@ function proxyRequest(req, res) {
req.pipe(proxy)
}
function pagePublicOrigin(req) {
const configured = PUBLIC_ORIGIN.split(',').map((origin) => origin.trim()).filter(Boolean)[0]
if (configured) return configured.replace(/\/$/, '')
const host = req.headers['x-forwarded-host'] || req.headers.host || `localhost:${PORT}`
const proto = req.headers['x-forwarded-proto'] || (req.socket.encrypted ? 'https' : 'http')
return `${String(proto).split(',')[0].trim()}://${String(host).split(',')[0].trim()}`.replace(/\/$/, '')
}
function sendHtml(req, res, data, status = 200) {
const html = data.toString('utf8').replaceAll('__PUBLIC_ORIGIN__', pagePublicOrigin(req))
send(res, status, html, {
'content-type': mimeTypes['.html'],
'cache-control': 'no-cache',
})
}
function serveStatic(req, res) {
const requestPath = decodeURIComponent(new URL(req.url, `http://localhost:${PORT}`).pathname)
const relativePath = requestPath === '/' ? '/index.html' : requestPath
@@ -811,15 +828,20 @@ function serveStatic(req, res) {
})
}
send(res, 200, indexData, { 'content-type': mimeTypes['.html'] })
sendHtml(req, res, indexData)
})
return
}
const ext = path.extname(filePath)
if (ext === '.html') {
sendHtml(req, res, data)
return
}
send(res, 200, data, {
'content-type': mimeTypes[ext] || 'application/octet-stream',
'cache-control': ext === '.html' ? 'no-cache' : 'public, max-age=31536000, immutable',
'cache-control': 'public, max-age=31536000, immutable',
})
})
}