import { Socket } from "net" ; import { TLSSocket } from "tls" ; import { ExpandedTcpTransportConfiguration } from "../../config/expanded" ; import { Transport } from "./transport" ; export class TcpTransport implements Transport { public readonly stream : Socket | TLSSocket ; private readonly backingSocket ? : Socket ; private readonly config : ExpandedTcpTransportConfiguration ; public constructor ( config ) { this . config = config ; if ( config . secure ) { this . backingSocket = new Socket ( ) ; this . backingSocket . setNoDelay ( true ) ; this . stream = new TLSSocket ( this . backingSocket ) ; } else { this . stream = new Socket ( ) ; } this . stream . setNoDelay ( true ) ; this . stream . setDefaultEncoding ( "utf-8" ) ; this . stream . setEncoding ( "utf-8" ) ; this . stream . cork ( ) ; } public connect ( connectionListener ? ) { if ( this . backingSocket != null ) { this . backingSocket . connect ( this . config . port , this . config . host ) ; } this . stream . connect ( this . config . port , this . config . host , ( ) => { this . stream . uncork ( ) ; if ( connectionListener != null ) { connectionListener ( ) ; } } ) ; } }