@@ -7,7 +7,6 @@ const util = require("util");
7
7
const fs = require ( "graceful-fs" ) ;
8
8
const ipaddr = require ( "ipaddr.js" ) ;
9
9
const internalIp = require ( "internal-ip" ) ;
10
- const killable = require ( "killable" ) ;
11
10
const express = require ( "express" ) ;
12
11
const { validate } = require ( "schema-utils" ) ;
13
12
const schema = require ( "./options.json" ) ;
@@ -35,6 +34,7 @@ class Server {
35
34
this . staticWatchers = [ ] ;
36
35
// Keep track of websocket proxies for external websocket upgrade.
37
36
this . webSocketProxies = [ ] ;
37
+ this . sockets = [ ] ;
38
38
this . compiler = compiler ;
39
39
}
40
40
@@ -682,8 +682,6 @@ class Server {
682
682
this . setupFeatures ( ) ;
683
683
this . createServer ( ) ;
684
684
685
- killable ( this . server ) ;
686
-
687
685
if ( this . options . setupExitSignals ) {
688
686
const signals = [ "SIGINT" , "SIGTERM" ] ;
689
687
@@ -1144,9 +1142,6 @@ class Server {
1144
1142
}
1145
1143
1146
1144
createServer ( ) {
1147
- const https = require ( "https" ) ;
1148
- const http = require ( "http" ) ;
1149
-
1150
1145
if ( this . options . https ) {
1151
1146
if ( this . options . http2 ) {
1152
1147
// TODO: we need to replace spdy with http2 which is an internal module
@@ -1160,12 +1155,26 @@ class Server {
1160
1155
this . app
1161
1156
) ;
1162
1157
} else {
1158
+ const https = require ( "https" ) ;
1159
+
1163
1160
this . server = https . createServer ( this . options . https , this . app ) ;
1164
1161
}
1165
1162
} else {
1163
+ const http = require ( "http" ) ;
1164
+
1166
1165
this . server = http . createServer ( this . app ) ;
1167
1166
}
1168
1167
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
+
1169
1178
this . server . on ( "error" , ( error ) => {
1170
1179
throw error ;
1171
1180
} ) ;
@@ -1775,34 +1784,42 @@ class Server {
1775
1784
}
1776
1785
1777
1786
async stop ( ) {
1778
- if ( this . webSocketProxies . length > 0 ) {
1779
- this . webSocketProxies = [ ] ;
1780
- }
1787
+ this . webSocketProxies = [ ] ;
1781
1788
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 ( ) ) ) ;
1784
1790
1785
- this . staticWatchers = [ ] ;
1786
- }
1791
+ this . staticWatchers = [ ] ;
1787
1792
1788
1793
if ( this . webSocketServer ) {
1789
1794
await new Promise ( ( resolve ) => {
1790
1795
this . webSocketServer . implementation . close ( ( ) => {
1796
+ this . webSocketServer = null ;
1797
+
1791
1798
resolve ( ) ;
1792
1799
} ) ;
1793
- } ) ;
1794
1800
1795
- this . webSocketServer = null ;
1801
+ for ( const client of this . webSocketServer . clients ) {
1802
+ client . terminate ( ) ;
1803
+ }
1804
+
1805
+ this . webSocketServer . clients = [ ] ;
1806
+ } ) ;
1796
1807
}
1797
1808
1798
1809
if ( this . server ) {
1799
1810
await new Promise ( ( resolve ) => {
1800
- this . server . kill ( ( ) => {
1811
+ this . server . close ( ( ) => {
1812
+ this . server = null ;
1813
+
1801
1814
resolve ( ) ;
1802
1815
} ) ;
1803
- } ) ;
1804
1816
1805
- this . server = null ;
1817
+ for ( const socket of this . sockets ) {
1818
+ socket . destroy ( ) ;
1819
+ }
1820
+
1821
+ this . sockets = [ ] ;
1822
+ } ) ;
1806
1823
1807
1824
if ( this . middleware ) {
1808
1825
await new Promise ( ( resolve , reject ) => {
0 commit comments