Skip to content
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

feat(node): add http and ws inbound transport #392

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ export { getDirFromFilePath } from './utils/path'
export { InjectionSymbols } from './constants'
export type { Wallet } from './wallet/Wallet'
export type { TransportSession } from './agent/TransportService'
export { TransportService } from './agent/TransportService'

import { uuid } from './utils/uuid'

export * from './transport'
export * from './modules/basic-messages'
Expand All @@ -23,3 +26,9 @@ export * from './modules/routing'
export * from './utils/JsonTransformer'
export * from './logger'
export * from './error'

const utils = {
uuid,
}

export { utils }
6 changes: 4 additions & 2 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@
},
"dependencies": {
"@aries-framework/core": "*",
"express": "^4.17.1",
"indy-sdk": "^1.16.0-dev-1634",
"node-fetch": "^2.6.1",
"ws": "^7.4.6"
"ws": "^7.5.3"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/node": "^15.14.1",
"@types/node-fetch": "^2.5.10",
"@types/ws": "^7.4.4",
"@types/ws": "^7.4.6",
"rimraf": "~3.0.2",
"typescript": "~4.3.0"
}
Expand Down
4 changes: 3 additions & 1 deletion packages/node/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import fetch from 'node-fetch'
import WebSocket from 'ws'

import { NodeFileSystem } from './NodeFileSystem'
import { HttpInboundTransport } from './transport/HttpInboundTransport'
import { WsInboundTransport } from './transport/WsInboundTransport'

