@apolitical/server 2.5.0-rc.2 → 2.5.2-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -5,9 +5,18 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [2.5.2] - 2022-06-08
9
+ ### Added
10
+ - Probes prefix to support custom path
11
+
12
+ ## [2.5.1] - 2022-06-01
13
+ ### Added
14
+ - Cache to `readinessCheck` and `livenessCheck` checks to avoid overload
15
+
8
16
  ## [2.5.0] - 2022-05-06
9
17
  ### Added
10
- - Custom `livenessCheck` to define liveness externally
18
+ - Custom `readinessCheck` and `livenessCheck` to define probes externally
19
+ - New probe `/health` endpoint to combine readiness and liveness
11
20
 
12
21
  ## [2.4.2] - 2022-03-03
13
22
  ### Bumped
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apolitical/server",
3
- "version": "2.5.0-rc.2",
3
+ "version": "2.5.2-rc.1",
4
4
  "description": "Node.js module to encapsulate Apolitical's express server setup",
5
5
  "author": "Apolitical Group Limited <engineering@apolitical.co>",
6
6
  "license": "MIT",
@@ -31,28 +31,29 @@
31
31
  "compression": "1.7.4",
32
32
  "cookie-parser": "1.4.6",
33
33
  "cors": "2.8.5",
34
- "dotenv": "16.0.0",
34
+ "dotenv": "16.0.1",
35
35
  "express": "4.18.1",
36
- "express-jwt": "7.7.0",
36
+ "express-jwt": "7.7.5",
37
37
  "http-status-codes": "2.2.0",
38
38
  "http-terminator": "3.2.0",
39
- "jsrsasign": "10.5.20",
40
- "jwks-rsa": "2.1.1",
39
+ "jsrsasign": "10.5.23",
40
+ "jwks-rsa": "2.1.3",
41
41
  "jwt-decode": "3.1.2",
42
+ "lru-cache": "7.10.1",
42
43
  "morgan": "1.10.0",
43
- "passport": "0.5.2",
44
+ "passport": "0.6.0",
44
45
  "passport-jwt": "4.0.0",
45
46
  "prerender-node": "3.5.0",
46
- "swagger-ui-express": "4.3.0"
47
+ "swagger-ui-express": "4.4.0"
47
48
  },
48
49
  "devDependencies": {
49
- "@apolitical/eslint-config": "2.0.0",
50
+ "@apolitical/eslint-config": "2.0.1",
50
51
  "@apolitical/testing": "1.0.2",
51
- "audit-ci": "6.2.0",
52
- "husky": "8.0.0",
52
+ "audit-ci": "6.3.0",
53
+ "husky": "8.0.1",
53
54
  "jest": "28.1.0",
54
55
  "jest-junit": "13.2.0",
55
- "lint-staged": "12.4.1",
56
+ "lint-staged": "13.0.0",
56
57
  "mock-jwks": "1.0.3",
57
58
  "nock": "13.2.4"
58
59
  },
package/src/config.js CHANGED
@@ -49,6 +49,20 @@ module.exports = {
49
49
  USER_SLUG: 'user-slug',
50
50
  },
51
51
  },
52
+ CACHE_OPTIONS: {
53
+ max: 2, // Only liveness and readiness
54
+ ttl: 60 * 1000, // One minute
55
+ },
56
+ PROBES_OPTIONS: {
57
+ API: {
58
+ ALLOWED_PREFIXES: ['api'],
59
+ PREFIX_PATH: '/api',
60
+ },
61
+ UI: {
62
+ ALLOWED_PREFIXES: ['ui', 'pages'],
63
+ PREFIX_PATH: '/ui',
64
+ },
65
+ },
52
66
  },
