befly 3.9.40 → 3.10.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.
Files changed (144) hide show
  1. package/README.md +47 -19
  2. package/befly.config.ts +19 -2
  3. package/checks/checkApi.ts +79 -77
  4. package/checks/checkHook.ts +48 -0
  5. package/checks/checkMenu.ts +168 -0
  6. package/checks/checkPlugin.ts +48 -0
  7. package/checks/checkTable.ts +137 -183
  8. package/docs/README.md +17 -11
  9. package/docs/api/api.md +16 -2
  10. package/docs/guide/quickstart.md +31 -10
  11. package/docs/hooks/hook.md +2 -2
  12. package/docs/hooks/rateLimit.md +1 -1
  13. package/docs/infra/redis.md +26 -14
  14. package/docs/plugins/plugin.md +23 -21
  15. package/docs/quickstart.md +5 -328
  16. package/docs/reference/addon.md +0 -4
  17. package/docs/reference/config.md +14 -31
  18. package/docs/reference/logger.md +3 -3
  19. package/docs/reference/sync.md +132 -237
  20. package/docs/reference/table.md +28 -30
  21. package/hooks/auth.ts +3 -4
  22. package/hooks/cors.ts +4 -6
  23. package/hooks/parser.ts +3 -4
  24. package/hooks/permission.ts +3 -4
  25. package/hooks/validator.ts +3 -4
  26. package/lib/cacheHelper.ts +89 -153
  27. package/lib/cacheKeys.ts +1 -1
  28. package/lib/connect.ts +9 -13
  29. package/lib/dbDialect.ts +285 -0
  30. package/lib/dbHelper.ts +179 -507
  31. package/lib/dbUtils.ts +450 -0
  32. package/lib/logger.ts +41 -5
  33. package/lib/redisHelper.ts +1 -0
  34. package/lib/sqlBuilder.ts +358 -58
  35. package/lib/sqlCheck.ts +136 -0
  36. package/lib/validator.ts +1 -1
  37. package/loader/loadApis.ts +23 -126
  38. package/loader/loadHooks.ts +31 -46
  39. package/loader/loadPlugins.ts +37 -52
  40. package/main.ts +58 -19
  41. package/package.json +24 -25
  42. package/paths.ts +14 -14
  43. package/plugins/cache.ts +12 -6
  44. package/plugins/cipher.ts +2 -2
  45. package/plugins/config.ts +6 -8
  46. package/plugins/db.ts +14 -19
  47. package/plugins/jwt.ts +6 -7
  48. package/plugins/logger.ts +7 -9
  49. package/plugins/redis.ts +8 -10
  50. package/plugins/tool.ts +3 -4
  51. package/router/api.ts +3 -2
  52. package/router/static.ts +7 -5
  53. package/sync/syncApi.ts +80 -235
  54. package/sync/syncCache.ts +16 -0
  55. package/sync/syncDev.ts +167 -202
  56. package/sync/syncMenu.ts +230 -444
  57. package/sync/syncTable.ts +1247 -0
  58. package/tests/_mocks/mockSqliteDb.ts +204 -0
  59. package/tests/addonHelper-cache.test.ts +32 -0
  60. package/tests/apiHandler-routePath-only.test.ts +32 -0
  61. package/tests/cacheHelper.test.ts +16 -51
  62. package/tests/checkApi-routePath-strict.test.ts +166 -0
  63. package/tests/checkMenu.test.ts +346 -0
  64. package/tests/checkTable-smoke.test.ts +157 -0
  65. package/tests/dbDialect-cache.test.ts +23 -0
  66. package/tests/dbDialect.test.ts +46 -0
  67. package/tests/dbHelper-advanced.test.ts +1 -1
  68. package/tests/dbHelper-all-array-types.test.ts +15 -15
  69. package/tests/dbHelper-batch-write.test.ts +90 -0
  70. package/tests/dbHelper-columns.test.ts +36 -54
  71. package/tests/dbHelper-execute.test.ts +26 -26
  72. package/tests/dbHelper-joins.test.ts +85 -176
  73. package/tests/fixtures/scanFilesAddon/node_modules/@befly-addon/demo/apis/sub/b.ts +3 -0
  74. package/tests/fixtures/scanFilesApis/a.ts +3 -0
  75. package/tests/fixtures/scanFilesApis/sub/b.ts +3 -0
  76. package/tests/loadPlugins-order-smoke.test.ts +75 -0
  77. package/tests/logger.test.ts +6 -6
  78. package/tests/redisHelper.test.ts +6 -1
  79. package/tests/scanFiles-routePath.test.ts +46 -0
  80. package/tests/smoke-sql.test.ts +24 -0
  81. package/tests/sqlBuilder-advanced.test.ts +18 -5
  82. package/tests/sqlBuilder.test.ts +24 -0
  83. package/tests/sync-init-guard.test.ts +105 -0
  84. package/tests/syncApi-insBatch-fields-consistent.test.ts +61 -0
  85. package/tests/syncApi-obsolete-records.test.ts +69 -0
  86. package/tests/syncApi-type-compat.test.ts +72 -0
  87. package/tests/syncDev-permissions.test.ts +81 -0
  88. package/tests/syncMenu-disableMenus-hard-delete.test.ts +88 -0
  89. package/tests/syncMenu-duplicate-path.test.ts +122 -0
  90. package/tests/syncMenu-obsolete-records.test.ts +161 -0
  91. package/tests/syncMenu-parentPath-from-tree.test.ts +75 -0
  92. package/tests/syncMenu-paths.test.ts +0 -9
  93. package/tests/{syncDb-apply.test.ts → syncTable-apply.test.ts} +14 -24
  94. package/tests/{syncDb-array-number.test.ts → syncTable-array-number.test.ts} +31 -31
  95. package/tests/syncTable-constants.test.ts +101 -0
  96. package/tests/syncTable-db-integration.test.ts +237 -0
  97. package/tests/{syncDb-ddl.test.ts → syncTable-ddl.test.ts} +67 -53
  98. package/tests/{syncDb-helpers.test.ts → syncTable-helpers.test.ts} +12 -26
  99. package/tests/syncTable-schema.test.ts +99 -0
  100. package/tests/syncTable-testkit.test.ts +25 -0
  101. package/tests/syncTable-types.test.ts +122 -0
  102. package/tests/tableRef-and-deserialize.test.ts +67 -0
  103. package/tsconfig.json +1 -1
  104. package/types/api.d.ts +1 -1
  105. package/types/befly.d.ts +13 -12
  106. package/types/cache.d.ts +2 -2
  107. package/types/context.d.ts +1 -1
  108. package/types/database.d.ts +0 -5
  109. package/types/hook.d.ts +1 -10
  110. package/types/plugin.d.ts +2 -96
  111. package/types/sync.d.ts +19 -25
  112. package/utils/convertBigIntFields.ts +38 -0
  113. package/utils/disableMenusGlob.ts +85 -0
  114. package/utils/importDefault.ts +21 -0
  115. package/utils/isDirentDirectory.ts +23 -0
  116. package/utils/loadMenuConfigs.ts +145 -0
  117. package/utils/processFields.ts +25 -0
  118. package/utils/scanAddons.ts +72 -0
  119. package/utils/scanFiles.ts +129 -21
  120. package/utils/scanSources.ts +64 -0
  121. package/utils/sortModules.ts +137 -0
  122. package/checks/checkApp.ts +0 -55
  123. package/docs/cipher.md +0 -582
  124. package/docs/database.md +0 -1176
  125. package/hooks/rateLimit.ts +0 -276
  126. package/sync/syncAll.ts +0 -35
  127. package/sync/syncDb/apply.ts +0 -192
  128. package/sync/syncDb/constants.ts +0 -119
  129. package/sync/syncDb/ddl.ts +0 -251
  130. package/sync/syncDb/helpers.ts +0 -84
  131. package/sync/syncDb/schema.ts +0 -202
  132. package/sync/syncDb/sqlite.ts +0 -48
  133. package/sync/syncDb/table.ts +0 -207
  134. package/sync/syncDb/tableCreate.ts +0 -163
  135. package/sync/syncDb/types.ts +0 -132
  136. package/sync/syncDb/version.ts +0 -69
  137. package/sync/syncDb.ts +0 -168
  138. package/tests/rateLimit-hook.test.ts +0 -477
  139. package/tests/syncDb-constants.test.ts +0 -130
  140. package/tests/syncDb-schema.test.ts +0 -179
  141. package/tests/syncDb-types.test.ts +0 -139
  142. package/utils/addonHelper.ts +0 -90
  143. package/utils/modules.ts +0 -98
  144. package/utils/route.ts +0 -23
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "befly",
3
- "version": "3.9.40",
3
+ "version": "3.10.1",
4
+ "gitHead": "123125b9d7a6c26879a5b5c9d3ac05e2082141c6",
4
5
  "private": false,
