tiny-essentials 1.1.1 → 1.2.1

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 (228) hide show
  1. package/README.md +3 -1
  2. package/dist/TinyBasicsEs.js +2821 -0
  3. package/dist/TinyBasicsEs.min.js +2 -0
  4. package/dist/TinyBasicsEs.min.js.LICENSE.txt +8 -0
  5. package/dist/TinyCertCrypto.js +31364 -0
  6. package/dist/TinyCertCrypto.min.js +2 -0
  7. package/dist/TinyCertCrypto.min.js.LICENSE.txt +8 -0
  8. package/dist/TinyCrypto.js +48138 -0
  9. package/dist/TinyCrypto.min.js +2 -0
  10. package/dist/TinyCrypto.min.js.LICENSE.txt +10 -0
  11. package/dist/TinyEssentials.js +77695 -0
  12. package/dist/TinyEssentials.min.js +2 -1
  13. package/dist/TinyEssentials.min.js.LICENSE.txt +10 -0
  14. package/dist/TinyLevelUp.js +173 -0
  15. package/dist/TinyLevelUp.min.js +1 -0
  16. package/dist/legacy/crypto/decrypt.cjs +1 -0
  17. package/dist/legacy/crypto/decrypt.mjs +1 -0
  18. package/dist/legacy/crypto/default.cjs +2 -0
  19. package/dist/legacy/crypto/default.mjs +1 -0
  20. package/dist/legacy/crypto/encrypt.cjs +2 -1
  21. package/dist/legacy/crypto/encrypt.mjs +1 -0
  22. package/dist/legacy/crypto/index.cjs +1 -1
  23. package/dist/legacy/crypto/index.mjs +1 -0
  24. package/dist/legacy/firebase/cookieSession.cjs +2 -0
  25. package/dist/legacy/firebase/cookieSession.mjs +1 -0
  26. package/dist/legacy/firebase/database/index.cjs +3 -0
  27. package/dist/legacy/firebase/database/index.mjs +1 -0
  28. package/dist/legacy/firebase/database/presence.cjs +3 -0
  29. package/dist/legacy/firebase/database/presence.mjs +1 -0
  30. package/dist/legacy/firebase/database/saveAsync.cjs +2 -0
  31. package/dist/legacy/firebase/database/saveAsync.mjs +1 -0
  32. package/dist/legacy/firebase/databaseEscape.cjs +3 -0
  33. package/dist/legacy/firebase/databaseEscape.mjs +1 -0
  34. package/dist/legacy/firebase/databaseLogger.cjs +3 -0
  35. package/dist/legacy/firebase/databaseLogger.mjs +1 -0
  36. package/dist/legacy/firebase/discord/api/addGuildMember.cjs +3 -0
  37. package/dist/legacy/firebase/discord/api/addGuildMember.mjs +1 -0
  38. package/dist/legacy/firebase/discord/api/getGuildWidget.cjs +3 -0
  39. package/dist/legacy/firebase/discord/api/getGuildWidget.mjs +1 -0
  40. package/dist/legacy/firebase/discord/api/getToken.cjs +3 -0
  41. package/dist/legacy/firebase/discord/api/getToken.mjs +1 -0
  42. package/dist/legacy/firebase/discord/api/getUser.cjs +3 -0
  43. package/dist/legacy/firebase/discord/api/getUser.mjs +1 -0
  44. package/dist/legacy/firebase/discord/api/getUserConnections.cjs +3 -0
  45. package/dist/legacy/firebase/discord/api/getUserConnections.mjs +1 -0
  46. package/dist/legacy/firebase/discord/api/getUserGuilds.cjs +3 -0
  47. package/dist/legacy/firebase/discord/api/getUserGuilds.mjs +1 -0
  48. package/dist/legacy/firebase/discord/api/index.cjs +3 -0
  49. package/dist/legacy/firebase/discord/api/index.mjs +1 -0
  50. package/dist/legacy/firebase/discord/api/refreshToken.cjs +3 -0
  51. package/dist/legacy/firebase/discord/api/refreshToken.mjs +1 -0
  52. package/dist/legacy/firebase/discord/api/revokeToken.cjs +3 -0
  53. package/dist/legacy/firebase/discord/api/revokeToken.mjs +1 -0
  54. package/dist/legacy/firebase/discord/config.cjs +2 -0
  55. package/dist/legacy/firebase/discord/config.mjs +1 -0
  56. package/dist/legacy/firebase/discord/firebase_redirect/index.cjs +3 -0
  57. package/dist/legacy/firebase/discord/firebase_redirect/index.mjs +1 -0
  58. package/dist/legacy/firebase/discord/firebase_redirect/login.cjs +2 -0
  59. package/dist/legacy/firebase/discord/firebase_redirect/login.mjs +1 -0
  60. package/dist/legacy/firebase/discord/firebase_redirect/logout.cjs +2 -0
  61. package/dist/legacy/firebase/discord/firebase_redirect/logout.mjs +1 -0
  62. package/dist/legacy/firebase/discord/get/authURLGenerator.cjs +3 -0
  63. package/dist/legacy/firebase/discord/get/authURLGenerator.mjs +1 -0
  64. package/dist/legacy/firebase/discord/get/cookie-session.cjs +3 -0
  65. package/dist/legacy/firebase/discord/get/cookie-session.mjs +1 -0
  66. package/dist/legacy/firebase/discord/get/credentials.cjs +2 -0
  67. package/dist/legacy/firebase/discord/get/credentials.mjs +1 -0
  68. package/dist/legacy/firebase/discord/get/errorValidator.cjs +3 -0
  69. package/dist/legacy/firebase/discord/get/errorValidator.mjs +1 -0
  70. package/dist/legacy/firebase/discord/get/index.cjs +3 -0
  71. package/dist/legacy/firebase/discord/get/index.mjs +1 -0
  72. package/dist/legacy/firebase/discord/get/randomAvatar.cjs +3 -0
  73. package/dist/legacy/firebase/discord/get/randomAvatar.mjs +1 -0
  74. package/dist/legacy/firebase/discord/http/index.cjs +3 -0
  75. package/dist/legacy/firebase/discord/http/index.mjs +1 -0
  76. package/dist/legacy/firebase/discord/http/login.cjs +3 -0
  77. package/dist/legacy/firebase/discord/http/login.mjs +1 -0
  78. package/dist/legacy/firebase/discord/http/logout.cjs +3 -0
  79. package/dist/legacy/firebase/discord/http/logout.mjs +1 -0
  80. package/dist/legacy/firebase/discord/http/redirect.cjs +3 -0
  81. package/dist/legacy/firebase/discord/http/redirect.mjs +1 -0
  82. package/dist/legacy/firebase/discord/http/refreshToken.cjs +3 -0
  83. package/dist/legacy/firebase/discord/http/refreshToken.mjs +1 -0
  84. package/dist/legacy/firebase/discord/index.cjs +3 -0
  85. package/dist/legacy/firebase/discord/index.mjs +1 -0
  86. package/dist/legacy/firebase/discord/template/cookie-session.cjs +3 -0
  87. package/dist/legacy/firebase/discord/template/cookie-session.mjs +1 -0
  88. package/dist/legacy/firebase/domainRedirect.cjs +2 -0
  89. package/dist/legacy/firebase/domainRedirect.mjs +1 -0
  90. package/dist/legacy/firebase/escape.cjs +2 -0
  91. package/dist/legacy/firebase/escape.mjs +1 -0
  92. package/dist/legacy/firebase/getDBAsync.cjs +2 -0
  93. package/dist/legacy/firebase/getDBAsync.mjs +1 -0
  94. package/dist/legacy/firebase/getDBData.cjs +3 -0
  95. package/dist/legacy/firebase/getDBData.mjs +1 -0
  96. package/dist/legacy/firebase/getDBValue.cjs +2 -0
  97. package/dist/legacy/firebase/getDBValue.mjs +1 -0
  98. package/dist/legacy/firebase/index.cjs +3 -0
  99. package/dist/legacy/firebase/index.mjs +1 -0
  100. package/dist/legacy/firebase/isEmulator.cjs +2 -0
  101. package/dist/legacy/firebase/isEmulator.mjs +1 -0
  102. package/dist/legacy/firebase/logger.cjs +3 -0
  103. package/dist/legacy/firebase/logger.mjs +1 -0
  104. package/dist/legacy/firebase/mySQL.cjs +3 -0
  105. package/dist/legacy/firebase/mySQL.mjs +1 -0
  106. package/dist/legacy/firebase/mysqlConnector/create.cjs +2 -0
  107. package/dist/legacy/firebase/mysqlConnector/create.mjs +1 -0
  108. package/dist/legacy/firebase/mysqlConnector/index.cjs +3 -0
  109. package/dist/legacy/firebase/mysqlConnector/index.mjs +1 -0
  110. package/dist/legacy/firebase/mysqlConnector/nextPrev.cjs +2 -0
  111. package/dist/legacy/firebase/mysqlConnector/nextPrev.mjs +1 -0
  112. package/dist/legacy/firebase/mysqlConnector/pagination.cjs +2 -0
  113. package/dist/legacy/firebase/mysqlConnector/pagination.mjs +1 -0
  114. package/dist/legacy/firebase/mysqlConnector/sameUser.cjs +3 -0
  115. package/dist/legacy/firebase/mysqlConnector/sameUser.mjs +1 -0
  116. package/dist/legacy/firebase/transactionDBAsync.cjs +2 -0
  117. package/dist/legacy/firebase/transactionDBAsync.mjs +1 -0
  118. package/dist/legacy/get/countObj.cjs +3 -0
  119. package/dist/legacy/get/countObj.mjs +1 -0
  120. package/dist/legacy/get/decimalColor.cjs +3 -0
  121. package/dist/legacy/get/decimalColor.mjs +1 -0
  122. package/dist/legacy/get/objType.cjs +2 -0
  123. package/dist/legacy/get/objType.mjs +1 -0
  124. package/dist/legacy/get/pagination.cjs +3 -0
  125. package/dist/legacy/get/pagination.mjs +1 -0
  126. package/dist/legacy/get/queryUrlByName.cjs +2 -0
  127. package/dist/legacy/get/queryUrlByName.mjs +1 -0
  128. package/dist/legacy/get/queryUrlJSON.cjs +2 -0
  129. package/dist/legacy/get/queryUrlJSON.mjs +1 -0
  130. package/dist/legacy/get/super_string_filter.cjs +3 -0
  131. package/dist/legacy/get/super_string_filter.mjs +1 -0
  132. package/dist/legacy/get/versionCheck.cjs +2 -1
  133. package/dist/legacy/get/versionCheck.mjs +1 -0
  134. package/dist/legacy/http/HTTP-1.0.cjs +2 -0
  135. package/dist/legacy/http/HTTP-1.0.mjs +1 -0
  136. package/dist/legacy/http/auth.cjs +2 -0
  137. package/dist/legacy/http/auth.mjs +1 -0
  138. package/dist/legacy/http/check_domain.cjs +2 -0
  139. package/dist/legacy/http/check_domain.mjs +1 -0
  140. package/dist/legacy/http/csrfTokenAnalyze.cjs +2 -0
  141. package/dist/legacy/http/csrfTokenAnalyze.mjs +1 -0
  142. package/dist/legacy/http/domainValidator.cjs +3 -0
  143. package/dist/legacy/http/domainValidator.mjs +1 -0
  144. package/dist/legacy/http/errorsCallback.cjs +2 -0
  145. package/dist/legacy/http/errorsCallback.mjs +1 -0
  146. package/dist/legacy/http/fetch/json.cjs +3 -0
  147. package/dist/legacy/http/fetch/json.mjs +1 -0
  148. package/dist/legacy/http/fetch/text.cjs +3 -0
  149. package/dist/legacy/http/fetch/text.mjs +1 -0
  150. package/dist/legacy/http/fileCache.cjs +3 -0
  151. package/dist/legacy/http/fileCache.mjs +1 -0
  152. package/dist/legacy/http/getDomainURL.cjs +3 -0
  153. package/dist/legacy/http/getDomainURL.mjs +1 -0
  154. package/dist/legacy/http/userIP.cjs +3 -0
  155. package/dist/legacy/http/userIP.mjs +1 -0
  156. package/dist/legacy/index.cjs +3 -0
  157. package/dist/legacy/index.mjs +1 -0
  158. package/dist/legacy/libs/arraySortPositions.cjs +2 -0
  159. package/dist/legacy/libs/arraySortPositions.mjs +1 -0
  160. package/dist/legacy/libs/capitalize.cjs +2 -0
  161. package/dist/legacy/libs/capitalize.mjs +1 -0
  162. package/dist/legacy/libs/convertBytes.cjs +2 -0
  163. package/dist/legacy/libs/convertBytes.mjs +1 -0
  164. package/dist/legacy/libs/custom_module_loader.cjs +3 -0
  165. package/dist/legacy/libs/custom_module_loader.mjs +1 -0
  166. package/dist/legacy/libs/dice.cjs +2 -0
  167. package/dist/legacy/libs/dice.mjs +1 -0
  168. package/dist/legacy/libs/markdown.cjs +2 -0
  169. package/dist/legacy/libs/markdown.mjs +1 -0
  170. package/dist/legacy/libs/percentage.cjs +2 -0
  171. package/dist/legacy/libs/percentage.mjs +1 -0
  172. package/dist/legacy/libs/regex/getLetter.cjs +2 -0
  173. package/dist/legacy/libs/regex/getLetter.mjs +1 -0
  174. package/dist/legacy/libs/replaceAsync.cjs +3 -0
  175. package/dist/legacy/libs/replaceAsync.mjs +3 -0
  176. package/dist/legacy/libs/rule3.cjs +2 -0
  177. package/dist/legacy/libs/rule3.mjs +1 -0
  178. package/dist/legacy/libs/userLevel.cjs +9 -9
  179. package/dist/legacy/libs/userLevel.d.mts +39 -16
  180. package/dist/legacy/libs/userLevel.mjs +9 -9
  181. package/dist/legacy/momentjs/getAge.cjs +3 -0
  182. package/dist/legacy/momentjs/getAge.mjs +1 -0
  183. package/dist/legacy/momentjs/index.cjs +1 -1
  184. package/dist/legacy/momentjs/index.mjs +1 -0
  185. package/dist/legacy/momentjs/timeDuration.cjs +3 -0
  186. package/dist/legacy/momentjs/timeDuration.mjs +1 -0
  187. package/dist/legacy/socket.io/antiFlood/index.cjs +1 -1
  188. package/dist/legacy/socket.io/antiFlood/index.mjs +1 -0
  189. package/dist/legacy/socket.io/antiFlood/install.cjs +3 -0
  190. package/dist/legacy/socket.io/antiFlood/install.mjs +1 -0
  191. package/dist/legacy/socket.io/antiFlood/verify.cjs +3 -0
  192. package/dist/legacy/socket.io/antiFlood/verify.mjs +1 -0
  193. package/dist/legacy/socket.io/cookie-session.cjs +2 -0
  194. package/dist/legacy/socket.io/cookie-session.mjs +1 -0
  195. package/dist/legacy/socket.io/discord.cjs +3 -0
  196. package/dist/legacy/socket.io/discord.mjs +1 -0
  197. package/dist/legacy/socket.io/index.cjs +3 -0
  198. package/dist/legacy/socket.io/index.mjs +1 -0
  199. package/dist/v1/basics/clock.cjs +43 -13
  200. package/dist/v1/basics/clock.mjs +31 -13
  201. package/dist/v1/basics/index.cjs +27 -0
  202. package/dist/v1/basics/index.d.mts +17 -0
  203. package/dist/v1/basics/index.mjs +7 -0
  204. package/dist/v1/basics/objFilter.cjs +7 -1
  205. package/dist/v1/basics/objFilter.mjs +4 -0
  206. package/dist/v1/basics/simpleMath.cjs +1 -1
  207. package/dist/v1/basics/simpleMath.mjs +1 -1
  208. package/dist/v1/build/TinyCertCrypto.cjs +7 -0
  209. package/dist/v1/build/TinyCertCrypto.d.mts +2 -0
  210. package/dist/v1/build/TinyCertCrypto.mjs +2 -0
  211. package/dist/v1/build/TinyCrypto.cjs +7 -0
  212. package/dist/v1/build/TinyCrypto.d.mts +2 -0
  213. package/dist/v1/build/TinyCrypto.mjs +2 -0
  214. package/dist/v1/build/TinyLevelUp.cjs +7 -0
  215. package/dist/v1/build/TinyLevelUp.d.mts +2 -0
  216. package/dist/v1/build/TinyLevelUp.mjs +2 -0
  217. package/dist/v1/index.cjs +2 -0
  218. package/dist/v1/index.d.mts +2 -1
  219. package/dist/v1/index.mjs +2 -1
  220. package/dist/v1/libs/TinyCertCrypto.cjs +573 -0
  221. package/dist/v1/libs/TinyCertCrypto.d.mts +193 -0
  222. package/dist/v1/libs/TinyCertCrypto.mjs +481 -0
  223. package/dist/v1/libs/TinyCrypto.cjs +91 -45
  224. package/dist/v1/libs/TinyCrypto.d.mts +65 -26
  225. package/dist/v1/libs/TinyCrypto.mjs +64 -39
  226. package/docs/README.md +2 -0
  227. package/docs/libs/TinyCertCrypto.md +202 -0
  228. package/package.json +11 -6
