1+ const chalk = require ( 'chalk' )
12const { createProxyServer } = require ( 'http-proxy' )
23const { createServer } = require ( 'https' )
34const { APP_NAME , findPortForMappedHost } = require ( './config' )
45
6+ const LOG_STAMP_FORMATTER = new Intl . DateTimeFormat ( [ ] , { timeStyle : 'medium' } )
7+
8+ function logRequest ( { req, listeningPort, port } ) {
9+ const { hostname, pathname } = new URL ( req . url , `https://${ req . headers . host } ` )
10+ const frontlinePort = listeningPort === 443 ? '' : `:${ listeningPort } `
11+ const stamp = LOG_STAMP_FORMATTER . format ( new Date ( ) )
12+
13+ console . debug (
14+ chalk `{dim [${ stamp } ] ${ req . method } https://}${ hostname } {dim ${ pathname } ${ frontlinePort } => port} {bold ${ port } }`
15+ )
16+ }
17+
518// Exported API: sets up an HTTPS server on the listening port and registers SSL
619// configs and proxying for all known mappings. This does not start listening,
720// but the resulting object has a `start()` method to do just that. Errors and
821// successful launch both are automatically reported on the console.
9- function setupFrontline ( { listeningPort, mappings } ) {
22+ function setupFrontline ( { listeningPort, mappings, verbose } ) {
1023 const frontline = createServer ( proxyRequest )
1124
1225 // Here we register SSL configs (cert + key) for every mapped domain, so we
@@ -37,7 +50,9 @@ function setupFrontline({ listeningPort, mappings }) {
3750 // actionable reporting.
3851 function handleListenError ( err ) {
3952 if ( err . code === 'EADDRINUSE' ) {
40- console . error ( `It looks like listening port ${ listeningPort } is in use already. Check your existing services?` )
53+ console . error (
54+ `It looks like listening port ${ listeningPort } is in use already. Check your existing services?`
55+ )
4156 } else {
4257 console . error ( err )
4358 }
@@ -52,14 +67,15 @@ function setupFrontline({ listeningPort, mappings }) {
5267 return
5368 }
5469
55- // TODO: log queries if config option 'verbose' is set
70+ if ( verbose ) {
71+ logRequest ( { req, listeningPort, port } )
72+ }
5673 proxy . web ( req , res , { target : `http://localhost:${ port } ` } , ( err ) => {
5774 if ( err . code === 'ECONNREFUSED' ) {
5875 const msg = `Could not connect to proxied port ${ port } `
59- res . writeHead ( 503 , msg )
60- res . end ( msg )
76+ res . writeHead ( 503 , msg ) . end ( msg )
6177 } else {
62- res . writeHead ( 500 , err . message )
78+ res . writeHead ( 500 , err . message ) . end ( err . message )
6379 }
6480 } )
6581 }
@@ -70,7 +86,9 @@ function setupFrontline({ listeningPort, mappings }) {
7086 `${ APP_NAME } frontline listening on port ${ frontline . address ( ) . port } `
7187 )
7288 console . log ( 'Active port mappings:' )
73- console . table ( Array . from ( mappings ) . map ( ( [ domain , { port } ] ) => ( { domain, port } ) ) )
89+ console . table (
90+ Array . from ( mappings ) . map ( ( [ domain , { port } ] ) => ( { domain, port } ) )
91+ )
7492 console . log ( 'Hit Ctrl+C to stop' )
7593 }
7694
0 commit comments