Skip to content

Commit 8c9ebc3

Browse files
authored
fix(typings): allow to bind to a non-secure Http2Server (#4853)
1 parent efb5c21 commit 8c9ebc3

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

lib/index.ts

+16-29
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import http = require("http");
22
import type { Server as HTTPSServer } from "https";
3-
import type { Http2SecureServer } from "http2";
3+
import type { Http2SecureServer, Http2Server } from "http2";
44
import { createReadStream } from "fs";
55
import { createDeflate, createGzip, createBrotliCompress } from "zlib";
66
import accepts = require("accepts");
@@ -56,6 +56,12 @@ type ParentNspNameMatchFn = (
5656

5757
type AdapterConstructor = typeof Adapter | ((nsp: Namespace) => Adapter);
5858

59+
type TServerInstance =
60+
| http.Server
61+
| HTTPSServer
62+
| Http2SecureServer
63+
| Http2Server;
64+
5965
interface ServerOptions extends EngineOptions, AttachOptions {
6066
/**
6167
* name of the path to capture
@@ -203,7 +209,7 @@ export class Server<
203209
* @private
204210
*/
205211
_connectTimeout: number;
206-
private httpServer: http.Server | HTTPSServer | Http2SecureServer;
212+
private httpServer: TServerInstance;
207213
private _corsMiddleware: (
208214
req: http.IncomingMessage,
209215
res: http.ServerResponse,
@@ -217,28 +223,13 @@ export class Server<
217223
* @param [opts]
218224
*/
219225
constructor(opts?: Partial<ServerOptions>);
226+
constructor(srv?: TServerInstance | number, opts?: Partial<ServerOptions>);
220227
constructor(
221-
srv?: http.Server | HTTPSServer | Http2SecureServer | number,
222-
opts?: Partial<ServerOptions>
223-
);
224-
constructor(
225-
srv:
226-
| undefined
227-
| Partial<ServerOptions>
228-
| http.Server
229-
| HTTPSServer
230-
| Http2SecureServer
231-
| number,
228+
srv: undefined | Partial<ServerOptions> | TServerInstance | number,
232229
opts?: Partial<ServerOptions>
233230
);
234231
constructor(
235-
srv:
236-
| undefined
237-
| Partial<ServerOptions>
238-
| http.Server
239-
| HTTPSServer
240-
| Http2SecureServer
241-
| number,
232+
srv: undefined | Partial<ServerOptions> | TServerInstance | number,
242233
opts: Partial<ServerOptions> = {}
243234
) {
244235
super();
@@ -271,9 +262,7 @@ export class Server<
271262
opts.cleanupEmptyChildNamespaces = !!opts.cleanupEmptyChildNamespaces;
272263
this.sockets = this.of("/");
273264
if (srv || typeof srv == "number")
274-
this.attach(
275-
srv as http.Server | HTTPSServer | Http2SecureServer | number
276-
);
265+
this.attach(srv as TServerInstance | number);
277266

278267
if (this.opts.cors) {
279268
this._corsMiddleware = corsMiddleware(this.opts.cors);
@@ -407,7 +396,7 @@ export class Server<
407396
* @return self
408397
*/
409398
public listen(
410-
srv: http.Server | HTTPSServer | Http2SecureServer | number,
399+
srv: TServerInstance | number,
411400
opts: Partial<ServerOptions> = {}
412401
): this {
413402
return this.attach(srv, opts);
@@ -421,7 +410,7 @@ export class Server<
421410
* @return self
422411
*/
423412
public attach(
424-
srv: http.Server | HTTPSServer | Http2SecureServer | number,
413+
srv: TServerInstance | number,
425414
opts: Partial<ServerOptions> = {}
426415
): this {
427416
if ("function" == typeof srv) {
@@ -527,7 +516,7 @@ export class Server<
527516
* @private
528517
*/
529518
private initEngine(
530-
srv: http.Server | HTTPSServer | Http2SecureServer,
519+
srv: TServerInstance,
531520
opts: EngineOptions & AttachOptions
532521
): void {
533522
// initialize engine
@@ -550,9 +539,7 @@ export class Server<
550539
* @param srv http server
551540
* @private
552541
*/
553-
private attachServe(
554-
srv: http.Server | HTTPSServer | Http2SecureServer
555-
): void {
542+
private attachServe(srv: TServerInstance): void {
556543
debug("attaching client serving req handler");
557544

558545
const evs = srv.listeners("request").slice(0);

0 commit comments

Comments
 (0)