@@ -2,9 +2,37 @@
2
2
 
3
3
  var crypto = require('crypto');
4
4
  var fs = require('fs');
5
+ var buffer = require('buffer');
5
6
  var objFilter = require('../basics/objFilter.cjs');
6
7
 
7
- // Detecta se estamos no browser
8
+ function _interopNamespaceDefault(e) {
9
+ var n = Object.create(null);
10
+ if (e) {
11
+ Object.keys(e).forEach(function (k) {
12
+ if (k !== 'default') {
13
+ var d = Object.getOwnPropertyDescriptor(e, k);
14
+ Object.defineProperty(n, k, d.get ? d : {
15
+ enumerable: true,
16
+ get: function () { return e[k]; }
17
+ });
18
+ }
19
+ });
20
+ }
21
+ n.default = e;
22
+ return Object.freeze(n);
23
+ }
24
+
25
+ var fs__namespace = /*#__PURE__*/_interopNamespaceDefault(fs);
26
+
27
+ /**
28
+ * Determines if the environment is a browser.
29
+ *
30
+ * This constant checks if the code is running in a browser environment by verifying if
31
+ * the `window` object and the `window.document` object are available. It will return `true`
32
+ * if the environment is a browser, and `false` otherwise (e.g., in a Node.js environment).
33
+ *
34
+ * @constant {boolean}
35
+ */
8
36
  const isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
