From c940dcd98b25c3fef36d09b7a7e85c64621a022f Mon Sep 17 00:00:00 2001 From: "James M. Greene" Date: Wed, 10 Mar 2021 19:12:41 -0600 Subject: [PATCH] Middleware overhaul! (#18218) * Middleware overhaul! - Remove unnecessary 'async' keywords from middleware functions - Ensure all middleware functions we create have names - Wrap the method contents of all async middleware functions in a try-catch+next(error) pattern * Use asyncMiddleware wrapper instead of try-catch+next(error) pattern * Remove unnecessary try-catch+next(error) pattern from context middleware --- middleware/abort.js | 2 +- .../archived-enterprise-versions-assets.js | 2 +- middleware/archived-enterprise-versions.js | 2 +- middleware/block-robots.js | 2 +- middleware/breadcrumbs.js | 2 +- middleware/categories-for-support-team.js | 2 +- .../enterprise-release-notes.js | 2 +- middleware/contextualizers/graphql.js | 2 +- middleware/contextualizers/rest.js | 2 +- middleware/contextualizers/webhooks.js | 2 +- middleware/csp.js | 2 +- middleware/detect-language.js | 2 +- middleware/dev-toc.js | 2 +- middleware/disable-caching-on-safari.js | 2 +- middleware/early-access-breadcrumbs.js | 2 +- middleware/enterprise-server-releases.js | 2 +- middleware/events.js | 2 +- middleware/featured-links.js | 2 +- middleware/handle-csrf-errors.js | 2 +- middleware/handle-errors.js | 75 ++++++++++--------- middleware/handle-invalid-paths.js | 5 +- middleware/index.js | 24 +++--- middleware/learning-track.js | 2 +- middleware/loaderio-verification.js | 2 +- middleware/record-redirect.js | 2 +- middleware/redirects/external.js | 2 +- middleware/redirects/handle-redirects.js | 2 +- middleware/redirects/help-to-docs.js | 2 +- .../redirects/language-code-redirects.js | 2 +- middleware/req-utils.js | 2 +- middleware/robots.js | 2 +- middleware/search.js | 2 +- middleware/set-fastly-cache-headers.js | 2 +- middleware/set-fastly-surrogate-key.js | 2 +- middleware/trigger-error.js | 5 ++ 35 files changed, 91 insertions(+), 80 deletions(-) diff --git a/middleware/abort.js b/middleware/abort.js index a7351c29bf2f..387a965ead6e 100644 --- a/middleware/abort.js +++ b/middleware/abort.js @@ -1,4 +1,4 @@ -module.exports = function (req, res, next) { +module.exports = function abort (req, res, next) { // If the client aborts the connection, send an error req.once('aborted', () => { // NOTE: Node.js will also automatically set `req.aborted = true` diff --git a/middleware/archived-enterprise-versions-assets.js b/middleware/archived-enterprise-versions-assets.js index c1f125371289..81ca64607846 100644 --- a/middleware/archived-enterprise-versions-assets.js +++ b/middleware/archived-enterprise-versions-assets.js @@ -11,7 +11,7 @@ const ONE_DAY = 24 * 60 * 60 // 1 day in seconds // // See also ./archived-enterprise-versions.js for non-CSS/JS paths -module.exports = async (req, res, next) => { +module.exports = async function archivedEnterpriseVersionsAssets (req, res, next) { const { isArchived, requestedVersion } = isArchivedVersion(req) if (!isArchived) return next() diff --git a/middleware/archived-enterprise-versions.js b/middleware/archived-enterprise-versions.js index 422484f977e0..3a5cd96f7fcc 100644 --- a/middleware/archived-enterprise-versions.js +++ b/middleware/archived-enterprise-versions.js @@ -11,7 +11,7 @@ const archivedFrontmatterFallbacks = require('../lib/redirects/static/archived-f // This module handles requests for deprecated GitHub Enterprise versions // by routing them to static content in help-docs-archived-enterprise-versions -module.exports = async (req, res, next) => { +module.exports = async function archivedEnterpriseVersions (req, res, next) { const { isArchived, requestedVersion } = isArchivedVersion(req) if (!isArchived) return next() diff --git a/middleware/block-robots.js b/middleware/block-robots.js index 7a437972ded9..286920acaaa4 100644 --- a/middleware/block-robots.js +++ b/middleware/block-robots.js @@ -30,7 +30,7 @@ function blockIndex (path) { return pathRegExps.some(pathRe => pathRe.test(path)) } -const middleware = (req, res, next) => { +const middleware = function blockRobots (req, res, next) { if (blockIndex(req.path)) res.set('x-robots-tag', 'noindex') return next() } diff --git a/middleware/breadcrumbs.js b/middleware/breadcrumbs.js index 86970bde35b4..e26545c4000a 100644 --- a/middleware/breadcrumbs.js +++ b/middleware/breadcrumbs.js @@ -3,7 +3,7 @@ const { getPathWithoutLanguage } = require('../lib/path-utils') const nonEnterpriseDefaultVersion = require('../lib/non-enterprise-default-version') const removeFPTFromPath = require('../lib/remove-fpt-from-path') -module.exports = async (req, res, next) => { +module.exports = async function breadcrumbs (req, res, next) { if (!req.context.page) return next() if (req.context.page.hidden) return next() diff --git a/middleware/categories-for-support-team.js b/middleware/categories-for-support-team.js index 5964ce109b33..b288bc3b1389 100644 --- a/middleware/categories-for-support-team.js +++ b/middleware/categories-for-support-team.js @@ -8,7 +8,7 @@ const dotcomDir = path.join(__dirname, '../content/github') const dotcomIndex = path.join(dotcomDir, 'index.md') const linkRegex = /{% (?:topic_)?link_in_list ?\/(.*?) ?%}/g -module.exports = async (req, res, next) => { +module.exports = async function categoriesForSupportTeam (req, res, next) { if (req.path !== '/categories.json') return next() const categories = await generateCategories() return res.json(categories) diff --git a/middleware/contextualizers/enterprise-release-notes.js b/middleware/contextualizers/enterprise-release-notes.js index 0396bd109d44..0e36c55e68ca 100644 --- a/middleware/contextualizers/enterprise-release-notes.js +++ b/middleware/contextualizers/enterprise-release-notes.js @@ -66,7 +66,7 @@ async function renderPatchNotes (patch, ctx) { return patch } -module.exports = async (req, res, next) => { +module.exports = async function enterpriseReleaseNotesContext (req, res, next) { // The `/release-notes` sub-path if (!req.path.endsWith('/release-notes')) return next() diff --git a/middleware/contextualizers/graphql.js b/middleware/contextualizers/graphql.js index e07094dba777..91575be17a15 100644 --- a/middleware/contextualizers/graphql.js +++ b/middleware/contextualizers/graphql.js @@ -8,7 +8,7 @@ const explorerUrl = process.env.NODE_ENV === 'production' ? 'https://graphql.github.com/explorer' : 'http://localhost:3000' -module.exports = async (req, res, next) => { +module.exports = function graphqlContext (req, res, next) { const currentVersionObj = allVersions[req.context.currentVersion] // ignore requests to non-GraphQL reference paths // and to versions that don't exist diff --git a/middleware/contextualizers/rest.js b/middleware/contextualizers/rest.js index 198a4d312a24..5b49cbfa19eb 100644 --- a/middleware/contextualizers/rest.js +++ b/middleware/contextualizers/rest.js @@ -2,7 +2,7 @@ const path = require('path') const rest = require('../../lib/rest') const removeFPTFromPath = require('../../lib/remove-fpt-from-path') -module.exports = async function (req, res, next) { +module.exports = function restContext (req, res, next) { req.context.rest = rest // link to include in `Works with GitHub Apps` notes diff --git a/middleware/contextualizers/webhooks.js b/middleware/contextualizers/webhooks.js index f2ed9a6cc750..90347b36fd68 100644 --- a/middleware/contextualizers/webhooks.js +++ b/middleware/contextualizers/webhooks.js @@ -4,7 +4,7 @@ const webhookPayloads = require(path.join(process.cwd(), 'lib/webhooks')) const nonEnterpriseDefaultVersion = require('../../lib/non-enterprise-default-version') const allVersions = require('../../lib/all-versions') -module.exports = async (req, res, next) => { +module.exports = function webhooksContext (req, res, next) { const currentVersionObj = allVersions[req.context.currentVersion] // ignore requests to non-webhook reference paths // and to versions that don't exist diff --git a/middleware/csp.js b/middleware/csp.js index 9f054036f7d2..08b618eac631 100644 --- a/middleware/csp.js +++ b/middleware/csp.js @@ -7,7 +7,7 @@ const versionSatisfiesRange = require('../lib/version-satisfies-range') const AZURE_STORAGE_URL = 'githubdocs.azureedge.net' // module.exports = contentSecurityPolicy({ -module.exports = async (req, res, next) => { +module.exports = function csp (req, res, next) { const csp = { directives: { defaultSrc: ["'none'"], diff --git a/middleware/detect-language.js b/middleware/detect-language.js index 9c46c3e0b3d3..1c407fe3e5b6 100644 --- a/middleware/detect-language.js +++ b/middleware/detect-language.js @@ -1,7 +1,7 @@ const languageCodes = Object.keys(require('../lib/languages')) // determine language code from first part of URL, or default to English -module.exports = async function detectLanguage (req, res, next) { +module.exports = function detectLanguage (req, res, next) { // /en/articles/foo // ^^ const firstPartOfPath = req.path.split('/')[1] diff --git a/middleware/dev-toc.js b/middleware/dev-toc.js index 5e00093e556a..9e5e5f2e512a 100644 --- a/middleware/dev-toc.js +++ b/middleware/dev-toc.js @@ -1,7 +1,7 @@ const { liquid } = require('../lib/render-content') const layouts = require('../lib/layouts') -module.exports = async (req, res, next) => { +module.exports = async function devToc (req, res, next) { if (process.env.NODE_ENV !== 'development') return next() if (!req.path.endsWith('/dev-toc')) return next() diff --git a/middleware/disable-caching-on-safari.js b/middleware/disable-caching-on-safari.js index 4281b37c91ed..123bdffbe07e 100644 --- a/middleware/disable-caching-on-safari.js +++ b/middleware/disable-caching-on-safari.js @@ -1,4 +1,4 @@ -module.exports = (req, res, next) => { +module.exports = function disableCachingOnSafari (req, res, next) { const isSafari = /^((?!chrome|android).)*safari/i.test(req.headers['user-agent']) if (isSafari) { res.header('Last-Modified', (new Date()).toUTCString()) diff --git a/middleware/early-access-breadcrumbs.js b/middleware/early-access-breadcrumbs.js index 2b7c95ca7660..5ada437c47ed 100644 --- a/middleware/early-access-breadcrumbs.js +++ b/middleware/early-access-breadcrumbs.js @@ -5,7 +5,7 @@ const removeFPTFromPath = require('../lib/remove-fpt-from-path') // Early Access content doesn't conform to the same structure as other products, so we // can't derive breadcrumbs from the siteTree in the same way. Hence, this separate middleware. -module.exports = async (req, res, next) => { +module.exports = async function earlyAccessBreadcrumbs (req, res, next) { if (!req.context.page) return next() if (!req.context.page.hidden) return next() diff --git a/middleware/enterprise-server-releases.js b/middleware/enterprise-server-releases.js index a3f331d97c9b..0c7035c46a58 100644 --- a/middleware/enterprise-server-releases.js +++ b/middleware/enterprise-server-releases.js @@ -2,7 +2,7 @@ const { liquid } = require('../lib/render-content') const layouts = require('../lib/layouts') const getMiniTocItems = require('../lib/get-mini-toc-items') -module.exports = async (req, res, next) => { +module.exports = async function enterpriseServerReleases (req, res, next) { if (!req.path.endsWith('/enterprise-server-releases')) return next() const html = await liquid.parseAndRender(layouts['enterprise-server-releases'], req.context) diff --git a/middleware/events.js b/middleware/events.js index e687954e8357..11871b4fd5b7 100644 --- a/middleware/events.js +++ b/middleware/events.js @@ -10,7 +10,7 @@ const ajv = new Ajv() const router = express.Router() -router.post('/', async (req, res, next) => { +router.post('/', async function postEvents (req, res, next) { const isDev = process.env.NODE_ENV === 'development' const fields = omit(req.body, '_csrf') diff --git a/middleware/featured-links.js b/middleware/featured-links.js index 0430d06af5ed..e0a779ce17ef 100644 --- a/middleware/featured-links.js +++ b/middleware/featured-links.js @@ -1,7 +1,7 @@ const getLinkData = require('../lib/get-link-data') // this middleware adds properties to the context object -module.exports = async (req, res, next) => { +module.exports = async function featuredLinks (req, res, next) { if (!req.context.page) return next() if (!(req.context.page.relativePath.endsWith('index.md') || req.context.page.layout === 'product-landing')) return next() diff --git a/middleware/handle-csrf-errors.js b/middleware/handle-csrf-errors.js index 738bbbf0e3a0..be673ec4e3e5 100644 --- a/middleware/handle-csrf-errors.js +++ b/middleware/handle-csrf-errors.js @@ -1,4 +1,4 @@ -module.exports = async function handleCSRFError (error, req, res, next) { +module.exports = function handleCSRFError (error, req, res, next) { // If the CSRF token is bad if (error.code === 'EBADCSRFTOKEN') { return res.sendStatus(403) diff --git a/middleware/handle-errors.js b/middleware/handle-errors.js index 7fbb456ab99d..98448486f36b 100644 --- a/middleware/handle-errors.js +++ b/middleware/handle-errors.js @@ -28,47 +28,52 @@ async function logException (error, req) { } module.exports = async function handleError (error, req, res, next) { - // If the headers have already been sent or the request was aborted... - if (res.headersSent || req.aborted) { - // Report to Failbot - await logException(error, req) + try { + // If the headers have already been sent or the request was aborted... + if (res.headersSent || req.aborted) { + // Report to Failbot + await logException(error, req) - // We MUST delegate to the default Express error handler - return next(error) - } + // We MUST delegate to the default Express error handler + return next(error) + } - // if the error is thrown before req.context is created (say, in the Page class), - // set req.context.site here so we can pass data/ui.yml text to the 500 layout - if (!req.context) { - const site = await loadSiteData() - req.context = { site: site[req.language || 'en'].site } - } + // if the error is thrown before req.context is created (say, in the Page class), + // set req.context.site here so we can pass data/ui.yml text to the 500 layout + if (!req.context) { + const site = await loadSiteData() + req.context = { site: site[req.language || 'en'].site } + } - // display error on the page in development, but not in production - if (process.env.NODE_ENV !== 'production' && req.context) { - req.context.error = error - } + // display error on the page in development, but not in production + if (process.env.NODE_ENV !== 'production' && req.context) { + req.context.error = error + } - // Special handling for when a middleware calls `next(404)` - if (error === 404) { - return res - .status(404) - .send(await liquid.parseAndRender(layouts['error-404'], req.context)) - } + // Special handling for when a middleware calls `next(404)` + if (error === 404) { + return res + .status(404) + .send(await liquid.parseAndRender(layouts['error-404'], req.context)) + } - // If the error contains a status code, just send that back. This is usually - // from a middleware like `express.json()` or `csrf`. - if (error.statusCode || error.status) { - return res.sendStatus(error.statusCode || error.status) - } + // If the error contains a status code, just send that back. This is usually + // from a middleware like `express.json()` or `csrf`. + if (error.statusCode || error.status) { + return res.sendStatus(error.statusCode || error.status) + } - if (process.env.NODE_ENV !== 'test') { - console.error('500 error!', req.path) - console.error(error) - } + if (process.env.NODE_ENV !== 'test') { + console.error('500 error!', req.path) + console.error(error) + } - res.status(500).send(await liquid.parseAndRender(layouts['error-500'], req.context)) + res.status(500).send(await liquid.parseAndRender(layouts['error-500'], req.context)) - // Report to Failbot AFTER responding to the user - await logException(error, req) + // Report to Failbot AFTER responding to the user + await logException(error, req) + } catch (error) { + console.error('An error occurred in the error handling middleware!', error) + return next(error) + } } diff --git a/middleware/handle-invalid-paths.js b/middleware/handle-invalid-paths.js index 151887ce5459..27de9f48ff36 100644 --- a/middleware/handle-invalid-paths.js +++ b/middleware/handle-invalid-paths.js @@ -1,6 +1,6 @@ const patterns = require('../lib/patterns') -module.exports = (req, res, next) => { +module.exports = function handleInvalidPaths (req, res, next) { // prevent open redirect vulnerability if (req.path.match(patterns.multipleSlashes)) { return next(404) @@ -10,7 +10,6 @@ module.exports = (req, res, next) => { // for paths like /%7B% try { decodeURIComponent(req.path) - return next() } catch (err) { if (process.env.NODE_ENV !== 'test') { console.log('unable to decode path', req.path, err) @@ -18,4 +17,6 @@ module.exports = (req, res, next) => { return res.sendStatus(400) } + + return next() } diff --git a/middleware/index.js b/middleware/index.js index 6a29d0a55a05..779fd3c22ce9 100644 --- a/middleware/index.js +++ b/middleware/index.js @@ -64,7 +64,7 @@ module.exports = function (app) { app.use(instrument('./redirects/handle-redirects')) // Must come before contextualizers // *** Config and context for rendering *** - app.use(instrument('./find-page')) // Must come before archived-enterprise-versions, breadcrumbs, featured-links, products, render-page + app.use(asyncMiddleware(instrument('./find-page'))) // Must come before archived-enterprise-versions, breadcrumbs, featured-links, products, render-page app.use(instrument('./block-robots')) // Check for a dropped connection before proceeding @@ -72,7 +72,7 @@ module.exports = function (app) { // *** Rendering, 2xx responses *** // I largely ordered these by use frequency - app.use(instrument('./archived-enterprise-versions-assets')) // Must come before static/assets + app.use(asyncMiddleware(instrument('./archived-enterprise-versions-assets'))) // Must come before static/assets app.use('/dist', express.static('dist', { index: false, etag: false, @@ -92,12 +92,12 @@ module.exports = function (app) { lastModified: false, maxAge: '7 days' // A bit longer since releases are more sparse })) - app.use('/events', instrument('./events')) - app.use('/search', instrument('./search')) - app.use(instrument('./archived-enterprise-versions')) + app.use('/events', asyncMiddleware(instrument('./events'))) + app.use('/search', asyncMiddleware(instrument('./search'))) + app.use(asyncMiddleware(instrument('./archived-enterprise-versions'))) app.use(instrument('./robots')) app.use(/(\/.*)?\/early-access$/, instrument('./contextualizers/early-access-links')) - app.use(instrument('./categories-for-support-team')) + app.use(asyncMiddleware(instrument('./categories-for-support-team'))) app.use(instrument('./loaderio-verification')) app.get('/_500', asyncMiddleware(instrument('./trigger-error'))) @@ -109,12 +109,12 @@ module.exports = function (app) { app.use(instrument('./contextualizers/graphql')) app.use(instrument('./contextualizers/rest')) app.use(instrument('./contextualizers/webhooks')) - app.use(instrument('./breadcrumbs')) - app.use(instrument('./early-access-breadcrumbs')) - app.use(instrument('./enterprise-server-releases')) - app.use(instrument('./dev-toc')) - app.use(instrument('./featured-links')) - app.use(instrument('./learning-track')) + app.use(asyncMiddleware(instrument('./breadcrumbs'))) + app.use(asyncMiddleware(instrument('./early-access-breadcrumbs'))) + app.use(asyncMiddleware(instrument('./enterprise-server-releases'))) + app.use(asyncMiddleware(instrument('./dev-toc'))) + app.use(asyncMiddleware(instrument('./featured-links'))) + app.use(asyncMiddleware(instrument('./learning-track'))) // *** Headers for pages only *** app.use(require('./set-fastly-cache-headers')) diff --git a/middleware/learning-track.js b/middleware/learning-track.js index 2dbb6c6c201c..dd55b0745fe4 100644 --- a/middleware/learning-track.js +++ b/middleware/learning-track.js @@ -1,7 +1,7 @@ const { getPathWithoutLanguage, getPathWithoutVersion } = require('../lib/path-utils') const getLinkData = require('../lib/get-link-data') -module.exports = async (req, res, next) => { +module.exports = async function learningTrack (req, res, next) { const noTrack = () => { req.context.currentLearningTrack = {} return next() diff --git a/middleware/loaderio-verification.js b/middleware/loaderio-verification.js index f31f980c29fc..08a4577a31c1 100644 --- a/middleware/loaderio-verification.js +++ b/middleware/loaderio-verification.js @@ -1,6 +1,6 @@ // prove to loader.io that we own this site // by responding to requests like `/loaderio-12345/` with `loaderio-12345` -module.exports = (req, res, next) => { +module.exports = function loaderIoVerification (req, res, next) { if (!req.path.startsWith('/loaderio-')) return next() return res.send(req.path.replace(/\//g, '')) } diff --git a/middleware/record-redirect.js b/middleware/record-redirect.js index b72e975db30c..b57418033493 100644 --- a/middleware/record-redirect.js +++ b/middleware/record-redirect.js @@ -1,6 +1,6 @@ const { v4: uuidv4 } = require('uuid') -module.exports = function (req, res, next) { +module.exports = function recordRedirects (req, res, next) { if (!req.hydro.maySend()) return next() res.on('finish', async function recordRedirect () { diff --git a/middleware/redirects/external.js b/middleware/redirects/external.js index 3500af37592c..b6172696e3a1 100644 --- a/middleware/redirects/external.js +++ b/middleware/redirects/external.js @@ -1,7 +1,7 @@ const externalSites = require('../../lib/redirects/external-sites') // blanket redirects to external websites -module.exports = async function externalRedirects (req, res, next) { +module.exports = function externalRedirects (req, res, next) { if (req.path in externalSites) { return res.redirect(301, externalSites[req.path]) } else { diff --git a/middleware/redirects/handle-redirects.js b/middleware/redirects/handle-redirects.js index a6db5bc16897..8fc89af0563f 100644 --- a/middleware/redirects/handle-redirects.js +++ b/middleware/redirects/handle-redirects.js @@ -1,7 +1,7 @@ const patterns = require('../../lib/patterns') const { URL } = require('url') -module.exports = async function handleRedirects (req, res, next) { +module.exports = function handleRedirects (req, res, next) { // never redirect assets if (patterns.assetPaths.test(req.path)) return next() diff --git a/middleware/redirects/help-to-docs.js b/middleware/redirects/help-to-docs.js index d57c2136bbfd..508e283dbf9f 100644 --- a/middleware/redirects/help-to-docs.js +++ b/middleware/redirects/help-to-docs.js @@ -3,7 +3,7 @@ const patterns = require('../../lib/patterns') // redirect help.github.com requests to docs.github.com -module.exports = async (req, res, next) => { +module.exports = function helpToDocs (req, res, next) { if (req.hostname === 'help.github.com') { // prevent open redirect security vulnerability const path = req.originalUrl.replace(patterns.multipleSlashes, '/') diff --git a/middleware/redirects/language-code-redirects.js b/middleware/redirects/language-code-redirects.js index 25f16d91a5cf..d0d2ff733b16 100644 --- a/middleware/redirects/language-code-redirects.js +++ b/middleware/redirects/language-code-redirects.js @@ -5,7 +5,7 @@ const languages = require('../../lib/languages') // Examples: // /jp* -> /ja* // /zh-TW* -> /cn* -module.exports = async function languageCodeRedirects (req, res, next) { +module.exports = function languageCodeRedirects (req, res, next) { for (const code in languages) { const language = languages[code] const redirectPatterns = language.redirectPatterns || [] diff --git a/middleware/req-utils.js b/middleware/req-utils.js index d82bb959ed32..3072de4265eb 100644 --- a/middleware/req-utils.js +++ b/middleware/req-utils.js @@ -1,6 +1,6 @@ const Hydro = require('../lib/hydro') -module.exports = (req, res, next) => { +module.exports = function reqUtils (req, res, next) { req.hydro = new Hydro() return next() } diff --git a/middleware/robots.js b/middleware/robots.js index 967d1f21dde9..8e12c8c1bc71 100644 --- a/middleware/robots.js +++ b/middleware/robots.js @@ -3,7 +3,7 @@ const defaultResponse = 'User-agent: *' const disallowAll = `User-agent: * Disallow: /` -module.exports = function (req, res, next) { +module.exports = function robots (req, res, next) { if (req.path !== '/robots.txt') return next() res.type('text/plain') diff --git a/middleware/search.js b/middleware/search.js index 58025beef6fe..c430cc1b3a33 100644 --- a/middleware/search.js +++ b/middleware/search.js @@ -6,7 +6,7 @@ const loadAlgoliaResults = require('../lib/search/algolia-search') const router = express.Router() -router.get('/', async (req, res) => { +router.get('/', async function postSearch (req, res, next) { res.set({ 'surrogate-control': 'private, no-store', 'cache-control': 'private, no-store' diff --git a/middleware/set-fastly-cache-headers.js b/middleware/set-fastly-cache-headers.js index 2463e63ea5cb..2a044da3f4b0 100644 --- a/middleware/set-fastly-cache-headers.js +++ b/middleware/set-fastly-cache-headers.js @@ -1,4 +1,4 @@ -module.exports = (req, res, next) => { +module.exports = function setFastlyCacheHeaders (req, res, next) { // Disallow both Fastly AND the browser from caching HTML pages res.set({ 'surrogate-control': 'private, no-store', diff --git a/middleware/set-fastly-surrogate-key.js b/middleware/set-fastly-surrogate-key.js index 5ca1eda829f5..4912435c1a7a 100644 --- a/middleware/set-fastly-surrogate-key.js +++ b/middleware/set-fastly-surrogate-key.js @@ -1,4 +1,4 @@ -module.exports = (req, res, next) => { +module.exports = function setFastlySurrogateKey (req, res, next) { // Fastly provides a Soft Purge feature that allows you to mark content as outdated (stale) instead of permanently // purging and thereby deleting it from Fastly's caches. Objects invalidated with Soft Purge will be treated as // outdated (stale) while Fastly fetches a new version from origin. diff --git a/middleware/trigger-error.js b/middleware/trigger-error.js index 74bacee0ab76..271e523a389e 100644 --- a/middleware/trigger-error.js +++ b/middleware/trigger-error.js @@ -1,6 +1,11 @@ // This module is for testing our handling of uncaught async rejections on incoming requests +// IMPORTANT: Leave this function as `async` even though it doesn't need to be! module.exports = async function triggerError (req, res, next) { + // IMPORTANT: + // Do NOT wrap this method's contents in the usual `try-catch+next(error)` + // pattern used on async middleware! This is an intentional omission! + // prevent this from being used in production if (process.env.NODE_ENV === 'production') return next()