@vbarbarosh/node-helpers 3.30.1 → 3.31.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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "Vladimir Barbarosh <vladimir.barbarosh@gmail.com>",
3
3
  "license": "MIT",
4
4
  "name": "@vbarbarosh/node-helpers",
5
- "version": "3.30.1",
5
+ "version": "3.31.0",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "files": [
8
8
  "src"
@@ -3,8 +3,8 @@ const stream = require('stream');
3
3
  function stream_discard()
4
4
  {
5
5
  return new stream.Writable({
6
- write: function (buf, encoding, next) {
7
- next();
6
+ write: function (buffer, encoding, callback) {
7
+ callback();
8
8
  },
9
9
  });
10
10
  }
@@ -0,0 +1,17 @@
1
+ const stream = require('stream');
2
+
3
+ /**
4
+ * Pass each item to a simple function. This is a final step.
5
+ */
6
+ function stream_each(fn)
7
+ {
8
+ return stream.Writable({
9
+ objectMode: true,
10
+ write: function (item, encoding, callback) {
11
+ fn(item);
12
+ callback();
13
+ }
14
+ });
15
+ }
16
+
17
+ module.exports = stream_each;
@@ -0,0 +1,24 @@
1
+ const stream = require('stream');
2
+
3
+ /**
4
+ * Pass down only those items passed user-defined criteria.
5
+ */
6
+ function stream_filter(fn)
7
+ {
8
+ return new stream.Transform({
9
+ objectMode: true,
10
+ transform: function (item, encoding, callback) {
11
+ try {
12
+ if (fn(item)) {
13
+ this.push(item);
14
+ }
15
+ callback();
16
+ }
17
+ catch (error) {
18
+ callback(error);
19
+ }
20
+ },
21
+ });
22
+ }
23
+
24
+ module.exports = stream_filter;
@@ -2,24 +2,26 @@ const stream = require('stream');
2
2
 
3
3
  /**
4
4
  * Returns a transform stream used to group object into chunks of objects.
5
+ *
6
+ * TODO Rename to stream_chunk
5
7
  */
6
8
  function stream_group(chunk_size)
7
9
  {
8
10
  const chunk = [];
9
11
  return new stream.Transform({
10
12
  objectMode: true,
11
- transform: function (item, encoding, next) {
13
+ transform: function (item, encoding, callback) {
12
14
  chunk.push(item);
13
15
  if (chunk.length >= chunk_size) {
14
16
  this.push(chunk.splice(0));
15
17
  }
16
- next();
18
+ callback();
17
19
  },
18
- flush: function (next) {
20
+ flush: function (callback) {
19
21
  if (chunk.length) {
20
22
  this.push(chunk.splice(0));
21
23
  }
22
- next();
24
+ callback();
23
25
  },
24
26
  });
25
27
  }
@@ -5,13 +5,13 @@ function stream_hash(algorithm = 'md5', options)
5
5
  {
6
6
  const hash = crypto.createHash(algorithm, options);
7
7
  return new stream.Transform({
8
- transform: function (chunk, encoding, next) {
8
+ transform: function (chunk, encoding, callback) {
9
9
  hash.update(chunk);
10
- next();
10
+ callback();
11
11
  },
12
- flush: function (next) {
12
+ flush: function (callback) {
13
13
  this.push(hash.digest('hex'));
14
- next();
14
+ callback();
15
15
  },
16
16
  });
17
17
  }
@@ -0,0 +1,22 @@
1
+ const stream = require('stream');
2
+
3
+ /**
4
+ * Transform each item by simple function.
5
+ */
6
+ function stream_map(fn)
7
+ {
8
+ return new stream.Transform({
9
+ objectMode: true,
10
+ transform: function (item, encoding, callback) {
11
+ try {
12
+ this.push(fn(item));
13
+ callback();
14
+ }
15
+ catch (error) {
16
+ callback(error);
17
+ }
18
+ },
19
+ });
20
+ }
21
+
22
+ module.exports = stream_map;
package/src/stream_md5.js CHANGED
@@ -1,17 +1,20 @@
1
1
  const crypto = require('crypto');
2
2
  const stream = require('stream');
3
3
 
4
+ /**
5
+ * TODO Rename to stream_hash_md5
6
+ */
4
7
  function stream_md5()
5
8
  {
6
9
  const md5 = crypto.createHash('md5');
7
10
  return new stream.Transform({
8
- transform: function (chunk, encoding, next) {
11
+ transform: function (chunk, encoding, callback) {
9
12
  md5.update(chunk, encoding);
10
- next();
13
+ callback();
11
14
  },
12
- flush: function (next) {
15
+ flush: function (callback) {
13
16
  this.push(md5.digest('hex'));
14
- next();
17
+ callback();
15
18
  },
16
19
  });
17
20
  }
@@ -10,38 +10,38 @@ function stream_multiplex(...streams)
10
10
  // `new stream.Writable()` will ignore second, third, etc. calls to `destroy`
11
11
  return stream.Writable({
12
12
  objectMode: true,
13
- construct: function (next) {
13
+ construct: function (callback) {
14
14
  streams.forEach(s => s.once('error', e => this.destroy(e)));
15
- next();
15
+ callback();
16
16
  },
17
- destroy: async function (error, next) {
17
+ destroy: async function (error, callback) {
18
18
  let done = 0;
19
19
  streams.forEach(function (stream) {
20
20
  stream.destroy(error, function () {
21
21
  if (++done === streams.length) {
22
- next();
22
+ callback();
23
23
  }
24
24
  });
25
25
  });
26
26
  },
27
- write: async function (chunk, encoding, next) {
27
+ write: async function (chunk, encoding, callback) {
28
28
  let done = 0;
29
29
  streams.forEach(function (stream) {
30
30
  stream.write(chunk, encoding, function (error) {
31
31
  if (++done === streams.length || error) {
32
- next(error);
33
- next = ignore;
32
+ callback(error);
33
+ callback = ignore;
34
34
  }
35
35
  });
36
36
  });
37
37
  },
38
- final: async function (next) {
38
+ final: async function (callback) {
39
39
  let done = 0;
40
40
  streams.forEach(function (stream) {
41
41
  stream.end(function (error) {
42
42
  if (++done === streams.length || error) {
43
- next(error);
44
- next = ignore;
43
+ callback(error);
44
+ callback = ignore;
45
45
  }
46
46
  });
47
47
  });
@@ -47,13 +47,13 @@ function stream_parse_xml(selector, mapper = stream_parse_xml.guess)
47
47
  const parser = new htmlparser2.Parser(events, options);
48
48
  return out = new stream.Transform({
49
49
  objectMode: true,
50
- transform: function (buf, encoding, next) {
50
+ transform: function (buf, encoding, callback) {
51
51
  parser.write(buf.toString());
52
- next();
52
+ callback();
53
53
  },
54
- flush: function (next) {
54
+ flush: function (callback) {
55
55
  parser.end();
56
- next();
56
+ callback();
57
57
  },
58
58
  });
59
59
  }
@@ -7,21 +7,28 @@ const stream = require('stream');
7
7
  *
8
8
  * @similar https://www.npmjs.com/package/progress-stream
9
9
  */
10
- function stream_progress({total, user_friendly_status = s => console.log(s)} = {})
10
+ function stream_progress({objectMode = false, total, user_friendly_status = s => console.log(s)} = {})
11
11
  {
12
12
  let done = 0;
13
13
  const timer = setInterval(tick, 1000);
14
14
  const progress = make_progress(total);
15
15
  return new stream.Transform({
16
- destroy: function (error, next) {
16
+ objectMode,
17
+ destroy: function (error, callback) {
17
18
  tick();
18
19
  clearInterval(timer);
19
- next();
20
+ callback();
20
21
  },
21
- transform: function (buf, encoding, next) {
22
- done += buf.length;
23
- this.push(buf, encoding);
24
- next();
22
+ transform: function (buffer, encoding, callback) {
23
+ if (objectMode) {
24
+ done++;
25
+ this.push(buffer);
26
+ }
27
+ else {
28
+ done += buffer.length;
29
+ this.push(buffer, encoding);
30
+ }
31
+ callback();
25
32
  },
26
33
  });
27
34
  function tick() {
@@ -40,6 +40,7 @@ const {finished} = require('stream/promises');
40
40
  *
41
41
  * @param stream
42
42
  * @returns {Promise<void>}
43
+ * @deprecated Deprecated in favor of `stream.promises.finished`. Should be removed in the next major release.
43
44
  */
44
45
  function stream_promise(stream)
45
46
  {
@@ -8,7 +8,7 @@ function stream_strpbrk(chars = '\r\n')
8
8
  const pending = [];
9
9
  return new stream.Transform({
10
10
  objectMode: true,
11
- transform: function (str, encoding, next) {
11
+ transform: function (str, encoding, callback) {
12
12
  str = Buffer.isBuffer(str) ? str.toString() : str;
13
13
 
14
14
  let off = 0;
@@ -55,7 +55,7 @@ function stream_strpbrk(chars = '\r\n')
55
55
  if (tmp.length) {
56
56
  pending.push(tmp);
57
57
  }
58
- next();
58
+ callback();
59
59
  return;
60
60
  }
61
61
 
@@ -64,13 +64,13 @@ function stream_strpbrk(chars = '\r\n')
64
64
  this.push(''.concat(...pending.splice(0)));
65
65
  off = delims[0];
66
66
  }
67
- next();
67
+ callback();
68
68
  },
69
- flush: function (next) {
69
+ flush: function (callback) {
70
70
  if (pending.length) {
71
71
  this.push(''.concat(...pending.splice(0)));
72
72
  }
73
- next();
73
+ callback();
74
74
  },
75
75
  });
76
76
  }
@@ -7,14 +7,14 @@ function stream_through(fn)
7
7
  {
8
8
  return new stream.Transform({
9
9
  objectMode: true,
10
- transform: function (item, encoding, next) {
10
+ transform: function (item, encoding, callback) {
11
11
  try {
12
12
  fn(item);
13
13
  this.push(item, encoding);
14
- next();
14
+ callback();
15
15
  }
16
16
  catch (error) {
17
- next(error);
17
+ callback(error);
18
18
  }
19
19
  },
20
20
  });
@@ -1,5 +1,8 @@
1
1
  const stream = require('stream');
2
2
 
3
+ /**
4
+ * @deprecated Deprecated in favor of stream_map
5
+ */
3
6
  function stream_transform(fn)
4
7
  {
5
8
  return new stream.Transform({