From 2f7601361ba3378536167f2b1b1c5eae62b4a815 Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Fri, 24 Jan 2025 14:51:35 -0800 Subject: [PATCH 1/3] Fix bug where sse response hangs if handler threw an error. --- spec/helper.ts | 42 +++++++++++++++++++++++------------ src/common/providers/https.ts | 3 ++- 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/spec/helper.ts b/spec/helper.ts index 04829be38..9ce0c9f74 100644 --- a/spec/helper.ts +++ b/spec/helper.ts @@ -47,10 +47,11 @@ export function runHandler( // MockResponse mocks an express.Response. // This class lives here so it can reference resolve and reject. class MockResponse { - private sentBody = ""; + private sentBody: string | undefined; private statusCode = 0; private headers: { [name: string]: string } = {}; private callback: () => void; + private writeCalled = false; constructor() { request.on("close", () => this.end()); @@ -71,29 +72,42 @@ export function runHandler( } public send(sendBody: any) { - const toSend = typeof sendBody === "object" ? JSON.stringify(sendBody) : sendBody; - const body = this.sentBody ? this.sentBody + ((toSend as string) || "") : toSend; - - resolve({ - status: this.statusCode, - headers: this.headers, - body, - }); - if (this.callback) { - this.callback(); + if (this.writeCalled) { + throw Error("Cannot set headers after they are sent to the client") } + + const toSend = typeof sendBody === "object" ? JSON.stringify(sendBody) : sendBody; + const body = typeof this.sentBody === 'undefined' ? toSend : this.sentBody + ((toSend as string) || ""); + this.end(body); } public write(writeBody: any, cb?: () => void) { - this.sentBody += typeof writeBody === "object" ? JSON.stringify(writeBody) : writeBody; + this.writeCalled = true; + + if (typeof this.sentBody === 'undefined') { + this.sentBody = writeBody; + } else { + this.sentBody += typeof writeBody === "object" ? JSON.stringify(writeBody) : writeBody; + } if (cb) { setImmediate(cb); } return true; } - public end() { - this.send(undefined); + public end(body?: unknown) { + if (body) { + this.write(body); + } + resolve({ + status: this.statusCode, + headers: this.headers, + body: this.sentBody, + }); + + if (this.callback) { + this.callback(); + } } public on(event: string, callback: () => void) { diff --git a/src/common/providers/https.ts b/src/common/providers/https.ts index c12278928..6ca37f69a 100644 --- a/src/common/providers/https.ts +++ b/src/common/providers/https.ts @@ -933,7 +933,8 @@ function wrapOnCallHandler( const { status } = httpErr.httpErrorCode; const body = { error: httpErr.toJSON() }; if (version === "gcfv2" && req.header("accept") === "text/event-stream") { - res.send(encodeSSE(body)); + res.write(encodeSSE(body)); + res.end(); } else { res.status(status).send(body); } From 7de0cc5669d50a2f48e3d43bac3ce8eb16f37bf6 Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Sat, 25 Jan 2025 11:16:43 -0800 Subject: [PATCH 2/3] Add formatter. --- spec/helper.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/spec/helper.ts b/spec/helper.ts index 9ce0c9f74..d611e2e9a 100644 --- a/spec/helper.ts +++ b/spec/helper.ts @@ -73,18 +73,21 @@ export function runHandler( public send(sendBody: any) { if (this.writeCalled) { - throw Error("Cannot set headers after they are sent to the client") + throw Error("Cannot set headers after they are sent to the client"); } const toSend = typeof sendBody === "object" ? JSON.stringify(sendBody) : sendBody; - const body = typeof this.sentBody === 'undefined' ? toSend : this.sentBody + ((toSend as string) || ""); + const body = + typeof this.sentBody === "undefined" + ? toSend + : this.sentBody + ((toSend as string) || ""); this.end(body); } public write(writeBody: any, cb?: () => void) { this.writeCalled = true; - if (typeof this.sentBody === 'undefined') { + if (typeof this.sentBody === "undefined") { this.sentBody = writeBody; } else { this.sentBody += typeof writeBody === "object" ? JSON.stringify(writeBody) : writeBody; From e44fa6abe1ae36826310dbb4165b5597685be251 Mon Sep 17 00:00:00 2001 From: Daniel Young Lee Date: Tue, 28 Jan 2025 07:43:35 -0800 Subject: [PATCH 3/3] Better typing. --- spec/helper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/helper.ts b/spec/helper.ts index d611e2e9a..bc1760f0e 100644 --- a/spec/helper.ts +++ b/spec/helper.ts @@ -80,7 +80,7 @@ export function runHandler( const body = typeof this.sentBody === "undefined" ? toSend - : this.sentBody + ((toSend as string) || ""); + : this.sentBody + String(toSend || ""); this.end(body); }