befly 3.10.18 → 3.10.19

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 (220) hide show
  1. package/README.md +83 -307
  2. package/dist/befly.config.d.ts +7 -0
  3. package/{befly.config.ts → dist/befly.config.js} +10 -33
  4. package/dist/checks/checkApi.d.ts +1 -0
  5. package/{checks/checkApi.ts → dist/checks/checkApi.js} +11 -28
  6. package/dist/checks/checkHook.d.ts +1 -0
  7. package/{checks/checkHook.ts → dist/checks/checkHook.js} +11 -20
  8. package/dist/checks/checkMenu.d.ts +7 -0
  9. package/{checks/checkMenu.ts → dist/checks/checkMenu.js} +18 -53
  10. package/dist/checks/checkPlugin.d.ts +1 -0
  11. package/{checks/checkPlugin.ts → dist/checks/checkPlugin.js} +11 -20
  12. package/dist/checks/checkTable.d.ts +6 -0
  13. package/{checks/checkTable.ts → dist/checks/checkTable.js} +17 -41
  14. package/dist/configs/presetFields.d.ts +4 -0
  15. package/{configs/presetFields.ts → dist/configs/presetFields.js} +1 -1
  16. package/dist/configs/presetRegexp.d.ts +145 -0
  17. package/{utils/regex.ts → dist/configs/presetRegexp.js} +8 -31
  18. package/dist/hooks/auth.d.ts +5 -0
  19. package/{hooks/auth.ts → dist/hooks/auth.js} +6 -10
  20. package/dist/hooks/cors.d.ts +9 -0
  21. package/{hooks/cors.ts → dist/hooks/cors.js} +3 -13
  22. package/dist/hooks/parser.d.ts +12 -0
  23. package/{hooks/parser.ts → dist/hooks/parser.js} +29 -44
  24. package/dist/hooks/permission.d.ts +12 -0
  25. package/{hooks/permission.ts → dist/hooks/permission.js} +14 -25
  26. package/dist/hooks/validator.d.ts +9 -0
  27. package/{hooks/validator.ts → dist/hooks/validator.js} +7 -14
  28. package/dist/lib/asyncContext.d.ts +21 -0
  29. package/dist/lib/asyncContext.js +27 -0
  30. package/dist/lib/cacheHelper.d.ts +95 -0
  31. package/{lib/cacheHelper.ts → dist/lib/cacheHelper.js} +45 -105
  32. package/dist/lib/cacheKeys.d.ts +23 -0
  33. package/{lib/cacheKeys.ts → dist/lib/cacheKeys.js} +5 -10
  34. package/dist/lib/cipher.d.ts +153 -0
  35. package/{lib/cipher.ts → dist/lib/cipher.js} +23 -44
  36. package/dist/lib/connect.d.ts +91 -0
  37. package/{lib/connect.ts → dist/lib/connect.js} +47 -88
  38. package/dist/lib/dbDialect.d.ts +87 -0
  39. package/{lib/dbDialect.ts → dist/lib/dbDialect.js} +32 -112
  40. package/dist/lib/dbHelper.d.ts +204 -0
  41. package/{lib/dbHelper.ts → dist/lib/dbHelper.js} +83 -240
  42. package/dist/lib/dbUtils.d.ts +68 -0
  43. package/{lib/dbUtils.ts → dist/lib/dbUtils.js} +51 -125
  44. package/dist/lib/jwt.d.ts +13 -0
  45. package/{lib/jwt.ts → dist/lib/jwt.js} +11 -32
  46. package/dist/lib/logger.d.ts +32 -0
  47. package/{lib/logger.ts → dist/lib/logger.js} +202 -279
  48. package/dist/lib/redisHelper.d.ts +185 -0
  49. package/{lib/redisHelper.ts → dist/lib/redisHelper.js} +97 -141
  50. package/dist/lib/sqlBuilder.d.ts +160 -0
  51. package/{lib/sqlBuilder.ts → dist/lib/sqlBuilder.js} +132 -278
  52. package/dist/lib/sqlCheck.d.ts +23 -0
  53. package/{lib/sqlCheck.ts → dist/lib/sqlCheck.js} +24 -41
  54. package/dist/lib/validator.d.ts +45 -0
  55. package/{lib/validator.ts → dist/lib/validator.js} +44 -61
  56. package/dist/loader/loadApis.d.ts +12 -0
  57. package/{loader/loadApis.ts → dist/loader/loadApis.js} +9 -19
  58. package/dist/loader/loadHooks.d.ts +8 -0
  59. package/{loader/loadHooks.ts → dist/loader/loadHooks.js} +7 -21
  60. package/dist/loader/loadPlugins.d.ts +8 -0
  61. package/{loader/loadPlugins.ts → dist/loader/loadPlugins.js} +10 -22
  62. package/dist/main.d.ts +26 -0
  63. package/{main.ts → dist/main.js} +60 -99
  64. package/dist/paths.d.ts +93 -0
  65. package/{paths.ts → dist/paths.js} +6 -19
  66. package/dist/plugins/cache.d.ts +14 -0
  67. package/{plugins/cache.ts → dist/plugins/cache.js} +5 -12
  68. package/dist/plugins/cipher.d.ts +10 -0
  69. package/{plugins/cipher.ts → dist/plugins/cipher.js} +2 -6
  70. package/dist/plugins/config.d.ts +10 -0
  71. package/dist/plugins/config.js +6 -0
  72. package/dist/plugins/db.d.ts +14 -0
  73. package/{plugins/db.ts → dist/plugins/db.js} +9 -17
  74. package/dist/plugins/jwt.d.ts +10 -0
  75. package/dist/plugins/jwt.js +10 -0
  76. package/dist/plugins/logger.d.ts +28 -0
  77. package/{plugins/logger.ts → dist/plugins/logger.js} +3 -8
  78. package/dist/plugins/redis.d.ts +14 -0
  79. package/{plugins/redis.ts → dist/plugins/redis.js} +7 -12
  80. package/dist/plugins/tool.d.ts +79 -0
  81. package/{plugins/tool.ts → dist/plugins/tool.js} +7 -30
  82. package/dist/router/api.d.ts +14 -0
  83. package/dist/router/api.js +107 -0
  84. package/dist/router/static.d.ts +9 -0
  85. package/{router/static.ts → dist/router/static.js} +20 -34
  86. package/dist/scripts/ensureDist.d.ts +1 -0
  87. package/dist/scripts/ensureDist.js +80 -0
  88. package/dist/sync/syncApi.d.ts +3 -0
  89. package/{sync/syncApi.ts → dist/sync/syncApi.js} +34 -54
  90. package/dist/sync/syncCache.d.ts +2 -0
  91. package/{sync/syncCache.ts → dist/sync/syncCache.js} +1 -6
  92. package/dist/sync/syncDev.d.ts +6 -0
  93. package/{sync/syncDev.ts → dist/sync/syncDev.js} +29 -62
  94. package/dist/sync/syncMenu.d.ts +14 -0
  95. package/{sync/syncMenu.ts → dist/sync/syncMenu.js} +65 -125
  96. package/dist/sync/syncTable.d.ts +151 -0
  97. package/{sync/syncTable.ts → dist/sync/syncTable.js} +171 -378
  98. package/{types → dist/types}/api.d.ts +8 -47
  99. package/dist/types/api.js +4 -0
  100. package/{types → dist/types}/befly.d.ts +31 -222
  101. package/dist/types/befly.js +4 -0
  102. package/{types → dist/types}/cache.d.ts +7 -15
  103. package/dist/types/cache.js +4 -0
  104. package/dist/types/cipher.d.ts +27 -0
  105. package/dist/types/cipher.js +7 -0
  106. package/{types → dist/types}/common.d.ts +8 -33
  107. package/dist/types/common.js +5 -0
  108. package/{types → dist/types}/context.d.ts +2 -4
  109. package/dist/types/context.js +4 -0
  110. package/{types → dist/types}/crypto.d.ts +0 -3
  111. package/dist/types/crypto.js +4 -0
  112. package/dist/types/database.d.ts +138 -0
  113. package/dist/types/database.js +4 -0
  114. package/dist/types/hook.d.ts +15 -0
  115. package/dist/types/hook.js +6 -0
  116. package/dist/types/jwt.d.ts +75 -0
  117. package/dist/types/jwt.js +4 -0
  118. package/dist/types/logger.d.ts +47 -0
  119. package/dist/types/logger.js +6 -0
  120. package/dist/types/plugin.d.ts +14 -0
  121. package/dist/types/plugin.js +6 -0
  122. package/dist/types/redis.d.ts +71 -0
  123. package/dist/types/redis.js +4 -0
  124. package/{types/roleApisCache.ts → dist/types/roleApisCache.d.ts} +0 -2
  125. package/dist/types/roleApisCache.js +8 -0
  126. package/dist/types/sync.d.ts +92 -0
  127. package/dist/types/sync.js +4 -0
  128. package/dist/types/table.d.ts +34 -0
  129. package/dist/types/table.js +4 -0
  130. package/dist/types/validate.d.ts +67 -0
  131. package/dist/types/validate.js +4 -0
  132. package/dist/utils/arrayKeysToCamel.d.ts +13 -0
  133. package/{utils/arrayKeysToCamel.ts → dist/utils/arrayKeysToCamel.js} +5 -5
  134. package/dist/utils/calcPerfTime.d.ts +4 -0
  135. package/{utils/calcPerfTime.ts → dist/utils/calcPerfTime.js} +3 -3
  136. package/dist/utils/configTypes.d.ts +1 -0
  137. package/dist/utils/configTypes.js +1 -0
  138. package/dist/utils/convertBigIntFields.d.ts +11 -0
  139. package/{utils/convertBigIntFields.ts → dist/utils/convertBigIntFields.js} +5 -9
  140. package/dist/utils/cors.d.ts +8 -0
  141. package/{utils/cors.ts → dist/utils/cors.js} +1 -3
  142. package/dist/utils/disableMenusGlob.d.ts +13 -0
  143. package/{utils/disableMenusGlob.ts → dist/utils/disableMenusGlob.js} +9 -29
  144. package/dist/utils/fieldClear.d.ts +11 -0
  145. package/{utils/fieldClear.ts → dist/utils/fieldClear.js} +15 -33
  146. package/dist/utils/genShortId.d.ts +10 -0
  147. package/{utils/genShortId.ts → dist/utils/genShortId.js} +1 -1
  148. package/dist/utils/getClientIp.d.ts +6 -0
  149. package/{utils/getClientIp.ts → dist/utils/getClientIp.js} +1 -7
  150. package/dist/utils/importDefault.d.ts +1 -0
  151. package/dist/utils/importDefault.js +29 -0
  152. package/dist/utils/isDirentDirectory.d.ts +2 -0
  153. package/{utils/isDirentDirectory.ts → dist/utils/isDirentDirectory.js} +3 -8
  154. package/dist/utils/keysToCamel.d.ts +10 -0
  155. package/{utils/keysToCamel.ts → dist/utils/keysToCamel.js} +4 -5
  156. package/dist/utils/keysToSnake.d.ts +10 -0
  157. package/{utils/keysToSnake.ts → dist/utils/keysToSnake.js} +4 -5
  158. package/dist/utils/loadMenuConfigs.d.ts +5 -0
  159. package/{utils/loadMenuConfigs.ts → dist/utils/loadMenuConfigs.js} +24 -51
  160. package/dist/utils/pickFields.d.ts +4 -0
  161. package/{utils/pickFields.ts → dist/utils/pickFields.js} +2 -5
  162. package/dist/utils/process.d.ts +24 -0
  163. package/{utils/process.ts → dist/utils/process.js} +2 -18
  164. package/dist/utils/processFields.d.ts +4 -0
  165. package/{utils/processFields.ts → dist/utils/processFields.js} +5 -9
  166. package/dist/utils/regex.d.ts +145 -0
  167. package/{configs/presetRegexp.ts → dist/utils/regex.js} +8 -31
  168. package/dist/utils/response.d.ts +20 -0
  169. package/{utils/response.ts → dist/utils/response.js} +28 -49
  170. package/dist/utils/scanAddons.d.ts +17 -0
  171. package/{utils/scanAddons.ts → dist/utils/scanAddons.js} +6 -40
  172. package/dist/utils/scanConfig.d.ts +26 -0
  173. package/{utils/scanConfig.ts → dist/utils/scanConfig.js} +22 -59
  174. package/dist/utils/scanFiles.d.ts +30 -0
  175. package/{utils/scanFiles.ts → dist/utils/scanFiles.js} +26 -66
  176. package/dist/utils/scanSources.d.ts +10 -0
  177. package/dist/utils/scanSources.js +41 -0
  178. package/dist/utils/sortModules.d.ts +28 -0
  179. package/{utils/sortModules.ts → dist/utils/sortModules.js} +25 -65
  180. package/dist/utils/sqlLog.d.ts +14 -0
  181. package/{utils/sqlLog.ts → dist/utils/sqlLog.js} +2 -14
  182. package/package.json +14 -28
  183. package/.gitignore +0 -0
  184. package/bunfig.toml +0 -3
  185. package/docs/README.md +0 -98
  186. package/docs/api/api.md +0 -1921
  187. package/docs/guide/examples.md +0 -926
  188. package/docs/guide/quickstart.md +0 -354
  189. package/docs/hooks/auth.md +0 -38
  190. package/docs/hooks/cors.md +0 -28
  191. package/docs/hooks/hook.md +0 -838
  192. package/docs/hooks/parser.md +0 -19
  193. package/docs/hooks/rateLimit.md +0 -47
  194. package/docs/infra/redis.md +0 -628
  195. package/docs/plugins/cipher.md +0 -61
  196. package/docs/plugins/database.md +0 -189
  197. package/docs/plugins/plugin.md +0 -986
  198. package/docs/reference/addon.md +0 -510
  199. package/docs/reference/config.md +0 -573
  200. package/docs/reference/logger.md +0 -495
  201. package/docs/reference/sync.md +0 -478
  202. package/docs/reference/table.md +0 -763
  203. package/docs/reference/validator.md +0 -620
  204. package/lib/asyncContext.ts +0 -43
  205. package/plugins/config.ts +0 -13
  206. package/plugins/jwt.ts +0 -15
  207. package/router/api.ts +0 -130
  208. package/tsconfig.json +0 -8
  209. package/types/database.d.ts +0 -541
  210. package/types/hook.d.ts +0 -25
  211. package/types/jwt.d.ts +0 -118
  212. package/types/logger.d.ts +0 -65
  213. package/types/plugin.d.ts +0 -19
  214. package/types/redis.d.ts +0 -83
  215. package/types/sync.d.ts +0 -398
  216. package/types/table.d.ts +0 -216
  217. package/types/validate.d.ts +0 -69
  218. package/utils/configTypes.ts +0 -3
  219. package/utils/importDefault.ts +0 -21
  220. package/utils/scanSources.ts +0 -64
