From 58bbd424f3855386c024ba3417e5f71b7b2e9520 Mon Sep 17 00:00:00 2001 From: 0x0a0d Date: Mon, 16 Oct 2023 08:53:09 +0700 Subject: [PATCH 1/3] fix(typings): accept an Http2Server non-secure as http-server instance --- lib/index.ts | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 5bd1d03d9b..5a99b7fae3 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -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"); @@ -55,6 +55,9 @@ type ParentNspNameMatchFn = ( ) => void; type AdapterConstructor = typeof Adapter | ((nsp: Namespace) => Adapter); +type TServerInstance = TBoolAcceptNumber extends true + ? http.Server | HTTPSServer | Http2SecureServer | Http2Server | number + : http.Server | HTTPSServer | Http2SecureServer | Http2Server interface ServerOptions extends EngineOptions, AttachOptions { /** @@ -203,7 +206,7 @@ export class Server< * @private */ _connectTimeout: number; - private httpServer: http.Server | HTTPSServer | Http2SecureServer; + private httpServer: TServerInstance; private _corsMiddleware: ( req: http.IncomingMessage, res: http.ServerResponse, @@ -218,27 +221,21 @@ export class Server< */ constructor(opts?: Partial); constructor( - srv?: http.Server | HTTPSServer | Http2SecureServer | number, + srv?: TServerInstance, opts?: Partial ); constructor( srv: | undefined | Partial - | http.Server - | HTTPSServer - | Http2SecureServer - | number, + | TServerInstance, opts?: Partial ); constructor( srv: | undefined | Partial - | http.Server - | HTTPSServer - | Http2SecureServer - | number, + | TServerInstance, opts: Partial = {} ) { super(); @@ -272,7 +269,7 @@ export class Server< this.sockets = this.of("/"); if (srv || typeof srv == "number") this.attach( - srv as http.Server | HTTPSServer | Http2SecureServer | number + srv as TServerInstance ); if (this.opts.cors) { @@ -407,7 +404,7 @@ export class Server< * @return self */ public listen( - srv: http.Server | HTTPSServer | Http2SecureServer | number, + srv: TServerInstance, opts: Partial = {} ): this { return this.attach(srv, opts); @@ -421,7 +418,7 @@ export class Server< * @return self */ public attach( - srv: http.Server | HTTPSServer | Http2SecureServer | number, + srv: TServerInstance, opts: Partial = {} ): this { if ("function" == typeof srv) { @@ -527,7 +524,7 @@ export class Server< * @private */ private initEngine( - srv: http.Server | HTTPSServer | Http2SecureServer, + srv: TServerInstance, opts: EngineOptions & AttachOptions ): void { // initialize engine @@ -551,7 +548,7 @@ export class Server< * @private */ private attachServe( - srv: http.Server | HTTPSServer | Http2SecureServer + srv: TServerInstance ): void { debug("attaching client serving req handler"); From e52c8eb8f0876b392754e5946b7c7a9d06f1abb0 Mon Sep 17 00:00:00 2001 From: 0x0a0d Date: Wed, 18 Oct 2023 07:08:55 +0700 Subject: [PATCH 2/3] fix(typings): fix prettier --- lib/index.ts | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 5a99b7fae3..208217cdab 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -55,9 +55,10 @@ type ParentNspNameMatchFn = ( ) => void; type AdapterConstructor = typeof Adapter | ((nsp: Namespace) => Adapter); -type TServerInstance = TBoolAcceptNumber extends true - ? http.Server | HTTPSServer | Http2SecureServer | Http2Server | number - : http.Server | HTTPSServer | Http2SecureServer | Http2Server +type TServerInstance = + TBoolAcceptNumber extends true + ? http.Server | HTTPSServer | Http2SecureServer | Http2Server | number + : http.Server | HTTPSServer | Http2SecureServer | Http2Server; interface ServerOptions extends EngineOptions, AttachOptions { /** @@ -220,22 +221,13 @@ export class Server< * @param [opts] */ constructor(opts?: Partial); + constructor(srv?: TServerInstance, opts?: Partial); constructor( - srv?: TServerInstance, + srv: undefined | Partial | TServerInstance, opts?: Partial ); constructor( - srv: - | undefined - | Partial - | TServerInstance, - opts?: Partial - ); - constructor( - srv: - | undefined - | Partial - | TServerInstance, + srv: undefined | Partial | TServerInstance, opts: Partial = {} ) { super(); @@ -268,9 +260,7 @@ export class Server< opts.cleanupEmptyChildNamespaces = !!opts.cleanupEmptyChildNamespaces; this.sockets = this.of("/"); if (srv || typeof srv == "number") - this.attach( - srv as TServerInstance - ); + this.attach(srv as TServerInstance); if (this.opts.cors) { this._corsMiddleware = corsMiddleware(this.opts.cors); @@ -547,9 +537,7 @@ export class Server< * @param srv http server * @private */ - private attachServe( - srv: TServerInstance - ): void { + private attachServe(srv: TServerInstance): void { debug("attaching client serving req handler"); const evs = srv.listeners("request").slice(0); From 426d16b6a89474225d88442830b5619f0ed6aa74 Mon Sep 17 00:00:00 2001 From: Damien Arrachequesne Date: Wed, 22 Nov 2023 17:45:00 +0100 Subject: [PATCH 3/3] nitpick --- lib/index.ts | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/index.ts b/lib/index.ts index 208217cdab..48497e3352 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -55,10 +55,12 @@ type ParentNspNameMatchFn = ( ) => void; type AdapterConstructor = typeof Adapter | ((nsp: Namespace) => Adapter); -type TServerInstance = - TBoolAcceptNumber extends true - ? http.Server | HTTPSServer | Http2SecureServer | Http2Server | number - : http.Server | HTTPSServer | Http2SecureServer | Http2Server; + +type TServerInstance = + | http.Server + | HTTPSServer + | Http2SecureServer + | Http2Server; interface ServerOptions extends EngineOptions, AttachOptions { /** @@ -207,7 +209,7 @@ export class Server< * @private */ _connectTimeout: number; - private httpServer: TServerInstance; + private httpServer: TServerInstance; private _corsMiddleware: ( req: http.IncomingMessage, res: http.ServerResponse, @@ -221,13 +223,13 @@ export class Server< * @param [opts] */ constructor(opts?: Partial); - constructor(srv?: TServerInstance, opts?: Partial); + constructor(srv?: TServerInstance | number, opts?: Partial); constructor( - srv: undefined | Partial | TServerInstance, + srv: undefined | Partial | TServerInstance | number, opts?: Partial ); constructor( - srv: undefined | Partial | TServerInstance, + srv: undefined | Partial | TServerInstance | number, opts: Partial = {} ) { super(); @@ -260,7 +262,7 @@ export class Server< opts.cleanupEmptyChildNamespaces = !!opts.cleanupEmptyChildNamespaces; this.sockets = this.of("/"); if (srv || typeof srv == "number") - this.attach(srv as TServerInstance); + this.attach(srv as TServerInstance | number); if (this.opts.cors) { this._corsMiddleware = corsMiddleware(this.opts.cors); @@ -394,7 +396,7 @@ export class Server< * @return self */ public listen( - srv: TServerInstance, + srv: TServerInstance | number, opts: Partial = {} ): this { return this.attach(srv, opts); @@ -408,7 +410,7 @@ export class Server< * @return self */ public attach( - srv: TServerInstance, + srv: TServerInstance | number, opts: Partial = {} ): this { if ("function" == typeof srv) { @@ -514,7 +516,7 @@ export class Server< * @private */ private initEngine( - srv: TServerInstance, + srv: TServerInstance, opts: EngineOptions & AttachOptions ): void { // initialize engine @@ -537,7 +539,7 @@ export class Server< * @param srv http server * @private */ - private attachServe(srv: TServerInstance): void { + private attachServe(srv: TServerInstance): void { debug("attaching client serving req handler"); const evs = srv.listeners("request").slice(0);