@ezs/basics 1.15.4 → 1.17.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/CHANGELOG.md +33 -0
- package/README.md +3 -0
- package/lib/index.js +4 -2
- package/lib/request.js +23 -0
- package/lib/url-connect.js +37 -24
- package/lib/url-fetch.js +12 -4
- package/lib/url-stream.js +26 -19
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,39 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [1.17.0](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.16.0...@ezs/basics@1.17.0) (2022-03-25)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* 🎸 add retry feature ([166c913](https://github.com/Inist-CNRS/ezs/commit/166c913302b4d305ea34d3b84f9b8be6b5188cbb))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# [1.16.0](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.15.5...@ezs/basics@1.16.0) (2022-02-07)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
* 🎸 module.exports for all packages ([086a289](https://github.com/Inist-CNRS/ezs/commit/086a289ccbaa5c72ee7bc6652ab3c6c6b5578138))
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
## [1.15.5](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.15.4...@ezs/basics@1.15.5) (2022-02-04)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
### Bug Fixes
|
|
32
|
+
|
|
33
|
+
* 🐛 improve warning debug message on error ([5ff32e8](https://github.com/Inist-CNRS/ezs/commit/5ff32e8e2e6c48913d8d1abd35b9cee68ff19be4))
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
6
39
|
## [1.15.4](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.15.3...@ezs/basics@1.15.4) (2022-01-31)
|
|
7
40
|
|
|
8
41
|
|
package/README.md
CHANGED
|
@@ -548,6 +548,7 @@ Useful to send JSON data to an API and get results.
|
|
|
548
548
|
- `json` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Parse as JSON the content of URL (optional, default `false`)
|
|
549
549
|
- `timeout` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Timeout in milliseconds (optional, default `1000`)
|
|
550
550
|
- `noerror` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Ignore all errors (optional, default `false`)
|
|
551
|
+
- `retries` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** The maximum amount of times to retry the connection (optional, default `5`)
|
|
551
552
|
|
|
552
553
|
Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
|
|
553
554
|
|
|
@@ -566,6 +567,7 @@ Or if no target is specified, the output will be the returned content of URL.
|
|
|
566
567
|
- `timeout` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** timeout in milliseconds (optional, default `1000`)
|
|
567
568
|
- `mimetype` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** mimetype for value of path (if presents) (optional, default `"application/json"`)
|
|
568
569
|
- `noerror` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** ignore all errors, the target field will remain undefined (optional, default `false`)
|
|
570
|
+
- `retries` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** The maximum amount of times to retry the connection (optional, default `5`)
|
|
569
571
|
|
|
570
572
|
Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
|
|
571
573
|
|
|
@@ -660,6 +662,7 @@ Output:
|
|
|
660
662
|
- `path` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** choose the path to split JSON result (optional, default `"*"`)
|
|
661
663
|
- `timeout` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Timeout in milliseconds (optional, default `1000`)
|
|
662
664
|
- `noerror` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Ignore all errors, the target field will remain undefined (optional, default `false`)
|
|
665
|
+
- `retries` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** The maximum amount of times to retry the connection (optional, default `5`)
|
|
663
666
|
|
|
664
667
|
Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
|
|
665
668
|
|
package/lib/index.js
CHANGED
|
@@ -55,7 +55,7 @@ var _iniString = _interopRequireDefault(require("./ini-string"));
|
|
|
55
55
|
|
|
56
56
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
57
57
|
|
|
58
|
-
|
|
58
|
+
const funcs = {
|
|
59
59
|
BUFObject: _bufObject.default,
|
|
60
60
|
OBJCount: _objCount.default,
|
|
61
61
|
OBJNamespaces: _objNamespaces.default,
|
|
@@ -87,4 +87,6 @@ var _default = {
|
|
|
87
87
|
split: _txtParse.default.TXTParse,
|
|
88
88
|
segmenter: _txtParse.default.TXTParse
|
|
89
89
|
};
|
|
90
|
-
|
|
90
|
+
var _default = funcs;
|
|
91
|
+
exports.default = _default;
|
|
92
|
+
module.exports = funcs;
|
package/lib/request.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
|
|
8
|
+
var _fetchWithProxy = _interopRequireDefault(require("fetch-with-proxy"));
|
|
9
|
+
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
|
|
12
|
+
const request = (url, parameters) => async () => {
|
|
13
|
+
const response = await (0, _fetchWithProxy.default)(url, parameters);
|
|
14
|
+
|
|
15
|
+
if (!response.ok) {
|
|
16
|
+
throw new Error(response.statusText);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return response;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
var _default = request;
|
|
23
|
+
exports.default = _default;
|
package/lib/url-connect.js
CHANGED
|
@@ -13,10 +13,12 @@ var _streamWrite = _interopRequireDefault(require("stream-write"));
|
|
|
13
13
|
|
|
14
14
|
var _nodeAbortController = _interopRequireDefault(require("node-abort-controller"));
|
|
15
15
|
|
|
16
|
-
var _fetchWithProxy = _interopRequireDefault(require("fetch-with-proxy"));
|
|
17
|
-
|
|
18
16
|
var _parseHeaders = _interopRequireDefault(require("parse-headers"));
|
|
19
17
|
|
|
18
|
+
var _asyncRetry = _interopRequireDefault(require("async-retry"));
|
|
19
|
+
|
|
20
|
+
var _request = _interopRequireDefault(require("./request"));
|
|
21
|
+
|
|
20
22
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
23
|
|
|
22
24
|
/**
|
|
@@ -31,10 +33,12 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
31
33
|
* @param {String} [json=false] Parse as JSON the content of URL
|
|
32
34
|
* @param {Number} [timeout=1000] Timeout in milliseconds
|
|
33
35
|
* @param {Boolean} [noerror=false] Ignore all errors
|
|
36
|
+
* @param {Number} [retries=5] The maximum amount of times to retry the connection
|
|
34
37
|
* @returns {Object}
|
|
35
38
|
*/
|
|
36
|
-
function URLConnect(data, feed) {
|
|
39
|
+
async function URLConnect(data, feed) {
|
|
37
40
|
const url = this.getParam('url');
|
|
41
|
+
const retries = Number(this.getParam('retries', 5));
|
|
38
42
|
const noerror = Boolean(this.getParam('noerror', false));
|
|
39
43
|
const json = this.getParam('json', true);
|
|
40
44
|
const {
|
|
@@ -46,42 +50,51 @@ function URLConnect(data, feed) {
|
|
|
46
50
|
const headers = (0, _parseHeaders.default)([].concat(this.getParam('header')).filter(Boolean).join('\n'));
|
|
47
51
|
const controller = new _nodeAbortController.default();
|
|
48
52
|
this.input = ezs.createStream(ezs.objectMode());
|
|
49
|
-
|
|
53
|
+
const output = ezs.createStream(ezs.objectMode());
|
|
54
|
+
this.whenFinish = feed.flow(output);
|
|
55
|
+
(0, _streamWrite.default)(this.input, data, () => feed.end());
|
|
56
|
+
const bodyIn = this.input.pipe(ezs('dump')).pipe(ezs.toBuffer());
|
|
57
|
+
const parameters = {
|
|
50
58
|
method: 'POST',
|
|
51
|
-
body:
|
|
59
|
+
body: bodyIn,
|
|
52
60
|
timeout,
|
|
61
|
+
headers,
|
|
53
62
|
signal: controller.signal
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}) => {
|
|
59
|
-
if (status !== 200) {
|
|
60
|
-
const msg = `Received status code ${status} (${statusText})`;
|
|
61
|
-
throw new Error(msg);
|
|
62
|
-
}
|
|
63
|
+
};
|
|
64
|
+
const options = {
|
|
65
|
+
retries
|
|
66
|
+
};
|
|
63
67
|
|
|
64
|
-
|
|
65
|
-
output.once('error', () => controller.abort());
|
|
66
|
-
this.whenFinish = feed.flow(output);
|
|
67
|
-
return Promise.resolve(true);
|
|
68
|
-
}).catch(e => {
|
|
68
|
+
const onError = e => {
|
|
69
69
|
controller.abort();
|
|
70
|
-
this.whenFinish = Promise.resolve(true);
|
|
71
70
|
|
|
72
71
|
if (!noerror) {
|
|
72
|
+
(0, _debug.default)('ezs')(`Break item #${this.getIndex()} [URLConnect] <${e}>`);
|
|
73
73
|
feed.stop(e);
|
|
74
74
|
} else {
|
|
75
75
|
(0, _debug.default)('ezs')(`Ignore item #${this.getIndex()} [URLConnect] <${e}>`);
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
}
|
|
78
|
+
output.end();
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
try {
|
|
82
|
+
const response = await (0, _asyncRetry.default)((0, _request.default)(url, parameters), options);
|
|
83
|
+
const bodyOut = json ? response.body.pipe(_JSONStream.default.parse('*')) : response.body;
|
|
84
|
+
bodyOut.once('error', onError);
|
|
85
|
+
bodyOut.pipe(output);
|
|
86
|
+
} catch (e) {
|
|
87
|
+
onError(e);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
;
|
|
91
|
+
return;
|
|
80
92
|
}
|
|
81
93
|
|
|
82
94
|
if (this.isLast()) {
|
|
83
|
-
this.
|
|
84
|
-
|
|
95
|
+
this.input.end();
|
|
96
|
+
this.whenFinish.finally(() => feed.close());
|
|
97
|
+
return;
|
|
85
98
|
}
|
|
86
99
|
|
|
87
100
|
(0, _streamWrite.default)(this.input, data, () => feed.end());
|
package/lib/url-fetch.js
CHANGED
|
@@ -13,10 +13,12 @@ var _lodash2 = _interopRequireDefault(require("lodash.set"));
|
|
|
13
13
|
|
|
14
14
|
var _nodeAbortController = _interopRequireDefault(require("node-abort-controller"));
|
|
15
15
|
|
|
16
|
-
var _fetchWithProxy = _interopRequireDefault(require("fetch-with-proxy"));
|
|
17
|
-
|
|
18
16
|
var _parseHeaders = _interopRequireDefault(require("parse-headers"));
|
|
19
17
|
|
|
18
|
+
var _asyncRetry = _interopRequireDefault(require("async-retry"));
|
|
19
|
+
|
|
20
|
+
var _request = _interopRequireDefault(require("./request"));
|
|
21
|
+
|
|
20
22
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
23
|
|
|
22
24
|
/**
|
|
@@ -32,6 +34,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
32
34
|
* @param {Number} [timeout=1000] timeout in milliseconds
|
|
33
35
|
* @param {String} [mimetype="application/json"] mimetype for value of path (if presents)
|
|
34
36
|
* @param {Boolean} [noerror=false] ignore all errors, the target field will remain undefined
|
|
37
|
+
* @param {Number} [retries=5] The maximum amount of times to retry the connection
|
|
35
38
|
* @returns {Object}
|
|
36
39
|
*/
|
|
37
40
|
async function URLFetch(data, feed) {
|
|
@@ -43,6 +46,7 @@ async function URLFetch(data, feed) {
|
|
|
43
46
|
const path = this.getParam('path');
|
|
44
47
|
const target = this.getParam('target');
|
|
45
48
|
const json = Boolean(this.getParam('json', false));
|
|
49
|
+
const retries = Number(this.getParam('retries', 5));
|
|
46
50
|
const noerror = Boolean(this.getParam('noerror', false));
|
|
47
51
|
const timeout = Number(this.getParam('timeout')) || 1000;
|
|
48
52
|
const headers = (0, _parseHeaders.default)([].concat(this.getParam('header')).filter(Boolean).join('\n'));
|
|
@@ -55,6 +59,9 @@ async function URLFetch(data, feed) {
|
|
|
55
59
|
headers,
|
|
56
60
|
signal: controller.signal
|
|
57
61
|
};
|
|
62
|
+
const options = {
|
|
63
|
+
retries
|
|
64
|
+
};
|
|
58
65
|
|
|
59
66
|
if (body) {
|
|
60
67
|
(0, _lodash2.default)(parameters, 'method', 'POST');
|
|
@@ -63,7 +70,7 @@ async function URLFetch(data, feed) {
|
|
|
63
70
|
}
|
|
64
71
|
|
|
65
72
|
try {
|
|
66
|
-
const response = await (0,
|
|
73
|
+
const response = await (0, _asyncRetry.default)((0, _request.default)(url, parameters), options);
|
|
67
74
|
const func = json ? 'json' : 'text';
|
|
68
75
|
const value = await response[func]();
|
|
69
76
|
|
|
@@ -79,10 +86,11 @@ async function URLFetch(data, feed) {
|
|
|
79
86
|
controller.abort();
|
|
80
87
|
|
|
81
88
|
if (noerror) {
|
|
89
|
+
(0, _debug.default)('ezs')(`Ignore item #${this.getIndex()} [URLFetch] <${e}>`);
|
|
82
90
|
return feed.send(data);
|
|
83
91
|
}
|
|
84
92
|
|
|
85
|
-
(0, _debug.default)('ezs')(`
|
|
93
|
+
(0, _debug.default)('ezs')(`Break item #${this.getIndex()} [URLFetch] <${e}>`);
|
|
86
94
|
return feed.send(e);
|
|
87
95
|
}
|
|
88
96
|
}
|
package/lib/url-stream.js
CHANGED
|
@@ -13,10 +13,12 @@ var _nodeAbortController = _interopRequireDefault(require("node-abort-controller
|
|
|
13
13
|
|
|
14
14
|
var _JSONStream = _interopRequireDefault(require("JSONStream"));
|
|
15
15
|
|
|
16
|
-
var _fetchWithProxy = _interopRequireDefault(require("fetch-with-proxy"));
|
|
17
|
-
|
|
18
16
|
var _parseHeaders = _interopRequireDefault(require("parse-headers"));
|
|
19
17
|
|
|
18
|
+
var _asyncRetry = _interopRequireDefault(require("async-retry"));
|
|
19
|
+
|
|
20
|
+
var _request = _interopRequireDefault(require("./request"));
|
|
21
|
+
|
|
20
22
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
21
23
|
|
|
22
24
|
/**
|
|
@@ -82,6 +84,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
82
84
|
* @param {String} [path="*"] choose the path to split JSON result
|
|
83
85
|
* @param {Number} [timeout=1000] Timeout in milliseconds
|
|
84
86
|
* @param {Boolean} [noerror=false] Ignore all errors, the target field will remain undefined
|
|
87
|
+
* @param {Number} [retries=5] The maximum amount of times to retry the connection
|
|
85
88
|
* @returns {Object}
|
|
86
89
|
*/
|
|
87
90
|
async function URLStream(data, feed) {
|
|
@@ -91,39 +94,43 @@ async function URLStream(data, feed) {
|
|
|
91
94
|
|
|
92
95
|
const url = this.getParam('url');
|
|
93
96
|
const path = this.getParam('path', '*');
|
|
97
|
+
const retries = Number(this.getParam('retries', 5));
|
|
94
98
|
const noerror = Boolean(this.getParam('noerror', false));
|
|
95
99
|
const timeout = Number(this.getParam('timeout')) || 1000;
|
|
96
100
|
const headers = (0, _parseHeaders.default)([].concat(this.getParam('header')).filter(Boolean).join('\n'));
|
|
97
101
|
const cURL = new _url.URL(url || data);
|
|
98
102
|
const controller = new _nodeAbortController.default();
|
|
103
|
+
const parameters = {
|
|
104
|
+
timeout,
|
|
105
|
+
headers,
|
|
106
|
+
signal: controller.signal
|
|
107
|
+
};
|
|
108
|
+
const options = {
|
|
109
|
+
retries
|
|
110
|
+
};
|
|
99
111
|
|
|
100
112
|
if (url) {
|
|
101
113
|
cURL.search = new _url.URLSearchParams(data);
|
|
102
114
|
}
|
|
103
115
|
|
|
104
|
-
|
|
105
|
-
const response = await (0, _fetchWithProxy.default)(cURL.href, {
|
|
106
|
-
headers,
|
|
107
|
-
timeout,
|
|
108
|
-
signal: controller.signal
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
if (response.status !== 200) {
|
|
112
|
-
const msg = `Received status code ${response.status} (${response.statusText})`;
|
|
113
|
-
throw new Error(msg);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
const output = path ? response.body.pipe(_JSONStream.default.parse(path)) : response.body;
|
|
117
|
-
output.once('error', () => controller.abort());
|
|
118
|
-
await feed.flow(output);
|
|
119
|
-
} catch (e) {
|
|
116
|
+
const onError = e => {
|
|
120
117
|
controller.abort();
|
|
121
118
|
|
|
122
119
|
if (noerror) {
|
|
120
|
+
(0, _debug.default)('ezs')(`Ignore item #${this.getIndex()} [URLStream] <${e}>`);
|
|
123
121
|
return feed.send(data);
|
|
124
122
|
}
|
|
125
123
|
|
|
126
|
-
(0, _debug.default)('ezs')(`
|
|
124
|
+
(0, _debug.default)('ezs')(`Break item #${this.getIndex()} [URLStream] <${e}>`);
|
|
127
125
|
return feed.send(e);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
try {
|
|
129
|
+
const response = await (0, _asyncRetry.default)((0, _request.default)(cURL.href, parameters), options);
|
|
130
|
+
const output = path ? response.body.pipe(_JSONStream.default.parse(path)) : response.body;
|
|
131
|
+
output.once('error', onError);
|
|
132
|
+
await feed.flow(output);
|
|
133
|
+
} catch (e) {
|
|
134
|
+
onError(e);
|
|
128
135
|
}
|
|
129
136
|
}
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ezs/basics",
|
|
3
3
|
"description": "Basics statements for EZS",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.17.0",
|
|
5
5
|
"author": "Nicolas Thouvenin <nthouvenin@gmail.com>",
|
|
6
6
|
"bugs": "https://github.com/Inist-CNRS/ezs/issues",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"JSONStream": "1.3.5",
|
|
9
|
+
"async-retry": "1.3.3",
|
|
9
10
|
"better-https-proxy-agent": "1.0.9",
|
|
10
11
|
"csv-string": "3.2.0",
|
|
11
12
|
"debug": "4.3.3",
|
|
@@ -33,7 +34,7 @@
|
|
|
33
34
|
"directories": {
|
|
34
35
|
"test": "test"
|
|
35
36
|
},
|
|
36
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "7c26e58f03f6675d98f22914cf5e315a89916e2e",
|
|
37
38
|
"homepage": "https://github.com/Inist-CNRS/ezs/tree/master/packages/basics#readme",
|
|
38
39
|
"keywords": [
|
|
39
40
|
"ezs"
|
|
@@ -41,7 +42,7 @@
|
|
|
41
42
|
"license": "MIT",
|
|
42
43
|
"main": "./lib/index.js",
|
|
43
44
|
"peerDependencies": {
|
|
44
|
-
"@ezs/core": "
|
|
45
|
+
"@ezs/core": "*"
|
|
45
46
|
},
|
|
46
47
|
"publishConfig": {
|
|
47
48
|
"access": "public"
|