@vbarbarosh/node-helpers 3.44.0 → 3.46.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
@@ -1,5 +1,7 @@
1
1
  A set of helpers for JavaScript/Node.js
2
2
 
3
+ ![](img/node-helpers-by-chat-gpt.webp)
4
+
3
5
  ## Installation
4
6
 
5
7
  npm install @vbarbarosh/node-helpers
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.44.0",
5
+ "version": "3.46.0",
6
6
  "description": "A set of helpers for JavaScript/Node.js",
7
7
  "files": [
8
8
  "src"
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Ensures that the provided value is a date pointing to the future.
3
+ */
4
+ function assert_valid_expires_at(expires_at)
5
+ {
6
+ if (expires_at instanceof Date) {
7
+ if (expires_at.getTime() > Date.now()) {
8
+ return;
9
+ }
10
+ }
11
+ throw new Error('Invalid value for expires_at; it should be a Date object pointing to the future.');
12
+ }
13
+
14
+ module.exports = assert_valid_expires_at;
@@ -0,0 +1,11 @@
1
+ const crypto = require('crypto');
2
+
3
+ /**
4
+ * Returns random bytes in hexadecimal format.
5
+ */
6
+ function random_hex(bytes = 32)
7
+ {
8
+ return crypto.randomBytes(bytes).toString('hex');
9
+ }
10
+
11
+ module.exports = random_hex;
@@ -1,4 +1,5 @@
1
1
  const format_progress_bytes = require('./format_progress_bytes');
2
+ const format_progress_kilo = require('./format_progress_kilo');
2
3
  const make_progress = require('./make_progress');
3
4
  const stream = require('stream');
4
5
 
@@ -12,6 +13,7 @@ function stream_progress({objectMode = false, total, user_friendly_status = s =>
12
13
  let done = 0;
13
14
  const timer = setInterval(tick, 1000);
14
15
  const progress = make_progress(total);
16
+ const format_progress = objectMode ? format_progress_kilo : format_progress_bytes;
15
17
  return new stream.Transform({
16
18
  objectMode,
17
19
  destroy: function (error, callback) {
@@ -33,7 +35,7 @@ function stream_progress({objectMode = false, total, user_friendly_status = s =>
33
35
  });
34
36
  function tick() {
35
37
  progress.update(done);
36
- user_friendly_status(format_progress_bytes(progress));
38
+ user_friendly_status(format_progress(progress));
37
39
  }
38
40
  }
39
41