5
6
  "description": "Befly - 为 Bun 专属打造的 TypeScript API 接口框架核心引擎",
6
7
  "keywords": [
@@ -16,26 +17,10 @@
16
17
  "typescript"
17
18
  ],
18
19
  "homepage": "https://chensuiyi.me",
19
- "author": "chensuiyi <bimostyle@qq.com>",
20
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",
21
+ "author": "chensuiyi <bimostyle@qq.com>",
33
22
  "files": [
34
23
  ".npmrc",
35
- ".prettierignore",
36
- ".prettierrc",
37
- "LICENSE",
38
- "README.md",
39
24
  "befly.config.ts",
40
25
  "bunfig.toml",
41
26
  "checks",
@@ -43,11 +28,13 @@
43
28
  "docs",
44
29
  "hooks",
45
30
  "lib",
31
+ "LICENSE",
46
32
  "loader",
47
33
  "main.ts",
48
34
  "package.json",
49
35
  "paths.ts",
50
36
  "plugins",
37
+ "README.md",
51
38
  "router",
52
39
  "sync",
53
40
  "tests",
@@ -55,11 +42,28 @@
55
42
  "types",
56
43
  "utils"
57
44
  ],
45
+ "type": "module",
46
+ "main": "main.ts",
47
+ "types": "main.ts",
48
+ "exports": {
49
+ ".": {
50
+ "types": "./main.ts",
51
+ "default": "./main.ts"
52
+ },
53
+ "./lib/*": "./lib/*.ts",
54
+ "./types/*": "./types/*.d.ts",
55
+ "./utils/*": "./utils/*.ts"
56
+ },
57
+ "publishConfig": {
58
+ "access": "public",
59
+ "registry": "https://registry.npmjs.org"
60
+ },
58
61
  "scripts": {
59
62
  "test": "bun test",
60
- "bundler": "bun build ./main.ts --outfile ./main.single.ts --minify --target bun"
63
+ "typecheck": "bunx tsgo --noEmit"
61
64
  },
