@vbarbarosh/node-helpers 3.7.0 → 3.8.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.7.0",
5
+ "version": "3.8.0",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "files": [
8
8
  "src"
package/src/fastdl.js CHANGED
@@ -2,6 +2,7 @@ const format_bytes = require('./format_bytes');
2
2
  const format_seconds = require('./format_seconds');
3
3
  const format_thousands = require('./format_thousands');
4
4
  const fs = require('fs');
5
+ const fs_path_basename = require('./fs_path_basename');
5
6
  const make_progress = require('./progress');
6
7
  const parallel = require('./parallel');
7
8
  const stream = require('stream');
@@ -14,16 +15,17 @@ const stream = require('stream');
14
15
  * read_stream_with_range: (first, last) => http_get_stream_range(url, first, last),
15
16
  * });
16
17
  */
17
- async function fastdl({file, read_stream_with_range, concurrency = 60, log = v => console.log(v)})
18
+ async function fastdl({file, read_stream_with_range, concurrency = 60, user_friendly_status = v => console.log(v)})
18
19
  {
20
+ const _ = user_friendly_status;
19
21
  const M = 1024*1024;
20
22
  const chunk_min_bytes = M;
21
23
  const chunk_max_bytes = 50*M;
22
24
 
23
- log(`Truncating destination file [${file}]...`);
25
+ _(`Truncating destination file [${fs_path_basename(file)}]...`);
24
26
  await fs.promises.writeFile(file, '');
25
27
 
26
- log('Requesting first chunk to determine total size...');
28
+ _('Requesting first chunk to determine total size...');
27
29
  const rs0 = await read_stream_with_range(0, chunk_min_bytes);
28
30
 
29
31
  const total = rs0.content_range.total;
@@ -39,10 +41,15 @@ async function fastdl({file, read_stream_with_range, concurrency = 60, log = v =
39
41
  const p = progress;
40
42
  const bb = format_bytes;
41
43
  const ss = format_seconds;
42
- log(`${bb(p.done)} of ${bb(p.total)} ${(p.percentage*100).toFixed(2)}% at ${bb(p.rate)}/s ETA ${ss(p.eta)} duration=${ss(p.duration)} connections=${connections}`);
44
+ if (p.rate) {
45
+ _(`${bb(p.done)} of ${bb(p.total)} [${(p.percentage*100).toFixed(2)}%] at ${bb(p.rate)}/s ETA ${ss(p.eta)} duration=${ss(p.duration)} connections=${connections}`);
46
+ }
47
+ else {
48
+ _(`${bb(p.done)} of ${bb(p.total)} [${(p.percentage*100).toFixed(2)}%] at ~ ETA ${ss(p.eta)} duration=${ss(p.duration)} connections=${connections}`);
49
+ }
43
50
  }
44
51
 
45
- log(`${format_bytes(total)} [${format_thousands(total)} bytes] to download`);
52
+ _(`${format_bytes(total)} [${format_thousands(total)} bytes] to download`);
46
53
 
47
54
  try {
48
55
  await parallel({concurrency, spawn});
package/src/progress.js CHANGED
@@ -21,10 +21,10 @@ function progress(total)
21
21
  progress: null,
22
22
  percentage: null,
23
23
  add: function (delta = 0) {
24
- const now_minus_5_seconds = Date.now() - 5000;
24
+ const now_minus_2sec = Date.now() - 2000;
25
25
  history.push({time: Date.now(), delta});
26
- // Keep last 5 seconds
27
- history.splice(0, history.findLastIndex(v => v.time < now_minus_5_seconds));
26
+ // Keep last 2 seconds
27
+ history.splice(0, history.findLastIndex(v => v.time < now_minus_2sec));
28
28
  out.done += delta;
29
29
  out.refresh();
30
30
  },
@@ -32,8 +32,8 @@ function progress(total)
32
32
  out.add(done - out.done);
33
33
  },
34
34
  refresh: function () {
35
- const now_minus_5_seconds = Date.now() - 5000;
36
- const tmp = history.filter(v => v.time >= now_minus_5_seconds);
35
+ const now_minus_2sec = Date.now() - 2000;
36
+ const tmp = history.filter(v => v.time >= now_minus_2sec);
37
37
  if (tmp.length > 1) {
38
38
  const items = tmp.reduce((a,v) => a + v.delta, 0);
39
39
  const time_sec = (tmp.pop().time - tmp[0].time)/1000;