From 8f5bcd94bf90650310f2495f8dc68841452a72f9 Mon Sep 17 00:00:00 2001 From: Sucukdeluxe <259325684+Sucukdeluxe@users.noreply.github.com> Date: Sun, 18 Oct 2026 03:03:58 +0200 Subject: [PATCH] Make upload history access asynchronous and release 2.1.70 --- lib/config-store.js | 42 ++++++++++++++++++++++------------- lib/diagnostics-collectors.js | 17 +++++++------- main.js | 8 +++---- package-lock.json | 4 ++-- package.json | 2 +- 5 files changed, 42 insertions(+), 31 deletions(-) diff --git a/lib/config-store.js b/lib/config-store.js index 7f2c54f..a317527 100644 --- a/lib/config-store.js +++ b/lib/config-store.js @@ -207,7 +207,7 @@ class ConfigStore { } } - _readHistoryFile() { + _readHistoryFileSync() { try { const raw = fs.readFileSync(this.historyPath, 'utf-8'); if (!raw || raw.trim().length < 2) return []; @@ -220,6 +220,24 @@ class ConfigStore { } } + async _readHistoryFile() { + let raw; + try { + raw = await fs.promises.readFile(this.historyPath, 'utf-8'); + } catch (error) { + if (error.code === 'ENOENT') return []; + throw error; + } + if (!raw || !raw.trim()) return []; + let parsed; + try { parsed = JSON.parse(raw); } catch {} + if (Array.isArray(parsed)) return parsed; + if (parsed && Array.isArray(parsed.history)) return parsed.history; + const error = new Error('Die Verlaufsdatei ist beschädigt und wurde nicht verändert'); + error.code = 'HISTORY_CORRUPT'; + throw error; + } + _writeHistoryFileDurable(arr) { const tmp = this.historyPath + '.tmp'; const fd = fs.openSync(tmp, 'w'); @@ -267,14 +285,14 @@ class ConfigStore { _migrateHistory() { try { if (fs.existsSync(this.historyPath)) { - this._historyMigrated = Array.isArray(this._readHistoryFile()); + this._historyMigrated = Array.isArray(this._readHistoryFileSync()); return; } let cfg = null; try { cfg = this._readAndParse(this.filePath); } catch {} const hist = (cfg && Array.isArray(cfg.history)) ? cfg.history : []; this._writeHistoryFileDurable(hist); - const check = this._readHistoryFile(); + const check = this._readHistoryFileSync(); if (Array.isArray(check) && check.length === hist.length) { if (hist.length > 0) { try { fs.copyFileSync(this.filePath, this.filePath + '.pre-history-split.bak'); } catch {} @@ -769,9 +787,9 @@ class ConfigStore { }); } - loadHistory() { + async loadHistory() { if (this._historyMigrated) { - return this._readHistoryFile() || []; + return this._readHistoryFile(); } const config = this.load(); return config.history || []; @@ -802,12 +820,8 @@ class ConfigStore { appendHistory(entry) { if (this._historyMigrated) { - return this._enqueueHistoryWrite(() => { - const cur = this._readHistoryFile(); - if (cur === null && fs.existsSync(this.historyPath)) { - throw new Error('Die Verlaufsdatei ist beschädigt und wurde nicht verändert'); - } - const arr = cur || []; + return this._enqueueHistoryWrite(async () => { + const arr = await this._readHistoryFile(); arr.push(entry); const gs = this.load().globalSettings; const retention = (gs && gs.historyRetention) || 'all'; @@ -828,11 +842,7 @@ class ConfigStore { const dryRun = !!opts.dryRun; if (this._historyMigrated) { return this._enqueueHistoryWrite(async () => { - const storedHistory = this._readHistoryFile(); - if (storedHistory === null && fs.existsSync(this.historyPath)) { - throw new Error('Die Verlaufsdatei ist beschädigt und wurde nicht verändert'); - } - const current = storedHistory || []; + const current = await this._readHistoryFile(); const beforeBatches = current.length; const beforeRows = countHistoryRows(current); const pruned = applyHistoryRetention(current, retention, Date.now()); diff --git a/lib/diagnostics-collectors.js b/lib/diagnostics-collectors.js index 026967b..84ca6d5 100644 --- a/lib/diagnostics-collectors.js +++ b/lib/diagnostics-collectors.js @@ -169,10 +169,10 @@ function createCollectors(deps) { return { errors, byCategory }; } - function listErrors(args) { + async function listErrors(args, historySnapshot) { const a = args || {}; const cfg = loadConfig(); - const history = typeof loadHistory === 'function' ? loadHistory() : cfg.history; + const history = historySnapshot ?? (typeof loadHistory === 'function' ? await loadHistory() : cfg.history); const { errors, byCategory } = _historyErrors(history, a); const limit = Math.min(Math.max(Number(a.limit) || 100, 1), 1000); const window = Number.isFinite(a.sinceMs) ? `since ${new Date(a.sinceMs).toISOString()}` : 'all history'; @@ -208,11 +208,11 @@ function createCollectors(deps) { return result; } - function getHistory(args) { + async function getHistory(args, historySnapshot) { const a = args || {}; - const history = typeof loadHistory === 'function' - ? (loadHistory() || []) - : (Array.isArray(loadConfig().history) ? loadConfig().history : []); + const history = historySnapshot ?? (typeof loadHistory === 'function' + ? (await loadHistory() || []) + : (Array.isArray(loadConfig().history) ? loadConfig().history : [])); const limit = Math.min(Math.max(Number(a.limit) || 20, 1), 200); const perHoster = stats.summarizePerHoster(history); const recent = [...history].slice(-limit).reverse(); @@ -254,9 +254,10 @@ function createCollectors(deps) { const a = args || {}; const errorLimit = Math.min(Math.max(Number(a.errorLimit) || 20, 1), 200); const errArgs = Number.isFinite(a.errorSinceMs) ? { sinceMs: a.errorSinceMs, limit: errorLimit } : { limit: errorLimit }; - const errors = listErrors(errArgs); + const historySnapshot = typeof loadHistory === 'function' ? await loadHistory() : loadConfig().history; + const errors = await listErrors(errArgs, historySnapshot); const queue = getQueueState({ includeJobs: false }); - const history = getHistory({ limit: 5 }); + const history = await getHistory({ limit: 5 }, historySnapshot); const warnings = []; if (queue.source === 'persisted' && queue.stale) warnings.push('queue state is from the persisted snapshot (may lag live state; UploadManager not introspected in this build).'); if (errors.total > 0) warnings.push(`${errors.total} non-success result(s) in the error window.`); diff --git a/main.js b/main.js index 0b4a3b0..f7e794e 100644 --- a/main.js +++ b/main.js @@ -2208,13 +2208,13 @@ ipcMain.handle('save-text-file', async (_event, defaultName, content, filters) = filters: safeFilters }); if (canceled || !filePath) return { ok: false, canceled: true }; - fs.writeFileSync(filePath, String(content === null || content === undefined ? '' : content), 'utf-8'); + await fs.promises.writeFile(filePath, String(content === null || content === undefined ? '' : content), 'utf-8'); return { ok: true, path: filePath }; }); ipcMain.handle('export-history', async (_event, format) => { const normalizedFormat = String(format || 'csv').toLowerCase() === 'json' ? 'json' : 'csv'; - const history = configStore.loadHistory(); + const history = await configStore.loadHistory(); const rows = flattenHistoryForExport(history); const datePrefix = new Date().toISOString().slice(0, 10); @@ -2235,9 +2235,9 @@ ipcMain.handle('export-history', async (_event, format) => { totalRows: rows.length, history }; - fs.writeFileSync(filePath, JSON.stringify(payload, null, 2), 'utf-8'); + await fs.promises.writeFile(filePath, JSON.stringify(payload, null, 2), 'utf-8'); } else { - fs.writeFileSync(filePath, buildHistoryCsv(rows), 'utf-8'); + await fs.promises.writeFile(filePath, buildHistoryCsv(rows), 'utf-8'); } return { diff --git a/package-lock.json b/package-lock.json index 6f14677..e4ce4c2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "multi-hoster-uploader", - "version": "2.1.69", + "version": "2.1.70", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "multi-hoster-uploader", - "version": "2.1.69", + "version": "2.1.70", "dependencies": { "chokidar": "^4.0.3", "undici": "7.29.1", diff --git a/package.json b/package.json index f0a5fac..36c155e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "multi-hoster-uploader", - "version": "2.1.69", + "version": "2.1.70", "description": "Upload files to doodstream, voe, vidmoly, byse simultaneously", "main": "main.js", "scripts": {