From 05747345a3b070e25556d24c4017ba1b66232b70 Mon Sep 17 00:00:00 2001 From: Yogesh Pawar Date: Mon, 22 Jul 2024 15:23:40 +0530 Subject: [PATCH] Added reuse connection event Signed-off-by: Yogesh Pawar --- src/events/ReuseConnectionEvents.ts | 34 +++++++++++++++++++++++++++++ src/server.ts | 2 ++ 2 files changed, 36 insertions(+) create mode 100644 src/events/ReuseConnectionEvents.ts diff --git a/src/events/ReuseConnectionEvents.ts b/src/events/ReuseConnectionEvents.ts new file mode 100644 index 00000000..7f4c8d25 --- /dev/null +++ b/src/events/ReuseConnectionEvents.ts @@ -0,0 +1,34 @@ +import type { ServerConfig } from '../utils/ServerConfig' +import type { Agent, HandshakeReusedEvent } from '@credo-ts/core' + +import { OutOfBandEventTypes } from '@credo-ts/core' + +import { sendWebSocketEvent } from './WebSocketEvents' +import { sendWebhookEvent } from './WebhookEvent' + +export const reuseConnectionEvents = async (agent: Agent, config: ServerConfig) => { + agent.events.on(OutOfBandEventTypes.HandshakeReused, async (event: HandshakeReusedEvent) => { + const body = { + connectionRecord: event.payload.connectionRecord.toJSON(), + outOfBandRecord: event.payload.outOfBandRecord.toJSON(), + reuseThreadId: event.payload.reuseThreadId, + ...event.metadata, + } + + // Only send webhook if webhook url is configured + if (config.webhookUrl) { + await sendWebhookEvent(config.webhookUrl + '/connections', body, agent.config.logger) + } + + if (config.socketServer) { + // Always emit websocket event to clients (could be 0) + sendWebSocketEvent(config.socketServer, { + ...event, + payload: { + ...event.payload, + connectionRecord: body, + }, + }) + } + }) +} \ No newline at end of file diff --git a/src/server.ts b/src/server.ts index 56f4c943..4a7a7050 100644 --- a/src/server.ts +++ b/src/server.ts @@ -20,6 +20,7 @@ import { questionAnswerEvents } from './events/QuestionAnswerEvents' import { RegisterRoutes } from './routes/routes' import { SecurityMiddleware } from './securityMiddleware' import { maxRateLimit, windowMs } from './utils/util' +import { reuseConnectionEvents } from './events/ReuseConnectionEvents' import { ValidateError, type Exception } from 'tsoa' @@ -35,6 +36,7 @@ export const setupServer = async (agent: Agent, config: ServerConfig, apiKey?: s connectionEvents(agent, config) credentialEvents(agent, config) proofEvents(agent, config) + reuseConnectionEvents(agent, config) } // Use body parser to read sent json payloads