@vbarbarosh/node-helpers 3.17.0 → 3.18.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/README.md CHANGED
@@ -12,6 +12,9 @@ A set of helpers for JavaScript/Node.js
12
12
 
13
13
  `concurrency` - limit for parallel processes.
14
14
 
15
+ `user_friendly_status` - a function that accepts a string ready to present for the end-user (i.e.
16
+ string which is guaranteed contains no sensitive information like usernames or passwords).
17
+
15
18
  * [Node • Stream • readable.every](https://nodejs.org/api/stream.html#readableeveryfn-options)
16
19
  * [Node • Stream • readable.filter](https://nodejs.org/api/stream.html#readablefilterfn-options)
17
20
  * [Node • Stream • readable.find](https://nodejs.org/api/stream.html#readablefindfn-options)
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.17.0",
5
+ "version": "3.18.0",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "files": [
8
8
  "src"
package/src/fastdl.js CHANGED
@@ -3,7 +3,7 @@ const format_seconds = require('./format_seconds');
3
3
  const format_thousands = require('./format_thousands');
4
4
  const fs = require('fs');
5
5
  const fs_path_basename = require('./fs_path_basename');
6
- const make_progress = require('./progress');
6
+ const make_progress = require('./make_progress');
7
7
  const parallel = require('./parallel');
8
8
  const stream = require('stream');
9
9
 
@@ -0,0 +1,6 @@
1
+ function format_percents(v)
2
+ {
3
+ return (v*100).toFixed(2).replace(/\.0+$/, '') + '%';
4
+ }
5
+
6
+ module.exports = format_percents;
@@ -0,0 +1,17 @@
1
+ const assert = require('assert');
2
+ const format_percents = require('./format_percents');
3
+
4
+ const items = [
5
+ ['', '0%'],
6
+ [0, '0%'],
7
+ [0.5, '50%'],
8
+ [1, '100%'],
9
+ ];
10
+
11
+ describe('format_percents', function () {
12
+ items.forEach(function ([input, expected]) {
13
+ it(`${input} → ${expected}`, function () {
14
+ assert.strictEqual(expected, format_percents(input));
15
+ });
16
+ });
17
+ });
@@ -9,7 +9,13 @@ 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.content_range = parse_http_content_range(res.headers['content-range']);
12
+ if (res.headers['content-range']) {
13
+ out.content_range = parse_http_content_range(res.headers['content-range']);
14
+ }
15
+ else {
16
+ const len = res.headers['content-length'];
17
+ out.content_range = parse_http_content_range(`${res.headers['accept-ranges']} 0-${len}/${len}`);
18
+ }
13
19
  if (a && out.content_range.first !== a) {
14
20
  out.destroy(new Error(`First byte of a returned range (${format_thousands(out.content_range.first)}) is not as expected: [${format_thousands(a)}]`));
15
21
  }
@@ -2,11 +2,11 @@
2
2
  // - update progress by replacing total amount
3
3
  // - render eta
4
4
  // - render items/second
5
- // - render percentage
5
+ // - render percents
6
6
  // - time spent
7
7
  //
8
8
  // https://bramcohen.livejournal.com/24122.html
9
- function progress(total)
9
+ function make_progress(total)
10
10
  {
11
11
  const history = [];
12
12
  const time0 = Date.now();
@@ -19,12 +19,12 @@ function progress(total)
19
19
  rate: null,
20
20
  duration: 0,
21
21
  progress: null,
22
- percentage: null,
22
+ percents: null,
23
23
  add: function (delta = 0) {
24
- const now_minus_2sec = Date.now() - 2000;
24
+ const now_minus_5sec = Date.now() - 5000;
25
25
  history.push({time: Date.now(), delta});
26
- // Keep last 2 seconds
27
- history.splice(0, history.findLastIndex(v => v.time < now_minus_2sec));
26
+ // Keep last 5 seconds
27
+ history.splice(0, history.findLastIndex(v => v.time < now_minus_5sec));
28
28
  out.done += delta;
29
29
  out.refresh();
30
30
  },
@@ -43,11 +43,11 @@ function progress(total)
43
43
  out.rate = out.done/out.duration;
44
44
  }
45
45
  out.duration = (Date.now() - time0)/1000;
46
- out.percentage = !out.total ? null : out.done/out.total;
46
+ out.percents = !out.total ? null : out.done/out.total;
47
47
  out.eta = !out.total ? null : (out.total - out.done)/out.rate;
48
48
  },
49
49
  };
50
50
  return out;
51
51
  }
52
52
 
53
- module.exports = progress;
53
+ module.exports = make_progress;
@@ -0,0 +1,12 @@
1
+ const stream = require('stream');
2
+
3
+ function stream_discard()
4
+ {
5
+ return new stream.Writable({
6
+ write: function (buf, encoding, next) {
7
+ next();
8
+ },
9
+ });
10
+ }
11
+
12
+ module.exports = stream_discard;
@@ -0,0 +1,33 @@
1
+ const format_bytes = require('./format_bytes');
2
+ const format_percents = require('./format_percents');
3
+ const format_seconds = require('./format_seconds');
4
+ const make_progress = require('./make_progress');
5
+ const stream = require('stream');
6
+
7
+ /**
8
+ * Monitor the progress of data through a pipe. Similar to UNIX `pv` command.
9
+ */
10
+ function stream_progress({total, user_friendly_status = s => console.log(s)})
11
+ {
12
+ let done = 0;
13
+ const timer = setInterval(tick, 1000);
14
+ const progress = make_progress(total);
15
+ return new stream.Transform({
16
+ destroy: function (error, next) {
17
+ tick();
18
+ clearInterval(timer);
19
+ next();
20
+ },
21
+ transform: function (buf, encoding, next) {
22
+ done += buf.length;
23
+ this.push(buf, encoding);
24
+ next();
25
+ },
26
+ });
27
+ function tick() {
28
+ progress.update(done);
29
+ user_friendly_status(`${format_percents(progress.percents)} | ${format_bytes(done)} of ${format_bytes(total)} at ${format_bytes(progress.rate)}/s ETA ${format_seconds(progress.eta)} duration=${format_seconds(progress.duration)}`);
30
+ }
31
+ }
32
+
33
+ module.exports = stream_progress;
@@ -8,10 +8,10 @@ function stream_strpbrk(chars = '\r\n')
8
8
  const pending = [];
9
9
  return new stream.Transform({
10
10
  objectMode: true,
11
- decodeStrings: true,
12
11
  transform: function (str, encoding, next) {
13
- for (let off = 0; off < str.length; ) {
12
+ str = Buffer.isBuffer(str) ? str.toString() : str;
14
13
 
14
+ for (let off = 0; off < str.length; ) {
15
15
  // skip consecutive delimiters
16
16
  while (off < str.length) {
17
17
  let stop = true;
@@ -14,6 +14,8 @@ const items = [
14
14
  ['a|||b → a,b', '|', ['a|||b'], ['a', 'b']],
15
15
  ['abc → abc', '|', ['a', 'b', 'c'], ['abc']],
16
16
  ['a|b;c', '|;', ['a|b;c'], ['a', 'b', 'c']],
17
+ ['Buffer(a) → a', '|', ['a'].map(Buffer.from), ['a']],
18
+ ['Buffer(a|b) → a,b', '|', ['a|b'].map(Buffer.from), ['a', 'b']],
17
19
  ];
18
20
 
19
21
  describe('stream_strpbrk', function () {
package/src/pv.js DELETED
@@ -1,27 +0,0 @@
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 (total) {
17
- const done = read + (resumed || 0);
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;