@mimik/init 6.0.2 → 6.0.4
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 +5 -2
- package/index.js +7 -8
- package/lib/common.js +20 -0
- package/lib/exit.js +1 -5
- package/lib/logs.js +11 -3
- package/lib/metrics.js +0 -2
- package/lib/route.js +10 -13
- package/package.json +12 -9
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
```js
|
|
6
6
|
import init from '@mimik/init';
|
|
7
7
|
|
|
8
|
-
init(config, dbValidate, { postOps: [
|
|
8
|
+
init(config, dbValidate, { postOps: [subscribe] }).then(result => config = result);
|
|
9
9
|
```
|
|
10
10
|
<a name="module_init..init"></a>
|
|
11
11
|
|
|
@@ -23,10 +23,10 @@ The secOptions has the following structure:
|
|
|
23
23
|
exitOps: [function], // functions to process before exiting the micro-service
|
|
24
24
|
secOptions: { securityDefinition: function }, // extra security options to validate the API request
|
|
25
25
|
extractName: string, // ability to extract data from the req and send it to a logging environment
|
|
26
|
-
metrics: object, // to
|
|
26
|
+
metrics: object, // to generate information for `/metrics`
|
|
27
27
|
}
|
|
28
28
|
```
|
|
29
|
-
The `return` object has the following
|
|
29
|
+
The `return` object has the following structure:
|
|
30
30
|
``` javascript
|
|
31
31
|
{
|
|
32
32
|
config: The configuration object,
|
|
@@ -34,7 +34,7 @@ The `return` object has the following strucuture:
|
|
|
34
34
|
```
|
|
35
35
|
For the preOps, postOps and exitOps the function are executed with 3 parameters (correlationId, config, server).
|
|
36
36
|
The following routes are reserved: `/healthcheck` and `/metrics`.
|
|
37
|
-
The base path is defined by taking the url property of the first item of the servers array in the api
|
|
37
|
+
The base path is defined by taking the url property of the first item of the servers array in the api definition file. Only relative url address will be handled (e.g. /mss/v1) and templates in the url are not allowed.
|
|
38
38
|
|
|
39
39
|
| Param | Type | Description |
|
|
40
40
|
| --- | --- | --- |
|
package/eslint.config.js
CHANGED
|
@@ -3,6 +3,7 @@ import js from '@eslint/js';
|
|
|
3
3
|
import processDoc from '@mimik/eslint-plugin-document-env';
|
|
4
4
|
import stylistic from '@stylistic/eslint-plugin';
|
|
5
5
|
|
|
6
|
+
const ECMA_VERSION = 'latest';
|
|
6
7
|
const MAX_LENGTH_LINE = 180;
|
|
7
8
|
const MAX_FUNCTION_PARAMETERS = 7;
|
|
8
9
|
const MAX_LINES_IN_FILES = 600;
|
|
@@ -23,11 +24,13 @@ export default [
|
|
|
23
24
|
processDoc,
|
|
24
25
|
},
|
|
25
26
|
languageOptions: {
|
|
26
|
-
ecmaVersion:
|
|
27
|
+
ecmaVersion: ECMA_VERSION,
|
|
27
28
|
globals: {
|
|
28
29
|
console: 'readonly',
|
|
29
30
|
describe: 'readonly',
|
|
31
|
+
http: 'readonly',
|
|
30
32
|
it: 'readonly',
|
|
33
|
+
process: 'readonly',
|
|
31
34
|
require: 'readonly',
|
|
32
35
|
},
|
|
33
36
|
sourceType: 'module',
|
|
@@ -49,7 +52,7 @@ export default [
|
|
|
49
52
|
'max-lines-per-function': ['warn', { max: MAX_LINES_IN_FUNCTION, skipComments: true }],
|
|
50
53
|
'max-params': ['error', MAX_FUNCTION_PARAMETERS],
|
|
51
54
|
'max-statements': ['warn', MAX_STATEMENTS_IN_FUNCTION],
|
|
52
|
-
'no-confusing-arrow': ['off'],
|
|
55
|
+
'no-confusing-arrow': ['off'],
|
|
53
56
|
'no-inline-comments': ['off'],
|
|
54
57
|
'no-process-env': ['error'],
|
|
55
58
|
'no-ternary': ['off'],
|
package/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
BUILD_FILENAME,
|
|
3
|
+
EXIT_ERROR,
|
|
3
4
|
HEALTHCHECK_ROUTE,
|
|
4
5
|
LOCAL,
|
|
5
6
|
METRICS_ROUTE,
|
|
@@ -15,10 +16,9 @@ import { extractLogs } from './lib/logs.js';
|
|
|
15
16
|
import { getCorrelationId } from '@mimik/request-helper';
|
|
16
17
|
import { getPublic } from '@mimik/public-helper';
|
|
17
18
|
import helmet from 'helmet';
|
|
18
|
-
import http from 'http';
|
|
19
|
+
import http from 'node:http';
|
|
19
20
|
import logger from '@mimik/sumologic-winston-logger';
|
|
20
|
-
import pathLib from 'path';
|
|
21
|
-
import process from 'process';
|
|
21
|
+
import pathLib from 'node:path';
|
|
22
22
|
import { sigProcess } from './lib/exit.js';
|
|
23
23
|
import { startHrTimeSet } from './lib/metrics.js';
|
|
24
24
|
import { startupHealthInfo } from '@mimik/healthcheck';
|
|
@@ -28,11 +28,10 @@ import { startupHealthInfo } from '@mimik/healthcheck';
|
|
|
28
28
|
* @example
|
|
29
29
|
* import init from '@mimik/init';
|
|
30
30
|
*
|
|
31
|
-
* init(config, dbValidate, { postOps: [
|
|
31
|
+
* init(config, dbValidate, { postOps: [subscribe] }).then(result => config = result);
|
|
32
32
|
*
|
|
33
33
|
*/
|
|
34
34
|
|
|
35
|
-
const EXIT_ERROR = 1;
|
|
36
35
|
const FIRST = 0;
|
|
37
36
|
const FIRST_PATH_ELEMENT = 0;
|
|
38
37
|
|
|
@@ -55,10 +54,10 @@ const FIRST_PATH_ELEMENT = 0;
|
|
|
55
54
|
* exitOps: [function], // functions to process before exiting the micro-service
|
|
56
55
|
* secOptions: { securityDefinition: function }, // extra security options to validate the API request
|
|
57
56
|
* extractName: string, // ability to extract data from the req and send it to a logging environment
|
|
58
|
-
* metrics: object, // to
|
|
57
|
+
* metrics: object, // to generate information for `/metrics`
|
|
59
58
|
* }
|
|
60
59
|
*```
|
|
61
|
-
* The `return` object has the following
|
|
60
|
+
* The `return` object has the following structure:
|
|
62
61
|
* ``` javascript
|
|
63
62
|
* {
|
|
64
63
|
* config: The configuration object,
|
|
@@ -66,7 +65,7 @@ const FIRST_PATH_ELEMENT = 0;
|
|
|
66
65
|
* ```
|
|
67
66
|
* For the preOps, postOps and exitOps the function are executed with 3 parameters (correlationId, config, server).
|
|
68
67
|
* The following routes are reserved: `/healthcheck` and `/metrics`.
|
|
69
|
-
* The base path is defined by taking the url property of the first item of the servers array in the api
|
|
68
|
+
* The base path is defined by taking the url property of the first item of the servers array in the api definition file. Only relative url address will be handled (e.g. /mss/v1) and templates in the url are not allowed.
|
|
70
69
|
*/
|
|
71
70
|
const init = (app, rootDir, config, validates, cluster, options) => {
|
|
72
71
|
const fatalError = (error, correlationId) => {
|
package/lib/common.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import logger from '@mimik/sumologic-winston-logger';
|
|
2
|
+
|
|
1
3
|
const METRICS_ROUTE = '/metrics';
|
|
2
4
|
const HEALTHCHECK_ROUTE = '/healthcheck';
|
|
3
5
|
const OUT_OF_SPEC_ROUTE = 'outOfSpecificationRoute';
|
|
@@ -8,11 +10,22 @@ const JSON_CONTENT = 'application/json';
|
|
|
8
10
|
const SIGINT = 'SIGINT';
|
|
9
11
|
const SIGTERM = 'SIGTERM';
|
|
10
12
|
|
|
13
|
+
const SHUTDOWN = 'service shutdown successfully';
|
|
14
|
+
const SHUTDOWN_WITH_ERROR = 'service shutdown with error';
|
|
15
|
+
const EXIT_OK = 0;
|
|
16
|
+
const EXIT_ERROR = 1;
|
|
17
|
+
|
|
11
18
|
const LOCAL = 'local';
|
|
12
19
|
const SET_ON = 'on';
|
|
13
20
|
|
|
21
|
+
const TAB = 2;
|
|
22
|
+
const DEBUG = 4;
|
|
23
|
+
|
|
14
24
|
const BUILD_FILENAME = 'register.js';
|
|
15
25
|
|
|
26
|
+
const NOTIFICATION = '/notifications';
|
|
27
|
+
const EXTRACT_LOG_LEVEL = logger.LEVELS[DEBUG];
|
|
28
|
+
|
|
16
29
|
export {
|
|
17
30
|
METRICS_ROUTE,
|
|
18
31
|
HEALTHCHECK_ROUTE,
|
|
@@ -24,4 +37,11 @@ export {
|
|
|
24
37
|
LOCAL,
|
|
25
38
|
SET_ON,
|
|
26
39
|
BUILD_FILENAME,
|
|
40
|
+
NOTIFICATION,
|
|
41
|
+
EXTRACT_LOG_LEVEL,
|
|
42
|
+
TAB,
|
|
43
|
+
SHUTDOWN,
|
|
44
|
+
SHUTDOWN_WITH_ERROR,
|
|
45
|
+
EXIT_OK,
|
|
46
|
+
EXIT_ERROR,
|
|
27
47
|
};
|
package/lib/exit.js
CHANGED
|
@@ -1,11 +1,7 @@
|
|
|
1
|
+
import { EXIT_ERROR, EXIT_OK, SHUTDOWN, SHUTDOWN_WITH_ERROR } from './common.js';
|
|
1
2
|
import Promise from 'bluebird';
|
|
2
3
|
import logger from '@mimik/sumologic-winston-logger';
|
|
3
4
|
|
|
4
|
-
const SHUTDOWN = 'service shutdown successfully';
|
|
5
|
-
const SHUTDOWN_WITH_ERROR = 'service shutdown with error';
|
|
6
|
-
const EXIT_OK = 0;
|
|
7
|
-
const EXIT_ERROR = 1;
|
|
8
|
-
|
|
9
5
|
const shutdown = (res, correlationId) => {
|
|
10
6
|
if (res.errors) {
|
|
11
7
|
logger.warn(SHUTDOWN_WITH_ERROR, res, correlationId);
|
package/lib/logs.js
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
|
+
import { EXTRACT_LOG_LEVEL, NOTIFICATION } from './common.js';
|
|
1
2
|
import { getCorrelationId } from '@mimik/request-helper';
|
|
2
3
|
import { getRichError } from '@mimik/response-helper';
|
|
3
4
|
import logger from '@mimik/sumologic-winston-logger';
|
|
4
5
|
import oauthHelper from '@mimik/oauth-helper';
|
|
5
6
|
import { publicIpFromRequest } from '@mimik/address-helper';
|
|
6
|
-
import urlLib from 'url';
|
|
7
|
+
import urlLib from 'node:url';
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
const NOTIFICATION = '/notifications';
|
|
9
|
+
let correlationId = getCorrelationId('log-extraction');
|
|
10
10
|
|
|
11
11
|
export const extractLogs = (config, extractName, options) => (req, res, next) => {
|
|
12
12
|
const { rpAuth } = oauthHelper(config);
|
|
13
13
|
let url = `${config.dependencies.mLG.url}${NOTIFICATION}`;
|
|
14
14
|
const params = new urlLib.URLSearchParams();
|
|
15
15
|
|
|
16
|
+
if (req.headers['x-correlation-id']) correlationId = `${req.headers['x-correlation-id']}:${correlationId}`;
|
|
16
17
|
if (req.tokenType) params.append('serverType', req.tokenType);
|
|
17
18
|
if (req.clientId) params.append('serverId', req.clientId);
|
|
18
19
|
if (params.keys().length) url = `${url}?${params.toString()}`;
|
|
@@ -34,6 +35,13 @@ export const extractLogs = (config, extractName, options) => (req, res, next) =>
|
|
|
34
35
|
data: {
|
|
35
36
|
Message: req.body[extractName],
|
|
36
37
|
},
|
|
38
|
+
retry: {
|
|
39
|
+
logLevel: {
|
|
40
|
+
request: EXTRACT_LOG_LEVEL,
|
|
41
|
+
response: EXTRACT_LOG_LEVEL,
|
|
42
|
+
error: EXTRACT_LOG_LEVEL,
|
|
43
|
+
},
|
|
44
|
+
},
|
|
37
45
|
};
|
|
38
46
|
|
|
39
47
|
if (options && options.metrics) {
|
package/lib/metrics.js
CHANGED
package/lib/route.js
CHANGED
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import { CONTENT_TYPE, JSON_CONTENT } from './common.js';
|
|
1
|
+
import { CONTENT_TYPE, JSON_CONTENT, TAB } from './common.js';
|
|
2
2
|
import { getCorrelationId, getUserAgent } from '@mimik/request-helper';
|
|
3
|
+
import { ERROR_CODE } from '@mimik/response-helper';
|
|
3
4
|
import { healthInfo } from '@mimik/systeminfo';
|
|
4
|
-
import http from 'http';
|
|
5
|
+
import http from 'node:http';
|
|
5
6
|
import logger from '@mimik/sumologic-winston-logger';
|
|
6
7
|
import { publicIpFromRequest } from '@mimik/address-helper';
|
|
7
8
|
|
|
8
|
-
const TAB = 2;
|
|
9
|
-
const SYSTEM_ERROR = 500;
|
|
10
|
-
const PARAMETER_ERROR = 400;
|
|
11
|
-
|
|
12
9
|
export const healthCheckRoute = () => (req, res) => {
|
|
13
10
|
res.setHeader(CONTENT_TYPE, JSON_CONTENT);
|
|
14
11
|
res.end(JSON.stringify({ data: healthInfo() }, null, TAB));
|
|
@@ -22,8 +19,8 @@ export const metricsRoute = options => (req, res) => options.metrics.register.me
|
|
|
22
19
|
.catch((err) => {
|
|
23
20
|
res.setHeader(CONTENT_TYPE, JSON_CONTENT);
|
|
24
21
|
const errResponse = {
|
|
25
|
-
statusCode:
|
|
26
|
-
title: http.STATUS_CODES[
|
|
22
|
+
statusCode: ERROR_CODE.SYSTEM,
|
|
23
|
+
title: http.STATUS_CODES[ERROR_CODE.SYSTEM],
|
|
27
24
|
message: err.message,
|
|
28
25
|
};
|
|
29
26
|
|
|
@@ -31,7 +28,7 @@ export const metricsRoute = options => (req, res) => options.metrics.register.me
|
|
|
31
28
|
});
|
|
32
29
|
|
|
33
30
|
export const bodyParserRoute = () => (err, req, res, next) => {
|
|
34
|
-
if (err instanceof SyntaxError && err.status ===
|
|
31
|
+
if (err instanceof SyntaxError && err.status === ERROR_CODE.PARAMETER && 'body' in err) {
|
|
35
32
|
const { originalUrl } = req;
|
|
36
33
|
const method = req.method.toUpperCase();
|
|
37
34
|
const correlationId = getCorrelationId(req);
|
|
@@ -39,15 +36,15 @@ export const bodyParserRoute = () => (err, req, res, next) => {
|
|
|
39
36
|
logger.warn(`Rejecting request with 400 for ${method} ${originalUrl}`, {
|
|
40
37
|
method,
|
|
41
38
|
path: originalUrl,
|
|
42
|
-
statusCode:
|
|
39
|
+
statusCode: ERROR_CODE.PARAMETER,
|
|
43
40
|
userAgent: getUserAgent(req),
|
|
44
41
|
clientIP: publicIpFromRequest(req),
|
|
45
42
|
error: err,
|
|
46
43
|
}, correlationId);
|
|
47
44
|
if (correlationId) res.setHeader('x-correlation-id', correlationId);
|
|
48
|
-
return res.status(
|
|
49
|
-
statusCode:
|
|
50
|
-
title: http.STATUS_CODES[
|
|
45
|
+
return res.status(ERROR_CODE.PARAMETER).json({
|
|
46
|
+
statusCode: ERROR_CODE.PARAMETER,
|
|
47
|
+
title: http.STATUS_CODES[ERROR_CODE.PARAMETER],
|
|
51
48
|
message: err.message,
|
|
52
49
|
error: err,
|
|
53
50
|
});
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mimik/init",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.4",
|
|
4
4
|
"description": "Init process for micro-service",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=20.0.0"
|
|
9
|
+
},
|
|
7
10
|
"scripts": {
|
|
8
11
|
"lint": "eslint . --no-error-on-unmatched-pattern",
|
|
9
12
|
"docs": "jsdoc2md index.js > README.md",
|
|
@@ -29,26 +32,26 @@
|
|
|
29
32
|
"url": "https://bitbucket.org/mimiktech/init"
|
|
30
33
|
},
|
|
31
34
|
"dependencies": {
|
|
32
|
-
"@mimik/address-helper": "^2.0.
|
|
33
|
-
"@mimik/api-helper": "^2.0.
|
|
35
|
+
"@mimik/address-helper": "^2.0.5",
|
|
36
|
+
"@mimik/api-helper": "^2.0.7",
|
|
34
37
|
"@mimik/healthcheck": "^2.0.0",
|
|
35
38
|
"@mimik/oauth-helper": "^4.0.2",
|
|
36
39
|
"@mimik/public-helper": "^3.0.3",
|
|
37
40
|
"@mimik/request-helper": "^2.0.2",
|
|
38
|
-
"@mimik/response-helper": "^4.0.
|
|
39
|
-
"@mimik/sumologic-winston-logger": "^2.
|
|
41
|
+
"@mimik/response-helper": "^4.0.6",
|
|
42
|
+
"@mimik/sumologic-winston-logger": "^2.1.8",
|
|
40
43
|
"@mimik/systeminfo": "^4.0.1",
|
|
41
44
|
"bluebird": "3.7.2",
|
|
42
45
|
"cors": "2.8.5",
|
|
43
46
|
"helmet": "8.1.0"
|
|
44
47
|
},
|
|
45
48
|
"devDependencies": {
|
|
46
|
-
"@eslint/js": "9.
|
|
49
|
+
"@eslint/js": "9.39.1",
|
|
47
50
|
"@mimik/eslint-plugin-document-env": "^2.0.8",
|
|
48
|
-
"@stylistic/eslint-plugin": "5.
|
|
49
|
-
"eslint": "9.
|
|
51
|
+
"@stylistic/eslint-plugin": "5.5.0",
|
|
52
|
+
"eslint": "9.39.1",
|
|
50
53
|
"eslint-plugin-import": "2.32.0",
|
|
51
54
|
"husky": "9.1.7",
|
|
52
|
-
"jsdoc-to-markdown": "9.1.
|
|
55
|
+
"jsdoc-to-markdown": "9.1.3"
|
|
53
56
|
}
|
|
54
57
|
}
|