fix(stores): 配置文件形状不符先备份再回退;日志尾部同步落盘
- commands/connections/settings 三个 store 原先只对 JSON.parse 抛错做 .bak 备份,合法 JSON 但形状不符(如 [1,2,3])会静默回退空/默认,下次 写盘把命令库/书签凭据/自定义主题整体销毁。现统一为形状不符也先备份 (与 knownHosts 的 fail-closed 策略对齐),三个 store 测试补对应用例 - sanitizeRules 回退分支返回预设副本而非模块级共享数组引用,并在回退时 记录 warning(原先连警告都没有) - 会话日志尾部:logStop 触发的最后一轮 flush 改同步落盘(在途异步写未 落地时由 drain 循环做保序的最终同步轮),before-quit 不再丢日志尾巴
This commit is contained in:
1 parent
e23010f950
commit
172cce1962
6 files changed
+253
-19
No files matched your search
@@ -167,6 +167,15 @@ function backupUnparseableConnections(file: string, err: unknown): void {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The single exit for an unusable connections.json — parse failure and wrong
|
||||
* shape both end here, so neither can quietly skip the backup.
|
||||
*/
|
||||
function discardConnectionsFile(file: string, err: unknown): StoredConnection[] {
|
||||
backupUnparseableConnections(file, err)
|
||||
return []
|
||||
}
|
||||
|
||||
export class ConnectionsStore {
|
||||
constructor(private readonly filePath: string) {}
|
||||
|
||||
@@ -182,11 +191,17 @@ export class ConnectionsStore {
|
||||
typeof (x as StoredConnection).host === 'string'
|
||||
)
|
||||
}
|
||||
// Valid JSON of the wrong shape (`{}`) is not "no bookmarks": it is a file
|
||||
// we cannot read, and the next write would rewrite it from an empty list.
|
||||
// Same exit as the parse failure below.
|
||||
return discardConnectionsFile(
|
||||
this.filePath,
|
||||
new Error('connections.json shape mismatch: expected an array')
|
||||
)
|
||||
} catch (err) {
|
||||
// missing -> start fresh; unreadable / corrupt -> keep the original first
|
||||
backupUnparseableConnections(this.filePath, err)
|
||||
return discardConnectionsFile(this.filePath, err)
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
private save(list: StoredConnection[]): void {
|
||||
|
||||
Reference in new issue
Block a user