Skip to content

Commit e1b6833

Browse files
committed
Finishing touches.
1 parent 003002f commit e1b6833

File tree

1 file changed

+19
-18
lines changed

1 file changed

+19
-18
lines changed

src/common/providers/https.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -747,16 +747,15 @@ function wrapOnCallHandler<Req = any, Res = any>(
747747
const abortController = new AbortController();
748748
let heartbeatInterval: NodeJS.Timeout | null = null;
749749

750-
const cleanup = () => {
750+
const clearHeartbeatInterval = () => {
751751
if (heartbeatInterval) {
752752
clearInterval(heartbeatInterval);
753753
heartbeatInterval = null;
754754
}
755-
req.removeAllListeners('close');
756-
};
755+
}
757756

758757
req.on('close', () => {
759-
cleanup()
758+
clearHeartbeatInterval();
760759
abortController.abort();
761760
});
762761

@@ -859,24 +858,24 @@ function wrapOnCallHandler<Req = any, Res = any>(
859858
// For some reason the type system isn't picking up that the handler
860859
// is a one argument function.
861860
result = await (handler as any)(arg, responseProxy);
862-
863-
if (heartbeatInterval) {
864-
clearInterval(heartbeatInterval);
865-
heartbeatInterval = null;
866-
}
861+
clearHeartbeatInterval();
867862
}
868863

869-
// Encode the result as JSON to preserve types like Dates.
870-
result = encode(result);
864+
if (!abortController.signal.aborted) {
865+
// Encode the result as JSON to preserve types like Dates.
866+
result = encode(result);
871867

872-
// If there was some result, encode it in the body.
873-
const responseBody: HttpResponseBody = { result };
868+
// If there was some result, encode it in the body.
869+
const responseBody: HttpResponseBody = { result };
874870

875-
if (acceptsStreaming) {
876-
res.write(encodeSSE(responseBody));
877-
res.end();
871+
if (acceptsStreaming) {
872+
res.write(encodeSSE(responseBody));
873+
res.end();
874+
} else {
875+
res.status(200).send(responseBody);
876+
}
878877
} else {
879-
res.status(200).send(responseBody);
878+
res.end();
880879
}
881880
} catch (err) {
882881
if (!abortController.signal.aborted) {
@@ -894,9 +893,11 @@ function wrapOnCallHandler<Req = any, Res = any>(
894893
} else {
895894
res.status(status).send(body);
896895
}
896+
} else {
897+
res.end();
897898
}
898899
} finally {
899-
cleanup();
900+
clearHeartbeatInterval();
900901
}
901902
};
902903
}

0 commit comments

Comments
 (0)