befly 3.9.37 → 3.9.39
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 +38 -39
- package/befly.config.ts +62 -40
- package/checks/checkApi.ts +16 -16
- package/checks/checkApp.ts +19 -25
- package/checks/checkTable.ts +42 -42
- package/docs/README.md +42 -35
- package/docs/{api.md → api/api.md} +225 -235
- package/docs/cipher.md +71 -69
- package/docs/database.md +155 -153
- package/docs/{examples.md → guide/examples.md} +181 -181
- package/docs/guide/quickstart.md +331 -0
- package/docs/hooks/auth.md +38 -0
- package/docs/hooks/cors.md +28 -0
- package/docs/{hook.md → hooks/hook.md} +140 -57
- package/docs/hooks/parser.md +19 -0
- package/docs/hooks/rateLimit.md +47 -0
- package/docs/{redis.md → infra/redis.md} +84 -93
- package/docs/plugins/cipher.md +61 -0
- package/docs/plugins/database.md +128 -0
- package/docs/{plugin.md → plugins/plugin.md} +83 -81
- package/docs/quickstart.md +26 -26
- package/docs/{addon.md → reference/addon.md} +46 -46
- package/docs/{config.md → reference/config.md} +32 -80
- package/docs/{logger.md → reference/logger.md} +52 -52
- package/docs/{sync.md → reference/sync.md} +32 -35
- package/docs/{table.md → reference/table.md} +7 -7
- package/docs/{validator.md → reference/validator.md} +57 -57
- package/hooks/auth.ts +8 -4
- package/hooks/cors.ts +13 -13
- package/hooks/parser.ts +37 -17
- package/hooks/permission.ts +26 -14
- package/hooks/rateLimit.ts +276 -0
- package/hooks/validator.ts +15 -7
- package/lib/asyncContext.ts +43 -0
- package/lib/cacheHelper.ts +212 -81
- package/lib/cacheKeys.ts +38 -0
- package/lib/cipher.ts +30 -30
- package/lib/connect.ts +28 -28
- package/lib/dbHelper.ts +211 -109
- package/lib/jwt.ts +16 -16
- package/lib/logger.ts +610 -19
- package/lib/redisHelper.ts +185 -44
- package/lib/sqlBuilder.ts +90 -91
- package/lib/validator.ts +59 -39
- package/loader/loadApis.ts +53 -47
- package/loader/loadHooks.ts +40 -14
- package/loader/loadPlugins.ts +16 -17
- package/main.ts +57 -47
- package/package.json +47 -45
- package/paths.ts +15 -14
- package/plugins/cache.ts +5 -4
- package/plugins/cipher.ts +3 -3
- package/plugins/config.ts +2 -2
- package/plugins/db.ts +9 -9
- package/plugins/jwt.ts +3 -3
- package/plugins/logger.ts +8 -12
- package/plugins/redis.ts +8 -8
- package/plugins/tool.ts +6 -6
- package/router/api.ts +85 -56
- package/router/static.ts +12 -12
- package/sync/syncAll.ts +12 -12
- package/sync/syncApi.ts +55 -54
- package/sync/syncDb/apply.ts +20 -19
- package/sync/syncDb/constants.ts +25 -23
- package/sync/syncDb/ddl.ts +35 -36
- package/sync/syncDb/helpers.ts +6 -9
- package/sync/syncDb/schema.ts +10 -9
- package/sync/syncDb/sqlite.ts +7 -8
- package/sync/syncDb/table.ts +37 -35
- package/sync/syncDb/tableCreate.ts +21 -20
- package/sync/syncDb/types.ts +23 -20
- package/sync/syncDb/version.ts +10 -10
- package/sync/syncDb.ts +43 -36
- package/sync/syncDev.ts +74 -66
- package/sync/syncMenu.ts +190 -57
- package/tests/api-integration-array-number.test.ts +282 -0
- package/tests/befly-config-env.test.ts +78 -0
- package/tests/cacheHelper.test.ts +135 -104
- package/tests/cacheKeys.test.ts +41 -0
- package/tests/cipher.test.ts +90 -89
- package/tests/dbHelper-advanced.test.ts +140 -134
- package/tests/dbHelper-all-array-types.test.ts +316 -0
- package/tests/dbHelper-array-serialization.test.ts +258 -0
- package/tests/dbHelper-columns.test.ts +56 -55
- package/tests/dbHelper-execute.test.ts +45 -44
- package/tests/dbHelper-joins.test.ts +124 -119
- package/tests/fields-redis-cache.test.ts +29 -27
- package/tests/fields-validate.test.ts +38 -38
- package/tests/getClientIp.test.ts +54 -0
- package/tests/integration.test.ts +69 -67
- package/tests/jwt.test.ts +27 -26
- package/tests/logger.test.ts +267 -34
- package/tests/rateLimit-hook.test.ts +477 -0
- package/tests/redisHelper.test.ts +187 -188
- package/tests/redisKeys.test.ts +6 -73
- package/tests/scanConfig.test.ts +144 -0
- package/tests/sqlBuilder-advanced.test.ts +217 -215
- package/tests/sqlBuilder.test.ts +92 -91
- package/tests/sync-connection.test.ts +29 -29
- package/tests/syncDb-apply.test.ts +97 -96
- package/tests/syncDb-array-number.test.ts +160 -0
- package/tests/syncDb-constants.test.ts +48 -47
- package/tests/syncDb-ddl.test.ts +99 -98
- package/tests/syncDb-helpers.test.ts +29 -28
- package/tests/syncDb-schema.test.ts +61 -60
- package/tests/syncDb-types.test.ts +60 -59
- package/tests/syncMenu-paths.test.ts +68 -0
- package/tests/util.test.ts +42 -41
- package/tests/validator-array-number.test.ts +310 -0
- package/tests/validator-default.test.ts +373 -0
- package/tests/validator.test.ts +271 -266
- package/tsconfig.json +4 -5
- package/types/api.d.ts +7 -12
- package/types/befly.d.ts +60 -13
- package/types/cache.d.ts +8 -4
- package/types/common.d.ts +17 -9
- package/types/context.d.ts +2 -2
- package/types/crypto.d.ts +23 -0
- package/types/database.d.ts +19 -19
- package/types/hook.d.ts +2 -2
- package/types/jwt.d.ts +118 -0
- package/types/logger.d.ts +30 -0
- package/types/plugin.d.ts +4 -4
- package/types/redis.d.ts +7 -3
- package/types/roleApisCache.ts +23 -0
- package/types/sync.d.ts +10 -10
- package/types/table.d.ts +50 -9
- package/types/validate.d.ts +69 -0
- package/utils/addonHelper.ts +90 -0
- package/utils/arrayKeysToCamel.ts +18 -0
- package/utils/calcPerfTime.ts +13 -0
- package/utils/configTypes.ts +3 -0
- package/utils/cors.ts +19 -0
- package/utils/fieldClear.ts +75 -0
- package/utils/genShortId.ts +12 -0
- package/utils/getClientIp.ts +45 -0
- package/utils/keysToCamel.ts +22 -0
- package/utils/keysToSnake.ts +22 -0
- package/utils/modules.ts +98 -0
- package/utils/pickFields.ts +19 -0
- package/utils/process.ts +56 -0
- package/utils/regex.ts +225 -0
- package/utils/response.ts +115 -0
- package/utils/route.ts +23 -0
- package/utils/scanConfig.ts +142 -0
- package/utils/scanFiles.ts +48 -0
- package/.prettierignore +0 -2
- package/.prettierrc +0 -12
- package/docs/1-/345/237/272/346/234/254/344/273/213/347/273/215.md +0 -35
- package/docs/2-/345/210/235/346/255/245/344/275/223/351/252/214.md +0 -64
- package/docs/3-/347/254/254/344/270/200/344/270/252/346/216/245/345/217/243.md +0 -46
- package/docs/4-/346/223/215/344/275/234/346/225/260/346/215/256/345/272/223.md +0 -172
- package/hooks/requestLogger.ts +0 -84
- package/types/index.ts +0 -24
- package/util.ts +0 -283
package/main.ts
CHANGED
|
@@ -1,31 +1,34 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
2
|
* Befly 框架主入口文件
|
|
3
3
|
* 提供简洁的框架接口,核心逻辑已提取到 loader 层
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
// ========== 外部依赖 ==========
|
|
7
|
-
import { calcPerfTime } from 'befly-shared/calcPerfTime';
|
|
8
|
-
|
|
9
|
-
// ========== 相对导入 ==========
|
|
10
|
-
import { Logger } from './lib/logger.js';
|
|
11
|
-
import { Connect } from './lib/connect.js';
|
|
12
|
-
import { loadPlugins } from './loader/loadPlugins.js';
|
|
13
|
-
import { loadHooks } from './loader/loadHooks.js';
|
|
14
|
-
import { loadApis } from './loader/loadApis.js';
|
|
15
|
-
import { apiHandler } from './router/api.js';
|
|
16
|
-
import { staticHandler } from './router/static.js';
|
|
17
|
-
import { checkApp } from './checks/checkApp.js';
|
|
18
|
-
import { checkTable } from './checks/checkTable.js';
|
|
19
|
-
import { checkApi } from './checks/checkApi.js';
|
|
20
|
-
import { syncAllCommand } from './sync/syncAll.js';
|
|
21
|
-
import { isPrimaryProcess, getProcessRole } from './util.js';
|
|
22
|
-
|
|
23
6
|
// ========== 类型导入 ==========
|
|
24
|
-
import type {
|
|
25
|
-
import type { BeflyContext, BeflyOptions } from
|
|
26
|
-
import type {
|
|
27
|
-
import type {
|
|
28
|
-
|
|
7
|
+
import type { ApiRoute } from "./types/api.js";
|
|
8
|
+
import type { BeflyContext, BeflyOptions } from "./types/befly.js";
|
|
9
|
+
import type { Hook } from "./types/hook.js";
|
|
10
|
+
import type { Plugin } from "./types/plugin.js";
|
|
11
|
+
|
|
12
|
+
// ========== 相对导入(项目内部文件) ==========
|
|
13
|
+
// 启动检查
|
|
14
|
+
import { checkApi } from "./checks/checkApi.js";
|
|
15
|
+
import { checkApp } from "./checks/checkApp.js";
|
|
16
|
+
import { checkTable } from "./checks/checkTable.js";
|
|
17
|
+
// 基础设施
|
|
18
|
+
import { Connect } from "./lib/connect.js";
|
|
19
|
+
import { Logger } from "./lib/logger.js";
|
|
20
|
+
// 加载器
|
|
21
|
+
import { loadApis } from "./loader/loadApis.js";
|
|
22
|
+
import { loadHooks } from "./loader/loadHooks.js";
|
|
23
|
+
import { loadPlugins } from "./loader/loadPlugins.js";
|
|
24
|
+
// 路由处理
|
|
25
|
+
import { apiHandler } from "./router/api.js";
|
|
26
|
+
import { staticHandler } from "./router/static.js";
|
|
27
|
+
// 同步
|
|
28
|
+
import { syncAllCommand } from "./sync/syncAll.js";
|
|
29
|
+
// 工具
|
|
30
|
+
import { calcPerfTime } from "./utils/calcPerfTime.js";
|
|
31
|
+
import { isPrimaryProcess, getProcessRole } from "./utils/process.js";
|
|
29
32
|
|
|
30
33
|
/**
|
|
31
34
|
* Befly 框架核心类
|
|
@@ -56,7 +59,7 @@ export class Befly {
|
|
|
56
59
|
const serverStartTime = Bun.nanoseconds();
|
|
57
60
|
|
|
58
61
|
// 0. 延迟加载配置(避免循环依赖)
|
|
59
|
-
const { beflyConfig } = await import(
|
|
62
|
+
const { beflyConfig } = await import("./befly.config.js");
|
|
60
63
|
this.config = beflyConfig;
|
|
61
64
|
|
|
62
65
|
// 1. 执行启动检查
|
|
@@ -79,44 +82,51 @@ export class Befly {
|
|
|
79
82
|
}
|
|
80
83
|
|
|
81
84
|
// 6. 启动 HTTP 服务器
|
|
85
|
+
const apiFetch = apiHandler(this.apis, this.hooks, this.context as BeflyContext);
|
|
86
|
+
const staticFetch = staticHandler();
|
|
87
|
+
|
|
82
88
|
const server = Bun.serve({
|
|
83
89
|
port: this.config!.appPort,
|
|
84
90
|
hostname: this.config!.appHost,
|
|
85
91
|
// 开发模式下启用详细错误信息
|
|
86
|
-
development: this.config!.nodeEnv ===
|
|
92
|
+
development: this.config!.nodeEnv === "development",
|
|
87
93
|
// 空闲连接超时时间(秒),防止恶意连接占用资源
|
|
88
94
|
idleTimeout: 30,
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
95
|
+
fetch: async (req, bunServer) => {
|
|
96
|
+
const url = new URL(req.url);
|
|
97
|
+
|
|
98
|
+
if (url.pathname === "/") {
|
|
99
|
+
return Response.json({ code: 0, msg: `${this.config!.appName} 接口服务已启动` });
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (url.pathname.startsWith("/api/")) {
|
|
103
|
+
return apiFetch(req, bunServer);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return staticFetch(req);
|
|
97
107
|
},
|
|
98
108
|
error: (error: Error) => {
|
|
99
|
-
Logger.error({ err: error },
|
|
109
|
+
Logger.error({ err: error }, "服务启动时发生错误");
|
|
100
110
|
// 开发模式下返回详细错误信息
|
|
101
|
-
if (this.config!.nodeEnv ===
|
|
111
|
+
if (this.config!.nodeEnv === "development") {
|
|
102
112
|
return Response.json(
|
|
103
113
|
{
|
|
104
114
|
code: 1,
|
|
105
|
-
msg:
|
|
115
|
+
msg: "内部服务器错误",
|
|
106
116
|
error: error.message,
|
|
107
117
|
stack: error.stack
|
|
108
118
|
},
|
|
109
119
|
{ status: 200 }
|
|
110
120
|
);
|
|
111
121
|
}
|
|
112
|
-
return Response.json({ code: 1, msg:
|
|
122
|
+
return Response.json({ code: 1, msg: "内部服务器错误" }, { status: 200 });
|
|
113
123
|
}
|
|
114
124
|
});
|
|
115
125
|
|
|
116
126
|
const finalStartupTime = calcPerfTime(serverStartTime);
|
|
117
127
|
const processRole = getProcessRole();
|
|
118
|
-
const roleLabel = processRole.role ===
|
|
119
|
-
const envLabel = processRole.env ===
|
|
128
|
+
const roleLabel = processRole.role === "primary" ? "主进程" : `工作进程 #${processRole.instanceId}`;
|
|
129
|
+
const envLabel = processRole.env === "standalone" ? "" : ` [${processRole.env}]`;
|
|
120
130
|
|
|
121
131
|
Logger.info(`${this.config!.appName} 启动成功! (${roleLabel}${envLabel})`);
|
|
122
132
|
Logger.info(`服务器启动耗时: ${finalStartupTime}`);
|
|
@@ -129,29 +139,29 @@ export class Befly {
|
|
|
129
139
|
// 优雅停止(等待进行中的请求完成)
|
|
130
140
|
try {
|
|
131
141
|
await server.stop();
|
|
132
|
-
Logger.info(
|
|
142
|
+
Logger.info("HTTP 服务器已停止");
|
|
133
143
|
} catch (error: any) {
|
|
134
|
-
Logger.error({ err: error },
|
|
144
|
+
Logger.error({ err: error }, "停止 HTTP 服务器时出错");
|
|
135
145
|
}
|
|
136
146
|
|
|
137
147
|
// 关闭数据库连接
|
|
138
148
|
try {
|
|
139
149
|
await Connect.disconnect();
|
|
140
|
-
Logger.info(
|
|
150
|
+
Logger.info("数据库连接已关闭");
|
|
141
151
|
} catch (error: any) {
|
|
142
|
-
Logger.error({ err: error },
|
|
152
|
+
Logger.error({ err: error }, "关闭数据库连接时出错");
|
|
143
153
|
}
|
|
144
154
|
|
|
145
|
-
Logger.info(
|
|
155
|
+
Logger.info("服务器已优雅关闭");
|
|
146
156
|
process.exit(0);
|
|
147
157
|
};
|
|
148
158
|
|
|
149
|
-
process.on(
|
|
150
|
-
process.on(
|
|
159
|
+
process.on("SIGTERM", () => gracefulShutdown("SIGTERM"));
|
|
160
|
+
process.on("SIGINT", () => gracefulShutdown("SIGINT"));
|
|
151
161
|
|
|
152
162
|
return server;
|
|
153
163
|
} catch (error: any) {
|
|
154
|
-
Logger.error({ err: error },
|
|
164
|
+
Logger.error({ err: error }, "项目启动失败");
|
|
155
165
|
process.exit(1);
|
|
156
166
|
}
|
|
157
167
|
}
|
package/package.json
CHANGED
|
@@ -1,81 +1,83 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly",
|
|
3
|
-
"version": "3.9.
|
|
4
|
-
"description": "Befly - 为 Bun 专属打造的 TypeScript API 接口框架核心引擎",
|
|
5
|
-
"type": "module",
|
|
3
|
+
"version": "3.9.39",
|
|
6
4
|
"private": false,
|
|
7
|
-
"
|
|
8
|
-
"access": "public",
|
|
9
|
-
"registry": "https://registry.npmjs.org"
|
|
10
|
-
},
|
|
11
|
-
"main": "main.ts",
|
|
12
|
-
"types": "main.ts",
|
|
13
|
-
"exports": {
|
|
14
|
-
".": {
|
|
15
|
-
"types": "./main.ts",
|
|
16
|
-
"default": "./main.ts"
|
|
17
|
-
},
|
|
18
|
-
"./*": "./*"
|
|
19
|
-
},
|
|
20
|
-
"scripts": {
|
|
21
|
-
"test": "bun test",
|
|
22
|
-
"bundler": "bun build ./main.ts --outfile ./main.single.ts --minify --target bun"
|
|
23
|
-
},
|
|
5
|
+
"description": "Befly - 为 Bun 专属打造的 TypeScript API 接口框架核心引擎",
|
|
24
6
|
"keywords": [
|
|
25
|
-
"bun",
|
|
26
7
|
"api",
|
|
27
|
-
"
|
|
8
|
+
"backend",
|
|
9
|
+
"bun",
|
|
28
10
|
"core",
|
|
29
|
-
"
|
|
11
|
+
"framework",
|
|
12
|
+
"http",
|
|
30
13
|
"javascript",
|
|
31
|
-
"backend",
|
|
32
14
|
"rest",
|
|
33
|
-
"
|
|
34
|
-
"
|
|
15
|
+
"type-safe",
|
|
16
|
+
"typescript"
|
|
35
17
|
],
|
|
36
|
-
"author": "chensuiyi <bimostyle@qq.com>",
|
|
37
18
|
"homepage": "https://chensuiyi.me",
|
|
19
|
+
"author": "chensuiyi <bimostyle@qq.com>",
|
|
38
20
|
"license": "Apache-2.0",
|
|
21
|
+
"type": "module",
|
|
22
|
+
"main": "main.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./main.ts",
|
|
26
|
+
"default": "./main.ts"
|
|
27
|
+
},
|
|
28
|
+
"./lib/*": "./lib/*.ts",
|
|
29
|
+
"./types/*": "./types/*.d.ts",
|
|
30
|
+
"./utils/*": "./utils/*.ts"
|
|
31
|
+
},
|
|
32
|
+
"types": "main.ts",
|
|
39
33
|
"files": [
|
|
34
|
+
".npmrc",
|
|
35
|
+
".prettierignore",
|
|
36
|
+
".prettierrc",
|
|
37
|
+
"LICENSE",
|
|
38
|
+
"README.md",
|
|
39
|
+
"befly.config.ts",
|
|
40
|
+
"bunfig.toml",
|
|
40
41
|
"checks",
|
|
41
|
-
"docs",
|
|
42
42
|
"dist",
|
|
43
|
+
"docs",
|
|
43
44
|
"hooks",
|
|
44
45
|
"lib",
|
|
45
46
|
"loader",
|
|
47
|
+
"main.ts",
|
|
48
|
+
"package.json",
|
|
49
|
+
"paths.ts",
|
|
46
50
|
"plugins",
|
|
47
51
|
"router",
|
|
48
52
|
"sync",
|
|
49
53
|
"tests",
|
|
50
|
-
"types",
|
|
51
|
-
".npmrc",
|
|
52
|
-
".prettierignore",
|
|
53
|
-
".prettierrc",
|
|
54
|
-
"befly.config.ts",
|
|
55
|
-
"bunfig.toml",
|
|
56
|
-
"LICENSE",
|
|
57
|
-
"main.ts",
|
|
58
|
-
"paths.ts",
|
|
59
|
-
"package.json",
|
|
60
|
-
"README.md",
|
|
61
54
|
"tsconfig.json",
|
|
62
|
-
"
|
|
55
|
+
"types",
|
|
56
|
+
"utils"
|
|
63
57
|
],
|
|
64
|
-
"
|
|
65
|
-
"
|
|
58
|
+
"scripts": {
|
|
59
|
+
"test": "bun test",
|
|
60
|
+
"bundler": "bun build ./main.ts --outfile ./main.single.ts --minify --target bun"
|
|
66
61
|
},
|
|
67
62
|
"dependencies": {
|
|
68
|
-
"befly-shared": "^1.2.8",
|
|
69
63
|
"chalk": "^5.6.2",
|
|
70
64
|
"es-toolkit": "^1.43.0",
|
|
71
65
|
"fast-jwt": "^6.1.0",
|
|
72
66
|
"fast-xml-parser": "^5.3.3",
|
|
67
|
+
"merge-anything": "^6.0.6",
|
|
73
68
|
"pathe": "^2.0.3",
|
|
74
69
|
"pino": "^10.1.0",
|
|
75
70
|
"pino-roll": "^4.0.0"
|
|
76
71
|
},
|
|
77
|
-
"gitHead": "e078b94f087ddf21f289b55ab86c0b8dce105807",
|
|
78
72
|
"devDependencies": {
|
|
79
73
|
"typescript": "^5.9.3"
|
|
80
|
-
}
|
|
74
|
+
},
|
|
75
|
+
"engines": {
|
|
76
|
+
"bun": ">=1.3.0"
|
|
77
|
+
},
|
|
78
|
+
"publishConfig": {
|
|
79
|
+
"access": "public",
|
|
80
|
+
"registry": "https://registry.npmjs.org"
|
|
81
|
+
},
|
|
82
|
+
"gitHead": "d42bf60ba6b8b11fef7e759fa008ada075829772"
|
|
81
83
|
}
|
package/paths.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
2
|
* Befly 框架路径配置
|
|
3
3
|
*
|
|
4
4
|
* 提供统一的路径常量,供整个框架使用
|
|
@@ -10,8 +10,9 @@
|
|
|
10
10
|
*
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
|
-
import { fileURLToPath } from
|
|
14
|
-
|
|
13
|
+
import { fileURLToPath } from "node:url";
|
|
14
|
+
|
|
15
|
+
import { dirname, join } from "pathe";
|
|
15
16
|
|
|
16
17
|
// 当前文件的路径信息
|
|
17
18
|
const __filename = fileURLToPath(import.meta.url);
|
|
@@ -30,35 +31,35 @@ export const coreDir = __dirname;
|
|
|
30
31
|
* @description packages/core/checks/
|
|
31
32
|
* @usage 存放启动检查模块(返回 boolean 的 default 函数)
|
|
32
33
|
*/
|
|
33
|
-
export const coreCheckDir = join(__dirname,
|
|
34
|
+
export const coreCheckDir = join(__dirname, "checks");
|
|
34
35
|
|
|
35
36
|
/**
|
|
36
37
|
* Core 框架插件目录
|
|
37
38
|
* @description packages/core/plugins/
|
|
38
39
|
* @usage 存放内置插件(db, logger, redis, tool 等)
|
|
39
40
|
*/
|
|
40
|
-
export const corePluginDir = join(__dirname,
|
|
41
|
+
export const corePluginDir = join(__dirname, "plugins");
|
|
41
42
|
|
|
42
43
|
/**
|
|
43
44
|
* Core 框架钩子目录
|
|
44
45
|
* @description packages/core/hooks/
|
|
45
46
|
* @usage 存放内置钩子(auth, cors, parser 等)
|
|
46
47
|
*/
|
|
47
|
-
export const coreHookDir = join(__dirname,
|
|
48
|
+
export const coreHookDir = join(__dirname, "hooks");
|
|
48
49
|
|
|
49
50
|
/**
|
|
50
51
|
* Core 框架 API 目录
|
|
51
52
|
* @description packages/core/apis/
|
|
52
53
|
* @usage 存放框架级别的 API 接口
|
|
53
54
|
*/
|
|
54
|
-
export const coreApiDir = join(__dirname,
|
|
55
|
+
export const coreApiDir = join(__dirname, "apis");
|
|
55
56
|
|
|
56
57
|
/**
|
|
57
58
|
* Core 框架表定义目录
|
|
58
59
|
* @description packages/core/tables/
|
|
59
60
|
* @usage 存放框架核心表定义(JSON 格式)
|
|
60
61
|
*/
|
|
61
|
-
export const coreTableDir = join(__dirname,
|
|
62
|
+
export const coreTableDir = join(__dirname, "tables");
|
|
62
63
|
|
|
63
64
|
// ==================== 用户项目路径 ====================
|
|
64
65
|
|
|
@@ -74,39 +75,39 @@ export const projectDir = process.cwd();
|
|
|
74
75
|
* @description {projectDir}/checks/
|
|
75
76
|
* @usage 存放用户自定义启动检查模块
|
|
76
77
|
*/
|
|
77
|
-
export const projectCheckDir = join(projectDir,
|
|
78
|
+
export const projectCheckDir = join(projectDir, "checks");
|
|
78
79
|
|
|
79
80
|
/**
|
|
80
81
|
* 项目插件目录
|
|
81
82
|
* @description {projectDir}/plugins/
|
|
82
83
|
* @usage 存放用户自定义插件
|
|
83
84
|
*/
|
|
84
|
-
export const projectPluginDir = join(projectDir,
|
|
85
|
+
export const projectPluginDir = join(projectDir, "plugins");
|
|
85
86
|
|
|
86
87
|
/**
|
|
87
88
|
* 项目钩子目录
|
|
88
89
|
* @description {projectDir}/hooks/
|
|
89
90
|
* @usage 存放用户自定义钩子
|
|
90
91
|
*/
|
|
91
|
-
export const projectHookDir = join(projectDir,
|
|
92
|
+
export const projectHookDir = join(projectDir, "hooks");
|
|
92
93
|
|
|
93
94
|
/**
|
|
94
95
|
* 项目 API 目录
|
|
95
96
|
* @description {projectDir}/apis/
|
|
96
97
|
* @usage 存放用户业务 API 接口
|
|
97
98
|
*/
|
|
98
|
-
export const projectApiDir = join(projectDir,
|
|
99
|
+
export const projectApiDir = join(projectDir, "apis");
|
|
99
100
|
|
|
100
101
|
/**
|
|
101
102
|
* 项目表定义目录
|
|
102
103
|
* @description {projectDir}/tables/
|
|
103
104
|
* @usage 存放用户业务表定义(JSON 格式)
|
|
104
105
|
*/
|
|
105
|
-
export const projectTableDir = join(projectDir,
|
|
106
|
+
export const projectTableDir = join(projectDir, "tables");
|
|
106
107
|
|
|
107
108
|
/**
|
|
108
109
|
* 项目组件目录
|
|
109
110
|
* @description {projectDir}/addons/
|
|
110
111
|
* @usage 存放本地组件(优先级高于 node_modules 中的组件)
|
|
111
112
|
*/
|
|
112
|
-
export const projectAddonsDir = join(projectDir,
|
|
113
|
+
export const projectAddonsDir = join(projectDir, "addons");
|
package/plugins/cache.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
2
|
* 缓存插件 - TypeScript 版本
|
|
3
3
|
* 负责在服务器启动时缓存接口、菜单和角色权限到 Redis
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {
|
|
7
|
-
import type { Plugin } from
|
|
8
|
-
|
|
6
|
+
import type { BeflyContext } from "../types/befly.js";
|
|
7
|
+
import type { Plugin } from "../types/plugin.js";
|
|
8
|
+
|
|
9
|
+
import { CacheHelper } from "../lib/cacheHelper.js";
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* 缓存插件
|
package/plugins/cipher.ts
CHANGED
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
* 提供加密解密功能
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {
|
|
6
|
+
import type { Plugin } from "../types/plugin.js";
|
|
7
7
|
|
|
8
|
-
import
|
|
8
|
+
import { Cipher } from "../lib/cipher.js";
|
|
9
9
|
|
|
10
10
|
export default {
|
|
11
|
-
name:
|
|
11
|
+
name: "cipher",
|
|
12
12
|
handler: () => {
|
|
13
13
|
return Cipher;
|
|
14
14
|
}
|
package/plugins/config.ts
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
* 配置插件
|
|
3
3
|
* 提供访问项目配置的能力
|
|
4
4
|
*/
|
|
5
|
-
import {
|
|
5
|
+
import type { Plugin } from "../types/plugin.js";
|
|
6
6
|
|
|
7
|
-
import
|
|
7
|
+
import { beflyConfig } from "../befly.config.js";
|
|
8
8
|
|
|
9
9
|
const plugin: Plugin = {
|
|
10
10
|
handler: () => {
|
package/plugins/db.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
2
|
* 数据库插件
|
|
3
3
|
* 初始化数据库连接和 SQL 管理器
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import { DbHelper } from '../lib/dbHelper.js';
|
|
6
|
+
import type { BeflyContext } from "../types/befly.js";
|
|
7
|
+
import type { Plugin } from "../types/plugin.js";
|
|
9
8
|
|
|
10
|
-
import
|
|
11
|
-
import
|
|
9
|
+
import { Connect } from "../lib/connect.js";
|
|
10
|
+
import { DbHelper } from "../lib/dbHelper.js";
|
|
11
|
+
import { Logger } from "../lib/logger.js";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* 数据库插件
|
|
15
15
|
*/
|
|
16
16
|
const dbPlugin: Plugin = {
|
|
17
|
-
after: [
|
|
17
|
+
after: ["logger"],
|
|
18
18
|
async handler(befly: BeflyContext): Promise<DbHelper> {
|
|
19
19
|
let sql: any = null;
|
|
20
20
|
|
|
@@ -26,14 +26,14 @@ const dbPlugin: Plugin = {
|
|
|
26
26
|
|
|
27
27
|
return dbManager;
|
|
28
28
|
} catch (error: any) {
|
|
29
|
-
Logger.error({ err: error },
|
|
29
|
+
Logger.error({ err: error }, "数据库初始化失败");
|
|
30
30
|
|
|
31
31
|
// 清理资源
|
|
32
32
|
if (sql) {
|
|
33
33
|
try {
|
|
34
34
|
await sql.close();
|
|
35
35
|
} catch (cleanupError: any) {
|
|
36
|
-
Logger.error({ err: cleanupError },
|
|
36
|
+
Logger.error({ err: cleanupError }, "清理连接池失败");
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
package/plugins/jwt.ts
CHANGED
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
* JWT 插件
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
import {
|
|
6
|
-
import { beflyConfig } from '../befly.config.js';
|
|
5
|
+
import type { Plugin } from "../types/plugin.js";
|
|
7
6
|
|
|
8
|
-
import
|
|
7
|
+
import { beflyConfig } from "../befly.config.js";
|
|
8
|
+
import { Jwt } from "../lib/jwt.js";
|
|
9
9
|
|
|
10
10
|
const jwtPlugin: Plugin = {
|
|
11
11
|
handler: () => {
|
package/plugins/logger.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
2
|
* 日志插件
|
|
3
3
|
* 提供全局日志功能
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {
|
|
7
|
-
import { beflyConfig } from '../befly.config.js';
|
|
6
|
+
import type { Plugin } from "../types/plugin.js";
|
|
8
7
|
|
|
9
|
-
import
|
|
8
|
+
import { beflyConfig } from "../befly.config.js";
|
|
9
|
+
import { Logger } from "../lib/logger.js";
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* 日志插件
|
|
@@ -14,15 +14,11 @@ import type { Plugin } from '../types/plugin.js';
|
|
|
14
14
|
const loggerPlugin: Plugin = {
|
|
15
15
|
after: [],
|
|
16
16
|
async handler(): Promise<typeof Logger> {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
Logger.configure(beflyConfig.logger);
|
|
21
|
-
}
|
|
22
|
-
return Logger;
|
|
23
|
-
} catch (error: any) {
|
|
24
|
-
throw error;
|
|
17
|
+
// 配置 Logger
|
|
18
|
+
if (beflyConfig.logger) {
|
|
19
|
+
Logger.configure(beflyConfig.logger);
|
|
25
20
|
}
|
|
21
|
+
return Logger;
|
|
26
22
|
}
|
|
27
23
|
};
|
|
28
24
|
|
package/plugins/redis.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
2
|
* Redis 插件
|
|
3
3
|
* 初始化 Redis 连接和助手工具
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import {
|
|
7
|
-
import { Connect } from '../lib/connect.js';
|
|
8
|
-
import { RedisHelper } from '../lib/redisHelper.js';
|
|
9
|
-
import { beflyConfig } from '../befly.config.js';
|
|
6
|
+
import type { Plugin } from "../types/plugin.js";
|
|
10
7
|
|
|
11
|
-
import
|
|
8
|
+
import { beflyConfig } from "../befly.config.js";
|
|
9
|
+
import { Connect } from "../lib/connect.js";
|
|
10
|
+
import { Logger } from "../lib/logger.js";
|
|
11
|
+
import { RedisHelper } from "../lib/redisHelper.js";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* Redis 插件
|
|
15
15
|
*/
|
|
16
16
|
const redisPlugin: Plugin = {
|
|
17
|
-
after: [
|
|
17
|
+
after: ["logger"],
|
|
18
18
|
async handler(): Promise<RedisHelper | Record<string, never>> {
|
|
19
19
|
const redisConfig = beflyConfig.redis || {};
|
|
20
20
|
try {
|
|
@@ -24,7 +24,7 @@ const redisPlugin: Plugin = {
|
|
|
24
24
|
// 返回 RedisHelper 实例
|
|
25
25
|
return new RedisHelper(redisConfig.prefix);
|
|
26
26
|
} catch (error: any) {
|
|
27
|
-
Logger.error({ err: error },
|
|
27
|
+
Logger.error({ err: error }, "Redis 初始化失败");
|
|
28
28
|
throw error;
|
|
29
29
|
}
|
|
30
30
|
}
|
package/plugins/tool.ts
CHANGED
|
@@ -3,9 +3,9 @@
|
|
|
3
3
|
* 提供常用的工具函数
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
import type { RequestContext } from "../types/context.js";
|
|
6
7
|
// 类型导入
|
|
7
|
-
import type { Plugin } from
|
|
8
|
-
import type { RequestContext } from '../types/context.js';
|
|
8
|
+
import type { Plugin } from "../types/plugin.js";
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
11
|
* 成功响应
|
|
@@ -87,23 +87,23 @@ export function Raw(ctx: RequestContext, data: Record<string, any> | string, opt
|
|
|
87
87
|
let finalContentType = contentType;
|
|
88
88
|
let body: string;
|
|
89
89
|
|
|
90
|
-
if (typeof data ===
|
|
90
|
+
if (typeof data === "string") {
|
|
91
91
|
// 字符串类型
|
|
92
92
|
body = data;
|
|
93
93
|
if (!finalContentType) {
|
|
94
94
|
// 自动判断:XML 或纯文本
|
|
95
|
-
finalContentType = data.trim().startsWith(
|
|
95
|
+
finalContentType = data.trim().startsWith("<") ? "application/xml" : "text/plain";
|
|
96
96
|
}
|
|
97
97
|
} else {
|
|
98
98
|
// 对象类型,JSON 序列化
|
|
99
99
|
body = JSON.stringify(data);
|
|
100
|
-
finalContentType = finalContentType ||
|
|
100
|
+
finalContentType = finalContentType || "application/json";
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
// 合并响应头
|
|
104
104
|
const responseHeaders = {
|
|
105
105
|
...ctx.corsHeaders,
|
|
106
|
-
|
|
106
|
+
"Content-Type": finalContentType,
|
|
107
107
|
...headers
|
|
108
108
|
};
|
|
109
109
|
|