import { BrowserWindow , IpcMain } from 'electron' ; import { debug } from 'electron-log' ; import windowState from 'electron-window-state' ; import { join } from 'path' ; import { ipcChannels } from '../src/shared' ; import { APP_ROOT , BASE_URL } from './constants' ; import { httpProxy } from './httpProxy' ; import { clearProxyCache } from './lnd/lndProxyServer' ; import { unzip , zip } from './utils/zip' ; const openWindow = async ( args ) : Promise < boolean > => { debug ( 'openWindow' , args ) ; const winState = windowState ( { defaultWidth : 800 , defaultHeight : 600 , file : `window-state-terminal.json` , } ) ; let window = new BrowserWindow ( { x : winState . x , y : winState . y , width : winState . width , height : winState . height , minWidth : 400 , icon : join ( APP_ROOT , 'assets' , 'icon.png' ) , show : false , webPreferences : { nodeIntegration : true , contextIsolation : false , enableRemoteModule : true , } , } ) ; window . setMenuBarVisibility ( false ) ; window . on ( 'closed' , ( ) => { window = null ; } ) ; window . once ( 'ready-to-show' , ( ) => { if ( window ) window . show ( ) ; } ) ; await window . loadURL ( `${ BASE_URL }#${ args . url }` ) ; winState . manage ( window ) ; return true ; } ; /**
 * Clears the LND proxy cache of LND nodes. This must return a promise to be
 * consistent with all the other listeners
 */ const clearCache = ( ) : Promise < { success : boolean ; } > => { clearProxyCache ( ) ; return Promise . resolve ( { success : true } ) ; } ; /**
 * A mapping of electron IPC channel names to the functions to execute when
 * messages are received
 */ const listeners = { [ ipcChannels . openWindow ] : openWindow , [ ipcChannels . clearCache ] : clearCache , [ ipcChannels . http ] : httpProxy , [ ipcChannels . zip ] : zip , [ ipcChannels . unzip ] : unzip , } ; param ipc export const initAppIpcListener = ( ipc ) => { const log = ( msg , ... rest ) => debug ( `AppIpcListener: ${ msg }` , ... rest ) ; Object . entries ( listeners ) . forEach ( ( [ channel , func ] ) => { const requestChan = `app-${ channel }-request` ; const responseChan = `app-${ channel }-response` ; log ( `listening for ipc command "${ channel }"` ) ; ipc . on ( requestChan , async ( event , ... args ) => { log ( `received request "${ requestChan }"` , JSON . stringify ( args , null , 2 ) ) ; let uniqueChan = responseChan ; if ( args && args [ 0 ] && args [ 0 ] . replyTo ) { uniqueChan = args [ 0 ] . replyTo ; } try { const result = await func ( ... args ) ; log ( `send response "${ uniqueChan }"` , JSON . stringify ( result , null , 2 ) ) ; event . reply ( uniqueChan , result ) ; } catch ( err ) { log ( `send error "${ uniqueChan }"` , JSON . stringify ( err , null , 2 ) ) ; event . reply ( uniqueChan , { err : err . message } ) ; } } ) ; } ) ; } ;