@apolitical/server 2.1.0-beta.1 → 2.2.0-rc.fallback.2

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,19 @@ 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.2.0] - 2022-02-07
9
+ ### Changed
10
+ - Pipeline uses `Node-Publish` template
11
+
12
+ ## [2.1.1] - 2022-01-31
13
+ ### Fixed
14
+ - Authorisation logic
15
+
8
16
  ## [2.1.0] - 2022-01-21
9
17
  ### Added
10
18
  - Prerender middleware
19
+ ### Fixed
20
+ - Node.js version
11
21
 
12
22
  ## [2.0.1] - 2022-01-21
13
23
  ### Added
package/README.md CHANGED
@@ -6,7 +6,7 @@ Node.js module to encapsulate Apolitical's express server setup
6
6
 
7
7
  Requires the following to run:
8
8
 
9
- - [node.js][node] 16.13.2+
9
+ - [node.js][node] 16.13.0+
10
10
  - [yarn][yarn]
11
11
 
12
12
  [node]: https://nodejs.org/en/download/
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apolitical/server",
3
- "version": "2.1.0-beta.1",
3
+ "version": "2.2.0-rc.fallback.2",
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",
@@ -24,14 +24,14 @@
24
24
  "Node Modules"
25
25
  ],
26
26
  "dependencies": {
27
- "@apolitical/logger": "2.0.0",
27
+ "@apolitical/logger": "2.0.1",
28
28
  "@cloudnative/health-connect": "2.1.0",
29
- "awilix": "6.0.0",
29
+ "awilix": "6.1.0",
30
30
  "body-parser": "1.19.1",
31
31
  "compression": "1.7.4",
32
32
  "cookie-parser": "1.4.6",
33
33
  "cors": "2.8.5",
34
- "dotenv": "14.2.0",
34
+ "dotenv": "15.0.0",
35
35
  "express": "4.17.2",
36
36
  "express-jwt": "6.1.0",
37
37
  "http-status-codes": "2.2.0",
@@ -46,18 +46,18 @@
46
46
  "swagger-ui-express": "4.3.0"
47
47
  },
48
48
  "devDependencies": {
49
- "@apolitical/eslint-config": "1.0.1",
50
- "@apolitical/testing": "1.0.1",
49
+ "@apolitical/eslint-config": "1.0.2",
50
+ "@apolitical/testing": "1.0.2",
51
51
  "audit-ci": "5.1.2",
52
52
  "husky": "7.0.4",
53
53
  "jest": "27.4.7",
54
54
  "jest-junit": "13.0.0",
55
- "lint-staged": "12.2.2",
55
+ "lint-staged": "12.3.2",
56
56
  "mock-jwks": "1.0.1",
57
57
  "nock": "13.2.2"
58
58
  },
59
59
  "engines": {
60
- "node": ">=16.13.2"
60
+ "node": ">=16.13.0"
61
61
  },
