import { pick } from "remeda" ; import { webFrame } from "electron" ; import { Config } from "src/shared/model/options" ; import { DEFAULT_API_CALL_TIMEOUT } from "src/shared/constants" ; import { IPC_MAIN_API } from "src/shared/api/main-process" ; import { LOGGER } from "src/electron-preload/lib/electron-exposure/logger" ; import { Logger } from "src/shared/model/common" ; import { ProtonApiError } from "src/electron-preload/webview/primary/types" ; import { curryFunctionMembers , depersonalizeProtonApiUrl } from "src/shared/util" ; export const buildLoggerBundle = ( prefix ) : Logger => curryFunctionMembers ( LOGGER , prefix ) ; export const applyZoomFactor = ( _logger ) : void => { const logger = curryFunctionMembers ( _logger , nameof ( applyZoomFactor ) ) ; logger . verbose ( ) ; ( async ( ) => { const { zoomFactor , zoomFactorDisabled } = await resolveCachedConfig ( logger ) ; logger . verbose ( JSON . stringify ( { zoomFactorDisabled } ) ) ; if ( zoomFactorDisabled ) { return ; } const webFrameZoomFactor = webFrame . getZoomFactor ( ) ; logger . verbose ( "config.zoomFactor" , JSON . stringify ( zoomFactor ) ) ; logger . verbose ( "webFrame.getZoomFactor() (before)" , JSON . stringify ( webFrameZoomFactor ) ) ; if ( webFrameZoomFactor !== zoomFactor ) { webFrame . setZoomFactor ( zoomFactor ) ; logger . verbose ( "webFrame.getZoomFactor() (after)" , JSON . stringify ( webFrame . getZoomFactor ( ) ) ) ; } } ) ( ) . catch ( ( error ) => { console . error ( error ) ; logger . error ( error ) ; } ) ; } ; export const isProtonApiError = ( error ) : error is ProtonApiError => { const result = ( typeof error === "object" && typeof ( error as ProtonApiError ) . name === "string" && typeof ( error as ProtonApiError ) . message === "string" && ! isNaN ( Number ( ( error as ProtonApiError ) . status ) ) && ( typeof ( error as ProtonApiError ) . response === "object" || [ "aborterror" , "timeouterror" , "offlineerror" ] . includes ( ( error as ProtonApiError ) . name . toLowerCase ( ) ) ) ) ; if ( BUILD_ENVIRONMENT === "development" && result ) { console . log ( `isProtonApiError() result:` , { error } , JSON . stringify ( { result } ) ) ; } return result ; } ; type SanitizedProtonApiError = NoExtraProps < Pick < ProtonApiError , "name" | "message" | "status" > & { responseUrl ? : string ; responseStatusText ? : string ; dataCode ? : number ; dataError ? : string ; dataErrorDescription ? : string ; } > ; export const sanitizeProtonApiError = ( error ) : typeof error extends ProtonApiError ? SanitizedProtonApiError : unknown => { if ( isProtonApiError ( error ) ) { const result = { ... pick ( error , [ "name" , "message" , "status" ] ) , responseUrl : error . response ?. url && depersonalizeProtonApiUrl ( error . response ?. url ) , responseStatusText : error . response ?. statusText , dataCode : error . data ?. Code , dataError : typeof error . data ?. Error === "string" ? error . data ?. Error : undefined , dataErrorDescription : typeof error . data ?. ErrorDescription === "string" ? error . data ?. ErrorDescription : undefined , } ; if ( ! result . message ) { for ( const propName of [ "dataError" , "responseStatusText" , "dataErrorDescription" ] as const ) { const propValue = result [ propName ] ; if ( propValue ) { result . message = propValue ; break ; } } } return result as any ; } return error as any ; } ; export const resolveIpcMainApi = ( { timeoutMs = DEFAULT_API_CALL_TIMEOUT , ... restOptions } ) : ReturnType < typeof IPC_MAIN_API . client > => { return IPC_MAIN_API . client ( { options : { timeoutMs , ... restOptions } } ) ; } ; export const resolveCachedConfig = ( ( ) => { let value ; const result = async ( logger ) => { if ( value ) { return value ; } return value = await resolveIpcMainApi ( { logger } ) ( "readConfig" ) ( ) ; } ; return result ; } ) ( ) ;