Skip to content

Commit fa56eb1

Browse files
refactor: tyes
1 parent 792c066 commit fa56eb1

File tree

2 files changed

+59
-66
lines changed

2 files changed

+59
-66
lines changed

lib/Server.js

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,10 @@ const schema = require("./options.json");
109109
*/
110110

111111
/**
112+
* @template {BasicApplication} [A=ExpressApplication]
113+
* @template {BasicServer} [S=import("http").Server]
112114
* @typedef {Object} ServerConfiguration
113-
* @property {ServerType} [type]
115+
* @property {ServerType<A, S>} [type]
114116
* @property {ServerOptions} [options]
115117
*/
116118

@@ -202,7 +204,7 @@ const schema = require("./options.json");
202204
* @typedef {{ name?: string, path?: string, middleware: MiddlewareHandler } | MiddlewareHandler } Middleware
203205
*/
204206

205-
/** @typedef {import("net").Server} BasicServer */
207+
/** @typedef {import("net").Server | import("tls").Server} BasicServer */
206208

207209
/**
208210
* @template {BasicApplication} [A=ExpressApplication]
@@ -220,14 +222,14 @@ const schema = require("./options.json");
220222
* @property {boolean | Record<string, never> | BonjourOptions} [bonjour]
221223
* @property {string | string[] | WatchFiles | Array<string | WatchFiles>} [watchFiles]
222224
* @property {boolean | string | Static | Array<string | Static>} [static]
223-
* @property {ServerType | ServerConfiguration} [server]
225+
* @property {ServerType<A, S> | ServerConfiguration<A, S>} [server]
224226
* @property {() => Promise<A>} [app]
225227
* @property {boolean | "sockjs" | "ws" | string | WebSocketServerConfiguration} [webSocketServer]
226228
* @property {ProxyConfigArray} [proxy]
227229
* @property {boolean | string | Open | Array<string | Open>} [open]
228230
* @property {boolean} [setupExitSignals]
229231
* @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]
231233
* @property {(devServer: Server<A, S>) => void} [onListening]
232234
* @property {(middlewares: Middleware[], devServer: Server<A, S>) => Middleware[]} [setupMiddlewares]
233235
*/
@@ -643,11 +645,7 @@ class Server {
643645
if (typeof webSocketURL.protocol !== "undefined") {
644646
protocol = webSocketURL.protocol;
645647
} 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:";
651649
}
652650

