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.
- package/README.md +83 -307
- package/dist/befly.config.d.ts +7 -0
- package/{befly.config.ts → dist/befly.config.js} +10 -33
- package/dist/checks/checkApi.d.ts +1 -0
- package/{checks/checkApi.ts → dist/checks/checkApi.js} +11 -28
- package/dist/checks/checkHook.d.ts +1 -0
- package/{checks/checkHook.ts → dist/checks/checkHook.js} +11 -20
- package/dist/checks/checkMenu.d.ts +7 -0
- package/{checks/checkMenu.ts → dist/checks/checkMenu.js} +18 -53
- package/dist/checks/checkPlugin.d.ts +1 -0
- package/{checks/checkPlugin.ts → dist/checks/checkPlugin.js} +11 -20
- package/dist/checks/checkTable.d.ts +6 -0
- package/{checks/checkTable.ts → dist/checks/checkTable.js} +17 -41
- package/dist/configs/presetFields.d.ts +4 -0
- package/{configs/presetFields.ts → dist/configs/presetFields.js} +1 -1
- package/dist/configs/presetRegexp.d.ts +145 -0
- package/{utils/regex.ts → dist/configs/presetRegexp.js} +8 -31
- package/dist/hooks/auth.d.ts +5 -0
- package/{hooks/auth.ts → dist/hooks/auth.js} +6 -10
- package/dist/hooks/cors.d.ts +9 -0
- package/{hooks/cors.ts → dist/hooks/cors.js} +3 -13
- package/dist/hooks/parser.d.ts +12 -0
- package/{hooks/parser.ts → dist/hooks/parser.js} +29 -44
- package/dist/hooks/permission.d.ts +12 -0
- package/{hooks/permission.ts → dist/hooks/permission.js} +14 -25
- package/dist/hooks/validator.d.ts +9 -0
- package/{hooks/validator.ts → dist/hooks/validator.js} +7 -14
- package/dist/lib/asyncContext.d.ts +21 -0
- package/dist/lib/asyncContext.js +27 -0
- package/dist/lib/cacheHelper.d.ts +95 -0
- package/{lib/cacheHelper.ts → dist/lib/cacheHelper.js} +45 -105
- package/dist/lib/cacheKeys.d.ts +23 -0
- package/{lib/cacheKeys.ts → dist/lib/cacheKeys.js} +5 -10
- package/dist/lib/cipher.d.ts +153 -0
- package/{lib/cipher.ts → dist/lib/cipher.js} +23 -44
- package/dist/lib/connect.d.ts +91 -0
- package/{lib/connect.ts → dist/lib/connect.js} +47 -88
- package/dist/lib/dbDialect.d.ts +87 -0
- package/{lib/dbDialect.ts → dist/lib/dbDialect.js} +32 -112
- package/dist/lib/dbHelper.d.ts +204 -0
- package/{lib/dbHelper.ts → dist/lib/dbHelper.js} +83 -240
- package/dist/lib/dbUtils.d.ts +68 -0
- package/{lib/dbUtils.ts → dist/lib/dbUtils.js} +51 -125
- package/dist/lib/jwt.d.ts +13 -0
- package/{lib/jwt.ts → dist/lib/jwt.js} +11 -32
- package/dist/lib/logger.d.ts +32 -0
- package/{lib/logger.ts → dist/lib/logger.js} +202 -279
- package/dist/lib/redisHelper.d.ts +185 -0
- package/{lib/redisHelper.ts → dist/lib/redisHelper.js} +97 -141
- package/dist/lib/sqlBuilder.d.ts +160 -0
- package/{lib/sqlBuilder.ts → dist/lib/sqlBuilder.js} +132 -278
- package/dist/lib/sqlCheck.d.ts +23 -0
- package/{lib/sqlCheck.ts → dist/lib/sqlCheck.js} +24 -41
- package/dist/lib/validator.d.ts +45 -0
- package/{lib/validator.ts → dist/lib/validator.js} +44 -61
- package/dist/loader/loadApis.d.ts +12 -0
- package/{loader/loadApis.ts → dist/loader/loadApis.js} +9 -19
- package/dist/loader/loadHooks.d.ts +8 -0
- package/{loader/loadHooks.ts → dist/loader/loadHooks.js} +7 -21
- package/dist/loader/loadPlugins.d.ts +8 -0
- package/{loader/loadPlugins.ts → dist/loader/loadPlugins.js} +10 -22
- package/dist/main.d.ts +26 -0
- package/{main.ts → dist/main.js} +60 -99
- package/dist/paths.d.ts +93 -0
- package/{paths.ts → dist/paths.js} +6 -19
- package/dist/plugins/cache.d.ts +14 -0
- package/{plugins/cache.ts → dist/plugins/cache.js} +5 -12
- package/dist/plugins/cipher.d.ts +10 -0
- package/{plugins/cipher.ts → dist/plugins/cipher.js} +2 -6
- package/dist/plugins/config.d.ts +10 -0
- package/dist/plugins/config.js +6 -0
- package/dist/plugins/db.d.ts +14 -0
- package/{plugins/db.ts → dist/plugins/db.js} +9 -17
- package/dist/plugins/jwt.d.ts +10 -0
- package/dist/plugins/jwt.js +10 -0
- package/dist/plugins/logger.d.ts +28 -0
- package/{plugins/logger.ts → dist/plugins/logger.js} +3 -8
- package/dist/plugins/redis.d.ts +14 -0
- package/{plugins/redis.ts → dist/plugins/redis.js} +7 -12
- package/dist/plugins/tool.d.ts +79 -0
- package/{plugins/tool.ts → dist/plugins/tool.js} +7 -30
- package/dist/router/api.d.ts +14 -0
- package/dist/router/api.js +107 -0
- package/dist/router/static.d.ts +9 -0
- package/{router/static.ts → dist/router/static.js} +20 -34
- package/dist/scripts/ensureDist.d.ts +1 -0
- package/dist/scripts/ensureDist.js +80 -0
- package/dist/sync/syncApi.d.ts +3 -0
- package/{sync/syncApi.ts → dist/sync/syncApi.js} +34 -54
- package/dist/sync/syncCache.d.ts +2 -0
- package/{sync/syncCache.ts → dist/sync/syncCache.js} +1 -6
- package/dist/sync/syncDev.d.ts +6 -0
- package/{sync/syncDev.ts → dist/sync/syncDev.js} +29 -62
- package/dist/sync/syncMenu.d.ts +14 -0
- package/{sync/syncMenu.ts → dist/sync/syncMenu.js} +65 -125
- package/dist/sync/syncTable.d.ts +151 -0
- package/{sync/syncTable.ts → dist/sync/syncTable.js} +171 -378
- package/{types → dist/types}/api.d.ts +8 -47
- package/dist/types/api.js +4 -0
- package/{types → dist/types}/befly.d.ts +31 -222
- package/dist/types/befly.js +4 -0
- package/{types → dist/types}/cache.d.ts +7 -15
- package/dist/types/cache.js +4 -0
- package/dist/types/cipher.d.ts +27 -0
- package/dist/types/cipher.js +7 -0
- package/{types → dist/types}/common.d.ts +8 -33
- package/dist/types/common.js +5 -0
- package/{types → dist/types}/context.d.ts +2 -4
- package/dist/types/context.js +4 -0
- package/{types → dist/types}/crypto.d.ts +0 -3
- package/dist/types/crypto.js +4 -0
- package/dist/types/database.d.ts +138 -0
- package/dist/types/database.js +4 -0
- package/dist/types/hook.d.ts +15 -0
- package/dist/types/hook.js +6 -0
- package/dist/types/jwt.d.ts +75 -0
- package/dist/types/jwt.js +4 -0
- package/dist/types/logger.d.ts +47 -0
- package/dist/types/logger.js +6 -0
- package/dist/types/plugin.d.ts +14 -0
- package/dist/types/plugin.js +6 -0
- package/dist/types/redis.d.ts +71 -0
- package/dist/types/redis.js +4 -0
- package/{types/roleApisCache.ts → dist/types/roleApisCache.d.ts} +0 -2
- package/dist/types/roleApisCache.js +8 -0
- package/dist/types/sync.d.ts +92 -0
- package/dist/types/sync.js +4 -0
- package/dist/types/table.d.ts +34 -0
- package/dist/types/table.js +4 -0
- package/dist/types/validate.d.ts +67 -0
- package/dist/types/validate.js +4 -0
- package/dist/utils/arrayKeysToCamel.d.ts +13 -0
- package/{utils/arrayKeysToCamel.ts → dist/utils/arrayKeysToCamel.js} +5 -5
- package/dist/utils/calcPerfTime.d.ts +4 -0
- package/{utils/calcPerfTime.ts → dist/utils/calcPerfTime.js} +3 -3
- package/dist/utils/configTypes.d.ts +1 -0
- package/dist/utils/configTypes.js +1 -0
- package/dist/utils/convertBigIntFields.d.ts +11 -0
- package/{utils/convertBigIntFields.ts → dist/utils/convertBigIntFields.js} +5 -9
- package/dist/utils/cors.d.ts +8 -0
- package/{utils/cors.ts → dist/utils/cors.js} +1 -3
- package/dist/utils/disableMenusGlob.d.ts +13 -0
- package/{utils/disableMenusGlob.ts → dist/utils/disableMenusGlob.js} +9 -29
- package/dist/utils/fieldClear.d.ts +11 -0
- package/{utils/fieldClear.ts → dist/utils/fieldClear.js} +15 -33
- package/dist/utils/genShortId.d.ts +10 -0
- package/{utils/genShortId.ts → dist/utils/genShortId.js} +1 -1
- package/dist/utils/getClientIp.d.ts +6 -0
- package/{utils/getClientIp.ts → dist/utils/getClientIp.js} +1 -7
- package/dist/utils/importDefault.d.ts +1 -0
- package/dist/utils/importDefault.js +29 -0
- package/dist/utils/isDirentDirectory.d.ts +2 -0
- package/{utils/isDirentDirectory.ts → dist/utils/isDirentDirectory.js} +3 -8
- package/dist/utils/keysToCamel.d.ts +10 -0
- package/{utils/keysToCamel.ts → dist/utils/keysToCamel.js} +4 -5
- package/dist/utils/keysToSnake.d.ts +10 -0
- package/{utils/keysToSnake.ts → dist/utils/keysToSnake.js} +4 -5
- package/dist/utils/loadMenuConfigs.d.ts +5 -0
- package/{utils/loadMenuConfigs.ts → dist/utils/loadMenuConfigs.js} +24 -51
- package/dist/utils/pickFields.d.ts +4 -0
- package/{utils/pickFields.ts → dist/utils/pickFields.js} +2 -5
- package/dist/utils/process.d.ts +24 -0
- package/{utils/process.ts → dist/utils/process.js} +2 -18
- package/dist/utils/processFields.d.ts +4 -0
- package/{utils/processFields.ts → dist/utils/processFields.js} +5 -9
- package/dist/utils/regex.d.ts +145 -0
- package/{configs/presetRegexp.ts → dist/utils/regex.js} +8 -31
- package/dist/utils/response.d.ts +20 -0
- package/{utils/response.ts → dist/utils/response.js} +28 -49
- package/dist/utils/scanAddons.d.ts +17 -0
- package/{utils/scanAddons.ts → dist/utils/scanAddons.js} +6 -40
- package/dist/utils/scanConfig.d.ts +26 -0
- package/{utils/scanConfig.ts → dist/utils/scanConfig.js} +22 -59
- package/dist/utils/scanFiles.d.ts +30 -0
- package/{utils/scanFiles.ts → dist/utils/scanFiles.js} +26 -66
- package/dist/utils/scanSources.d.ts +10 -0
- package/dist/utils/scanSources.js +41 -0
- package/dist/utils/sortModules.d.ts +28 -0
- package/{utils/sortModules.ts → dist/utils/sortModules.js} +25 -65
- package/dist/utils/sqlLog.d.ts +14 -0
- package/{utils/sqlLog.ts → dist/utils/sqlLog.js} +2 -14
- package/package.json +14 -28
- package/.gitignore +0 -0
- package/bunfig.toml +0 -3
- package/docs/README.md +0 -98
- package/docs/api/api.md +0 -1921
- package/docs/guide/examples.md +0 -926
- package/docs/guide/quickstart.md +0 -354
- package/docs/hooks/auth.md +0 -38
- package/docs/hooks/cors.md +0 -28
- package/docs/hooks/hook.md +0 -838
- package/docs/hooks/parser.md +0 -19
- package/docs/hooks/rateLimit.md +0 -47
- package/docs/infra/redis.md +0 -628
- package/docs/plugins/cipher.md +0 -61
- package/docs/plugins/database.md +0 -189
- package/docs/plugins/plugin.md +0 -986
- package/docs/reference/addon.md +0 -510
- package/docs/reference/config.md +0 -573
- package/docs/reference/logger.md +0 -495
- package/docs/reference/sync.md +0 -478
- package/docs/reference/table.md +0 -763
- package/docs/reference/validator.md +0 -620
- package/lib/asyncContext.ts +0 -43
- package/plugins/config.ts +0 -13
- package/plugins/jwt.ts +0 -15
- package/router/api.ts +0 -130
- package/tsconfig.json +0 -8
- package/types/database.d.ts +0 -541
- package/types/hook.d.ts +0 -25
- package/types/jwt.d.ts +0 -118
- package/types/logger.d.ts +0 -65
- package/types/plugin.d.ts +0 -19
- package/types/redis.d.ts +0 -83
- package/types/sync.d.ts +0 -398
- package/types/table.d.ts +0 -216
- package/types/validate.d.ts +0 -69
- package/utils/configTypes.ts +0 -3
- package/utils/importDefault.ts +0 -21
- 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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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)
|
|
155
|
+
if (done)
|
|
156
|
+
break;
|
|
173
157
|
hasher.update(value);
|
|
174
158
|
}
|
|
175
159
|
return hasher.digest(encoding);
|
|
176
|
-
}
|
|
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
|
|
177
|
+
static async hashPassword(password, options = {}) {
|
|
194
178
|
const finalOptions = {
|
|
195
179
|
algorithm: "bcrypt",
|
|
196
180
|
...options
|
|
197
|
-
}
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
+
}
|