import * as net from "net" ; export interface DestroyableServer extends net . Server { destroy ( ) : Promise < void > ; } export function destroyable ( server ) { const connections = { } ; server . on ( 'connection' , function ( conn ) { const key = conn . remoteAddress + ':' + conn . remotePort ; connections [ key ] = conn ; conn . on ( 'close' , function ( ) { delete connections [ key ] ; } ) ; } ) ; return Object . assign ( server , { destroy : ( ) => { return new Promise < void > ( ( resolve , reject ) => { server . close ( ( err ) => { if ( err ) reject ( err ) ; else resolve ( ) ; } ) ; for ( let key in connections ) { connections [ key ] . destroy ( ) ; } } ) ; } } ) ; }