Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typings): accept an Http2Server non-secure as http-server instance #4853

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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