From 514a578260716f8317a893cabab59e4013be076a Mon Sep 17 00:00:00 2001 From: garrettmills Date: Wed, 30 Mar 2022 22:19:33 -0500 Subject: [PATCH] Make HTTPServer ignore responses that cannot be sent --- package.json | 2 +- src/http/lifecycle/Response.ts | 8 ++++++++ src/service/HTTPServer.ts | 4 +++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index f8fd009..74ccb7a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@extollo/lib", - "version": "0.9.14", + "version": "0.9.15", "description": "The framework library that lifts up your code.", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/http/lifecycle/Response.ts b/src/http/lifecycle/Response.ts index 0eadf00..0128020 100644 --- a/src/http/lifecycle/Response.ts +++ b/src/http/lifecycle/Response.ts @@ -255,6 +255,14 @@ export class Response { await this.sent$.next(this) } + /** + * Returns true if the response can still be sent. False if it has been sent + * or the connection has been destroyed. + */ + public canSend(): boolean { + return !(this.responseEnded || this.serverResponse.destroyed) + } + /** * Mark the response as ended and close the socket. */ diff --git a/src/service/HTTPServer.ts b/src/service/HTTPServer.ts index d0d4ce1..3e7e12a 100644 --- a/src/service/HTTPServer.ts +++ b/src/service/HTTPServer.ts @@ -128,7 +128,9 @@ export class HTTPServer extends Unit { await error(new ErrorWithContext('Unknown error occurred.', { e })) } - await extolloReq.response.send() + if ( extolloReq.response.canSend() ) { + await extolloReq.response.send() + } }) } }