-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: move modules out of folders * chore: add eventsource * feat: implement sse module * feat: register for newly created accounts * fix: downgrade eventsource * chore: upgrade eventsource * test: add sse server to local backbone * test: add baseUrlOverride to the SSEModule config * chore: use better logging solution * chore: add logs * test: sse tests * chore: node-logger > simple-logger * fix: get rid of all errors logged during testing * chore: add test logs * ci: archive backbone logs * fix: higher timeout * chore: bump * chore: debug * chore: remove old config * chore: logs * ci: only test sse * ci: log correlation id * fix: test name * chore: remove logs * chore: disable sse test * ci: use 6.30.0 again * chore: do not save backbone logs * chore: undo printing correlation id * chore: backbone loglevel --------- Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
03fbd92
commit 4eb70ec
Showing
27 changed files
with
294 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...time/src/modules/appSync/AppSyncModule.ts → .../app-runtime/src/modules/AppSyncModule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { ILogger } from "@js-soft/logging-abstractions"; | ||
import { ModuleConfiguration } from "@nmshd/runtime"; | ||
import { EventSource } from "eventsource"; | ||
import { AppRuntime } from "../AppRuntime"; | ||
import { AccountSelectedEvent } from "../events"; | ||
import { LocalAccountSession } from "../multiAccount"; | ||
import { AppRuntimeModule } from "./AppRuntimeModule"; | ||
|
||
export interface SSEModuleConfiguration extends ModuleConfiguration { | ||
baseUrlOverride?: string; | ||
} | ||
|
||
export class SSEModule extends AppRuntimeModule<SSEModuleConfiguration> { | ||
private eventSource: Record<string, EventSource | undefined> = {}; | ||
|
||
public constructor(runtime: AppRuntime, configuration: SSEModuleConfiguration, logger: ILogger) { | ||
super(runtime, configuration, logger); | ||
} | ||
|
||
public init(): void { | ||
// Nothing to do here | ||
} | ||
|
||
public async start(): Promise<void> { | ||
for (const session of this.runtime.getSessions()) { | ||
await this.runSync(session); | ||
await this.recreateEventSource(session); | ||
} | ||
|
||
this.subscribeToEvent(AccountSelectedEvent, this.handleAccountSelected.bind(this)); | ||
} | ||
|
||
private async handleAccountSelected(event: AccountSelectedEvent) { | ||
const session = await this.runtime.getOrCreateSession(event.eventTargetAddress); | ||
|
||
if (this.eventSource[session.account.id]) return; | ||
|
||
await this.runSync(session); | ||
await this.recreateEventSource(session); | ||
} | ||
|
||
private async recreateEventSource(session: LocalAccountSession): Promise<void> { | ||
const existingEventSource = this.eventSource[session.account.id]; | ||
if (existingEventSource) { | ||
try { | ||
existingEventSource.close(); | ||
} catch (error) { | ||
this.logger.error("Failed to close event source", error); | ||
} | ||
} | ||
|
||
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 eventSource = new EventSource(sseUrl, { | ||
fetch: async (url, options) => { | ||
const token = await session.accountController.authenticator.getToken(); | ||
|
||
const result = await fetch(url, { | ||
...options, | ||
headers: { ...options?.headers, authorization: `Bearer ${token}` } | ||
}); | ||
|
||
this.logger.info(`SSE fetch result: ${result.status}`); | ||
|
||
return result; | ||
} | ||
}); | ||
|
||
this.eventSource[session.account.id] = eventSource; | ||
|
||
eventSource.addEventListener("ExternalEventCreated", async () => await this.runSync(session)); | ||
|
||
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.onerror = async (error) => { | ||
if (error.code === 401) await this.recreateEventSource(session); | ||
}; | ||
} | ||
|
||
private async runSync(session: LocalAccountSession): Promise<void> { | ||
const syncResult = await session.transportServices.account.syncEverything(); | ||
if (syncResult.isError) { | ||
this.logger.error(syncResult); | ||
} | ||
} | ||
|
||
public stop(): void { | ||
for (const eventsource of Object.values(this.eventSource).filter((eventsource) => typeof eventsource !== "undefined")) { | ||
eventsource.close(); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.