const agentDependencies: AgentDependencies = {
FileSystem: NodeFileSystem,
Expand All @@ -15,4 +17,4 @@ const agentDependencies: AgentDependencies = {
indy,
}

export { agentDependencies }
export { agentDependencies, HttpInboundTransport, WsInboundTransport }
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import type { InboundTransporter, Agent, OutboundPackage } from '../../packages/core/src'
import type { TransportSession } from '../../packages/core/src/agent/TransportService'
import type { InboundTransporter, Agent, OutboundPackage, TransportSession } from '@aries-framework/core'
import type { Express, Request, Response } from 'express'
import type { Server } from 'http'

import { DidCommMimeType, AriesFrameworkError, AgentConfig, TransportService, utils } from '@aries-framework/core'
import express, { text } from 'express'

import { DidCommMimeType, AriesFrameworkError } from '../../packages/core/src'
import { AgentConfig } from '../../packages/core/src/agent/AgentConfig'
import { TransportService } from '../../packages/core/src/agent/TransportService'
import { uuid } from '../../packages/core/src/utils/uuid'

export class HttpInboundTransporter implements InboundTransporter {
export class HttpInboundTransport implements InboundTransporter {
public readonly app: Express
private port: number
private server?: Server
Expand Down Expand Up @@ -39,7 +34,7 @@ export class HttpInboundTransporter implements InboundTransporter {
})

this.app.post('/', async (req, res) => {
const session = new HttpTransportSession(uuid(), req, res)
const session = new HttpTransportSession(utils.uuid(), req, res)
try {
const message = req.body
const packedMessage = JSON.parse(message)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import type { Agent, InboundTransporter, Logger, OutboundPackage } from '../../packages/core/src'
import type { TransportSession } from '../../packages/core/src/agent/TransportService'
import type { Agent, InboundTransporter, Logger, OutboundPackage, TransportSession } from '@aries-framework/core'

import WebSocket from 'ws'
import { AriesFrameworkError, AgentConfig, TransportService, utils } from '@aries-framework/core'
import WebSocket, { Server } from 'ws'

import { AriesFrameworkError } from '../../packages/core/src'
import { AgentConfig } from '../../packages/core/src/agent/AgentConfig'
import { TransportService } from '../../packages/core/src/agent/TransportService'
import { uuid } from '../../packages/core/src/utils/uuid'

export class WsInboundTransporter implements InboundTransporter {
private socketServer: WebSocket.Server
export class WsInboundTransport implements InboundTransporter {
private socketServer: Server
private logger!: Logger

// We're using a `socketId` just for the prevention of calling the connection handler twice.
private socketIds: Record<string, unknown> = {}

public constructor(socketServer: WebSocket.Server) {
this.socketServer = socketServer
public constructor({ server, port }: { server: Server; port?: undefined } | { server?: undefined; port: number }) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it work with the following types?

Suggested change
public constructor({ server, port }: { server: Server; port?: undefined } | { server?: undefined; port: number }) {
public constructor({ server, port }: { server: Server; port?: string } | { server?: Server; port: number }) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want it to be an XOR statement. So either port or server should be provided but not both. If you create the server yourself, you set the port on the server. If you don't create the server yourself, you need to pass the port.

Could probably improve, but this was the easiest way I could think of with explicit typing that you shouldn't provide both

this.socketServer = server ?? new Server({ port })
}

public async start(agent: Agent) {
Expand All @@ -30,7 +25,7 @@ export class WsInboundTransporter implements InboundTransporter {
})

this.socketServer.on('connection', (socket: WebSocket) => {
const socketId = uuid()
const socketId = utils.uuid()
this.logger.debug('Socket connected.')

if (!this.socketIds[socketId]) {
Expand Down
5 changes: 2 additions & 3 deletions samples/mediator-ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import express from 'express'
import { Server } from 'ws'

import { TestLogger } from '../packages/core/tests/logger'
import { WsInboundTransporter } from '../tests/transport/WsInboundTransport'

import { WsOutboundTransporter, Agent, ConnectionInvitationMessage, LogLevel, AgentConfig } from '@aries-framework/core'
import { agentDependencies } from '@aries-framework/node'
import { WsInboundTransport, agentDependencies } from '@aries-framework/node'

const port = process.env.AGENT_PORT ? Number(process.env.AGENT_PORT) : 3002

Expand All @@ -26,7 +25,7 @@ const socketServer = new Server({ noServer: true })
const agent = new Agent(agentConfig, agentDependencies)
const config = agent.injectionContainer.resolve(AgentConfig)
const messageSender = new WsOutboundTransporter()
const messageReceiver = new WsInboundTransporter(socketServer)
const messageReceiver = new WsInboundTransport({ server: socketServer })
agent.setInboundTransporter(messageReceiver)
agent.setOutboundTransporter(messageSender)

Expand Down
5 changes: 2 additions & 3 deletions samples/mediator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { TestLogger } from '../packages/core/tests/logger'
import { HttpInboundTransporter } from '../tests/transport/HttpInboundTransport'

import {
HttpOutboundTransporter,
Expand All @@ -8,7 +7,7 @@ import {
LogLevel,
AgentConfig,
} from '@aries-framework/core'
import { agentDependencies } from '@aries-framework/node'
import { HttpInboundTransport, agentDependencies } from '@aries-framework/node'

const port = process.env.AGENT_PORT ? Number(process.env.AGENT_PORT) : 3001

Expand All @@ -26,7 +25,7 @@ const agentConfig = {
// Set up agent
const agent = new Agent(agentConfig, agentDependencies)
const config = agent.injectionContainer.resolve(AgentConfig)
const inboundTransporter = new HttpInboundTransporter({ port })
const inboundTransporter = new HttpInboundTransport({ port })
const outboundTransporter = new HttpOutboundTransporter()

agent.setInboundTransporter(inboundTransporter)
Expand Down
14 changes: 5 additions & 9 deletions tests/e2e.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { SubjectMessage } from './transport/SubjectInboundTransport'

import { Subject } from 'rxjs'
import { Server } from 'ws'

import {
getBaseConfig,
Expand All @@ -12,10 +11,8 @@ import {
previewFromAttributes,
} from '../packages/core/tests/helpers'

import { HttpInboundTransporter } from './transport/HttpInboundTransport'
import { SubjectInboundTransporter } from './transport/SubjectInboundTransport'
import { SubjectOutboundTransporter } from './transport/SubjectOutboundTransport'
import { WsInboundTransporter } from './transport/WsInboundTransport'

import {
HttpOutboundTransporter,
Expand All @@ -30,6 +27,7 @@ import {
ProofState,
AutoAcceptCredential,
} from '@aries-framework/core'
import { HttpInboundTransport, WsInboundTransport } from '@aries-framework/node'

const recipientConfig = getBaseConfig('E2E Recipient', {
autoAcceptCredentials: AutoAcceptCredential.ContentApproved,
Expand Down Expand Up @@ -67,12 +65,12 @@ describe('E2E tests', () => {
await recipientAgent.initialize()

// Mediator Setup
mediatorAgent.setInboundTransporter(new HttpInboundTransporter({ port: 3002 }))
mediatorAgent.setInboundTransporter(new HttpInboundTransport({ port: 3002 }))
mediatorAgent.setOutboundTransporter(new HttpOutboundTransporter())
await mediatorAgent.initialize()

// Sender Setup
senderAgent.setInboundTransporter(new HttpInboundTransporter({ port: 3003 }))
senderAgent.setInboundTransporter(new HttpInboundTransport({ port: 3003 }))
senderAgent.setOutboundTransporter(new HttpOutboundTransporter())
await senderAgent.initialize()

Expand All @@ -89,14 +87,12 @@ describe('E2E tests', () => {
await recipientAgent.initialize()

// Mediator Setup
const mediatorSocketServer = new Server({ port: 3002 })
mediatorAgent.setInboundTransporter(new WsInboundTransporter(mediatorSocketServer))
mediatorAgent.setInboundTransporter(new WsInboundTransport({ port: 3002 }))
mediatorAgent.setOutboundTransporter(new WsOutboundTransporter())
await mediatorAgent.initialize()

// Sender Setup
const senderSocketServer = new Server({ port: 3003 })
senderAgent.setInboundTransporter(new WsInboundTransporter(senderSocketServer))
senderAgent.setInboundTransporter(new WsInboundTransport({ port: 3003 }))
senderAgent.setOutboundTransporter(new WsOutboundTransporter())
await senderAgent.initialize()

Expand Down