Remove synchronous runtime configuration checks and release 2.1.68
CI / verify (push) Canceled after 0s

This commit is contained in:
Sucukdeluxe committed 2026-10-18 02:11:48 +02:00
1 parent 9b06b4f852
commit 9d96f67261
3 files changed
+88 -74

No files matched your search

+85 -71
View File
@@ -193,6 +193,8 @@ class ConfigStore {
this._historyMigrated = false; this._historyMigrated = false;
this._cache = null; this._cache = null;
this._cacheKey = ''; this._cacheKey = '';
this._lastConfigCheck = 0;
this._refreshTask = null;
this._perfLog = null; this._perfLog = null;
this._wqDepth = 0; this._wqDepth = 0;
@@ -353,40 +355,7 @@ class ConfigStore {
return r; return r;
} }
_loadImpl() { _normalizeConfig(data) {
try {
// In-memory cache keyed on the file's mtime+size. The processed config
// (merged + credential-decrypted) is reparsed/re-decrypted from disk ONLY
// when the file actually changes. Our own writes refresh the cache (see
// _commit), and an external edit changes mtime/size so the cache misses
// and we reread. Without this, every one of the ~38 main.js load() call
// sites (incl. the per-500ms log-flush path) re-read disk + JSON.parse the
// whole growing history + DPAPI-decrypt every credential — the dominant
// long-running main-thread drag. load() always returns a CLONE so callers
// can mutate the result without corrupting the cache.
let stat = null;
try { stat = fs.statSync(this.filePath); } catch {}
const statKey = stat ? `${stat.mtimeMs}:${stat.size}` : '';
if (stat && this._cache && this._cacheKey === statKey) {
return this._clone(this._cache);
}
let data = null;
// Try main config
try { data = this._readAndParse(this.filePath); } catch {}
// Fallback to backup if main is empty/corrupt
if (!data) {
try { data = this._readAndParse(this.filePath + '.bak'); } catch {}
}
if (!data) {
try { data = this._readAndParse(this.filePath + '.pre-history-split.bak'); } catch {}
}
if (!data) {
const fresh = JSON.parse(JSON.stringify(DEFAULTS));
fresh.globalSettings.logMode = normalizeLogMode(fresh.globalSettings);
return fresh;
}
// Migrate old single-object format to array format // Migrate old single-object format to array format
for (const [name, val] of Object.entries(data.hosters || {})) { for (const [name, val] of Object.entries(data.hosters || {})) {
if (val && !Array.isArray(val)) { if (val && !Array.isArray(val)) {
@@ -462,18 +431,61 @@ class ConfigStore {
// Decrypt credentials stored with safeStorage so the rest of the app // Decrypt credentials stored with safeStorage so the rest of the app
// keeps working with plaintext in memory. // keeps working with plaintext in memory.
secretStore.decryptCredentials(result); secretStore.decryptCredentials(result);
if (stat) { return result;
this._cache = result;
this._cacheKey = statKey;
} }
return this._clone(result);
_loadImpl() {
if (this._cache) {
this._scheduleConfigRefresh();
return this._clone(this._cache);
}
let data;
for (const file of [this.filePath, this.filePath + '.bak', this.filePath + '.pre-history-split.bak']) {
try { data = this._readAndParse(file); } catch {}
if (data) break;
}
try { this._cache = this._normalizeConfig(data || {}); }
catch (error) {
if (error instanceof secretStore.SecretStoreError) throw error;
this._cache = this._normalizeConfig({});
}
this._lastConfigCheck = Date.now();
return this._clone(this._cache);
}
_scheduleConfigRefresh() {
if (this._refreshTask || this._writesQuiesced || Date.now() - this._lastConfigCheck < 2000) return;
this._lastConfigCheck = Date.now();
void this.refresh().catch(() => {});
}
refresh() {
if (this._refreshTask) return this._refreshTask;
const operation = this._enqueueWrite(() => this._refreshConfigFromDisk(), { skipRefresh: true });
this._refreshTask = operation.finally(() => { this._refreshTask = null; });
return this._refreshTask;
}
async _refreshConfigFromDisk() {
this._lastConfigCheck = Date.now();
let stat;
try { stat = await fs.promises.stat(this.filePath); } catch {}
const key = stat ? stat.mtimeMs + ':' + stat.size : '';
if (stat && this._cache && this._cacheKey === key) return;
const candidates = this._cache ? [this.filePath] : [this.filePath, this.filePath + '.bak', this.filePath + '.pre-history-split.bak'];
for (const file of candidates) {
try {
const data = JSON.parse(await fs.promises.readFile(file, 'utf8'));
if (!data || typeof data !== 'object' || Array.isArray(data)) continue;
this._cache = this._normalizeConfig(data);
this._cacheKey = file === this.filePath ? key : '';
return;
} catch (error) { } catch (error) {
if (error instanceof secretStore.SecretStoreError) throw error; if (error instanceof secretStore.SecretStoreError) throw error;
const fresh = JSON.parse(JSON.stringify(DEFAULTS));
fresh.globalSettings.logMode = normalizeLogMode(fresh.globalSettings);
return fresh;
} }
} }
if (!this._cache) this._cache = this._normalizeConfig({});
}
// Encrypt credential fields without mutating the caller's plaintext object. // Encrypt credential fields without mutating the caller's plaintext object.
// Only `hosters` carries credentials, so we clone ONLY that subtree — the rest // Only `hosters` carries credentials, so we clone ONLY that subtree — the rest
@@ -506,7 +518,11 @@ class ConfigStore {
_enqueueWrite(fn, options = {}) { _enqueueWrite(fn, options = {}) {
if (this._writesQuiesced && !options.allowDuringQuiesce) return Promise.reject(this._quiescedWriteError()); if (this._writesQuiesced && !options.allowDuringQuiesce) return Promise.reject(this._quiescedWriteError());
this._wqDepth++; this._wqDepth++;
const operation = this._writeQueue.then(fn, fn); const execute = async () => {
if (!options.skipRefresh) await this._refreshConfigFromDisk();
return fn();
};
const operation = this._writeQueue.then(execute, execute);
this._pendingWriteOperations.add(operation); this._pendingWriteOperations.add(operation);
this._writeQueue = operation.then( this._writeQueue = operation.then(
() => { this._wqDepth--; }, () => { this._wqDepth--; },
@@ -535,10 +551,10 @@ class ConfigStore {
return !!h && typeof h === 'object' && Object.values(h).some(a => Array.isArray(a) && a.length > 0); return !!h && typeof h === 'object' && Object.values(h).some(a => Array.isArray(a) && a.length > 0);
} }
_recoverHostersFromDisk() { async _recoverHostersFromDisk() {
for (const p of [this.filePath, this.filePath + '.bak', this.filePath + '.pre-history-split.bak']) { for (const p of [this.filePath, this.filePath + '.bak', this.filePath + '.pre-history-split.bak']) {
try { try {
const raw = fs.readFileSync(p, 'utf-8'); const raw = await fs.promises.readFile(p, 'utf-8');
if (!raw || raw.trim().length < 2) continue; if (!raw || raw.trim().length < 2) continue;
const data = JSON.parse(raw); const data = JSON.parse(raw);
if (this._anyHosters(data)) return data.hosters; if (this._anyHosters(data)) return data.hosters;
@@ -547,9 +563,9 @@ class ConfigStore {
return null; return null;
} }
_guardHosters(current, hostersIntentional) { async _guardHosters(current, hostersIntentional) {
if (!hostersIntentional && !this._anyHosters(current)) { if (!hostersIntentional && !this._anyHosters(current)) {
const recovered = this._recoverHostersFromDisk(); const recovered = await this._recoverHostersFromDisk();
if (recovered) { if (recovered) {
current.hosters = recovered; current.hosters = recovered;
if (this._perfLog) this._perfLog('config-guard: prevented account wipe — restored hosters from on-disk backup after a corrupt/empty read'); if (this._perfLog) this._perfLog('config-guard: prevented account wipe — restored hosters from on-disk backup after a corrupt/empty read');
@@ -559,25 +575,25 @@ class ConfigStore {
} }
save(config) { save(config) {
return this._enqueueWrite(() => { return this._enqueueWrite(async () => {
const current = this.load(); const current = this.load();
if (config.hosters) current.hosters = config.hosters; if (config.hosters) current.hosters = config.hosters;
if (config.hosterSettings) current.hosterSettings = config.hosterSettings; if (config.hosterSettings) current.hosterSettings = config.hosterSettings;
if (config.globalSettings) current.globalSettings = config.globalSettings; if (config.globalSettings) current.globalSettings = config.globalSettings;
this._guardHosters(current, !!config.hosters); await this._guardHosters(current, !!config.hosters);
return this._commit(current); return this._commit(current);
}); });
} }
savePendingQueue(pendingQueue, options = {}) { savePendingQueue(pendingQueue, options = {}) {
const snapshot = pendingQueue === null || pendingQueue === undefined ? null : this._clone(pendingQueue); const snapshot = pendingQueue === null || pendingQueue === undefined ? null : this._clone(pendingQueue);
return this._enqueueWrite(() => { return this._enqueueWrite(async () => {
const current = this.load(); const current = this.load();
current.globalSettings = { current.globalSettings = {
...(current.globalSettings || {}), ...(current.globalSettings || {}),
pendingQueue: snapshot pendingQueue: snapshot
}; };
this._guardHosters(current, false); await this._guardHosters(current, false);
return this._commit(current); return this._commit(current);
}, options); }, options);
} }
@@ -587,7 +603,7 @@ class ConfigStore {
paused: folderMonitor?.paused === true, paused: folderMonitor?.paused === true,
pausedAt: folderMonitor?.paused === true ? (folderMonitor?.pausedAt ?? null) : null pausedAt: folderMonitor?.paused === true ? (folderMonitor?.pausedAt ?? null) : null
}; };
return this._enqueueWrite(() => { return this._enqueueWrite(async () => {
const current = this.load(); const current = this.load();
const currentGlobalSettings = current.globalSettings || {}; const currentGlobalSettings = current.globalSettings || {};
current.globalSettings = { current.globalSettings = {
@@ -597,20 +613,20 @@ class ConfigStore {
...snapshot ...snapshot
} }
}; };
this._guardHosters(current, false); await this._guardHosters(current, false);
return this._commit(current); return this._commit(current);
}, options); }, options);
} }
saveUploadRecovery(uploadRecovery, options = {}) { saveUploadRecovery(uploadRecovery, options = {}) {
const snapshot = uploadRecovery === null || uploadRecovery === undefined ? null : this._clone(uploadRecovery); const snapshot = uploadRecovery === null || uploadRecovery === undefined ? null : this._clone(uploadRecovery);
return this._enqueueWrite(() => { return this._enqueueWrite(async () => {
const current = this.load(); const current = this.load();
current.globalSettings = { current.globalSettings = {
...(current.globalSettings || {}), ...(current.globalSettings || {}),
uploadRecovery: snapshot uploadRecovery: snapshot
}; };
this._guardHosters(current, false); await this._guardHosters(current, false);
return this._commit(current); return this._commit(current);
}, options); }, options);
} }
@@ -682,20 +698,20 @@ class ConfigStore {
saveLastBrowseDirectory(directory) { saveLastBrowseDirectory(directory) {
const snapshot = String(directory || '').trim(); const snapshot = String(directory || '').trim();
return this._enqueueWrite(() => { return this._enqueueWrite(async () => {
const current = this.load(); const current = this.load();
current.globalSettings = { current.globalSettings = {
...(current.globalSettings || {}), ...(current.globalSettings || {}),
lastBrowseDirectory: snapshot lastBrowseDirectory: snapshot
}; };
this._guardHosters(current, false); await this._guardHosters(current, false);
return this._commit(current); return this._commit(current);
}); });
} }
saveRendererGlobalSettings(globalSettings) { saveRendererGlobalSettings(globalSettings) {
const snapshot = this._clone(globalSettings || {}); const snapshot = this._clone(globalSettings || {});
return this._enqueueWrite(() => { return this._enqueueWrite(async () => {
const current = this.load(); const current = this.load();
const currentGlobalSettings = current.globalSettings || {}; const currentGlobalSettings = current.globalSettings || {};
const currentFolderMonitor = currentGlobalSettings.folderMonitor || {}; const currentFolderMonitor = currentGlobalSettings.folderMonitor || {};
@@ -714,14 +730,14 @@ class ConfigStore {
pausedAt: currentFolderMonitor.pausedAt ?? null pausedAt: currentFolderMonitor.pausedAt ?? null
} }
}; };
this._guardHosters(current, false); await this._guardHosters(current, false);
return this._commit(current); return this._commit(current);
}); });
} }
saveCloudLogSettings(settings) { saveCloudLogSettings(settings) {
const snapshot = this._clone(settings); const snapshot = this._clone(settings);
return this._enqueueWrite(() => { return this._enqueueWrite(async () => {
const current = this.load(); const current = this.load();
current.globalSettings.cloudLogSync = snapshot; current.globalSettings.cloudLogSync = snapshot;
return this._commit(current); return this._commit(current);
@@ -731,14 +747,14 @@ class ConfigStore {
saveRemoteSettings() { saveRemoteSettings() {
return this._enqueueWrite(async () => { return this._enqueueWrite(async () => {
const current = this.load(); const current = this.load();
this._guardHosters(current, false); await this._guardHosters(current, false);
await this._commit(current); await this._commit(current);
return { enabled: false, retired: true, token: '' }; return { enabled: false, retired: true, token: '' };
}); });
} }
replaceSettings(config) { replaceSettings(config) {
return this._enqueueWrite(() => { return this._enqueueWrite(async () => {
const current = this.load(); const current = this.load();
const globalSettings = this._clone(config.globalSettings); const globalSettings = this._clone(config.globalSettings);
globalSettings.pendingQueue = current.globalSettings.pendingQueue ?? null; globalSettings.pendingQueue = current.globalSettings.pendingQueue ?? null;
@@ -779,11 +795,9 @@ class ConfigStore {
if (current && current.trim().length > 2) await fs.promises.writeFile(backupPath, current, 'utf-8'); if (current && current.trim().length > 2) await fs.promises.writeFile(backupPath, current, 'utf-8');
} catch {} } catch {}
await fs.promises.rename(tmpPath, this.filePath); await fs.promises.rename(tmpPath, this.filePath);
// Invalidate the read cache: the next load() re-reads + re-merges the this._cache = this._normalizeConfig(JSON.parse(data));
// freshly-written file (the on-disk format is sparse — load() fills
// defaults — so we must NOT serve a pre-merge in-memory object).
this._cache = null;
this._cacheKey = ''; this._cacheKey = '';
this._lastConfigCheck = Date.now();
} }
appendHistory(entry) { appendHistory(entry) {
@@ -801,7 +815,7 @@ class ConfigStore {
return this._writeHistoryFileAtomic(pruned); return this._writeHistoryFileAtomic(pruned);
}); });
} }
return this._enqueueWrite(() => { return this._enqueueWrite(async () => {
const config = this.load(); const config = this.load();
config.history.push(entry); config.history.push(entry);
const retention = (config.globalSettings && config.globalSettings.historyRetention) || 'all'; const retention = (config.globalSettings && config.globalSettings.historyRetention) || 'all';
@@ -833,7 +847,7 @@ class ConfigStore {
const config = this.load(); const config = this.load();
const previousGlobalSettings = this._clone(config.globalSettings || {}); const previousGlobalSettings = this._clone(config.globalSettings || {});
config.globalSettings = { ...previousGlobalSettings, historyRetention: String(retention || 'all') }; config.globalSettings = { ...previousGlobalSettings, historyRetention: String(retention || 'all') };
this._guardHosters(config, false); await this._guardHosters(config, false);
await this._commit(config); await this._commit(config);
try { try {
await this._writeHistoryFileAtomic(pruned); await this._writeHistoryFileAtomic(pruned);
@@ -850,7 +864,7 @@ class ConfigStore {
}); });
}); });
} }
return this._enqueueWrite(() => { return this._enqueueWrite(async () => {
const config = this.load(); const config = this.load();
const beforeBatches = config.history.length; const beforeBatches = config.history.length;
const beforeRows = countHistoryRows(config.history); const beforeRows = countHistoryRows(config.history);
@@ -872,7 +886,7 @@ class ConfigStore {
if (this._historyMigrated) { if (this._historyMigrated) {
return this._enqueueHistoryWrite(() => this._writeHistoryFileAtomic([])); return this._enqueueHistoryWrite(() => this._writeHistoryFileAtomic([]));
} }
return this._enqueueWrite(() => { return this._enqueueWrite(async () => {
const config = this.load(); const config = this.load();
config.history = []; config.history = [];
return this._commit(config); return this._commit(config);
@@ -880,10 +894,10 @@ class ConfigStore {
} }
saveRotationCursors(cursors) { saveRotationCursors(cursors) {
return this._enqueueWrite(() => { return this._enqueueWrite(async () => {
const config = this.load(); const config = this.load();
config.rotationCursors = (cursors && typeof cursors === 'object' && !Array.isArray(cursors)) ? cursors : {}; config.rotationCursors = (cursors && typeof cursors === 'object' && !Array.isArray(cursors)) ? cursors : {};
this._guardHosters(config, false); await this._guardHosters(config, false);
return this._commit(config); return this._commit(config);
}); });
} }
+2 -2
View File
@@ -1,12 +1,12 @@
{ {
"name": "multi-hoster-uploader", "name": "multi-hoster-uploader",
"version": "2.1.67", "version": "2.1.68",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "multi-hoster-uploader", "name": "multi-hoster-uploader",
"version": "2.1.67", "version": "2.1.68",
"dependencies": { "dependencies": {
"chokidar": "^4.0.3", "chokidar": "^4.0.3",
"undici": "7.29.1", "undici": "7.29.1",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "multi-hoster-uploader", "name": "multi-hoster-uploader",
"version": "2.1.67", "version": "2.1.68",
"description": "Upload files to doodstream, voe, vidmoly, byse simultaneously", "description": "Upload files to doodstream, voe, vidmoly, byse simultaneously",
"main": "main.js", "main": "main.js",
"scripts": { "scripts": {