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 bug where sse response hangs if handler threw an error. #1668

Merged
merged 5 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
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: 31 additions & 14 deletions spec/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@
// 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());
Expand All @@ -71,29 +72,45 @@
}

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"

Check failure on line 81 in spec/helper.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Replace `⏎············?·toSend⏎···········` with `·?·toSend`
? toSend
: this.sentBody + String(toSend || "");
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) {
Expand Down
3 changes: 2 additions & 1 deletion src/common/providers/https.ts
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,8 @@ function wrapOnCallHandler<Req = any, Res = any, Stream = unknown>(
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);
}
Expand Down
Loading