@catbee/utils 2.0.0-next.0 → 2.0.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.
Files changed (122) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +102 -52
  3. package/array/index.cjs +215 -74
  4. package/array/index.d.ts +345 -2
  5. package/array/index.mjs +201 -74
  6. package/async/index.cjs +116 -39
  7. package/async/index.d.ts +292 -2
  8. package/async/index.mjs +116 -40
  9. package/cache/index.cjs +2 -2
  10. package/cache/index.d.ts +156 -2
  11. package/cache/index.mjs +3 -3
  12. package/config/index.cjs +80 -66
  13. package/config/index.d.ts +65 -3
  14. package/config/index.mjs +77 -65
  15. package/context-store/index.cjs +2 -3
  16. package/context-store/index.d.ts +193 -2
  17. package/context-store/index.mjs +2 -3
  18. package/crypto/index.cjs +55 -5
  19. package/crypto/index.d.ts +225 -2
  20. package/crypto/index.mjs +52 -7
  21. package/date/index.cjs +676 -2
  22. package/date/index.d.ts +676 -2
  23. package/date/index.mjs +665 -3
  24. package/decorator/index.cjs +2172 -0
  25. package/{decorators/decorators.utils.d.ts → decorator/index.d.ts} +58 -54
  26. package/decorator/index.mjs +2131 -0
  27. package/{dir → directory}/index.cjs +5 -4
  28. package/{dir/dir.utils.d.ts → directory/index.d.ts} +24 -21
  29. package/{dir → directory}/index.mjs +5 -4
  30. package/env/index.cjs +100 -68
  31. package/env/index.d.ts +391 -2
  32. package/env/index.mjs +100 -68
  33. package/exception/index.cjs +1 -1
  34. package/exception/index.d.ts +233 -2
  35. package/exception/index.mjs +1 -1
  36. package/fs/index.cjs +71 -37
  37. package/fs/index.d.ts +206 -2
  38. package/fs/index.mjs +65 -35
  39. package/http-status-codes/index.cjs +1 -1
  40. package/http-status-codes/index.d.ts +268 -2
  41. package/http-status-codes/index.mjs +1 -1
  42. package/id/index.cjs +1 -1
  43. package/id/index.d.ts +38 -2
  44. package/id/index.mjs +1 -1
  45. package/index.cjs +13 -13
  46. package/index.d.ts +5 -5
  47. package/index.mjs +5 -5
  48. package/logger/index.cjs +13 -15
  49. package/logger/index.d.ts +190 -2
  50. package/logger/index.mjs +14 -16
  51. package/middleware/index.cjs +1 -1
  52. package/middleware/index.d.ts +104 -2
  53. package/middleware/index.mjs +1 -1
  54. package/object/index.cjs +379 -0
  55. package/{obj/obj.utils.d.ts → object/index.d.ts} +73 -33
  56. package/object/index.mjs +360 -0
  57. package/package.json +41 -23
  58. package/performance/index.cjs +4 -4
  59. package/performance/index.d.ts +139 -2
  60. package/performance/index.mjs +4 -4
  61. package/request/index.cjs +37 -25
  62. package/request/index.d.ts +242 -3
  63. package/request/index.mjs +37 -25
  64. package/response/index.cjs +1 -1
  65. package/response/index.d.ts +319 -3
  66. package/response/index.mjs +1 -1
  67. package/server/index.cjs +249 -146
  68. package/server/index.d.ts +866 -5
  69. package/server/index.mjs +248 -144
  70. package/stream/index.cjs +1 -1
  71. package/stream/index.d.ts +91 -2
  72. package/stream/index.mjs +1 -1
  73. package/string/index.cjs +34 -1
  74. package/string/index.d.ts +146 -2
  75. package/string/index.mjs +31 -2
  76. package/type/index.cjs +19 -2
  77. package/type/index.d.ts +144 -2
  78. package/type/index.mjs +17 -3
  79. package/types/index.cjs +1 -1
  80. package/types/index.d.ts +775 -5
  81. package/types/index.mjs +1 -1
  82. package/url/index.cjs +63 -5
  83. package/url/index.d.ts +200 -2
  84. package/url/index.mjs +59 -6
  85. package/{validate → validation}/index.cjs +91 -44
  86. package/{validate/validate.utils.d.ts → validation/index.d.ts} +33 -24
  87. package/{validate → validation}/index.mjs +87 -44
  88. package/array/array.utils.d.ts +0 -191
  89. package/async/async.utils.d.ts +0 -296
  90. package/cache/cache.utils.d.ts +0 -176
  91. package/config/config.d.ts +0 -57
  92. package/context-store/context-store.utils.d.ts +0 -212
  93. package/crypto/crypto.utils.d.ts +0 -183
  94. package/date/date.utils.d.ts +0 -190
  95. package/decorators/index.cjs +0 -913
  96. package/decorators/index.d.ts +0 -25
  97. package/decorators/index.mjs +0 -872
  98. package/dir/index.d.ts +0 -25
  99. package/env/env.utils.d.ts +0 -400
  100. package/exception/exception.utils.d.ts +0 -253
  101. package/fs/fs.utils.d.ts +0 -196
  102. package/http-status-codes/http-status-codes.d.ts +0 -289
  103. package/id/id.utils.d.ts +0 -59
  104. package/logger/logger.utils.d.ts +0 -210
  105. package/middleware/middleware.utils.d.ts +0 -123
  106. package/obj/index.cjs +0 -317
  107. package/obj/index.d.ts +0 -25
  108. package/obj/index.mjs +0 -301
  109. package/performance/performance.utils.d.ts +0 -159
  110. package/request/request.utils.d.ts +0 -109
  111. package/response/response.utils.d.ts +0 -186
  112. package/server/server.builder.d.ts +0 -531
  113. package/server/server.d.ts +0 -303
  114. package/stream/stream.utils.d.ts +0 -111
  115. package/string/string.utils.d.ts +0 -124
  116. package/type/type.utils.d.ts +0 -129
  117. package/types/api-response.d.ts +0 -175
  118. package/types/common.d.ts +0 -148
  119. package/types/config.d.ts +0 -88
  120. package/types/server.d.ts +0 -291
  121. package/url/url.utils.d.ts +0 -164
  122. package/validate/index.d.ts +0 -25
