@apolitical/server 2.5.0 → 2.5.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,13 @@ 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.1] - 2022-06-01
9
+ ### Added
10
+ - Cache to `readinessCheck` and `livenessCheck` checks to avoid overload
11
+
8
12
  ## [2.5.0] - 2022-05-06
9
13
  ### Added
10
- - Custom `readinessCheck` and `livenessCheck` to define liveness externally
14
+ - Custom `readinessCheck` and `livenessCheck` to define probes externally
11
15
  - New probe `/health` endpoint to combine readiness and liveness
12
16
 
13
17
  ## [2.4.2] - 2022-03-03
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apolitical/server",
3
- "version": "2.5.0",
3
+ "version": "2.5.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
+ "audit-ci": "6.3.0",
52
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,10 @@ 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
+ },
52
56
  },
53
57
  JWT: {
54
58
  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),
@@ -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() {