befly 3.10.18 → 3.11.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/README.md +83 -307
- package/dist/befly.config.d.ts +7 -0
- package/{befly.config.ts → dist/befly.config.js} +11 -36
- package/dist/befly.js +15621 -0
- package/dist/befly.min.js +21 -0
- package/dist/checks/checkApi.d.ts +1 -0
- package/{checks/checkApi.ts → dist/checks/checkApi.js} +12 -30
- package/dist/checks/checkHook.d.ts +1 -0
- package/dist/checks/checkHook.js +86 -0
- 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/dist/checks/checkPlugin.js +86 -0
- 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 +7 -0
- package/{hooks/auth.ts → dist/hooks/auth.js} +8 -10
- package/dist/hooks/cors.d.ts +11 -0
- package/{hooks/cors.ts → dist/hooks/cors.js} +5 -13
- package/dist/hooks/parser.d.ts +14 -0
- package/{hooks/parser.ts → dist/hooks/parser.js} +31 -45
- package/dist/hooks/permission.d.ts +14 -0
- package/{hooks/permission.ts → dist/hooks/permission.js} +16 -25
- package/dist/hooks/validator.d.ts +11 -0
- package/{hooks/validator.ts → dist/hooks/validator.js} +9 -14
- package/dist/index.d.ts +26 -0
- package/{main.ts → dist/index.js} +61 -100
- 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} +82 -241
- package/dist/lib/dbUtils.d.ts +68 -0
- package/{lib/dbUtils.ts → dist/lib/dbUtils.js} +51 -126
- package/dist/lib/jwt.d.ts +13 -0
- package/{lib/jwt.ts → dist/lib/jwt.js} +11 -32
- package/dist/lib/logger.d.ts +42 -0
- package/dist/lib/logger.js +1144 -0
- 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} +10 -20
- package/dist/loader/loadHooks.d.ts +7 -0
- package/dist/loader/loadHooks.js +35 -0
- package/dist/loader/loadPlugins.d.ts +8 -0
- package/{loader/loadPlugins.ts → dist/loader/loadPlugins.js} +14 -26
- package/dist/paths.d.ts +93 -0
- package/{paths.ts → dist/paths.js} +6 -19
- package/dist/plugins/cache.d.ts +16 -0
- package/{plugins/cache.ts → dist/plugins/cache.js} +7 -12
- package/dist/plugins/cipher.d.ts +12 -0
- package/{plugins/cipher.ts → dist/plugins/cipher.js} +4 -6
- package/dist/plugins/config.d.ts +12 -0
- package/dist/plugins/config.js +8 -0
- package/dist/plugins/db.d.ts +16 -0
- package/{plugins/db.ts → dist/plugins/db.js} +11 -17
- package/dist/plugins/jwt.d.ts +12 -0
- package/dist/plugins/jwt.js +12 -0
- package/dist/plugins/logger.d.ts +32 -0
- package/{plugins/logger.ts → dist/plugins/logger.js} +5 -8
- package/dist/plugins/redis.d.ts +16 -0
- package/{plugins/redis.ts → dist/plugins/redis.js} +9 -12
- package/dist/plugins/tool.d.ts +81 -0
- package/{plugins/tool.ts → dist/plugins/tool.js} +9 -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 +296 -0
- package/dist/sync/syncApi.d.ts +3 -0
- package/{sync/syncApi.ts → dist/sync/syncApi.js} +35 -55
- 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} +172 -379
- package/{types → dist/types}/api.d.ts +12 -51
- package/dist/types/api.js +4 -0
- package/{types → dist/types}/befly.d.ts +32 -227
- 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 +3 -5
- 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 +17 -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 +59 -0
- package/dist/types/logger.js +6 -0
- package/dist/types/plugin.d.ts +16 -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/calcPerfTime.d.ts +4 -0
- package/{utils/calcPerfTime.ts → dist/utils/calcPerfTime.js} +3 -3
- 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/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/loadMenuConfigs.d.ts +29 -0
- package/{utils/loadMenuConfigs.ts → dist/utils/loadMenuConfigs.js} +66 -52
- package/dist/utils/mergeAndConcat.d.ts +7 -0
- package/dist/utils/mergeAndConcat.js +72 -0
- package/dist/utils/processAtSymbol.d.ts +4 -0
- package/{utils/processFields.ts → dist/utils/processAtSymbol.js} +5 -9
- package/dist/utils/processInfo.d.ts +24 -0
- package/{utils/process.ts → dist/utils/processInfo.js} +2 -18
- 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} +7 -41
- package/dist/utils/scanConfig.d.ts +26 -0
- package/{utils/scanConfig.ts → dist/utils/scanConfig.js} +28 -66
- package/dist/utils/scanCoreBuiltins.d.ts +3 -0
- package/dist/utils/scanCoreBuiltins.js +65 -0
- package/dist/utils/scanFiles.d.ts +30 -0
- package/{utils/scanFiles.ts → dist/utils/scanFiles.js} +44 -71
- package/dist/utils/scanSources.d.ts +10 -0
- package/dist/utils/scanSources.js +46 -0
- package/dist/utils/sortModules.d.ts +28 -0
- package/{utils/sortModules.ts → dist/utils/sortModules.js} +26 -66
- package/dist/utils/util.d.ts +84 -0
- package/dist/utils/util.js +262 -0
- package/package.json +26 -34
- package/.gitignore +0 -0
- package/bunfig.toml +0 -3
- package/checks/checkHook.ts +0 -48
- package/checks/checkPlugin.ts +0 -48
- package/configs/presetRegexp.ts +0 -225
- 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/lib/logger.ts +0 -811
- package/loader/loadHooks.ts +0 -51
- 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/arrayKeysToCamel.ts +0 -18
- package/utils/configTypes.ts +0 -3
- package/utils/genShortId.ts +0 -12
- package/utils/importDefault.ts +0 -21
- package/utils/keysToCamel.ts +0 -22
- package/utils/keysToSnake.ts +0 -22
- package/utils/pickFields.ts +0 -19
- package/utils/scanSources.ts +0 -64
- package/utils/sqlLog.ts +0 -37
package/loader/loadHooks.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 钩子加载器
|
|
3
|
-
* 默认加载所有来源钩子(core/addon/app)
|
|
4
|
-
* 可通过 disableHooks 禁用指定钩子
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
// 类型导入
|
|
8
|
-
import type { Hook } from "../types/hook.ts";
|
|
9
|
-
import type { ScanFileResult } from "../utils/scanFiles.ts";
|
|
10
|
-
|
|
11
|
-
import { Logger } from "../lib/logger.ts";
|
|
12
|
-
import { sortModules } from "../utils/sortModules.ts";
|
|
13
|
-
|
|
14
|
-
export async function loadHooks(hooks: ScanFileResult[], disableHooks: string[] = []): Promise<Hook[]> {
|
|
15
|
-
const hooksMap: Hook[] = [];
|
|
16
|
-
|
|
17
|
-
if (disableHooks.length > 0) {
|
|
18
|
-
Logger.info({ hooks: disableHooks }, "禁用钩子");
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const enabledHooks = hooks.filter((item: any) => {
|
|
22
|
-
const moduleName = item?.moduleName;
|
|
23
|
-
if (typeof moduleName !== "string" || moduleName.trim() === "") {
|
|
24
|
-
return false;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (disableHooks.includes(moduleName)) {
|
|
28
|
-
return false;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return true;
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
const sortedHooks = sortModules(enabledHooks, { moduleLabel: "钩子" });
|
|
35
|
-
if (sortedHooks === false) {
|
|
36
|
-
throw new Error("钩子依赖关系错误");
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
for (const item of sortedHooks) {
|
|
40
|
-
const hookName = (item as any).moduleName as string;
|
|
41
|
-
const hook = item as any as Hook;
|
|
42
|
-
|
|
43
|
-
hooksMap.push({
|
|
44
|
-
name: hookName,
|
|
45
|
-
deps: hook.deps,
|
|
46
|
-
handler: hook.handler
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return hooksMap;
|
|
51
|
-
}
|
package/plugins/config.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 配置插件
|
|
3
|
-
* 提供访问项目配置的能力
|
|
4
|
-
*/
|
|
5
|
-
import type { BeflyContext } from "../types/befly.ts";
|
|
6
|
-
import type { Plugin } from "../types/plugin.ts";
|
|
7
|
-
|
|
8
|
-
export default {
|
|
9
|
-
deps: [],
|
|
10
|
-
handler: (context: BeflyContext) => {
|
|
11
|
-
return context.config;
|
|
12
|
-
}
|
|
13
|
-
} satisfies Plugin;
|
package/plugins/jwt.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* JWT 插件
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import type { BeflyContext } from "../types/befly.ts";
|
|
6
|
-
import type { Plugin } from "../types/plugin.ts";
|
|
7
|
-
|
|
8
|
-
import { Jwt } from "../lib/jwt.ts";
|
|
9
|
-
|
|
10
|
-
export default {
|
|
11
|
-
deps: [],
|
|
12
|
-
handler: (context: BeflyContext) => {
|
|
13
|
-
return new Jwt(context.config ? context.config.auth : undefined);
|
|
14
|
-
}
|
|
15
|
-
} satisfies Plugin;
|
package/router/api.ts
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* API路由处理器
|
|
3
|
-
* 处理 /api/* 路径的请求
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import type { ApiRoute } from "../types/api.ts";
|
|
7
|
-
import type { BeflyContext } from "../types/befly.ts";
|
|
8
|
-
// 类型导入
|
|
9
|
-
import type { RequestContext } from "../types/context.ts";
|
|
10
|
-
import type { Hook } from "../types/hook.ts";
|
|
11
|
-
|
|
12
|
-
import { withCtx } from "../lib/asyncContext.ts";
|
|
13
|
-
import { Logger } from "../lib/logger.ts";
|
|
14
|
-
// 相对导入
|
|
15
|
-
import { genShortId } from "../utils/genShortId.ts";
|
|
16
|
-
import { getClientIp } from "../utils/getClientIp.ts";
|
|
17
|
-
import { FinalResponse } from "../utils/response.ts";
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* API处理器工厂函数
|
|
21
|
-
* @param apis - API路由映射表
|
|
22
|
-
* @param hooks - 钩子列表
|
|
23
|
-
* @param context - 应用上下文
|
|
24
|
-
*/
|
|
25
|
-
export function apiHandler(apis: Map<string, ApiRoute>, hooks: Hook[], context: BeflyContext) {
|
|
26
|
-
return async (req: Request, server?: any): Promise<Response> => {
|
|
27
|
-
// 1. 生成请求 ID
|
|
28
|
-
const requestId = genShortId();
|
|
29
|
-
|
|
30
|
-
// 2. 创建请求上下文
|
|
31
|
-
const url = new URL(req.url);
|
|
32
|
-
// 只用接口路径做存在性判断与匹配:不要把 method 拼进 key
|
|
33
|
-
// 说明:apisMap 的 key 来源于 scanFiles/loadApis 生成的 routePath(例如 /api/core/xxx)
|
|
34
|
-
const apiPath = url.pathname || "/";
|
|
35
|
-
|
|
36
|
-
const clientIp = getClientIp(req, server);
|
|
37
|
-
|
|
38
|
-
const now = Date.now();
|
|
39
|
-
|
|
40
|
-
const ctx: RequestContext = {
|
|
41
|
-
method: req.method,
|
|
42
|
-
body: {},
|
|
43
|
-
user: {},
|
|
44
|
-
req: req,
|
|
45
|
-
now: now,
|
|
46
|
-
ip: clientIp,
|
|
47
|
-
headers: req.headers,
|
|
48
|
-
route: apiPath,
|
|
49
|
-
requestId: requestId,
|
|
50
|
-
corsHeaders: {
|
|
51
|
-
"X-Request-ID": requestId
|
|
52
|
-
},
|
|
53
|
-
api: apis.get(apiPath),
|
|
54
|
-
response: undefined,
|
|
55
|
-
result: undefined
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
return withCtx(
|
|
59
|
-
{
|
|
60
|
-
requestId: requestId,
|
|
61
|
-
method: req.method,
|
|
62
|
-
route: apiPath,
|
|
63
|
-
ip: clientIp,
|
|
64
|
-
now: now
|
|
65
|
-
},
|
|
66
|
-
async () => {
|
|
67
|
-
try {
|
|
68
|
-
// 4. 串联执行所有钩子
|
|
69
|
-
for (const hook of hooks) {
|
|
70
|
-
await hook.handler(context, ctx);
|
|
71
|
-
|
|
72
|
-
// 如果钩子已经设置了 response,停止执行
|
|
73
|
-
if (ctx.response) {
|
|
74
|
-
return ctx.response;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
// hooks 全部通过后记录请求日志(拦截请求仅由 ErrorResponse 记录)
|
|
79
|
-
if (ctx.api && req.method !== "OPTIONS") {
|
|
80
|
-
const logData: Record<string, any> = {
|
|
81
|
-
event: "request",
|
|
82
|
-
apiName: ctx.api.name
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
if (ctx.body && Object.keys(ctx.body).length > 0) {
|
|
86
|
-
logData.body = ctx.body;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
Logger.info(logData, "request");
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
// 5. 执行 API handler
|
|
93
|
-
if (!ctx.api) {
|
|
94
|
-
if (req.method !== "OPTIONS") {
|
|
95
|
-
ctx.response = Response.json(
|
|
96
|
-
{
|
|
97
|
-
code: 1,
|
|
98
|
-
msg: "接口不存在"
|
|
99
|
-
},
|
|
100
|
-
{
|
|
101
|
-
headers: ctx.corsHeaders
|
|
102
|
-
}
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
} else if (ctx.api.handler) {
|
|
106
|
-
const result = await ctx.api.handler(context, ctx);
|
|
107
|
-
|
|
108
|
-
if (result instanceof Response) {
|
|
109
|
-
ctx.response = result;
|
|
110
|
-
} else {
|
|
111
|
-
ctx.result = result;
|
|
112
|
-
}
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// 7. 返回响应(自动处理 response/result/日志)
|
|
116
|
-
return FinalResponse(ctx);
|
|
117
|
-
} catch (err: any) {
|
|
118
|
-
// 全局错误处理
|
|
119
|
-
const errorPath = ctx.api ? apiPath : req.url;
|
|
120
|
-
Logger.error({ err: err, path: errorPath }, "请求错误");
|
|
121
|
-
ctx.result = {
|
|
122
|
-
code: 1,
|
|
123
|
-
msg: "内部服务错误"
|
|
124
|
-
};
|
|
125
|
-
return FinalResponse(ctx);
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
|
-
);
|
|
129
|
-
};
|
|
130
|
-
}
|