package/crypto/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * The MIT License
3
3
  *
4
- * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
4
+ * Copyright (c) 2026 Catbee Technologies. https://catbee.in/license
5
5
  *
6
6
  * Permission is hereby granted, free of charge, to any person obtaining a copy
7
7
  * of this software and associated documentation files (the "Software"), to deal
@@ -22,12 +22,14 @@
22
22
  * SOFTWARE.
23
23
  */
24
24
 
25
- import { scrypt, createHmac, createHash, randomBytes, timingSafeEqual, createCipheriv, createDecipheriv } from 'crypto';
25
+ import { pbkdf2, scrypt, createHmac, createHash, randomBytes, timingSafeEqual, createCipheriv, createDecipheriv } from 'crypto';
26
26
  import { promisify } from 'util';
27
27
  import { uuid } from '@catbee/utils/id';
28
28
 
29
29
  var __defProp = Object.defineProperty;
30
30
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
31
+ var pbkdf2Async = promisify(pbkdf2);
32
+ var scryptAsync = promisify(scrypt);
31
33
  function hmac(algorithm, input, secret, encoding = "hex") {
32
34
  return createHmac(algorithm, secret).update(input).digest(encoding);
33
35
  }
@@ -83,13 +85,13 @@ function safeCompare(a, b) {
83
85
  throw new Error("Cannot compare: inputs must be strings, Buffers, or Uint8Arrays");
84
86
  }
85
87
  __name(safeCompare, "safeCompare");