53
67
  JWT: {
54
68
  APOLITICAL: {
package/src/container.js CHANGED
@@ -14,6 +14,7 @@ const httpTerminator = require('http-terminator');
14
14
  const jwksRsa = require('jwks-rsa');
15
15
  const jsrsasign = require('jsrsasign');
16
16
  const jwtDecode = require('jwt-decode');
17
+ const lru = require('lru-cache');
17
18
  const morgan = require('morgan');
18
19
  const passport = require('passport');
19
20
  const passportJwt = require('passport-jwt');
@@ -67,6 +68,7 @@ container.register({
67
68
  jwksRsa: asValue(jwksRsa),
68
69
  jsrsasign: asValue(jsrsasign),
69
70
  jwtDecode: asValue(jwtDecode),
71
+ lru: asValue(lru),
70
72
  morgan: asValue(morgan),
71
73
  passport: asValue(passport),
72
74
  passportJwt: asValue(passportJwt),
@@ -2,21 +2,41 @@
2
2
 
3
3
  module.exports = ({ config, logger, healthService }) => {
4
4
  const { HEALTH, LIVENESS, READINESS } = config.ENDPOINTS.PROBES;
5
+ const { API, UI } = config.SERVER.PROBES_OPTIONS;
5
6
 
6
- return function load(app, { readinessCheck, livenessCheck }) {
7
+ function buildPrefix({ prefix }) {
8
+ let result = '';
9
+ if (prefix) {
10
+ if (API.ALLOWED_PREFIXES.some((allowedPrefix) => prefix.includes(allowedPrefix))) {
11
+ result = `${API.PREFIX_PATH}/${prefix}`;
12
+ } else if (UI.ALLOWED_PREFIXES.some((allowedPrefix) => prefix.includes(allowedPrefix))) {
13
+ result = `${UI.PREFIX_PATH}/${prefix}`;
14
+ } else {
15
+ result = prefix;
16
+ }
17
+ }
18
+ return result;
19
+ }
20
+
21
+ return function load(app, { probes }) {
7
22
  const childLogger = logger.where(__filename, 'load');
8
23
  childLogger.debug('Started');
9
- // Overwrite checks when defined
10
- if (readinessCheck) {
11
- healthService.registerReadiness(readinessCheck);
12
- }
13
- if (livenessCheck) {
14
- healthService.registerLiveness(livenessCheck);
24
+ if (probes) {
25
+ const { readinessCheck, livenessCheck } = probes;
26
+ // Overwrite checks when defined
27
+ if (readinessCheck) {
28
+ healthService.registerReadiness(readinessCheck);
29
+ }
30
+ if (livenessCheck) {
31
+ healthService.registerLiveness(livenessCheck);
32
+ }
33
+ // Build base URL
34
+ const probesPath = buildPrefix(probes);
35
+ // Load probes endpoints
36
+ app.get(`${probesPath}${HEALTH}`, healthService.healthEndpoint());
37
+ app.get(`${probesPath}${READINESS}`, healthService.readinessEndpoint());
38
+ app.get(`${probesPath}${LIVENESS}`, healthService.livenessEndpoint());
15
39
  }
16
- // Load probes endpoints
17
- app.get(HEALTH, healthService.healthEndpoint());
18
- app.get(READINESS, healthService.readinessEndpoint());
19
- app.get(LIVENESS, healthService.livenessEndpoint());
20
40
  childLogger.debug('Finished');
21
41
  };
22
42
  };
@@ -1,8 +1,22 @@
1
1
  'use strict';
2
2
 
3
- module.exports = ({ health }) => {
3
+ module.exports = ({ health, lru, config }) => {
4
+ const { CACHE_OPTIONS } = config.SERVER;
4
5
  // Setup health checker
5
6
  const healthCheck = new health.HealthChecker();
7
+ // Setup cache
8
+ const cache = new lru(CACHE_OPTIONS);
9
+
10
+ function buildCheckWithCache(key, check) {
11
+ cache.clear();
12
+ return async function () {
13
+ const checked = cache.get(key);
14
+ if (!checked) {
15
+ await check();
16
+ cache.set(key, true);
17
+ }
18
+ };
19
+ }
6
20
 
7
21
  // function registerStartup(startup) {
8
22
  // healthCheck.registerStartupCheck(new health.StartupCheck("Startup Check", startup));
@@ -13,7 +27,8 @@ module.exports = ({ health }) => {
13
27
  }
14
28
 
15
29
  function registerReadiness(readiness) {
16
- healthCheck.registerReadinessCheck(new health.ReadinessCheck('Readiness Check', readiness));
30
+ const readinessWithCache = buildCheckWithCache('readiness', readiness);
31
+ healthCheck.registerReadinessCheck(new health.ReadinessCheck('Readiness Check', readinessWithCache));
17
32
  }
18
33
 
19
34
  function readinessEndpoint() {
@@ -21,7 +36,8 @@ module.exports = ({ health }) => {
21
36
  }
22
37
 
23
38
  function registerLiveness(liveness) {
24
- healthCheck.registerLivenessCheck(new health.LivenessCheck('Liveness Check', liveness));
39
+ const livenessWithCache = buildCheckWithCache('liveness', liveness);
40
+ healthCheck.registerLivenessCheck(new health.LivenessCheck('Liveness Check', livenessWithCache));
25
41
  }
26
42
 
27
43
  function livenessEndpoint() {