import { ParsedHost , parseHost } from '@deepkit/core' ; import { ClientTransportAdapter , TransportConnectionHooks } from '@deepkit/rpc' ; import { connect } from 'net' ; import * as turbo from 'turbo-net' ; /**
 * Uses `turbo-net` module to connect to the server.
 */ export class TcpRpcClientAdapter implements ClientTransportAdapter { protected host : ParsedHost ; public bufferSize : number = 100 * 1024 ; constructor ( host ) { this . host = parseHost ( host ) ; } public async connect ( connection ) { const port = this . host . port || 8811 ; const socket = turbo . connect ( port , this . host . host ) ; socket . on ( 'close' , ( ) => { connection . onClose ( ) ; } ) ; socket . on ( 'error' , ( error ) => { connection . onError ( error ) ; } ) ; const bufferSize = this . bufferSize ; const buffer = Buffer . allocUnsafe ( bufferSize ) ; function read ( ) { socket . read ( buffer , onRead ) ; } function onRead ( err , buf , bytes ) { if ( bytes ) { connection . onData ( buf , bytes ) ; read ( ) ; } } read ( ) ; socket . on ( 'connect' , ( ) => { connection . onConnected ( { clientAddress : ( ) => { return this . host . toString ( ) ; } , bufferedAmount ( ) { return 0 ; } , close ( ) { socket . end ( ) ; } , send ( message ) { socket . write ( message ) ; } } ) ; } ) ; } } export class NetTcpRpcClientAdapter implements ClientTransportAdapter { protected host ; constructor ( host ) { this . host = parseHost ( host ) ; } public async connect ( connection ) { const port = this . host . port || 8811 ; const socket = this . host . isUnixSocket ? connect ( { path : this . host . unixSocket } ) : connect ( { port : port , host : this . host . host } ) ; socket . on ( 'data' , ( data ) => { connection . onData ( data ) ; } ) ; socket . on ( 'close' , ( ) => { connection . onClose ( ) ; } ) ; socket . on ( 'error' , ( error ) => { connection . onError ( error ) ; } ) ; socket . on ( 'connect' , async ( ) => { connection . onConnected ( { clientAddress : ( ) => { return this . host . toString ( ) ; } , bufferedAmount ( ) { return socket . bufferSize ; } , close ( ) { socket . end ( ) ; } , send ( message ) { socket . write ( message ) ; } } ) ; } ) ; } }