| 160 | } |
| 161 | |
| 162 | private static async init(): Promise<void> { |
| 163 | if (this.initialized) return; |
| 164 | |
| 165 | // 简单的锁机制防止并发初始化 |
| 166 | while (this.initLock) { |
| 167 | await new Promise(resolve => setTimeout(resolve, 10)); |
| 168 | } |
| 169 | |
| 170 | if (this.initialized) return; |
| 171 | this.initLock = true; |
| 172 | |
| 173 | try { |
| 174 | this.configPath = path.join( |
| 175 | createDirectoryInAssets("sshkey"), |
| 176 | "sshkey_config.json" |
| 177 | ); |
| 178 | this.db = await JSONFilePreset<Record<string, any>>( |
| 179 | this.configPath, |
| 180 | { ...DEFAULT_CONFIG } |
| 181 | ); |
| 182 | this.initialized = true; |
| 183 | } catch (error) { |
| 184 | console.error("[ssh] 初始化配置失败:", error); |
| 185 | } finally { |
| 186 | this.initLock = false; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | static async get(key: string, defaultValue?: string): Promise<string> { |
| 191 | await this.init(); |