diff --git a/package-lock.json b/package-lock.json index 6b2412a90bf7..8eba666fef5c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -108,6 +108,7 @@ "@types/imurmurhash": "^0.1.4", "@types/js-cookie": "^3.0.6", "@types/lodash": "^4.17.0", + "@types/morgan": "1.9.9", "@types/react": "18.3.1", "@types/react-dom": "^18.3.0", "@types/tcp-port-used": "1.0.4", @@ -3252,6 +3253,15 @@ "version": "3.0.5", "license": "MIT" }, + "node_modules/@types/morgan": { + "version": "1.9.9", + "resolved": "https://registry.npmjs.org/@types/morgan/-/morgan-1.9.9.tgz", + "integrity": "sha512-iRYSDKVaC6FkGSpEVVIvrRGw0DfJMiQzIn3qr2G5B3C//AWkulhXgaBd7tS9/J79GWSYMTHGs7PfI5b3Y8m+RQ==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@types/ms": { "version": "0.7.31", "resolved": "https://registry.npmjs.org/@types/ms/-/ms-0.7.31.tgz", diff --git a/package.json b/package.json index 095c7eea5b5d..1d9caf424c69 100644 --- a/package.json +++ b/package.json @@ -294,6 +294,7 @@ "@types/imurmurhash": "^0.1.4", "@types/js-cookie": "^3.0.6", "@types/lodash": "^4.17.0", + "@types/morgan": "1.9.9", "@types/react": "18.3.1", "@types/react-dom": "^18.3.0", "@types/tcp-port-used": "1.0.4", diff --git a/src/frame/lib/app.ts b/src/frame/lib/app.ts index 3060493b9751..aa3506be7396 100644 --- a/src/frame/lib/app.ts +++ b/src/frame/lib/app.ts @@ -1,5 +1,5 @@ import express from 'express' -import middleware from '@/frame/middleware/index.js' +import middleware from '@/frame/middleware' function createApp() { const app = express() diff --git a/src/frame/middleware/index.js b/src/frame/middleware/index.ts similarity index 83% rename from src/frame/middleware/index.js rename to src/frame/middleware/index.ts index 683628418024..6249beb79d74 100644 --- a/src/frame/middleware/index.js +++ b/src/frame/middleware/index.ts @@ -2,42 +2,43 @@ import fs from 'fs' 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 abort from './abort.js' import timeout from './timeout.js' import morgan from 'morgan' -import datadog from '#src/observability/middleware/connect-datadog.js' +import datadog from '@/observability/middleware/connect-datadog.js' import helmet from './helmet.js' import cookieParser from './cookie-parser.js' import { setDefaultFastlySurrogateKey, setLanguageFastlySurrogateKey, } from './set-fastly-surrogate-key.js' -import handleErrors from '#src/observability/middleware/handle-errors.js' +import handleErrors from '@/observability/middleware/handle-errors.js' import handleNextDataPath from './handle-next-data-path.js' -import detectLanguage from '#src/languages/middleware/detect-language.js' +import detectLanguage from '@/languages/middleware/detect-language.js' import reloadTree from './reload-tree.js' import context from './context/context.js' -import shortVersions from '#src/versions/middleware/short-versions.js' -import languageCodeRedirects from '#src/redirects/middleware/language-code-redirects.js' -import handleRedirects from '#src/redirects/middleware/handle-redirects.js' +import shortVersions from '@/versions/middleware/short-versions.js' +import languageCodeRedirects from '@/redirects/middleware/language-code-redirects.js' +import handleRedirects from '@/redirects/middleware/handle-redirects.js' import findPage from './find-page.js' import blockRobots from './block-robots.js' -import archivedEnterpriseVersionsAssets from '#src/archives/middleware/archived-enterprise-versions-assets.js' +import archivedEnterpriseVersionsAssets from '@/archives/middleware/archived-enterprise-versions-assets.js' import api from './api.js' import healthz from './healthz.js' import productIcons from './product-icons.js' import manifestJson from './manifest-json.js' import remoteIP from './remote-ip.js' import buildInfo from './build-info.js' -import archivedEnterpriseVersions from '#src/archives/middleware/archived-enterprise-versions.js' +import archivedEnterpriseVersions from '@/archives/middleware/archived-enterprise-versions.js' import robots from './robots.js' -import earlyAccessLinks from '#src/early-access/middleware/early-access-links.js' +import earlyAccessLinks from '@/early-access/middleware/early-access-links.js' import categoriesForSupport from './categories-for-support.js' -import triggerError from '#src/observability/middleware/trigger-error.js' -import secretScanning from '#src/secret-scanning/middleware/secret-scanning.js' -import ghesReleaseNotes from '#src/release-notes/middleware/ghes-release-notes.js' +import triggerError from '@/observability/middleware/trigger-error.js' +import secretScanning from '@/secret-scanning/middleware/secret-scanning.js' +import ghesReleaseNotes from '@/release-notes/middleware/ghes-release-notes.js' import whatsNewChangelog from './context/whats-new-changelog.js' import layout from './context/layout.js' import currentProductTree from './context/current-product-tree.js' @@ -45,26 +46,26 @@ import genericToc from './context/generic-toc.js' import breadcrumbs from './context/breadcrumbs.js' import glossaries from './context/glossaries.js' import renderProductName from './context/render-product-name.js' -import features from '#src/versions/middleware/features.js' +import features from '@/versions/middleware/features.js' import productExamples from './context/product-examples.js' import productGroups from './context/product-groups.js' -import featuredLinks from '#src/landings/middleware/featured-links.js' -import learningTrack from '#src/learning-track/middleware/learning-track.js' +import featuredLinks from '@/landings/middleware/featured-links.js' +import learningTrack from '@/learning-track/middleware/learning-track.js' import next from './next.js' import renderPage from './render-page.js' -import assetPreprocessing from '#src/assets/middleware/asset-preprocessing.js' -import archivedAssetRedirects from '#src/archives/middleware/archived-asset-redirects.js' +import assetPreprocessing from '@/assets/middleware/asset-preprocessing.js' +import archivedAssetRedirects from '@/archives/middleware/archived-asset-redirects.js' import favicons from './favicons.js' -import setStaticAssetCaching from '#src/assets/middleware/static-asset-caching.js' +import setStaticAssetCaching from '@/assets/middleware/static-asset-caching.js' import fastHead from './fast-head.js' import fastlyCacheTest from './fastly-cache-test.js' import trailingSlashes from './trailing-slashes.js' import fastlyBehavior from './fastly-behavior.js' import mockVaPortal from './mock-va-portal.js' -import dynamicAssets from '#src/assets/middleware/dynamic-assets.js' -import contextualizeSearch from '#src/search/middleware/contextualize.js' -import shielding from '#src/shielding/middleware/index.js' -import tracking from '#src/tracking/middleware/index.js' +import dynamicAssets from '@/assets/middleware/dynamic-assets.js' +import contextualizeSearch from '@/search/middleware/contextualize.js' +import shielding from '@/shielding/middleware/index.js' +import tracking from '@/tracking/middleware/index.js' const { DEPLOYMENT_ENV, NODE_ENV } = process.env const isTest = NODE_ENV === 'test' || process.env.GITHUB_ACTIONS === 'true' @@ -72,19 +73,21 @@ const isTest = NODE_ENV === 'test' || process.env.GITHUB_ACTIONS === 'true' // By default, logging each request (with morgan), is on. And by default // it's off if you're in a production environment or running automated tests. // But if you set the env var, that takes precedence. -const ENABLE_DEV_LOGGING = JSON.parse( - process.env.ENABLE_DEV_LOGGING || !(DEPLOYMENT_ENV === 'azure' || isTest), +const ENABLE_DEV_LOGGING = Boolean( + process.env.ENABLE_DEV_LOGGING + ? JSON.parse(process.env.ENABLE_DEV_LOGGING) + : !(DEPLOYMENT_ENV === 'azure' || isTest), ) const ENABLE_FASTLY_TESTING = JSON.parse(process.env.ENABLE_FASTLY_TESTING || 'false') // Catch unhandled promise rejections and passing them to Express's error handler // https://medium.com/@Abazhenov/using-async-await-in-express-with-node-8-b8af872c0016 -const asyncMiddleware = (fn) => (req, res, next) => { +const asyncMiddleware = (fn: Function) => (req: Request, res: Response, next: NextFunction) => { Promise.resolve(fn(req, res, next)).catch(next) } -export default function (app) { +export default function (app: Express) { // *** Request connection management *** if (!isTest) app.use(timeout) app.use(abort)