-
Notifications
You must be signed in to change notification settings - Fork 24
feat: socketdock implementation #29
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
Open
pallavighule
wants to merge
24
commits into
openwallet-foundation:main
Choose a base branch
from
credebl:feat/socketdock-implementation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
ddbba3d
added coonect message disconnect endpoints
pallavighule e8fbc1e
feat:added socketdock routes and socketdock session
pallavighule cb2730c
fix:changes in dev.ts
pallavighule e63bf2b
fix:removed axios dependancy
pallavighule fd82c8a
fix:removed unused variables
pallavighule 5e3ead1
chore:install dependancies
pallavighule 537deaf
fix:format the file
pallavighule 1a33da3
fix:deleted setting.json
pallavighule 40cac71
fix:resolved comments
pallavighule fc3b2d8
fix:resolved comments
pallavighule 85c8c6b
fix:resolved comments
pallavighule 8b221d1
fix:resolved comments
pallavighule 650b6e2
fix:add some checks
pallavighule d8eba9d
fix:add some checks
pallavighule 063ac8e
fix:add event listeners
pallavighule becbc31
made skipLibCheck true
pallavighule 64ab6a6
removed transport session if socket is disconnected
RinkalBhojani d7db235
resolved prettier issue
RinkalBhojani c28cf55
chore:changed debug message
pallavighule 46bfeda
fix:resolved merged conflicts
pallavighule e1e4f45
fix: improve logic to treat USE_SOCKETDOCK as a boolean
pallavighule 6e68a9d
fix:add formating
pallavighule a587ef4
fix:changed request mime type from any to didcomm mime type
pallavighule 6813a84
fix:added constant in config file
pallavighule 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 was deleted.
Oops, something went wrong.
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
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,98 @@ | ||
import { | ||
type Agent, | ||
AgentEventTypes, | ||
type AgentMessageReceivedEvent, | ||
type DidCommMimeType, | ||
type InboundTransport, | ||
TransportService, | ||
} from '@credo-ts/core' | ||
import type { Express } from 'express' | ||
import express from 'express' | ||
import { SocketDockTransportSession } from './SocketDockTransportSession' | ||
|
||
export class SocketDockInboundTransport implements InboundTransport { | ||
private app: Express | ||
private activeConnections: Record<string, string> = {} | ||
|
||
public constructor({ app }: { app: Express }) { | ||
this.app = app | ||
|
||
this.app.use(express.json()) | ||
} | ||
|
||
public async start(agent: Agent) { | ||
this.app.post('/connect', async (req, res) => { | ||
agent.config.logger.info('SocketDockInboundTransport.connect') | ||
const { connection_id: connectionId } = req.body.meta | ||
if (!connectionId) { | ||
throw new Error('ConnectionId is not sent from socketDock server') | ||
} | ||
|
||
const socketId = this.activeConnections[connectionId] | ||
if (!socketId) { | ||
this.activeConnections[connectionId] = connectionId | ||
agent.config.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) => { | ||
agent.config.logger.info('SocketDockInboundTransport.message') | ||
|
||
const { connection_id: connectionId } = req.body.meta | ||
if (!connectionId) { | ||
throw new Error('ConnectionId is not sent from socketDock server') | ||
} | ||
|
||
try { | ||
const socketId = this.activeConnections[connectionId] | ||
agent.config.logger.debug(`activeConnections transport session : ${socketId} ${connectionId}`) | ||
const sendUrl = req.body.meta.send | ||
const requestMimeType = req.headers['content-type'] as DidCommMimeType | ||
const session = new SocketDockTransportSession(socketId, res, sendUrl, requestMimeType) | ||
const message = req.body.message | ||
const encryptedMessage = JSON.parse(message) | ||
|
||
agent.config.logger.debug(`Session value for transport session : ${session.id}`) | ||
|
||
agent.events.emit<AgentMessageReceivedEvent>(agent.context, { | ||
type: AgentEventTypes.AgentMessageReceived, | ||
payload: { | ||
message: encryptedMessage, | ||
session: session, | ||
}, | ||
}) | ||
} catch (error) { | ||
if (!res.headersSent) { | ||
res.status(500).send('Error processing message') | ||
} | ||
} | ||
}) | ||
|
||
this.app.post('/disconnect', async (req, res) => { | ||
agent.config.logger.info('SocketDockInboundTransport.disconnect') | ||
const transportService = agent.dependencyManager.resolve(TransportService) | ||
const { connection_id } = req.body | ||
if (!connection_id) { | ||
throw new Error('ConnectionId is not sent from socketDock server') | ||
} | ||
|
||
delete this.activeConnections[connection_id] | ||
const session = transportService.findSessionById(connection_id) | ||
agent.config.logger.debug(`Got session from transportService ${session?.id}`) | ||
if (session) { | ||
transportService.removeSession(session) | ||
} | ||
|
||
agent.config.logger.debug(`removed mapping of socketId : ${connection_id}`) | ||
res.status(200).send(`Mapping with socketId : ${connection_id} removed successfully`) | ||
}) | ||
} | ||
|
||
public async stop(): Promise<void> {} | ||
} |
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,49 @@ | ||
import { type AgentContext, DidCommMimeType, type EncryptedMessage, type TransportSession } from '@credo-ts/core' | ||
import { CredoError } from '@credo-ts/core' | ||
import { agentDependencies } from '@credo-ts/node' | ||
import type { Response } from 'express' | ||
|
||
export const supportedContentTypes: string[] = [DidCommMimeType.V0, DidCommMimeType.V1] | ||
|
||
export class SocketDockTransportSession implements TransportSession { | ||
public id: string | ||
public readonly type = 'socketdock' | ||
public res: Response | ||
public sendUrl: string | ||
public requestMimeType: DidCommMimeType | ||
|
||
public constructor(id: string, res: Response, sendUrl: string, requestMimeType: DidCommMimeType) { | ||
this.id = id | ||
this.res = res | ||
this.sendUrl = sendUrl | ||
this.requestMimeType = requestMimeType | ||
} | ||
|
||
public async close() { | ||
if (!this.res.headersSent) { | ||
this.res.status(200).end() | ||
} | ||
} | ||
|
||
public async send(agentContext: AgentContext, encryptedMessage: EncryptedMessage): Promise<void> { | ||
if (this.res.headersSent) { | ||
throw new CredoError(`${this.type} transport session has been closed.`) | ||
} | ||
|
||
// By default we take the agent config's default DIDComm content-type | ||
let responseMimeType = agentContext.config.didCommMimeType | ||
|
||
if (this.requestMimeType && supportedContentTypes.includes(this.requestMimeType)) { | ||
responseMimeType = this.requestMimeType | ||
} | ||
|
||
const requestOptions = { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': responseMimeType, | ||
}, | ||
body: JSON.stringify(encryptedMessage), | ||
} | ||
await agentDependencies.fetch(this.sendUrl, requestOptions) | ||
} | ||
} |
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.