86
- var scryptAsync = promisify(scrypt);
87
88
  async function encrypt(data, key, options = {}) {
88
89
  const algorithm = options.algorithm || "aes-256-gcm";
89
90
  const inputEncoding = options.inputEncoding || "utf8";
90
91
  const outputEncoding = options.outputEncoding || "hex";
91
92
  const iv = randomBytes(16);
92
- const derivedKey = typeof key === "string" ? await scryptAsync(key, iv.slice(0, 8), 32) : key;
93
+ const salt = randomBytes(8);
94
+ const derivedKey = typeof key === "string" ? await scryptAsync(key, salt, 32) : key;
93
95
  const cipher = createCipheriv(algorithm, derivedKey, iv);
94
96
  let ciphertext;
95
97
  if (typeof data === "string") {
@@ -107,7 +109,8 @@ async function encrypt(data, key, options = {}) {
107
109
  ciphertext,
108
110
  iv,
109
111
  authTag,
110
- algorithm
112
+ algorithm,
113
+ salt
111
114
  };
112
115
  }
113
116
  __name(encrypt, "encrypt");
@@ -115,7 +118,7 @@ async function decrypt(encryptedData, key, options = {}) {
115
118
  const algorithm = options.algorithm || encryptedData.algorithm || "aes-256-gcm";
116
119
  const inputEncoding = options.inputEncoding || "hex";
117
120
  const outputEncoding = options.outputEncoding || "utf8";
118
- const derivedKey = typeof key === "string" ? await scryptAsync(key, encryptedData.iv.slice(0, 8), 32) : key;
121
+ const derivedKey = typeof key === "string" ? await scryptAsync(key, encryptedData.salt, 32) : key;
119
122
  const decipher = createDecipheriv(algorithm, derivedKey, encryptedData.iv);
120
123
  if (encryptedData.authTag && algorithm.includes("gcm")) {
121
124
  decipher.setAuthTag(encryptedData.authTag);
@@ -162,5 +165,47 @@ function verifySignedToken(token, secret) {
162
165
  }
163
166
  }
164
167
  __name(verifySignedToken, "verifySignedToken");
168
+ async function pbkdf2Hash(password, salt, keyLength = 32, iterations = 31e4) {
169
+ return pbkdf2Async(password, salt, iterations, keyLength, "sha256");
170
+ }
171
+ __name(pbkdf2Hash, "pbkdf2Hash");
172
+ function generateNonce(byteLength = 16, encoding = "hex") {
173
+ return randomBytes(byteLength).toString(encoding);
174
+ }
175
+ __name(generateNonce, "generateNonce");
176
+ function secureRandomInt(min, max) {
177
+ if (min > max) throw new Error("min must be less than or equal to max");
178
+ const range = max - min + 1;
179
+ const bytesNeeded = Math.ceil(Math.log2(range) / 8);
180
+ const maxValid = Math.floor(256 ** bytesNeeded / range) * range;
181
+ let randomValue;
182
+ do {
183
+ const bytes = randomBytes(bytesNeeded);
184
+ randomValue = bytes.reduce((acc, byte, i) => acc + byte * 256 ** i, 0);
185
+ } while (randomValue >= maxValid);
186
+ return min + randomValue % range;
187
+ }
188
+ __name(secureRandomInt, "secureRandomInt");
189
+ async function hashPassword(password, saltLength = 16, keyLength = 32) {
190
+ const salt = randomBytes(saltLength);
191
+ const scryptAsync2 = promisify(scrypt);
192
+ const derivedKey = await scryptAsync2(password, salt, keyLength);
193
+ return `${salt.toString("base64")}:${derivedKey.toString("base64")}`;
194
+ }
195
+ __name(hashPassword, "hashPassword");
196
+ async function verifyPassword(password, hash2) {
197
+ try {
198
+ const [saltB64, keyB64] = hash2.split(":");
199
+ if (!saltB64 || !keyB64) return false;
200
+ const salt = Buffer.from(saltB64, "base64");
201
+ const key = Buffer.from(keyB64, "base64");
202
+ const scryptAsync2 = promisify(scrypt);
203
+ const derivedKey = await scryptAsync2(password, salt, key.length);
204
+ return timingSafeEqual(key, derivedKey);
205
+ } catch {
206
+ return false;
207
+ }
208
+ }
209
+ __name(verifyPassword, "verifyPassword");
165
210
 
166
- export { createSignedToken, decrypt, encrypt, generateApiKey, generateRandomBytes, generateRandomBytesAsString, hash, hmac, md5, randomString, safeCompare, sha1, sha256, sha256Hmac, verifySignedToken };
211
+ export { createSignedToken, decrypt, encrypt, generateApiKey, generateNonce, generateRandomBytes, generateRandomBytesAsString, hash, hashPassword, hmac, md5, pbkdf2Hash, randomString, safeCompare, secureRandomInt, sha1, sha256, sha256Hmac, verifyPassword, verifySignedToken };