import chalk from 'chalk' ; import isPortReachable from 'is-port-reachable' ; import Koa from 'koa' ; import proxy from 'koa-better-http-proxy' ; import url from 'url' ; export async function createProxy ( config ) { const { name , port , localURL , stagingURL , localPathResolver } = config ; console . log ( chalk . green ( `Starting "${ name }" proxy on port ${ port }...` ) ) ; const { port : localPort , hostname : localHost } = url . parse ( localURL ) ; let useLocalhost = await isPortReachable ( localPort , { host : localHost } ) ; setInterval ( async ( ) => { const isLocalAvailable = await isPortReachable ( localPort , { host : localHost } ) ; if ( useLocalhost !== isLocalAvailable ) { useLocalhost = isLocalAvailable ; console . log ( chalk . green ( `Detected change in local "${ name }" service, now proxying to -> ${ useLocalhost ? localURL : stagingURL }` ) ) ; } } , 1000 ) ; const app = new Koa ( ) ; app . use ( proxy ( localURL , { filter : ( ) => useLocalhost , proxyReqPathResolver : async ( ctx ) => { console . log ( chalk . yellow ( `${ ctx . request . method } ${ ctx . url } (local)` ) ) ; const path = url . parse ( ctx . url ) . path ! ; return localPathResolver ? localPathResolver ( ctx , path ) : path ! ; } , } ) ) ; app . use ( proxy ( stagingURL , { filter : ( ) => ! useLocalhost , proxyReqPathResolver : async ( ctx ) => { console . log ( chalk . yellow ( `${ ctx . request . method } ${ ctx . url } (staging)` ) ) ; return url . parse ( ctx . url ) . path ! ; } , } ) ) ; const server = app . listen ( port ) ; console . log ( chalk . green ( `Listening for "${ name }" connections on port ${ port }, proxying to -> ${ useLocalhost ? localURL : stagingURL }` ) ) ; return server ; }