@ezs/basics 2.5.0 → 2.5.2

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/lib/tar-dump.js CHANGED
@@ -4,16 +4,12 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = TARDump;
7
-
8
7
  var _tarStream = _interopRequireDefault(require("tar-stream"));
9
-
10
8
  var _zlib = require("zlib");
11
-
12
9
  var _lodash = _interopRequireDefault(require("lodash.merge"));
13
-
14
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
11
  const eol = '\n';
12
+
17
13
  /**
18
14
  * Take all recevied objects and build a tar file
19
15
  *
@@ -29,7 +25,6 @@ const eol = '\n';
29
25
  * @param {String} [extension=json] Choose extension fo each file
30
26
  * @param {Boolean} [compress=false] Enable gzip compression
31
27
  */
32
-
33
28
  function TARDump(data, feed) {
34
29
  if (!this.pack) {
35
30
  this.pack = _tarStream.default.pack();
@@ -38,7 +33,6 @@ function TARDump(data, feed) {
38
33
  const stream = compress ? this.pack.pipe((0, _zlib.createGzip)()) : this.pack;
39
34
  feed.flow(stream).finally(() => feed.close());
40
35
  }
41
-
42
36
  if (this.isLast()) {
43
37
  const updated = new Date();
44
38
  const metadata = {
@@ -55,7 +49,6 @@ function TARDump(data, feed) {
55
49
  this.pack.finalize();
56
50
  return;
57
51
  }
58
-
59
52
  const json = this.getParam('json', true);
60
53
  const extension = this.getParam('extension', 'json');
61
54
  const id = this.getIndex().toString().padStart(10, '0');
@@ -4,19 +4,12 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = TARExtract;
7
-
8
7
  var _tarStream = _interopRequireDefault(require("tar-stream"));
9
-
10
8
  var _micromatch = _interopRequireDefault(require("micromatch"));
11
-
12
9
  var _zlib = require("zlib");
13
-
14
10
  var _getStream = _interopRequireDefault(require("get-stream"));
15
-
16
11
  var _streamWrite = _interopRequireDefault(require("stream-write"));
17
-
18
12
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19
-
20
13
  /**
21
14
  * Take the content of a tar file, extract some files.
22
15
  * The JSON object is sent to the output stream for each file.
@@ -37,7 +30,6 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
37
30
  */
38
31
  function TARExtract(data, feed) {
39
32
  const filesPatern = this.getParam('path', '**/*.json');
40
-
41
33
  if (this.isFirst()) {
42
34
  const {
43
35
  ezs
@@ -46,40 +38,32 @@ function TARExtract(data, feed) {
46
38
  const compress = this.getParam('compress', false);
47
39
  this.input = ezs.createStream(ezs.objectMode());
48
40
  this.output = ezs.createStream(ezs.objectMode());
49
-
50
41
  const extract = _tarStream.default.extract();
51
-
52
42
  this.whenEnd = new Promise((resolve, reject) => {
53
43
  extract.on('entry', async (header, stream, next) => {
54
44
  if (_micromatch.default.isMatch(header.name, filesPatern)) {
55
45
  const contentRaw = await (0, _getStream.default)(stream);
56
-
57
46
  if (json) {
58
47
  const contentJson = JSON.parse(contentRaw);
59
48
  return (0, _streamWrite.default)(this.output, contentJson, () => next());
60
49
  }
61
-
62
50
  return (0, _streamWrite.default)(this.output, {
63
51
  id: header.name,
64
52
  value: contentRaw
65
53
  }, () => next());
66
54
  }
67
-
68
55
  return next();
69
56
  });
70
57
  extract.on('error', reject);
71
58
  extract.on('finish', resolve);
72
59
  });
73
-
74
60
  if (compress) {
75
61
  this.input.pipe((0, _zlib.createGunzip)()).pipe(extract);
76
62
  } else {
77
63
  this.input.pipe(extract);
78
64
  }
79
-
80
65
  this.whenFinish = feed.flow(this.output);
81
66
  }
82
-
83
67
  if (this.isLast()) {
84
68
  this.whenEnd.finally(() => this.output.end());
85
69
  this.whenFinish.finally(() => feed.close());
package/lib/txt-concat.js CHANGED
@@ -4,18 +4,14 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _string_decoder = require("string_decoder");
9
-
10
8
  function TXTConcat(data, feed) {
11
9
  if (!this.decoder) {
12
10
  this.decoder = new _string_decoder.StringDecoder('utf8');
13
11
  }
14
-
15
12
  if (this.buffer === undefined) {
16
13
  this.buffer = '';
17
14
  }
18
-
19
15
  if (this.isLast()) {
20
16
  this.decoder.end();
21
17
  feed.send(this.buffer);
@@ -25,6 +21,7 @@ function TXTConcat(data, feed) {
25
21
  feed.end();
26
22
  }
27
23
  }
24
+
28
25
  /**
29
26
  * Concatenate all `String` items into one string
30
27
  *
@@ -45,8 +42,6 @@ function TXTConcat(data, feed) {
45
42
  * @param {undefined} none
46
43
  * @returns {String}
47
44
  */
48
-
49
-
50
45
  var _default = {
51
46
  TXTConcat
52
47
  };
@@ -4,30 +4,26 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _lodash = _interopRequireDefault(require("lodash.get"));
9
-
10
8
  var _inflection = require("inflection");
11
-
12
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
13
-
14
10
  const transformer = transformations => str => str && typeof str === 'string' ? (0, _inflection.transform)(str, transformations) : str;
15
-
16
11
  const TXTInflection = (data, feed, ctx) => {
17
12
  if (ctx.isLast()) {
18
13
  return feed.close();
19
14
  }
20
-
21
15
  const transformations = [].concat(ctx.getParam('transform', [])).filter(Boolean);
22
16
  const path = ctx.getParam('path', 'value');
23
17
  const value = (0, _lodash.default)(data, path, '');
24
18
  const process = transformer(transformations);
25
19
  const result = Array.isArray(value) ? value.map(item => process(item)) : process(value);
26
- feed.write({ ...data,
20
+ feed.write({
21
+ ...data,
27
22
  [path]: result
28
23
  });
29
24
  return feed.end();
30
25
  };
26
+
31
27
  /**
32
28
  * Take a `String` and inflect it with or more transformers from this list
33
29
  * pluralize, singularize, camelize, underscore, humanize, capitalize,
@@ -60,8 +56,6 @@ const TXTInflection = (data, feed, ctx) => {
60
56
  * see https://inist-cnrs.github.io/ezs/#/plugin-strings?id=inflection
61
57
  * see https://www.npmjs.com/package/inflection
62
58
  */
63
-
64
-
65
59
  var _default = {
66
60
  TXTInflection
67
61
  };
package/lib/txt-object.js CHANGED
@@ -4,22 +4,19 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  function TXTObject(data, feed) {
9
8
  if (this.isLast()) {
10
9
  return feed.send(data);
11
10
  }
12
-
13
11
  const key = this.getParam('key', 'value');
14
-
15
12
  if (typeof key !== 'string') {
16
13
  throw new Error('Invalid parameter: key is not a string');
17
14
  }
18
-
19
15
  const obj = {};
20
16
  obj[key] = data;
21
17
  feed.send(obj);
22
18
  }
19
+
23
20
  /**
24
21
  * Take an array of values and generate an array containing objects with the
25
22
  * given `key` and matching value from the input array.
@@ -40,8 +37,6 @@ function TXTObject(data, feed) {
40
37
  * @param {String} [key="value"] choose a the key name
41
38
  * @returns {Object}
42
39
  */
43
-
44
-
45
40
  var _default = {
46
41
  TXTObject
47
42
  };
package/lib/txt-parse.js CHANGED
@@ -4,31 +4,24 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _string_decoder = require("string_decoder");
9
-
10
8
  function TXTParse(data, feed) {
11
9
  if (!this.decoder) {
12
10
  this.decoder = new _string_decoder.StringDecoder('utf8');
13
11
  }
14
-
15
12
  if (this.isLast()) {
16
13
  this.decoder.end();
17
14
  return feed.end();
18
15
  }
19
-
20
16
  this.remainder = this.remainder || '';
21
17
  let separator;
22
-
23
18
  try {
24
19
  const val = '"'.concat(this.getParam('separator', '\n')).concat('"');
25
20
  separator = JSON.parse(val);
26
21
  } catch (e) {
27
22
  separator = '\n';
28
23
  }
29
-
30
24
  let lines;
31
-
32
25
  if (Buffer.isBuffer(data)) {
33
26
  lines = this.decoder.write(data).split(separator);
34
27
  } else if (typeof data === 'string') {
@@ -36,7 +29,6 @@ function TXTParse(data, feed) {
36
29
  } else {
37
30
  lines = ['', ''];
38
31
  }
39
-
40
32
  lines.unshift(this.remainder + lines.shift());
41
33
  this.remainder = lines.pop();
42
34
  lines.forEach(line => {
@@ -44,6 +36,7 @@ function TXTParse(data, feed) {
44
36
  });
45
37
  feed.end();
46
38
  }
39
+
47
40
  /**
48
41
  * Take a `String` and split it at each separator found.
49
42
  *
@@ -65,8 +58,6 @@ function TXTParse(data, feed) {
65
58
  * @param {String} [separator="\n"] choose character which trigger the split
66
59
  * @returns {String}
67
60
  */
68
-
69
-
70
61
  var _default = {
71
62
  TXTParse
72
63
  };
@@ -4,20 +4,17 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _lodash = _interopRequireDefault(require("lodash.get"));
9
-
10
8
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
11
-
12
9
  const UPPER_LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
13
10
  const SENTENCE_INIT = ' ';
14
11
  const SENTENCE_ENDING = '.?!';
12
+
15
13
  /*
16
14
  * Segment sentences from `str` into an array
17
15
  * @param {string} str
18
16
  * @returns {string[]}
19
17
  */
20
-
21
18
  const segmentSentences = str => {
22
19
  const characters = Array.from(str);
23
20
  const sentences = characters.reduce(
@@ -29,40 +26,36 @@ const segmentSentences = str => {
29
26
  (prevSentences, character) => {
30
27
  const currentSentence = prevSentences.slice(-1)[0];
31
28
  const [char1, char2] = currentSentence.slice(-2);
32
-
33
29
  if (SENTENCE_ENDING.includes(character)) {
34
30
  if (character !== '.') {
35
31
  return [...prevSentences.slice(0, -1), currentSentence + character, SENTENCE_INIT];
36
32
  }
37
-
38
33
  if (char1 !== ' ') {
39
34
  return [...prevSentences.slice(0, -1), currentSentence + character, SENTENCE_INIT];
40
35
  }
41
-
42
36
  if (!UPPER_LETTERS.includes(char2)) {
43
37
  return [...prevSentences.slice(0, -1), currentSentence + character, SENTENCE_INIT];
44
38
  }
45
39
  }
46
-
47
40
  return [...prevSentences.slice(0, -1), currentSentence + character];
48
41
  }, [SENTENCE_INIT]).filter(sentence => sentence !== SENTENCE_INIT).map(sentence => sentence.trimStart());
49
42
  return sentences;
50
43
  };
51
-
52
44
  const TXTSentences = (data, feed, ctx) => {
53
45
  if (ctx.isLast()) {
54
46
  return feed.close();
55
47
  }
56
-
57
48
  const path = ctx.getParam('path', 'value');
58
49
  const value = (0, _lodash.default)(data, path);
59
50
  const str = Array.isArray(value) ? value.map(item => typeof item === 'string' ? item : '').join(' ') : value;
60
51
  const sentences = str ? segmentSentences(str) : [];
61
- feed.write({ ...data,
52
+ feed.write({
53
+ ...data,
62
54
  [path]: sentences
63
55
  });
64
56
  return feed.end();
65
57
  };
58
+
66
59
  /**
67
60
  * Take a `String` and split it into an array of sentences.
68
61
  *
@@ -84,8 +77,6 @@ const TXTSentences = (data, feed, ctx) => {
84
77
  * @deprecated
85
78
  * see https://inist-cnrs.github.io/ezs/#/plugin-strings?id=sentences
86
79
  */
87
-
88
-
89
80
  var _default = {
90
81
  TXTSentences
91
82
  };
package/lib/txt-zip.js CHANGED
@@ -4,15 +4,10 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = TXTZip;
7
-
8
7
  var _zlib = _interopRequireDefault(require("zlib"));
9
-
10
8
  var _stream = require("stream");
11
-
12
9
  var _streamWrite = _interopRequireDefault(require("stream-write"));
13
-
14
10
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
11
  /**
17
12
  * Take a `String` and zip it.
18
13
  *
@@ -25,17 +20,14 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
25
20
  */
26
21
  function TXTZip(data, feed) {
27
22
  const unzip = this.getParam('unzip', false);
28
-
29
23
  if (this.isFirst()) {
30
24
  this.input = new _stream.PassThrough();
31
25
  const z = unzip ? _zlib.default.createGunzip() : _zlib.default.createGzip();
32
26
  this.whenFinish = feed.flow(this.input.pipe(z));
33
27
  }
34
-
35
28
  if (this.isLast()) {
36
29
  this.whenFinish.finally(() => feed.close());
37
30
  return this.input.end();
38
31
  }
39
-
40
32
  (0, _streamWrite.default)(this.input, data, () => feed.end());
41
33
  }
@@ -4,27 +4,16 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = URLConnect;
7
-
8
7
  var _JSONStream = _interopRequireDefault(require("JSONStream"));
9
-
10
8
  var _from = _interopRequireDefault(require("from"));
11
-
12
9
  var _debug = _interopRequireDefault(require("debug"));
13
-
14
10
  var _streamWrite = _interopRequireDefault(require("stream-write"));
15
-
16
11
  var _nodeAbortController = _interopRequireDefault(require("node-abort-controller"));
17
-
18
12
  var _parseHeaders = _interopRequireDefault(require("parse-headers"));
19
-
20
13
  var _asyncRetry = _interopRequireDefault(require("async-retry"));
21
-
22
14
  var _getStream = _interopRequireDefault(require("get-stream"));
23
-
24
15
  var _fetchWithProxy = _interopRequireDefault(require("fetch-with-proxy"));
25
-
26
16
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
27
-
28
17
  /**
29
18
  * Take an `Object` and send it to an URL.
30
19
  *
@@ -55,7 +44,6 @@ async function URLConnect(data, feed) {
55
44
  const {
56
45
  ezs
57
46
  } = this;
58
-
59
47
  if (this.isFirst()) {
60
48
  const timeout = Number(this.getParam('timeout')) || 1000;
61
49
  const headers = (0, _parseHeaders.default)([].concat(this.getParam('header')).filter(Boolean).join('\n'));
@@ -65,39 +53,34 @@ async function URLConnect(data, feed) {
65
53
  (0, _streamWrite.default)(this.input, data, () => feed.end());
66
54
  const streamIn = this.input.pipe(ezs(encoder));
67
55
  let bodyIn;
68
-
69
56
  if (retries === 1) {
70
57
  bodyIn = streamIn.pipe(ezs.toBuffer());
71
58
  } else {
72
59
  bodyIn = await (0, _getStream.default)(streamIn);
73
60
  headers['Content-Type'] = 'application/json';
74
61
  }
75
-
76
62
  const parameters = {
77
63
  timeout,
78
64
  method: 'POST',
79
65
  body: bodyIn,
80
66
  headers
81
67
  };
82
-
83
68
  try {
84
69
  await (0, _asyncRetry.default)(async (bail, numberOfTimes) => {
85
70
  if (numberOfTimes > 1) {
86
71
  (0, _debug.default)('ezs')(`Attempts to reconnect (${numberOfTimes})`);
87
72
  }
88
-
89
73
  const controller = new _nodeAbortController.default();
90
- const response = await (0, _fetchWithProxy.default)(url, { ...parameters,
74
+ const response = await (0, _fetchWithProxy.default)(url, {
75
+ ...parameters,
91
76
  signal: controller.signal
92
77
  });
93
-
94
78
  if (!response.ok) {
95
79
  const err = new Error(response.statusText);
96
80
  const text = await response.text();
97
81
  err.responseText = text;
98
82
  throw err;
99
83
  }
100
-
101
84
  if (retries === 1) {
102
85
  const bodyOut = json ? response.body.pipe(_JSONStream.default.parse('*')) : response.body;
103
86
  bodyOut.once('error', e => {
@@ -124,14 +107,11 @@ async function URLConnect(data, feed) {
124
107
  } else {
125
108
  (0, _debug.default)('ezs')(`Ignore item #${this.getIndex()} [URLConnect] <${e}>`);
126
109
  }
127
-
128
110
  output.end();
129
111
  }
130
-
131
112
  ;
132
113
  return;
133
114
  }
134
-
135
115
  if (this.isLast()) {
136
116
  this.input.end();
137
117
  this.whenFinish.finally(() => {
@@ -139,6 +119,5 @@ async function URLConnect(data, feed) {
139
119
  });
140
120
  return;
141
121
  }
142
-
143
122
  (0, _streamWrite.default)(this.input, data, () => feed.end());
144
123
  }
package/lib/url-fetch.js CHANGED
@@ -4,23 +4,14 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = URLFetch;
7
-
8
7
  var _debug = _interopRequireDefault(require("debug"));
9
-
10
8
  var _lodash = _interopRequireDefault(require("lodash.get"));
11
-
12
9
  var _lodash2 = _interopRequireDefault(require("lodash.set"));
13
-
14
10
  var _nodeAbortController = _interopRequireDefault(require("node-abort-controller"));
15
-
16
11
  var _parseHeaders = _interopRequireDefault(require("parse-headers"));
17
-
18
12
  var _asyncRetry = _interopRequireDefault(require("async-retry"));
19
-
20
13
  var _request = _interopRequireDefault(require("./request"));
21
-
22
14
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
-
24
15
  /**
25
16
  * Add a new field to an `Object`, with the returned content of URL.
26
17
  *
@@ -41,7 +32,6 @@ async function URLFetch(data, feed) {
41
32
  if (this.isLast()) {
42
33
  return feed.close();
43
34
  }
44
-
45
35
  const url = this.getParam('url');
46
36
  const path = this.getParam('path');
47
37
  const target = [].concat(this.getParam('target')).filter(Boolean).shift();
@@ -62,36 +52,31 @@ async function URLFetch(data, feed) {
62
52
  const options = {
63
53
  retries
64
54
  };
65
-
66
55
  if (body) {
67
56
  (0, _lodash2.default)(parameters, 'method', 'POST');
68
57
  (0, _lodash2.default)(parameters, 'body', Buffer.isBuffer(body) ? body : JSON.stringify(body));
69
58
  (0, _lodash2.default)(parameters, 'headers.content-type', mimetype);
70
59
  }
71
-
72
60
  try {
73
61
  const response = await (0, _asyncRetry.default)((0, _request.default)(url, parameters), options);
74
62
  const func = json ? 'json' : 'text';
75
63
  const value = await response[func]();
76
-
77
64
  if (target) {
78
- const result = typeof data === 'object' ? { ...data
65
+ const result = typeof data === 'object' ? {
66
+ ...data
79
67
  } : {
80
68
  input: data
81
69
  };
82
70
  (0, _lodash2.default)(result, target, value);
83
71
  return feed.send(result);
84
72
  }
85
-
86
73
  return feed.send(value);
87
74
  } catch (e) {
88
75
  controller.abort();
89
-
90
76
  if (noerror) {
91
77
  (0, _debug.default)('ezs')(`Ignore item #${this.getIndex()} [URLFetch] <${e}>`);
92
78
  return feed.send(data);
93
79
  }
94
-
95
80
  (0, _debug.default)('ezs')(`Break item #${this.getIndex()} [URLFetch] <${e}>`);
96
81
  return feed.send(e);
97
82
  }
@@ -4,7 +4,6 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = URLPagination;
7
-
8
7
  /**
9
8
  * Take `Object` and multiple it to make it one object per page
10
9
  *
@@ -70,27 +69,22 @@ function URLPagination(data, feed) {
70
69
  if (this.isLast()) {
71
70
  return feed.close();
72
71
  }
73
-
74
72
  const total = Number(this.getParam('total', 0));
75
73
  const limit = Number(this.getParam('limit', 10));
76
74
  const maxPages = Number(this.getParam('maxPages', 1000));
77
-
78
75
  if (total === 0) {
79
76
  return feed.send(new Error('No result.'));
80
77
  }
81
-
82
78
  if (Number.isNaN(total)) {
83
79
  return feed.send(new Error('Unexpected response.'));
84
80
  }
85
-
86
81
  let totalPages = Math.ceil(total / limit);
87
-
88
82
  if (totalPages > maxPages) {
89
83
  totalPages = maxPages;
90
84
  }
91
-
92
85
  for (let pageNumber = 1; pageNumber <= totalPages; pageNumber += 1) {
93
- feed.write({ ...data,
86
+ feed.write({
87
+ ...data,
94
88
  offset: (pageNumber - 1) * limit,
95
89
  pageNumber,
96
90
  totalPages,
@@ -98,6 +92,5 @@ function URLPagination(data, feed) {
98
92
  limit
99
93
  });
100
94
  }
101
-
102
95
  feed.end();
103
96
  }
package/lib/url-parse.js CHANGED
@@ -4,14 +4,11 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  var _url = require("url");
9
-
10
8
  function URLParse(data, feed) {
11
9
  if (this.isLast()) {
12
10
  return feed.close();
13
11
  }
14
-
15
12
  const u = new _url.URL(data);
16
13
  return feed.send({
17
14
  href: u.href,
@@ -27,6 +24,7 @@ function URLParse(data, feed) {
27
24
  hash: u.hash
28
25
  });
29
26
  }
27
+
30
28
  /**
31
29
  * Take an URL `String`, parse it and return `Object`.
32
30
  *
@@ -54,8 +52,6 @@ function URLParse(data, feed) {
54
52
  * @name URLParse
55
53
  * @returns {Object}
56
54
  */
57
-
58
-
59
55
  var _default = {
60
56
  URLParse
61
57
  };