fix(main): add safeStorage API compatibility check

- Check if getSelectedStorageBackend exists before calling (Electron 30+)
- Fallback to isEncryptionAvailable() for older Electron versions
- Add try-catch to prevent unhandled promise rejection
This commit is contained in:
2026-03-12 11:03:50 +08:00
parent 184919b562
commit 8b0805bebf

View File

@@ -81,15 +81,31 @@ function writeJsonFile<T>(filePath: string, data: T): void {
* On Linux, this may return 'basic_text' if no keyring is available * On Linux, this may return 'basic_text' if no keyring is available
*/ */
export function isSecureStorageAvailable(): boolean { export function isSecureStorageAvailable(): boolean {
const backend = safeStorage.getSelectedStorageBackend(); try {
return backend !== "basic_text"; // Check if the method exists (added in Electron 30+)
if (typeof safeStorage.getSelectedStorageBackend === 'function') {
const backend = safeStorage.getSelectedStorageBackend()
return backend !== 'basic_text'
}
// Fallback: check if encryption is available
return safeStorage.isEncryptionAvailable()
} catch {
return false
}
} }
/** /**
* Get the current storage backend name * Get the current storage backend name
*/ */
export function getStorageBackend(): string { export function getStorageBackend(): string {
return safeStorage.getSelectedStorageBackend(); try {
if (typeof safeStorage.getSelectedStorageBackend === 'function') {
return safeStorage.getSelectedStorageBackend()
}
return 'unknown'
} catch {
return 'unknown'
}
} }
/** /**