653651
searchParams.set("protocol", protocol);
@@ -1125,7 +1123,7 @@ class Server {
11251123
};
11261124
} else {
11271125
const serverOptions =
1128-
/** @type {ServerConfiguration} */
1126+
/** @type {ServerConfiguration<A, S>} */
11291127
(options.server || {});
11301128

11311129
options.server = {
@@ -1953,7 +1951,8 @@ class Server {
19531951
});
19541952

19551953
const isHTTP2 =
1956-
/** @type {ServerConfiguration} */ (this.options.server).type === "http2";
1954+
/** @type {ServerConfiguration<A, S>} */ (this.options.server).type ===
1955+
"http2";
19571956

19581957
if (isHTTP2) {
19591958
// TODO patch for https://github.com/pillarjs/finalhandler/pull/45, need remove then will be resolved
@@ -2484,9 +2483,10 @@ class Server {
24842483
(item) => item.name === "webpack-dev-middleware",
24852484
);
24862485

2486+
// TODO make it lazy
24872487
/** @type {import("webpack-dev-middleware").API<Request, Response> | undefined} */
24882488
this.middleware = foundDevMiddleware
2489-
? /** @type {Middleware} */ (foundDevMiddleware).middleware
2489+
? /** @type {any} */ (foundDevMiddleware).middleware
24902490
: // eslint-disable-next-line no-undefined
24912491
undefined;
24922492

@@ -2520,7 +2520,7 @@ class Server {
25202520
*/
25212521
async createServer() {
25222522
const { type, options } =
2523-
/** @type {ServerConfiguration} */
2523+
/** @type {ServerConfiguration<A, S>} */
25242524
(this.options.server);
25252525

25262526
if (typeof type === "function") {
@@ -2545,6 +2545,11 @@ class Server {
25452545
: serverType.createServer(options, this.app);
25462546
}
25472547

2548+
this.isTlsServer =
2549+
typeof (
2550+
/** @type {import("tls").Server} */ (this.server).setSecureContext
2551+
) !== "undefined";
2552+
25482553
/** @type {S} */
25492554
(this.server).on(
25502555
"connection",
@@ -2748,16 +2753,12 @@ class Server {
27482753
*/
27492754
runBonjour() {
27502755
const { Bonjour } = require("bonjour-service");
2756+
const type = this.isTlsServer ? "https" : "http";
2757+
27512758
/**
27522759
* @private
27532760
* @type {Bonjour | undefined}
27542761
*/
2755-
const type =
2756-
typeof this.server.key !== "undefined" ||
2757-
typeof this.server.pfx !== "undefined"
2758-
? "https"
2759-
: "http";
2760-
27612762
this.bonjour = new Bonjour();
27622763
this.bonjour.publish({
27632764
// @ts-expect-error
@@ -2852,10 +2853,7 @@ class Server {
28522853
if (this.options.ipc) {
28532854
this.logger.info(`Project is running at: "${server.address()}"`);
28542855
} else {
2855-
const protocol =
2856-
typeof server.key !== "undefined" || typeof server.pfx !== "undefined"
2857-
? "https"
2858-
: "http";
2856+
const protocol = this.isTlsServer ? "https" : "http";
28592857
const { address, port } =
28602858
/** @type {import("net").AddressInfo} */
28612859
(server.address());
@@ -3005,11 +3003,7 @@ class Server {
30053003
if (this.options.bonjour) {
30063004
const bonjourProtocol =
30073005
/** @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";
30133007

30143008
this.logger.info(
30153009
`Broadcasting "${bonjourProtocol}" with subtype of "webpack" via ZeroConf DNS (Bonjour)`,
@@ -3031,7 +3025,8 @@ class Server {
30313025
headers = headers(
30323026
req,
30333027
res,
3034-
this.middleware ? this.middleware.context : {},
3028+
// eslint-disable-next-line no-undefined
3029+
this.middleware ? this.middleware.context : undefined,
30353030
);
30363031
}
30373032

types/lib/Server.d.ts

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,9 @@ declare class Server<
602602
ServerType: {
603603
enum: string[];
604604
};
605+
ServerFn: {
606+
instanceof: string;
607+
};
605608
ServerEnum: {
606609
enum: string[];
607610
cli: {
@@ -1260,34 +1263,6 @@ declare class Server<
12601263
* @type {Stats | MultiStats}
12611264
*/
12621265
private stats;
1263-
/**
1264-
* @private
1265-
* @returns {void}
1266-
*/
1267-
private setupHostHeaderCheck;
1268-
/**
1269-
* @private
1270-
* @returns {void}
1271-
*/
1272-
private setupDevMiddleware;
1273-
middleware:
1274-
| import("webpack-dev-middleware").API<
1275-
import("express").Request<
1276-
import("express-serve-static-core").ParamsDictionary,
1277-
any,
1278-
any,
1279-
qs.ParsedQs,
1280-
Record<string, any>
1281-
>,
1282-
import("express").Response<any, Record<string, any>>
1283-
>
1284-
| null
1285-
| undefined;
1286-
/**
1287-
* @private
1288-
* @returns {void}
1289-
*/
1290-
private setupBuiltInRoutes;
12911266
/**
12921267
* @private
12931268
* @returns {void}
@@ -1303,13 +1278,18 @@ declare class Server<
13031278
* @returns {void}
13041279
*/
13051280
private setupMiddlewares;
1281+
/** @type {import("webpack-dev-middleware").API<Request, Response> | undefined} */
1282+
middleware:
1283+
| import("webpack-dev-middleware").API<Request, Response>
1284+
| undefined;
13061285
/**
13071286
* @private
1308-
* @returns {void}
1287+
* @returns {Promise<void>}
13091288
*/
13101289
private createServer;
1311-
/** @type {S | null | undefined}*/
1312-
server: S | null | undefined;
1290+
/** @type {S | undefined}*/
1291+
server: S | undefined;
1292+
isTlsServer: boolean | undefined;
13131293
/**
13141294
* @private
13151295
* @returns {void}
@@ -1589,9 +1569,27 @@ type NormalizedStatic = {
15891569
staticOptions: ServeStaticOptions;
15901570
watch: false | WatchOptions;
15911571
};
1592-
type ServerType = "http" | "https" | "spdy" | "http2" | string;
1593-
type ServerConfiguration = {
1594-
type?: string | undefined;
1572+
type ServerType<
1573+
A extends BasicApplication = import("express").Application,
1574+
S extends BasicServer = import("http").Server<
1575+
typeof import("http").IncomingMessage,
1576+
typeof import("http").ServerResponse
1577+
>,
1578+
> =
1579+
| "http"
1580+
| "https"
1581+
| "spdy"
1582+
| "http2"
1583+
| string
1584+
| ((arg0: ServerOptions, arg1: A) => S);
1585+
type ServerConfiguration<
1586+
A extends BasicApplication = import("express").Application,
1587+
S extends BasicServer = import("http").Server<
1588+
typeof import("http").IncomingMessage,
1589+
typeof import("http").ServerResponse
1590+
>,
1591+
> = {
1592+
type?: ServerType<A, S> | undefined;
15951593
options?: ServerOptions | undefined;
15961594
};
15971595
type WebSocketServerConfiguration = {
@@ -1690,7 +1688,7 @@ type Middleware =
16901688
middleware: MiddlewareHandler;
16911689
}
16921690
| MiddlewareHandler;
1693-
type BasicServer = import("net").Server;
1691+
type BasicServer = import("net").Server | import("tls").Server;
16941692
type Configuration<
16951693
A extends BasicApplication = import("express").Application,
16961694
S extends BasicServer = import("http").Server<
@@ -1733,7 +1731,7 @@ type Configuration<
17331731
| (string | WatchFiles)[]
17341732
| undefined;
17351733
static?: string | boolean | Static | (string | Static)[] | undefined;
1736-
server?: string | ServerConfiguration | undefined;
1734+
server?: ServerType<A, S> | ServerConfiguration<A, S> | undefined;
17371735
app?: (() => Promise<A>) | undefined;
17381736
webSocketServer?: string | boolean | WebSocketServerConfiguration | undefined;
17391737
proxy?: ProxyConfigArray | undefined;
@@ -1745,7 +1743,7 @@ type Configuration<
17451743
| ((
17461744
req: Request,
17471745
res: Response,
1748-
context: DevMiddlewareContext<Request, Response>,
1746+
context: DevMiddlewareContext<Request, Response> | undefined,
17491747
) => Headers)
17501748
| undefined;
17511749
onListening?: ((devServer: Server<A, S>) => void) | undefined;

0 commit comments

Comments
 (0)