@vbarbarosh/node-helpers 3.48.0 → 3.49.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.48.0",
5
+ "version": "3.49.0",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "files": [
8
8
  "src"
@@ -0,0 +1,30 @@
1
+ /**
2
+ * @link https://github.com/jshttp/range-parser
3
+ */
4
+ function http_range_parse(range, total)
5
+ {
6
+ const m = range.match(/^bytes\s*=\s*(\d*)-(\d*)$/);
7
+
8
+ if (!m || (!m[1] && !m[2])) {
9
+ throw new Error(`Cannot parse range "${range}". Only "bytes=FIRST-LAST", "bytes=FIRST-", or "bytes=-LAST" are supported.`);
10
+ }
11
+
12
+ let first = +m[1];
13
+ let last = +m[2];
14
+
15
+ if (!m[1]) { // -LAST
16
+ first = total - last;
17
+ last = total - 1;
18
+ }
19
+ else if (!m[2]) { // FIRST-
20
+ last = total - 1;
21
+ }
22
+
23
+ if (first < 0 || first > last || first >= total || last < 0 || last < first || last >= total) {
24
+ throw new Error(`Invalid range: first=${first}, last=${last}, total=${total}, expr="${range}"`);
25
+ }
26
+
27
+ return {first, last};
28
+ }
29
+
30
+ module.exports = http_range_parse;