9
37
 
10
38
  /**
@@ -21,8 +49,8 @@ class TinyCrypto {
21
49
  *
22
50
  * @param {Object} [options={}] - Configuration options for encryption and decryption.
23
51
  * @param {string} [options.algorithm='aes-256-gcm'] - The encryption algorithm to use. Recommended: 'aes-256-gcm' for authenticated encryption.
24
- * @param {string} [options.outputEncoding='hex'] - The encoding used when returning encrypted data (e.g., 'hex', 'base64').
25
- * @param {string} [options.inputEncoding='utf8'] - The encoding used for plaintext inputs (e.g., 'utf8').
52
+ * @param {BufferEncoding} [options.outputEncoding='hex'] - The encoding used when returning encrypted data (e.g., 'hex', 'base64').
53
+ * @param {BufferEncoding} [options.inputEncoding='utf8'] - The encoding used for plaintext inputs (e.g., 'utf8').
26
54
  * @param {number} [options.authTagLength=16] - The length of the authentication tag used in GCM mode. Usually 16 for AES-256-GCM.
27
55
  * @param {Buffer} [options.key] - Optional 32-byte cryptographic key. If not provided, a random key is generated.
28
56
  *
@@ -32,15 +60,17 @@ class TinyCrypto {
32
60
  * const crypto = new CryptoManager({
33
61
  * algorithm: 'aes-256-gcm',
34
62
  * outputEncoding: 'base64',
35
- * key: crypto.randomBytes(32),
63
+ * key: randomBytes(32),
36
64
  * });
37
65
  */
