import * as net from "net" ; import { ServerProxy } from "../common/proxy" ; import { DuplexProxy } from "./stream" ; export class NetSocketProxy extends DuplexProxy < net . Socket > { public constructor ( socket ) { super ( socket , [ "connect" , "lookup" , "timeout" ] ) ; } public async connect ( options , host ? ) { this . instance . connect ( options as any , host as any ) ; } public async unref ( ) { this . instance . unref ( ) ; } public async ref ( ) { this . instance . ref ( ) ; } public async dispose ( ) { this . instance . end ( ) ; this . instance . destroy ( ) ; this . instance . unref ( ) ; await super . dispose ( ) ; } } export class NetServerProxy extends ServerProxy < net . Server > { public constructor ( instance ) { super ( { bindEvents : [ "close" , "error" , "listening" ] , doneEvents : [ "close" ] , instance , } ) ; } public async listen ( handle ? , hostname ? , backlog ? ) { this . instance . listen ( handle , hostname as any , backlog as any ) ; } public async ref ( ) { this . instance . ref ( ) ; } public async unref ( ) { this . instance . unref ( ) ; } public async close ( ) { this . instance . close ( ) ; } public async onConnection ( cb ) { this . instance . on ( "connection" , ( socket ) => cb ( new NetSocketProxy ( socket ) ) ) ; } public async dispose ( ) { this . instance . close ( ) ; this . instance . removeAllListeners ( ) ; } } export class NetModuleProxy { public async createSocket ( options ? ) { return new NetSocketProxy ( new net . Socket ( options ) ) ; } public async createConnection ( target , host ? ) { return new NetSocketProxy ( net . createConnection ( target as any , host ) ) ; } public async createServer ( options ? ) { return new NetServerProxy ( net . createServer ( options ) ) ; } }