@ezs/basics 1.20.0 → 1.22.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,44 @@
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.1](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.22.0...@ezs/basics@1.22.1) (2022-09-07)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * 🐛 stop loop (and refactoring) ([2c162aa](https://github.com/Inist-CNRS/ezs/commit/2c162aa64a4603f79c5bdddbca8b58272278f745))
12
+
13
+
14
+
15
+
16
+
17
+ # [1.22.0](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.21.0...@ezs/basics@1.22.0) (2022-09-05)
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * use location to check filename ([cb9c6e2](https://github.com/Inist-CNRS/ezs/commit/cb9c6e2f42d12c287d806b4ccebf8a4f8607e755))
23
+
24
+
25
+ ### Features
26
+
27
+ * 🎸 add file load ([a06c221](https://github.com/Inist-CNRS/ezs/commit/a06c221c3463b4299383db5c613f5cabc687961c))
28
+
29
+
30
+
31
+
32
+
33
+ # [1.21.0](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.20.0...@ezs/basics@1.21.0) (2022-07-11)
34
+
35
+
36
+ ### Features
37
+
38
+ * 🎸 add target option to URLRequest ([3861c5e](https://github.com/Inist-CNRS/ezs/commit/3861c5e10b4dcfd060b49fa9b23f2bcf98d8c942))
39
+
40
+
41
+
42
+
43
+
6
44
  # [1.20.0](https://github.com/Inist-CNRS/ezs/compare/@ezs/basics@1.19.0...@ezs/basics@1.20.0) (2022-06-24)
7
45
 
8
46
 
package/README.md CHANGED
@@ -18,6 +18,7 @@ npm install @ezs/basics
18
18
  - [CSVObject](#csvobject)
19
19
  - [CSVParse](#csvparse)
20
20
  - [CSVString](#csvstring)
21
+ - [FILELoad](#fileload)
21
22
  - [FILESave](#filesave)
22
23
  - [INIString](#inistring)
23
24
  - [JSONParse](#jsonparse)
@@ -192,6 +193,41 @@ a;b;c
192
193
 
193
194
  Returns **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**
194
195
 
196
+ ### FILELoad
197
+
198
+ Take `Object` containing filename et throw content by chunk
199
+
200
+ ```json
201
+ [ fi1e1.csv, file2.csv ]
202
+ ```
203
+
204
+ Script:
205
+
206
+ ```ini
207
+ [use]
208
+ plugin = analytics
209
+ plugin = basics
210
+
211
+ [FILELoad]
212
+ location = /tmp
213
+ [CSVParse]
214
+ ```
215
+
216
+ Output:
217
+
218
+ ```json
219
+ [
220
+ (...)
221
+ ]
222
+ ```
223
+
224
+ #### Parameters
225
+
226
+ - `location` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** Directory location (optional, default `TMPDIR`)
227
+ - `compress` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** Enable gzip compression (optional, default `false`)
228
+
229
+ Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
230
+
195
231
  ### FILESave
196
232
 
197
233
  Take data, convert it to buffer and append it to file
@@ -723,7 +759,7 @@ Output:
723
759
  ```json
724
760
  [
725
761
  {
726
- "result": "a"
762
+ "result": "a"
727
763
  }
728
764
  ]
729
765
  ```
@@ -732,9 +768,11 @@ Output:
732
768
 
733
769
  - `url` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** URL to fetch
734
770
  - `json` **[Boolean](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** parse result as json (optional, default `true`)
771
+ - `target` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** choose the key to set
735
772
  - `timeout` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** Timeout in milliseconds (optional, default `1000`)
736
773
  - `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`)
737
774
  - `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`)
775
+ - `insert` **[String](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)?** a header response value in the result
738
776
 
739
777
  Returns **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)**
740
778
 
@@ -0,0 +1,73 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = FILELoad;
7
+
8
+ var _zlib = require("zlib");
9
+
10
+ var _fs = require("fs");
11
+
12
+ var _path = require("path");
13
+
14
+ var _os = require("os");
15
+
16
+ /**
17
+ * Take `Object` containing filename et throw content by chunk
18
+ *
19
+ * ```json
20
+ * [ fi1e1.csv, file2.csv ]
21
+ * ```
22
+ *
23
+ * Script:
24
+ *
25
+ * ```ini
26
+ * [use]
27
+ * plugin = analytics
28
+ * plugin = basics
29
+ *
30
+ * [FILELoad]
31
+ * location = /tmp
32
+ * [CSVParse]
33
+ *
34
+ * ```
35
+ *
36
+ * Output:
37
+ *
38
+ * ```json
39
+ * [
40
+ * (...)
41
+ * ]
42
+ * ```
43
+ *
44
+ * @name FILELoad
45
+ * @param {String} [location=TMPDIR] Directory location
46
+ * @param {Boolean} [compress=false] Enable gzip compression
47
+ * @returns {Object}
48
+ */
49
+ function FILELoad(data, feed) {
50
+ if (this.isLast()) {
51
+ feed.close();
52
+ return;
53
+ }
54
+
55
+ const cwd = process.cwd();
56
+ const tpd = (0, _os.tmpdir)();
57
+ const location = this.getParam('location', tpd);
58
+ const compress = this.getParam('compress', false);
59
+ const locations = [cwd, tpd, location];
60
+ const file = locations.filter(Boolean).map(dir => (0, _path.resolve)(dir, String(data).trim())).filter(fil => (0, _fs.existsSync)(fil)).shift();
61
+
62
+ if (!file) {
63
+ feed.end();
64
+ return;
65
+ }
66
+
67
+ if (compress) {
68
+ feed.flow((0, _fs.createReadStream)(file).pipe((0, _zlib.createGunzip)()));
69
+ return;
70
+ }
71
+
72
+ feed.flow((0, _fs.createReadStream)(file));
73
+ }
package/lib/file-save.js CHANGED
@@ -19,6 +19,8 @@ var _makeDir = _interopRequireDefault(require("make-dir"));
19
19
 
20
20
  var _streamWrite = _interopRequireDefault(require("stream-write"));
21
21
 
22
+ var _debug = _interopRequireDefault(require("debug"));
23
+
22
24
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
25
 
24
26
  /**
@@ -56,8 +58,10 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
56
58
  * @param {Boolean} [compress=false] Enable gzip compression
57
59
  * @returns {Object}
58
60
  */
59
- async function FILESave(data, feed) {
60
- if (!this.handle) {
61
+ function FILESave(data, feed) {
62
+ const buf = Buffer.from(String(data));
63
+
64
+ if (this.isFirst()) {
61
65
  const identifier = String(this.getParam('identifier'));
62
66
  const location = this.getParam('location', (0, _os.tmpdir)());
63
67
  const compress = this.getParam('compress', false);
@@ -66,29 +70,37 @@ async function FILESave(data, feed) {
66
70
  _makeDir.default.sync(location);
67
71
  }
68
72
 
69
- if (compress) {
70
- this.filename = _path.default.resolve(location, `${identifier}.gz`);
71
- this.handle = (0, _zlib.createGzip)();
72
- this.handleEnd = this.handle.pipe((0, _fs.createWriteStream)(this.filename));
73
- } else {
74
- this.filename = _path.default.resolve(location, identifier);
75
- this.handle = (0, _fs.createWriteStream)(this.filename);
76
- this.handleEnd = this.handle;
77
- }
78
- }
73
+ const name = compress ? `${identifier}.gz` : identifier;
79
74
 
80
- if (this.isLast()) {
81
- this.handleEnd.on('close', () => {
82
- (0, _fs.lstat)(this.filename, (err, stat) => {
83
- feed.write({
84
- filename: this.filename,
85
- ...stat
75
+ const filename = _path.default.resolve(location, name);
76
+
77
+ this.input = compress ? (0, _zlib.createGzip)() : (0, _fs.createWriteStream)(filename);
78
+ this.whenFinish = new Promise((resolve, reject) => {
79
+ const output = compress ? this.input.pipe((0, _fs.createWriteStream)(filename)) : this.input;
80
+ output.once('error', err => {
81
+ (0, _debug.default)('ezs')(`WARNING: ${filename} not saved. ${err}`);
82
+ reject(err);
83
+ });
84
+ output.once('close', () => {
85
+ (0, _debug.default)('ezs')(`${filename} saved.`);
86
+ (0, _fs.lstat)(filename, (err, stat) => {
87
+ if (err) {
88
+ return reject(err);
89
+ }
90
+
91
+ return resolve({
92
+ filename,
93
+ ...stat
94
+ });
86
95
  });
87
- return feed.close();
88
96
  });
89
97
  });
90
- return this.handle.end();
91
98
  }
92
99
 
93
- (0, _streamWrite.default)(this.handle, Buffer.from(String(data)), () => feed.end());
100
+ if (this.isLast()) {
101
+ this.input.end();
102
+ return this.whenFinish.then(stats => feed.write(stats)).catch(err => feed.stop(err)).finally(() => feed.close());
103
+ }
104
+
105
+ (0, _streamWrite.default)(this.input, buf, () => feed.end());
94
106
  }
package/lib/index.js CHANGED
@@ -59,6 +59,8 @@ var _iniString = _interopRequireDefault(require("./ini-string"));
59
59
 
60
60
  var _fileSave = _interopRequireDefault(require("./file-save"));
61
61
 
62
+ var _fileLoad = _interopRequireDefault(require("./file-load"));
63
+
62
64
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
63
65
 
64
66
  const funcs = {
@@ -89,6 +91,7 @@ const funcs = {
89
91
  ZIPExtract: _zipExtract.default,
90
92
  INIString: _iniString.default,
91
93
  FILESave: _fileSave.default,
94
+ FILELoad: _fileLoad.default,
92
95
  // aliases
93
96
  bufferify: _bufObject.default.BUFObject,
94
97
  concat: _txtConcat.default.TXTConcat,
package/lib/request.js CHANGED
@@ -13,7 +13,10 @@ const request = (url, parameters) => async () => {
13
13
  const response = await (0, _fetchWithProxy.default)(url, parameters);
14
14
 
15
15
  if (!response.ok) {
16
- throw new Error(response.statusText);
16
+ const err = new Error(response.statusText);
17
+ const text = await response.text();
18
+ err.responseText = text;
19
+ throw err;
17
20
  }
18
21
 
19
22
  return response;
package/lib/url-fetch.js CHANGED
@@ -44,7 +44,7 @@ async function URLFetch(data, feed) {
44
44
 
45
45
  const url = this.getParam('url');
46
46
  const path = this.getParam('path');
47
- const target = this.getParam('target');
47
+ const target = [].concat(this.getParam('target')).filter(Boolean).shift();
48
48
  const json = Boolean(this.getParam('json', false));
49
49
  const retries = Number(this.getParam('retries', 5));
50
50
  const noerror = Boolean(this.getParam('noerror', false));
@@ -74,8 +74,10 @@ async function URLFetch(data, feed) {
74
74
  const func = json ? 'json' : 'text';
75
75
  const value = await response[func]();
76
76
 
77
- if (target && typeof target === 'string' && typeof data === 'object') {
78
- const result = { ...data
77
+ if (target) {
78
+ const result = typeof data === 'object' ? { ...data
79
+ } : {
80
+ input: data
79
81
  };
80
82
  (0, _lodash2.default)(result, target, value);
81
83
  return feed.send(result);
@@ -5,6 +5,8 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = URLRequest;
7
7
 
8
+ var _lodash = _interopRequireDefault(require("lodash.set"));
9
+
8
10
  var _debug = _interopRequireDefault(require("debug"));
9
11
 
10
12
  var _url = require("url");
@@ -41,7 +43,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
41
43
  * ```json
42
44
  * [
43
45
  * {
44
- * "result": "a"
46
+ * "result": "a"
45
47
  * }
46
48
  * ]
47
49
  * ```
@@ -49,9 +51,11 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
49
51
  * @name URLRequest
50
52
  * @param {String} [url] URL to fetch
51
53
  * @param {Boolean} [json=true] parse result as json
54
+ * @param {String} [target] choose the key to set
52
55
  * @param {Number} [timeout=1000] Timeout in milliseconds
53
56
  * @param {Boolean} [noerror=false] Ignore all errors, the target field will remain undefined
54
57
  * @param {Number} [retries=5] The maximum amount of times to retry the connection
58
+ * @param {String} [insert] a header response value in the result
55
59
  * @returns {Object}
56
60
  */
57
61
  async function URLRequest(data, feed) {
@@ -61,10 +65,12 @@ async function URLRequest(data, feed) {
61
65
 
62
66
  const url = this.getParam('url');
63
67
  const json = Boolean(this.getParam('json', true));
68
+ const target = [].concat(this.getParam('target')).filter(Boolean).shift();
64
69
  const retries = Number(this.getParam('retries', 5));
65
70
  const noerror = Boolean(this.getParam('noerror', false));
66
71
  const timeout = Number(this.getParam('timeout')) || 1000;
67
72
  const headers = (0, _parseHeaders.default)([].concat(this.getParam('header')).filter(Boolean).join('\n'));
73
+ const inserts = [].concat(this.getParam('insert')).filter(Boolean);
68
74
  const cURL = new _url.URL(url || data);
69
75
  const controller = new _nodeAbortController.default();
70
76
  const parameters = {
@@ -75,7 +81,10 @@ async function URLRequest(data, feed) {
75
81
  const options = {
76
82
  retries
77
83
  };
78
- cURL.search = new _url.URLSearchParams(data);
84
+
85
+ if (url) {
86
+ cURL.search = new _url.URLSearchParams(data);
87
+ }
79
88
 
80
89
  const onError = e => {
81
90
  controller.abort();
@@ -93,7 +102,19 @@ async function URLRequest(data, feed) {
93
102
  const response = await (0, _asyncRetry.default)((0, _request.default)(cURL.href, parameters), options);
94
103
  const func = json ? 'json' : 'text';
95
104
  const value = await response[func]();
96
- feed.send(value);
105
+
106
+ if (target) {
107
+ const result = typeof data === 'object' ? { ...data
108
+ } : {
109
+ url: data
110
+ };
111
+ (0, _lodash.default)(result, target, value);
112
+ inserts.forEach(i => (0, _lodash.default)(result, i, response.headers.get(i)));
113
+ return feed.send(result);
114
+ }
115
+
116
+ inserts.forEach(i => (0, _lodash.default)(value, i, response.headers.get(i)));
117
+ return feed.send(value);
97
118
  } catch (e) {
98
119
  onError(e);
99
120
  }
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.20.0",
4
+ "version": "1.22.1",
5
5
  "author": "Nicolas Thouvenin <nthouvenin@gmail.com>",
6
6
  "bugs": "https://github.com/Inist-CNRS/ezs/issues",
7
7
  "dependencies": {
@@ -36,7 +36,7 @@
36
36
  "directories": {
37
37
  "test": "test"
38
38
  },
39
- "gitHead": "4a9a6877830fb4a769c1aae19d798b7985783760",
39
+ "gitHead": "ec76c69ceefbb96d50a208cba0a1b3cc9d40c3d1",
40
40
  "homepage": "https://github.com/Inist-CNRS/ezs/tree/master/packages/basics#readme",
41
41
  "keywords": [
42
42
  "ezs"