62
65
  "dependencies": {
66
+ "befly-shared": "1.3.0",
63
67
  "chalk": "^5.6.2",
64
68
  "es-toolkit": "^1.43.0",
65
69
  "fast-jwt": "^6.1.0",
@@ -74,10 +78,5 @@
74
78
  },
75
79
  "engines": {
76
80
  "bun": ">=1.3.0"
77
- },
78
- "publishConfig": {
79
- "access": "public",
80
- "registry": "https://registry.npmjs.org"
81
- },
82
- "gitHead": "0ba53d2bbdc6a7f2659ef60256cda7302fe585b0"
81
+ }
83
82
  }
package/paths.ts CHANGED
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * 路径分类:
8
8
  * - root* 系列:Core 框架内部路径(packages/core/*)
9
- * - project* 系列:用户项目路径(process.cwd()/*)
9
+ * - app* 系列:用户项目路径(process.cwd()/*)
10
10
  *
11
11
  */
12
12
 
@@ -68,46 +68,46 @@ export const coreTableDir = join(__dirname, "tables");
68
68
  * @description process.cwd()
69
69
  * @usage 用户项目的根目录
70
70
  */
71
- export const projectDir = process.cwd();
71
+ export const appDir = process.cwd();
72
72
 
73
73
  /**
74
74
  * 项目检查目录
75
- * @description {projectDir}/checks/
75
+ * @description {appDir}/checks/
76
76
  * @usage 存放用户自定义启动检查模块
77
77
  */
78
- export const projectCheckDir = join(projectDir, "checks");
78
+ export const appCheckDir = join(appDir, "checks");
79
79
 
80
80
  /**
81
81
  * 项目插件目录
82
- * @description {projectDir}/plugins/
82
+ * @description {appDir}/plugins/
83
83
  * @usage 存放用户自定义插件
84
84
  */
85
- export const projectPluginDir = join(projectDir, "plugins");
85
+ export const appPluginDir = join(appDir, "plugins");
86
86
 
87
87
  /**
88
88
  * 项目钩子目录
89
- * @description {projectDir}/hooks/
89
+ * @description {appDir}/hooks/
90
90
  * @usage 存放用户自定义钩子
91
91
  */
92
- export const projectHookDir = join(projectDir, "hooks");
92
+ export const appHookDir = join(appDir, "hooks");
93
93
 
94
94
  /**
95
95
  * 项目 API 目录
96
- * @description {projectDir}/apis/
96
+ * @description {appDir}/apis/
97
97
  * @usage 存放用户业务 API 接口
98
98
  */
99
- export const projectApiDir = join(projectDir, "apis");
99
+ export const appApiDir = join(appDir, "apis");
100
100
 
101
101
  /**
102
102
  * 项目表定义目录
103
- * @description {projectDir}/tables/
103
+ * @description {appDir}/tables/
104
104
  * @usage 存放用户业务表定义(JSON 格式)
105
105
  */
106
- export const projectTableDir = join(projectDir, "tables");
106
+ export const appTableDir = join(appDir, "tables");
107
107
 
108
108
  /**
109
109
  * 项目组件目录
110
- * @description {projectDir}/addons/
110
+ * @description {appDir}/addons/
111
111
  * @usage 存放本地组件(优先级高于 node_modules 中的组件)
112
112
  */
113
- export const projectAddonsDir = join(projectDir, "addons");
113
+ export const appAddonsDir = join(appDir, "addons");
package/plugins/cache.ts CHANGED
@@ -11,11 +11,17 @@ import { CacheHelper } from "../lib/cacheHelper.js";
11
11
  /**
12
12
  * 缓存插件
13
13
  */
14
- const cachePlugin: Plugin = {
15
- after: [],
14
+ export default {
15
+ deps: ["logger", "redis", "db"],
16
16
  async handler(befly: BeflyContext): Promise<CacheHelper> {
17
- return new CacheHelper(befly);
18
- }
19
- };
17
+ if (!(befly as any).db) {
18
+ throw new Error("缓存初始化失败:ctx.db 未初始化(Db 插件未加载或注入失败)");
19
+ }
20
+
21
+ if (!(befly as any).redis) {
22
+ throw new Error("缓存初始化失败:ctx.redis 未初始化(Redis 插件未加载或注入失败)");
23
+ }
20
24
 
21
- export default cachePlugin;
25
+ return new CacheHelper({ db: befly.db, redis: befly.redis });
26
+ }
27
+ } satisfies Plugin;
package/plugins/cipher.ts CHANGED
@@ -8,8 +8,8 @@ import type { Plugin } from "../types/plugin.js";
8
8
  import { Cipher } from "../lib/cipher.js";
