-
Notifications
You must be signed in to change notification settings - Fork 0
feat(node-message-broker): resolve analysis participants via server-core #12
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,8 @@ | ||
| /* | ||
| * Copyright (c) 2026. | ||
| * Author Peter Placzek (tada5hi) | ||
| * For the full copyright and license information, | ||
| * view the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| export * from './participant-resolver.ts'; |
109 changes: 109 additions & 0 deletions
109
apps/node-message-broker/src/adapters/core/participant-resolver.ts
This file contains hidden or 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,109 @@ | ||
| /* | ||
| * Copyright (c) 2026. | ||
| * Author Peter Placzek (tada5hi) | ||
| * For the full copyright and license information, | ||
| * view the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| import type { Logger } from '@privateaim/server-kit'; | ||
| import { LRUCache } from 'lru-cache'; | ||
| import type { | ||
| AnalysisParticipant, | ||
| IAnalysisNodeProvider, | ||
| IParticipantResolver, | ||
| } from '../../core/analysis/index.ts'; | ||
|
|
||
| const DEFAULT_CACHE_MAX = 256; | ||
|
|
||
| const DEFAULT_CACHE_TTL_MS = 30_000; | ||
|
|
||
| type ParticipantResolverContext = { | ||
| provider: IAnalysisNodeProvider, | ||
| /** this node's own authup client id (config.clientId) — used by {@link resolveSelf} */ | ||
| selfClientId: string, | ||
| cacheMax?: number, | ||
| /** per-analysis cache lifetime; `<= 0` disables caching. */ | ||
| cacheTtlMs?: number, | ||
| logger?: Logger | ||
| }; | ||
|
|
||
| /** | ||
| * Resolves analysis participants — their node id / type / client id / public key — | ||
| * from server-core's analysis-node API, mapping each included `node` to an | ||
| * {@link AnalysisParticipant}. Results are cached per analysis (short TTL) to keep | ||
| * resolution off the Hub send/deliver hot path; participants missing a client id | ||
| * or public key are skipped, since they can't be addressed or encrypted to. | ||
| */ | ||
| export class ParticipantResolver implements IParticipantResolver { | ||
| protected provider: IAnalysisNodeProvider; | ||
|
|
||
| protected selfClientId: string; | ||
|
|
||
| protected logger: Logger | undefined; | ||
|
|
||
| protected cache: LRUCache<string, Promise<AnalysisParticipant[]>> | undefined; | ||
|
|
||
| constructor(ctx: ParticipantResolverContext) { | ||
| this.provider = ctx.provider; | ||
| this.selfClientId = ctx.selfClientId; | ||
| this.logger = ctx.logger; | ||
|
|
||
| const ttl = ctx.cacheTtlMs ?? DEFAULT_CACHE_TTL_MS; | ||
| if (ttl > 0) { | ||
| this.cache = new LRUCache<string, Promise<AnalysisParticipant[]>>({ | ||
| max: Math.max(1, Math.floor(ctx.cacheMax ?? DEFAULT_CACHE_MAX)), | ||
| ttl, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| resolve(analysisId: string): Promise<AnalysisParticipant[]> { | ||
| const cached = this.cache?.get(analysisId); | ||
| if (cached) { | ||
| return cached; | ||
| } | ||
|
|
||
| const promise = this.fetch(analysisId); | ||
|
|
||
| const { cache } = this; | ||
| if (cache) { | ||
| // evict a rejected fetch so a transient failure isn't cached for the TTL | ||
| promise.catch(() => { | ||
| if (cache.peek(analysisId) === promise) { | ||
| cache.delete(analysisId); | ||
| } | ||
| }); | ||
| cache.set(analysisId, promise); | ||
| } | ||
|
|
||
| return promise; | ||
| } | ||
|
|
||
| async resolveSelf(analysisId: string): Promise<AnalysisParticipant | undefined> { | ||
| const participants = await this.resolve(analysisId); | ||
| return participants.find((participant) => participant.clientId === this.selfClientId); | ||
| } | ||
|
|
||
| protected async fetch(analysisId: string): Promise<AnalysisParticipant[]> { | ||
| const nodes = await this.provider.list(analysisId); | ||
|
|
||
| const participants: AnalysisParticipant[] = []; | ||
| for (const node of nodes) { | ||
| if (!node.client_id || !node.public_key) { | ||
| this.logger?.warn( | ||
| `Analysis ${analysisId}: skipping participant ${node.id} with a missing client id or public key`, | ||
| ); | ||
| continue; | ||
| } | ||
|
|
||
| participants.push({ | ||
| nodeId: node.id, | ||
| nodeType: node.type, | ||
| clientId: node.client_id, | ||
| publicKey: node.public_key, | ||
| }); | ||
| } | ||
|
|
||
| return participants; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
11 changes: 11 additions & 0 deletions
11
apps/node-message-broker/src/app/modules/core-client/constants.ts
This file contains hidden or 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,11 @@ | ||
| /* | ||
| * Copyright (c) 2026. | ||
| * Author Peter Placzek (tada5hi) | ||
| * For the full copyright and license information, | ||
| * view the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| import { TypedToken } from 'eldin'; | ||
| import type { IParticipantResolver } from '../../../core/analysis/index.ts'; | ||
|
|
||
| export const CoreClientInjectionKey = { ParticipantResolver: new TypedToken<IParticipantResolver>('ParticipantResolver') }; |
9 changes: 9 additions & 0 deletions
9
apps/node-message-broker/src/app/modules/core-client/index.ts
This file contains hidden or 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,9 @@ | ||
| /* | ||
| * Copyright (c) 2026. | ||
| * Author Peter Placzek (tada5hi) | ||
| * For the full copyright and license information, | ||
| * view the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| export * from './constants.ts'; | ||
| export * from './module.ts'; |
88 changes: 88 additions & 0 deletions
88
apps/node-message-broker/src/app/modules/core-client/module.ts
This file contains hidden or 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,88 @@ | ||
| /* | ||
| * Copyright (c) 2026. | ||
| * Author Peter Placzek (tada5hi) | ||
| * For the full copyright and license information, | ||
| * view the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| import type { IContainer } from 'eldin'; | ||
| import type { IModule } from 'orkos'; | ||
| import { Client } from '@privateaim/core-http-kit'; | ||
| import { | ||
| LoggerInjectionKey, | ||
| createAuthupClientAuthenticationHook, | ||
| createAuthupClientTokenCreator, | ||
| } from '@privateaim/server-kit'; | ||
| import type { IAnalysisNodeProvider } from '../../../core/analysis/index.ts'; | ||
| import { ParticipantResolver } from '../../../adapters/core/index.ts'; | ||
| import { ConfigInjectionKey } from '../config/constants.ts'; | ||
| import { CoreClientInjectionKey } from './constants.ts'; | ||
|
|
||
| /** | ||
| * Wires the server-core link: a `@privateaim/core-http-kit` client (authenticated | ||
| * as the node client via client credentials, mirroring the Hub link) backing the | ||
| * {@link ParticipantResolver}. Registered for the analysis policy (S3) and the | ||
| * send/deliver flows (S5/S6) to consume. | ||
| */ | ||
| export class CoreClientModule implements IModule { | ||
| readonly name = 'coreClient'; | ||
|
|
||
| readonly dependencies: string[] = ['config']; | ||
|
|
||
| private authHook: ReturnType<typeof createAuthupClientAuthenticationHook> | undefined; | ||
|
|
||
| async setup(container: IContainer): Promise<void> { | ||
| const config = container.resolve(ConfigInjectionKey); | ||
|
|
||
| const loggerResult = container.tryResolve(LoggerInjectionKey); | ||
| const logger = loggerResult.success ? loggerResult.data : undefined; | ||
|
|
||
| const tokenCreator = createAuthupClientTokenCreator({ | ||
| baseURL: config.authupURL, | ||
| clientId: config.clientId, | ||
| clientSecret: config.clientSecret, | ||
| realm: config.realm, | ||
| }); | ||
|
|
||
| const client = new Client({ baseURL: config.coreURL }); | ||
| const authHook = createAuthupClientAuthenticationHook({ | ||
| baseURL: config.authupURL, | ||
| tokenCreator, | ||
| }); | ||
| authHook.attach(client); | ||
| this.authHook = authHook; | ||
|
|
||
| // wrap the core client's analysis-node lookup behind the narrow provider port | ||
| const provider: IAnalysisNodeProvider = { | ||
| list: async (analysisId) => { | ||
| const response = await client.analysisNode.getMany({ | ||
| filter: { analysis_id: analysisId }, | ||
| include: ['node'], | ||
| }); | ||
| // `node` is typed non-null, but server-core may omit the relation | ||
| // (include not honored / incomplete data); drop those so the | ||
| // resolver never dereferences an undefined node. | ||
| return response.data | ||
| .map((entry) => entry.node) | ||
| .filter((node): node is NonNullable<typeof node> => !!node); | ||
| }, | ||
| }; | ||
|
|
||
| const resolver = new ParticipantResolver({ | ||
| provider, | ||
| selfClientId: config.clientId, | ||
| logger, | ||
| }); | ||
| container.register(CoreClientInjectionKey.ParticipantResolver, { useValue: resolver }); | ||
| } | ||
|
|
||
| async teardown(): Promise<void> { | ||
| // The auth hook owns a token-refresh timer; drop it so it can't fire (and | ||
| // hit Authup) after shutdown. | ||
| if (this.authHook) { | ||
| this.authHook.disable(); | ||
| this.authHook.clearTimer(); | ||
| this.authHook = undefined; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
30 changes: 30 additions & 0 deletions
30
apps/node-message-broker/test/unit/adapters/core/fake-analysis-node-provider.ts
This file contains hidden or 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,30 @@ | ||
| /* | ||
| * Copyright (c) 2026. | ||
| * Author Peter Placzek (tada5hi) | ||
| * For the full copyright and license information, | ||
| * view the LICENSE file that was distributed with this source code. | ||
| */ | ||
|
|
||
| import type { CoreNode, IAnalysisNodeProvider } from '../../../../src/core/analysis/index.ts'; | ||
|
|
||
| /** | ||
| * In-memory `IAnalysisNodeProvider` that records every lookup and returns | ||
| * configurable canned nodes per analysis — stands in for the server-core | ||
| * analysis-node lookup so the resolver is testable without a live core client. | ||
| */ | ||
| export class FakeAnalysisNodeProvider implements IAnalysisNodeProvider { | ||
| calls: string[] = []; | ||
|
|
||
| nodesByAnalysis = new Map<string, CoreNode[]>(); | ||
|
|
||
| /** When set, the next `list` rejects with this error. */ | ||
| error: Error | undefined; | ||
|
|
||
| list = async (analysisId: string): Promise<CoreNode[]> => { | ||
| this.calls.push(analysisId); | ||
| if (this.error) { | ||
| throw this.error; | ||
| } | ||
| return this.nodesByAnalysis.get(analysisId) ?? []; | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.