befly 3.8.9 → 3.8.10
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/.prettierignore +2 -0
- package/.prettierrc +12 -0
- package/bunfig.toml +3 -0
- package/env.ts +0 -1
- package/package.json +5 -9
- package/plugins/cache.ts +22 -0
- package/plugins/db.ts +59 -0
- package/plugins/logger.ts +27 -0
- package/plugins/redis.ts +41 -0
package/.prettierignore
ADDED
package/.prettierrc
ADDED
package/bunfig.toml
ADDED
package/env.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly",
|
|
3
|
-
"version": "3.8.
|
|
3
|
+
"version": "3.8.10",
|
|
4
4
|
"description": "Befly - 为 Bun 专属打造的 TypeScript API 接口框架核心引擎",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
@@ -40,13 +40,9 @@
|
|
|
40
40
|
"files": [
|
|
41
41
|
"lib/",
|
|
42
42
|
"loader/",
|
|
43
|
-
"
|
|
43
|
+
"plugins/",
|
|
44
44
|
"router/",
|
|
45
45
|
"types/",
|
|
46
|
-
".gitignore",
|
|
47
|
-
".npmignore",
|
|
48
|
-
".bunignore",
|
|
49
|
-
".editorconfig",
|
|
50
46
|
".npmrc",
|
|
51
47
|
".prettierignore",
|
|
52
48
|
".prettierrc",
|
|
@@ -56,11 +52,11 @@
|
|
|
56
52
|
"main.ts",
|
|
57
53
|
"check.ts",
|
|
58
54
|
"env.ts",
|
|
59
|
-
"menu.json",
|
|
60
55
|
"paths.ts",
|
|
61
56
|
"util.ts",
|
|
62
57
|
"package.json",
|
|
63
|
-
"README.md"
|
|
58
|
+
"README.md",
|
|
59
|
+
"LICENSE"
|
|
64
60
|
],
|
|
65
61
|
"engines": {
|
|
66
62
|
"bun": ">=1.3.0"
|
|
@@ -69,5 +65,5 @@
|
|
|
69
65
|
"es-toolkit": "^1.41.0",
|
|
70
66
|
"pathe": "^2.0.3"
|
|
71
67
|
},
|
|
72
|
-
"gitHead": "
|
|
68
|
+
"gitHead": "492e46d692787e6d141659fba27c26e030cd1181"
|
|
73
69
|
}
|
package/plugins/cache.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 缓存插件 - TypeScript 版本
|
|
3
|
+
* 负责在服务器启动时缓存接口、菜单和角色权限到 Redis
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { CacheHelper } from '../lib/cacheHelper.js';
|
|
7
|
+
import type { Plugin } from '../types/plugin.js';
|
|
8
|
+
import type { BeflyContext } from '../types/befly.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 缓存插件
|
|
12
|
+
*/
|
|
13
|
+
const cachePlugin: Plugin = {
|
|
14
|
+
name: '_cache',
|
|
15
|
+
after: ['_db', '_redis'],
|
|
16
|
+
|
|
17
|
+
async onInit(befly: BeflyContext): Promise<CacheHelper> {
|
|
18
|
+
return new CacheHelper(befly);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export default cachePlugin;
|
package/plugins/db.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 数据库插件 - TypeScript 版本
|
|
3
|
+
* 初始化数据库连接和 SQL 管理器
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Env } from '../env.js';
|
|
7
|
+
import { Logger } from '../lib/logger.js';
|
|
8
|
+
import { Database } from '../lib/database.js';
|
|
9
|
+
import { DbHelper } from '../lib/dbHelper.js';
|
|
10
|
+
import type { Plugin } from '../types/plugin.js';
|
|
11
|
+
import type { BeflyContext } from '../types/befly.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 数据库插件
|
|
15
|
+
*/
|
|
16
|
+
const dbPlugin: Plugin = {
|
|
17
|
+
name: '_db',
|
|
18
|
+
after: ['_redis'],
|
|
19
|
+
|
|
20
|
+
async onInit(befly: BeflyContext): Promise<DbHelper | Record<string, never>> {
|
|
21
|
+
let sql: any = null;
|
|
22
|
+
|
|
23
|
+
try {
|
|
24
|
+
if (Env.DB_ENABLE === 1) {
|
|
25
|
+
// 创建 Bun SQL 客户端(内置连接池),并确保连接验证成功后再继续
|
|
26
|
+
// 从环境变量读取连接超时配置
|
|
27
|
+
const connectionTimeout = Env.DB_CONNECTION_TIMEOUT ? parseInt(Env.DB_CONNECTION_TIMEOUT) : 30000;
|
|
28
|
+
|
|
29
|
+
sql = await Database.connectSql({
|
|
30
|
+
connectionTimeout
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// 创建数据库管理器实例,直接传入 sql 对象
|
|
34
|
+
const dbManager = new DbHelper(befly, sql);
|
|
35
|
+
|
|
36
|
+
return dbManager;
|
|
37
|
+
} else {
|
|
38
|
+
Logger.warn('数据库未启用(DB_ENABLE≠1),跳过初始化');
|
|
39
|
+
return {};
|
|
40
|
+
}
|
|
41
|
+
} catch (error: any) {
|
|
42
|
+
Logger.error('数据库初始化失败', error);
|
|
43
|
+
|
|
44
|
+
// 清理资源
|
|
45
|
+
if (sql) {
|
|
46
|
+
try {
|
|
47
|
+
await sql.close();
|
|
48
|
+
} catch (cleanupError: any) {
|
|
49
|
+
Logger.error('清理连接池失败:', cleanupError);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// 插件内禁止直接退出进程,抛出异常交由主流程统一处理
|
|
54
|
+
throw error;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export default dbPlugin;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 日志插件 - TypeScript 版本
|
|
3
|
+
* 提供全局日志功能
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Logger } from '../lib/logger.js';
|
|
7
|
+
import type { Plugin } from '../types/plugin.js';
|
|
8
|
+
import type { BeflyContext } from '../types/befly.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 日志插件
|
|
12
|
+
*/
|
|
13
|
+
const loggerPlugin: Plugin = {
|
|
14
|
+
name: '_logger',
|
|
15
|
+
after: [],
|
|
16
|
+
|
|
17
|
+
async onInit(befly: BeflyContext): Promise<typeof Logger> {
|
|
18
|
+
try {
|
|
19
|
+
return Logger;
|
|
20
|
+
} catch (error: any) {
|
|
21
|
+
// 插件内禁止直接退出进程,抛出异常交由主流程统一处理
|
|
22
|
+
throw error;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export default loggerPlugin;
|
package/plugins/redis.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Redis 插件 - TypeScript 版本
|
|
3
|
+
* 初始化 Redis 连接和助手工具
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { Env } from '../env.js';
|
|
7
|
+
import { Logger } from '../lib/logger.js';
|
|
8
|
+
import { RedisHelper } from '../lib/redisHelper.js';
|
|
9
|
+
import { Database } from '../lib/database.js';
|
|
10
|
+
import type { Plugin } from '../types/plugin.js';
|
|
11
|
+
import type { BeflyContext } from '../types/befly.js';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Redis 插件
|
|
15
|
+
*/
|
|
16
|
+
const redisPlugin: Plugin = {
|
|
17
|
+
name: '_redis',
|
|
18
|
+
after: ['_logger'],
|
|
19
|
+
|
|
20
|
+
async onInit(befly: BeflyContext): Promise<RedisHelper | Record<string, never>> {
|
|
21
|
+
try {
|
|
22
|
+
if (Env.REDIS_ENABLE === 1) {
|
|
23
|
+
// 初始化 Redis 客户端(统一使用 database.ts 的连接管理)
|
|
24
|
+
await Database.connectRedis();
|
|
25
|
+
|
|
26
|
+
// 返回 RedisHelper 实例
|
|
27
|
+
return new RedisHelper();
|
|
28
|
+
} else {
|
|
29
|
+
Logger.warn('Redis 未启用,跳过初始化');
|
|
30
|
+
return {};
|
|
31
|
+
}
|
|
32
|
+
} catch (error: any) {
|
|
33
|
+
Logger.error('Redis 初始化失败', error);
|
|
34
|
+
|
|
35
|
+
// 插件内禁止直接退出进程,抛出异常交由主流程统一处理
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export default redisPlugin;
|