UNPKG

@deepkit/rpc-tcp

Version:

45 lines 1.45 kB
import { parseHost } from '@deepkit/core'; import { connect } from 'net'; /* * Uses the node `net` module to connect. Supports unix sockets. */ export class RpcTcpClientAdapter { constructor(host) { this.host = parseHost(host); } 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.readBinary(data); }); socket.on('close', () => { connection.onClose('socket closed'); }); socket.on('error', (error) => { error = error instanceof Error ? error : new Error(String(error)); connection.onError(error); }); socket.on('connect', async () => { connection.onConnected({ clientAddress: () => { return this.host.toString(); }, bufferedAmount() { //implement that to step back when too big return socket.bufferSize; }, close() { socket.end(); }, writeBinary(message) { socket.write(message); } }); }); } } //# sourceMappingURL=client.js.map