@mimik/rediser 2.0.1 → 2.1.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/.claude/settings.local.json +11 -0
- package/README.md +30 -15
- package/index.js +33 -20
- package/package.json +24 -21
- package/.husky/pre-commit +0 -4
- package/.husky/pre-push +0 -4
- package/.nycrc +0 -4
- package/eslint.config.js +0 -63
package/README.md
CHANGED
|
@@ -3,37 +3,52 @@
|
|
|
3
3
|
## rediser
|
|
4
4
|
**Example**
|
|
5
5
|
```js
|
|
6
|
-
import
|
|
6
|
+
import rediser from '@mimik/rediser';
|
|
7
7
|
```
|
|
8
8
|
|
|
9
9
|
* [rediser](#module_rediser)
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
|
|
13
|
-
*
|
|
10
|
+
* [module.exports(set, config)](#exp_module_rediser--module.exports) ⇒ <code>object</code> ⏏
|
|
11
|
+
* _async_
|
|
12
|
+
* [~validate()](#module_rediser--module.exports..validate) ⇒ <code>Promise</code>
|
|
13
|
+
* _sync_
|
|
14
|
+
* [~initializeSync()](#module_rediser--module.exports..initializeSync) ⇒ <code>object</code> \| <code>null</code>
|
|
14
15
|
|
|
15
|
-
<a name="
|
|
16
|
+
<a name="exp_module_rediser--module.exports"></a>
|
|
16
17
|
|
|
17
|
-
###
|
|
18
|
+
### module.exports(set, config) ⇒ <code>object</code> ⏏
|
|
19
|
+
Creates a Redis connection manager.
|
|
20
|
+
|
|
21
|
+
**Kind**: Exported function
|
|
22
|
+
**Returns**: <code>object</code> - An object with `initializeSync` and `validate` methods.
|
|
23
|
+
|
|
24
|
+
| Param | Type | Description |
|
|
25
|
+
| --- | --- | --- |
|
|
26
|
+
| set | <code>string</code> | Whether redis is enabled ('on' or 'off'). |
|
|
27
|
+
| config | <code>object</code> | The application configuration object. |
|
|
28
|
+
| config.redisSettings | <code>object</code> | Redis-specific settings. |
|
|
29
|
+
|
|
30
|
+
<a name="module_rediser--module.exports..validate"></a>
|
|
31
|
+
|
|
32
|
+
#### module.exports~validate() ⇒ <code>Promise</code>
|
|
18
33
|
Database connection validation.
|
|
19
34
|
|
|
20
|
-
**Kind**: inner method of [<code>
|
|
21
|
-
**Returns**: <code>Promise</code> - .
|
|
35
|
+
**Kind**: inner method of [<code>module.exports</code>](#exp_module_rediser--module.exports)
|
|
36
|
+
**Returns**: <code>Promise</code> - Resolves with null on success.
|
|
22
37
|
**Category**: async
|
|
23
38
|
**Throws**:
|
|
24
39
|
|
|
25
|
-
- <code>
|
|
40
|
+
- <code>Error</code> Will throw an error if the database connection is not `connected` and is not `disconnected`.
|
|
26
41
|
|
|
27
42
|
Will exit 1 if the connection request times out and the connection state is not `connected`.
|
|
28
43
|
|
|
29
44
|
**Requires**: <code>module:@mimik/sumologic-winston-logger</code>
|
|
30
45
|
**Fulfil**: Return `null`.
|
|
31
|
-
<a name="module_rediser..initializeSync"></a>
|
|
46
|
+
<a name="module_rediser--module.exports..initializeSync"></a>
|
|
32
47
|
|
|
33
|
-
|
|
48
|
+
#### module.exports~initializeSync() ⇒ <code>object</code> \| <code>null</code>
|
|
34
49
|
Database initialization.
|
|
35
50
|
|
|
36
|
-
**Kind**: inner method of [<code>
|
|
37
|
-
**Returns**: <code>object</code> - The database
|
|
51
|
+
**Kind**: inner method of [<code>module.exports</code>](#exp_module_rediser--module.exports)
|
|
52
|
+
**Returns**: <code>object</code> \| <code>null</code> - The database client, or null if cache is not enabled.
|
|
38
53
|
**Category**: sync
|
|
39
|
-
**Requires**: <code>module
|
|
54
|
+
**Requires**: <code>module:@mimik/sumologic-winston-logger</code>
|
package/index.js
CHANGED
|
@@ -5,14 +5,14 @@ import {
|
|
|
5
5
|
import {
|
|
6
6
|
clearInterval,
|
|
7
7
|
setInterval,
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
setTimeout,
|
|
9
|
+
} from 'node:timers';
|
|
10
10
|
import logger from '@mimik/sumologic-winston-logger';
|
|
11
11
|
import redis from 'redis';
|
|
12
12
|
/**
|
|
13
13
|
* @module rediser
|
|
14
14
|
* @example
|
|
15
|
-
* import
|
|
15
|
+
* import rediser from '@mimik/rediser';
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
const type = 'redis';
|
|
@@ -20,25 +20,37 @@ const correlationId = `${type}-cache-start@0/${new Date().toISOString()}`;
|
|
|
20
20
|
|
|
21
21
|
const CONNECTED = 1;
|
|
22
22
|
const DISCONNECTED = 2;
|
|
23
|
-
const FIRST = 0;
|
|
24
23
|
const ISSUE_EXIT = 1;
|
|
25
24
|
const MILLI_SEC = 1000;
|
|
26
25
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
let clusterConnectInterval;
|
|
26
|
+
const delay = ms => new Promise((resolve) => {
|
|
27
|
+
setTimeout(resolve, ms);
|
|
28
|
+
});
|
|
31
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Creates a Redis connection manager.
|
|
32
|
+
*
|
|
33
|
+
* @param {string} set - Whether redis is enabled ('on' or 'off').
|
|
34
|
+
* @param {object} config - The application configuration object.
|
|
35
|
+
* @param {object} config.redisSettings - Redis-specific settings.
|
|
36
|
+
* @returns {object} An object with `initializeSync` and `validate` methods.
|
|
37
|
+
*/
|
|
32
38
|
export default (set, config) => {
|
|
33
39
|
const { redisSettings } = config;
|
|
40
|
+
|
|
41
|
+
let displayCreate = true;
|
|
42
|
+
let displayDisconnect = true;
|
|
43
|
+
let state = DISCONNECTED;
|
|
44
|
+
let clusterConnectInterval;
|
|
45
|
+
|
|
34
46
|
/**
|
|
35
47
|
* Database initialization.
|
|
36
48
|
*
|
|
37
|
-
* @requires sumologic-winston-logger
|
|
49
|
+
* @requires @mimik/sumologic-winston-logger
|
|
38
50
|
*
|
|
39
51
|
* @function initializeSync
|
|
40
52
|
* @category sync
|
|
41
|
-
* @return {object} The database
|
|
53
|
+
* @return {object|null} The database client, or null if cache is not enabled.
|
|
42
54
|
*/
|
|
43
55
|
const initializeSync = () => {
|
|
44
56
|
if (set !== 'on') {
|
|
@@ -66,7 +78,7 @@ export default (set, config) => {
|
|
|
66
78
|
}
|
|
67
79
|
logger.flushAndExit(ISSUE_EXIT);
|
|
68
80
|
}
|
|
69
|
-
}, config.redisSettings.connectTimeout * MILLI_SEC); // convert
|
|
81
|
+
}, config.redisSettings.connectTimeout * MILLI_SEC); // convert seconds to milliseconds
|
|
70
82
|
}
|
|
71
83
|
};
|
|
72
84
|
|
|
@@ -74,7 +86,9 @@ export default (set, config) => {
|
|
|
74
86
|
if (isProd(config.nodeEnvironment)) {
|
|
75
87
|
const redisSettingsClone = { ...redisSettings };
|
|
76
88
|
|
|
77
|
-
|
|
89
|
+
const eqIdx = redisSettings.url.indexOf('=');
|
|
90
|
+
|
|
91
|
+
if (redisSettings.password && eqIdx !== -1) redisSettingsClone.url = `${redisSettings.url.substring(0, eqIdx + 1)}${MASK}`;
|
|
78
92
|
logger.info('creating a cache connection', { type, settings: redisSettingsClone }, correlationId);
|
|
79
93
|
}
|
|
80
94
|
else logger.info('creating a cache connection', { type, settings: redisSettings }, correlationId);
|
|
@@ -110,7 +124,7 @@ export default (set, config) => {
|
|
|
110
124
|
interval = null;
|
|
111
125
|
}
|
|
112
126
|
})
|
|
113
|
-
.on('end',
|
|
127
|
+
.on('end', disconnectHandler)
|
|
114
128
|
.connect();
|
|
115
129
|
|
|
116
130
|
// eslint-disable-next-line no-warning-comments
|
|
@@ -139,16 +153,15 @@ export default (set, config) => {
|
|
|
139
153
|
};
|
|
140
154
|
|
|
141
155
|
/**
|
|
142
|
-
*
|
|
143
156
|
* Database connection validation.
|
|
144
157
|
*
|
|
145
158
|
* @requires @mimik/sumologic-winston-logger
|
|
146
159
|
*
|
|
147
160
|
* @function validate
|
|
148
161
|
* @category async
|
|
149
|
-
* @returns {Promise}.
|
|
162
|
+
* @returns {Promise} Resolves with null on success.
|
|
150
163
|
* @fulfil {} Return `null`.
|
|
151
|
-
* @throws {
|
|
164
|
+
* @throws {Error} Will throw an error if the database connection is not `connected` and is not `disconnected`.
|
|
152
165
|
*
|
|
153
166
|
* Will exit 1 if the connection request times out and the connection state is not `connected`.
|
|
154
167
|
*/
|
|
@@ -161,10 +174,11 @@ export default (set, config) => {
|
|
|
161
174
|
}, correlationId);
|
|
162
175
|
logger.flushAndExit(ISSUE_EXIT);
|
|
163
176
|
}
|
|
164
|
-
}, config.redisSettings.connectTimeout * MILLI_SEC); // convert
|
|
177
|
+
}, config.redisSettings.connectTimeout * MILLI_SEC); // convert seconds to milliseconds
|
|
165
178
|
|
|
166
|
-
return
|
|
179
|
+
return delay(redisSettings.validationCheck)
|
|
167
180
|
.then(() => {
|
|
181
|
+
clearInterval(interval);
|
|
168
182
|
if (state !== DISCONNECTED && state !== CONNECTED) {
|
|
169
183
|
const error = new Error(`cache connection not established: ${state}`);
|
|
170
184
|
|
|
@@ -174,13 +188,12 @@ export default (set, config) => {
|
|
|
174
188
|
if (state === DISCONNECTED) {
|
|
175
189
|
return validate();
|
|
176
190
|
}
|
|
177
|
-
clearInterval(interval);
|
|
178
191
|
logger.info('cache connection established', { type }, correlationId);
|
|
179
192
|
return null;
|
|
180
193
|
});
|
|
181
194
|
};
|
|
182
195
|
|
|
183
|
-
|
|
196
|
+
initializeSync();
|
|
184
197
|
return {
|
|
185
198
|
initializeSync,
|
|
186
199
|
validate,
|
package/package.json
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mimik/rediser",
|
|
3
|
-
"version": "2.0
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "2.1.0",
|
|
4
|
+
"description": "Redis connection helper for mimik microservices",
|
|
5
5
|
"main": "./index.js",
|
|
6
|
+
"exports": "./index.js",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=24"
|
|
9
|
+
},
|
|
6
10
|
"type": "module",
|
|
7
11
|
"scripts": {
|
|
8
12
|
"lint": "eslint . --no-error-on-unmatched-pattern",
|
|
9
13
|
"docs": "jsdoc2md index.js > README.md",
|
|
10
|
-
"test": "
|
|
11
|
-
"test-ci": "
|
|
14
|
+
"test": "mocha --reporter mochawesome --bail --exit --timeout 5000 --node-option loader=esmock test/",
|
|
15
|
+
"test-ci": "c8 --reporter=lcov --reporter=text npm test",
|
|
12
16
|
"prepublishOnly": "npm run docs && npm run lint && npm run test-ci",
|
|
13
|
-
"commit-ready": "npm run docs && npm run lint && npm run test-ci"
|
|
14
|
-
|
|
15
|
-
"husky": {
|
|
16
|
-
"hooks": {
|
|
17
|
-
"pre-commit": "npm run commit-ready",
|
|
18
|
-
"pre-push": "npm run test"
|
|
19
|
-
}
|
|
17
|
+
"commit-ready": "npm run docs && npm run lint && npm run test-ci",
|
|
18
|
+
"prepare": "husky"
|
|
20
19
|
},
|
|
21
20
|
"keywords": [
|
|
22
21
|
"mimik",
|
|
@@ -29,18 +28,22 @@
|
|
|
29
28
|
"url": "https://bitbucket.org/mimiktech/rediser"
|
|
30
29
|
},
|
|
31
30
|
"dependencies": {
|
|
32
|
-
"@mimik/lib-filters": "^2.0.
|
|
33
|
-
"@mimik/sumologic-winston-logger": "^2.
|
|
34
|
-
"
|
|
35
|
-
"redis": "4.7.0"
|
|
31
|
+
"@mimik/lib-filters": "^2.0.7",
|
|
32
|
+
"@mimik/sumologic-winston-logger": "^2.1.14",
|
|
33
|
+
"redis": "5.11.0"
|
|
36
34
|
},
|
|
37
35
|
"devDependencies": {
|
|
38
|
-
"@eslint/js": "9.
|
|
39
|
-
"@mimik/eslint-plugin-document-env": "^2.0.
|
|
40
|
-
"@stylistic/eslint-plugin": "
|
|
41
|
-
"
|
|
42
|
-
"eslint
|
|
36
|
+
"@eslint/js": "9.39.3",
|
|
37
|
+
"@mimik/eslint-plugin-document-env": "^2.0.8",
|
|
38
|
+
"@stylistic/eslint-plugin": "5.9.0",
|
|
39
|
+
"c8": "11.0.0",
|
|
40
|
+
"eslint": "9.39.3",
|
|
41
|
+
"eslint-plugin-import": "2.32.0",
|
|
42
|
+
"esmock": "^2.7.3",
|
|
43
|
+
"globals": "17.3.0",
|
|
43
44
|
"husky": "9.1.7",
|
|
44
|
-
"jsdoc-to-markdown": "9.1.
|
|
45
|
+
"jsdoc-to-markdown": "9.1.3",
|
|
46
|
+
"mocha": "11.7.5",
|
|
47
|
+
"mochawesome": "7.1.4"
|
|
45
48
|
}
|
|
46
49
|
}
|
package/.husky/pre-commit
DELETED
package/.husky/pre-push
DELETED
package/.nycrc
DELETED
package/eslint.config.js
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
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 = 6;
|
|
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-flat'],
|
|
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
|
-
];
|