Skip to content

Commit bab58fa

Browse files
authored
Merge pull request #1017 from Financial-Times/named-middleware
Explicitly name all middleware functions to improve OpenTelemetry spans
2 parents 1d3d46b + 341da8e commit bab58fa

File tree

5 files changed

+42
-38
lines changed

5 files changed

+42
-38
lines changed

main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ const getAppContainer = (options) => {
8888

8989
// Debug related headers.
9090
app.use(
91-
/** @type {Callback} */ (req, res, next) => {
91+
/** @type {Callback} */ function setDebugHeaders (req, res, next) {
9292
res.set('FT-App-Name', meta.name);
9393
res.set('FT-Backend-Timestamp', new Date().toISOString());
9494
res.set('FT-System-Code', meta.systemCode);

src/middleware/backend-authentication.js

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,41 +35,40 @@ module.exports = (app) => {
3535
return;
3636
}
3737

38-
// @ts-ignore
39-
app.use(
40-
/** @type {Express.Handler} */ (req, res, next) => {
41-
// allow static assets, healthchecks, etc., through
42-
if (req.path.indexOf('/__') === 0) {
43-
next();
44-
} else if (
45-
backendKeys.includes(
46-
/** @type {string} */ (req.get('FT-Next-Backend-Key'))
47-
)
48-
) {
49-
res.set('FT-Backend-Authentication', 'true');
50-
next();
51-
} else if (
52-
backendKeys.includes(
53-
/** @type {string} */ (req.get('FT-Next-Backend-Key-Old'))
54-
)
55-
) {
56-
res.set('FT-Backend-Authentication', 'true');
57-
next();
38+
/** @type {Express.Handler} */
39+
const backendAuthentication = (req, res, next) => {
40+
// allow static assets, healthchecks, etc., through
41+
if (req.path.indexOf('/__') === 0) {
42+
next();
43+
} else if (
44+
backendKeys.includes(
45+
/** @type {string} */ (req.get('FT-Next-Backend-Key'))
46+
)
47+
) {
48+
res.set('FT-Backend-Authentication', 'true');
49+
next();
50+
} else if (
51+
backendKeys.includes(
52+
/** @type {string} */ (req.get('FT-Next-Backend-Key-Old'))
53+
)
54+
) {
55+
res.set('FT-Backend-Authentication', 'true');
56+
next();
57+
} else {
58+
res.set('FT-Backend-Authentication', 'false');
59+
if (process.env.NODE_ENV === 'production') {
60+
// Setting the WWW-Authenticate header tells ft.com-cdn
61+
// to serve stale content instead of 401s if there's a key error.
62+
res.set('WWW-Authenticate', 'FT-Backend-Key');
63+
res.status(401).json({
64+
status: 'Error',
65+
reason: 'Invalid backend key',
66+
source: 'n-express'
67+
});
5868
} else {
59-
res.set('FT-Backend-Authentication', 'false');
60-
if (process.env.NODE_ENV === 'production') {
61-
// Setting the WWW-Authenticate header tells ft.com-cdn
62-
// to serve stale content instead of 401s if there's a key error.
63-
res.set('WWW-Authenticate', 'FT-Backend-Key');
64-
res.status(401).json({
65-
status: 'Error',
66-
reason: 'Invalid backend key',
67-
source: 'n-express'
68-
});
69-
} else {
70-
next();
71-
}
69+
next();
7270
}
7371
}
74-
);
72+
};
73+
app.use(backendAuthentication);
7574
};

src/middleware/consent.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* @type {Callback}
77
*/
8-
module.exports = (req, res, next) => {
8+
const consent = (req, res, next) => {
99
res.locals = res.locals || {};
1010
res.locals.consent = res.locals.consent || {};
1111

@@ -28,3 +28,5 @@ module.exports = (req, res, next) => {
2828
}
2929
next();
3030
};
31+
32+
module.exports = consent;

src/middleware/security.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
/**
66
* @type {Callback}
77
*/
8-
module.exports = (_req, res, next) => {
8+
const security = (_req, res, next) => {
99
res.set('X-Content-Type-Options', 'nosniff');
1010
res.set('X-Download-Options', 'noopen');
1111
res.set('X-XSS-Protection', '1; mode=block');
1212
next();
1313
};
14+
module.exports = security;

src/middleware/vary.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const extendVary = (val, set) => {
1818
/**
1919
* @type {Callback}
2020
*/
21-
module.exports = (_req, res, next) => {
21+
const vary = (_req, res, next) => {
2222
const resSet = res.set;
2323

2424
/** @type {Set<string>} */
@@ -74,3 +74,5 @@ module.exports = (_req, res, next) => {
7474

7575
next();
7676
};
77+
78+
module.exports = vary;

0 commit comments

Comments
 (0)