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