fix: drain accepted exports and close failed store initialization

This commit is contained in:
abaishu0730 committed 2026-10-19 09:11:40 +08:00
1 parent 54e5bafd86
commit a420d2d9fb
12 files changed
+204 -71

No files matched your search

+13 -7
View File
@@ -10,13 +10,19 @@ const text=(v,label,max=200)=>{if(typeof v!=='string'||!v.trim()||v.length>max||
const now=()=>new Date().toISOString();
class StorageStore{
constructor(dir){
fs.mkdirSync(dir,{recursive:true});this.vault=new BpmVault({stateDir:path.join(dir,'secrets')});
this.db=new DatabaseSync(path.join(dir,'storage.sqlite'));this.db.exec(`PRAGMA journal_mode=WAL; PRAGMA busy_timeout=5000; PRAGMA secure_delete=ON;
CREATE TABLE IF NOT EXISTS servers(id TEXT PRIMARY KEY,version INTEGER NOT NULL,payload BLOB NOT NULL,state TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS settings(id INTEGER PRIMARY KEY CHECK(id=1),value TEXT NOT NULL);
INSERT OR IGNORE INTO settings VALUES(1,'{"serverId":"","enabled":false,"since":null}');
CREATE TABLE IF NOT EXISTS archives(id TEXT PRIMARY KEY,server_id TEXT NOT NULL,job_id TEXT NOT NULL,status TEXT NOT NULL,value TEXT NOT NULL,UNIQUE(server_id,job_id));
UPDATE archives SET status='interrupted' WHERE status='running';`);
try {
fs.mkdirSync(dir,{recursive:true});this.vault=new BpmVault({stateDir:path.join(dir,'secrets')});
this.db=new DatabaseSync(path.join(dir,'storage.sqlite'));this.db.exec(`PRAGMA journal_mode=WAL; PRAGMA busy_timeout=5000; PRAGMA secure_delete=ON;
CREATE TABLE IF NOT EXISTS servers(id TEXT PRIMARY KEY,version INTEGER NOT NULL,payload BLOB NOT NULL,state TEXT NOT NULL);
CREATE TABLE IF NOT EXISTS settings(id INTEGER PRIMARY KEY CHECK(id=1),value TEXT NOT NULL);
INSERT OR IGNORE INTO settings VALUES(1,'{"serverId":"","enabled":false,"since":null}');
CREATE TABLE IF NOT EXISTS archives(id TEXT PRIMARY KEY,server_id TEXT NOT NULL,job_id TEXT NOT NULL,status TEXT NOT NULL,value TEXT NOT NULL,UNIQUE(server_id,job_id));
UPDATE archives SET status='interrupted' WHERE status='running';`);
} catch(error) {
try { this.db?.close(); } catch {}
try { this.vault?.destroy(); } catch {}
throw error;
}
}
row(id){if(typeof id!=='string')fail('请选择存储服务器。');const row=this.db.prepare('SELECT * FROM servers WHERE id=?').get(id);if(!row)fail('存储服务器不存在。',404);return row;}
secret(id){const row=this.row(id);try{const saved=this.vault.decode(id,row.payload);return {authMode:'credentials',passwordless:false,...saved,id,version:row.version,state:JSON.parse(row.state)};}catch{fail('存储凭据无法解密,请联系管理员。',503);}}