@vbarbarosh/node-helpers 3.18.0 → 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 +1 -1
- package/src/make_progress.js +9 -12
- package/src/stream_hash.js +19 -0
- package/src/stream_md5.js +19 -0
- package/src/stream_multiplex.js +18 -17
- package/src/wait_while.js +4 -0
package/package.json
CHANGED
package/src/make_progress.js
CHANGED
|
@@ -21,10 +21,9 @@ function make_progress(total)
|
|
|
21
21
|
progress: null,
|
|
22
22
|
percents: null,
|
|
23
23
|
add: function (delta = 0) {
|
|
24
|
-
const
|
|
24
|
+
const now_minus_10sec = Date.now() - 10000;
|
|
25
|
+
history.splice(0, history.findLastIndex(v => v.time < now_minus_10sec));
|
|
25
26
|
history.push({time: Date.now(), delta});
|
|
26
|
-
// Keep last 5 seconds
|
|
27
|
-
history.splice(0, history.findLastIndex(v => v.time < now_minus_5sec));
|
|
28
27
|
out.done += delta;
|
|
29
28
|
out.refresh();
|
|
30
29
|
},
|
|
@@ -32,19 +31,17 @@ function make_progress(total)
|
|
|
32
31
|
out.add(done - out.done);
|
|
33
32
|
},
|
|
34
33
|
refresh: function () {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
34
|
+
out.duration = (Date.now() - time0)/1000;
|
|
35
|
+
out.percents = !out.total ? null : out.done/out.total;
|
|
36
|
+
out.eta = !out.total ? null : (out.total - out.done)/out.rate;
|
|
37
|
+
if (history.length) {
|
|
38
|
+
const delta = history.reduce((a,v) => a + v.delta, 0);
|
|
39
|
+
const time_sec = (Date.now() - history[0].time)/1000;
|
|
40
|
+
out.rate = delta/time_sec;
|
|
41
41
|
}
|
|
42
42
|
else {
|
|
43
43
|
out.rate = out.done/out.duration;
|
|
44
44
|
}
|
|
45
|
-
out.duration = (Date.now() - time0)/1000;
|
|
46
|
-
out.percents = !out.total ? null : out.done/out.total;
|
|
47
|
-
out.eta = !out.total ? null : (out.total - out.done)/out.rate;
|
|
48
45
|
},
|
|
49
46
|
};
|
|
50
47
|
return out;
|
|
@@ -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;
|
package/src/stream_multiplex.js
CHANGED
|
@@ -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
|
|
16
|
-
streams.forEach(
|
|
17
|
-
|
|
18
|
-
|
|
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,
|
|
21
|
-
let
|
|
27
|
+
write: async function (chunk, encoding, next) {
|
|
28
|
+
let done = 0;
|
|
22
29
|
streams.forEach(function (stream) {
|
|
23
|
-
stream.write(chunk, function (error) {
|
|
24
|
-
|
|
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
|
|
39
|
+
let done = 0;
|
|
36
40
|
streams.forEach(function (stream) {
|
|
37
41
|
stream.end(function (error) {
|
|
38
|
-
|
|
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
|
{
|