import path from 'path' ; import { Module } from 'module' ; import { Server } from 'net' ; const originalListen = Server . prototype . listen ; const originalAddress = Server . prototype . address ; const envPort = process . env . PORT ; declare module 'net' { interface Server { __tramvai_cli_port ? : number ; } } Server . prototype . listen = function ( this , opts ? , ... args ) { let hasPort = false ; if ( `${ opts }` === envPort ) { hasPort = true ; this . __tramvai_cli_port = opts ; originalListen . call ( this , 0 , ... args ) ; } else if ( `${ opts ?. port }` === envPort ) { hasPort = true ; this . __tramvai_cli_port = opts . port ; originalListen . call ( this , { ... opts , port : 0 } , ... args ) ; } if ( hasPort ) { this . once ( 'listening' , ( ) => { process . send ( { cmd : 'listen' , port : ( this . address ( ) as any ) . __tramvai_cli_port } ) ; } ) ; return this ; } return originalListen . call ( this , opts , ... args ) ; } ; Server . prototype . address = function ( this ) { const address = originalAddress . call ( this ) ; if ( typeof this . __tramvai_cli_port !== 'undefined' ) { return { ... address , __tramvai_cli_port : address . port , port : + envPort , } ; } return address ; } ; function requireFromString ( code , filename ) { const newModule = new Module ( filename , module . parent ) ; newModule . filename = filename ; newModule . paths = ( Module as any ) . _nodeModulePaths ( path . dirname ( filename ) ) ; ( newModule as any ) . _compile ( code , filename ) ; return newModule . exports ; } process . on ( 'message' , ( { filename , script } ) => { try { requireFromString ( script , filename ) ; } catch ( err ) { console . error ( err ) ; } } ) ;