@ezs/basics 1.22.3 → 1.22.5

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,28 @@
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.22.5](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.22.4...@ezs/basics@1.22.5) (2022-12-21)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * 🐛 timeout and retry error ([efa2cfa](https://github.com/Inist-CNRS/ezs/commit/efa2cfaab4b827700d8e89d1bc59cfb266237ec2))
12
+
13
+
14
+
15
+
16
+
17
+ ## [1.22.4](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.22.3...@ezs/basics@1.22.4) (2022-12-02)
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * on retry, input stream was lost ([1b710f1](https://github.com/Inist-CNRS/ezs/commit/1b710f19d1dd163bcaf3580f0d23def6f948423a))
23
+
24
+
25
+
26
+
27
+
6
28
  ## [1.22.3](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.22.2...@ezs/basics@1.22.3) (2022-09-19)
7
29
 
8
30
 
package/README.md CHANGED
@@ -619,6 +619,11 @@ The output will be the returned content of URL.
619
619
 
620
620
  Useful to send JSON data to an API and get results.
621
621
 
622
+ Warning :
623
+ if retries === 1, it will directly use the stream
624
+ to connect to the server otherwise the stream will be fully
625
+ read to be buffered and sent to the server (n times)
626
+
622
627
  #### Parameters
623
628
 
624
629
  - `url` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** URL to fetch
@@ -17,7 +17,9 @@ var _parseHeaders = _interopRequireDefault(require("parse-headers"));
17
17
 
18
18
  var _asyncRetry = _interopRequireDefault(require("async-retry"));
19
19
 
20
- var _request = _interopRequireDefault(require("./request"));
20
+ var _getStream = _interopRequireDefault(require("get-stream"));
21
+
22
+ var _fetchWithProxy = _interopRequireDefault(require("fetch-with-proxy"));
21
23
 
22
24
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
25
 
@@ -28,6 +30,11 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
28
30
  *
29
31
  * Useful to send JSON data to an API and get results.
30
32
  *
33
+ * Warning :
34
+ * if retries === 1, it will directly use the stream
35
+ * to connect to the server otherwise the stream will be fully
36
+ * read to be buffered and sent to the server (n times)
37
+ *
31
38
  * @name URLConnect
32
39
  * @param {String} [url] URL to fetch
33
40
  * @param {String} [json=false] Parse as JSON the content of URL
@@ -48,26 +55,78 @@ async function URLConnect(data, feed) {
48
55
  if (this.isFirst()) {
49
56
  const timeout = Number(this.getParam('timeout')) || 1000;
50
57
  const headers = (0, _parseHeaders.default)([].concat(this.getParam('header')).filter(Boolean).join('\n'));
51
- const controller = new _nodeAbortController.default();
52
58
  this.input = ezs.createStream(ezs.objectMode());
53
59
  const output = ezs.createStream(ezs.objectMode());
54
60
  this.whenFinish = feed.flow(output);
55
61
  (0, _streamWrite.default)(this.input, data, () => feed.end());
56
- const bodyIn = this.input.pipe(ezs('dump')).pipe(ezs.toBuffer());
62
+ const streamIn = this.input.pipe(ezs('dump'));
63
+ let bodyIn;
64
+
65
+ if (retries === 1) {
66
+ bodyIn = streamIn.pipe(ezs.toBuffer());
67
+ } else {
68
+ bodyIn = await (0, _getStream.default)(streamIn);
69
+ headers['Content-Type'] = 'application/json';
70
+ }
71
+
57
72
  const parameters = {
58
73
  method: 'POST',
59
74
  body: bodyIn,
60
- timeout,
61
- headers,
62
- signal: controller.signal
63
- };
64
- const options = {
65
- retries
75
+ headers
66
76
  };
67
77
 
68
- const onError = e => {
69
- controller.abort();
70
-
78
+ try {
79
+ await (0, _asyncRetry.default)(async (bail, numberOfTimes) => {
80
+ if (numberOfTimes > 1) {
81
+ (0, _debug.default)('ezs')(`Attempts to reconnect (${numberOfTimes})`);
82
+ }
83
+
84
+ const controller = new _nodeAbortController.default();
85
+ const timeoutHandle = setTimeout(() => {
86
+ (0, _debug.default)('ezs')(`The maximum time allowed to start sending data has been reached (${timeout} msec).`);
87
+ controller.abort();
88
+ }, timeout);
89
+ const response = await (0, _fetchWithProxy.default)(url, { ...parameters,
90
+ signal: controller.signal
91
+ });
92
+
93
+ if (!response.ok) {
94
+ const err = new Error(response.statusText);
95
+ const text = await response.text();
96
+ err.responseText = text;
97
+ throw err;
98
+ }
99
+
100
+ if (retries === 1) {
101
+ const bodyOut = json ? response.body.pipe(_JSONStream.default.parse('*')) : response.body;
102
+ bodyOut.once('data', () => {
103
+ clearTimeout(timeoutHandle);
104
+ });
105
+ bodyOut.once('error', e => {
106
+ output.emit('error', e);
107
+ clearTimeout(timeoutHandle);
108
+ });
109
+ bodyOut.pipe(output);
110
+ } else {
111
+ const bodyOutRaw = await (0, _getStream.default)(response.body);
112
+
113
+ if (json) {
114
+ try {
115
+ const bodyOut = JSON.parse(bodyOutRaw);
116
+ bodyOut.forEach(item => output.write(item));
117
+ } catch (ee) {
118
+ throw ee;
119
+ }
120
+ } else {
121
+ output.write(bodyOutRaw);
122
+ }
123
+
124
+ output.end();
125
+ }
126
+ }, {
127
+ retries
128
+ });
129
+ } catch (e) {
71
130
  if (!noerror) {
72
131
  (0, _debug.default)('ezs')(`Break item #${this.getIndex()} [URLConnect] <${e}>`);
73
132
  feed.stop(e);
@@ -76,15 +135,6 @@ async function URLConnect(data, feed) {
76
135
  }
77
136
 
78
137
  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
138
  }
89
139
 
90
140
  ;
@@ -93,7 +143,9 @@ async function URLConnect(data, feed) {
93
143
 
94
144
  if (this.isLast()) {
95
145
  this.input.end();
96
- this.whenFinish.finally(() => feed.close());
146
+ this.whenFinish.finally(() => {
147
+ feed.close();
148
+ });
97
149
  return;
98
150
  }
99
151
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ezs/basics",
3
3
  "description": "Basics statements for EZS",
4
- "version": "1.22.3",
4
+ "version": "1.22.5",
5
5
  "author": "Nicolas Thouvenin <nthouvenin@gmail.com>",
6
6
  "bugs": "https://github.com/Inist-CNRS/ezs/issues",
7
7
  "dependencies": {
@@ -12,6 +12,7 @@
12
12
  "debug": "4.3.3",
13
13
  "fetch-with-proxy": "3.0.1",
14
14
  "flat": "5.0.2",
15
+ "get-stream": "6.0.1",
15
16
  "lodash.escaperegexp": "4.1.2",
16
17
  "lodash.get": "4.4.2",
17
18
  "lodash.mapkeys": "4.6.0",
@@ -36,7 +37,7 @@
36
37
  "directories": {
37
38
  "test": "test"
38
39
  },
39
- "gitHead": "7fa485938639c0821968dc60de66d248e15a9cdf",
40
+ "gitHead": "41c6ff719d5312aef866016cbe891aa2cc53fa4e",
40
41
  "homepage": "https://github.com/Inist-CNRS/ezs/tree/master/packages/basics#readme",
41
42
  "keywords": [
42
43
  "ezs"