From afe6eef80f981f74e36b2bd444adca8f3b569ab3 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Wed, 26 Jun 2024 15:41:36 -0400 Subject: [PATCH] Port `trailing-slashes.js` to TypeScript (#51431) --- src/frame/middleware/index.ts | 2 +- .../{trailing-slashes.js => trailing-slashes.ts} | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) rename src/frame/middleware/{trailing-slashes.js => trailing-slashes.ts} (65%) diff --git a/src/frame/middleware/index.ts b/src/frame/middleware/index.ts index e7bb0e765d2d..ec29892df4d2 100644 --- a/src/frame/middleware/index.ts +++ b/src/frame/middleware/index.ts @@ -58,7 +58,7 @@ import favicons from './favicons' import setStaticAssetCaching from '@/assets/middleware/static-asset-caching' import fastHead from './fast-head' import fastlyCacheTest from './fastly-cache-test' -import trailingSlashes from './trailing-slashes.js' +import trailingSlashes from './trailing-slashes' import fastlyBehavior from './fastly-behavior.js' import mockVaPortal from './mock-va-portal.js' import dynamicAssets from '@/assets/middleware/dynamic-assets.js' diff --git a/src/frame/middleware/trailing-slashes.js b/src/frame/middleware/trailing-slashes.ts similarity index 65% rename from src/frame/middleware/trailing-slashes.js rename to src/frame/middleware/trailing-slashes.ts index fcfceb42f5b3..a1400df72dcf 100644 --- a/src/frame/middleware/trailing-slashes.js +++ b/src/frame/middleware/trailing-slashes.ts @@ -1,10 +1,13 @@ +import type { Response, NextFunction } from 'express' + +import type { ExtendedRequest } from '@/types' import { defaultCacheControl } from './cache-control.js' -export default function trailingSlashes(req, res, next) { +export default function trailingSlashes(req: ExtendedRequest, res: Response, next: NextFunction) { if (req.method === 'GET' || req.method === 'HEAD' || req.method === 'OPTIONS') { const split = req.url.split('?') let pathname = split.shift() - if (pathname !== '/' && pathname.endsWith('/')) { + if (pathname && pathname !== '/' && pathname.endsWith('/')) { while (pathname.endsWith('/')) { pathname = pathname.slice(0, pathname.length - 1) }