Skip to content

Commit

Permalink
fix:resolved comments
Browse files Browse the repository at this point in the history
Signed-off-by: pallavicoder <pallavi.ghule@ayanworks.com>
  • Loading branch information
pallavighule committed Dec 4, 2024
1 parent 1a33da3 commit 40cac71
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 114 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ POSTGRES_ADMIN_PASSWORD=

USE_PUSH_NOTIFICATIONS='true'
NOTIFICATION_WEBHOOK_URL=
USE_SOCKETDOCK='true'
USE_SOCKETDOCK='false'
1 change: 1 addition & 0 deletions dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ void connect({
const url = app.url()

process.env.NODE_ENV = 'development'
process.env.USE_SOCKETDOCK = 'false'
process.env.AGENT_PORT = `${port}`
process.env.AGENT_ENDPOINTS = `${url},${url?.replace('http', 'ws')}`
process.env.SHORTENER_BASE_URL = `${url}/s`
Expand Down
10 changes: 3 additions & 7 deletions src/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { Socket } from 'net'

import express from 'express'
import { Server } from 'ws'
import { registerSocketDockRoutes } from './transport/SocketDockInboundTransport'
import { SocketDockInboundTransport } from './transport/SocketDockInboundTransport'

import {
AGENT_ENDPOINTS,
Expand All @@ -35,8 +35,6 @@ import { Logger } from './logger'
import { StorageMessageQueueModule } from './storage/StorageMessageQueueModule'
import { PushNotificationsFcmModule } from './push-notifications/fcm'

import { SocketIdsManager } from './transport/SocketIdManager'

function createModules() {
const modules = {
storageModule: new StorageMessageQueueModule(),
Expand Down Expand Up @@ -114,6 +112,8 @@ export async function createAgent() {
const wsOutboundTransport = new WsOutboundTransport()
agent.registerInboundTransport(wsInboundTransport)
agent.registerOutboundTransport(wsOutboundTransport)
} else {
agent.registerInboundTransport(new SocketDockInboundTransport(app, logger, agent))
}

// Added health check endpoint
Expand Down Expand Up @@ -142,10 +142,6 @@ export async function createAgent() {
httpInboundTransport.app.use(express.json())

await agent.initialize()
if (USE_SOCKETDOCK === 'true') {
const socketIdManager = SocketIdsManager.getInstance()
await registerSocketDockRoutes(app, logger, socketIdManager, agent)
}

// When an 'upgrade' to WS is made on our http server, we forward the
// request to the WS server
Expand Down
23 changes: 0 additions & 23 deletions src/transport/ProcessInboundMessage.ts

This file was deleted.

123 changes: 70 additions & 53 deletions src/transport/SocketDockInboundTransport.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,73 @@
import { Express } from 'express'
import { SocketIdsManager } from './SocketIdManager'
import { processInboundMessage } from './ProcessInboundMessage'
import { Logger } from '../logger'
import { Agent } from '@credo-ts/core'

export function registerSocketDockRoutes(
app: Express,
logger: Logger,
socketIdManager: SocketIdsManager,
agent: Agent
) {
app.post('/connect', async (req, res) => {
logger.info('httpInboundTransport.connect')
const sendUrl = req.body.meta.send
const connectionId = req.body.meta.connection_id

const socketId = socketIdManager.getConnectionBySocketId(connectionId)
if (!socketId) {
socketIdManager.addSocketId(connectionId)
logger.debug(`Saving new socketId : ${connectionId}`)
}

if (!sendUrl) {
logger.error('Missing "send" URL in request body')
return res.status(400).send('Missing "send" URL')
}

try {
res.status(200).send(`connection with socketId : ${connectionId} added successfully`)
} catch (error) {
res.status(500).send('Error sending response to send URL')
}
})

app.post('/message', async (req, res) => {
logger.info('httpInboundTransport.message')

const connectionId = req.body.meta.connection_id

try {
const socketId = socketIdManager.getConnectionBySocketId(connectionId)
await processInboundMessage(req, res, agent, socketId)
} catch (error) {
res.status(500).send('Error sending response to send URL')
}
})

app.post('/disconnect', async (req, res) => {
logger.info('httpInboundTransport.disconnect')
const { connection_id } = req.body
socketIdManager.removeSocketId(connection_id)
logger.debug(`removed connection with socketId : ${connection_id}`)
res.status(200).send(`connection with socketId : ${connection_id} removed successfully`)
})
import { Agent, InboundTransport } from '@credo-ts/core'
import { WebSocketTransportSession } from './SocketDockTransportSession'

export class SocketDockInboundTransport implements InboundTransport {
private app: Express
private logger: Logger
private agent: Agent
private active_connections: Record<string, unknown> = {}

constructor(app: Express, logger: Logger, agent: Agent) {
this.app = app
this.logger = logger
this.agent = agent
}

async start(agent: Agent<any>): Promise<void> {
this.app.post('/connect', async (req, res) => {
this.logger.info('SocketDockInboundTransport.connect')
const sendUrl = req.body.meta.send
const connectionId = req.body.meta.connection_id

const socketId = this.active_connections[connectionId] as string
if (!socketId) {
this.active_connections[socketId] = socketId
this.logger.debug(`Saving new socketId : ${connectionId}`)
}

try {
res.status(200).send(`connection with socketId : ${connectionId} added successfully`)
} catch (error) {
res.status(500).send('Error sending response to send URL')
}
})

this.app.post('/message', async (req, res) => {
this.logger.info('SocketDockInboundTransport.message')

const connectionId = req.body.meta.connection_id

try {
const socketId = this.active_connections[connectionId] as string
const sendUrl = req.body.meta.send
const requestMimeType = req.headers['content-type']
const session = new WebSocketTransportSession(socketId, res, sendUrl, requestMimeType)
const message = req.body.message
const encryptedMessage = JSON.parse(message)
await agent.receiveMessage(encryptedMessage, session)
if (!res.headersSent) {
res.status(200).end()
}
} catch (error) {
if (!res.headersSent) {
res.status(500).send('Error processing message')
}
}
})

this.app.post('/disconnect', async (req, res) => {
this.logger.info('SocketDockInboundTransport.disconnect')
const { connection_id } = req.body

delete this.active_connections[connection_id]
this.logger.debug(`removed connection with socketId : ${connection_id}`)
res.status(200).send(`connection with socketId : ${connection_id} removed successfully`)
})
}

stop(): Promise<void> {
throw new Error('Method not implemented.')
}
}
30 changes: 0 additions & 30 deletions src/transport/SocketIdManager.ts

This file was deleted.

0 comments on commit 40cac71

Please sign in to comment.