@vbarbarosh/node-helpers 3.18.1 → 3.19.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.18.1",
5
+ "version": "3.19.0",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "files": [
8
8
  "src"
@@ -0,0 +1,19 @@
1
+ const crypto = require('crypto');
2
+ const stream = require('stream');
3
+
4
+ function stream_hash(algorithm = 'md5', options)
5
+ {
6
+ const hash = crypto.createHash(algorithm, options);
7
+ return new stream.Transform({
8
+ transform: function (chunk, encoding, next) {
9
+ hash.update(chunk);
10
+ next();
11
+ },
12
+ flush: function (next) {
13
+ this.push(hash.digest('hex'));
14
+ next();
15
+ },
16
+ });
17
+ }
18
+
19
+ module.exports = stream_hash;
@@ -0,0 +1,19 @@
1
+ const crypto = require('crypto');
2
+ const stream = require('stream');
3
+
4
+ function stream_md5()
5
+ {
6
+ const md5 = crypto.createHash('md5');
7
+ return new stream.Transform({
8
+ transform: function (chunk, encoding, next) {
9
+ md5.update(chunk, encoding);
10
+ next();
11
+ },
12
+ flush: function (next) {
13
+ this.push(md5.digest('hex'));
14
+ next();
15
+ },
16
+ });
17
+ }
18
+
19
+ module.exports = stream_md5;
@@ -1,9 +1,12 @@
1
1
  const ignore = require('./ignore');
2
2
  const stream = require('stream');
3
- const wait_while = require('./wait_while');
4
3
 
5
4
  function stream_multiplex(...streams)
6
5
  {
6
+ if (streams.length === 1) {
7
+ return streams[0];
8
+ }
9
+
7
10
  // `new stream.Writable()` will ignore second, third, etc. calls to `destroy`
8
11
  return stream.Writable({
9
12
  objectMode: true,
@@ -12,38 +15,36 @@ function stream_multiplex(...streams)
12
15
  next();
13
16
  },
14
17
  destroy: async function (error, next) {
15
- let running = streams.length;
16
- streams.forEach(s => s.destroy(error, () => running--));
17
- await wait_while(() => running > 0);
18
- next();
18
+ let done = 0;
19
+ streams.forEach(function (stream) {
20
+ stream.destroy(error, function () {
21
+ if (++done === streams.length) {
22
+ next();
23
+ }
24
+ });
25
+ });
19
26
  },
20
- write: async function (chunk, enc, next) {
21
- let running = streams.length;
27
+ write: async function (chunk, encoding, next) {
28
+ let done = 0;
22
29
  streams.forEach(function (stream) {
23
- stream.write(chunk, function (error) {
24
- running--;
25
- if (error) {
30
+ stream.write(chunk, encoding, function (error) {
31
+ if (++done === streams.length || error) {
26
32
  next(error);
27
33
  next = ignore;
28
34
  }
29
35
  });
30
36
  });
31
- await wait_while(() => running > 0);
32
- next();
33
37
  },
34
38
  final: async function (next) {
35
- let running = streams.length;
39
+ let done = 0;
36
40
  streams.forEach(function (stream) {
37
41
  stream.end(function (error) {
38
- running--;
39
- if (error) {
42
+ if (++done === streams.length || error) {
40
43
  next(error);
41
44
  next = ignore;
42
45
  }
43
46
  });
44
47
  });
45
- await wait_while(() => running > 0);
46
- next();
47
48
  },
48
49
  });
49
50
  }
package/src/wait_while.js CHANGED
@@ -2,6 +2,10 @@ const Promise = require('bluebird');
2
2
 
3
3
  /**
4
4
  * Wait until `fn` returns `true`.
5
+ *
6
+ * W A R N I N G
7
+ * This method was designed mainly for prototyping (where code cleanliness is important).
8
+ * It might drastically decrease performance.
5
9
  */
6
10
  function wait_while(fn)
7
11
  {