@vbarbarosh/node-helpers 3.7.0 → 3.9.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/errors/AbortError.js +24 -0
- package/src/fastdl.js +12 -5
- package/src/progress.js +5 -5
package/package.json
CHANGED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// https://github.com/nodejs/node/issues/36084#issuecomment-729894622
|
|
2
|
+
class AbortError extends Error
|
|
3
|
+
{
|
|
4
|
+
constructor(message = 'The operation was aborted') {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = 'AbortError';
|
|
7
|
+
this.code = 'ABORT_ERR';
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
static absorb(error) {
|
|
11
|
+
if (AbortError.check(error)) {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
throw error;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
static check(error) {
|
|
18
|
+
return error
|
|
19
|
+
&& (error.name === 'AbortError')
|
|
20
|
+
&& (error.code === 'ABORT_ERR');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
module.exports = AbortError;
|
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,
|
|
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
|
-
|
|
25
|
+
_(`Truncating destination file [${fs_path_basename(file)}]...`);
|
|
24
26
|
await fs.promises.writeFile(file, '');
|
|
25
27
|
|
|
26
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
24
|
+
const now_minus_2sec = Date.now() - 2000;
|
|
25
25
|
history.push({time: Date.now(), delta});
|
|
26
|
-
// Keep last
|
|
27
|
-
history.splice(0, history.findLastIndex(v => v.time <
|
|
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
|
|
36
|
-
const tmp = history.filter(v => v.time >=
|
|
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;
|