1
0
mirror of synced 2025-12-21 19:06:49 -05:00
Files
docs/middleware/halt-on-dropped-connection.js
James M. Greene fd7d0eeb1a Add timeout and abort middleware and processing halts (#18177)
* Add middleware to timeout requests after a period

* Add halt-on-dropped-connection middleware to stop the middleware processing stack if the connection was already dropped

* Add a few strategic bail-out spots for dropped connections during the render-page middleware

* Handle 404s and HEAD requests earlier in the page rendering flow

* Add a few more strategic bail-out spots for dropped connections during the render-page middleware

* Add middleware to notice aborted requests

* Add a check for aborted requests into the isConnectionDropped logic

* Reformat comment for consistency

* Handle aborted requests correctly in the error handler

* Explicit returns for consistency
2021-03-09 19:14:02 +00:00

19 lines
631 B
JavaScript

function isConnectionDropped (req, res) {
// Have the flags been set for:
// - a global request timeout (via the express-timeout-handler middleware)?
// - an aborted request connection (via Node.js core's HTTP IncomingMessage)?
return Boolean(res.globalTimeout || req.aborted)
}
function haltOnDroppedConnection (req, res, next) {
// Only proceed if the flag has not been set for the express-timeout-handler middleware
if (!isConnectionDropped(req, res)) {
return next()
}
}
// Export this logic, too
haltOnDroppedConnection.isConnectionDropped = isConnectionDropped
module.exports = haltOnDroppedConnection