38
66
  constructor(options = {}) {
39
67
  this.algorithm = options.algorithm || 'aes-256-gcm';
40
- this.outputEncoding = options.outputEncoding || 'hex';
41
- this.inputEncoding = options.inputEncoding || 'utf8';
42
68
  this.authTagLength = options.authTagLength || 16;
43
69
  this.key = options.key || this.generateKey();
70
+ /** @type {BufferEncoding} */
71
+ this.outputEncoding = options.outputEncoding || 'hex';
72
+ /** @type {BufferEncoding} */
73
+ this.inputEncoding = options.inputEncoding || 'utf8';
44
74
  }
45
75
 
46
76
  /**
@@ -78,10 +108,7 @@ class TinyCrypto {
78
108
  *
79
109
  * @param {*} data - The data to encrypt. Can be of any supported type (string, number, boolean, Date, JSON, etc.).
80
110
  * @param {Buffer} [iv=this.generateIV()] - Optional Initialization Vector (IV). If not provided, a secure random IV is generated.
81
- * @returns {Object} An object containing:
82
- * - `iv` {string} - The IV used, encoded with the output encoding.
83
- * - `encrypted` {string} - The encrypted payload.
84
- * - `authTag` {string} - The authentication tag used to verify the integrity of the ciphertext.
111
+ * @returns {EncryptedDataParams} An object containing the encrypted data.
85
112
  *
86
113
  * @example
87
114
  * const result = cryptoManager.encrypt('Hello, world!');
@@ -95,30 +122,40 @@ class TinyCrypto {
95
122
  const plainText = this.#serialize(data);
96
123
 
97
124
  const cipher = crypto.createCipheriv(this.algorithm, this.key, iv, {
125
+ // @ts-ignore
98
126
  authTagLength: this.authTagLength,
99
127
  });
100
128
 
129
+ // @ts-ignore
101
130
  let encrypted = cipher.update(plainText, this.inputEncoding);
102
- encrypted = Buffer.concat([encrypted, cipher.final()]);
131
+ // @ts-ignore
132
+ encrypted = buffer.Buffer.concat([encrypted, cipher.final()]);
103
133
  const authTag = cipher.getAuthTag();
104
134
 
105
135
  return {
136
+ // @ts-ignore
106
137
  iv: iv.toString(this.outputEncoding),
138
+ // @ts-ignore
107
139
  encrypted: encrypted.toString(this.outputEncoding),
140
+ // @ts-ignore
108
141
  authTag: authTag.toString(this.outputEncoding),
109
142
  };
110
143
  }
111
144
 
145
+ /**
146
+ * @typedef {Object} EncryptedDataParams
147
+ * @property {string} iv - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
148
+ * @property {string} encrypted - The encrypted data to decrypt, encoded with the output encoding.
149
+ * @property {string} authTag - The authentication tag used to verify the integrity of the encrypted data.
150
+ */
151
+
112
152
  /**
113
153
  * Decrypts a previously encrypted value.
114
154
  *
115
155
  * The method checks the integrity of the data using the authentication tag (`authTag`) and ensures the data is properly decrypted.
116
156
  * After decryption, it automatically deserializes the data back to its original type.
117
157
  *
118
- * @param {Object} params - An object containing the encrypted data:
119
- * - `iv` {string} - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
120
- * - `encrypted` {string} - The encrypted data to decrypt, encoded with the output encoding.
121
- * - `authTag` {string} - The authentication tag used to verify the integrity of the encrypted data.
158
+ * @param {EncryptedDataParams} params - An object containing the encrypted data.
122
159
  * @param {string|null} [expectedType=null] - Optionally specify the expected type of the decrypted data. If provided, the method will validate the type of the deserialized value.
123
160
  * @returns {*} The decrypted value, which will be the original type of the data before encryption.
124
161
  * @throws {Error} Throws if the authentication tag doesn't match or the data has been tampered with.
@@ -134,17 +171,21 @@ class TinyCrypto {
134
171
  * console.log(decrypted); // Outputs: 'Hello, world!'
135
172
  */
