@vbarbarosh/node-helpers 3.19.1 → 3.20.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/fastdl.js +5 -15
- package/src/format_progress.js +13 -0
- package/src/stream_progress.js +2 -4
package/package.json
CHANGED
package/src/fastdl.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
const format_bytes = require('./format_bytes');
|
|
2
|
-
const
|
|
3
|
-
const format_seconds = require('./format_seconds');
|
|
2
|
+
const format_progress = require('./format_progress');
|
|
4
3
|
const format_thousands = require('./format_thousands');
|
|
5
4
|
const fs = require('fs');
|
|
6
5
|
const fs_path_basename = require('./fs_path_basename');
|
|
@@ -18,15 +17,14 @@ const stream = require('stream');
|
|
|
18
17
|
*/
|
|
19
18
|
async function fastdl({file, read_stream_with_range, concurrency = 60, user_friendly_status = v => console.log(v)})
|
|
20
19
|
{
|
|
21
|
-
const _ = user_friendly_status;
|
|
22
20
|
const M = 1024*1024;
|
|
23
21
|
const chunk_min_bytes = M;
|
|
24
22
|
const chunk_max_bytes = 50*M;
|
|
25
23
|
|
|
26
|
-
|
|
24
|
+
user_friendly_status(`Truncating destination file [${fs_path_basename(file)}]...`);
|
|
27
25
|
await fs.promises.writeFile(file, '');
|
|
28
26
|
|
|
29
|
-
|
|
27
|
+
user_friendly_status('Requesting first chunk to determine total size...');
|
|
30
28
|
const rs0 = await read_stream_with_range(0, chunk_min_bytes);
|
|
31
29
|
|
|
32
30
|
const total = rs0.content_range.total;
|
|
@@ -39,18 +37,10 @@ async function fastdl({file, read_stream_with_range, concurrency = 60, user_frie
|
|
|
39
37
|
|
|
40
38
|
const timer = setInterval(tick, 1000);
|
|
41
39
|
function tick() {
|
|
42
|
-
|
|
43
|
-
const bb = format_bytes;
|
|
44
|
-
const ss = format_seconds;
|
|
45
|
-
if (p.rate) {
|
|
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}`);
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
_(`${bb(p.done)} of ${bb(p.total)} [${format_percents(p.percents)}] at ~ ETA ${ss(p.eta)} duration=${ss(p.duration)} connections=${connections}`);
|
|
50
|
-
}
|
|
40
|
+
user_friendly_status(`${format_progress(progress)} connections=${connections}`);
|
|
51
41
|
}
|
|
52
42
|
|
|
53
|
-
|
|
43
|
+
user_friendly_status(`${format_bytes(total)} [${format_thousands(total)} bytes] to download`);
|
|
54
44
|
|
|
55
45
|
try {
|
|
56
46
|
await parallel({concurrency, spawn});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const format_bytes = require('./format_bytes');
|
|
2
|
+
const format_percents = require('./format_percents');
|
|
3
|
+
const format_seconds = require('./format_seconds');
|
|
4
|
+
|
|
5
|
+
function format_progress({percents, total, done, rate, eta, duration})
|
|
6
|
+
{
|
|
7
|
+
if (total) {
|
|
8
|
+
return `${format_percents(percents)} | ${format_bytes(done)} of ${format_bytes(total)} at ${format_bytes(rate)}/s ETA ${format_seconds(eta)} duration=${format_seconds(duration)}`;
|
|
9
|
+
}
|
|
10
|
+
return `${format_bytes(done)} of ~ at ${format_bytes(rate)}/s duration=${format_seconds(duration)}`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = format_progress;
|
package/src/stream_progress.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
const
|
|
2
|
-
const format_percents = require('./format_percents');
|
|
3
|
-
const format_seconds = require('./format_seconds');
|
|
1
|
+
const format_progress = require('./format_progress');
|
|
4
2
|
const make_progress = require('./make_progress');
|
|
5
3
|
const stream = require('stream');
|
|
6
4
|
|
|
@@ -26,7 +24,7 @@ function stream_progress({total, user_friendly_status = s => console.log(s)})
|
|
|
26
24
|
});
|
|
27
25
|
function tick() {
|
|
28
26
|
progress.update(done);
|
|
29
|
-
user_friendly_status(
|
|
27
|
+
user_friendly_status(format_progress(progress));
|
|
30
28
|
}
|
|
31
29
|
}
|
|
32
30
|
|