befly 3.10.16 → 3.10.17
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/befly.config.ts +43 -33
- package/main.ts +2 -2
- package/package.json +2 -2
package/befly.config.ts
CHANGED
|
@@ -89,43 +89,53 @@ const defaultOptions: BeflyOptions = {
|
|
|
89
89
|
addons: {}
|
|
90
90
|
};
|
|
91
91
|
|
|
92
|
-
|
|
93
|
-
cwd?: string;
|
|
94
|
-
nodeEnv?: string;
|
|
95
|
-
};
|
|
92
|
+
const beflyConfigCache: Map<string, Promise<BeflyOptions>> = new Map();
|
|
96
93
|
|
|
97
|
-
export async function loadBeflyConfig(
|
|
98
|
-
const nodeEnv =
|
|
94
|
+
export async function loadBeflyConfig(): Promise<BeflyOptions> {
|
|
95
|
+
const nodeEnv = process.env.NODE_ENV || defaultOptions.nodeEnv || "development";
|
|
99
96
|
const envSuffix = nodeEnv === "production" ? "production" : "development";
|
|
100
97
|
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
dirs: ["configs"],
|
|
106
|
-
files: ["befly.common", `befly.${envSuffix}`],
|
|
107
|
-
extensions: [".json"],
|
|
108
|
-
mode: "merge",
|
|
109
|
-
defaults: defaultOptions
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
// 配置校验:redis.prefix 作为 key 前缀,由 RedisHelper 统一拼接 ":"。
|
|
113
|
-
// 因此 prefix 本身不允许包含 ":",否则会导致 key 结构出现空段或多段分隔(例如 "prefix::key"),
|
|
114
|
-
// 在 RedisInsight 等工具里可能显示 [NO NAME] 空分组,且容易造成 key 管理混乱。
|
|
115
|
-
const redisPrefix = (config as any)?.redis?.prefix;
|
|
116
|
-
if (typeof redisPrefix === "string") {
|
|
117
|
-
const trimmedPrefix = redisPrefix.trim();
|
|
118
|
-
if (trimmedPrefix.includes(":")) {
|
|
119
|
-
throw new Error(`配置错误:redis.prefix 不允许包含 ':'(RedisHelper 会自动拼接分隔符 ':'),请改为不带冒号的前缀,例如 'befly_demo',当前值=${redisPrefix}`);
|
|
120
|
-
}
|
|
98
|
+
const cacheKey = nodeEnv;
|
|
99
|
+
const cached = beflyConfigCache.get(cacheKey);
|
|
100
|
+
if (cached) {
|
|
101
|
+
return await cached;
|
|
121
102
|
}
|
|
122
103
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
104
|
+
const promise = (async () => {
|
|
105
|
+
// 使用 scanConfig 一次性加载并合并所有配置文件
|
|
106
|
+
// 合并顺序:defaultOptions ← befly.common.json ← befly.development/production.json
|
|
107
|
+
const config = await scanConfig({
|
|
108
|
+
dirs: ["configs"],
|
|
109
|
+
files: ["befly.common", `befly.${envSuffix}`],
|
|
110
|
+
extensions: [".json"],
|
|
111
|
+
mode: "merge",
|
|
112
|
+
defaults: defaultOptions
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
// 配置校验:redis.prefix 作为 key 前缀,由 RedisHelper 统一拼接 ":"。
|
|
116
|
+
// 因此 prefix 本身不允许包含 ":",否则会导致 key 结构出现空段或多段分隔(例如 "prefix::key"),
|
|
117
|
+
// 在 RedisInsight 等工具里可能显示 [NO NAME] 空分组,且容易造成 key 管理混乱。
|
|
118
|
+
const redisPrefix = (config as any)?.redis?.prefix;
|
|
119
|
+
if (typeof redisPrefix === "string") {
|
|
120
|
+
const trimmedPrefix = redisPrefix.trim();
|
|
121
|
+
if (trimmedPrefix.includes(":")) {
|
|
122
|
+
throw new Error(`配置错误:redis.prefix 不允许包含 ':'(RedisHelper 会自动拼接分隔符 ':'),请改为不带冒号的前缀,例如 'befly_demo',当前值=${redisPrefix}`);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
127
125
|
|
|
128
|
-
|
|
129
|
-
|
|
126
|
+
// 预编译 disableMenus 的 Bun.Glob 规则:
|
|
127
|
+
// - 提前暴露配置错误(fail-fast)
|
|
128
|
+
// - 后续 checkMenu 会复用同一进程级缓存
|
|
129
|
+
compileDisableMenuGlobRules((config as any)?.disableMenus);
|
|
130
130
|
|
|
131
|
-
|
|
131
|
+
return config as BeflyOptions;
|
|
132
|
+
})();
|
|
133
|
+
|
|
134
|
+
beflyConfigCache.set(cacheKey, promise);
|
|
135
|
+
try {
|
|
136
|
+
return await promise;
|
|
137
|
+
} catch (error: any) {
|
|
138
|
+
beflyConfigCache.delete(cacheKey);
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
}
|
package/main.ts
CHANGED
|
@@ -65,8 +65,8 @@ export class Befly {
|
|
|
65
65
|
const serverStartTime = Bun.nanoseconds();
|
|
66
66
|
|
|
67
67
|
// 0. 延迟加载配置(避免循环依赖)
|
|
68
|
-
const {
|
|
69
|
-
this.config =
|
|
68
|
+
const { loadBeflyConfig } = await import("./befly.config.js");
|
|
69
|
+
this.config = await loadBeflyConfig();
|
|
70
70
|
|
|
71
71
|
// 将配置注入到 ctx,供插件/Hook/sync 等按需读取
|
|
72
72
|
this.context.config = this.config;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly",
|
|
3
|
-
"version": "3.10.
|
|
4
|
-
"gitHead": "
|
|
3
|
+
"version": "3.10.17",
|
|
4
|
+
"gitHead": "f3285b9ba4e510e6e903fbf800c96480d56ce36b",
|
|
5
5
|
"private": false,
|
|
6
6
|
"description": "Befly - 为 Bun 专属打造的 TypeScript API 接口框架核心引擎",
|
|
7
7
|
"keywords": [
|