@vbarbarosh/node-helpers
Advanced tools
Comparing version 3.7.0 to 3.8.0
@@ -5,3 +5,3 @@ { | ||
"name": "@vbarbarosh/node-helpers", | ||
"version": "3.7.0", | ||
"version": "3.8.0", | ||
"description": "A set of helpers for JavaScript/Node.js", | ||
@@ -8,0 +8,0 @@ "files": [ |
@@ -5,2 +5,3 @@ const format_bytes = require('./format_bytes'); | ||
const fs = require('fs'); | ||
const fs_path_basename = require('./fs_path_basename'); | ||
const make_progress = require('./progress'); | ||
@@ -18,4 +19,5 @@ const parallel = require('./parallel'); | ||
*/ | ||
async function fastdl({file, read_stream_with_range, concurrency = 60, log = v => console.log(v)}) | ||
async function fastdl({file, read_stream_with_range, concurrency = 60, user_friendly_status = v => console.log(v)}) | ||
{ | ||
const _ = user_friendly_status; | ||
const M = 1024*1024; | ||
@@ -25,6 +27,6 @@ const chunk_min_bytes = M; | ||
log(`Truncating destination file [${file}]...`); | ||
_(`Truncating destination file [${fs_path_basename(file)}]...`); | ||
await fs.promises.writeFile(file, ''); | ||
log('Requesting first chunk to determine total size...'); | ||
_('Requesting first chunk to determine total size...'); | ||
const rs0 = await read_stream_with_range(0, chunk_min_bytes); | ||
@@ -45,6 +47,11 @@ | ||
const ss = format_seconds; | ||
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}`); | ||
if (p.rate) { | ||
_(`${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}`); | ||
} | ||
else { | ||
_(`${bb(p.done)} of ${bb(p.total)} [${(p.percentage*100).toFixed(2)}%] at ~ ETA ${ss(p.eta)} duration=${ss(p.duration)} connections=${connections}`); | ||
} | ||
} | ||
log(`${format_bytes(total)} [${format_thousands(total)} bytes] to download`); | ||
_(`${format_bytes(total)} [${format_thousands(total)} bytes] to download`); | ||
@@ -51,0 +58,0 @@ try { |
@@ -24,6 +24,6 @@ // - update progress by delta | ||
add: function (delta = 0) { | ||
const now_minus_5_seconds = Date.now() - 5000; | ||
const now_minus_2sec = Date.now() - 2000; | ||
history.push({time: Date.now(), delta}); | ||
// Keep last 5 seconds | ||
history.splice(0, history.findLastIndex(v => v.time < now_minus_5_seconds)); | ||
// Keep last 2 seconds | ||
history.splice(0, history.findLastIndex(v => v.time < now_minus_2sec)); | ||
out.done += delta; | ||
@@ -36,4 +36,4 @@ out.refresh(); | ||
refresh: function () { | ||
const now_minus_5_seconds = Date.now() - 5000; | ||
const tmp = history.filter(v => v.time >= now_minus_5_seconds); | ||
const now_minus_2sec = Date.now() - 2000; | ||
const tmp = history.filter(v => v.time >= now_minus_2sec); | ||
if (tmp.length > 1) { | ||
@@ -40,0 +40,0 @@ const items = tmp.reduce((a,v) => a + v.delta, 0); |
86130
2478