@vbarbarosh/node-helpers 3.4.6 → 3.5.1
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/format_bytes.js +17 -0
- package/src/http_get_stream_range.js +22 -0
- package/src/http_head.js +8 -0
- package/src/pv.js +27 -0
package/package.json
CHANGED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
function format_bytes(bytes)
|
|
2
|
+
{
|
|
3
|
+
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
|
|
4
|
+
if (Number.isNaN(bytes*1)) {
|
|
5
|
+
return 'n/a';
|
|
6
|
+
}
|
|
7
|
+
if (bytes == 0) {
|
|
8
|
+
return '0KB';
|
|
9
|
+
}
|
|
10
|
+
if (bytes < 1024) {
|
|
11
|
+
return '1KB';
|
|
12
|
+
}
|
|
13
|
+
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10);
|
|
14
|
+
return `${(bytes / (1024 ** i)).toFixed(bytes > 1024*1024 ? 1 : 0)}${sizes[i]}`;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
module.exports = format_bytes;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const axios = require('axios');
|
|
2
|
+
const format_thousands = require('./format_thousands');
|
|
3
|
+
const parse_http_content_range = require('./parse_http_content_range');
|
|
4
|
+
|
|
5
|
+
async function http_get_stream_range(url, begin, end)
|
|
6
|
+
{
|
|
7
|
+
const a = Number.isInteger(begin) ? begin : '';
|
|
8
|
+
const b = Number.isInteger(end) ? (end - 1) : '';
|
|
9
|
+
const headers = {Range: `bytes=${a}-${b}`};
|
|
10
|
+
const res = await axios.get(url, {responseType: 'stream', headers});
|
|
11
|
+
const out = res.data;
|
|
12
|
+
out.content_range = parse_http_content_range(res.headers['content-range']);
|
|
13
|
+
if (a && out.content_range.first !== a) {
|
|
14
|
+
out.destroy(new Error(`First byte of a returned range (${format_thousands(out.content_range.first)}) is not as expected: [${format_thousands(a)}]`));
|
|
15
|
+
}
|
|
16
|
+
else if (b && out.content_range.last !== b) {
|
|
17
|
+
out.destroy(new Error(`Last byte of a returned range (${format_thousands(out.content_range.last)}) is not as expected: [${format_thousands(b)}]`));
|
|
18
|
+
}
|
|
19
|
+
return out;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = http_get_stream_range;
|
package/src/http_head.js
ADDED
package/src/pv.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const eta = require('./eta');
|
|
2
|
+
const format_bytes = require('./format_bytes');
|
|
3
|
+
const format_seconds = require('./format_seconds');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Monitor the progress of data through a pipe.
|
|
7
|
+
*/
|
|
8
|
+
async function* pv(stream, {total, resumed, log = s => console.log(s)})
|
|
9
|
+
{
|
|
10
|
+
let read = 0;
|
|
11
|
+
const time0 = Date.now();
|
|
12
|
+
for await (const chunk of stream) {
|
|
13
|
+
read += chunk.length;
|
|
14
|
+
const seconds = (Date.now() - time0)/1000;
|
|
15
|
+
const bps = read/seconds;
|
|
16
|
+
if (resumed) {
|
|
17
|
+
const done = read + resumed;
|
|
18
|
+
log(`${format_bytes(done)} of ${format_bytes(total)} at ${format_bytes(bps)}/s ETA ${eta(time0, total, done, resumed)}`);
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
log(`[${format_seconds(seconds)}] ${format_bytes(read)} at ${format_bytes(bps)}/s`);
|
|
22
|
+
}
|
|
23
|
+
yield chunk;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
module.exports = pv;
|