serve favicons over hashed paths for improved caching
This commit is contained in:
@ -11,9 +11,11 @@ function getRemoteAddr (req) {
|
||||
}
|
||||
|
||||
class Server {
|
||||
constructor (clientSocketHandler) {
|
||||
constructor (app) {
|
||||
this._app = app
|
||||
|
||||
this.createHttpServer()
|
||||
this.createWebSocketServer(clientSocketHandler)
|
||||
this.createWebSocketServer()
|
||||
}
|
||||
|
||||
createHttpServer () {
|
||||
@ -23,15 +25,40 @@ class Server {
|
||||
this._http = http.createServer((req, res) => {
|
||||
logger.log('info', '%s requested: %s', getRemoteAddr(req), req.url)
|
||||
|
||||
// Attempt to handle req using distServeStatic, otherwise fail over to faviconServeStatic
|
||||
// If faviconServeStatic fails, pass to finalHttpHandler to terminate
|
||||
distServeStatic(req, res, () => {
|
||||
faviconsServeStatic(req, res, finalHttpHandler(req, res))
|
||||
})
|
||||
if (req.url.startsWith('/hashedfavicon?')) {
|
||||
this.handleFaviconRequest(req, res)
|
||||
} else {
|
||||
// Attempt to handle req using distServeStatic, otherwise fail over to faviconServeStatic
|
||||
// If faviconServeStatic fails, pass to finalHttpHandler to terminate
|
||||
distServeStatic(req, res, () => {
|
||||
faviconsServeStatic(req, res, finalHttpHandler(req, res))
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
createWebSocketServer (proxyClientSocketHandler) {
|
||||
handleFaviconRequest = (req, res) => {
|
||||
const hash = req.url.split('?')[1]
|
||||
|
||||
for (const serverRegistration of this._app.serverRegistrations) {
|
||||
if (serverRegistration.faviconHash && serverRegistration.faviconHash === hash) {
|
||||
const buf = Buffer.from(serverRegistration.lastFavicon.split(',')[1], 'base64')
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'image/png',
|
||||
'Content-Length': buf.length
|
||||
}).end(buf)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Terminate request, no match
|
||||
res.writeHead(404)
|
||||
res.end()
|
||||
}
|
||||
|
||||
createWebSocketServer () {
|
||||
this._wss = new WebSocket.Server({
|
||||
server: this._http
|
||||
})
|
||||
@ -45,7 +72,7 @@ class Server {
|
||||
})
|
||||
|
||||
// Pass client off to proxy handler
|
||||
proxyClientSocketHandler(client)
|
||||
this._app.handleClientConnection(client)
|
||||
})
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user