@ezs/basics 1.15.5 → 1.17.1

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 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.1](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.17.0...@ezs/basics@1.17.1) (2022-04-01)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * erratic error with store ([a26febc](https://github.com/Inist-CNRS/ezs/commit/a26febc4fe7bc0a66a7d32781dc6ef175f707f0a))
12
+
13
+
14
+
15
+
16
+
17
+ # [1.17.0](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.16.0...@ezs/basics@1.17.0) (2022-03-25)
18
+
19
+
20
+ ### Features
21
+
22
+ * 🎸 add retry feature ([166c913](https://github.com/Inist-CNRS/ezs/commit/166c913302b4d305ea34d3b84f9b8be6b5188cbb))
23
+
24
+
25
+
26
+
27
+
28
+ # [1.16.0](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.15.5...@ezs/basics@1.16.0) (2022-02-07)
29
+
30
+
31
+ ### Features
32
+
33
+ * 🎸 module.exports for all packages ([086a289](https://github.com/Inist-CNRS/ezs/commit/086a289ccbaa5c72ee7bc6652ab3c6c6b5578138))
34
+
35
+
36
+
37
+
38
+
6
39
  ## [1.15.5](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.15.4...@ezs/basics@1.15.5) (2022-02-04)
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
- var _default = {
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
- exports.default = _default;
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;
@@ -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,29 +50,23 @@ 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
- this.whenReady = (0, _fetchWithProxy.default)(url, {
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: this.input.pipe(ezs('dump')).pipe(ezs.toBuffer()),
59
+ body: bodyIn,
52
60
  timeout,
53
61
  headers,
54
62
  signal: controller.signal
55
- }).then(({
56
- body,
57
- status,
58
- statusText
59
- }) => {
60
- if (status !== 200) {
61
- const msg = `Received status code ${status} (${statusText})`;
62
- throw new Error(msg);
63
- }
63
+ };
64
+ const options = {
65
+ retries
66
+ };
64
67
 
65
- const output = json ? body.pipe(_JSONStream.default.parse('*')) : body;
66
- output.once('error', () => controller.abort());
67
- this.whenFinish = feed.flow(output);
68
- return Promise.resolve(true);
69
- }).catch(e => {
68
+ const onError = e => {
70
69
  controller.abort();
71
- this.whenFinish = Promise.resolve(true);
72
70
 
73
71
  if (!noerror) {
74
72
  (0, _debug.default)('ezs')(`Break item #${this.getIndex()} [URLConnect] <${e}>`);
@@ -77,13 +75,26 @@ function URLConnect(data, feed) {
77
75
  (0, _debug.default)('ezs')(`Ignore item #${this.getIndex()} [URLConnect] <${e}>`);
78
76
  }
79
77
 
80
- return Promise.resolve(true);
81
- });
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;
82
92
  }
83
93
 
84
94
  if (this.isLast()) {
85
- this.whenReady.finally(() => this.whenFinish.finally(() => feed.close()));
86
- return this.input.end();
95
+ this.input.end();
96
+ this.whenFinish.finally(() => feed.close());
97
+ return;
87
98
  }
88
99
 
89
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,13 +70,7 @@ async function URLFetch(data, feed) {
63
70
  }
64
71
 
65
72
  try {
66
- const response = await (0, _fetchWithProxy.default)(url, parameters);
67
-
68
- if (!response.ok) {
69
- const msg = `Received status code ${response.status} (${response.statusText})`;
70
- throw new Error(msg);
71
- }
72
-
73
+ const response = await (0, _asyncRetry.default)((0, _request.default)(url, parameters), options);
73
74
  const func = json ? 'json' : 'text';
74
75
  const value = await response[func]();
75
76
 
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,32 +94,26 @@ 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
- try {
105
- const response = await (0, _fetchWithProxy.default)(cURL.href, {
106
- timeout,
107
- headers,
108
- signal: controller.signal
109
- });
110
-
111
- if (!response.ok) {
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) {
@@ -126,5 +123,14 @@ async function URLStream(data, feed) {
126
123
 
127
124
  (0, _debug.default)('ezs')(`Break item #${this.getIndex()} [URLStream] <${e}>`);
128
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);
129
135
  }
130
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.15.5",
4
+ "version": "1.17.1",
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": "c428e447199c38dd527d94df7f2f08a3aa72b405",
37
+ "gitHead": "0ccde3c9e03a451c95a20372ff41437c9ec214e7",
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": "1.27.2"
45
+ "@ezs/core": "*"
45
46
  },
46
47
  "publishConfig": {
47
48
  "access": "public"