@vbarbarosh/node-helpers 3.4.2 → 3.4.4

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.4.2",
5
+ "version": "3.4.4",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "files": [
8
8
  "src"
package/src/eta.js CHANGED
@@ -3,10 +3,9 @@ const format_seconds = require('./format_seconds');
3
3
  function eta(time0, total, done, resumed = 0)
4
4
  {
5
5
  const seconds = (Date.now() - time0)/1000;
6
- const xdone = done - resumed;
7
- const xtotal = total - resumed;
8
- const bps = xdone/seconds;
9
- return format_seconds(xtotal/bps);
6
+ const remained = total - done;
7
+ const bps = (done - resumed)/seconds;
8
+ return format_seconds(remained/bps);
10
9
  }
11
10
 
12
11
  module.exports = eta;
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Returns size of a file, or 0 if file is not present. Was created for cases
3
+ * when you need to resume download.
4
+ *
5
+ * @returns {Promise<int>}
6
+ */
7
+ async function fs_size_enoent(path)
8
+ {
9
+ try {
10
+ const stat = await fs.promises.stat(path);
11
+ return stat.size;
12
+ }
13
+ catch (error) {
14
+ if (error.code === 'ENOENT') {
15
+ return 0;
16
+ }
17
+ throw error;
18
+ }
19
+ }
20
+
21
+ module.exports = fs_size_enoent;
@@ -0,0 +1,15 @@
1
+ function parse_http_content_range(value)
2
+ {
3
+ // Content-Range: bytes 262144000-272629759/5037662208
4
+ const m = value.match(/^(\w+)\s+(\d+)-(\d+)[/](\d+)$/);
5
+ if (!m) {
6
+ throw new Error(`Invalid Content-Range: [${value}]`);
7
+ }
8
+ const type = m[1];
9
+ const first = +m[2];
10
+ const last = +m[3];
11
+ const total = +m[4];
12
+ return {type, first, last, total};
13
+ }
14
+
15
+ module.exports = parse_http_content_range;
@@ -0,0 +1,14 @@
1
+ const assert = require('assert');
2
+ const parse_http_content_range = require('./parse_http_content_range');
3
+
4
+ const items = [
5
+ ['bytes 262144000-272629759/5037662208', {type: 'bytes', first: 262144000, last: 272629759, total: 5037662208}],
6
+ ];
7
+
8
+ describe('parse_http_content_range', function () {
9
+ items.forEach(function ([expr, expected]) {
10
+ it(expr, function () {
11
+ assert.deepStrictEqual(parse_http_content_range(expr), expected);
12
+ });
13
+ });
14
+ });