62
62
  "eslintConfig": {
63
63
  "extends": "@apolitical/eslint-config/base.config"
package/src/container.js CHANGED
@@ -36,7 +36,7 @@ const documentationLoader = require('./loaders/documentation.loader');
36
36
  const loggerLoader = require('./loaders/logger.loader');
37
37
  const middlewaresLoader = require('./loaders/middlewares.loader');
38
38
  const probesLoader = require('./loaders/probes.loader');
39
- const staticLoader = require('./loaders/static.loader');
39
+ const fallbackLoader = require('./loaders/fallback.loader');
40
40
  // Middleware
41
41
  const authenticationMiddleware = require('./middlewares/authentication.middleware');
42
42
  const authorisationMiddleware = require('./middlewares/authorisation.middleware');
@@ -85,7 +85,7 @@ container.register({
85
85
  loggerLoader: asFunction(loggerLoader).singleton(),
86
86
  middlewaresLoader: asFunction(middlewaresLoader).singleton(),
87
87
  probesLoader: asFunction(probesLoader).singleton(),
88
- staticLoader: asFunction(staticLoader).singleton(),
88
+ fallbackLoader: asFunction(fallbackLoader).singleton(),
89
89
  // Middlewares
90
90
  authenticationMiddleware: asFunction(authenticationMiddleware).singleton(),
91
91
  authorisationMiddleware: asFunction(authorisationMiddleware).singleton(),
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ module.exports = ({ logger }) => {
4
+ return function load(app, { fallbackLoader, staticFiles }) {
5
+ const childLogger = logger.where(__filename, 'load');
6
+ childLogger.debug('Started');
7
+
8
+ if (staticFiles) {
9
+ const { baseUrl, indexFilePath } = staticFiles;
10
+ // Load fallback or static content
11
+ if (fallbackLoader) {
12
+ app.get(`${baseUrl}*`, fallbackLoader);
13
+ } else if (indexFilePath) {
14
+ app.get(`${baseUrl}*`, function (req, res) {
15
+ res.sendFile(indexFilePath);
16
+ });
17
+ }
18
+ }
19
+ childLogger.debug('Finished');
20
+ };
21
+ };
@@ -1,9 +1,18 @@
1
1
  'use strict';
2
2
 
3
- module.exports = ({ bodyParser: { json, urlencoded }, compression, cors, cookieParser, config, logger }) => {
3
+ module.exports = ({
4
+ bodyParser: { json, urlencoded },
5
+ express,
6
+ compression,
7
+ cors,
8
+ cookieParser,
9
+ prerender,
10
+ config,
11
+ logger,
12
+ }) => {
4
13
  const { BODY_PARSER_OPTIONS, CORS_OPTIONS } = config.SERVER;
5
14
 
6
- return function load(app, { corsOptions }) {
15
+ return function load(app, { staticFiles, corsOptions, prerenderToken }) {
7
16
  const childLogger = logger.where(__filename, 'load');
8
17
  childLogger.debug('Started');
9
18
  // Load useful middlewares
@@ -12,6 +21,13 @@ module.exports = ({ bodyParser: { json, urlencoded }, compression, cors, cookieP
12
21
  app.use(urlencoded(BODY_PARSER_OPTIONS));
13
22
  app.use(cors(Object.assign({}, CORS_OPTIONS, corsOptions)));
14
23
  app.use(compression());
24
+ if (prerenderToken) {
25
+ app.use(prerender.set('prerenderToken', prerenderToken));
26
+ }
27
+ if (staticFiles) {
28
+ const { baseUrl, folderPath } = staticFiles;
29
+ app.use(baseUrl, express.static(folderPath));
30
+ }
15
31
  childLogger.debug('Finished');
16
32
  };
17
33
  };
@@ -8,21 +8,21 @@ module.exports = ({ logger, serverError: { Forbidden }, config }) => {
8
8
  return function handler(req, res, next) {
9
9
  const childLogger = logger.where(__filename, 'handler');
10
10
  childLogger.debug('Started');
11
- let isNotMyselfSlug = false;
12
11
  let isNotAdmin = false;
13
- // Check myself param
14
- if (myselfSource) {
15
- isNotMyselfSlug = req.params[myselfSource] !== MYSELF_SLUG;
16
- }
12
+ let isNotMyselfSlug = false;
17
13
  // Check user role (from JWT)
18
14
  if (!allowNonAdmin) {
19
15
  isNotAdmin = req.user && req.user.role !== ADMIN_ROLE;
20
16
  }
17
+ // Check myself param
18
+ if (myselfSource) {
19
+ isNotMyselfSlug = req.params[myselfSource] !== MYSELF_SLUG;
20
+ }
21
21
  // Check permissions and prevent unauthorised requests
22
22
  let unauthorised = false;
23
- if (isNotMyselfSlug && myselfSource && isNotAdmin && !allowNonAdmin) {
23
+ if (!allowNonAdmin && isNotAdmin && isNotMyselfSlug) {
24
24
  unauthorised = true;
25
- } else if (isNotAdmin && !allowNonAdmin) {
25
+ } else if (!allowNonAdmin && isNotAdmin && !myselfSource) {
26
26
  unauthorised = true;
27
27
  }
28
28
  if (unauthorised) {
@@ -11,7 +11,7 @@ module.exports = ({
11
11
  errorMiddleware,
12
12
  middlewaresLoader,
13
13
  probesLoader,
14
- staticLoader,
14
+ fallbackLoader,
15
15
  jwtService,
16
16
  serverError,
17
17
  }) => {
@@ -52,10 +52,10 @@ module.exports = ({
52
52
  }
53
53
  // Load documentation
54
54
  documentationLoader(app, opts);
55
- // Load static resources
56
- if (opts.staticFiles) {
57
- staticLoader(app, opts);
58
- }
55
+
56
+ // Load static resources or fallback
57
+ fallbackLoader(app, opts);
58
+
59
59
  // Use error handler
60
60
  if (opts.handleErrors) {
61
61
  app.use(errorMiddleware(opts));
@@ -1,19 +0,0 @@
1
- 'use strict';
2
-
3
- module.exports = ({ express, prerender, logger }) => {
4
- return function load(app, { staticFiles: { baseUrl, folderPath, indexFilePath } }) {
5
- const childLogger = logger.where(__filename, 'load');
6
- childLogger.debug('Started');
7
- // Use prerendering middleware
8
- app.use(prerender);
9
- // Use Express built-in middleware
10
- app.use(baseUrl, express.static(folderPath));
11
- // Load static endpoint
12
- if (indexFilePath) {
13
- app.get(`${baseUrl}*`, function (req, res) {
14
- res.sendFile(indexFilePath);
15
- });
16
- }
17
- childLogger.debug('Finished');
18
- };
19
- };