Files
Multi-Hoster-Upload/lib/config-store.js
T
SucukdeluxeandClaude Opus 5.5 0b30a9180a
CI / verify (push) Canceled after 0s
Migrate credential encryption to the async safeStorage API
Electron 45 deprecates the synchronous safeStorage methods and Electron 46
removes them. Credential encryption now uses encryptStringAsync,
decryptStringAsync and isAsyncEncryptionAvailable.

- secret-store: async encryptField/decryptField/encryptCredentials/
  decryptCredentials, decryptFieldWithStatus exposes shouldReEncrypt.
  Unavailable or failing encryption still rejects with the existing
  SECRET_STORE_* codes; legacy plaintext stays readable.
- config-store: the first load runs asynchronously through init();
  writes encrypt before the atomic write. A synchronous load() of an
  encrypted config before init() fails with SECRET_STORE_NOT_READY and
  schedules a refresh. Values flagged with shouldReEncrypt are
  re-encrypted once after loading.
- main: await configStore.init() after app ready, before the first load.
- MCP access store and online backup keyring await the async helpers;
  MCP saves keep their call order.

Existing enc:v1 values written with encryptString stay readable: on
Windows both APIs use the same v10 format and Local State key, verified
in both directions with real Electron 41 and 45.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 07:57:54 +02:00

