From 18624306f5c60e7206d4b90c5b5ea7f938cf5b4e Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Mon, 30 Sep 2024 14:44:55 -0700 Subject: [PATCH 1/3] Remove `boundListener` --- src/index.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index ad68845..082749a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -76,13 +76,9 @@ export class Server extends net.Server { requestListener = listener!; } - // We bind the request listener, so 'this' always refers to us, not each subserver. - // This means 'this' is consistent (and this.close() works). - const boundListener = requestListener.bind(this); - // Create subservers for each supported protocol: - this._httpServer = new http.Server(boundListener); - this._http2Server = http2.createServer({}, boundListener as any as Http2Listener); + this._httpServer = new http.Server(requestListener); + this._http2Server = http2.createServer({}, requestListener); if (tlsServer) { // If we've been given a preconfigured TLS server, we use that directly, and From 56c85bec1be23921eb67729348cfa6c6401c2572 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Tue, 1 Oct 2024 12:41:15 -0700 Subject: [PATCH 2/3] Use `Function.prototype.bind` --- src/index.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/index.ts b/src/index.ts index 082749a..0a7e7d2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -76,9 +76,15 @@ export class Server extends net.Server { requestListener = listener!; } + // We bind the request listener, so 'this' always refers to us, not each subserver. + // This means 'this' is consistent (and this.close() works). + // Use `Function.prototype.bind` directly as frameworks like Express generate + // methods from `http.METHODS`, and `BIND` is an included HTTP method. + const boundListener = Function.prototype.bind.call(requestListener, this); + // Create subservers for each supported protocol: - this._httpServer = new http.Server(requestListener); - this._http2Server = http2.createServer({}, requestListener); + this._httpServer = new http.Server(boundListener); + this._http2Server = http2.createServer({}, boundListener); if (tlsServer) { // If we've been given a preconfigured TLS server, we use that directly, and From c2fc6996a326fe06882f64878b0f5b61a180f139 Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Tue, 1 Oct 2024 12:41:57 -0700 Subject: [PATCH 3/3] Avoid type change --- src/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index 0a7e7d2..0c261fe 100644 --- a/src/index.ts +++ b/src/index.ts @@ -84,7 +84,7 @@ export class Server extends net.Server { // Create subservers for each supported protocol: this._httpServer = new http.Server(boundListener); - this._http2Server = http2.createServer({}, boundListener); + this._http2Server = http2.createServer({}, boundListener as any as Http2Listener); if (tlsServer) { // If we've been given a preconfigured TLS server, we use that directly, and