@@ -7,13 +7,13 @@ import { memoize } from 'memoza'
77import { useSubscription } from 'seitu/react'
88import { createIndexedDbStorage } from 'seitu/web'
99
10- const safeStorageAvailable = memoize ( ( ) => window . electron ? window . electron . safeStorage . isEncryptionAvailable ( ) : false )
10+ const safeStorageAvailable = memoize ( async ( ) => window . electron ? window . electron . safeStorage . isEncryptionAvailable ( ) : false )
1111
1212function importAesKey ( raw : Uint8Array < ArrayBuffer > ) {
1313 return crypto . subtle . importKey ( 'raw' , raw , { name : 'AES-GCM' } , false , [ 'encrypt' , 'decrypt' ] )
1414}
1515
16- const connectionsStringsStorage = createIndexedDbStorage ( {
16+ const storage = createIndexedDbStorage ( {
1717 databaseName : 'secure-storage' ,
1818 storeName : 'connections-strings' ,
1919 schemas : {
@@ -38,13 +38,13 @@ const connectionsStringsStorage = createIndexedDbStorage({
3838} )
3939
4040function resetEncryptionKey ( ) {
41- return connectionsStringsStorage . set ( { encryptionKey : null , connectionStrings : { } } )
41+ return storage . set ( { encryptionKey : null , connectionStrings : { } } )
4242}
4343
4444const getEncryptionKey = memoize ( async ( ) : Promise < CryptoKey > => {
45- await connectionsStringsStorage . ready
45+ await storage . ready
4646
47- const stored = connectionsStringsStorage . get ( ) . encryptionKey
47+ const stored = storage . get ( ) . encryptionKey
4848
4949 if ( stored instanceof CryptoKey )
5050 return stored
@@ -62,7 +62,7 @@ const getEncryptionKey = memoize(async (): Promise<CryptoKey> => {
6262 }
6363
6464 const raw = crypto . getRandomValues ( new Uint8Array ( 32 ) )
65- await connectionsStringsStorage . set ( { encryptionKey : await safeStorage . encryptString ( bytesToBase64 ( raw ) ) } )
65+ await storage . set ( { encryptionKey : await safeStorage . encryptString ( bytesToBase64 ( raw ) ) } )
6666 return importAesKey ( raw )
6767 }
6868
@@ -72,7 +72,7 @@ const getEncryptionKey = memoize(async (): Promise<CryptoKey> => {
7272 await resetEncryptionKey ( )
7373
7474 const key = await crypto . subtle . generateKey ( { name : 'AES-GCM' , length : 256 } , false , [ 'encrypt' , 'decrypt' ] )
75- await connectionsStringsStorage . set ( { encryptionKey : key } )
75+ await storage . set ( { encryptionKey : key } )
7676 return key
7777} )
7878
@@ -85,19 +85,19 @@ async function decryptConnectionString(encryptedConnectionString: string) {
8585}
8686
8787export const connectionStringStorage = {
88- ready : connectionsStringsStorage . ready ,
89- has : ( id : string ) => id in connectionsStringsStorage . get ( ) . connectionStrings ,
90- get : ( id : string ) => connectionsStringsStorage . get ( ) . connectionStrings [ id ] ,
88+ ready : storage . ready ,
89+ has : ( id : string ) => id in storage . get ( ) . connectionStrings ,
90+ get : ( id : string ) => storage . get ( ) . connectionStrings [ id ] ,
9191 decrypt : ( id : string ) : Promise < string > => {
92- const record = connectionsStringsStorage . get ( ) . connectionStrings [ id ]
92+ const record = connectionStringStorage . get ( id )
9393 if ( ! record )
9494 throw new Error ( `No connection string found for connection "${ id } "` )
9595 return decryptConnectionString ( record . encrypted )
9696 } ,
9797 set : async ( id : string , connectionString : string ) => {
9898 const url = new SafeURL ( connectionString )
9999 const encrypted = await encryptConnectionString ( connectionString )
100- await connectionsStringsStorage . set ( prev => ( {
100+ await storage . set ( prev => ( {
101101 connectionStrings : {
102102 ...prev . connectionStrings ,
103103 [ id ] : {
@@ -113,15 +113,15 @@ export const connectionStringStorage = {
113113 } ) )
114114 } ,
115115 remove : async ( id : string ) => {
116- await connectionsStringsStorage . set ( ( prev ) => {
116+ await storage . set ( ( prev ) => {
117117 const connectionStrings = { ...prev . connectionStrings }
118118 delete connectionStrings [ id ]
119119 return { connectionStrings }
120120 } )
121121 } ,
122- clear : ( ) => connectionsStringsStorage . set ( { connectionStrings : { } } ) ,
122+ clear : ( ) => storage . set ( { connectionStrings : { } } ) ,
123123}
124124
125125export function useConnectionString ( id : string ) {
126- return useSubscription ( connectionsStringsStorage , { selector : state => state . connectionStrings [ id ] } )
126+ return useSubscription ( storage , { selector : state => state . connectionStrings [ id ] } )
127127}
0 commit comments