import httpProxy from 'http-proxy' ; export const getHttpProxyServer = async ( targetUrl , kbnTestServerConfig , onProxyResHandler ) : Promise < httpProxy > => { const proxyServer = httpProxy . createProxyServer ( { target : targetUrl , secure : false , selfHandleResponse : false , } ) ; proxyServer . on ( 'proxyRes' , ( proxyRes , req , res ) => { onProxyResHandler ( proxyRes , req , res ) ; } ) ; const proxyPort = getProxyPort ( kbnTestServerConfig ) ; proxyServer . listen ( proxyPort ) ; return proxyServer ; } ; export const getProxyPort = ( kbnTestServerConfig ) : number => { const proxyUrl = kbnTestServerConfig . find ( ( val ) => val . startsWith ( '--xpack.actions.proxyUrl=' ) ) . replace ( '--xpack.actions.proxyUrl=' , '' ) ; const urlObject = new URL ( proxyUrl ) ; return Number ( urlObject . port ) ; } ;