Skip to content

Commit

Permalink
The SSE Module does not sync on reconnect (#358)
Browse files Browse the repository at this point in the history
* fix: log when actually connecting to sse

* fix: sync on reconnect
  • Loading branch information
jkoenig134 authored Jan 31, 2025
1 parent 48e6292 commit d27abb8
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions src/modules/sse/SseModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,23 @@ export default class SseModule extends ConnectorRuntimeModule<SseModuleConfigura
const baseUrl = this.configuration.baseUrlOverride ?? this.runtime["runtimeConfig"].transportLibrary.baseUrl;
const sseUrl = `${baseUrl}/api/v1/sse`;

this.logger.info(`Connecting to SSE endpoint: ${sseUrl}`);

const baseOptions = { connect: { rejectUnauthorized: false } };
const proxy = baseUrl.startsWith("https://") ? (process.env.https_proxy ?? process.env.HTTPS_PROXY) : (process.env.http_proxy ?? process.env.HTTP_PROXY);

const eventSource = new EventSource(sseUrl, {
fetch: async (url, options) => {
const token = await this.runtime.getBackboneAuthenticationToken();

return await fetch(url, {
this.logger.info(`Connecting to SSE endpoint: ${sseUrl}`);
const response = await fetch(url, {
...options,
dispatcher: proxy ? new ProxyAgent({ ...baseOptions, uri: proxy }) : new Agent(baseOptions),
headers: { ...options?.headers, authorization: `Bearer ${token}` }
});

this.logger.info(`Connected to SSE endpoint: ${sseUrl}`);

return response;
}
});

Expand All @@ -63,20 +66,11 @@ export default class SseModule extends ConnectorRuntimeModule<SseModuleConfigura
eventSource.addEventListener("ExternalEventCreated", async () => await this.runSync());

await new Promise<void>((resolve, reject) => {
eventSource.onopen = () => {
this.logger.info("Connected to SSE endpoint");
resolve();

eventSource.onopen = () => {
// noop
};
};

eventSource.onerror = (error) => {
reject(error);
};
eventSource.onopen = () => resolve();
eventSource.onerror = (error) => reject(error);
});

eventSource.onopen = async () => await this.runSync();
eventSource.onerror = async (error) => {
if (error.code === 401) await this.recreateEventSource();
};
Expand Down

0 comments on commit d27abb8

Please sign in to comment.