feat: make get config sync

This commit is contained in:
shinya
2025-07-13 20:04:30 +08:00
parent 6f4678c32a
commit c325cfaa84
14 changed files with 28 additions and 25 deletions

View File

@@ -44,6 +44,10 @@ let fileConfig: ConfigFileStruct;
let cachedConfig: AdminConfig;
async function initConfig() {
if (cachedConfig) {
return;
}
if (process.env.DOCKER_ENV === 'true') {
// 这里用 eval("require") 避开静态分析,防止 Edge Runtime 打包时报 "Can't resolve 'fs'"
// 在实际 Node.js 运行时才会执行到,因此不会影响 Edge 环境。
@@ -216,9 +220,8 @@ async function initConfig() {
}
}
initConfig();
export function getConfig(): AdminConfig {
export async function getConfig(): Promise<AdminConfig> {
await initConfig();
return cachedConfig;
}
@@ -283,13 +286,13 @@ export async function resetConfig() {
cachedConfig.SourceConfig = adminConfig.SourceConfig;
}
export function getCacheTime(): number {
const config = getConfig();
export async function getCacheTime(): Promise<number> {
const config = await getConfig();
return config.SiteConfig.SiteInterfaceCacheTime || 7200;
}
export function getAvailableApiSites(): ApiSite[] {
const config = getConfig();
export async function getAvailableApiSites(): Promise<ApiSite[]> {
const config = await getConfig();
return config.SourceConfig.filter((s) => !s.disabled).map((s) => ({
key: s.key,
name: s.name,