Skip to content

Commit a0b9c69

Browse files
committed
Fix bugs.
1 parent 9e0df98 commit a0b9c69

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

src/common/providers/https.ts

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

750+
const heartbeatSeconds =
751+
options.heartbeatSeconds === undefined
752+
? DEFAULT_HEARTBEAT_SECONDS
753+
: options.heartbeatSeconds;
754+
750755
const clearScheduledHeartbeat = () => {
751756
if (heartbeatInterval) {
752757
clearTimeout(heartbeatInterval);
753758
heartbeatInterval = null;
754759
}
755760
};
756761

757-
const scheduleHeartbeat = (heartbeatSeconds: number) => {
758-
clearScheduledHeartbeat(); // Clear any existing timeout
762+
const scheduleHeartbeat = () => {
763+
clearScheduledHeartbeat();
759764
if (!abortController.signal.aborted) {
760765
heartbeatInterval = setTimeout(() => {
761766
if (!abortController.signal.aborted) {
762767
res.write(": ping\n");
763-
scheduleHeartbeat(heartbeatSeconds);
768+
scheduleHeartbeat();
764769
}
765770
}, heartbeatSeconds * 1000);
766771
}
767772
};
768773

769-
req.on("close", () => {
774+
res.on("close", () => {
770775
clearScheduledHeartbeat();
771776
abortController.abort();
772777
});
@@ -841,27 +846,24 @@ function wrapOnCallHandler<Req = any, Res = any>(
841846
data,
842847
};
843848

844-
const heartbeatSeconds =
845-
options.heartbeatSeconds === undefined
846-
? DEFAULT_HEARTBEAT_SECONDS
847-
: options.heartbeatSeconds;
848-
849849
const responseProxy: CallableProxyResponse = {
850850
write(chunk): boolean {
851851
// if client doesn't accept sse-protocol, response.write() is no-op.
852852
if (!acceptsStreaming) {
853853
return false;
854854
}
855+
855856
// if connection is already closed, response.write() is no-op.
856857
if (abortController.signal.aborted) {
857858
return false;
858859
}
860+
859861
const formattedData = encodeSSE({ message: chunk });
860862
const wrote = res.write(formattedData);
861-
//
863+
862864
// Reset heartbeat timer after successful write
863865
if (wrote && heartbeatInterval !== null && heartbeatSeconds > 0) {
864-
scheduleHeartbeat(heartbeatSeconds);
866+
scheduleHeartbeat();
865867
}
866868
return wrote;
867869
},
@@ -873,7 +875,7 @@ function wrapOnCallHandler<Req = any, Res = any>(
873875
res.status(200);
874876

875877
if (heartbeatSeconds !== null && heartbeatSeconds > 0) {
876-
scheduleHeartbeat(heartbeatSeconds);
878+
scheduleHeartbeat();
877879
}
878880
}
879881
// For some reason the type system isn't picking up that the handler

src/v2/providers/https.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ export function onCall<T = any, Return = any | Promise<any>>(
395395
cors: { origin, methods: "POST" },
396396
enforceAppCheck: opts.enforceAppCheck ?? options.getGlobalOptions().enforceAppCheck,
397397
consumeAppCheckToken: opts.consumeAppCheckToken,
398+
heartbeatSeconds: opts.heartbeatSeconds,
398399
},
399400
fixedLen,
400401
"gcfv2"

0 commit comments

Comments
 (0)