@@ -7,7 +7,6 @@ const util = require("util");
77const  fs  =  require ( "graceful-fs" ) ; 
88const  ipaddr  =  require ( "ipaddr.js" ) ; 
99const  internalIp  =  require ( "internal-ip" ) ; 
10- const  killable  =  require ( "killable" ) ; 
1110const  express  =  require ( "express" ) ; 
1211const  {  validate }  =  require ( "schema-utils" ) ; 
1312const  schema  =  require ( "./options.json" ) ; 
@@ -35,6 +34,7 @@ class Server {
3534    this . staticWatchers  =  [ ] ; 
3635    // Keep track of websocket proxies for external websocket upgrade. 
3736    this . webSocketProxies  =  [ ] ; 
37+     this . sockets  =  [ ] ; 
3838    this . compiler  =  compiler ; 
3939  } 
4040
@@ -682,8 +682,6 @@ class Server {
682682    this . setupFeatures ( ) ; 
683683    this . createServer ( ) ; 
684684
685-     killable ( this . server ) ; 
686- 
687685    if  ( this . options . setupExitSignals )  { 
688686      const  signals  =  [ "SIGINT" ,  "SIGTERM" ] ; 
689687
@@ -1144,9 +1142,6 @@ class Server {
11441142  } 
11451143
11461144  createServer ( )  { 
1147-     const  https  =  require ( "https" ) ; 
1148-     const  http  =  require ( "http" ) ; 
1149- 
11501145    if  ( this . options . https )  { 
11511146      if  ( this . options . http2 )  { 
11521147        // TODO: we need to replace spdy with http2 which is an internal module 
@@ -1160,12 +1155,26 @@ class Server {
11601155          this . app 
11611156        ) ; 
11621157      }  else  { 
1158+         const  https  =  require ( "https" ) ; 
1159+ 
11631160        this . server  =  https . createServer ( this . options . https ,  this . app ) ; 
11641161      } 
11651162    }  else  { 
1163+       const  http  =  require ( "http" ) ; 
1164+ 
11661165      this . server  =  http . createServer ( this . app ) ; 
11671166    } 
11681167
1168+     this . server . on ( "connection" ,  ( socket )  =>  { 
1169+       // Add socket to list 
1170+       this . sockets . push ( socket ) ; 
1171+ 
1172+       socket . once ( "close" ,  ( )  =>  { 
1173+         // Remove socket from list 
1174+         this . sockets . splice ( this . sockets . indexOf ( socket ) ,  1 ) ; 
1175+       } ) ; 
1176+     } ) ; 
1177+ 
11691178    this . server . on ( "error" ,  ( error )  =>  { 
11701179      throw  error ; 
11711180    } ) ; 
@@ -1775,34 +1784,42 @@ class Server {
17751784  } 
17761785
17771786  async  stop ( )  { 
1778-     if  ( this . webSocketProxies . length  >  0 )  { 
1779-       this . webSocketProxies  =  [ ] ; 
1780-     } 
1787+     this . webSocketProxies  =  [ ] ; 
17811788
1782-     if  ( this . staticWatchers . length  >  0 )  { 
1783-       await  Promise . all ( this . staticWatchers . map ( ( watcher )  =>  watcher . close ( ) ) ) ; 
1789+     await  Promise . all ( this . staticWatchers . map ( ( watcher )  =>  watcher . close ( ) ) ) ; 
17841790
1785-       this . staticWatchers  =  [ ] ; 
1786-     } 
1791+     this . staticWatchers  =  [ ] ; 
17871792
17881793    if  ( this . webSocketServer )  { 
17891794      await  new  Promise ( ( resolve )  =>  { 
17901795        this . webSocketServer . implementation . close ( ( )  =>  { 
1796+           this . webSocketServer  =  null ; 
1797+ 
17911798          resolve ( ) ; 
17921799        } ) ; 
1793-       } ) ; 
17941800
1795-       this . webSocketServer  =  null ; 
1801+         for  ( const  client  of  this . webSocketServer . clients )  { 
1802+           client . terminate ( ) ; 
1803+         } 
1804+ 
1805+         this . webSocketServer . clients  =  [ ] ; 
1806+       } ) ; 
17961807    } 
17971808
17981809    if  ( this . server )  { 
17991810      await  new  Promise ( ( resolve )  =>  { 
1800-         this . server . kill ( ( )  =>  { 
1811+         this . server . close ( ( )  =>  { 
1812+           this . server  =  null ; 
1813+ 
18011814          resolve ( ) ; 
18021815        } ) ; 
1803-       } ) ; 
18041816
1805-       this . server  =  null ; 
1817+         for  ( const  socket  of  this . sockets )  { 
1818+           socket . destroy ( ) ; 
1819+         } 
1820+ 
1821+         this . sockets  =  [ ] ; 
1822+       } ) ; 
18061823
18071824      if  ( this . middleware )  { 
18081825        await  new  Promise ( ( resolve ,  reject )  =>  { 
0 commit comments