@@ -160,6 +160,12 @@ class Server {
160
160
options . allowedHosts = [ options . allowedHosts ] ;
161
161
}
162
162
163
+ if ( typeof options . bonjour === "undefined" ) {
164
+ options . bonjour = false ;
165
+ } else if ( typeof options . bonjour === "boolean" ) {
166
+ options . bonjour = options . bonjour ? { } : false ;
167
+ }
168
+
163
169
if (
164
170
typeof options . client === "undefined" ||
165
171
( typeof options . client === "object" && options . client !== null )
@@ -203,7 +209,22 @@ class Server {
203
209
options . compress = true ;
204
210
}
205
211
206
- options . devMiddleware = options . devMiddleware || { } ;
212
+ if ( typeof options . devMiddleware === "undefined" ) {
213
+ options . devMiddleware = { } ;
214
+ }
215
+
216
+ // No need to normalize `headers`
217
+
218
+ if ( typeof options . historyApiFallback === "undefined" ) {
219
+ options . historyApiFallback = false ;
220
+ } else if (
221
+ typeof options . historyApiFallback === "boolean" &&
222
+ options . historyApiFallback
223
+ ) {
224
+ options . historyApiFallback = { } ;
225
+ }
226
+
227
+ // No need to normalize `host`
207
228
208
229
options . hot =
209
230
typeof options . hot === "boolean" || options . hot === "only"
@@ -525,6 +546,31 @@ class Server {
525
546
} ) ;
526
547
}
527
548
549
+ if ( typeof options . watchFiles === "string" ) {
550
+ options . watchFiles = [ { paths : options . watchFiles , options : { } } ] ;
551
+ } else if (
552
+ typeof options . watchFiles === "object" &&
553
+ options . watchFiles !== null &&
554
+ ! Array . isArray ( options . watchFiles )
555
+ ) {
556
+ options . watchFiles = [
557
+ {
558
+ paths : options . watchFiles . paths ,
559
+ options : options . watchFiles . options || { } ,
560
+ } ,
561
+ ] ;
562
+ } else if ( Array . isArray ( options . watchFiles ) ) {
563
+ options . watchFiles = options . watchFiles . map ( ( item ) => {
564
+ if ( typeof item === "string" ) {
565
+ return { paths : item , options : { } } ;
566
+ }
567
+
568
+ return { paths : item . paths , options : item . options || { } } ;
569
+ } ) ;
570
+ } else {
571
+ options . watchFiles = [ ] ;
572
+ }
573
+
528
574
const defaultWebSocketServerType = "ws" ;
529
575
const defaultWebSocketServerOptions = { path : "/ws" } ;
530
576
@@ -844,24 +890,20 @@ class Server {
844
890
}
845
891
846
892
setupHistoryApiFallbackFeature ( ) {
847
- const historyApiFallback = require ( "connect-history-api-fallback" ) ;
893
+ const { historyApiFallback } = this . options ;
848
894
849
- const options =
850
- typeof this . options . historyApiFallback !== "boolean"
851
- ? this . options . historyApiFallback
852
- : { } ;
853
-
854
- let logger ;
855
-
856
- if ( typeof options . verbose === "undefined" ) {
857
- logger = this . logger . log . bind (
895
+ if (
896
+ typeof historyApiFallback . logger === "undefined" &&
897
+ ! historyApiFallback . verbose
898
+ ) {
899
+ historyApiFallback . logger = this . logger . log . bind (
858
900
this . logger ,
859
901
"[connect-history-api-fallback]"
860
902
) ;
861
903
}
862
904
863
905
// Fall back to /index.html if nothing else matches.
864
- this . app . use ( historyApiFallback ( { logger , ... options } ) ) ;
906
+ this . app . use ( require ( "connect-history-api-fallback" ) ( historyApiFallback ) ) ;
865
907
}
866
908
867
909
setupStaticFeature ( ) {
@@ -911,23 +953,12 @@ class Server {
911
953
}
912
954
913
955
setupWatchFiles ( ) {
914
- if ( this . options . watchFiles ) {
915
- const { watchFiles } = this . options ;
916
-
917
- if ( typeof watchFiles === "string" ) {
918
- this . watchFiles ( watchFiles , { } ) ;
919
- } else if ( Array . isArray ( watchFiles ) ) {
920
- watchFiles . forEach ( ( file ) => {
921
- if ( typeof file === "string" ) {
922
- this . watchFiles ( file , { } ) ;
923
- } else {
924
- this . watchFiles ( file . paths , file . options || { } ) ;
925
- }
926
- } ) ;
927
- } else {
928
- // { paths: [...], options: {} }
929
- this . watchFiles ( watchFiles . paths , watchFiles . options || { } ) ;
930
- }
956
+ const { watchFiles } = this . options ;
957
+
958
+ if ( watchFiles . length > 0 ) {
959
+ watchFiles . forEach ( ( item ) => {
960
+ this . watchFiles ( item . paths , item . options ) ;
961
+ } ) ;
931
962
}
932
963
}
933
964
@@ -940,7 +971,7 @@ class Server {
940
971
}
941
972
942
973
setupHeadersFeature ( ) {
943
- this . app . all ( "*" , this . setContentHeaders . bind ( this ) ) ;
974
+ this . app . all ( "*" , this . setHeaders . bind ( this ) ) ;
944
975
}
945
976
946
977
setupMagicHtmlFeature ( ) {
@@ -1555,12 +1586,14 @@ class Server {
1555
1586
return statsObj . toJson ( stats ) ;
1556
1587
}
1557
1588
1558
- setContentHeaders ( req , res , next ) {
1589
+ setHeaders ( req , res , next ) {
1559
1590
let { headers } = this . options ;
1591
+
1560
1592
if ( headers ) {
1561
1593
if ( typeof headers === "function" ) {
1562
1594
headers = headers ( req , res , this . middleware . context ) ;
1563
1595
}
1596
+
1564
1597
// eslint-disable-next-line guard-for-in
1565
1598
for ( const name in headers ) {
1566
1599
res . setHeader ( name , headers [ name ] ) ;
@@ -1623,11 +1656,11 @@ class Server {
1623
1656
return true ;
1624
1657
}
1625
1658
1626
- const allowedHosts = this . options . allowedHosts ;
1659
+ const { allowedHosts } = this . options ;
1627
1660
1628
1661
// always allow localhost host, for convenience
1629
1662
// allow if hostname is in allowedHosts
1630
- if ( Array . isArray ( allowedHosts ) && allowedHosts . length ) {
1663
+ if ( Array . isArray ( allowedHosts ) && allowedHosts . length > 0 ) {
1631
1664
for ( let hostIdx = 0 ; hostIdx < allowedHosts . length ; hostIdx ++ ) {
1632
1665
const allowedHost = allowedHosts [ hostIdx ] ;
1633
1666
0 commit comments