Added function names to more middleware

refs 319f251ad2

- this helps debugging because all middleware in the stack will have a
  function name, so it'll show up instead of `<anonymous>`
This commit is contained in:
Daniel Lockyer 2024-05-06 17:26:14 +02:00 committed by Daniel Lockyer
parent 2aad4ca06f
commit 265a8dd16f
13 changed files with 29 additions and 19 deletions

View File

@ -15,7 +15,7 @@ const isMaintenanceModeEnabled = (req) => {
};
// We never want middleware functions to be anonymous
const maintenanceMiddleware = (req, res, next) => {
const maintenanceMiddleware = function maintenanceMiddleware(req, res, next) {
if (!isMaintenanceModeEnabled(req)) {
return next();
}

View File

@ -34,7 +34,7 @@ const errorFallbackMessage = err => `<h1>${tpl(messages.oopsErrorTemplateHasErro
<br ><p>${tpl(messages.whilstTryingToRender)}</p>
${err.statusCode} <pre>${escapeExpression(err.message || err)}</pre>`;
const themeErrorRenderer = (err, req, res, next) => {
const themeErrorRenderer = function themeErrorRenderer(err, req, res, next) {
// If the error code is explicitly set to STATIC_FILE_NOT_FOUND,
// Skip trying to render an HTML error, and move on to the basic error renderer
// We do this because customised 404 templates could reference the image that's missing

View File

@ -50,7 +50,7 @@ module.exports = function setupSiteApp(routerConfig) {
// enable CORS headers (allows admin client to hit front-end when configured on separate URLs)
siteApp.use(mw.cors);
siteApp.use(async (req, res, next) => {
siteApp.use(async function nestApp(req, res, next) {
if (labs.isSet('NestPlayground') || labs.isSet('ActivityPub')) {
const originalExpressApp = req.app;
const app = await GhostNestApp.getApp();
@ -112,7 +112,9 @@ module.exports = function setupSiteApp(routerConfig) {
siteApp.use(
'/members/.well-known',
shared.middleware.cacheControl('public', {maxAge: config.get('caching:wellKnown:maxAge')}),
(req, res, next) => membersService.api.middleware.wellKnown(req, res, next)
function lazyWellKnownMw(req, res, next) {
return membersService.api.middleware.wellKnown(req, res, next);
}
);
// Recommendations well-known

View File

@ -31,7 +31,7 @@ const session = {
email: object.username,
password: object.password
}).then((user) => {
return Promise.resolve((req, res, next) => {
return Promise.resolve(function sessionMiddleware(req, res, next) {
req.brute.reset(function (err) {
if (err) {
return next(err);
@ -60,7 +60,7 @@ const session = {
});
},
delete() {
return Promise.resolve((req, res, next) => {
return Promise.resolve(function destroySessionMw(req, res, next) {
auth.session.destroySession(req, res, next);
});
}

View File

@ -34,7 +34,7 @@ module.exports = function setupAdminApp() {
// request to the Admin API /users/me/ endpoint to check if the user is logged in.
//
// Used by comments-ui to add moderation options to front-end comments when logged in.
adminApp.use('/auth-frame', (req, res, next) => {
adminApp.use('/auth-frame', function authFrameMw(req, res, next) {
// only render content when we have an Admin session cookie,
// otherwise return a 204 to avoid JS and API requests being made unnecessarily
try {

View File

@ -35,7 +35,7 @@ module.exports = function setupApiApp() {
// Routing
apiApp.use(routes());
apiApp.use(async (req, res, next) => {
apiApp.use(async function nestApp(req, res, next) {
if (labs.isSet('NestPlayground') || labs.isSet('ActivityPub')) {
const originalExpressApp = req.app;
const app = await GhostNestApp.getApp();

View File

@ -61,11 +61,19 @@ module.exports = function setupMembersApp() {
shared.middleware.brute.membersAuthEnumeration,
// Prevent brute forcing passwords for the same email address
shared.middleware.brute.membersAuth,
(req, res, next) => membersService.api.middleware.sendMagicLink(req, res, next)
function lazySendMagicLinkMw(req, res, next) {
return membersService.api.middleware.sendMagicLink(req, res, next);
}
);
membersApp.post('/api/create-stripe-checkout-session', (req, res, next) => membersService.api.middleware.createCheckoutSession(req, res, next));
membersApp.post('/api/create-stripe-update-session', (req, res, next) => membersService.api.middleware.createCheckoutSetupSession(req, res, next));
membersApp.put('/api/subscriptions/:id', (req, res, next) => membersService.api.middleware.updateSubscription(req, res, next));
membersApp.post('/api/create-stripe-checkout-session', function lazyCreateCheckoutSessionMw(req, res, next) {
return membersService.api.middleware.createCheckoutSession(req, res, next);
});
membersApp.post('/api/create-stripe-update-session', function lazyCreateCheckoutSetupSessionMw(req, res, next) {
return membersService.api.middleware.createCheckoutSetupSession(req, res, next);
});
membersApp.put('/api/subscriptions/:id', function lazyUpdateSubscriptionMw(req, res, next) {
return membersService.api.middleware.updateSubscription(req, res, next);
});
// Comments
membersApp.use('/api/comments', commentRouter());

View File

@ -44,7 +44,7 @@ module.exports = function queueRequest(
debug(`Request completed: ${job.data.req.path}`);
});
return (req, res, next) => {
return function queueRequestMw(req, res, next) {
req.queueDepth = queue.queue.getLength();
// Do not queue requests for static assets - We assume that any path

View File

@ -142,7 +142,7 @@ module.exports.enabledHelper = function enabledHelper(options, callback) {
return errString;
};
module.exports.enabledMiddleware = flag => (req, res, next) => {
module.exports.enabledMiddleware = flag => function labsEnabledMw(req, res, next) {
if (module.exports.isSet(flag) === true) {
return next();
} else {

View File

@ -317,7 +317,7 @@ module.exports = function MembersAPI({
return getMemberIdentityData(email);
}
const forwardError = fn => async (req, res, next) => {
const forwardError = fn => async function forwardErrorMw(req, res, next) {
try {
await fn(req, res, next);
} catch (err) {

View File

@ -7,7 +7,7 @@ const versionMismatchHandler = (APIVersionCompatibilityService) => {
* @param {import('express').Response} res
* @param {import('express').NextFunction} next
*/
return async (err, req, res, next) => {
return async function versionMismatchHandlerMiddlware(err, req, res, next) {
if (err && err.errorType === 'RequestNotAcceptableError') {
if (err.code === 'UPDATE_CLIENT') {
const {key, type} = extractApiKey(req);

View File

@ -23,9 +23,9 @@ const sessionFromTokenMiddleware = require('@tryghost/mw-session-from-token')({
}
});
someExpressApp.get('/some/sso/url', someSessionMiddleware, sessionFromTokenMiddleware, (req, res, next) => {
someExpressApp.get('/some/sso/url', someSessionMiddleware, sessionFromTokenMiddleware, function sessionFromTokenMiddleware(req, res, next) {
res.redirect('/loggedin');
}, (err, res, res, next) => {
}, function nextMw(err, res, res, next) => {
res.redirect('/error');
});
```

View File

@ -24,7 +24,7 @@ const sessionService = SessionService({
}
});
app.use(async (req, res, next) => {
app.use(async function sessionMiddleware(req, res, next) {
try {
const user = await sessionService.getUserForSession(req, res);
req.user = user;