966 lines
36 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const secretStore = require('./secret-store');
const { normalizeLogMode } = require('./log-mode');
const { mergeAutomationCompletions, normalizeAutomationCompletion, removeAutomationCompletions } = require('./automation-control');
const HOSTER_SETTINGS_DEFAULTS = {
retries: 3,
maxSpeedKbs: 0, // 0 = unlimited
parallelCount: 2, // 1-100
restartBelowKbs: 0, // 0 = off
timeIntervalSec: 0, // delay between jobs
maxSizeMb: 0, // 0 = unlimited
logToFile: true, // write this hoster's successful links to fileuploader.log
rotateAccounts: false,
sizeMemoEnabled: true
};
// Template for each hoster type (used as defaults for new accounts)
const HOSTER_ACCOUNT_TEMPLATES = {
'doodstream.com': { enabled: true, authType: 'login', username: '', password: '' },
'doodstream.com:api': { enabled: true, authType: 'api', apiKey: '' },
'voe.sx': { enabled: true, authType: 'login', username: '', password: '' },
'voe.sx:api': { enabled: true, authType: 'api', apiKey: '' },
'vidmoly.me': { enabled: true, authType: 'login', username: '', password: '' },
'byse.sx': { enabled: true, authType: 'api', apiKey: '' },
'clouddrop.cc': { enabled: true, authType: 'api', apiKey: '' }
};
// All known hoster names (used for iteration)
const HOSTER_NAMES = ['doodstream.com', 'voe.sx', 'vidmoly.me', 'byse.sx', 'clouddrop.cc'];
// Dropdown options for "Add Account" modal: value -> label
const HOSTER_ADD_OPTIONS = [
{ value: 'doodstream.com', label: 'Doodstream (Web Login)', hoster: 'doodstream.com', authType: 'login' },
{ value: 'doodstream.com:api', label: 'Doodstream (API)', hoster: 'doodstream.com', authType: 'api' },
{ value: 'voe.sx', label: 'Voe (Web Login)', hoster: 'voe.sx', authType: 'login' },
{ value: 'voe.sx:api', label: 'Voe (API)', hoster: 'voe.sx', authType: 'api' },
{ value: 'vidmoly.me', label: 'Vidmoly (Web Login)', hoster: 'vidmoly.me', authType: 'login' },
{ value: 'byse.sx', label: 'Byse (API)', hoster: 'byse.sx', authType: 'api' },
{ value: 'clouddrop.cc', label: 'Clouddrop (API)', hoster: 'clouddrop.cc', authType: 'api' }
];
const DEFAULTS = {
hosters: {
'doodstream.com': [],
'voe.sx': [],
'vidmoly.me': [],
'byse.sx': [],
'clouddrop.cc': []
},
hosterSettings: {
'doodstream.com': { ...HOSTER_SETTINGS_DEFAULTS },
'voe.sx': { ...HOSTER_SETTINGS_DEFAULTS },
'vidmoly.me': { ...HOSTER_SETTINGS_DEFAULTS },
'byse.sx': { ...HOSTER_SETTINGS_DEFAULTS },
'clouddrop.cc': { ...HOSTER_SETTINGS_DEFAULTS }
},
globalSettings: {
language: 'en',
autoHealthCheckEnabled: true,
alwaysOnTop: false,
shutdownAfterFinish: 'nothing', // nothing | sleep | shutdown | restart
logFilePath: '',
cloudLogSync: { enabled: false, apiKey: '', accountId: '', ip: '', folderId: '' },
sessionLog: false, // legacy boolean (kept for back-compat reads); normalized into logMode on load
logVerbose: false, // when true, [DEBUG] level entries are written to debug.log
webhookUrl: '', // POST target on batch-done (Discord or generic JSON)
webhookMention: '', // optional Discord ping target: user-id, role:id, @here, @everyone
autoRetryRounds: 0, // 0 = off; 1-5 automatic retry rounds for transient failures after batch end
autoRetryDelayMin: 5, // base delay in minutes between auto-retry rounds (linear backoff: round N waits N*delay)
historyRetention: 'all', // 'all' | '7d' | '14d' | '30d' | '90d' | '1000' | '100' — storage cap for upload history
// NOTE: logMode is intentionally NOT in DEFAULTS. If it were, the deep-merge
// would seed logMode='single' for every load, which would beat (and silently
// erase) the legacy sessionLog:true → "daily" migration. normalizeLogMode in
// load() sets logMode after the merge, looking at the saved-only data.
resumeQueueOnLaunch: true,
autoStartRestoredQueue: false,
parallelUploadCount: 0, // 0 = use per-hoster limits only
scaleParallelUploads: false,
lastBrowseDirectory: '',
removeFromQueueOnDone: false,
deleteSourceAfterSuccessfulUpload: false,
showDropTarget: false,
globalMaxSpeedKbs: 0, // 0 = unlimited global speed
speedUnits: 'decimal',
pendingQueue: null,
uploadRecovery: null,
scramble: {
active: false,
prefix: '',
suffix: '',
chars: 'both', // 'letters' | 'numbers' | 'both'
length: 0 // 0 = same as original basename length
},
folderMonitor: {
enabled: false,
folderPath: '',
recursive: false,
includeExisting: false,
filterMode: 'include', // 'include' | 'exclude'
extensions: '', // comma-separated: 'mp4,mkv,avi'
skipDuplicates: true,
delaySec: 3,
autoStart: true,
hosters: [], // pre-selected hosters, empty = ask via modal
queueLimitJobs: 15000,
reconcileIntervalMinutes: 5,
paused: false,
pausedAt: null
},
},
history: [],
rotationCursors: {}
};
const HISTORY_RETENTION_OPTIONS = [
{ value: 'all', label: 'Alles behalten' },
{ value: '7d', label: 'Letzte 7 Tage' },
{ value: '14d', label: 'Letzte 14 Tage' },
{ value: '30d', label: 'Letzte 30 Tage' },
{ value: '90d', label: 'Letzte 90 Tage' },
{ value: '1000', label: 'Letzte 1000 Uploads' },
{ value: '100', label: 'Letzte 100 Uploads' }
];
function batchTimestampMs(batch) {
const raw = batch && batch.timestamp;
if (raw === null || raw === undefined || raw === '') return null;
const ms = typeof raw === 'number' ? raw : Date.parse(raw);
return Number.isFinite(ms) ? ms : null;
}
function batchRowCount(batch) {
let n = 0;
const files = (batch && batch.files) || [];
for (const file of files) {
n += (file.results || []).length;
}
return n;
}
function countHistoryRows(history) {
let n = 0;
for (const batch of (history || [])) n += batchRowCount(batch);
return n;
}
function applyHistoryRetention(history, retention, nowMs) {
if (!Array.isArray(history) || history.length === 0) return history;
const policy = String(retention || 'all');
if (policy === 'all') return history;
if (/^\d+d$/.test(policy)) {
const days = parseInt(policy, 10);
if (!Number.isFinite(days) || days <= 0) return history;
const cutoff = nowMs - days * 86400000;
return history.filter(b => {
const ts = batchTimestampMs(b);
return ts === null || ts >= cutoff;
});
}
const maxRows = parseInt(policy, 10);
if (!Number.isFinite(maxRows) || maxRows <= 0) return history;
const keptReversed = [];
let acc = 0;
for (let i = history.length - 1; i >= 0; i--) {
keptReversed.push(history[i]);
acc += batchRowCount(history[i]);
if (acc >= maxRows) break;
}
return keptReversed.reverse();
}
class ConfigStore {
constructor(app) {
const useUserDataDir = app && (
app.isPackaged ||
(app.commandLine && typeof app.commandLine.hasSwitch === 'function' && app.commandLine.hasSwitch('user-data-dir'))
);
const dir = useUserDataDir
? app.getPath('userData')
: path.join(__dirname, '..');
this.filePath = path.join(dir, 'electron-config.json');
this.historyPath = path.join(dir, 'electron-history.json');
this.automationCompletionPath = path.join(dir, 'automation-completions.json');
this._writeQueue = Promise.resolve(); // Serializes all writes to prevent race conditions
this._historyWriteQueue = Promise.resolve();
this._automationCompletionWriteQueue = Promise.resolve();
this._automationCompletionCache = null;
this._pendingWriteOperations = new Set();
this._writesQuiesced = false;
this._historyMigrated = false;
this._cache = null;
this._cacheKey = '';
this._lastConfigCheck = 0;
this._refreshTask = null;
this._reEncryptScheduled = false;
this._perfLog = null;
this._wqDepth = 0;
// Migrate config from old location if current doesn't exist
if (!fs.existsSync(this.filePath) && app && app.isPackaged) {
this._migrateFromOldPath(app);
}
if (app && app.isPackaged) {
this._migrateHistory();
}
}
_readHistoryFileSync() {
try {
const raw = fs.readFileSync(this.historyPath, 'utf-8');
if (!raw || raw.trim().length < 2) return [];
const parsed = JSON.parse(raw);
if (Array.isArray(parsed)) return parsed;
if (parsed && Array.isArray(parsed.history)) return parsed.history;
return [];
} catch {
return null;
}
}
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');
try {
fs.writeSync(fd, JSON.stringify(arr));
fs.fsyncSync(fd);
} finally {
fs.closeSync(fd);
}
fs.renameSync(tmp, this.historyPath);
}
_writeHistoryFileAtomic(arr) {
return new Promise((resolve, reject) => {
const tmp = this.historyPath + '.tmp';
fs.writeFile(tmp, JSON.stringify(arr), 'utf-8', (err) => {
if (err) return reject(err);
fs.rename(tmp, this.historyPath, error => error ? reject(error) : resolve());
});
});
}
_quiescedWriteError() {
const error = new Error('Die Anwendung wird gerade beendet');
error.code = 'CONFIG_WRITES_QUIESCED';
return error;
}
setWritesQuiesced(quiesced) {
this._writesQuiesced = !!quiesced;
}
_enqueueHistoryWrite(fn, options = {}) {
if (this._writesQuiesced && !options.allowDuringQuiesce) return Promise.reject(this._quiescedWriteError());
const operation = this._historyWriteQueue.then(fn, fn);
this._pendingWriteOperations.add(operation);
this._historyWriteQueue = operation.then(() => undefined, () => undefined);
operation.then(
() => this._pendingWriteOperations.delete(operation),
() => this._pendingWriteOperations.delete(operation)
);
return operation;
}
_migrateHistory() {
try {
if (fs.existsSync(this.historyPath)) {
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._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 {}
}
this._historyMigrated = true;
} else {
this._historyMigrated = false;
}
} catch {
this._historyMigrated = false;
}
}
_migrateFromOldPath(app) {
try {
const appDataDir = path.dirname(app.getPath('userData'));
// Check alternate folder names that may have been used
const candidates = ['multi-hoster-uploader', 'Multi-Hoster-Upload'];
for (const name of candidates) {
const oldPath = path.join(appDataDir, name, 'electron-config.json');
if (oldPath !== this.filePath && fs.existsSync(oldPath)) {
fs.mkdirSync(path.dirname(this.filePath), { recursive: true });
fs.copyFileSync(oldPath, this.filePath);
return;
}
}
// Also check next to the executable (portable mode previous location)
const exeDir = path.dirname(app.getPath('exe'));
const portablePath = path.join(exeDir, 'electron-config.json');
if (portablePath !== this.filePath && fs.existsSync(portablePath)) {
fs.mkdirSync(path.dirname(this.filePath), { recursive: true });
fs.copyFileSync(portablePath, this.filePath);
}
} catch {}
}
_readAndParse(filePath) {
const raw = fs.readFileSync(filePath, 'utf-8');
if (!raw || raw.trim().length < 2) return null;
return JSON.parse(raw);
}
_clone(obj) {
try { return structuredClone(obj); }
catch { return JSON.parse(JSON.stringify(obj)); }
}
setPerfLog(fn) { this._perfLog = typeof fn === 'function' ? fn : null; }
_pqLen(globalSettings) {
const pq = globalSettings && globalSettings.pendingQueue;
return pq && Array.isArray(pq.queueJobs) ? pq.queueJobs.length : 0;
}
_callerTag() {
const lines = (new Error().stack || '').split('\n');
const out = [];
for (let i = 2; i < lines.length && out.length < 3; i++) {
const line = lines[i].trim();
if (/config-store\.js/.test(line)) continue;
const m = line.match(/at (?:async )?([^ (]+)/);
if (m) out.push(m[1].split('.').pop());
}
return out.join('<') || '?';
}
getGlobalSetting(key) {
if (!this._cache) this.load();
this._scheduleConfigRefresh();
return this._clone(this._cache.globalSettings?.[key]);
}
load() {
if (!this._perfLog) return this._loadImpl();
const hadCache = !!this._cache;
const t0 = performance.now();
const r = this._loadImpl();
const dt = performance.now() - t0;
if (dt >= 20) {
const q = this._pqLen(r && r.globalSettings);
const h = (r && r.history || []).length;
this._perfLog(`config-load wall=${dt.toFixed(0)}ms cache=${hadCache ? 'hit' : 'miss'} hist=${h} queue=${q} via=${this._callerTag()}`);
}
return r;
}
_normalizeConfig(data) {
// Migrate old single-object format to array format
for (const [name, val] of Object.entries(data.hosters || {})) {
if (val && !Array.isArray(val)) {
if (!val.id) val.id = `${name}-migrated-${Date.now()}`;
// Infer authType for old format accounts
if (!val.authType) {
if (name === 'byse.sx') val.authType = 'api';
else if (name === 'vidmoly.me') val.authType = 'login';
else if (val.username && val.password) val.authType = 'login';
else if (val.apiKey) val.authType = 'api';
else val.authType = 'login';
}
data.hosters[name] = [val];
}
}
// Merge hosters: ensure all known hosters exist as arrays
const hosters = {};
for (const name of HOSTER_NAMES) {
const saved = data.hosters && data.hosters[name];
if (Array.isArray(saved) && saved.length > 0) {
hosters[name] = saved.map((acc, i) => {
// Ensure authType is set on every account
if (!acc.authType) {
if (name === 'byse.sx') acc.authType = 'api';
else if (name === 'vidmoly.me') acc.authType = 'login';
else if (acc.username && acc.password) acc.authType = 'login';
else if (acc.apiKey) acc.authType = 'api';
else acc.authType = 'login';
}
return {
...acc,
id: acc.id || `${name}-${Date.now()}-${i}`
};
});
} else {
hosters[name] = [];
}
}
const savedGlobal = data.globalSettings || {};
const binarySpeeds = savedGlobal.speedUnits !== 'decimal';
const toDecimalKbs = (value) => binarySpeeds && Number(value) > 0 ? Math.round(Number(value) * 1000 / 1024) : value;
// Merge hoster settings with defaults
const hosterSettings = {};
for (const name of Object.keys(DEFAULTS.hosterSettings)) {
hosterSettings[name] = {
...HOSTER_SETTINGS_DEFAULTS,
...(data.hosterSettings && data.hosterSettings[name] || {})
};
hosterSettings[name].maxSpeedKbs = toDecimalKbs(hosterSettings[name].maxSpeedKbs);
hosterSettings[name].restartBelowKbs = toDecimalKbs(hosterSettings[name].restartBelowKbs);
}
const globalSettings = {
...DEFAULTS.globalSettings,
...savedGlobal
};
globalSettings.globalMaxSpeedKbs = toDecimalKbs(globalSettings.globalMaxSpeedKbs);
globalSettings.speedUnits = 'decimal';
delete globalSettings.allowPlaintextCredentialStorage;
// Deep-merge nested objects so new keys are always present
for (const key of Object.keys(DEFAULTS.globalSettings)) {
const def = DEFAULTS.globalSettings[key];
if (def && typeof def === 'object' && !Array.isArray(def)) {
globalSettings[key] = { ...def, ...(savedGlobal[key] || {}) };
}
}
delete globalSettings.remote;
delete globalSettings.diagnostics;
// Normalize logMode at this single boundary. Legacy sessionLog: true
// means *daily* (the old field was named after a misnomer); see log-mode.js.
// Downstream readers consume logMode only and must NOT derive from
// sessionLog at call sites.
globalSettings.logMode = normalizeLogMode(globalSettings);
const rotationCursors = (data.rotationCursors && typeof data.rotationCursors === 'object' && !Array.isArray(data.rotationCursors))
? data.rotationCursors
: {};
return { hosters, hosterSettings, globalSettings, history: this._historyMigrated ? [] : (data.history || []), rotationCursors };
}
async _normalizeAndDecrypt(data) {
const config = this._normalizeConfig(data);
const { shouldReEncrypt } = await secretStore.decryptCredentials(config);
return { config, shouldReEncrypt };
}
init() {
if (this._cache) return Promise.resolve();
return this.refresh();
}
_scheduleReEncrypt() {
if (this._reEncryptScheduled || this._writesQuiesced) return;
this._reEncryptScheduled = true;
void this._enqueueWrite(async () => this._commit(this.load())).catch(() => {});
}
_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;
}
let normalized;
try { normalized = this._normalizeConfig(data || {}); }
catch { normalized = this._normalizeConfig({}); }
if (secretStore.hasEncryptedCredentials(normalized)) {
this._scheduleConfigRefresh();
throw new secretStore.SecretStoreError('SECRET_STORE_NOT_READY', 'Gespeicherte Zugangsdaten wurden noch nicht geladen');
}
this._cache = normalized;
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;
const { config, shouldReEncrypt } = await this._normalizeAndDecrypt(data);
this._cache = config;
this._cacheKey = file === this.filePath ? key : '';
if (shouldReEncrypt) this._scheduleReEncrypt();
return;
} catch (error) {
if (error instanceof secretStore.SecretStoreError) throw error;
}
}
if (!this._cache) this._cache = this._normalizeConfig({});
}
// Encrypt credential fields without mutating the caller's plaintext object.
// Only `hosters` carries credentials, so we clone ONLY that subtree — the rest
// (history, globalSettings, …) is referenced read-only into the stringified
// object. Deep-cloning the whole config here (incl. an ever-growing history)
// on every write was a primary long-running main-thread stall.
async _serializeForDisk(config) {
const hosters = this._clone(config.hosters || {});
const globalSettings = this._clone(config.globalSettings || {});
delete globalSettings.remote;
delete globalSettings.diagnostics;
delete globalSettings.allowPlaintextCredentialStorage;
await secretStore.encryptCredentials({ hosters, globalSettings });
return JSON.stringify({ ...config, globalSettings, hosters }, null, 2);
}
async _commit(config) {
if (!this._perfLog) return this._atomicWrite(await this._serializeForDisk(config));
const t0 = performance.now();
const data = await this._serializeForDisk(config);
const dt = performance.now() - t0;
if (dt >= 20) {
const q = this._pqLen(config.globalSettings);
const h = (config.history || []).length;
this._perfLog(`config-serialize wall=${dt.toFixed(0)}ms bytes=${data.length} hist=${h} queue=${q} wqDepth=${this._wqDepth} via=${this._callerTag()}`);
}
return this._atomicWrite(data);
}
_enqueueWrite(fn, options = {}) {
if (this._writesQuiesced && !options.allowDuringQuiesce) return Promise.reject(this._quiescedWriteError());
const previous = this._lastQueuedWrite;
if (options.coalesceKey && previous?.key === options.coalesceKey && !previous.started) {
previous.fn = fn;
return previous.operation;
}
const queued = { key: options.coalesceKey, fn, started: false, operation: null };
this._lastQueuedWrite = queued;
this._wqDepth++;
const execute = async () => {
queued.started = true;
if (this._lastQueuedWrite === queued) this._lastQueuedWrite = null;
if (!options.skipRefresh) await this._refreshConfigFromDisk();
return queued.fn();
};
const operation = this._writeQueue.then(execute, execute);
queued.operation = operation;
this._pendingWriteOperations.add(operation);
this._writeQueue = operation.then(
() => { this._wqDepth--; },
() => {
this._wqDepth--;
}
);
operation.then(
() => this._pendingWriteOperations.delete(operation),
() => this._pendingWriteOperations.delete(operation)
);
return operation;
}
async drainWrites() {
while (this._pendingWriteOperations.size > 0) {
const pending = Array.from(this._pendingWriteOperations);
const results = await Promise.allSettled(pending);
const failed = results.find(result => result.status === 'rejected');
if (failed) throw failed.reason;
}
}
_anyHosters(cfg) {
const h = cfg && cfg.hosters;
return !!h && typeof h === 'object' && Object.values(h).some(a => Array.isArray(a) && a.length > 0);
}
async _recoverHostersFromDisk() {
for (const p of [this.filePath, this.filePath + '.bak', this.filePath + '.pre-history-split.bak']) {
try {
const raw = await fs.promises.readFile(p, 'utf-8');
if (!raw || raw.trim().length < 2) continue;
const data = JSON.parse(raw);
if (this._anyHosters(data)) return data.hosters;
} catch {}
}
return null;
}
async _guardHosters(current, hostersIntentional) {
if (!hostersIntentional && !this._anyHosters(current)) {
const recovered = await this._recoverHostersFromDisk();
if (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');
}
}
return current;
}
save(config) {
return this._enqueueWrite(async () => {
const current = this.load();
if (config.hosters) current.hosters = config.hosters;
if (config.hosterSettings) current.hosterSettings = config.hosterSettings;
if (config.globalSettings) current.globalSettings = config.globalSettings;
await this._guardHosters(current, !!config.hosters);
return this._commit(current);
});
}
savePendingQueue(pendingQueue, options = {}) {
const snapshot = pendingQueue === null || pendingQueue === undefined ? null : this._clone(pendingQueue);
return this._enqueueWrite(async () => {
const current = this.load();
current.globalSettings = {
...(current.globalSettings || {}),
pendingQueue: snapshot
};
await this._guardHosters(current, false);
return this._commit(current);
}, { ...options, coalesceKey: 'pending-queue' });
}
saveFolderMonitorRuntimeState(folderMonitor, options = {}) {
const snapshot = {
paused: folderMonitor?.paused === true,
pausedAt: folderMonitor?.paused === true ? (folderMonitor?.pausedAt ?? null) : null
};
return this._enqueueWrite(async () => {
const current = this.load();
const currentGlobalSettings = current.globalSettings || {};
current.globalSettings = {
...currentGlobalSettings,
folderMonitor: {
...(currentGlobalSettings.folderMonitor || {}),
...snapshot
}
};
await this._guardHosters(current, false);
return this._commit(current);
}, options);
}
saveUploadRecovery(uploadRecovery, options = {}) {
const snapshot = uploadRecovery === null || uploadRecovery === undefined ? null : this._clone(uploadRecovery);
return this._enqueueWrite(async () => {
const current = this.load();
current.globalSettings = {
...(current.globalSettings || {}),
uploadRecovery: snapshot
};
await this._guardHosters(current, false);
return this._commit(current);
}, options);
}
async _readAutomationCompletionFile() {
try {
const raw = await fs.promises.readFile(this.automationCompletionPath, 'utf8');
const parsed = JSON.parse(raw);
if (!parsed || parsed.version !== 1 || !Array.isArray(parsed.entries)) throw new Error('Automatik-Abschlussdatei ist ungültig');
if (parsed.entries.some(entry => !normalizeAutomationCompletion(entry))) throw new Error('Automatik-Abschlussdatei ist ungültig');
return mergeAutomationCompletions([], parsed.entries);
} catch (error) {
if (error?.code === 'ENOENT') return [];
throw error;
}
}
async _writeAutomationCompletionFile(entries) {
const target = this.automationCompletionPath;
const temporary = `${target}.tmp`;
await fs.promises.mkdir(path.dirname(target), { recursive: true });
const handle = await fs.promises.open(temporary, 'w');
try {
await handle.writeFile(JSON.stringify({ version: 1, entries }), 'utf8');
await handle.sync();
} finally {
await handle.close();
}
await fs.promises.rename(temporary, target);
}
_enqueueAutomationCompletionWrite(operation) {
const result = this._automationCompletionWriteQueue.then(operation);
this._automationCompletionWriteQueue = result.catch(() => {});
return result;
}
async loadAutomationCompletions() {
await this._automationCompletionWriteQueue;
if (!this._automationCompletionCache) this._automationCompletionCache = await this._readAutomationCompletionFile();
return this._clone(this._automationCompletionCache);
}
saveAutomationCompletions(entries) {
const incoming = this._clone(Array.isArray(entries) ? entries : []);
return this._enqueueAutomationCompletionWrite(async () => {
const current = this._automationCompletionCache || await this._readAutomationCompletionFile();
const next = mergeAutomationCompletions(current, incoming);
await this._writeAutomationCompletionFile(next);
this._automationCompletionCache = next;
return this._clone(next);
});
}
clearAutomationCompletions(removals) {
const requested = this._clone(Array.isArray(removals) ? removals : []);
return this._enqueueAutomationCompletionWrite(async () => {
const current = this._automationCompletionCache || await this._readAutomationCompletionFile();
const next = removeAutomationCompletions(current, requested);
await this._writeAutomationCompletionFile(next);
this._automationCompletionCache = next;
return this._clone(next);
});
}
async drainAutomationCompletionWrites() {
await this._automationCompletionWriteQueue;
}
saveLastBrowseDirectory(directory) {
const snapshot = String(directory || '').trim();
return this._enqueueWrite(async () => {
const current = this.load();
current.globalSettings = {
...(current.globalSettings || {}),
lastBrowseDirectory: snapshot
};
await this._guardHosters(current, false);
return this._commit(current);
});
}
saveRendererGlobalSettings(globalSettings) {
const snapshot = this._clone(globalSettings || {});
return this._enqueueWrite(async () => {
const current = this.load();
const currentGlobalSettings = current.globalSettings || {};
const currentFolderMonitor = currentGlobalSettings.folderMonitor || {};
const incomingFolderMonitor = snapshot.folderMonitor || {};
current.globalSettings = {
...snapshot,
pendingQueue: currentGlobalSettings.pendingQueue ?? null,
uploadRecovery: currentGlobalSettings.uploadRecovery ?? null,
lastBrowseDirectory: currentGlobalSettings.lastBrowseDirectory || '',
cloudLogSync: this._clone(currentGlobalSettings.cloudLogSync || {}),
historyRetention: currentGlobalSettings.historyRetention || 'all',
folderMonitor: {
...currentFolderMonitor,
...incomingFolderMonitor,
paused: currentFolderMonitor.paused === true,
pausedAt: currentFolderMonitor.pausedAt ?? null
}
};
await this._guardHosters(current, false);
return this._commit(current);
});
}
saveCloudLogSettings(settings) {
const snapshot = this._clone(settings);
return this._enqueueWrite(async () => {
const current = this.load();
current.globalSettings.cloudLogSync = snapshot;
return this._commit(current);
});
}
saveRemoteSettings() {
return this._enqueueWrite(async () => {
const current = this.load();
await this._guardHosters(current, false);
await this._commit(current);
return { enabled: false, retired: true, token: '' };
});
}
replaceSettings(config) {
return this._enqueueWrite(async () => {
const current = this.load();
const globalSettings = this._clone(config.globalSettings);
globalSettings.pendingQueue = current.globalSettings.pendingQueue ?? null;
globalSettings.uploadRecovery = current.globalSettings.uploadRecovery ?? null;
return this._commit({
hosters: this._clone(config.hosters),
hosterSettings: this._clone(config.hosterSettings),
globalSettings,
history: this._clone(current.history || []),
rotationCursors: {}
});
});
}
async loadHistory() {
if (this._historyMigrated) {
return this._readHistoryFile();
}
const config = this.load();
return config.history || [];
}
async _atomicWrite(data) {
const { config: nextCache } = await this._normalizeAndDecrypt(JSON.parse(data));
const tmpPath = this.filePath + '.tmp';
const backupPath = this.filePath + '.bak';
const fileHandle = await fs.promises.open(tmpPath, 'w');
let writeError = null;
try {
await fileHandle.writeFile(data);
await fileHandle.sync();
} catch (error) {
writeError = error;
}
try { await fileHandle.close(); } catch {}
if (writeError) throw writeError;
try {
const current = await fs.promises.readFile(this.filePath, 'utf-8');
if (current && current.trim().length > 2) await fs.promises.writeFile(backupPath, current, 'utf-8');
} catch {}
await fs.promises.rename(tmpPath, this.filePath);
this._cache = nextCache;
this._cacheKey = '';
this._lastConfigCheck = Date.now();
}
appendHistory(entry) {
if (this._historyMigrated) {
return this._enqueueHistoryWrite(async () => {
const arr = await this._readHistoryFile();
arr.push(entry);
const gs = this.load().globalSettings;
const retention = (gs && gs.historyRetention) || 'all';
const pruned = applyHistoryRetention(arr, retention, Date.now());
return this._writeHistoryFileAtomic(pruned);
});
}
return this._enqueueWrite(async () => {
const config = this.load();
config.history.push(entry);
const retention = (config.globalSettings && config.globalSettings.historyRetention) || 'all';
config.history = applyHistoryRetention(config.history, retention, Date.now());
return this._commit(config);
});
}
pruneHistory(retention, opts = {}) {
const dryRun = !!opts.dryRun;
if (this._historyMigrated) {
return this._enqueueHistoryWrite(async () => {
const current = await this._readHistoryFile();
const beforeBatches = current.length;
const beforeRows = countHistoryRows(current);
const pruned = applyHistoryRetention(current, retention, Date.now());
const result = {
removedBatches: beforeBatches - pruned.length,
removedRows: beforeRows - countHistoryRows(pruned),
keptBatches: pruned.length,
keptRows: countHistoryRows(pruned)
};
if (dryRun) return result;
return this._enqueueWrite(async () => {
const config = this.load();
const previousGlobalSettings = this._clone(config.globalSettings || {});
config.globalSettings = { ...previousGlobalSettings, historyRetention: String(retention || 'all') };
await this._guardHosters(config, false);
await this._commit(config);
try {
await this._writeHistoryFileAtomic(pruned);
} catch (historyError) {
config.globalSettings = previousGlobalSettings;
try {
await this._commit(config);
} catch (rollbackError) {
throw new AggregateError([historyError, rollbackError], 'Verlauf und Aufbewahrung konnten nicht konsistent gespeichert werden');
}
throw historyError;
}
return result;
});
});
}
return this._enqueueWrite(async () => {
const config = this.load();
const beforeBatches = config.history.length;
const beforeRows = countHistoryRows(config.history);
const pruned = applyHistoryRetention(config.history, retention, Date.now());
const result = {
removedBatches: beforeBatches - pruned.length,
removedRows: beforeRows - countHistoryRows(pruned),
keptBatches: pruned.length,
keptRows: countHistoryRows(pruned)
};
if (dryRun) return result;
config.history = pruned;
if (config.globalSettings) config.globalSettings.historyRetention = String(retention || 'all');
return this._commit(config).then(() => result);
});
}
clearHistory() {
if (this._historyMigrated) {
return this._enqueueHistoryWrite(() => this._writeHistoryFileAtomic([]));
}
return this._enqueueWrite(async () => {
const config = this.load();
config.history = [];
return this._commit(config);
});
}
saveRotationCursors(cursors) {
return this._enqueueWrite(async () => {
const config = this.load();
config.rotationCursors = (cursors && typeof cursors === 'object' && !Array.isArray(cursors)) ? cursors : {};
await this._guardHosters(config, false);
return this._commit(config);
});
}
}
module.exports = ConfigStore;
module.exports.HOSTER_ACCOUNT_TEMPLATES = HOSTER_ACCOUNT_TEMPLATES;
module.exports.HOSTER_NAMES = HOSTER_NAMES;
module.exports.HOSTER_ADD_OPTIONS = HOSTER_ADD_OPTIONS;
module.exports.HISTORY_RETENTION_OPTIONS = HISTORY_RETENTION_OPTIONS;
module.exports.applyHistoryRetention = applyHistoryRetention;
module.exports.countHistoryRows = countHistoryRows;