@vbarbarosh/node-helpers 3.43.0 → 3.45.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.43.0",
5
+ "version": "3.45.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,10 @@
1
+ /**
2
+ * Return a human-readable representation of hrtime (the return value from `process.hrtime(hrtime0)`).
3
+ */
4
+ function format_hrtime(hrtime, digits = 6)
5
+ {
6
+ const [u, v] = hrtime; // process.hrtime(hrtime0)
7
+ return (u + v/1E9).toFixed(digits) + 's';
8
+ }
9
+
10
+ module.exports = format_hrtime;
@@ -2,7 +2,7 @@ const format_hrtime = require('./format_hrtime');
2
2
 
3
3
  function perf_end_human(time0, digits = 4)
4
4
  {
5
- return format_hrtime(time0, digits);
5
+ return format_hrtime(process.hrtime(time0), digits);
6
6
  }
7
7
 
8
8
  module.exports = perf_end_human;
@@ -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;