import WebSocket = require ( 'isomorphic-ws' ) ; import querystring = require ( 'querystring' ) ; import stream = require ( 'stream' ) ; import { V1Status } from './api' ; import { KubeConfig } from './config' ; import { isResizable , ResizableStream , TerminalSizeQueue } from './terminal-size-queue' ; import { WebSocketHandler , WebSocketInterface } from './web-socket-handler' ; export class Exec { public 'handler' : WebSocketInterface ; private terminalSizeQueue ? : TerminalSizeQueue ; public constructor ( config , wsInterface ? ) { this . handler = wsInterface || new WebSocketHandler ( config ) ; } param { string } namespace param { string } podName param { string } containerName param { ( string | string [ ] ) } command param { stream . Writable } stdout param { stream . Writable } stderr param { stream . Readable } stdin param { boolean } tty param { ( V1Status ) => void } statusCallback return { string } public async exec ( namespace , podName , containerName , command , stdout , stderr , stdin , tty , statusCallback ? ) { const query = { stdout : stdout != null , stderr : stderr != null , stdin : stdin != null , tty , command , container : containerName , } ; const queryStr = querystring . stringify ( query ) ; const path = `/api/v1/namespaces/${ namespace }/pods/${ podName }/exec?${ queryStr }` ; const conn = await this . handler . connect ( path , null , ( streamNum , buff ) : boolean => { const status = WebSocketHandler . handleStandardStreams ( streamNum , buff , stdout , stderr ) ; if ( status != null ) { if ( statusCallback ) { statusCallback ( status ) ; } return false ; } return true ; } ) ; if ( stdin != null ) { WebSocketHandler . handleStandardInput ( conn , stdin , WebSocketHandler . StdinStream ) ; } if ( isResizable ( stdout ) ) { this . terminalSizeQueue = new TerminalSizeQueue ( ) ; WebSocketHandler . handleStandardInput ( conn , this . terminalSizeQueue , WebSocketHandler . ResizeStream ) ; this . terminalSizeQueue . handleResizes ( ( stdout as any ) as ResizableStream ) ; } return conn ; } }