From 655c4667068a93fb4c3141c17a9842bf46ff3e80 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Wed, 8 May 2024 14:56:21 -0400 Subject: [PATCH] Port `halt-on-dropped-connection` to TypeScript (#50497) --- .../middleware/halt-on-dropped-connection.js | 18 ---------------- .../middleware/halt-on-dropped-connection.ts | 21 +++++++++++++++++++ src/frame/middleware/index.ts | 2 +- src/frame/middleware/render-page.js | 2 +- 4 files changed, 23 insertions(+), 20 deletions(-) delete mode 100644 src/frame/middleware/halt-on-dropped-connection.js create mode 100644 src/frame/middleware/halt-on-dropped-connection.ts diff --git a/src/frame/middleware/halt-on-dropped-connection.js b/src/frame/middleware/halt-on-dropped-connection.js deleted file mode 100644 index 93a7d5c352fb..000000000000 --- a/src/frame/middleware/halt-on-dropped-connection.js +++ /dev/null @@ -1,18 +0,0 @@ -export 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) -} - -export 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 - -export default haltOnDroppedConnection diff --git a/src/frame/middleware/halt-on-dropped-connection.ts b/src/frame/middleware/halt-on-dropped-connection.ts new file mode 100644 index 000000000000..fc12f90515c3 --- /dev/null +++ b/src/frame/middleware/halt-on-dropped-connection.ts @@ -0,0 +1,21 @@ +import type { NextFunction, Request, Response } from 'express' + +// The `express-timeout-handler` middleware sets a property on +// the response object if it triggered a timeout. +// So we have to "pretend" that the request of this this type because +// it can be because of how that middleware works. +type ResponseWithGlobalTimeout = Response & { globalTimeout?: boolean } + +export function isConnectionDropped(req: Request, res: ResponseWithGlobalTimeout) { + // 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) +} + +export function haltOnDroppedConnection(req: Request, res: Response, next: NextFunction) { + // Only proceed if the flag has not been set for the express-timeout-handler middleware + if (!isConnectionDropped(req, res)) { + return next() + } +} diff --git a/src/frame/middleware/index.ts b/src/frame/middleware/index.ts index eebb046984da..45fae1d08c61 100644 --- a/src/frame/middleware/index.ts +++ b/src/frame/middleware/index.ts @@ -4,7 +4,7 @@ import path from 'path' import express from 'express' import type { NextFunction, Request, Response, Express } from 'express' -import haltOnDroppedConnection from './halt-on-dropped-connection.js' +import { haltOnDroppedConnection } from './halt-on-dropped-connection' import abort from './abort' import timeout from './timeout.js' import morgan from 'morgan' diff --git a/src/frame/middleware/render-page.js b/src/frame/middleware/render-page.js index bab7c37e8cc8..7f93a81b2c6e 100644 --- a/src/frame/middleware/render-page.js +++ b/src/frame/middleware/render-page.js @@ -8,7 +8,7 @@ import getMiniTocItems from '#src/frame/lib/get-mini-toc-items.js' import { pathLanguagePrefixed } from '#src/languages/lib/languages.js' import statsd from '#src/observability/lib/statsd.js' import { allVersions } from '#src/versions/lib/all-versions.js' -import { isConnectionDropped } from './halt-on-dropped-connection.js' +import { isConnectionDropped } from './halt-on-dropped-connection' import { nextHandleRequest } from './next.js' import { defaultCacheControl } from './cache-control.js' import { minimumNotFoundHtml } from '../lib/constants.js'