136
173
  decrypt({ iv, encrypted, authTag }, expectedType = null) {
137
- const ivBuffer = Buffer.from(iv, this.outputEncoding);
138
- const encryptedBuffer = Buffer.from(encrypted, this.outputEncoding);
139
- const authTagBuffer = Buffer.from(authTag, this.outputEncoding);
174
+ const ivBuffer = buffer.Buffer.from(iv, this.outputEncoding);
175
+ const encryptedBuffer = buffer.Buffer.from(encrypted, this.outputEncoding);
176
+ const authTagBuffer = buffer.Buffer.from(authTag, this.outputEncoding);
140
177
 
141
178
  const decipher = crypto.createDecipheriv(this.algorithm, this.key, ivBuffer, {
179
+ // @ts-ignore
142
180
  authTagLength: this.authTagLength,
143
181
  });
144
182
 
145
183
  decipher.setAuthTag(authTagBuffer);
146
184
 
185
+ /** @type {string} */
186
+ // @ts-ignore
147
187
  let decrypted = decipher.update(encryptedBuffer, null, this.inputEncoding);
188
+ // @ts-ignore
148
189
  decrypted += decipher.final(this.inputEncoding);
149
190
  const { value, type } = this.#deserialize(decrypted);
150
191
 
@@ -158,10 +199,7 @@ class TinyCrypto {
158
199
  * This method decrypts the encrypted data and extracts its type information without fully deserializing the value.
159
200
  * It is useful when you need to verify the type of the encrypted data before fully decrypting it.
160
201
  *
161
- * @param {Object} params - An object containing the encrypted data:
162
- * - `iv` {string} - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
163
- * - `encrypted` {string} - The encrypted data to decrypt, encoded with the output encoding.
164
- * - `authTag` {string} - The authentication tag used to verify the integrity of the encrypted data.
202
+ * @param {EncryptedDataParams} params - An object containing the encrypted data.
165
203
  * @returns {string} The type of the original data (e.g., 'string', 'number', 'date', etc.).
166
204
  *
167
205
  * @example
@@ -174,17 +212,20 @@ class TinyCrypto {
174
212
  * console.log(dataType); // Outputs: 'string'
175
213
  */
176
214
  getTypeFromEncrypted({ iv, encrypted, authTag }) {
177
- const ivBuffer = Buffer.from(iv, this.outputEncoding);
178
- const encryptedBuffer = Buffer.from(encrypted, this.outputEncoding);
179
- const authTagBuffer = Buffer.from(authTag, this.outputEncoding);
215
+ const ivBuffer = buffer.Buffer.from(iv, this.outputEncoding);
216
+ const encryptedBuffer = buffer.Buffer.from(encrypted, this.outputEncoding);
217
+ const authTagBuffer = buffer.Buffer.from(authTag, this.outputEncoding);
180
218
 
181
219
  const decipher = crypto.createDecipheriv(this.algorithm, this.key, ivBuffer, {
220
+ // @ts-ignore
182
221
  authTagLength: this.authTagLength,
183
222
  });
184
223
 
185
224
  decipher.setAuthTag(authTagBuffer);
186
225
 
226
+ // @ts-ignore
187
227
  let decrypted = decipher.update(encryptedBuffer, null, this.inputEncoding);
228
+ // @ts-ignore
188
229
  decrypted += decipher.final(this.inputEncoding);
189
230
 
190
231
  const { type } = this.#deserialize(decrypted);
@@ -217,7 +258,7 @@ class TinyCrypto {
217
258
  a.download = filename;
218
259
  a.click();
219
260
  URL.revokeObjectURL(url);
220
- } else fs.writeFileSync(filename, data);
261
+ } else fs__namespace.writeFileSync(filename, data);
221
262
  }
222
263
 
223
264
  /**
@@ -246,7 +287,7 @@ class TinyCrypto {
246
287
  a.download = filename;
247
288
  a.click();
248
289
  URL.revokeObjectURL(url);
249
- } else fs.writeFileSync(filename, configData);
290
+ } else fs__namespace.writeFileSync(filename, configData);
250
291
  }
251
292
 
252
293
  /**
@@ -274,10 +315,12 @@ class TinyCrypto {
274
315
  async loadConfigFromFile(file) {
275
316
  if (isBrowser) {
276
317
  return new Promise((resolve, reject) => {
318
+ if (!(file instanceof File))
319
+ return reject(new Error('In browser, the file must be a File object'));
277
320
  const reader = new FileReader();
278
321
  reader.onload = () => {
279
322
  try {
280
- const config = JSON.parse(reader.result);
323
+ const config = typeof reader.result === 'string' ? JSON.parse(reader.result) : {};
281
324
  resolve(this.importConfig(config));
282
325
  } catch (err) {
283
326
  reject(new Error('Invalid config JSON file'));
@@ -287,7 +330,7 @@ class TinyCrypto {
287
330
  reader.readAsText(file);
288
331
  });
289
332
  } else {
290
- const raw = fs.readFileSync(file, 'utf8');
333
+ const raw = fs__namespace.readFileSync(/** @type {string} */ (file), 'utf8');
291
334
  const config = JSON.parse(raw);
292
335
  return this.importConfig(config);
293
336
  }
