import { Mockttp , getLocal } from 'mockttp' ; import { Interceptor } from '..' ; import { HtkConfig } from '../../config' ; import { getTerminalEnvVars } from './terminal-env-overrides' ; import { getShellScript } from './terminal-scripts' ; interface ServerState { server : Mockttp ; isActive : boolean ; } export class ExistingTerminalInterceptor implements Interceptor { private servers : { [ proxyPort ] : ServerState ; } = { } ; id = 'existing-terminal' ; version = '1.0.0' ; constructor ( private config ) { } isActivable ( ) { return Promise . resolve ( process . platform !== 'win32' ) ; } isActive ( proxyPort ) { const serverState = this . servers [ proxyPort ] ; return ! ! serverState && serverState . isActive ; } async activate ( proxyPort , options = { } ) { if ( this . servers [ proxyPort ] ) { this . servers [ proxyPort ] . isActive = false ; return { port : this . servers [ proxyPort ] . server . port } ; } const server = getLocal ( ) ; await server . start ( { startPort : proxyPort + 1 , endPort : 65535 } ) ; const envVars = getTerminalEnvVars ( proxyPort , this . config . https , 'runtime-inherit' , { } , { dockerEnabled : options . dockerEnabled } ) ; const setupScript = getShellScript ( server . urlFor ( '/success' ) , envVars ) ; const serverState = { server , isActive : false } ; await server . get ( '/setup' ) . thenCallback ( ( ) => { return { status : 200 , headers : { "content-type" : "text/x-shellscript" } , body : setupScript } ; } ) ; await server . post ( '/success' ) . thenCallback ( ( ) => { serverState . isActive = true ; return { status : 200 } ; } ) ; this . servers [ proxyPort ] = serverState ; return { port : server . port } ; } async deactivate ( proxyPort ) { if ( this . servers [ proxyPort ] ) { await this . servers [ proxyPort ] . server . stop ( ) ; delete this . servers [ proxyPort ] ; } } deactivateAll ( ) { return Promise . all ( Object . keys ( this . servers ) . map ( ( port ) => this . deactivate ( parseInt ( port , 10 ) ) ) ) . then ( ( ) => { } ) ; } }