Files
overleaf-cep/services/web/app/src/infrastructure/ServeStaticWrapper.mjs
T
Anna Claire Fields ee4b5f515c Handle ERR_STREAM_UNABLE_TO_PIPE alongside ERR_STREAM_PREMATURE_CLOSE (#31174)
GitOrigin-RevId: bbf49237b177d7a58a9b13efc6f38f5eecfb745c
2026-02-03 09:05:50 +00:00

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