@mimik/request-retry 4.0.6 → 4.0.7
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 +8 -8
- package/index.js +11 -14
- package/lib/rp-axios-wrapper.js +1 -1
- package/manual-test/man.js +1 -1
- package/manual-test/retryMockMan.js +3 -3
- package/manual-test/testEnv.js +1 -1
- package/manual-test/testRace.js +1 -4
- package/package.json +14 -14
- package/test/retryMock.js +2 -2
- package/test/rpRetry.spec.js +3 -3
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
**Example**
|
|
5
5
|
```js
|
|
6
6
|
import { rpRetry } from '@mimik/request-retry';
|
|
7
|
-
or
|
|
7
|
+
// or
|
|
8
8
|
import rp from '@mimik/request-retry';
|
|
9
9
|
```
|
|
10
10
|
<a name="module_request-retry..rpRetry"></a>
|
|
@@ -16,8 +16,8 @@ Make a request with retries.
|
|
|
16
16
|
**Category**: async
|
|
17
17
|
**Throws**:
|
|
18
18
|
|
|
19
|
-
- <code>Error</code> An error produced by `getRichError`, wrapping an
|
|
20
|
-
[
|
|
19
|
+
- <code>Error</code> An error produced by `getRichError`, wrapping an
|
|
20
|
+
[Axios](https://www.npmjs.com/package/axios) error or a `TimeoutError`.
|
|
21
21
|
|
|
22
22
|
The following properties may be added to the `options` object under the `retry` property:
|
|
23
23
|
- retries `{int}`: Maximum number of retries, independent of the `retryStrategy`. Default: `2`.
|
|
@@ -27,9 +27,9 @@ The following properties may be added to the `options` object under the `retry`
|
|
|
27
27
|
- delayStrategy `{function}`: A function that returns the delay (in milliseconds) to wait before the next retry.
|
|
28
28
|
Signature: `(nbRetry, err, options, correlationId)`. Must return a number `> 0` and `< 30000`; otherwise `delay` is used.
|
|
29
29
|
- logLevel `{object}`: Controls logging behavior:
|
|
30
|
-
- *response*: Log level for responses.
|
|
31
|
-
- *error*: Log level for errors.
|
|
32
|
-
- *request*: Log level for requests.
|
|
30
|
+
- *response*: Log level for responses. Invalid values fall back to `silly`.
|
|
31
|
+
- *error*: Log level for errors. Invalid values fall back to `silly`.
|
|
32
|
+
- *request*: Log level for requests. Invalid values fall back to `silly`.
|
|
33
33
|
- *responseDetails*: Detail level when displaying the response: `count`, `type`, or `full`. Default: `type`
|
|
34
34
|
(invalid values fall back to `type`).
|
|
35
35
|
- *responseName*: Label associated with the response. Default: `response`
|
|
@@ -55,8 +55,8 @@ defaultRetry = (...args) => {
|
|
|
55
55
|
```
|
|
56
56
|
|
|
57
57
|
If `logLevel` is empty, requests and responses are logged at `info`, the response is logged with full details
|
|
58
|
-
under the `response` property, and request errors generate a `warn`. If
|
|
59
|
-
the provided keys
|
|
58
|
+
under the `response` property, and request errors generate a `warn`. If logLevel is provided but incomplete,
|
|
59
|
+
only the provided keys are active; missing keys disable that logging.
|
|
60
60
|
|
|
61
61
|
If not already set, the `User-Agent` header is set to:
|
|
62
62
|
`mimik-{serverType}/{serverVersion}/{serverId} {architecture} {node}`.
|
package/index.js
CHANGED
|
@@ -25,10 +25,8 @@ const DEFAULT_LOGLEVEL_RESPONSE = 'info';
|
|
|
25
25
|
const DEFAULT_LOGLEVEL_ERROR = 'warn';
|
|
26
26
|
const DEFAULT_LOGLEVEL_REQUEST = 'info';
|
|
27
27
|
|
|
28
|
-
const MILLI_SECONDS = 0;
|
|
29
|
-
const MILLI_NANO_SECONDS = 1;
|
|
30
28
|
const MILLI = 1000;
|
|
31
|
-
const
|
|
29
|
+
const NANO_TO_MS = 1000000;
|
|
32
30
|
|
|
33
31
|
Promise.config({ cancellation: true });
|
|
34
32
|
|
|
@@ -36,7 +34,7 @@ Promise.config({ cancellation: true });
|
|
|
36
34
|
* @module request-retry
|
|
37
35
|
* @example
|
|
38
36
|
* import { rpRetry } from '@mimik/request-retry';
|
|
39
|
-
* or
|
|
37
|
+
* // or
|
|
40
38
|
* import rp from '@mimik/request-retry';
|
|
41
39
|
*/
|
|
42
40
|
|
|
@@ -53,8 +51,8 @@ Promise.config({ cancellation: true });
|
|
|
53
51
|
* when `true`, the full Axios response object is returned; when `false` or omitted, only `response.data` is returned.
|
|
54
52
|
* @return {Promise}
|
|
55
53
|
* @fulfil {object} - The Axios response when `resolveWithFullResponse` is `true`; otherwise `response.data`.
|
|
56
|
-
* @throws {Error} An error produced by `getRichError`, wrapping an
|
|
57
|
-
* [
|
|
54
|
+
* @throws {Error} An error produced by `getRichError`, wrapping an
|
|
55
|
+
* [Axios](https://www.npmjs.com/package/axios) error or a `TimeoutError`.
|
|
58
56
|
*
|
|
59
57
|
* The following properties may be added to the `options` object under the `retry` property:
|
|
60
58
|
* - retries `{int}`: Maximum number of retries, independent of the `retryStrategy`. Default: `2`.
|
|
@@ -64,9 +62,9 @@ Promise.config({ cancellation: true });
|
|
|
64
62
|
* - delayStrategy `{function}`: A function that returns the delay (in milliseconds) to wait before the next retry.
|
|
65
63
|
* Signature: `(nbRetry, err, options, correlationId)`. Must return a number `> 0` and `< 30000`; otherwise `delay` is used.
|
|
66
64
|
* - logLevel `{object}`: Controls logging behavior:
|
|
67
|
-
* - *response*: Log level for responses.
|
|
68
|
-
* - *error*: Log level for errors.
|
|
69
|
-
* - *request*: Log level for requests.
|
|
65
|
+
* - *response*: Log level for responses. Invalid values fall back to `silly`.
|
|
66
|
+
* - *error*: Log level for errors. Invalid values fall back to `silly`.
|
|
67
|
+
* - *request*: Log level for requests. Invalid values fall back to `silly`.
|
|
70
68
|
* - *responseDetails*: Detail level when displaying the response: `count`, `type`, or `full`. Default: `type`
|
|
71
69
|
* (invalid values fall back to `type`).
|
|
72
70
|
* - *responseName*: Label associated with the response. Default: `response`
|
|
@@ -92,8 +90,8 @@ Promise.config({ cancellation: true });
|
|
|
92
90
|
* ```
|
|
93
91
|
*
|
|
94
92
|
* If `logLevel` is empty, requests and responses are logged at `info`, the response is logged with full details
|
|
95
|
-
* under the `response` property, and request errors generate a `warn`. If
|
|
96
|
-
* the provided keys
|
|
93
|
+
* under the `response` property, and request errors generate a `warn`. If logLevel is provided but incomplete,
|
|
94
|
+
* only the provided keys are active; missing keys disable that logging.
|
|
97
95
|
*
|
|
98
96
|
* If not already set, the `User-Agent` header is set to:
|
|
99
97
|
* `mimik-{serverType}/{serverVersion}/{serverId} {architecture} {node}`.
|
|
@@ -104,7 +102,7 @@ Promise.config({ cancellation: true });
|
|
|
104
102
|
* - `qs` is mapped to Axios `params`.
|
|
105
103
|
*/
|
|
106
104
|
export const rpRetry = (origOptions) => {
|
|
107
|
-
const startHrTime = process.hrtime();
|
|
105
|
+
const startHrTime = process.hrtime.bigint();
|
|
108
106
|
const options = origOptions;
|
|
109
107
|
const { metrics } = options;
|
|
110
108
|
const enteredUrl = options.uri || options.url;
|
|
@@ -118,8 +116,7 @@ export const rpRetry = (origOptions) => {
|
|
|
118
116
|
let mainTimeout;
|
|
119
117
|
const measure = (statusCode) => {
|
|
120
118
|
if (metrics && metrics.HTTPRequestDuration) {
|
|
121
|
-
const
|
|
122
|
-
const elapsedTimeInMs = elapsedHrTime[MILLI_SECONDS] * MILLI + elapsedHrTime[MILLI_NANO_SECONDS] / NANO;
|
|
119
|
+
const elapsedTimeInMs = Number(process.hrtime.bigint() - startHrTime) / NANO_TO_MS;
|
|
123
120
|
|
|
124
121
|
metrics.HTTPRequestDuration
|
|
125
122
|
.labels('rpRetry', options.method, metrics.url || enteredUrl, enteredUrl.includes('?'), statusCode)
|
package/lib/rp-axios-wrapper.js
CHANGED
package/manual-test/man.js
CHANGED
|
@@ -30,11 +30,11 @@ let message;
|
|
|
30
30
|
|
|
31
31
|
app.use(bodyParser.json());
|
|
32
32
|
app.get(`${config.base}${config.path}`, (req, res) => {
|
|
33
|
-
if (nbRequest === 0) message = '
|
|
33
|
+
if (nbRequest === 0) message = 'Received first request';
|
|
34
34
|
else {
|
|
35
35
|
const timeLap = Date.now() - time;
|
|
36
36
|
|
|
37
|
-
message = `Received ${nbRequest} retry with
|
|
37
|
+
message = `Received ${nbRequest} retry with timelapse ${timeLap}`;
|
|
38
38
|
}
|
|
39
39
|
console.log('----->', message);
|
|
40
40
|
time = Date.now();
|
|
@@ -57,7 +57,7 @@ app.post(`${config.base}${config.path}`, (req, res) => {
|
|
|
57
57
|
});
|
|
58
58
|
|
|
59
59
|
app.post('/logs/:id', (req, res) => {
|
|
60
|
-
console.log('--->', '
|
|
60
|
+
console.log('--->', 'Received a log');
|
|
61
61
|
console.log('---> id:', req.params.id);
|
|
62
62
|
console.log('---> body:', req.body);
|
|
63
63
|
res.statusCode = config.post.success;
|
package/manual-test/testEnv.js
CHANGED
package/manual-test/testRace.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mimik/request-retry",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.7",
|
|
4
4
|
"description": "Request retry wrapping axios",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -29,27 +29,27 @@
|
|
|
29
29
|
"url": "https://bitbucket.org/mimiktech/request-retry"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@mimik/request-helper": "^2.0.
|
|
33
|
-
"@mimik/response-helper": "^4.0.
|
|
34
|
-
"@mimik/sumologic-winston-logger": "^2.1.
|
|
35
|
-
"axios": "1.
|
|
32
|
+
"@mimik/request-helper": "^2.0.3",
|
|
33
|
+
"@mimik/response-helper": "^4.0.9",
|
|
34
|
+
"@mimik/sumologic-winston-logger": "^2.1.10",
|
|
35
|
+
"axios": "1.13.5",
|
|
36
36
|
"bluebird": "3.7.2"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@eslint/js": "9.
|
|
39
|
+
"@eslint/js": "9.39.2",
|
|
40
40
|
"@mimik/eslint-plugin-document-env": "^2.0.8",
|
|
41
|
-
"@stylistic/eslint-plugin": "5.
|
|
41
|
+
"@stylistic/eslint-plugin": "5.8.0",
|
|
42
42
|
"acorn": "8.15.0",
|
|
43
|
-
"body-parser": "2.2.
|
|
43
|
+
"body-parser": "2.2.2",
|
|
44
44
|
"c8": "10.1.3",
|
|
45
|
-
"chai": "6.
|
|
46
|
-
"eslint": "9.
|
|
45
|
+
"chai": "6.2.2",
|
|
46
|
+
"eslint": "9.39.2",
|
|
47
47
|
"eslint-plugin-import": "2.32.0",
|
|
48
|
-
"express": "5.1
|
|
48
|
+
"express": "5.2.1",
|
|
49
49
|
"husky": "9.1.7",
|
|
50
|
-
"jsdoc-to-markdown": "9.1.
|
|
51
|
-
"mocha": "11.7.
|
|
50
|
+
"jsdoc-to-markdown": "9.1.3",
|
|
51
|
+
"mocha": "11.7.5",
|
|
52
52
|
"mochawesome": "7.1.4",
|
|
53
|
-
"sinon": "21.0.
|
|
53
|
+
"sinon": "21.0.1"
|
|
54
54
|
}
|
|
55
55
|
}
|
package/test/retryMock.js
CHANGED
|
@@ -30,11 +30,11 @@ let message;
|
|
|
30
30
|
|
|
31
31
|
app.use(bodyParser.json());
|
|
32
32
|
app.get(`${config.base}${config.path}`, (req, res) => {
|
|
33
|
-
if (nbRequest === 0) message = '
|
|
33
|
+
if (nbRequest === 0) message = 'Received first request';
|
|
34
34
|
else {
|
|
35
35
|
const timeLap = Date.now() - time;
|
|
36
36
|
|
|
37
|
-
message = `Received ${nbRequest} retry with
|
|
37
|
+
message = `Received ${nbRequest} retry with timelapse ${timeLap}`;
|
|
38
38
|
}
|
|
39
39
|
console.log('----->', message);
|
|
40
40
|
time = Date.now();
|
package/test/rpRetry.spec.js
CHANGED
|
@@ -139,7 +139,7 @@ describe('request-retry Unit Tests', () => {
|
|
|
139
139
|
expect(callArgs[CALL_ARG_MESSAGE]).to.equal('out of scope timeout, reset to min');
|
|
140
140
|
});
|
|
141
141
|
});
|
|
142
|
-
it('should generate warning: out of scope timeout: 150
|
|
142
|
+
it('should generate warning: out of scope timeout: 150 becoming max', () => {
|
|
143
143
|
options.retry.timeout = OUT_OF_SCOPE_RETRY_TIMEOUT;
|
|
144
144
|
return rpRetry(options).then(() => {
|
|
145
145
|
assert.calledOnce(loggerSpyWarn);
|
|
@@ -163,7 +163,7 @@ describe('request-retry Unit Tests', () => {
|
|
|
163
163
|
assert.notCalled(loggerSpyWarn);
|
|
164
164
|
});
|
|
165
165
|
});
|
|
166
|
-
it('should generate warning: invalid retryStrategy: "
|
|
166
|
+
it('should generate warning: invalid retryStrategy: "notAFunction" becoming default function', () => {
|
|
167
167
|
options.retry.retryStrategy = 'notAFunction';
|
|
168
168
|
return rpRetry(options).then(() => {
|
|
169
169
|
assert.calledOnce(loggerSpyWarn);
|
|
@@ -240,7 +240,7 @@ describe('request-retry Unit Tests', () => {
|
|
|
240
240
|
expect(callArgs[CALL_ARG_MESSAGE]).to.equal('invalid logLevel.responseName, reset to default');
|
|
241
241
|
});
|
|
242
242
|
});
|
|
243
|
-
it('should generate warning: invalid logLevel.responseName ("
|
|
243
|
+
it('should generate warning: invalid logLevel.responseName (" "), reset to default', () => {
|
|
244
244
|
options.retry.logLevel = {
|
|
245
245
|
responseName: ' ',
|
|
246
246
|
};
|