Skip to content

Commit

Permalink
fix(typings): allow to bind to a non-secure Http2Server (#4853)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0a0d authored Nov 22, 2023
1 parent efb5c21 commit 8c9ebc3
Showing 1 changed file with 16 additions and 29 deletions.
45 changes: 16 additions & 29 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import http = require("http");
import type { Server as HTTPSServer } from "https";
import type { Http2SecureServer } from "http2";
import type { Http2SecureServer, Http2Server } from "http2";
import { createReadStream } from "fs";
import { createDeflate, createGzip, createBrotliCompress } from "zlib";
import accepts = require("accepts");
Expand Down Expand Up @@ -56,6 +56,12 @@ type ParentNspNameMatchFn = (

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

type TServerInstance =
| http.Server
| HTTPSServer
| Http2SecureServer
| Http2Server;

interface ServerOptions extends EngineOptions, AttachOptions {
/**
* name of the path to capture
Expand Down Expand Up @@ -203,7 +209,7 @@ export class Server<
* @private
*/
_connectTimeout: number;
private httpServer: http.Server | HTTPSServer | Http2SecureServer;
private httpServer: TServerInstance;
private _corsMiddleware: (
req: http.IncomingMessage,
res: http.ServerResponse,
Expand All @@ -217,28 +223,13 @@ export class Server<
* @param [opts]
*/
constructor(opts?: Partial<ServerOptions>);
constructor(srv?: TServerInstance | number, opts?: Partial<ServerOptions>);
constructor(
srv?: http.Server | HTTPSServer | Http2SecureServer | number,
opts?: Partial<ServerOptions>
);
constructor(
srv:
| undefined
| Partial<ServerOptions>
| http.Server
| HTTPSServer
| Http2SecureServer
| number,
srv: undefined | Partial<ServerOptions> | TServerInstance | number,
opts?: Partial<ServerOptions>
);
constructor(
srv:
| undefined
| Partial<ServerOptions>
| http.Server
| HTTPSServer
| Http2SecureServer
| number,
srv: undefined | Partial<ServerOptions> | TServerInstance | number,
opts: Partial<ServerOptions> = {}
) {
super();
Expand Down Expand Up @@ -271,9 +262,7 @@ export class Server<
opts.cleanupEmptyChildNamespaces = !!opts.cleanupEmptyChildNamespaces;
this.sockets = this.of("/");
if (srv || typeof srv == "number")
this.attach(
srv as http.Server | HTTPSServer | Http2SecureServer | number
);
this.attach(srv as TServerInstance | number);

if (this.opts.cors) {
this._corsMiddleware = corsMiddleware(this.opts.cors);
Expand Down Expand Up @@ -407,7 +396,7 @@ export class Server<
* @return self
*/
public listen(
srv: http.Server | HTTPSServer | Http2SecureServer | number,
srv: TServerInstance | number,
opts: Partial<ServerOptions> = {}
): this {
return this.attach(srv, opts);
Expand All @@ -421,7 +410,7 @@ export class Server<
* @return self
*/
public attach(
srv: http.Server | HTTPSServer | Http2SecureServer | number,
srv: TServerInstance | number,
opts: Partial<ServerOptions> = {}
): this {
if ("function" == typeof srv) {
Expand Down Expand Up @@ -527,7 +516,7 @@ export class Server<
* @private
*/
private initEngine(
srv: http.Server | HTTPSServer | Http2SecureServer,
srv: TServerInstance,
opts: EngineOptions & AttachOptions
): void {
// initialize engine
Expand All @@ -550,9 +539,7 @@ export class Server<
* @param srv http server
* @private
*/
private attachServe(
srv: http.Server | HTTPSServer | Http2SecureServer
): void {
private attachServe(srv: TServerInstance): void {
debug("attaching client serving req handler");

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

0 comments on commit 8c9ebc3

Please sign in to comment.