9
9
 
10
10
  export default {
11
- name: "cipher",
11
+ deps: [],
12
12
  handler: () => {
13
13
  return Cipher;
14
14
  }
15
- } as Plugin;
15
+ } satisfies Plugin;
package/plugins/config.ts CHANGED
@@ -2,14 +2,12 @@
2
2
  * 配置插件
3
3
  * 提供访问项目配置的能力
4
4
  */
5
+ import type { BeflyContext } from "../types/befly.js";
5
6
  import type { Plugin } from "../types/plugin.js";
6
7
 
7
- import { beflyConfig } from "../befly.config.js";
8
-
9
- const plugin: Plugin = {
10
- handler: () => {
11
- return beflyConfig;
8
+ export default {
9
+ deps: [],
10
+ handler: (context: BeflyContext) => {
11
+ return context.config;
12
12
  }
13
- };
14
-
15
- export default plugin;
13
+ } satisfies Plugin;
package/plugins/db.ts CHANGED
@@ -7,39 +7,34 @@ import type { BeflyContext } from "../types/befly.js";
7
7
  import type { Plugin } from "../types/plugin.js";
8
8
 
9
9
  import { Connect } from "../lib/connect.js";
10
+ import { getDialectByName } from "../lib/dbDialect.js";
10
11
  import { DbHelper } from "../lib/dbHelper.js";
11
12
  import { Logger } from "../lib/logger.js";
12
13
 
13
14
  /**
14
15
  * 数据库插件
15
16
  */
16
- const dbPlugin: Plugin = {
17
- after: ["logger"],
17
+ export default {
18
+ deps: ["logger", "redis"],
18
19
  async handler(befly: BeflyContext): Promise<DbHelper> {
19
- let sql: any = null;
20
+ if (!(befly as any).redis) {
21
+ throw new Error("数据库初始化失败:ctx.redis 未初始化(Redis 插件未加载或注入失败)");
22
+ }
20
23
 
21
24
  try {
22
- sql = await Connect.connectSql();
25
+ const sql = Connect.getSql();
26
+
27
+ const rawDbType = befly.config && befly.config.db ? befly.config.db.type : undefined;
28
+ const resolvedDbType = rawDbType === "postgres" ? "postgresql" : rawDbType;
29
+ const dialect = getDialectByName(resolvedDbType === "postgresql" || resolvedDbType === "sqlite" ? resolvedDbType : "mysql");
23
30
 
24
- // 创建数据库管理器实例,直接传入 sql 对象
25
- const dbManager = new DbHelper(befly, sql);
31
+ // 创建数据库管理器实例
32
+ const dbManager = new DbHelper({ redis: befly.redis, sql: sql, dialect: dialect });
26
33
 
27
34
  return dbManager;
28
35
  } catch (error: any) {
29
36
  Logger.error({ err: error }, "数据库初始化失败");
30
-
31
- // 清理资源
32
- if (sql) {
33
- try {
34
- await sql.close();
35
- } catch (cleanupError: any) {
36
- Logger.error({ err: cleanupError }, "清理连接池失败");
37
- }
38
- }
39
-
40
37
  throw error;
41
38
  }
42
39
  }
43
- };
44
-
45
- export default dbPlugin;
40
+ } satisfies Plugin;
package/plugins/jwt.ts CHANGED
@@ -2,15 +2,14 @@
2
2
  * JWT 插件
3
3
  */
4
4
 
5
+ import type { BeflyContext } from "../types/befly.js";
5
6
  import type { Plugin } from "../types/plugin.js";
6
7
 
7
- import { beflyConfig } from "../befly.config.js";
8
8
  import { Jwt } from "../lib/jwt.js";
9
9
 
10
- const jwtPlugin: Plugin = {
11
- handler: () => {
12
- return new Jwt(beflyConfig.auth);
10
+ export default {
11
+ deps: [],
12
+ handler: (context: BeflyContext) => {
13
+ return new Jwt(context.config ? context.config.auth : undefined);
13
14
  }
14
- };
15
-
16
- export default jwtPlugin;
15
+ } satisfies Plugin;
package/plugins/logger.ts CHANGED
@@ -3,23 +3,21 @@
3
3
  * 提供全局日志功能
4
4
  */
5
5
 
6
+ import type { BeflyContext } from "../types/befly.js";
6
7
  import type { Plugin } from "../types/plugin.js";
7
8
 
8
- import { beflyConfig } from "../befly.config.js";
9
9
  import { Logger } from "../lib/logger.js";
10
10
 
11
11
  /**
12
12
  * 日志插件
13
13
  */
14
- const loggerPlugin: Plugin = {
15
- after: [],
16
- async handler(): Promise<typeof Logger> {
14
+ export default {
15
+ deps: [],
16
+ async handler(context: BeflyContext): Promise<typeof Logger> {
17
17
  // 配置 Logger
18
- if (beflyConfig.logger) {
19
- Logger.configure(beflyConfig.logger);
18
+ if (context.config && context.config.logger) {
19
+ Logger.configure(context.config.logger);
20
20
  }
21
21
  return Logger;
22
22
  }
23
- };
24
-
25
- export default loggerPlugin;
23
+ } satisfies Plugin;
package/plugins/redis.ts CHANGED
@@ -3,9 +3,9 @@
3
3
  * 初始化 Redis 连接和助手工具
4
4
  */
5
5
 
6
+ import type { BeflyContext } from "../types/befly.js";
6
7
  import type { Plugin } from "../types/plugin.js";
7
8
 
8
- import { beflyConfig } from "../befly.config.js";
9
9
  import { Connect } from "../lib/connect.js";
10
10
  import { Logger } from "../lib/logger.js";
11
11
  import { RedisHelper } from "../lib/redisHelper.js";
@@ -13,13 +13,13 @@ import { RedisHelper } from "../lib/redisHelper.js";
13
13
  /**
14
14
  * Redis 插件
15
15
  */
16
- const redisPlugin: Plugin = {
17
- after: ["logger"],
18
- async handler(): Promise<RedisHelper | Record<string, never>> {
19
- const redisConfig = beflyConfig.redis || {};
16
+ export default {
17
+ deps: ["logger"],
18
+ async handler(context: BeflyContext): Promise<RedisHelper | Record<string, never>> {
19
+ const redisConfig = context.config && context.config.redis ? context.config.redis : {};
20
20
  try {
21
- // 初始化 Redis 客户端
22
- await Connect.connectRedis();
21
+ // 启动期已建立 Redis 连接;这里仅校验连接存在
22
+ Connect.getRedis();
23
23
 
24
24
  // 返回 RedisHelper 实例
25
25
  return new RedisHelper(redisConfig.prefix);
@@ -28,6 +28,4 @@ const redisPlugin: Plugin = {
28
28
  throw error;
29
29
  }
30
30
  }
31
- };
32
-
33
- export default redisPlugin;
31
+ } satisfies Plugin;
package/plugins/tool.ts CHANGED
@@ -113,7 +113,8 @@ export function Raw(ctx: RequestContext, data: Record<string, any> | string, opt
113
113
  });
114
114
  }
115
115
 
116
- const plugin: Plugin = {
116
+ export default {
117
+ deps: [],
117
118
  handler: () => {
118
119
  return {
119
120
  Yes: Yes,
@@ -121,6 +122,4 @@ const plugin: Plugin = {
121
122
  Raw: Raw
122
123
  };
123
124
  }
124
- };
125
-
126
- export default plugin;
125
+ } satisfies Plugin;
package/router/api.ts CHANGED
@@ -15,7 +15,6 @@ import { Logger } from "../lib/logger.js";
15
15
  import { genShortId } from "../utils/genShortId.js";
16
16
  import { getClientIp } from "../utils/getClientIp.js";
17
17
  import { FinalResponse } from "../utils/response.js";
18
- import { makeRouteKey } from "../utils/route.js";
19
18
 
20
19
  /**
21
20
  * API处理器工厂函数
@@ -30,7 +29,9 @@ export function apiHandler(apis: Map<string, ApiRoute>, hooks: Hook[], context:
30
29
 
31
30
  // 2. 创建请求上下文
32
31
  const url = new URL(req.url);
33
- const apiPath = makeRouteKey(req.method, url.pathname);
32
+ // 只用接口路径做存在性判断与匹配:不要把 method 拼进 key
33
+ // 说明:apisMap 的 key 来源于 scanFiles/loadApis 生成的 routePath(例如 /api/core/xxx)
34
+ const apiPath = url.pathname || "/";
34
35
 
35
36
  const clientIp = getClientIp(req, server);
36
37
 
package/router/static.ts CHANGED
@@ -3,25 +3,27 @@
3
3
  * 处理 /* 路径的静态文件请求
4
4
  */
5
5
 
6
+ // 类型导入
7
+ import type { CorsConfig } from "../types/befly.js";
8
+
6
9
  // 外部依赖
7
10
  import { join } from "pathe";
8
11
 
9
- import { beflyConfig } from "../befly.config.js";
10
12
  import { Logger } from "../lib/logger.js";
11
13
  // 相对导入
12
- import { projectDir } from "../paths.js";
14
+ import { appDir } from "../paths.js";
13
15
  import { setCorsOptions } from "../utils/cors.js";
14
16
 
15
17
  /**
16
18
  * 静态文件处理器工厂
17
19
  */
18
- export function staticHandler() {
20
+ export function staticHandler(corsConfig: CorsConfig | undefined = undefined) {
19
21
  return async (req: Request): Promise<Response> => {
20
22
  // 设置 CORS 响应头
21
- const corsHeaders = setCorsOptions(req, beflyConfig.cors);
23
+ const corsHeaders = setCorsOptions(req, corsConfig);
22
24
 
23
25
  const url = new URL(req.url);
24
- const filePath = join(projectDir, "public", url.pathname);
26
+ const filePath = join(appDir, "public", url.pathname);
25
27
 
26
28
  try {
27
29
  // OPTIONS预检请求