@vbarbarosh/node-helpers 3.22.0 → 3.23.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.22.0",
5
+ "version": "3.23.0",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "files": [
8
8
  "src"
@@ -1,10 +1,12 @@
1
1
  const axios = require('axios');
2
+ const make_int = require('./make_int');
2
3
 
3
4
  async function http_get_stream(url, options)
4
5
  {
5
6
  const res = await axios.get(url, {responseType: 'stream', ...options});
6
7
  const out = res.data;
7
8
  out.headers = res.headers;
9
+ out.total = make_int(res.headers['content-length'], null, 0);
8
10
  return out;
9
11
  }
10
12
 
@@ -9,6 +9,7 @@ async function http_get_stream_range(url, first, last)
9
9
  const headers = {Range: `bytes=${a}-${b}`};
10
10
  const res = await axios.get(url, {responseType: 'stream', headers});
11
11
  const out = res.data;
12
+ out.headers = res.headers;
12
13
  if (res.headers['content-range']) {
13
14
  out.content_range = parse_http_content_range(res.headers['content-range']);
14
15
  }
@@ -22,6 +23,7 @@ async function http_get_stream_range(url, first, last)
22
23
  else if (b && out.content_range.last !== b) {
23
24
  out.destroy(new Error(`Last byte of a returned range (${format_thousands(out.content_range.last)}) is not as expected: [${format_thousands(b)}]`));
24
25
  }
26
+ out.total = out.content_range.total;
25
27
  return out;
26
28
  }
27
29
 
@@ -0,0 +1,15 @@
1
+ function make_int(value, default_value = 0, min = Number.MIN_SAFE_INTEGER, max = Number.MAX_SAFE_INTEGER)
2
+ {
3
+ switch (typeof value) {
4
+ case 'bigint':
5
+ return Math.max(min, Math.min(max, Number(value)));
6
+ case 'symbol':
7
+ return default_value;
8
+ }
9
+ if (value === null || value === undefined || !isFinite(value)) {
10
+ return default_value;
11
+ }
12
+ return Math.max(min, Math.min(max, Math.round(value === 0 ? value : (+value||default_value))));
13
+ }
14
+
15
+ module.exports = make_int;