@@ -0,0 +1,153 @@
1
+ /**
2
+ * 加密工具类 - TypeScript 版本
3
+ * 提供各种哈希、HMAC、密码加密等功能
4
+ */
5
+ import type { EncodingType, HashAlgorithm, PasswordHashOptions } from "../types/crypto.ts";
6
+ /**
7
+ * 加密工具类
8
+ */
9
+ export declare class Cipher {
10
+ /**
11
+ * MD5 哈希
12
+ * @param data - 要哈希的数据
13
+ * @param encoding - 输出编码
14
+ * @returns MD5 哈希值
15
+ */
16
+ static md5(data: string | Uint8Array, encoding?: EncodingType): string;
17
+ /**
18
+ * HMAC-MD5 签名
19
+ * @param key - 密钥
20
+ * @param data - 要签名的数据
21
+ * @param encoding - 输出编码
22
+ * @returns HMAC-MD5 签名
23
+ */
24
+ static hmacMd5(key: string | Uint8Array, data: string | Uint8Array, encoding?: EncodingType): string;
25
+ /**
26
+ * SHA-1 哈希
27
+ * @param data - 要哈希的数据
28
+ * @param encoding - 输出编码
29
+ * @returns SHA-1 哈希值
30
+ */
31
+ static sha1(data: string | Uint8Array, encoding?: EncodingType): string;
32
+ /**
33
+ * HMAC-SHA1 签名
34
+ * @param key - 密钥
35
+ * @param data - 要签名的数据
36
+ * @param encoding - 输出编码
37
+ * @returns HMAC-SHA1 签名
38
+ */
39
+ static hmacSha1(key: string | Uint8Array, data: string | Uint8Array, encoding?: EncodingType): string;
40
+ /**
41
+ * SHA-256 哈希
42
+ * @param data - 要哈希的数据
43
+ * @param encoding - 输出编码
44
+ * @returns SHA-256 哈希值
45
+ */
46
+ static sha256(data: string | Uint8Array, encoding?: EncodingType): string;
47
+ /**
48
+ * RSA-SHA256 签名
49
+ * @param data - 要签名的数据
50
+ * @param privateKey - 私钥
51
+ * @param encoding - 输出编码('hex' | 'base64' | 'base64url')
52
+ * @returns RSA-SHA256 签名
53
+ */
54
+ static rsaSha256(data: string, privateKey: string | Buffer, encoding?: "hex" | "base64" | "base64url"): string;
55
+ /**
56
+ * HMAC-SHA256 签名
57
+ * @param key - 密钥
58
+ * @param data - 要签名的数据
59
+ * @param encoding - 输出编码
60
+ * @returns HMAC-SHA256 签名
61
+ */
62
+ static hmacSha256(key: string | Uint8Array, data: string | Uint8Array, encoding?: EncodingType): string;
63
+ /**
64
+ * SHA-512 哈希
65
+ * @param data - 要哈希的数据
66
+ * @param encoding - 输出编码
67
+ * @returns SHA-512 哈希值
68
+ */
69
+ static sha512(data: string | Uint8Array, encoding?: EncodingType): string;
70
+ /**
71
+ * HMAC-SHA512 签名
72
+ * @param key - 密钥
73
+ * @param data - 要签名的数据
74
+ * @param encoding - 输出编码
75
+ * @returns HMAC-SHA512 签名
76
+ */
77
+ static hmacSha512(key: string | Uint8Array, data: string | Uint8Array, encoding?: EncodingType): string;
78
+ /**
79
+ * 通用哈希方法
80
+ * @param algorithm - 算法名称
81
+ * @param data - 要哈希的数据
82
+ * @param encoding - 输出编码
83
+ * @returns 哈希值
84
+ */
85
+ static hash(algorithm: HashAlgorithm, data: string | Uint8Array, encoding?: EncodingType): string;
86
+ /**
87
+ * 通用 HMAC 方法
88
+ * @param algorithm - 算法名称
89
+ * @param key - 密钥
90
+ * @param data - 要签名的数据
91
+ * @param encoding - 输出编码
92
+ * @returns HMAC 签名
93
+ */
94
+ static hmac(algorithm: HashAlgorithm, key: string | Uint8Array, data: string | Uint8Array, encoding?: EncodingType): string;
95
+ /**
96
+ * 文件哈希
97
+ * @param filePath - 文件路径
98
+ * @param algorithm - 算法名称
99
+ * @param encoding - 输出编码
100
+ * @returns 文件哈希值
101
+ */
102
+ static hashFile(filePath: string, algorithm?: HashAlgorithm, encoding?: EncodingType): Promise<string>;
103
+ /**
104
+ * 密码哈希(使用 bcrypt 算法)
105
+ * @param password - 密码
106
+ * @param options - 选项(可选,支持自定义 cost 参数)
107
+ * @returns 哈希后的密码(固定 60 字符)
108
+ * @example
109
+ * // 默认配置(推荐)
110
+ * const hash = await Cipher.hashPassword('123456');
111
+ *
112
+ * // 自定义强度(可选)
113
+ * const hash = await Cipher.hashPassword('123456', { cost: 12 });
114
+ */
115
+ static hashPassword(password: string, options?: PasswordHashOptions): Promise<string>;
116
+ /**
117
+ * 验证密码
118
+ * @param password - 原始密码
119
+ * @param hash - 存储的哈希值(自动识别算法和提取盐值)
120
+ * @returns 验证结果
121
+ * @example
122
+ * const isValid = await Cipher.verifyPassword('123456', storedHash);
123
+ * if (isValid) {
124
+ * // 密码正确
125
+ * }
126
+ */
127
+ static verifyPassword(password: string, hash: string): Promise<boolean>;
128
+ /**
129
+ * Base64 编码
130
+ * @param data - 要编码的数据
131
+ * @returns Base64 编码的字符串
132
+ */
133
+ static base64Encode(data: string): string;
134
+ /**
135
+ * Base64 解码
136
+ * @param data - Base64 编码的字符串
137
+ * @returns 解码后的字符串
138
+ */
139
+ static base64Decode(data: string): string;
140
+ /**
141
+ * 生成随机十六进制字符串
142
+ * @param length - 字符串长度
143
+ * @returns 随机十六进制字符串
144
+ */
145
+ static randomString(length: number): string;
146
+ /**
147
+ * 快速哈希 (非密码学)
148
+ * @param data - 数据
149
+ * @param seed - 种子值
150
+ * @returns 64位哈希值
151
+ */
152
+ static fastHash(data: string | Uint8Array, seed?: number): number;
153
+ }
@@ -2,11 +2,7 @@
2
2
  * 加密工具类 - TypeScript 版本
