mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-06-02 13:49:00 +02:00
ee4b5f515c
GitOrigin-RevId: bbf49237b177d7a58a9b13efc6f38f5eecfb745c
38 lines
1.0 KiB
JavaScript
38 lines
1.0 KiB
JavaScript
import express from 'express'
|
|
import { plainTextResponse } from './Response.mjs'
|
|
|
|
/*
|
|
This wrapper is implemented specifically to handle "Premature Close" errors.
|
|
These errors occur when the client cancels a request while static assets are being loaded.
|
|
This issue is beyond our control, it can result in unnecessary log noise.
|
|
Therefore, this wrapper is added to handle such errors.
|
|
*/
|
|
function serveStaticWrapper(root, options) {
|
|
const serveStatic = express.static(root, options)
|
|
return (req, res, next) => {
|
|
serveStatic(req, res, error => {
|
|
if (!error) {
|
|
return next()
|
|
}
|
|
|
|
if (
|
|
error.code !== 'ERR_STREAM_PREMATURE_CLOSE' &&
|
|
error.code !== 'ERR_STREAM_UNABLE_TO_PIPE'
|
|
) {
|
|
return next(error)
|
|
}
|
|
|
|
req.logger.addFields({ err: error })
|
|
req.logger.setLevel('debug')
|
|
if (res.headersSent) {
|
|
res.end()
|
|
} else {
|
|
res.status(400)
|
|
plainTextResponse(res, 'Premature close')
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
export default serveStaticWrapper
|