nhb-toolbox 4.31.3 → 4.31.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.
@@ -124,7 +124,7 @@ export type Expect<T extends true> = T;
124
124
  * type Check = Expect<Equal<'a', 'a'>>; // ✅ Compiles
125
125
  * type Fail = Expect<Equal<'a', 'b'>>; // ❌ Type error
126
126
  */
127
- export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
127
+ export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => (T extends Y ? 1 : 2) ? true : false;
128
128
  /**
129
129
  * * Ensures that an array has **at least one element**.
130
130
  *
@@ -24,16 +24,13 @@ export class Cipher {
24
24
  }
25
25
  encrypt(text) {
26
26
  const plain = utf8ToBytes(text);
27
- const seed = utf8ToBytes(String(Date.now()) + '-' + String(Math.random()));
27
+ const seed = utf8ToBytes(`${Date.now()}-${Math.random()}`);
28
28
  const ivFull = sha256Bytes(seed);
29
29
  const iv = ivFull.subarray(0, 16);
30
30
  const keystream = this.#genKeystream(plain, iv);
31
- const ct = new Uint8Array(plain.length);
32
- for (let i = 0; i < plain.length; i++)
33
- ct[i] = plain[i] ^ keystream[i];
31
+ const ct = plain.map((byte, i) => byte ^ keystream[i]);
34
32
  const tag = hmacSha256(this.#macKey, concatBytes(iv, ct));
35
- const blob = concatBytes(iv, ct, tag);
36
- return bytesToBase64(blob);
33
+ return bytesToBase64(concatBytes(iv, ct, tag));
37
34
  }
38
35
  isValid(token) {
39
36
  if (!isBase64(token))
@@ -58,12 +55,10 @@ export class Cipher {
58
55
  const ct = blob.subarray(16, blob.length - 32);
59
56
  const expectedTag = hmacSha256(this.#macKey, concatBytes(iv, ct));
60
57
  if (!_constantTimeEquals(expectedTag, tag)) {
61
- throw new Error('Key in the token is tampered or invalid!)');
58
+ throw new Error('Key in the token is tampered or invalid!');
62
59
  }
63
60
  const keystream = this.#genKeystream(ct, iv);
64
- const pt = new Uint8Array(ct.length);
65
- for (let i = 0; i < ct.length; i++)
66
- pt[i] = ct[i] ^ keystream[i];
61
+ const pt = ct.map((byte, i) => byte ^ keystream[i]);
67
62
  return bytesToUtf8(pt);
68
63
  }
69
64
  }
@@ -1,3 +1,4 @@
1
+ import { isUndefined } from '../guards/primitives.js';
1
2
  import { isHexString } from '../guards/specials.js';
2
3
  import { _bytesToRandomHex, _splitByCharLength } from './helpers.js';
3
4
  export function randomHex(length, uppercase = false) {
@@ -86,14 +87,14 @@ export function bytesToBase64(bytes) {
86
87
  const o2 = bytes[i++];
87
88
  const o3 = bytes[i++];
88
89
  const h1 = o1 >> 2;
89
- const h2 = ((o1 & 3) << 4) | (o2 >> 4);
90
- const h3 = ((o2 & 15) << 2) | (o3 >> 6);
91
- const h4 = o3 & 63;
90
+ const h2 = ((o1 & 3) << 4) | (!isUndefined(o2) ? o2 >> 4 : 0);
91
+ const h3 = !isUndefined(o2) ? ((o2 & 15) << 2) | (!isUndefined(o3) ? o3 >> 6 : 0) : 0;
92
+ const h4 = !isUndefined(o3) ? o3 & 63 : 0;
92
93
  out +=
93
94
  _b64chars[h1] +
94
95
  _b64chars[h2] +
95
- _b64chars[Number.isNaN(o2) ? 64 : h3] +
96
- _b64chars[Number.isNaN(o3) ? 64 : h4];
96
+ _b64chars[isUndefined(o2) ? 64 : h3] +
97
+ _b64chars[isUndefined(o3) ? 64 : h4];
97
98
  }
98
99
  return out;
99
100
  }
@@ -16,9 +16,9 @@ export function countObjectFields(obj) {
16
16
  return Object.keys(obj)?.length;
17
17
  return 0;
18
18
  }
19
- export function extractObjectKeys(obj, tuple) {
19
+ export function extractObjectKeys(obj, _tuple) {
20
20
  const keys = isNotEmptyObject(obj) ? Object.keys(obj) : [];
21
- return tuple ? keys : keys;
21
+ return keys;
22
22
  }
23
23
  export function extractObjectKeysDeep(obj) {
24
24
  function _getDeepKeys(candidate) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhb-toolbox",
3
- "version": "4.31.3",
3
+ "version": "4.31.4",
4
4
  "description": "A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -44,7 +44,7 @@
44
44
  },
45
45
  "license": "Apache-2.0",
46
46
  "devDependencies": {
47
- "@biomejs/biome": "^2.5.2",
47
+ "@biomejs/biome": "^2.5.3",
48
48
  "@types/jest": "^30.0.0",
49
49
  "@types/node": "^26.1.1",
50
50
  "husky": "^9.1.7",
@@ -52,7 +52,7 @@
52
52
  "lint-staged": "^17.0.8",
53
53
  "nhb-scripts": "^1.9.2",
54
54
  "ts-jest": "^29.4.11",
55
- "typescript": "^6.0.3"
55
+ "typescript": "^7.0.2"
56
56
  },
57
57
  "keywords": [
58
58
  "toolbox",