@@ -109,8 +109,10 @@ const schema = require("./options.json");
109
109
*/
110
110
111
111
/**
112
+ * @template {BasicApplication} [A=ExpressApplication]
113
+ * @template {BasicServer} [S=import("http").Server]
112
114
* @typedef {Object } ServerConfiguration
113
- * @property {ServerType } [type]
115
+ * @property {ServerType<A, S> } [type]
114
116
* @property {ServerOptions } [options]
115
117
*/
116
118
@@ -202,7 +204,7 @@ const schema = require("./options.json");
202
204
* @typedef {{ name?: string, path?: string, middleware: MiddlewareHandler } | MiddlewareHandler } Middleware
203
205
*/
204
206
205
- /** @typedef {import("net").Server } BasicServer */
207
+ /** @typedef {import("net").Server | import("tls").Server } BasicServer */
206
208
207
209
/**
208
210
* @template {BasicApplication} [A=ExpressApplication]
@@ -220,14 +222,14 @@ const schema = require("./options.json");
220
222
* @property {boolean | Record<string, never> | BonjourOptions } [bonjour]
221
223
* @property {string | string[] | WatchFiles | Array<string | WatchFiles> } [watchFiles]
222
224
* @property {boolean | string | Static | Array<string | Static> } [static]
223
- * @property {ServerType | ServerConfiguration } [server]
225
+ * @property {ServerType<A, S> | ServerConfiguration<A, S> } [server]
224
226
* @property {() => Promise<A> } [app]
225
227
* @property {boolean | "sockjs" | "ws" | string | WebSocketServerConfiguration } [webSocketServer]
226
228
* @property {ProxyConfigArray } [proxy]
227
229
* @property {boolean | string | Open | Array<string | Open> } [open]
228
230
* @property {boolean } [setupExitSignals]
229
231
* @property {boolean | ClientConfiguration } [client]
230
- * @property {Headers | ((req: Request, res: Response, context: DevMiddlewareContext<Request, Response>) => Headers) } [headers]
232
+ * @property {Headers | ((req: Request, res: Response, context: DevMiddlewareContext<Request, Response> | undefined ) => Headers) } [headers]
231
233
* @property {(devServer: Server<A, S>) => void } [onListening]
232
234
* @property {(middlewares: Middleware[], devServer: Server<A, S>) => Middleware[] } [setupMiddlewares]
233
235
*/
@@ -643,11 +645,7 @@ class Server {
643
645
if ( typeof webSocketURL . protocol !== "undefined" ) {
644
646
protocol = webSocketURL . protocol ;
645
647
} else {
646
- protocol =
647
- typeof this . server . key !== "undefined" ||
648
- typeof this . server . pfx !== "undefined"
649
- ? "wss:"
650
- : "ws:" ;
648
+ protocol = this . isTlsServer ? "wss:" : "ws:" ;
651
649
}
652
650
653
651
searchParams . set ( "protocol" , protocol ) ;
@@ -1125,7 +1123,7 @@ class Server {
1125
1123
} ;
1126
1124
} else {
1127
1125
const serverOptions =
1128
- /** @type {ServerConfiguration } */
1126
+ /** @type {ServerConfiguration<A, S> } */
1129
1127
( options . server || { } ) ;
1130
1128
1131
1129
options . server = {
@@ -1953,7 +1951,8 @@ class Server {
1953
1951
} ) ;
1954
1952
1955
1953
const isHTTP2 =
1956
- /** @type {ServerConfiguration } */ ( this . options . server ) . type === "http2" ;
1954
+ /** @type {ServerConfiguration<A, S> } */ ( this . options . server ) . type ===
1955
+ "http2" ;
1957
1956
1958
1957
if ( isHTTP2 ) {
1959
1958
// TODO patch for https://github.com/pillarjs/finalhandler/pull/45, need remove then will be resolved
@@ -2484,9 +2483,10 @@ class Server {
2484
2483
( item ) => item . name === "webpack-dev-middleware" ,
2485
2484
) ;
2486
2485
2486
+ // TODO make it lazy
2487
2487
/** @type {import("webpack-dev-middleware").API<Request, Response> | undefined } */
2488
2488
this . middleware = foundDevMiddleware
2489
- ? /** @type {Middleware } */ ( foundDevMiddleware ) . middleware
2489
+ ? /** @type {any } */ ( foundDevMiddleware ) . middleware
2490
2490
: // eslint-disable-next-line no-undefined
2491
2491
undefined ;
2492
2492
@@ -2520,7 +2520,7 @@ class Server {
2520
2520
*/
2521
2521
async createServer ( ) {
2522
2522
const { type, options } =
2523
- /** @type {ServerConfiguration } */
2523
+ /** @type {ServerConfiguration<A, S> } */
2524
2524
( this . options . server ) ;
2525
2525
2526
2526
if ( typeof type === "function" ) {
@@ -2545,6 +2545,11 @@ class Server {
2545
2545
: serverType . createServer ( options , this . app ) ;
2546
2546
}
2547
2547
2548
+ this . isTlsServer =
2549
+ typeof (
2550
+ /** @type {import("tls").Server } */ ( this . server ) . setSecureContext
2551
+ ) !== "undefined" ;
2552
+
2548
2553
/** @type {S } */
2549
2554
( this . server ) . on (
2550
2555
"connection" ,
@@ -2748,16 +2753,12 @@ class Server {
2748
2753
*/
2749
2754
runBonjour ( ) {
2750
2755
const { Bonjour } = require ( "bonjour-service" ) ;
2756
+ const type = this . isTlsServer ? "https" : "http" ;
2757
+
2751
2758
/**
2752
2759
* @private
2753
2760
* @type {Bonjour | undefined }
2754
2761
*/
2755
- const type =
2756
- typeof this . server . key !== "undefined" ||
2757
- typeof this . server . pfx !== "undefined"
2758
- ? "https"
2759
- : "http" ;
2760
-
2761
2762
this . bonjour = new Bonjour ( ) ;
2762
2763
this . bonjour . publish ( {
2763
2764
// @ts -expect-error
@@ -2852,10 +2853,7 @@ class Server {
2852
2853
if ( this . options . ipc ) {
2853
2854
this . logger . info ( `Project is running at: "${ server . address ( ) } "` ) ;
2854
2855
} else {
2855
- const protocol =
2856
- typeof server . key !== "undefined" || typeof server . pfx !== "undefined"
2857
- ? "https"
2858
- : "http" ;
2856
+ const protocol = this . isTlsServer ? "https" : "http" ;
2859
2857
const { address, port } =
2860
2858
/** @type {import("net").AddressInfo } */
2861
2859
( server . address ( ) ) ;
@@ -3005,11 +3003,7 @@ class Server {
3005
3003
if ( this . options . bonjour ) {
3006
3004
const bonjourProtocol =
3007
3005
/** @type {BonjourOptions } */
3008
- ( this . options . bonjour ) . type ||
3009
- typeof server . key !== "undefined" ||
3010
- typeof server . pfx !== "undefined"
3011
- ? "https"
3012
- : "http" ;
3006
+ ( this . options . bonjour ) . type || this . isTlsServer ? "https" : "http" ;
3013
3007
3014
3008
this . logger . info (
3015
3009
`Broadcasting "${ bonjourProtocol } " with subtype of "webpack" via ZeroConf DNS (Bonjour)` ,
@@ -3031,7 +3025,8 @@ class Server {
3031
3025
headers = headers (
3032
3026
req ,
3033
3027
res ,
3034
- this . middleware ? this . middleware . context : { } ,
3028
+ // eslint-disable-next-line no-undefined
3029
+ this . middleware ? this . middleware . context : undefined ,
3035
3030
) ;
3036
3031
}
3037
3032
0 commit comments