befly 3.12.4 → 3.13.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.
- package/dist/befly.config.js +1 -1
- package/dist/befly.js +735 -882
- package/dist/befly.min.js +15 -18
- package/dist/checks/checkApi.js +14 -17
- package/dist/checks/checkHook.js +15 -18
- package/dist/checks/checkMenu.js +10 -10
- package/dist/checks/checkPlugin.js +15 -18
- package/dist/checks/checkTable.js +29 -28
- package/dist/hooks/auth.d.ts +3 -7
- package/dist/hooks/auth.js +2 -1
- package/dist/hooks/cors.d.ts +3 -7
- package/dist/hooks/cors.js +2 -1
- package/dist/hooks/parser.d.ts +3 -7
- package/dist/hooks/parser.js +2 -1
- package/dist/hooks/permission.d.ts +3 -7
- package/dist/hooks/permission.js +5 -3
- package/dist/hooks/validator.d.ts +3 -7
- package/dist/hooks/validator.js +2 -1
- package/dist/index.js +2 -2
- package/dist/lib/cacheHelper.js +8 -8
- package/dist/lib/connect.js +5 -5
- package/dist/lib/dbHelper.js +6 -5
- package/dist/lib/logger.d.ts +16 -17
- package/dist/lib/logger.js +335 -757
- package/dist/lib/redisHelper.js +27 -26
- package/dist/loader/loadApis.js +1 -1
- package/dist/loader/loadPlugins.js +1 -1
- package/dist/plugins/cache.d.ts +3 -9
- package/dist/plugins/cache.js +2 -1
- package/dist/plugins/cipher.d.ts +3 -8
- package/dist/plugins/cipher.js +2 -1
- package/dist/plugins/config.d.ts +3 -12
- package/dist/plugins/config.js +2 -1
- package/dist/plugins/db.d.ts +3 -9
- package/dist/plugins/db.js +3 -2
- package/dist/plugins/jwt.d.ts +3 -9
- package/dist/plugins/jwt.js +2 -1
- package/dist/plugins/logger.d.ts +3 -25
- package/dist/plugins/logger.js +2 -1
- package/dist/plugins/redis.d.ts +3 -9
- package/dist/plugins/redis.js +3 -2
- package/dist/plugins/tool.d.ts +3 -11
- package/dist/plugins/tool.js +2 -1
- package/dist/router/api.js +3 -2
- package/dist/router/static.js +1 -1
- package/dist/sync/syncApi.js +3 -3
- package/dist/sync/syncMenu.js +3 -2
- package/dist/sync/syncTable.js +2 -2
- package/dist/types/hook.d.ts +13 -0
- package/dist/types/hook.js +13 -0
- package/dist/types/logger.d.ts +20 -6
- package/dist/types/plugin.d.ts +12 -1
- package/dist/types/plugin.js +12 -1
- package/dist/utils/formatYmdHms.d.ts +1 -0
- package/dist/utils/formatYmdHms.js +20 -0
- package/dist/utils/importDefault.js +1 -1
- package/dist/utils/loadMenuConfigs.js +7 -6
- package/dist/utils/loggerUtils.d.ts +18 -0
- package/dist/utils/loggerUtils.js +167 -0
- package/dist/utils/response.js +6 -4
- package/dist/utils/sortModules.js +8 -7
- package/dist/utils/util.d.ts +2 -0
- package/dist/utils/util.js +16 -0
- package/package.json +2 -2
package/dist/hooks/parser.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Hook } from "../types/hook";
|
|
1
2
|
/**
|
|
2
3
|
* 请求参数解析钩子
|
|
3
4
|
* - GET 请求:解析 URL 查询参数
|
|
@@ -5,10 +6,5 @@
|
|
|
5
6
|
* - 根据 API 定义的 fields 过滤字段
|
|
6
7
|
* - rawBody: true 时跳过解析,由 handler 自行处理原始请求
|
|
7
8
|
*/
|
|
8
|
-
declare const
|
|
9
|
-
|
|
10
|
-
enable: true;
|
|
11
|
-
deps: string[];
|
|
12
|
-
handler: (befly: import("../types/befly").BeflyContext, ctx: import("../types/context").RequestContext<Record<string, any>>) => Promise<void>;
|
|
13
|
-
};
|
|
14
|
-
export default _default;
|
|
9
|
+
declare const parserHook: Hook;
|
|
10
|
+
export default parserHook;
|
package/dist/hooks/parser.js
CHANGED
|
@@ -11,7 +11,7 @@ const xmlParser = new XMLParser();
|
|
|
11
11
|
* - 根据 API 定义的 fields 过滤字段
|
|
12
12
|
* - rawBody: true 时跳过解析,由 handler 自行处理原始请求
|
|
13
13
|
*/
|
|
14
|
-
|
|
14
|
+
const parserHook = {
|
|
15
15
|
name: "parser",
|
|
16
16
|
enable: true,
|
|
17
17
|
deps: ["auth"],
|
|
@@ -90,3 +90,4 @@ export default {
|
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
};
|
|
93
|
+
export default parserHook;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { Hook } from "../types/hook";
|
|
1
2
|
/**
|
|
2
3
|
* 权限检查钩子
|
|
3
4
|
* - 接口无需权限(auth=false):直接通过
|
|
@@ -5,10 +6,5 @@
|
|
|
5
6
|
* - 开发者角色(dev):最高权限,直接通过
|
|
6
7
|
* - 其他角色:检查 Redis 中的角色权限集合
|
|
7
8
|
*/
|
|
8
|
-
declare const
|
|
9
|
-
|
|
10
|
-
enable: true;
|
|
11
|
-
deps: string[];
|
|
12
|
-
handler: (befly: import("../types/befly").BeflyContext, ctx: import("../types/context").RequestContext<Record<string, any>>) => Promise<void>;
|
|
13
|
-
};
|
|
14
|
-
export default _default;
|
|
9
|
+
declare const permissionHook: Hook;
|
|
10
|
+
export default permissionHook;
|
package/dist/hooks/permission.js
CHANGED
|
@@ -9,7 +9,7 @@ import { ErrorResponse } from "../utils/response";
|
|
|
9
9
|
* - 开发者角色(dev):最高权限,直接通过
|
|
10
10
|
* - 其他角色:检查 Redis 中的角色权限集合
|
|
11
11
|
*/
|
|
12
|
-
|
|
12
|
+
const permissionHook = {
|
|
13
13
|
name: "permission",
|
|
14
14
|
enable: true,
|
|
15
15
|
deps: ["validator"],
|
|
@@ -46,8 +46,9 @@ export default {
|
|
|
46
46
|
event: "hook_permission_redis_error",
|
|
47
47
|
apiPath: apiPath,
|
|
48
48
|
roleCode: roleCode,
|
|
49
|
-
err: err
|
|
50
|
-
|
|
49
|
+
err: err,
|
|
50
|
+
msg: "hook permission redis error"
|
|
51
|
+
});
|
|
51
52
|
hasPermission = false;
|
|
52
53
|
}
|
|
53
54
|
}
|
|
@@ -60,3 +61,4 @@ export default {
|
|
|
60
61
|
}
|
|
61
62
|
}
|
|
62
63
|
};
|
|
64
|
+
export default permissionHook;
|
|
@@ -1,11 +1,7 @@
|
|
|
1
|
+
import type { Hook } from "../types/hook";
|
|
1
2
|
/**
|
|
2
3
|
* 参数验证钩子
|
|
3
4
|
* 根据 API 定义的 fields 和 required 验证请求参数
|
|
4
5
|
*/
|
|
5
|
-
declare const
|
|
6
|
-
|
|
7
|
-
enable: true;
|
|
8
|
-
deps: string[];
|
|
9
|
-
handler: (befly: import("../types/befly").BeflyContext, ctx: import("../types/context").RequestContext<Record<string, any>>) => Promise<void>;
|
|
10
|
-
};
|
|
11
|
-
export default _default;
|
|
6
|
+
declare const validatorHook: Hook;
|
|
7
|
+
export default validatorHook;
|
package/dist/hooks/validator.js
CHANGED
|
@@ -5,7 +5,7 @@ import { ErrorResponse } from "../utils/response";
|
|
|
5
5
|
* 参数验证钩子
|
|
6
6
|
* 根据 API 定义的 fields 和 required 验证请求参数
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
const validatorHook = {
|
|
9
9
|
name: "validator",
|
|
10
10
|
enable: true,
|
|
11
11
|
deps: ["parser"],
|
|
@@ -31,3 +31,4 @@ export default {
|
|
|
31
31
|
}
|
|
32
32
|
}
|
|
33
33
|
};
|
|
34
|
+
export default validatorHook;
|
package/dist/index.js
CHANGED
|
@@ -114,7 +114,7 @@ export class Befly {
|
|
|
114
114
|
return staticFetch(req);
|
|
115
115
|
},
|
|
116
116
|
error: (error) => {
|
|
117
|
-
Logger.error({ err: error
|
|
117
|
+
Logger.error({ err: error, msg: "服务启动时发生错误" });
|
|
118
118
|
// 开发模式下返回详细错误信息
|
|
119
119
|
if (this.config.nodeEnv === "development") {
|
|
120
120
|
return Response.json({
|
|
@@ -142,7 +142,7 @@ export class Befly {
|
|
|
142
142
|
// 注意:这里不能直接 process.exit(1)
|
|
143
143
|
// - Logger 是异步缓冲写入,exit 会导致日志来不及 flush(实际项目里表现为“完全没打印”)
|
|
144
144
|
// - 作为库代码,也不应该强行终止宿主进程
|
|
145
|
-
Logger.error({ err: error
|
|
145
|
+
Logger.error({ err: error, msg: "项目启动失败" });
|
|
146
146
|
// 尽力把错误日志落盘/输出后再把异常抛给上层。
|
|
147
147
|
try {
|
|
148
148
|
await Logger.flush();
|
package/dist/lib/cacheHelper.js
CHANGED
|
@@ -85,7 +85,7 @@ export class CacheHelper {
|
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
catch (error) {
|
|
88
|
-
Logger.error({ err: error
|
|
88
|
+
Logger.error({ err: error, msg: "⚠️ 接口缓存异常" });
|
|
89
89
|
}
|
|
90
90
|
}
|
|
91
91
|
/**
|
|
@@ -110,7 +110,7 @@ export class CacheHelper {
|
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
112
|
catch (error) {
|
|
113
|
-
Logger.warn({ err: error
|
|
113
|
+
Logger.warn({ err: error, msg: "⚠️ 菜单缓存异常" });
|
|
114
114
|
}
|
|
115
115
|
}
|
|
116
116
|
/**
|
|
@@ -161,7 +161,7 @@ export class CacheHelper {
|
|
|
161
161
|
// 极简方案不做版本/ready/meta:重建完成即生效
|
|
162
162
|
}
|
|
163
163
|
catch (error) {
|
|
164
|
-
Logger.error({ err: error
|
|
164
|
+
Logger.error({ err: error, msg: "⚠️ 角色权限缓存异常(将阻断启动)" });
|
|
165
165
|
throw error;
|
|
166
166
|
}
|
|
167
167
|
}
|
|
@@ -211,7 +211,7 @@ export class CacheHelper {
|
|
|
211
211
|
return apis || [];
|
|
212
212
|
}
|
|
213
213
|
catch (error) {
|
|
214
|
-
Logger.error({ err: error
|
|
214
|
+
Logger.error({ err: error, msg: "获取接口缓存失败" });
|
|
215
215
|
return [];
|
|
216
216
|
}
|
|
217
217
|
}
|
|
@@ -225,7 +225,7 @@ export class CacheHelper {
|
|
|
225
225
|
return menus || [];
|
|
226
226
|
}
|
|
227
227
|
catch (error) {
|
|
228
|
-
Logger.error({ err: error
|
|
228
|
+
Logger.error({ err: error, msg: "获取菜单缓存失败" });
|
|
229
229
|
return [];
|
|
230
230
|
}
|
|
231
231
|
}
|
|
@@ -240,7 +240,7 @@ export class CacheHelper {
|
|
|
240
240
|
return permissions || [];
|
|
241
241
|
}
|
|
242
242
|
catch (error) {
|
|
243
|
-
Logger.error({ err: error, roleCode: roleCode
|
|
243
|
+
Logger.error({ err: error, roleCode: roleCode, msg: "获取角色权限缓存失败" });
|
|
244
244
|
return [];
|
|
245
245
|
}
|
|
246
246
|
}
|
|
@@ -256,7 +256,7 @@ export class CacheHelper {
|
|
|
256
256
|
return await this.redis.sismember(CacheKeys.roleApis(roleCode), pathname);
|
|
257
257
|
}
|
|
258
258
|
catch (error) {
|
|
259
|
-
Logger.error({ err: error, roleCode: roleCode
|
|
259
|
+
Logger.error({ err: error, roleCode: roleCode, msg: "检查角色权限失败" });
|
|
260
260
|
return false;
|
|
261
261
|
}
|
|
262
262
|
}
|
|
@@ -275,7 +275,7 @@ export class CacheHelper {
|
|
|
275
275
|
return false;
|
|
276
276
|
}
|
|
277
277
|
catch (error) {
|
|
278
|
-
Logger.error({ err: error, roleCode: roleCode
|
|
278
|
+
Logger.error({ err: error, roleCode: roleCode, msg: "删除角色权限缓存失败" });
|
|
279
279
|
return false;
|
|
280
280
|
}
|
|
281
281
|
}
|
package/dist/lib/connect.js
CHANGED
|
@@ -82,7 +82,7 @@ export class Connect {
|
|
|
82
82
|
return sql;
|
|
83
83
|
}
|
|
84
84
|
catch (error) {
|
|
85
|
-
Logger.error({ err: error
|
|
85
|
+
Logger.error({ err: error, msg: "[Connect] SQL 连接失败" });
|
|
86
86
|
try {
|
|
87
87
|
await sql?.close();
|
|
88
88
|
}
|
|
@@ -99,7 +99,7 @@ export class Connect {
|
|
|
99
99
|
await this.sqlClient.close();
|
|
100
100
|
}
|
|
101
101
|
catch (error) {
|
|
102
|
-
Logger.error({ err: error
|
|
102
|
+
Logger.error({ err: error, msg: "[Connect] 关闭 SQL 连接时出错" });
|
|
103
103
|
}
|
|
104
104
|
this.sqlClient = null;
|
|
105
105
|
this.sqlConnectedAt = null;
|
|
@@ -153,7 +153,7 @@ export class Connect {
|
|
|
153
153
|
return redis;
|
|
154
154
|
}
|
|
155
155
|
catch (error) {
|
|
156
|
-
Logger.error({ err: error
|
|
156
|
+
Logger.error({ err: error, msg: "[Connect] Redis 连接失败" });
|
|
157
157
|
throw new Error(`Redis 连接失败: ${error.message}`);
|
|
158
158
|
}
|
|
159
159
|
}
|
|
@@ -167,7 +167,7 @@ export class Connect {
|
|
|
167
167
|
this.redisConnectedAt = null;
|
|
168
168
|
}
|
|
169
169
|
catch (error) {
|
|
170
|
-
Logger.error({ err: error
|
|
170
|
+
Logger.error({ err: error, msg: "[Connect] 关闭 Redis 连接时出错" });
|
|
171
171
|
}
|
|
172
172
|
this.redisClient = null;
|
|
173
173
|
}
|
|
@@ -196,7 +196,7 @@ export class Connect {
|
|
|
196
196
|
await this.connectRedis(config.redis || {});
|
|
197
197
|
}
|
|
198
198
|
catch (error) {
|
|
199
|
-
Logger.error({ err: error
|
|
199
|
+
Logger.error({ err: error, msg: "数据库初始化失败" });
|
|
200
200
|
await this.disconnect();
|
|
201
201
|
throw error;
|
|
202
202
|
}
|
package/dist/lib/dbHelper.js
CHANGED
|
@@ -58,7 +58,7 @@ export class DbHelper {
|
|
|
58
58
|
// 3. 写入 Redis 缓存
|
|
59
59
|
const cacheRes = await this.redis.setObject(cacheKey, columnNames, TABLE_COLUMNS_CACHE_TTL_SECONDS);
|
|
60
60
|
if (cacheRes === null) {
|
|
61
|
-
Logger.warn({ table: table, cacheKey: cacheKey
|
|
61
|
+
Logger.warn({ table: table, cacheKey: cacheKey, msg: "表字段缓存写入 Redis 失败" });
|
|
62
62
|
}
|
|
63
63
|
return columnNames;
|
|
64
64
|
}
|
|
@@ -422,11 +422,11 @@ export class DbHelper {
|
|
|
422
422
|
const result = dataExecRes.data || [];
|
|
423
423
|
// 警告日志:返回数据超过警告阈值
|
|
424
424
|
if (result.length >= WARNING_LIMIT) {
|
|
425
|
-
Logger.warn({ table: options.table, count: result.length, total: total
|
|
425
|
+
Logger.warn({ table: options.table, count: result.length, total: total, msg: "getAll 返回数据过多,建议使用 getList 分页查询" });
|
|
426
426
|
}
|
|
427
427
|
// 如果达到上限,额外警告
|
|
428
428
|
if (result.length >= MAX_LIMIT) {
|
|
429
|
-
Logger.warn({ table: options.table, limit: MAX_LIMIT, total: total
|
|
429
|
+
Logger.warn({ table: options.table, limit: MAX_LIMIT, total: total, msg: `getAll 达到最大限制 ${MAX_LIMIT},实际总数 ${total},只返回前 ${MAX_LIMIT} 条` });
|
|
430
430
|
}
|
|
431
431
|
// 字段名转换:下划线 → 小驼峰
|
|
432
432
|
const camelResult = arrayKeysToCamel(result);
|
|
@@ -525,8 +525,9 @@ export class DbHelper {
|
|
|
525
525
|
table: table,
|
|
526
526
|
snakeTable: snakeTable,
|
|
527
527
|
count: dataList.length,
|
|
528
|
-
fields: insertFields
|
|
529
|
-
|
|
528
|
+
fields: insertFields,
|
|
529
|
+
msg: "批量插入失败"
|
|
530
|
+
});
|
|
530
531
|
throw error;
|
|
531
532
|
}
|
|
532
533
|
}
|
package/dist/lib/logger.d.ts
CHANGED
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
* 日志系统 - Bun 环境自定义实现(替换 pino / pino-roll)
|
|
3
3
|
*/
|
|
4
4
|
import type { LoggerConfig } from "../types/logger";
|
|
5
|
-
export declare function escapeRegExp(input: string): string;
|
|
6
5
|
type SinkLogger = {
|
|
7
|
-
info(
|
|
8
|
-
warn(
|
|
9
|
-
error(
|
|
10
|
-
debug(
|
|
6
|
+
info(record: unknown): any;
|
|
7
|
+
warn(record: unknown): any;
|
|
8
|
+
error(record: unknown): any;
|
|
9
|
+
debug(record: unknown): any;
|
|
11
10
|
};
|
|
12
11
|
export declare function flush(): Promise<void>;
|
|
13
12
|
export declare function shutdown(): Promise<void>;
|
|
@@ -24,19 +23,19 @@ export declare function setMockLogger(mock: SinkLogger | null): void;
|
|
|
24
23
|
* 获取 Logger 实例(延迟初始化)
|
|
25
24
|
*/
|
|
26
25
|
export declare function getLogger(): SinkLogger;
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
declare class LoggerFacade {
|
|
27
|
+
private maybeSanitizeForMock;
|
|
28
|
+
info(input: unknown): any;
|
|
29
|
+
warn(input: unknown): any;
|
|
30
|
+
error(input: unknown): any;
|
|
31
|
+
debug(input: unknown): any;
|
|
32
|
+
flush(): Promise<void>;
|
|
33
|
+
configure(cfg: LoggerConfig): void;
|
|
34
|
+
setMock(mock: SinkLogger | null): void;
|
|
35
|
+
shutdown(): Promise<void>;
|
|
36
|
+
}
|
|
29
37
|
/**
|
|
30
38
|
* 日志实例(延迟初始化)
|
|
31
39
|
*/
|
|
32
|
-
export declare const Logger:
|
|
33
|
-
info(...args: LoggerCallArgs): any;
|
|
34
|
-
warn(...args: LoggerCallArgs): any;
|
|
35
|
-
error(...args: LoggerCallArgs): any;
|
|
36
|
-
debug(...args: LoggerCallArgs): any;
|
|
37
|
-
flush(): Promise<void>;
|
|
38
|
-
configure: typeof configure;
|
|
39
|
-
setMock: typeof setMockLogger;
|
|
40
|
-
shutdown: typeof shutdown;
|
|
41
|
-
};
|
|
40
|
+
export declare const Logger: LoggerFacade;
|
|
42
41
|
export {};
|