befly 3.8.19 → 3.8.21
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 +7 -6
- package/bunfig.toml +1 -1
- package/checks/checkApi.ts +92 -0
- package/checks/checkApp.ts +31 -0
- package/{check.ts → checks/checkTable.ts} +28 -159
- package/config.ts +71 -0
- package/hooks/auth.ts +30 -0
- package/hooks/cors.ts +48 -0
- package/hooks/errorHandler.ts +23 -0
- package/hooks/parser.ts +67 -0
- package/hooks/permission.ts +54 -0
- package/hooks/rateLimit.ts +70 -0
- package/hooks/requestId.ts +24 -0
- package/hooks/requestLogger.ts +25 -0
- package/hooks/responseFormatter.ts +64 -0
- package/hooks/validator.ts +34 -0
- package/lib/database.ts +28 -25
- package/lib/dbHelper.ts +3 -3
- package/lib/jwt.ts +90 -99
- package/lib/logger.ts +44 -23
- package/lib/redisHelper.ts +19 -22
- package/loader/loadApis.ts +69 -114
- package/loader/loadHooks.ts +65 -0
- package/loader/loadPlugins.ts +50 -219
- package/main.ts +106 -133
- package/package.json +23 -14
- package/paths.ts +20 -0
- package/plugins/cache.ts +1 -3
- package/plugins/db.ts +8 -11
- package/plugins/logger.ts +5 -3
- package/plugins/redis.ts +10 -14
- package/router/api.ts +60 -106
- package/router/root.ts +15 -12
- package/router/static.ts +54 -58
- package/sync/syncAll.ts +58 -0
- package/sync/syncApi.ts +264 -0
- package/sync/syncDb/apply.ts +194 -0
- package/sync/syncDb/constants.ts +76 -0
- package/sync/syncDb/ddl.ts +194 -0
- package/sync/syncDb/helpers.ts +200 -0
- package/sync/syncDb/index.ts +164 -0
- package/sync/syncDb/schema.ts +201 -0
- package/sync/syncDb/sqlite.ts +50 -0
- package/sync/syncDb/table.ts +321 -0
- package/sync/syncDb/tableCreate.ts +146 -0
- package/sync/syncDb/version.ts +72 -0
- package/sync/syncDb.ts +19 -0
- package/sync/syncDev.ts +206 -0
- package/sync/syncMenu.ts +331 -0
- package/tests/cipher.test.ts +248 -0
- package/tests/dbHelper-advanced.test.ts +717 -0
- package/tests/dbHelper-columns.test.ts +266 -0
- package/tests/dbHelper-execute.test.ts +240 -0
- package/tests/fields-redis-cache.test.ts +123 -0
- package/tests/fields-validate.test.ts +99 -0
- package/tests/integration.test.ts +202 -0
- package/tests/jwt.test.ts +122 -0
- package/tests/logger.test.ts +94 -0
- package/tests/redisHelper.test.ts +231 -0
- package/tests/sqlBuilder-advanced.test.ts +593 -0
- package/tests/sqlBuilder.test.ts +184 -0
- package/tests/util.test.ts +95 -0
- package/tests/validator-advanced.test.ts +653 -0
- package/tests/validator.test.ts +148 -0
- package/tests/xml.test.ts +101 -0
- package/tsconfig.json +2 -4
- package/types/api.d.ts +6 -0
- package/types/befly.d.ts +152 -28
- package/types/context.d.ts +29 -3
- package/types/hook.d.ts +35 -0
- package/types/index.ts +14 -1
- package/types/plugin.d.ts +6 -7
- package/types/sync.d.ts +403 -0
- package/env.ts +0 -106
- package/lib/middleware.ts +0 -275
- package/types/env.ts +0 -65
- package/types/util.d.ts +0 -45
- package/util.ts +0 -257
package/main.ts
CHANGED
|
@@ -3,39 +3,34 @@
|
|
|
3
3
|
* 提供简洁的框架接口,核心逻辑已提取到 loader 层
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
// ========== 外部依赖 ==========
|
|
7
|
+
import { calcPerfTime } from 'befly-util';
|
|
7
8
|
|
|
9
|
+
// ========== 相对导入 ==========
|
|
8
10
|
import { Logger } from './lib/logger.js';
|
|
9
11
|
import { Cipher } from './lib/cipher.js';
|
|
10
12
|
import { Jwt } from './lib/jwt.js';
|
|
11
13
|
import { Database } from './lib/database.js';
|
|
14
|
+
import { DbHelper } from './lib/dbHelper.js';
|
|
15
|
+
import { RedisHelper } from './lib/redisHelper.js';
|
|
12
16
|
import { loadPlugins } from './loader/loadPlugins.js';
|
|
17
|
+
import { loadHooks } from './loader/loadHooks.js';
|
|
13
18
|
import { loadApis } from './loader/loadApis.js';
|
|
14
19
|
import { rootHandler } from './router/root.js';
|
|
15
20
|
import { apiHandler } from './router/api.js';
|
|
16
21
|
import { staticHandler } from './router/static.js';
|
|
22
|
+
import { checkApp } from './checks/checkApp.js';
|
|
23
|
+
import { checkTable } from './checks/checkTable.js';
|
|
24
|
+
import { checkApi } from './checks/checkApi.js';
|
|
25
|
+
import { syncAllCommand } from './sync/syncAll.js';
|
|
17
26
|
import { coreDir } from './paths.js';
|
|
18
|
-
import {
|
|
19
|
-
import { RedisHelper } from './lib/redisHelper.js';
|
|
20
|
-
import { checkTable, checkApi, checkApp } from './check.js';
|
|
21
|
-
import {
|
|
22
|
-
//
|
|
23
|
-
Yes,
|
|
24
|
-
No,
|
|
25
|
-
keysToSnake,
|
|
26
|
-
keysToCamel,
|
|
27
|
-
arrayKeysToCamel,
|
|
28
|
-
pickFields,
|
|
29
|
-
fieldClear,
|
|
30
|
-
calcPerfTime,
|
|
31
|
-
scanAddons,
|
|
32
|
-
getAddonDir,
|
|
33
|
-
addonDirExists
|
|
34
|
-
} from './util.js';
|
|
27
|
+
import { defaultOptions } from './config.js';
|
|
35
28
|
|
|
29
|
+
// ========== 类型导入 ==========
|
|
36
30
|
import type { Server } from 'bun';
|
|
37
|
-
import type { BeflyContext } from './types/befly.js';
|
|
31
|
+
import type { BeflyContext, BeflyOptions } from './types/befly.js';
|
|
38
32
|
import type { Plugin } from './types/plugin.js';
|
|
33
|
+
import type { Hook } from './types/hook.js';
|
|
39
34
|
import type { ApiRoute } from './types/api.js';
|
|
40
35
|
/**
|
|
41
36
|
* Befly 框架核心类
|
|
@@ -48,142 +43,120 @@ export class Befly {
|
|
|
48
43
|
/** 插件列表 */
|
|
49
44
|
private pluginLists: Plugin[] = [];
|
|
50
45
|
|
|
46
|
+
/** 钩子列表 */
|
|
47
|
+
private hookLists: Hook[] = [];
|
|
48
|
+
|
|
51
49
|
/** 应用上下文 */
|
|
52
50
|
public appContext: BeflyContext;
|
|
53
51
|
|
|
54
|
-
|
|
52
|
+
/** 最终配置(合并默认值后) */
|
|
53
|
+
public config: BeflyOptions;
|
|
54
|
+
|
|
55
|
+
constructor(options: BeflyOptions = {}) {
|
|
55
56
|
this.appContext = {};
|
|
57
|
+
// 合并配置:用户配置 > 默认配置(最多 2 级)
|
|
58
|
+
this.config = { ...defaultOptions };
|
|
59
|
+
for (const key in options) {
|
|
60
|
+
const value = options[key as keyof BeflyOptions];
|
|
61
|
+
if (value !== undefined && value !== null) {
|
|
62
|
+
if (typeof value === 'object' && !Array.isArray(value)) {
|
|
63
|
+
this.config[key as keyof BeflyOptions] = { ...defaultOptions[key as keyof typeof defaultOptions], ...value } as any;
|
|
64
|
+
} else {
|
|
65
|
+
this.config[key as keyof BeflyOptions] = value as any;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
56
69
|
}
|
|
57
70
|
|
|
58
71
|
/**
|
|
59
72
|
* 启动完整的生命周期流程
|
|
60
73
|
* @returns HTTP 服务器实例
|
|
61
74
|
*/
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
75
|
+
public async start(): Promise<Server> {
|
|
76
|
+
try {
|
|
77
|
+
const serverStartTime = Bun.nanoseconds();
|
|
78
|
+
|
|
79
|
+
// 1. 执行启动检查
|
|
80
|
+
await checkApp();
|
|
81
|
+
await checkTable();
|
|
82
|
+
await checkApi();
|
|
83
|
+
|
|
84
|
+
// 4. 加载插件
|
|
85
|
+
await loadPlugins({
|
|
86
|
+
pluginLists: this.pluginLists,
|
|
87
|
+
appContext: this.appContext,
|
|
88
|
+
pluginsConfig: this.config as any
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
// 5. 加载钩子
|
|
92
|
+
await loadHooks({
|
|
93
|
+
hookLists: this.hookLists,
|
|
94
|
+
pluginsConfig: this.config as any
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
// 6. 加载所有 API
|
|
98
|
+
await loadApis(this.apiRoutes);
|
|
99
|
+
|
|
100
|
+
// 7. 自动同步 (默认开启)
|
|
101
|
+
await syncAllCommand(this.config);
|
|
102
|
+
|
|
103
|
+
// 8. 启动 HTTP 服务器
|
|
104
|
+
const server = Bun.serve({
|
|
105
|
+
port: this.config.appPort,
|
|
106
|
+
hostname: this.config.appHost,
|
|
107
|
+
routes: {
|
|
108
|
+
'/': rootHandler,
|
|
109
|
+
'/api/*': apiHandler(this.apiRoutes, this.hookLists, this.appContext),
|
|
110
|
+
'/*': staticHandler(this.config.cors)
|
|
111
|
+
},
|
|
112
|
+
error: (error: Error) => {
|
|
113
|
+
Logger.error('服务启动时发生错误', error);
|
|
114
|
+
return Response.json({ code: 1, msg: '内部服务器错误' });
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const finalStartupTime = calcPerfTime(serverStartTime);
|
|
119
|
+
Logger.info(`${this.config.appName} 启动成功! `);
|
|
120
|
+
Logger.info(`服务器启动耗时: ${finalStartupTime}`);
|
|
121
|
+
Logger.info(`服务器监听地址: http://${this.config.appHost}:${this.config.appPort}`);
|
|
122
|
+
|
|
123
|
+
// 9. 注册优雅关闭处理
|
|
124
|
+
const gracefulShutdown = async (signal: string) => {
|
|
125
|
+
// 停止接收新请求
|
|
126
|
+
server.stop(true);
|
|
127
|
+
Logger.info('HTTP 服务器已停止');
|
|
128
|
+
|
|
129
|
+
// 关闭数据库连接
|
|
130
|
+
try {
|
|
131
|
+
await Database.disconnect();
|
|
132
|
+
Logger.info('数据库连接已关闭');
|
|
133
|
+
} catch (error: any) {
|
|
134
|
+
Logger.error('关闭数据库连接时出错:', error);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
Logger.info('服务器已优雅关闭');
|
|
138
|
+
process.exit(0);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
|
|
142
|
+
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
|
|
143
|
+
|
|
144
|
+
return server;
|
|
145
|
+
} catch (error: any) {
|
|
146
|
+
Logger.error('Befly 启动失败', error);
|
|
72
147
|
process.exit(1);
|
|
73
148
|
}
|
|
74
|
-
|
|
75
|
-
// 3. 执行表定义检查
|
|
76
|
-
const tableCheckResult = await checkTable();
|
|
77
|
-
if (!tableCheckResult) {
|
|
78
|
-
Logger.error('表定义检查失败,程序退出');
|
|
79
|
-
process.exit(1);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// 4. 执行 API 定义检查
|
|
83
|
-
const apiCheckResult = await checkApi();
|
|
84
|
-
if (!apiCheckResult) {
|
|
85
|
-
Logger.error('API 定义检查失败,程序退出');
|
|
86
|
-
process.exit(1);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// 5. 加载插件
|
|
90
|
-
await loadPlugins({
|
|
91
|
-
pluginLists: this.pluginLists,
|
|
92
|
-
appContext: this.appContext
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
// 4. 启动 HTTP 服务器
|
|
96
|
-
const totalStartupTime = calcPerfTime(serverStartTime);
|
|
97
|
-
|
|
98
|
-
return await this.startServer();
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* 启动 HTTP 服务器
|
|
103
|
-
* @returns HTTP 服务器实例
|
|
104
|
-
*/
|
|
105
|
-
private async startServer(): Promise<Server> {
|
|
106
|
-
const startTime = Bun.nanoseconds();
|
|
107
|
-
|
|
108
|
-
const server = Bun.serve({
|
|
109
|
-
port: Env.APP_PORT,
|
|
110
|
-
hostname: Env.APP_HOST,
|
|
111
|
-
routes: {
|
|
112
|
-
'/': rootHandler,
|
|
113
|
-
'/api/*': apiHandler(this.apiRoutes, this.pluginLists, this.appContext),
|
|
114
|
-
'/*': staticHandler
|
|
115
|
-
},
|
|
116
|
-
error: (error: Error) => {
|
|
117
|
-
Logger.error('服务启动时发生错误', error);
|
|
118
|
-
return Response.json({ code: 1, msg: '内部服务器错误' });
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
|
|
122
|
-
const finalStartupTime = calcPerfTime(startTime);
|
|
123
|
-
Logger.info(`${Env.APP_NAME} 启动成功! `);
|
|
124
|
-
Logger.info(`服务器启动耗时: ${finalStartupTime}`);
|
|
125
|
-
Logger.info(`服务器监听地址: http://${Env.APP_HOST}:${Env.APP_PORT}`);
|
|
126
|
-
|
|
127
|
-
return server;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* 启动服务器并注册优雅关闭处理
|
|
132
|
-
*/
|
|
133
|
-
async listen(): Promise<Server> {
|
|
134
|
-
const server = await this.start();
|
|
135
|
-
|
|
136
|
-
const gracefulShutdown = async (signal: string) => {
|
|
137
|
-
// 1. 停止接收新请求
|
|
138
|
-
server.stop(true);
|
|
139
|
-
Logger.info('HTTP 服务器已停止');
|
|
140
|
-
|
|
141
|
-
// 2. 关闭数据库连接
|
|
142
|
-
try {
|
|
143
|
-
await Database.disconnect();
|
|
144
|
-
Logger.info('数据库连接已关闭');
|
|
145
|
-
} catch (error: any) {
|
|
146
|
-
Logger.err('关闭数据库连接时出错:', error);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
Logger.info('服务器已优雅关闭');
|
|
150
|
-
process.exit(0);
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
process.on('SIGTERM', () => gracefulShutdown('SIGTERM'));
|
|
154
|
-
process.on('SIGINT', () => gracefulShutdown('SIGINT'));
|
|
155
|
-
|
|
156
|
-
return server;
|
|
157
149
|
}
|
|
158
150
|
}
|
|
159
151
|
|
|
160
152
|
// 核心类和工具导出
|
|
161
153
|
export {
|
|
162
154
|
// 配置
|
|
163
|
-
Env,
|
|
164
155
|
Logger,
|
|
165
156
|
Cipher,
|
|
166
157
|
Jwt,
|
|
167
|
-
Yes,
|
|
168
|
-
No,
|
|
169
158
|
Database,
|
|
170
159
|
DbHelper,
|
|
171
160
|
RedisHelper,
|
|
172
|
-
coreDir
|
|
173
|
-
checkTable,
|
|
174
|
-
checkApi,
|
|
175
|
-
checkApp
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
// 工具函数命名空间导出
|
|
179
|
-
export const utils = {
|
|
180
|
-
keysToSnake: keysToSnake,
|
|
181
|
-
keysToCamel: keysToCamel,
|
|
182
|
-
arrayKeysToCamel: arrayKeysToCamel,
|
|
183
|
-
pickFields: pickFields,
|
|
184
|
-
fieldClear: fieldClear,
|
|
185
|
-
calcPerfTime: calcPerfTime,
|
|
186
|
-
scanAddons: scanAddons,
|
|
187
|
-
getAddonDir: getAddonDir,
|
|
188
|
-
addonDirExists: addonDirExists
|
|
161
|
+
coreDir
|
|
189
162
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly",
|
|
3
|
-
"version": "3.8.
|
|
3
|
+
"version": "3.8.21",
|
|
4
4
|
"description": "Befly - 为 Bun 专属打造的 TypeScript API 接口框架核心引擎",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
@@ -9,14 +9,15 @@
|
|
|
9
9
|
"registry": "https://registry.npmjs.org"
|
|
10
10
|
},
|
|
11
11
|
"main": "main.ts",
|
|
12
|
-
"types": "
|
|
12
|
+
"types": "main.ts",
|
|
13
13
|
"exports": {
|
|
14
14
|
".": {
|
|
15
|
+
"types": "./main.ts",
|
|
15
16
|
"default": "./main.ts"
|
|
16
17
|
},
|
|
17
18
|
"./types": {
|
|
18
|
-
"types": "./types/index.
|
|
19
|
-
"default": "./types/index.
|
|
19
|
+
"types": "./types/index.ts",
|
|
20
|
+
"default": "./types/index.ts"
|
|
20
21
|
}
|
|
21
22
|
},
|
|
22
23
|
"scripts": {
|
|
@@ -38,32 +39,40 @@
|
|
|
38
39
|
"homepage": "https://chensuiyi.me",
|
|
39
40
|
"license": "Apache-2.0",
|
|
40
41
|
"files": [
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
"
|
|
42
|
+
"checks",
|
|
43
|
+
"dist",
|
|
44
|
+
"hooks",
|
|
45
|
+
"lib",
|
|
46
|
+
"loader",
|
|
47
|
+
"plugins",
|
|
48
|
+
"router",
|
|
49
|
+
"sync",
|
|
50
|
+
"tests",
|
|
51
|
+
"types",
|
|
46
52
|
".npmrc",
|
|
47
53
|
".prettierignore",
|
|
48
54
|
".prettierrc",
|
|
49
55
|
"bunfig.toml",
|
|
50
|
-
"
|
|
56
|
+
"config.ts",
|
|
51
57
|
"LICENSE",
|
|
52
58
|
"main.ts",
|
|
53
|
-
"check.ts",
|
|
54
|
-
"env.ts",
|
|
55
59
|
"paths.ts",
|
|
56
|
-
"util.ts",
|
|
57
60
|
"package.json",
|
|
58
61
|
"README.md",
|
|
62
|
+
"tsconfig.json",
|
|
59
63
|
"LICENSE"
|
|
60
64
|
],
|
|
61
65
|
"engines": {
|
|
62
66
|
"bun": ">=1.3.0"
|
|
63
67
|
},
|
|
64
68
|
"dependencies": {
|
|
69
|
+
"befly-util": "1.0.0",
|
|
70
|
+
"chalk": "^5.6.2",
|
|
65
71
|
"es-toolkit": "^1.41.0",
|
|
66
72
|
"pathe": "^2.0.3"
|
|
67
73
|
},
|
|
68
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "ac90b47c807b2c016ac2331b56814e335a82b291",
|
|
75
|
+
"devDependencies": {
|
|
76
|
+
"typescript": "^5.9.3"
|
|
77
|
+
}
|
|
69
78
|
}
|
package/paths.ts
CHANGED
|
@@ -39,6 +39,16 @@ export const coreCheckDir = join(__dirname, 'checks');
|
|
|
39
39
|
*/
|
|
40
40
|
export const corePluginDir = join(__dirname, 'plugins');
|
|
41
41
|
|
|
42
|
+
/**
|
|
43
|
+
* Core 框架钩子目录
|
|
44
|
+
* @description packages/core/hooks/
|
|
45
|
+
* @usage 存放内置钩子(auth, cors, parser 等)
|
|
46
|
+
*/
|
|
47
|
+
export const coreHookDir = join(__dirname, 'hooks');
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Core 框架 API 目录
|
|
51
|
+
|
|
42
52
|
/**
|
|
43
53
|
* Core 框架 API 目录
|
|
44
54
|
* @description packages/core/apis/
|
|
@@ -76,6 +86,16 @@ export const projectCheckDir = join(projectDir, 'checks');
|
|
|
76
86
|
*/
|
|
77
87
|
export const projectPluginDir = join(projectDir, 'plugins');
|
|
78
88
|
|
|
89
|
+
/**
|
|
90
|
+
* 项目钩子目录
|
|
91
|
+
* @description {projectDir}/hooks/
|
|
92
|
+
* @usage 存放用户自定义钩子
|
|
93
|
+
*/
|
|
94
|
+
export const projectHookDir = join(projectDir, 'hooks');
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 项目 API 目录
|
|
98
|
+
|
|
79
99
|
/**
|
|
80
100
|
* 项目 API 目录
|
|
81
101
|
* @description {projectDir}/apis/
|
package/plugins/cache.ts
CHANGED
|
@@ -11,9 +11,7 @@ import type { BeflyContext } from '../types/befly.js';
|
|
|
11
11
|
* 缓存插件
|
|
12
12
|
*/
|
|
13
13
|
const cachePlugin: Plugin = {
|
|
14
|
-
|
|
15
|
-
after: ['_db', '_redis'],
|
|
16
|
-
|
|
14
|
+
after: ['db', 'redis'],
|
|
17
15
|
async onInit(befly: BeflyContext): Promise<CacheHelper> {
|
|
18
16
|
return new CacheHelper(befly);
|
|
19
17
|
}
|
package/plugins/db.ts
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
* 初始化数据库连接和 SQL 管理器
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import { Env } from '../env.js';
|
|
7
6
|
import { Logger } from '../lib/logger.js';
|
|
8
7
|
import { Database } from '../lib/database.js';
|
|
9
8
|
import { DbHelper } from '../lib/dbHelper.js';
|
|
@@ -14,21 +13,19 @@ import type { BeflyContext } from '../types/befly.js';
|
|
|
14
13
|
* 数据库插件
|
|
15
14
|
*/
|
|
16
15
|
const dbPlugin: Plugin = {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
async onInit(befly: BeflyContext): Promise<DbHelper | Record<string, never>> {
|
|
16
|
+
after: ['redis'],
|
|
17
|
+
async onInit(this: Plugin, befly: BeflyContext): Promise<DbHelper | Record<string, never>> {
|
|
21
18
|
let sql: any = null;
|
|
19
|
+
const config = this.config || {};
|
|
22
20
|
|
|
23
21
|
try {
|
|
24
|
-
|
|
22
|
+
// 默认启用,除非显式禁用
|
|
23
|
+
if (config.enable !== 0) {
|
|
25
24
|
// 创建 Bun SQL 客户端(内置连接池),并确保连接验证成功后再继续
|
|
26
|
-
//
|
|
27
|
-
const connectionTimeout =
|
|
25
|
+
// 从配置读取连接超时配置
|
|
26
|
+
// const connectionTimeout = config.connectionTimeout ? parseInt(config.connectionTimeout) : 30000;
|
|
28
27
|
|
|
29
|
-
sql = await Database.connectSql(
|
|
30
|
-
connectionTimeout
|
|
31
|
-
});
|
|
28
|
+
sql = await Database.connectSql(config);
|
|
32
29
|
|
|
33
30
|
// 创建数据库管理器实例,直接传入 sql 对象
|
|
34
31
|
const dbManager = new DbHelper(befly, sql);
|
package/plugins/logger.ts
CHANGED
|
@@ -11,11 +11,13 @@ import type { BeflyContext } from '../types/befly.js';
|
|
|
11
11
|
* 日志插件
|
|
12
12
|
*/
|
|
13
13
|
const loggerPlugin: Plugin = {
|
|
14
|
-
name: '_logger',
|
|
15
14
|
after: [],
|
|
16
|
-
|
|
17
|
-
async onInit(befly: BeflyContext): Promise<typeof Logger> {
|
|
15
|
+
async onInit(this: Plugin, befly: BeflyContext): Promise<typeof Logger> {
|
|
18
16
|
try {
|
|
17
|
+
// 配置 Logger
|
|
18
|
+
if (this.config) {
|
|
19
|
+
Logger.configure(this.config);
|
|
20
|
+
}
|
|
19
21
|
return Logger;
|
|
20
22
|
} catch (error: any) {
|
|
21
23
|
// 插件内禁止直接退出进程,抛出异常交由主流程统一处理
|
package/plugins/redis.ts
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
* 初始化 Redis 连接和助手工具
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import { Env } from '../env.js';
|
|
7
6
|
import { Logger } from '../lib/logger.js';
|
|
8
7
|
import { RedisHelper } from '../lib/redisHelper.js';
|
|
9
8
|
import { Database } from '../lib/database.js';
|
|
@@ -14,21 +13,18 @@ import type { BeflyContext } from '../types/befly.js';
|
|
|
14
13
|
* Redis 插件
|
|
15
14
|
*/
|
|
16
15
|
const redisPlugin: Plugin = {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
async onInit(befly: BeflyContext): Promise<RedisHelper | Record<string, never>> {
|
|
16
|
+
after: ['logger'],
|
|
17
|
+
async onInit(this: Plugin, befly: BeflyContext): Promise<RedisHelper | Record<string, never>> {
|
|
18
|
+
const config = this.config || {};
|
|
21
19
|
try {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
// 默认启用,除非显式禁用 (这里假设只要配置了 redis 插件就启用,或者检查 enable 字段)
|
|
21
|
+
// 为了兼容性,如果 config 为空,可能意味着使用默认值连接本地 redis
|
|
22
|
+
|
|
23
|
+
// 初始化 Redis 客户端(统一使用 database.ts 的连接管理)
|
|
24
|
+
await Database.connectRedis(config);
|
|
25
25
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
} else {
|
|
29
|
-
Logger.warn('Redis 未启用,跳过初始化');
|
|
30
|
-
return {};
|
|
31
|
-
}
|
|
26
|
+
// 返回 RedisHelper 实例
|
|
27
|
+
return new RedisHelper(config.prefix);
|
|
32
28
|
} catch (error: any) {
|
|
33
29
|
Logger.error('Redis 初始化失败', error);
|
|
34
30
|
|