@mimik/init 5.2.2 → 6.0.0
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/README.md +4 -4
- package/eslint.config.js +63 -0
- package/index.js +81 -68
- package/lib/common.js +1 -1
- package/lib/exit.js +10 -9
- package/lib/logs.js +9 -10
- package/lib/metrics.js +4 -2
- package/lib/route.js +42 -11
- package/package.json +21 -23
- package/.eslintrc +0 -43
package/README.md
CHANGED
|
@@ -3,16 +3,16 @@
|
|
|
3
3
|
## init
|
|
4
4
|
**Example**
|
|
5
5
|
```js
|
|
6
|
-
|
|
6
|
+
import init from '@mimik/init';
|
|
7
7
|
|
|
8
8
|
init(config, dbValidate, { postOps: [suscribe] }).then(result => config = result);
|
|
9
9
|
```
|
|
10
|
-
<a name="
|
|
10
|
+
<a name="module_init..init"></a>
|
|
11
11
|
|
|
12
|
-
###
|
|
12
|
+
### init~init(app, rootDir, config, validates, cluster, options) ⇒ <code>object</code>
|
|
13
13
|
Init process for a micro-service.
|
|
14
14
|
|
|
15
|
-
**Kind**:
|
|
15
|
+
**Kind**: inner method of [<code>init</code>](#module_init)
|
|
16
16
|
**Returns**: <code>object</code> - The updated configuration.
|
|
17
17
|
|
|
18
18
|
The secOptions has the following structure:
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import importPlugin from 'eslint-plugin-import';
|
|
2
|
+
import js from '@eslint/js';
|
|
3
|
+
import processDoc from '@mimik/eslint-plugin-document-env';
|
|
4
|
+
import stylistic from '@stylistic/eslint-plugin';
|
|
5
|
+
|
|
6
|
+
const MAX_LENGTH_LINE = 180;
|
|
7
|
+
const MAX_FUNCTION_PARAMETERS = 7;
|
|
8
|
+
const MAX_LINES_IN_FILES = 600;
|
|
9
|
+
const MAX_LINES_IN_FUNCTION = 150;
|
|
10
|
+
const MAX_STATEMENTS_IN_FUNCTION = 45;
|
|
11
|
+
const MIN_KEYS_IN_OBJECT = 10;
|
|
12
|
+
const MAX_COMPLEXITY = 30;
|
|
13
|
+
|
|
14
|
+
export default [
|
|
15
|
+
{
|
|
16
|
+
ignores: ['mochawesome-report/**', 'node_modules/**', 'dist/**'],
|
|
17
|
+
},
|
|
18
|
+
importPlugin.flatConfigs.recommended,
|
|
19
|
+
stylistic.configs.recommended,
|
|
20
|
+
js.configs.all,
|
|
21
|
+
{
|
|
22
|
+
plugins: {
|
|
23
|
+
processDoc,
|
|
24
|
+
},
|
|
25
|
+
languageOptions: {
|
|
26
|
+
ecmaVersion: 2022,
|
|
27
|
+
globals: {
|
|
28
|
+
console: 'readonly',
|
|
29
|
+
describe: 'readonly',
|
|
30
|
+
it: 'readonly',
|
|
31
|
+
require: 'readonly',
|
|
32
|
+
},
|
|
33
|
+
sourceType: 'module',
|
|
34
|
+
},
|
|
35
|
+
rules: {
|
|
36
|
+
'@stylistic/brace-style': ['warn', 'stroustrup', { allowSingleLine: true }],
|
|
37
|
+
'@stylistic/line-comment-position': ['off'],
|
|
38
|
+
'@stylistic/semi': ['error', 'always'],
|
|
39
|
+
'capitalized-comments': ['off'],
|
|
40
|
+
'complexity': ['error', MAX_COMPLEXITY],
|
|
41
|
+
'curly': ['off'],
|
|
42
|
+
'id-length': ['error', { exceptions: ['x', 'y', 'z', 'i', 'j', 'k'] }],
|
|
43
|
+
'import/no-extraneous-dependencies': ['error', { devDependencies: true }],
|
|
44
|
+
'import/no-unresolved': ['error', { amd: true, caseSensitiveStrict: true, commonjs: true }],
|
|
45
|
+
'init-declarations': ['off'],
|
|
46
|
+
'linebreak-style': ['off'],
|
|
47
|
+
'max-len': ['warn', MAX_LENGTH_LINE, { ignoreComments: true }],
|
|
48
|
+
'max-lines': ['warn', { max: MAX_LINES_IN_FILES, skipComments: true }],
|
|
49
|
+
'max-lines-per-function': ['warn', { max: MAX_LINES_IN_FUNCTION, skipComments: true }],
|
|
50
|
+
'max-params': ['error', MAX_FUNCTION_PARAMETERS],
|
|
51
|
+
'max-statements': ['warn', MAX_STATEMENTS_IN_FUNCTION],
|
|
52
|
+
'no-confusing-arrow': ['off'], // arrow isnt confusing
|
|
53
|
+
'no-inline-comments': ['off'],
|
|
54
|
+
'no-process-env': ['error'],
|
|
55
|
+
'no-ternary': ['off'],
|
|
56
|
+
'no-undefined': ['off'],
|
|
57
|
+
'one-var': ['error', 'never'],
|
|
58
|
+
'processDoc/validate-document-env': ['error'],
|
|
59
|
+
'quotes': ['warn', 'single'],
|
|
60
|
+
'sort-keys': ['error', 'asc', { caseSensitive: true, minKeys: MIN_KEYS_IN_OBJECT, natural: false }],
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
];
|
package/index.js
CHANGED
|
@@ -1,37 +1,41 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const pathLib = require('path');
|
|
4
|
-
const Promise = require('bluebird');
|
|
5
|
-
const helmet = require('helmet');
|
|
6
|
-
|
|
7
|
-
const logger = require('@mimik/sumologic-winston-logger');
|
|
8
|
-
const { startupHealthInfo } = require('@mimik/healthcheck');
|
|
9
|
-
const { getPublic } = require('@mimik/public-helper');
|
|
10
|
-
const { getCorrelationId } = require('@mimik/request-helper');
|
|
11
|
-
const { apiSetup, setupServerFiles } = require('@mimik/api-helper');
|
|
12
|
-
|
|
13
|
-
const { extractLogs } = require('./lib/logs');
|
|
14
|
-
const { sigProcess } = require('./lib/exit');
|
|
15
|
-
const { startHrTimeSet } = require('./lib/metrics');
|
|
16
|
-
const { healthCheckRoute, metricsRoute } = require('./lib/route');
|
|
17
|
-
const {
|
|
1
|
+
import {
|
|
2
|
+
BUILD_FILENAME,
|
|
18
3
|
HEALTHCHECK_ROUTE,
|
|
4
|
+
LOCAL,
|
|
19
5
|
METRICS_ROUTE,
|
|
6
|
+
SET_ON,
|
|
20
7
|
SIGINT,
|
|
21
8
|
SIGTERM,
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
9
|
+
} from './lib/common.js';
|
|
10
|
+
import { apiSetup, setupServerFiles } from '@mimik/api-helper';
|
|
11
|
+
import { bodyParserRoute, healthCheckRoute, metricsRoute } from './lib/route.js';
|
|
12
|
+
import Promise from 'bluebird';
|
|
13
|
+
import cors from 'cors';
|
|
14
|
+
import { extractLogs } from './lib/logs.js';
|
|
15
|
+
import { getCorrelationId } from '@mimik/request-helper';
|
|
16
|
+
import { getPublic } from '@mimik/public-helper';
|
|
17
|
+
import helmet from 'helmet';
|
|
18
|
+
import http from 'http';
|
|
19
|
+
import logger from '@mimik/sumologic-winston-logger';
|
|
20
|
+
import pathLib from 'path';
|
|
21
|
+
import process from 'process';
|
|
22
|
+
import { sigProcess } from './lib/exit.js';
|
|
23
|
+
import { startHrTimeSet } from './lib/metrics.js';
|
|
24
|
+
import { startupHealthInfo } from '@mimik/healthcheck';
|
|
26
25
|
|
|
27
26
|
/**
|
|
28
27
|
* @module init
|
|
29
28
|
* @example
|
|
30
|
-
*
|
|
29
|
+
* import init from '@mimik/init';
|
|
31
30
|
*
|
|
32
31
|
* init(config, dbValidate, { postOps: [suscribe] }).then(result => config = result);
|
|
33
32
|
*
|
|
34
33
|
*/
|
|
34
|
+
|
|
35
|
+
const EXIT_ERROR = 1;
|
|
36
|
+
const FIRST = 0;
|
|
37
|
+
const FIRST_PATH_ELEMENT = 0;
|
|
38
|
+
|
|
35
39
|
/**
|
|
36
40
|
* Init process for a micro-service.
|
|
37
41
|
*
|
|
@@ -64,11 +68,11 @@ const {
|
|
|
64
68
|
* The following routes are reserved: `/healthcheck` and `/metrics`.
|
|
65
69
|
* The base path is defined by taking the url property of the first item of the servers array in the api defintion file. Only relative url address will be handled (e.g. /mss/v1) and templates in the url are not allowed.
|
|
66
70
|
*/
|
|
67
|
-
|
|
71
|
+
const init = (app, rootDir, config, validates, cluster, options) => {
|
|
68
72
|
const fatalError = (error, correlationId) => {
|
|
69
|
-
if (
|
|
70
|
-
else logger.error('fatal error: could not start server', {
|
|
71
|
-
logger.flushAndExit(
|
|
73
|
+
if (config) logger.error('fatal error: could not start server', { name: config.serverSettings.name, id: config.serverSettings.id, error }, correlationId);
|
|
74
|
+
else logger.error('fatal error: could not start server', { error }, getCorrelationId('system-start'));
|
|
75
|
+
logger.flushAndExit(EXIT_ERROR);
|
|
72
76
|
};
|
|
73
77
|
|
|
74
78
|
if (!config) return fatalError(new Error('config is required'));
|
|
@@ -101,7 +105,9 @@ module.exports = (app, rootDir, config, validates, cluster, options) => {
|
|
|
101
105
|
let apiFilename = serverSettings.api;
|
|
102
106
|
let registration = false;
|
|
103
107
|
|
|
104
|
-
if ((config.nodeEnvironment.toLowerCase() !== LOCAL) || (config.registration.set === SET_ON)) {
|
|
108
|
+
if ((config.nodeEnvironment.toLowerCase() !== LOCAL) || (config.registration.set === SET_ON)) {
|
|
109
|
+
registration = true;
|
|
110
|
+
}
|
|
105
111
|
else logger.warn('registration disabled: cluster will not work', correlationIdStart);
|
|
106
112
|
if (extractName) {
|
|
107
113
|
if (!dependencies || !dependencies.mLG) return fatalError(new Error('requesting extraction without mLG configuration'), correlationIdStart);
|
|
@@ -116,6 +122,7 @@ module.exports = (app, rootDir, config, validates, cluster, options) => {
|
|
|
116
122
|
sigProcess(SIGTERM, registration, options, config, server, unRegister, getCorrelationId(`system-shutdown(${SIGTERM}):${serverSettings.id}`));
|
|
117
123
|
});
|
|
118
124
|
|
|
125
|
+
app.use(bodyParserRoute());
|
|
119
126
|
app.use(helmet.contentSecurityPolicy({
|
|
120
127
|
directives: {
|
|
121
128
|
...helmet.contentSecurityPolicy.getDefaultDirectives(),
|
|
@@ -132,13 +139,15 @@ module.exports = (app, rootDir, config, validates, cluster, options) => {
|
|
|
132
139
|
if (oidcOps && typeof oidcOps === 'function') {
|
|
133
140
|
oidcOps();
|
|
134
141
|
}
|
|
135
|
-
if (extractName) {
|
|
136
|
-
|
|
142
|
+
if (extractName) {
|
|
143
|
+
app.use(extractLogs(config, extractName, options));
|
|
144
|
+
}
|
|
145
|
+
if (apiFilename[FIRST_PATH_ELEMENT] !== '/') apiFilename = pathLib.join(rootDir, apiFilename);
|
|
137
146
|
logger.debug('location of the api file', { apiFilename }, correlationIdStart);
|
|
138
147
|
return setupServerFiles(apiFilename, controllersDirectory, buildDirectory, correlationIdStart, updateOptions(options))
|
|
139
148
|
.then((setupResult) => {
|
|
140
|
-
serverSettings.basePath = setupResult.apiDefinition.servers[
|
|
141
|
-
// this will be deprecated by init
|
|
149
|
+
serverSettings.basePath = setupResult.apiDefinition.servers[FIRST].url; // need to address that later
|
|
150
|
+
// this will be deprecated by init 7.0, to handle lower case paths
|
|
142
151
|
app.use((req, res, next) => {
|
|
143
152
|
const parts = req.url.split('/');
|
|
144
153
|
const partsLength = parts.length;
|
|
@@ -153,47 +162,51 @@ module.exports = (app, rootDir, config, validates, cluster, options) => {
|
|
|
153
162
|
next();
|
|
154
163
|
});
|
|
155
164
|
// end of deprecation
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
165
|
+
return import(pathLib.resolve(buildRequire))
|
|
166
|
+
.then((registeredOperationsModule) => {
|
|
167
|
+
const registeredOperations = registeredOperationsModule.default;
|
|
168
|
+
const api = apiSetup(setupResult, registeredOperations, options?.secOptions, options?.formats, config, correlationIdStart);
|
|
159
169
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
170
|
+
app.use((req, res) => api.handleRequest(req, req, res));
|
|
171
|
+
return Promise.each(validates, validate => validate())
|
|
172
|
+
.then(() => getPublic(config.locationProvider, config.cloudProvider, correlationIdStart, options).then((publicConfig) => {
|
|
173
|
+
logger.info('public environment', { status: publicConfig.status }, correlationIdStart);
|
|
174
|
+
serverSettings.ip.public = publicConfig.ip || serverSettings.ip.public;
|
|
175
|
+
serverSettings.instanceId = publicConfig.id;
|
|
176
|
+
serverSettings.public.location = publicConfig.location || serverSettings.public.location;
|
|
177
|
+
}))
|
|
178
|
+
.then(() => {
|
|
179
|
+
if (options && options.preOps) return Promise.each(options.preOps, ops => ops(correlationIdStart, config, server));
|
|
180
|
+
return null;
|
|
181
|
+
})
|
|
182
|
+
.then(() => {
|
|
183
|
+
server.listen(serverPort, () => {
|
|
184
|
+
startupHealthInfo(config, correlationIdStart);
|
|
185
|
+
const afterPostOps = config.registration.afterPostOpsSet === SET_ON;
|
|
176
186
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
187
|
+
return Promise.resolve()
|
|
188
|
+
.then(() => {
|
|
189
|
+
if (afterPostOps) {
|
|
190
|
+
if (options && options.postOps) return Promise.each(options.postOps, ops => ops(correlationIdStart, config, server));
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
if (registration) return register(correlationIdStart);
|
|
194
|
+
return null;
|
|
195
|
+
})
|
|
196
|
+
.then(() => {
|
|
197
|
+
if (afterPostOps) {
|
|
198
|
+
if (registration) return register(correlationIdStart);
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
if (options && options.postOps) return Promise.each(options.postOps, ops => ops(correlationIdStart, config, server));
|
|
202
|
+
return null;
|
|
203
|
+
});
|
|
193
204
|
});
|
|
194
|
-
|
|
205
|
+
});
|
|
195
206
|
});
|
|
196
207
|
})
|
|
197
|
-
.catch(
|
|
208
|
+
.catch(error => fatalError(error, correlationIdStart))
|
|
198
209
|
.then(() => ({ config }));
|
|
199
210
|
};
|
|
211
|
+
|
|
212
|
+
export default init;
|
package/lib/common.js
CHANGED
package/lib/exit.js
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
const logger = require('@mimik/sumologic-winston-logger');
|
|
1
|
+
import Promise from 'bluebird';
|
|
2
|
+
import logger from '@mimik/sumologic-winston-logger';
|
|
4
3
|
|
|
5
4
|
const SHUTDOWN = 'service shutdown successfully';
|
|
6
5
|
const SHUTDOWN_WITH_ERROR = 'service shutdown with error';
|
|
6
|
+
const EXIT_OK = 0;
|
|
7
|
+
const EXIT_ERROR = 1;
|
|
7
8
|
|
|
8
9
|
const shutdown = (res, correlationId) => {
|
|
9
10
|
if (res.errors) {
|
|
10
11
|
logger.warn(SHUTDOWN_WITH_ERROR, res, correlationId);
|
|
11
|
-
logger.flushAndExit(
|
|
12
|
+
logger.flushAndExit(EXIT_ERROR);
|
|
12
13
|
}
|
|
13
14
|
else {
|
|
14
15
|
logger.info(SHUTDOWN, res, correlationId);
|
|
15
|
-
logger.flushAndExit(
|
|
16
|
+
logger.flushAndExit(EXIT_OK);
|
|
16
17
|
}
|
|
17
18
|
};
|
|
18
19
|
|
|
19
|
-
const sigProcess = (signal, registration, options, config, server, unRegister, correlationId) => {
|
|
20
|
+
export const sigProcess = (signal, registration, options, config, server, unRegister, correlationId) => {
|
|
20
21
|
if (registration) {
|
|
21
22
|
return unRegister(correlationId)
|
|
22
23
|
.then((origResult) => {
|
|
@@ -26,7 +27,7 @@ const sigProcess = (signal, registration, options, config, server, unRegister, c
|
|
|
26
27
|
if (!options || !options.exitOps) {
|
|
27
28
|
return shutdown(result, correlationId);
|
|
28
29
|
}
|
|
29
|
-
return Promise.each(options.exitOps,
|
|
30
|
+
return Promise.each(options.exitOps, ops => ops(correlationId, config, server)
|
|
30
31
|
.catch((err) => {
|
|
31
32
|
if (result.errors) result.errors.push(err);
|
|
32
33
|
else result.errors = [err];
|
|
@@ -35,10 +36,10 @@ const sigProcess = (signal, registration, options, config, server, unRegister, c
|
|
|
35
36
|
});
|
|
36
37
|
}
|
|
37
38
|
logger.info(SHUTDOWN, { serverId: config.serverSettings.id, signal }, correlationId);
|
|
38
|
-
logger.flushAndExit(
|
|
39
|
+
logger.flushAndExit(EXIT_OK);
|
|
39
40
|
return null;
|
|
40
41
|
};
|
|
41
42
|
|
|
42
|
-
|
|
43
|
+
export default {
|
|
43
44
|
sigProcess,
|
|
44
45
|
};
|
package/lib/logs.js
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const { getCorrelationId } = require('@mimik/request-helper');
|
|
1
|
+
import { getCorrelationId } from '@mimik/request-helper';
|
|
2
|
+
import { getRichError } from '@mimik/response-helper';
|
|
3
|
+
import logger from '@mimik/sumologic-winston-logger';
|
|
4
|
+
import oauthHelper from '@mimik/oauth-helper';
|
|
5
|
+
import { publicIpFromRequest } from '@mimik/address-helper';
|
|
6
|
+
import urlLib from 'url';
|
|
8
7
|
|
|
9
8
|
const correlationId = getCorrelationId('log-extraction');
|
|
10
9
|
const NOTIFICATION = '/notifications';
|
|
11
10
|
|
|
12
|
-
const extractLogs = (config, extractName, options) => (req, res, next) => {
|
|
11
|
+
export const extractLogs = (config, extractName, options) => (req, res, next) => {
|
|
13
12
|
const { rpAuth } = oauthHelper(config);
|
|
14
13
|
let url = `${config.dependencies.mLG.url}${NOTIFICATION}`;
|
|
15
14
|
const params = new urlLib.URLSearchParams();
|
|
@@ -42,12 +41,12 @@ const extractLogs = (config, extractName, options) => (req, res, next) => {
|
|
|
42
41
|
opts.metrics.url = url;
|
|
43
42
|
}
|
|
44
43
|
rpAuth('mLG', opts)
|
|
45
|
-
.catch(
|
|
44
|
+
.catch(err => getRichError('System', 'could not send extracted data', { url, extractName }, err, 'warn', correlationId));
|
|
46
45
|
delete req.body[extractName];
|
|
47
46
|
}
|
|
48
47
|
next();
|
|
49
48
|
};
|
|
50
49
|
|
|
51
|
-
|
|
50
|
+
export default {
|
|
52
51
|
extractLogs,
|
|
53
52
|
};
|
package/lib/metrics.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
import process from 'process';
|
|
2
|
+
|
|
3
|
+
export const startHrTimeSet = APIRequestDuration => (req, res, next) => {
|
|
2
4
|
req.metrics = { startHrTime: process.hrtime(), APIRequestDuration };
|
|
3
5
|
next();
|
|
4
6
|
};
|
|
5
7
|
|
|
6
|
-
|
|
8
|
+
export default {
|
|
7
9
|
startHrTimeSet,
|
|
8
10
|
};
|
package/lib/route.js
CHANGED
|
@@ -1,15 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
import { CONTENT_TYPE, JSON_CONTENT } from './common.js';
|
|
2
|
+
import { getCorrelationId, getUserAgent } from '@mimik/request-helper';
|
|
3
|
+
import { healthInfo } from '@mimik/systeminfo';
|
|
4
|
+
import http from 'http';
|
|
5
|
+
import logger from '@mimik/sumologic-winston-logger';
|
|
6
|
+
import { publicIpFromRequest } from '@mimik/address-helper';
|
|
2
7
|
|
|
3
|
-
const
|
|
8
|
+
const TAB = 2;
|
|
9
|
+
const SYSTEM_ERROR = 500;
|
|
10
|
+
const PARAMETER_ERROR = 400;
|
|
4
11
|
|
|
5
|
-
const
|
|
6
|
-
|
|
7
|
-
const healthCheckRoute = () => (req, res) => {
|
|
12
|
+
export const healthCheckRoute = () => (req, res) => {
|
|
8
13
|
res.setHeader(CONTENT_TYPE, JSON_CONTENT);
|
|
9
|
-
res.end(JSON.stringify({ data: healthInfo() }, null,
|
|
14
|
+
res.end(JSON.stringify({ data: healthInfo() }, null, TAB));
|
|
10
15
|
};
|
|
11
16
|
|
|
12
|
-
const metricsRoute =
|
|
17
|
+
export const metricsRoute = options => (req, res) => options.metrics.register.metrics()
|
|
13
18
|
.then((result) => {
|
|
14
19
|
res.setHeader(CONTENT_TYPE, options.metrics.register.contentType);
|
|
15
20
|
res.end(result);
|
|
@@ -17,15 +22,41 @@ const metricsRoute = (options) => (req, res) => options.metrics.register.metrics
|
|
|
17
22
|
.catch((err) => {
|
|
18
23
|
res.setHeader(CONTENT_TYPE, JSON_CONTENT);
|
|
19
24
|
const errResponse = {
|
|
20
|
-
statusCode:
|
|
21
|
-
title: http.STATUS_CODES[
|
|
25
|
+
statusCode: SYSTEM_ERROR,
|
|
26
|
+
title: http.STATUS_CODES[SYSTEM_ERROR],
|
|
22
27
|
message: err.message,
|
|
23
28
|
};
|
|
24
29
|
|
|
25
|
-
res.end(JSON.stringify(errResponse), null,
|
|
30
|
+
res.end(JSON.stringify(errResponse), null, TAB);
|
|
26
31
|
});
|
|
27
32
|
|
|
28
|
-
|
|
33
|
+
export const bodyParserRoute = () => (err, req, res, next) => {
|
|
34
|
+
if (err instanceof SyntaxError && err.status === PARAMETER_ERROR && 'body' in err) {
|
|
35
|
+
const { originalUrl } = req;
|
|
36
|
+
const method = req.method.toUpperCase();
|
|
37
|
+
const correlationId = getCorrelationId(req);
|
|
38
|
+
|
|
39
|
+
logger.warn(`Rejecting request with 400 for ${method} ${originalUrl}`, {
|
|
40
|
+
method,
|
|
41
|
+
path: originalUrl,
|
|
42
|
+
statusCode: PARAMETER_ERROR,
|
|
43
|
+
userAgent: getUserAgent(req),
|
|
44
|
+
clientIP: publicIpFromRequest(req),
|
|
45
|
+
error: err,
|
|
46
|
+
}, correlationId);
|
|
47
|
+
if (correlationId) res.setHeader('x-correlation-id', correlationId);
|
|
48
|
+
return res.status(PARAMETER_ERROR).json({
|
|
49
|
+
statusCode: PARAMETER_ERROR,
|
|
50
|
+
title: http.STATUS_CODES[PARAMETER_ERROR],
|
|
51
|
+
message: err.message,
|
|
52
|
+
error: err,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return next();
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
export default {
|
|
29
59
|
healthCheckRoute,
|
|
30
60
|
metricsRoute,
|
|
61
|
+
bodyParserRoute,
|
|
31
62
|
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mimik/init",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.0",
|
|
4
4
|
"description": "Init process for micro-service",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "./index.js",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"scripts": {
|
|
7
|
-
"lint": "eslint --
|
|
8
|
+
"lint": "eslint . --no-error-on-unmatched-pattern",
|
|
8
9
|
"docs": "jsdoc2md index.js > README.md",
|
|
9
10
|
"test": "echo \"Error: no test specified\" && exit 0",
|
|
10
11
|
"test-ci": "echo \"Error: no test specified\" && exit 0",
|
|
@@ -28,29 +29,26 @@
|
|
|
28
29
|
"url": "https://bitbucket.org/mimiktech/init"
|
|
29
30
|
},
|
|
30
31
|
"dependencies": {
|
|
31
|
-
"@mimik/address-helper": "^
|
|
32
|
-
"@mimik/api-helper": "^
|
|
33
|
-
"@mimik/healthcheck": "^
|
|
34
|
-
"@mimik/oauth-helper": "^
|
|
35
|
-
"@mimik/public-helper": "^
|
|
36
|
-
"@mimik/request-helper": "^
|
|
37
|
-
"@mimik/response-helper": "^
|
|
38
|
-
"@mimik/sumologic-winston-logger": "^
|
|
39
|
-
"@mimik/systeminfo": "^
|
|
32
|
+
"@mimik/address-helper": "^2.0.4",
|
|
33
|
+
"@mimik/api-helper": "^2.0.2",
|
|
34
|
+
"@mimik/healthcheck": "^2.0.0",
|
|
35
|
+
"@mimik/oauth-helper": "^4.0.2",
|
|
36
|
+
"@mimik/public-helper": "^3.0.3",
|
|
37
|
+
"@mimik/request-helper": "^2.0.2",
|
|
38
|
+
"@mimik/response-helper": "^4.0.4",
|
|
39
|
+
"@mimik/sumologic-winston-logger": "^2.0.3",
|
|
40
|
+
"@mimik/systeminfo": "^4.0.1",
|
|
40
41
|
"bluebird": "3.7.2",
|
|
41
42
|
"cors": "2.8.5",
|
|
42
|
-
"helmet": "
|
|
43
|
+
"helmet": "8.1.0"
|
|
43
44
|
},
|
|
44
45
|
"devDependencies": {
|
|
45
|
-
"@
|
|
46
|
-
"@mimik/eslint-plugin-document-env": "^
|
|
47
|
-
"eslint": "
|
|
48
|
-
"eslint
|
|
49
|
-
"eslint-plugin-import": "2.
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
"eslint-plugin-react-hooks": "4.6.2",
|
|
53
|
-
"husky": "9.0.11",
|
|
54
|
-
"jsdoc-to-markdown": "8.0.1"
|
|
46
|
+
"@eslint/js": "9.31.0",
|
|
47
|
+
"@mimik/eslint-plugin-document-env": "^2.0.8",
|
|
48
|
+
"@stylistic/eslint-plugin": "5.2.2",
|
|
49
|
+
"eslint": "9.31.0",
|
|
50
|
+
"eslint-plugin-import": "2.32.0",
|
|
51
|
+
"husky": "9.1.7",
|
|
52
|
+
"jsdoc-to-markdown": "9.1.2"
|
|
55
53
|
}
|
|
56
54
|
}
|
package/.eslintrc
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"plugins": [
|
|
3
|
-
"@mimik/document-env",
|
|
4
|
-
"@mimik/dependencies"
|
|
5
|
-
],
|
|
6
|
-
"env": {
|
|
7
|
-
"node": true
|
|
8
|
-
},
|
|
9
|
-
"parserOptions": {
|
|
10
|
-
"ecmaVersion": 2020
|
|
11
|
-
},
|
|
12
|
-
"extends": "airbnb",
|
|
13
|
-
"rules": {
|
|
14
|
-
"import/no-extraneous-dependencies": ["error", { "devDependencies": true }],
|
|
15
|
-
"import/no-unresolved": ["error", { "amd": true, "commonjs": true, "caseSensitiveStrict": true }],
|
|
16
|
-
"brace-style": [1, "stroustrup", { "allowSingleLine": true }],
|
|
17
|
-
"no-confusing-arrow": [0], // arrow isnt confusing
|
|
18
|
-
"max-len": [1, 180, { "ignoreComments": true }],
|
|
19
|
-
"linebreak-style": 0,
|
|
20
|
-
"quotes": [1, "single"],
|
|
21
|
-
"semi": [1, "always"],
|
|
22
|
-
"no-process-env": ["error"],
|
|
23
|
-
"@mimik/document-env/validate-document-env": 2,
|
|
24
|
-
"@mimik/dependencies/case-sensitive": 2,
|
|
25
|
-
"@mimik/dependencies/no-cycles": 2,
|
|
26
|
-
"@mimik/dependencies/require-json-ext": 2
|
|
27
|
-
},
|
|
28
|
-
"settings":{
|
|
29
|
-
"react": {
|
|
30
|
-
"version": "detect"
|
|
31
|
-
}
|
|
32
|
-
},
|
|
33
|
-
"globals": {
|
|
34
|
-
"module": true,
|
|
35
|
-
"require": true,
|
|
36
|
-
"const": false,
|
|
37
|
-
"it": false,
|
|
38
|
-
"describe": false,
|
|
39
|
-
"before": true,
|
|
40
|
-
"after": true,
|
|
41
|
-
"JSON": true
|
|
42
|
-
}
|
|
43
|
-
}
|