import { find } from 'lodash' ; import { ForwardingStatus , fwdTypes } from '../forward' ; import { ProxyTarget } from './simple-proxy-server' ; import { State } from '../types/redux' ; const getForwardingStatus = ( state , id , fwdId ) : ForwardingStatus | null => { const forwarding = find ( state . forwardings , { id , fwdId } ) ; return ! forwarding ? null : forwarding . state . status ; } ; const getForwardingBind = ( state , id , fwdId ) : string | null => { const forwarding = find ( state . forwardings , { id , fwdId } ) ; if ( ! forwarding ) { return null ; } if ( ! forwarding . state . params ) { return null ; } return forwarding . state . params . bind ; } ; type AddForwardingProxy = ( id : string , fwdId : string , target : ProxyTarget ) => void ; type RemoveForwardingProxy = ( id : string , fwdId : string ) => void ; const makeProxyTarget = ( address ) : ProxyTarget | null => { if ( address . includes ( '/' ) ) { return { socketPath : address } ; } if ( ( address . match ( /:/g ) || [ ] ) . length !== 1 ) { return null ; } const [ host , portStr ] = address . split ( ':' ) ; const port = parseInt ( portStr , 10 ) ; return { host , port } ; } ; const addForwardingProxies = ( prevState , state , addForwardingProxy ) => { state . forwardings . filter ( ( { config } ) => config . spec . type === fwdTypes . http ) . filter ( ( { id , fwdId } ) => getForwardingStatus ( prevState , id , fwdId ) !== 'connected' ) . filter ( ( { id , fwdId } ) => getForwardingStatus ( state , id , fwdId ) === 'connected' ) . forEach ( ( { id , fwdId } ) => { const bind = getForwardingBind ( state , id , fwdId ) ; if ( bind === null ) { return ; } const target = makeProxyTarget ( bind ) ; if ( target === null ) { return ; } addForwardingProxy ( id , fwdId , target ) ; } ) ; } ; const removeForwardingProxies = ( prevState , state , removeForwardingProxy ) => { state . forwardings . filter ( ( { config } ) => config . spec . type === fwdTypes . http ) . filter ( ( { id , fwdId } ) => getForwardingStatus ( prevState , id , fwdId ) === 'connected' ) . filter ( ( { id , fwdId } ) => getForwardingStatus ( state , id , fwdId ) !== 'connected' ) . forEach ( ( { id , fwdId } ) => { removeForwardingProxy ( id , fwdId ) ; } ) ; } ; export const onStateChange = ( prevState , state , addForwardingProxy , removeForwardingProxy ) => { addForwardingProxies ( prevState , state , addForwardingProxy ) ; removeForwardingProxies ( prevState , state , removeForwardingProxy ) ; } ;