@vbarbarosh/node-helpers 3.19.1 → 3.21.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.19.1",
5
+ "version": "3.21.0",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "files": [
8
8
  "src"
package/src/fastdl.js CHANGED
@@ -1,6 +1,5 @@
1
1
  const format_bytes = require('./format_bytes');
2
- const format_percents = require('./format_percents');
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
- _(`Truncating destination file [${fs_path_basename(file)}]...`);
24
+ user_friendly_status(`Truncating destination file [${fs_path_basename(file)}]...`);
27
25
  await fs.promises.writeFile(file, '');
28
26
 
29
- _('Requesting first chunk to determine total size...');
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
- const p = progress;
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
- _(`${format_bytes(total)} [${format_thousands(total)} bytes] to download`);
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;
@@ -1,8 +1,11 @@
1
1
  const axios = require('axios');
2
2
 
3
- function http_get_stream(url, options)
3
+ async function http_get_stream(url, options)
4
4
  {
5
- return axios.get(url, {responseType: 'stream', ...options}).then(v => v.data);
5
+ const res = await axios.get(url, {responseType: 'stream', ...options});
6
+ const out = res.data;
7
+ out.headers = res.headers;
8
+ return out;
6
9
  }
7
10
 
8
11
  module.exports = http_get_stream;
@@ -1,6 +1,4 @@
1
- const format_bytes = require('./format_bytes');
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(`${format_percents(progress.percents)} | ${format_bytes(done)} of ${format_bytes(total)} at ${format_bytes(progress.rate)}/s ETA ${format_seconds(progress.eta)} duration=${format_seconds(progress.duration)}`);
27
+ user_friendly_status(format_progress(progress));
30
28
  }
31
29
  }
32
30