3
3
  * 提供各种哈希、HMAC、密码加密等功能
4
4
  */
5
-
6
- import type { EncodingType, HashAlgorithm, PasswordHashOptions } from "../types/crypto.ts";
7
-
8
5
  import { createSign } from "node:crypto";
9
-
10
6
  /**
11
7
  * 加密工具类
12
8
  */
@@ -17,12 +13,11 @@ export class Cipher {
17
13
  * @param encoding - 输出编码
18
14
  * @returns MD5 哈希值
19
15
  */
20
- static md5(data: string | Uint8Array, encoding: EncodingType = "hex"): string {
16
+ static md5(data, encoding = "hex") {
21
17
  const hasher = new Bun.CryptoHasher("md5");
22
18
  hasher.update(data);
23
19
  return hasher.digest(encoding);
24
20
  }
25
-
26
21
  /**
27
22
  * HMAC-MD5 签名
28
23
  * @param key - 密钥
@@ -30,24 +25,22 @@ export class Cipher {
30
25
  * @param encoding - 输出编码
31
26
  * @returns HMAC-MD5 签名
32
27
  */
33
- static hmacMd5(key: string | Uint8Array, data: string | Uint8Array, encoding: EncodingType = "hex"): string {
28
+ static hmacMd5(key, data, encoding = "hex") {
34
29
  const hasher = new Bun.CryptoHasher("md5", key);
35
30
  hasher.update(data);
36
31
  return hasher.digest(encoding);
37
32
  }
38
-
39
33
  /**
40
34
  * SHA-1 哈希
41
35
  * @param data - 要哈希的数据
42
36
  * @param encoding - 输出编码
43
37
  * @returns SHA-1 哈希值
44
38
  */
45
- static sha1(data: string | Uint8Array, encoding: EncodingType = "hex"): string {
39
+ static sha1(data, encoding = "hex") {
46
40
  const hasher = new Bun.CryptoHasher("sha1");
47
41
  hasher.update(data);
48
42
  return hasher.digest(encoding);
49
43
  }
50
-
51
44
  /**
52
45
  * HMAC-SHA1 签名
53
46
  * @param key - 密钥
@@ -55,24 +48,22 @@ export class Cipher {
55
48
  * @param encoding - 输出编码
56
49
  * @returns HMAC-SHA1 签名
57
50
  */
58
- static hmacSha1(key: string | Uint8Array, data: string | Uint8Array, encoding: EncodingType = "hex"): string {
51
+ static hmacSha1(key, data, encoding = "hex") {
59
52
  const hasher = new Bun.CryptoHasher("sha1", key);
60
53
  hasher.update(data);
61
54
  return hasher.digest(encoding);
62
55
  }
63
-
64
56
  /**
65
57
  * SHA-256 哈希
66
58
  * @param data - 要哈希的数据
67
59
  * @param encoding - 输出编码
68
60
  * @returns SHA-256 哈希值
69
61
  */
70
- static sha256(data: string | Uint8Array, encoding: EncodingType = "hex"): string {
62
+ static sha256(data, encoding = "hex") {
71
63
  const hasher = new Bun.CryptoHasher("sha256");
72
64
  hasher.update(data);
73
65
  return hasher.digest(encoding);
74
66
  }
75
-
76
67
  /**
77
68
  * RSA-SHA256 签名
78
69
  * @param data - 要签名的数据
@@ -80,13 +71,12 @@ export class Cipher {
80
71
  * @param encoding - 输出编码('hex' | 'base64' | 'base64url')
81
72
  * @returns RSA-SHA256 签名
82
73
  */
83
- static rsaSha256(data: string, privateKey: string | Buffer, encoding: "hex" | "base64" | "base64url" = "hex"): string {
74
+ static rsaSha256(data, privateKey, encoding = "hex") {
84
75
  const sign = createSign("RSA-SHA256");
85
76
  sign.update(data);
86
77
  const signature = sign.sign(privateKey, encoding);
87
78
  return signature;
88
79
  }
89
-
90
80
  /**
91
81
  * HMAC-SHA256 签名
92
82
  * @param key - 密钥
@@ -94,24 +84,22 @@ export class Cipher {
94
84
  * @param encoding - 输出编码
95
85
  * @returns HMAC-SHA256 签名
96
86
  */
97
- static hmacSha256(key: string | Uint8Array, data: string | Uint8Array, encoding: EncodingType = "hex"): string {
87
+ static hmacSha256(key, data, encoding = "hex") {
98
88
  const hasher = new Bun.CryptoHasher("sha256", key);
99
89
  hasher.update(data);
100
90
  return hasher.digest(encoding);
101
91
  }
102
-
103
92
  /**
104
93
  * SHA-512 哈希
105
94
  * @param data - 要哈希的数据
106
95
  * @param encoding - 输出编码
107
96
  * @returns SHA-512 哈希值
108
97
  */
109
- static sha512(data: string | Uint8Array, encoding: EncodingType = "hex"): string {
98
+ static sha512(data, encoding = "hex") {
110
99
  const hasher = new Bun.CryptoHasher("sha512");
111
100
  hasher.update(data);
112
101
  return hasher.digest(encoding);
113
102
  }
114
-
115
103
  /**
116
104
  * HMAC-SHA512 签名
117
105
  * @param key - 密钥
@@ -119,12 +107,11 @@ export class Cipher {
119
107
  * @param encoding - 输出编码
120
108
  * @returns HMAC-SHA512 签名
121
109
  */
122
- static hmacSha512(key: string | Uint8Array, data: string | Uint8Array, encoding: EncodingType = "hex"): string {
110
+ static hmacSha512(key, data, encoding = "hex") {
123
111
  const hasher = new Bun.CryptoHasher("sha512", key);
124
112
  hasher.update(data);
125
113
  return hasher.digest(encoding);
126
114
  }
127
-
128
115
  /**
129
116
  * 通用哈希方法
130
117
  * @param algorithm - 算法名称
@@ -132,12 +119,11 @@ export class Cipher {
132
119
  * @param encoding - 输出编码
133
120
  * @returns 哈希值
134
121
  */
135
- static hash(algorithm: HashAlgorithm, data: string | Uint8Array, encoding: EncodingType = "hex"): string {
122
+ static hash(algorithm, data, encoding = "hex") {
136
123
  const hasher = new Bun.CryptoHasher(algorithm);
137
124
  hasher.update(data);
138
125
  return hasher.digest(encoding);
139
126
  }
140
-
141
127
  /**
142
128
  * 通用 HMAC 方法
143
129
  * @param algorithm - 算法名称
@@ -146,12 +132,11 @@ export class Cipher {
146
132
  * @param encoding - 输出编码
147
133
  * @returns HMAC 签名
148
134
  */
149
- static hmac(algorithm: HashAlgorithm, key: string | Uint8Array, data: string | Uint8Array, encoding: EncodingType = "hex"): string {
135
+ static hmac(algorithm, key, data, encoding = "hex") {
150
136
  const hasher = new Bun.CryptoHasher(algorithm, key);
151
137
  hasher.update(data);
152
138
  return hasher.digest(encoding);
153
139
  }
154
-
155
140
  /**
156
141
  * 文件哈希
157
142
  * @param filePath - 文件路径
@@ -159,25 +144,24 @@ export class Cipher {
159
144
  * @param encoding - 输出编码
160
145
  * @returns 文件哈希值
161
146
  */
162
- static async hashFile(filePath: string, algorithm: HashAlgorithm = "sha256", encoding: EncodingType = "hex"): Promise<string> {
147
+ static async hashFile(filePath, algorithm = "sha256", encoding = "hex") {
163
148
  const file = Bun.file(filePath);
164
149
  const hasher = new Bun.CryptoHasher(algorithm);
165
-
166
150
  const stream = file.stream();
167
151
  const reader = stream.getReader();
168
-
169
152
  try {
170
153
  while (true) {
171
154
  const { done, value } = await reader.read();
172
- if (done) break;
155
+ if (done)
156
+ break;
173
157
  hasher.update(value);
174
158
  }
175
159
  return hasher.digest(encoding);
176
- } finally {
160
+ }
161
+ finally {
177
162
  reader.releaseLock();
178
163
  }
179
164
  }
180
-
181
165
  /**
182
166
  * 密码哈希(使用 bcrypt 算法)
183
167
  * @param password - 密码
@@ -190,14 +174,13 @@ export class Cipher {
190
174
  * // 自定义强度(可选)
191
175
  * const hash = await Cipher.hashPassword('123456', { cost: 12 });
192
176
  */
193
- static async hashPassword(password: string, options: PasswordHashOptions = {}): Promise<string> {
177
+ static async hashPassword(password, options = {}) {
194
178
  const finalOptions = {
195
179
  algorithm: "bcrypt",
196
180
  ...options
197
- } as any;
181
+ };
198
182
  return await Bun.password.hash(password, finalOptions);
199
183
  }
200
-
201
184
  /**
202
185
  * 验证密码
203
186
  * @param password - 原始密码
@@ -209,34 +192,31 @@ export class Cipher {
209
192
  * // 密码正确
210
193
  * }
211
194
  */
212
- static async verifyPassword(password: string, hash: string): Promise<boolean> {
195
+ static async verifyPassword(password, hash) {
213
196
  return await Bun.password.verify(password, hash);
214
197
  }
215
-
216
198
  /**
217
199
  * Base64 编码
218
200
  * @param data - 要编码的数据
219
201
  * @returns Base64 编码的字符串
220
202
  */
221
- static base64Encode(data: string): string {
203
+ static base64Encode(data) {
222
204
  return Buffer.from(data, "utf8").toString("base64");
223
205
  }
224
-
225
206
  /**
226
207
  * Base64 解码
227
208
  * @param data - Base64 编码的字符串
228
209
  * @returns 解码后的字符串
229
210
  */
230
- static base64Decode(data: string): string {
211
+ static base64Decode(data) {
231
212
  return Buffer.from(data, "base64").toString("utf8");
232
213
  }
233
-
234
214
  /**
235
215
  * 生成随机十六进制字符串
236
216
  * @param length - 字符串长度
237
217
  * @returns 随机十六进制字符串
238
218
  */
239
- static randomString(length: number): string {
219
+ static randomString(length) {
240
220
  const bytes = Math.ceil(length / 2);
241
221
  const randomBytes = crypto.getRandomValues(new Uint8Array(bytes));
242
222
  let result = "";
@@ -245,14 +225,13 @@ export class Cipher {
245
225
  }
246
226
  return result.slice(0, length);
247
227
  }
248
-
249
228
  /**
250
229
  * 快速哈希 (非密码学)
251
230
  * @param data - 数据
252
231
  * @param seed - 种子值
253
232
  * @returns 64位哈希值
254
233
  */
255
- static fastHash(data: string | Uint8Array, seed: number = 0): number {
234
+ static fastHash(data, seed = 0) {
256
235
  const result = Bun.hash(data, seed);
257
236
  return typeof result === "bigint" ? Number(result) : result;
258
237
  }
@@ -0,0 +1,91 @@
1
+ /**
2
+ * 数据库连接管理器
3
+ * 统一管理 SQL 和 Redis 连接
4
+ */
5
+ import type { DatabaseConfig, RedisConfig } from "../types/befly.ts";
6
+ import { SQL, RedisClient } from "bun";
7
+ /**
8
+ * 数据库连接管理器
9
+ * 使用静态方法管理全局单例连接
10
+ */
11
+ export declare class Connect {
12
+ private static sqlClient;
13
+ private static redisClient;
14
+ private static sqlConnectedAt;
15
+ private static redisConnectedAt;
16
+ private static sqlPoolMax;
17
+ /**
18
+ * 连接 SQL 数据库
19
+ * @returns SQL 客户端实例
20
+ */
21
+ static connectSql(dbConfig: DatabaseConfig): Promise<SQL>;
22
+ /**
23
+ * 断开 SQL 连接
24
+ */
25
+ static disconnectSql(): Promise<void>;
26
+ /**
27
+ * 获取 SQL 客户端实例
28
+ * @throws 如果未连接则抛出错误
29
+ */
30
+ static getSql(): SQL;
31
+ /**
32
+ * 连接 Redis
33
+ * @returns Redis 客户端实例
34
+ */
35
+ static connectRedis(redisConfig: RedisConfig): Promise<RedisClient>;
36
+ /**
37
+ * 断开 Redis 连接
38
+ */
39
+ static disconnectRedis(): Promise<void>;
40
+ /**
41
+ * 获取 Redis 客户端实例
42
+ * @throws 如果未连接则抛出错误
43
+ */
44
+ static getRedis(): RedisClient;
45
+ /**
46
+ * 连接所有数据库(SQL + Redis)
47
+ */
48
+ static connect(config: {
49
+ db: DatabaseConfig;
50
+ redis: RedisConfig;
51
+ }): Promise<void>;
52
+ /**
53
+ * 断开所有数据库连接
54
+ */
55
+ static disconnect(): Promise<void>;
56
+ /**
57
+ * 检查连接状态
58
+ */
59
+ static isConnected(): {
60
+ sql: boolean;
61
+ redis: boolean;
62
+ };
63
+ /**
64
+ * 获取连接状态详细信息(用于监控和调试)
65
+ */
66
+ static getStatus(): {
67
+ sql: {
68
+ connected: boolean;
69
+ connectedAt: number | null;
70
+ uptime: number | null;
71
+ poolMax: number;
72
+ };
73
+ redis: {
74
+ connected: boolean;
75
+ connectedAt: number | null;
76
+ uptime: number | null;
77
+ };
78
+ };
79
+ /**
80
+ * 设置 mock SQL 客户端(仅用于测试)
81
+ */
82
+ static __setMockSql(mockClient: SQL): void;
83
+ /**
84
+ * 设置 mock Redis 客户端(仅用于测试)
85
+ */
86
+ static __setMockRedis(mockClient: RedisClient): void;
87
+ /**
88
+ * 重置所有连接状态(仅用于测试)
89
+ */
90
+ static __reset(): void;
91
+ }