diff --git a/lib/config-store.js b/lib/config-store.js index b8eba3e..7f2c54f 100644 --- a/lib/config-store.js +++ b/lib/config-store.js @@ -193,6 +193,8 @@ class ConfigStore { this._historyMigrated = false; this._cache = null; this._cacheKey = ''; + this._lastConfigCheck = 0; + this._refreshTask = null; this._perfLog = null; this._wqDepth = 0; @@ -353,126 +355,136 @@ class ConfigStore { return r; } - _loadImpl() { - 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 - 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]; + _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] = []; - } - } - - // 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] || {}) - }; - } - const savedGlobal = data.globalSettings || {}; - const globalSettings = { - ...DEFAULTS.globalSettings, - ...savedGlobal - }; - 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 - : {}; - const result = { hosters, hosterSettings, globalSettings, history: this._historyMigrated ? [] : (data.history || []), rotationCursors }; - // Decrypt credentials stored with safeStorage so the rest of the app - // keeps working with plaintext in memory. - secretStore.decryptCredentials(result); - if (stat) { - this._cache = result; - this._cacheKey = statKey; - } - return this._clone(result); - } catch (error) { - if (error instanceof secretStore.SecretStoreError) throw error; - const fresh = JSON.parse(JSON.stringify(DEFAULTS)); - fresh.globalSettings.logMode = normalizeLogMode(fresh.globalSettings); - return fresh; } + + // 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] = []; + } + } + + // 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] || {}) + }; + } + const savedGlobal = data.globalSettings || {}; + const globalSettings = { + ...DEFAULTS.globalSettings, + ...savedGlobal + }; + 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 + : {}; + const result = { hosters, hosterSettings, globalSettings, history: this._historyMigrated ? [] : (data.history || []), rotationCursors }; + // Decrypt credentials stored with safeStorage so the rest of the app + // keeps working with plaintext in memory. + secretStore.decryptCredentials(result); + return 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) { + if (error instanceof secretStore.SecretStoreError) throw error; + } + } + if (!this._cache) this._cache = this._normalizeConfig({}); } // Encrypt credential fields without mutating the caller's plaintext object. @@ -506,7 +518,11 @@ class ConfigStore { _enqueueWrite(fn, options = {}) { if (this._writesQuiesced && !options.allowDuringQuiesce) return Promise.reject(this._quiescedWriteError()); 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._writeQueue = operation.then( () => { this._wqDepth--; }, @@ -535,10 +551,10 @@ class ConfigStore { 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']) { try { - const raw = fs.readFileSync(p, 'utf-8'); + 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; @@ -547,9 +563,9 @@ class ConfigStore { return null; } - _guardHosters(current, hostersIntentional) { + async _guardHosters(current, hostersIntentional) { if (!hostersIntentional && !this._anyHosters(current)) { - const recovered = this._recoverHostersFromDisk(); + 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'); @@ -559,25 +575,25 @@ class ConfigStore { } save(config) { - return this._enqueueWrite(() => { + 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; - this._guardHosters(current, !!config.hosters); + 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(() => { + return this._enqueueWrite(async () => { const current = this.load(); current.globalSettings = { ...(current.globalSettings || {}), pendingQueue: snapshot }; - this._guardHosters(current, false); + await this._guardHosters(current, false); return this._commit(current); }, options); } @@ -587,7 +603,7 @@ class ConfigStore { paused: folderMonitor?.paused === true, pausedAt: folderMonitor?.paused === true ? (folderMonitor?.pausedAt ?? null) : null }; - return this._enqueueWrite(() => { + return this._enqueueWrite(async () => { const current = this.load(); const currentGlobalSettings = current.globalSettings || {}; current.globalSettings = { @@ -597,20 +613,20 @@ class ConfigStore { ...snapshot } }; - this._guardHosters(current, false); + 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(() => { + return this._enqueueWrite(async () => { const current = this.load(); current.globalSettings = { ...(current.globalSettings || {}), uploadRecovery: snapshot }; - this._guardHosters(current, false); + await this._guardHosters(current, false); return this._commit(current); }, options); } @@ -682,20 +698,20 @@ class ConfigStore { saveLastBrowseDirectory(directory) { const snapshot = String(directory || '').trim(); - return this._enqueueWrite(() => { + return this._enqueueWrite(async () => { const current = this.load(); current.globalSettings = { ...(current.globalSettings || {}), lastBrowseDirectory: snapshot }; - this._guardHosters(current, false); + await this._guardHosters(current, false); return this._commit(current); }); } saveRendererGlobalSettings(globalSettings) { const snapshot = this._clone(globalSettings || {}); - return this._enqueueWrite(() => { + return this._enqueueWrite(async () => { const current = this.load(); const currentGlobalSettings = current.globalSettings || {}; const currentFolderMonitor = currentGlobalSettings.folderMonitor || {}; @@ -714,14 +730,14 @@ class ConfigStore { pausedAt: currentFolderMonitor.pausedAt ?? null } }; - this._guardHosters(current, false); + await this._guardHosters(current, false); return this._commit(current); }); } saveCloudLogSettings(settings) { const snapshot = this._clone(settings); - return this._enqueueWrite(() => { + return this._enqueueWrite(async () => { const current = this.load(); current.globalSettings.cloudLogSync = snapshot; return this._commit(current); @@ -731,14 +747,14 @@ class ConfigStore { saveRemoteSettings() { return this._enqueueWrite(async () => { const current = this.load(); - this._guardHosters(current, false); + await this._guardHosters(current, false); await this._commit(current); return { enabled: false, retired: true, token: '' }; }); } replaceSettings(config) { - return this._enqueueWrite(() => { + return this._enqueueWrite(async () => { const current = this.load(); const globalSettings = this._clone(config.globalSettings); 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'); } catch {} await fs.promises.rename(tmpPath, this.filePath); - // Invalidate the read cache: the next load() re-reads + re-merges the - // 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._cache = this._normalizeConfig(JSON.parse(data)); this._cacheKey = ''; + this._lastConfigCheck = Date.now(); } appendHistory(entry) { @@ -801,7 +815,7 @@ class ConfigStore { return this._writeHistoryFileAtomic(pruned); }); } - return this._enqueueWrite(() => { + return this._enqueueWrite(async () => { const config = this.load(); config.history.push(entry); const retention = (config.globalSettings && config.globalSettings.historyRetention) || 'all'; @@ -833,7 +847,7 @@ class ConfigStore { const config = this.load(); const previousGlobalSettings = this._clone(config.globalSettings || {}); config.globalSettings = { ...previousGlobalSettings, historyRetention: String(retention || 'all') }; - this._guardHosters(config, false); + await this._guardHosters(config, false); await this._commit(config); try { await this._writeHistoryFileAtomic(pruned); @@ -850,7 +864,7 @@ class ConfigStore { }); }); } - return this._enqueueWrite(() => { + return this._enqueueWrite(async () => { const config = this.load(); const beforeBatches = config.history.length; const beforeRows = countHistoryRows(config.history); @@ -872,7 +886,7 @@ class ConfigStore { if (this._historyMigrated) { return this._enqueueHistoryWrite(() => this._writeHistoryFileAtomic([])); } - return this._enqueueWrite(() => { + return this._enqueueWrite(async () => { const config = this.load(); config.history = []; return this._commit(config); @@ -880,10 +894,10 @@ class ConfigStore { } saveRotationCursors(cursors) { - return this._enqueueWrite(() => { + return this._enqueueWrite(async () => { const config = this.load(); config.rotationCursors = (cursors && typeof cursors === 'object' && !Array.isArray(cursors)) ? cursors : {}; - this._guardHosters(config, false); + await this._guardHosters(config, false); return this._commit(config); }); } diff --git a/package-lock.json b/package-lock.json index 105f3e2..a0d99cf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "multi-hoster-uploader", - "version": "2.1.67", + "version": "2.1.68", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "multi-hoster-uploader", - "version": "2.1.67", + "version": "2.1.68", "dependencies": { "chokidar": "^4.0.3", "undici": "7.29.1", diff --git a/package.json b/package.json index 6e3e5e6..fa148b4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "multi-hoster-uploader", - "version": "2.1.67", + "version": "2.1.68", "description": "Upload files to doodstream, voe, vidmoly, byse simultaneously", "main": "main.js", "scripts": {