@vbarbarosh/node-helpers 3.18.1 → 3.19.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/package.json +1 -1
- package/src/fastdl.js +3 -2
- 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/fastdl.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const format_bytes = require('./format_bytes');
|
|
2
|
+
const format_percents = require('./format_percents');
|
|
2
3
|
const format_seconds = require('./format_seconds');
|
|
3
4
|
const format_thousands = require('./format_thousands');
|
|
4
5
|
const fs = require('fs');
|
|
@@ -42,10 +43,10 @@ async function fastdl({file, read_stream_with_range, concurrency = 60, user_frie
|
|
|
42
43
|
const bb = format_bytes;
|
|
43
44
|
const ss = format_seconds;
|
|
44
45
|
if (p.rate) {
|
|
45
|
-
_(`${bb(p.done)} of ${bb(p.total)} [${(p.
|
|
46
|
+
_(`${bb(p.done)} of ${bb(p.total)} [${format_percents(p.percents)}] at ${bb(p.rate)}/s ETA ${ss(p.eta)} duration=${ss(p.duration)} connections=${connections}`);
|
|
46
47
|
}
|
|
47
48
|
else {
|
|
48
|
-
_(`${bb(p.done)} of ${bb(p.total)} [${(p.
|
|
49
|
+
_(`${bb(p.done)} of ${bb(p.total)} [${format_percents(p.percents)}] at ~ ETA ${ss(p.eta)} duration=${ss(p.duration)} connections=${connections}`);
|
|
49
50
|
}
|
|
50
51
|
}
|
|
51
52
|
|
|
@@ -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
|
{
|