@@ -319,10 +362,12 @@ class TinyCrypto {
319
362
  async loadKeyFromFile(file) {
320
363
  if (isBrowser) {
321
364
  return new Promise((resolve, reject) => {
365
+ if (!(file instanceof File))
366
+ return reject(new Error('In browser, the file must be a File object'));
322
367
  const reader = new FileReader();
323
368
  reader.onload = () => {
324
- const hexKey = reader.result.trim();
325
- const keyBuffer = Buffer.from(hexKey, 'hex');
369
+ const hexKey = typeof reader.result === 'string' ? reader.result.trim() : '';
370
+ const keyBuffer = buffer.Buffer.from(hexKey, 'hex');
326
371
  this.key = keyBuffer;
327
372
  resolve(keyBuffer);
328
373
  };
@@ -330,8 +375,8 @@ class TinyCrypto {
330
375
  reader.readAsText(file);
331
376
  });
332
377
  } else {
333
- const hexKey = fs.readFileSync(file, 'utf8');
334
- const keyBuffer = Buffer.from(hexKey, 'hex');
378
+ const hexKey = fs__namespace.readFileSync(/** @type {string} */ (file), 'utf8');
379
+ const keyBuffer = buffer.Buffer.from(hexKey, 'hex');
335
380
  this.key = keyBuffer;
336
381
  return keyBuffer;
337
382
  }
@@ -376,8 +421,8 @@ class TinyCrypto {
376
421
  *
377
422
  * @param {Object} config - The configuration object to import.
378
423
  * @param {string} config.algorithm - The encryption algorithm (e.g., 'aes-256-gcm').
379
- * @param {string} config.outputEncoding - The output encoding format (e.g., 'hex').
380
- * @param {string} config.inputEncoding - The input encoding format (e.g., 'utf8').
424
+ * @param {BufferEncoding} config.outputEncoding - The output encoding format (e.g., 'hex').
425
+ * @param {BufferEncoding} config.inputEncoding - The input encoding format (e.g., 'utf8').
381
426
  * @param {number} config.authTagLength - The authentication tag length (e.g., 16).
382
427
  * @param {string} config.key - The cryptographic key in hexadecimal string format.
383
428
  *
@@ -409,7 +454,7 @@ class TinyCrypto {
409
454
  else if (typeof config.authTagLength !== 'undefined')
410
455
  throw new Error('Invalid or missing "authTagLength" property. Expected a number.');
411
456
 
412
- if (typeof config.key === 'string') this.key = Buffer.from(config.key, 'hex');
457
+ if (typeof config.key === 'string') this.key = buffer.Buffer.from(config.key, 'hex');
413
458
  else if (typeof config.key !== 'undefined')
414
459
  throw new Error('Invalid or missing "key" property. Expected a hexadecimal string.');
415
460
  }
@@ -424,7 +469,7 @@ class TinyCrypto {
424
469
  * Each key corresponds to a specific data type (e.g., 'number', 'date', 'buffer', etc.),
425
470
  * and the value is a function that serializes the data to a specific format.
426
471
  *
427
- * @type {Object}
472
+ * @type {Record<string, (data: any) => string>}
428
473
  * @property {Function} weakmap - Throws an error as WeakMap cannot be serialized.
429
474
  * @property {Function} weakset - Throws an error as WeakSet cannot be serialized.
430
475
  * @property {Function} promise - Throws an error as Promise cannot be serialized.
@@ -465,8 +510,8 @@ class TinyCrypto {
465
510
  number: (data) => JSON.stringify({ __type: 'number', value: data }),
466
511
  boolean: (data) => JSON.stringify({ __type: 'boolean', value: data }),
467
512
  string: (data) => JSON.stringify({ __type: 'string', value: data }),
468
- null: (data) => JSON.stringify({ __type: 'null' }),
469
- undefined: (data) => JSON.stringify({ __type: 'undefined' }),
513
+ null: () => JSON.stringify({ __type: 'null' }),
514
+ undefined: () => JSON.stringify({ __type: 'undefined' }),
470
515
  map: (data) =>
471
516
  JSON.stringify({
472
517
  __type: 'map',
@@ -491,7 +536,7 @@ class TinyCrypto {
491
536
  * Each key corresponds to a specific data type (e.g., 'Date', 'BigInt', 'Buffer', etc.),
492
537
  * and the value is a function that deserializes the value to its original format.
493
538
  *
494
- * @type {Object}
539
+ * @type {Record<string, (data: any) => any>}
495
540
  * @property {Function} regexp - Deserializes a regular expression from its string representation (e.g., `/pattern/flags`).
496
541
  * @property {Function} htmlelement - Deserializes an HTML element from its serialized outerHTML string (only works in browser environments).
497
542
  * @property {Function} date - Deserializes a date from its ISO string representation.
@@ -518,21 +563,21 @@ class TinyCrypto {
518
563
  throw new Error('HTMLElement deserialization is only supported in browsers');
519
564
  const div = document.createElement('div');
520
565
  div.innerHTML = value;
521
- return div.firstElementChild;
566
+ return div;
522
567
  },
523
568
  date: (value) => new Date(value),
524
569
  bigint: (value) => BigInt(value),
525
570
  number: (value) => Number(value),
526
571
  boolean: (value) => Boolean(value),
527
- null: (value) => null,
528
- undefined: (value) => undefined,
572
+ null: () => null,
573
+ undefined: () => undefined,
529
574
  map: (value) => new Map(value),
530
575
  set: (value) => new Set(value),
531
576
  symbol: (value) => Symbol(value),
532
577
  array: (value) => value,
533
578
  object: (value) => value,
534
579
  string: (value) => String(value),
535
- buffer: (value) => Buffer.from(value, 'base64'),
580
+ buffer: (value) => buffer.Buffer.from(value, 'base64'),
536
581
  };
537
582
 
538
583
  /**
@@ -546,7 +591,8 @@ class TinyCrypto {
546
591
  */
547
592
  #serialize(data) {
548
593
  const type = objFilter.objType(data) || 'undefined';
549
- if (this.#valueConvertTypes[type]) return this.#valueConvertTypes[type](data);
594
+ if (typeof type === 'string' && this.#valueConvertTypes[type])
595
+ return this.#valueConvertTypes[type](data);
550
596
  throw new Error(`Unsupported data type for encryption: ${type}`);
551
597
  }
552
598
 
@@ -13,8 +13,8 @@ declare class TinyCrypto {
13
13
  *
14
14
  * @param {Object} [options={}] - Configuration options for encryption and decryption.
15
15
  * @param {string} [options.algorithm='aes-256-gcm'] - The encryption algorithm to use. Recommended: 'aes-256-gcm' for authenticated encryption.
16
- * @param {string} [options.outputEncoding='hex'] - The encoding used when returning encrypted data (e.g., 'hex', 'base64').
17
- * @param {string} [options.inputEncoding='utf8'] - The encoding used for plaintext inputs (e.g., 'utf8').
16
+ * @param {BufferEncoding} [options.outputEncoding='hex'] - The encoding used when returning encrypted data (e.g., 'hex', 'base64').
17
+ * @param {BufferEncoding} [options.inputEncoding='utf8'] - The encoding used for plaintext inputs (e.g., 'utf8').
18
18
  * @param {number} [options.authTagLength=16] - The length of the authentication tag used in GCM mode. Usually 16 for AES-256-GCM.
19
19
  * @param {Buffer} [options.key] - Optional 32-byte cryptographic key. If not provided, a random key is generated.
20
20
  *
@@ -24,21 +24,23 @@ declare class TinyCrypto {
24
24
  * const crypto = new CryptoManager({
25
25
  * algorithm: 'aes-256-gcm',
26
26
  * outputEncoding: 'base64',
27
- * key: crypto.randomBytes(32),
27
+ * key: randomBytes(32),
28
28
  * });
29
29
  */
30
30
  constructor(options?: {
31
31
  algorithm?: string | undefined;
32
- outputEncoding?: string | undefined;
33
- inputEncoding?: string | undefined;
32
+ outputEncoding?: BufferEncoding | undefined;
33
+ inputEncoding?: BufferEncoding | undefined;
34
34
  authTagLength?: number | undefined;
35
35
  key?: Buffer<ArrayBufferLike> | undefined;
36
36
  });
37
37
  algorithm: string;
38
- outputEncoding: string;
39
- inputEncoding: string;
40
38
  authTagLength: number;
41
39
  key: Buffer<ArrayBufferLike>;
40
+ /** @type {BufferEncoding} */
41
+ outputEncoding: BufferEncoding;
42
+ /** @type {BufferEncoding} */
43
+ inputEncoding: BufferEncoding;
42
44
  /**
43
45
  * Generates a secure random cryptographic key.
44
46
  *
@@ -68,10 +70,7 @@ declare class TinyCrypto {
68
70
  *
69
71
  * @param {*} data - The data to encrypt. Can be of any supported type (string, number, boolean, Date, JSON, etc.).
70
72
  * @param {Buffer} [iv=this.generateIV()] - Optional Initialization Vector (IV). If not provided, a secure random IV is generated.
71
- * @returns {Object} An object containing:
72
- * - `iv` {string} - The IV used, encoded with the output encoding.
73
- * - `encrypted` {string} - The encrypted payload.
74
- * - `authTag` {string} - The authentication tag used to verify the integrity of the ciphertext.
73
+ * @returns {EncryptedDataParams} An object containing the encrypted data.
75
74
  *
76
75
  * @example
77
76
  * const result = cryptoManager.encrypt('Hello, world!');
@@ -81,17 +80,33 @@ declare class TinyCrypto {
81
80
  * // authTag: 'aa93...'
82
81
  * // }
83
82
  */
84
- encrypt(data: any, iv?: Buffer): Object;
83
+ encrypt(data: any, iv?: Buffer): {
84
+ /**
85
+ * - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
86
+ */
87
+ iv: string;
88
+ /**
89
+ * - The encrypted data to decrypt, encoded with the output encoding.
90
+ */
91
+ encrypted: string;
92
+ /**
93
+ * - The authentication tag used to verify the integrity of the encrypted data.
94
+ */
95
+ authTag: string;
96
+ };
97
+ /**
98
+ * @typedef {Object} EncryptedDataParams
99
+ * @property {string} iv - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
100
+ * @property {string} encrypted - The encrypted data to decrypt, encoded with the output encoding.
101
+ * @property {string} authTag - The authentication tag used to verify the integrity of the encrypted data.
102
+ */
85
103
  /**
86
104
  * Decrypts a previously encrypted value.
87
105
  *
88
106
  * The method checks the integrity of the data using the authentication tag (`authTag`) and ensures the data is properly decrypted.
89
107
  * After decryption, it automatically deserializes the data back to its original type.
90
108
  *
91
- * @param {Object} params - An object containing the encrypted data:
92
- * - `iv` {string} - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
93
- * - `encrypted` {string} - The encrypted data to decrypt, encoded with the output encoding.
94
- * - `authTag` {string} - The authentication tag used to verify the integrity of the encrypted data.
109
+ * @param {EncryptedDataParams} params - An object containing the encrypted data.
95
110
  * @param {string|null} [expectedType=null] - Optionally specify the expected type of the decrypted data. If provided, the method will validate the type of the deserialized value.
96
111
  * @returns {*} The decrypted value, which will be the original type of the data before encryption.
97
112
  * @throws {Error} Throws if the authentication tag doesn't match or the data has been tampered with.
@@ -106,17 +121,27 @@ declare class TinyCrypto {
106
121
  * const decrypted = cryptoManager.decrypt(encryptedData, 'string');
107
122
  * console.log(decrypted); // Outputs: 'Hello, world!'
108
123
  */
109
- decrypt({ iv, encrypted, authTag }: Object, expectedType?: string | null): any;
124
+ decrypt({ iv, encrypted, authTag }: {
125
+ /**
126
+ * - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
127
+ */
128
+ iv: string;
129
+ /**
130
+ * - The encrypted data to decrypt, encoded with the output encoding.
131
+ */
132
+ encrypted: string;
133
+ /**
134
+ * - The authentication tag used to verify the integrity of the encrypted data.
135
+ */
136
+ authTag: string;
137
+ }, expectedType?: string | null): any;
110
138
  /**
111
139
  * Retrieves the type of the original data from an encrypted object.
112
140
  *
113
141
  * This method decrypts the encrypted data and extracts its type information without fully deserializing the value.
114
142
  * It is useful when you need to verify the type of the encrypted data before fully decrypting it.
115
143
  *
116
- * @param {Object} params - An object containing the encrypted data:
117
- * - `iv` {string} - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
118
- * - `encrypted` {string} - The encrypted data to decrypt, encoded with the output encoding.
119
- * - `authTag` {string} - The authentication tag used to verify the integrity of the encrypted data.
144
+ * @param {EncryptedDataParams} params - An object containing the encrypted data.
120
145
  * @returns {string} The type of the original data (e.g., 'string', 'number', 'date', etc.).
121
146
  *
122
147
  * @example
@@ -128,7 +153,20 @@ declare class TinyCrypto {
128
153
  * const dataType = cryptoManager.getTypeFromEncrypted(encryptedData);
129
154
  * console.log(dataType); // Outputs: 'string'
130
155
  */
131
- getTypeFromEncrypted({ iv, encrypted, authTag }: Object): string;
156
+ getTypeFromEncrypted({ iv, encrypted, authTag }: {
157
+ /**
158
+ * - The Initialization Vector (IV) used in encryption, encoded with the output encoding.
159
+ */
160
+ iv: string;
161
+ /**
162
+ * - The encrypted data to decrypt, encoded with the output encoding.
163
+ */
164
+ encrypted: string;
165
+ /**
166
+ * - The authentication tag used to verify the integrity of the encrypted data.
167
+ */
168
+ authTag: string;
169
+ }): string;
132
170
  /**
133
171
  * Saves the cryptographic key to a file.
134
172
  *
@@ -240,8 +278,8 @@ declare class TinyCrypto {
240
278
  *
241
279
  * @param {Object} config - The configuration object to import.
242
280
  * @param {string} config.algorithm - The encryption algorithm (e.g., 'aes-256-gcm').
243
- * @param {string} config.outputEncoding - The output encoding format (e.g., 'hex').
244
- * @param {string} config.inputEncoding - The input encoding format (e.g., 'utf8').
281
+ * @param {BufferEncoding} config.outputEncoding - The output encoding format (e.g., 'hex').
282
+ * @param {BufferEncoding} config.inputEncoding - The input encoding format (e.g., 'utf8').
245
283
  * @param {number} config.authTagLength - The authentication tag length (e.g., 16).
246
284
  * @param {string} config.key - The cryptographic key in hexadecimal string format.
247
285
  *
@@ -258,10 +296,11 @@ declare class TinyCrypto {
258
296
  */
259
297
  importConfig(config: {
260
298
  algorithm: string;
261
- outputEncoding: string;
262
- inputEncoding: string;
299
+ outputEncoding: BufferEncoding;
300
+ inputEncoding: BufferEncoding;
263
301
  authTagLength: number;
264
302
  key: string;
265
303
  }): void;
266
304
  #private;
267
305
  }
306
+ import { Buffer } from 'buffer';