|
1 | 1 | // This is a CommonJS wrapper around electron-store
|
2 | 2 |
|
3 | 3 | let storeInstance = null;
|
| 4 | +let initPromise = null; |
| 5 | +let isInitializing = false; |
| 6 | + |
| 7 | +// Get app info for proper store naming/location |
| 8 | +const getAppInfo = async () => { |
| 9 | + try { |
| 10 | + const { app } = await import('electron'); |
| 11 | + return { |
| 12 | + appName: app.name || 'tlon-desktop', |
| 13 | + isPackaged: app.isPackaged, |
| 14 | + userDataPath: app.getPath('userData'), |
| 15 | + }; |
| 16 | + } catch (e) { |
| 17 | + console.error('Failed to get app info:', e); |
| 18 | + return { |
| 19 | + appName: 'tlon-desktop', |
| 20 | + isPackaged: false, |
| 21 | + userDataPath: null, |
| 22 | + }; |
| 23 | + } |
| 24 | +}; |
4 | 25 |
|
5 | 26 | async function initStore() {
|
6 |
| - if (!storeInstance) { |
7 |
| - // Dynamically import electron-store (ESM) |
8 |
| - const { default: Store } = await import('electron-store'); |
9 |
| - storeInstance = new Store({ |
10 |
| - defaults: { |
11 |
| - shipUrl: '', |
12 |
| - } |
13 |
| - }); |
| 27 | + if (isInitializing) { |
| 28 | + return initPromise; |
14 | 29 | }
|
15 |
| - return storeInstance; |
| 30 | + |
| 31 | + if (storeInstance) { |
| 32 | + return storeInstance; |
| 33 | + } |
| 34 | + |
| 35 | + isInitializing = true; |
| 36 | + initPromise = (async () => { |
| 37 | + try { |
| 38 | + const { default: Store } = await import('electron-store'); |
| 39 | + const { isPackaged } = await getAppInfo(); |
| 40 | + |
| 41 | + const storeName = isPackaged ? 'tlon-auth-data' : 'tlon-auth-data-dev'; |
| 42 | + |
| 43 | + console.log(`Initializing electron-store with name: ${storeName}`); |
| 44 | + |
| 45 | + storeInstance = new Store({ |
| 46 | + name: storeName, |
| 47 | + clearInvalidConfig: true, // Clear if the config becomes corrupted |
| 48 | + defaults: { |
| 49 | + shipUrl: '', |
| 50 | + ship: '', |
| 51 | + encryptedAuthCookie: '', |
| 52 | + encryptionKey: '', |
| 53 | + }, |
| 54 | + }); |
| 55 | + |
| 56 | + console.log(`Store location: ${storeInstance.path}`); |
| 57 | + |
| 58 | + const allData = storeInstance.store; |
| 59 | + console.log('Store contents on init:', { |
| 60 | + hasShipUrl: !!allData.shipUrl, |
| 61 | + hasShip: !!allData.ship, |
| 62 | + hasEncryptedCookie: !!allData.encryptedAuthCookie, |
| 63 | + hasEncryptionKey: !!allData.encryptionKey, |
| 64 | + }); |
| 65 | + |
| 66 | + return storeInstance; |
| 67 | + } catch (error) { |
| 68 | + console.error('Failed to initialize electron-store:', error); |
| 69 | + // We won't throw, to avoid breaking the app, but we will return null |
| 70 | + // so the app can handle missing store |
| 71 | + return null; |
| 72 | + } finally { |
| 73 | + isInitializing = false; |
| 74 | + } |
| 75 | + })(); |
| 76 | + |
| 77 | + return initPromise; |
16 | 78 | }
|
17 | 79 |
|
18 |
| -// Expose a promise-based API |
19 | 80 | module.exports = {
|
20 | 81 | async get(key) {
|
21 |
| - const store = await initStore(); |
22 |
| - return store.get(key); |
| 82 | + try { |
| 83 | + const store = await initStore(); |
| 84 | + if (!store) return null; |
| 85 | + const value = store.get(key); |
| 86 | + if (key === 'encryptionKey' || key === 'encryptedAuthCookie') { |
| 87 | + console.log( |
| 88 | + `Reading ${key} from store: ${value ? 'exists' : 'missing'}` |
| 89 | + ); |
| 90 | + } else { |
| 91 | + console.log(`Reading ${key} from store:`, value); |
| 92 | + } |
| 93 | + return value; |
| 94 | + } catch (error) { |
| 95 | + console.error(`Error getting ${key} from store:`, error); |
| 96 | + return null; |
| 97 | + } |
23 | 98 | },
|
| 99 | + |
24 | 100 | async set(key, value) {
|
25 |
| - const store = await initStore(); |
26 |
| - store.set(key, value); |
| 101 | + try { |
| 102 | + const store = await initStore(); |
| 103 | + if (!store) { |
| 104 | + console.error(`Cannot set ${key}: store initialization failed`); |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + if (key === 'encryptionKey' || key === 'encryptedAuthCookie') { |
| 109 | + console.log(`Setting ${key} in store`); |
| 110 | + } else { |
| 111 | + console.log(`Setting ${key} in store:`, value); |
| 112 | + } |
| 113 | + |
| 114 | + store.set(key, value); |
| 115 | + |
| 116 | + const verifyValue = store.get(key); |
| 117 | + const writeSuccessful = |
| 118 | + (typeof value === 'string' && verifyValue === value) || |
| 119 | + (typeof value === 'object' && |
| 120 | + JSON.stringify(verifyValue) === JSON.stringify(value)); |
| 121 | + |
| 122 | + if (!writeSuccessful) { |
| 123 | + console.error(`Failed to verify write of ${key} to store`); |
| 124 | + return false; |
| 125 | + } |
| 126 | + |
| 127 | + return true; |
| 128 | + } catch (error) { |
| 129 | + console.error(`Error setting ${key} in store:`, error); |
| 130 | + return false; |
| 131 | + } |
27 | 132 | },
|
| 133 | + |
28 | 134 | async delete(key) {
|
29 |
| - const store = await initStore(); |
30 |
| - store.delete(key); |
| 135 | + try { |
| 136 | + const store = await initStore(); |
| 137 | + if (!store) return false; |
| 138 | + console.log(`Deleting ${key} from store`); |
| 139 | + store.delete(key); |
| 140 | + return true; |
| 141 | + } catch (error) { |
| 142 | + console.error(`Error deleting ${key} from store:`, error); |
| 143 | + return false; |
| 144 | + } |
31 | 145 | },
|
| 146 | + |
32 | 147 | async clear() {
|
33 |
| - const store = await initStore(); |
34 |
| - store.clear(); |
35 |
| - } |
| 148 | + try { |
| 149 | + const store = await initStore(); |
| 150 | + if (!store) return false; |
| 151 | + console.log('Clearing store'); |
| 152 | + store.clear(); |
| 153 | + return true; |
| 154 | + } catch (error) { |
| 155 | + console.error('Error clearing store:', error); |
| 156 | + return false; |
| 157 | + } |
| 158 | + }, |
| 159 | + |
| 160 | + async checkStore() { |
| 161 | + try { |
| 162 | + const store = await initStore(); |
| 163 | + if (!store) |
| 164 | + return { healthy: false, message: 'Store initialization failed' }; |
| 165 | + |
| 166 | + const path = store.path; |
| 167 | + const allData = store.store; |
| 168 | + |
| 169 | + return { |
| 170 | + healthy: true, |
| 171 | + path, |
| 172 | + hasShipUrl: !!allData.shipUrl, |
| 173 | + hasShip: !!allData.ship, |
| 174 | + hasEncryptedCookie: !!allData.encryptedAuthCookie, |
| 175 | + hasEncryptionKey: !!allData.encryptionKey, |
| 176 | + }; |
| 177 | + } catch (error) { |
| 178 | + return { |
| 179 | + healthy: false, |
| 180 | + message: error.message, |
| 181 | + }; |
| 182 | + } |
| 183 | + }, |
36 | 184 | };
|
0 commit comments