import * as net from "net"; import { ServerProxy } from "../../common/proxy"; import { DuplexProxy } from "./stream"; // tslint:disable completed-docs no-any export class NetSocketProxy extends DuplexProxy { public constructor(socket: net.Socket) { super(socket, ["connect", "lookup", "timeout"]); } public async connect(options: number | string | net.SocketConnectOpts, host?: string): Promise { this.instance.connect(options as any, host as any); } public async unref(): Promise { this.instance.unref(); } public async ref(): Promise { this.instance.ref(); } public async dispose(): Promise { this.instance.end(); this.instance.destroy(); this.instance.unref(); await super.dispose(); } } export class NetServerProxy extends ServerProxy { public constructor(instance: net.Server) { super({ bindEvents: ["close", "error", "listening"], doneEvents: ["close"], instance, }); } public async listen(handle?: net.ListenOptions | number | string, hostname?: string | number, backlog?: number): Promise { this.instance.listen(handle, hostname as any, backlog as any); } public async ref(): Promise { this.instance.ref(); } public async unref(): Promise { this.instance.unref(); } public async close(): Promise { this.instance.close(); } public async onConnection(cb: (proxy: NetSocketProxy) => void): Promise { this.instance.on("connection", (socket) => cb(new NetSocketProxy(socket))); } public async dispose(): Promise { this.instance.close(); this.instance.removeAllListeners(); } } export class NetModuleProxy { public async createSocket(options?: net.SocketConstructorOpts): Promise { return new NetSocketProxy(new net.Socket(options)); } public async createConnection(target: string | number | net.NetConnectOpts, host?: string): Promise { return new NetSocketProxy(net.createConnection(target as any, host)); } public async createServer(options?: { allowHalfOpen?: boolean, pauseOnConnect?: boolean }): Promise { return new NetServerProxy(net.createServer(options)); } }