Skip to content

Commit

Permalink
fix: issues with endpoints and transports
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra committed Jul 15, 2021
1 parent 6bdf7a3 commit 534cfc4
Show file tree
Hide file tree
Showing 16 changed files with 40 additions and 89 deletions.
5 changes: 5 additions & 0 deletions jest.config.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const config: Config.InitialOptions = {
'<rootDir>/packages/$1/src',
],
},
globals: {
'ts-jest': {
isolatedModules: true,
},
},
}

export default config
21 changes: 4 additions & 17 deletions packages/core/src/agent/AgentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,29 +86,16 @@ export class AgentConfig {
}

public getEndpoint() {
// Otherwise we check if an endpoint is set
if (this.initConfig.endpoint) return this.initConfig.endpoint

// Otherwise we'll try to construct it from the host/port
let hostEndpoint = this.initConfig.host
if (hostEndpoint) {
if (this.initConfig.port) hostEndpoint += `:${this.initConfig.port}`
return hostEndpoint
// If we have an endpoint set, use it
if (this.initConfig.endpoint) {
return this.initConfig.endpoint
}

// If we still don't have an endpoint, return didcomm:transport/queue
// Otherwise, return didcomm:transport/queue
// https://github.com/hyperledger/aries-rfcs/issues/405#issuecomment-582612875
return DID_COMM_TRANSPORT_QUEUE
}

public get port() {
return this.initConfig.port
}

public get host() {
return this.initConfig.host
}

public get mediatorConnectionsInvite() {
return this.initConfig.mediatorConnectionsInvite
}
Expand Down
37 changes: 0 additions & 37 deletions packages/core/src/agent/__tests__/AgentConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,6 @@ describe('AgentConfig', () => {
expect(agentConfig.getEndpoint()).toBe(endpoint)
})

it('should return the config host if no inbound connection or config endpoint is available', () => {
const host = 'https://local-url.com'
const port = '3001'

const agentConfig = getAgentConfig('AgentConfig Test', {
host,
port,
})

expect(agentConfig.getEndpoint()).toBe(host + ':' + port)
})

it('should return the config host and port if no inbound connection or config endpoint is available', () => {
const host = 'https://local-url.com'
const port = 8080

const agentConfig = getAgentConfig('AgentConfig Test', {
host,
port,
})

expect(agentConfig.getEndpoint()).toBe(`${host}:${port}`)
})

// added because on first implementation this is what it did. Never again!
it('should return the endpoint without port if the endpoint and port are available', () => {
const endpoint = 'https://local-url.com'
const port = 8080

const agentConfig = getAgentConfig('AgentConfig TesT', {
endpoint,
port,
})

expect(agentConfig.getEndpoint()).toBe(`${endpoint}`)
})

it("should return 'didcomm:transport/queue' if no inbound connection or config endpoint or host/port is available", () => {
const agentConfig = getAgentConfig('AgentConfig Test')

Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import 'reflect-metadata'

export { Agent } from './agent/Agent'
export { AgentConfig } from './agent/AgentConfig'
export { AgentDependencies } from './agent/AgentDependencies'
export { InitConfig, OutboundPackage, DidCommMimeType } from './types'
export { FileSystem } from './storage/FileSystem'
export type { AgentDependencies } from './agent/AgentDependencies'
export type { InitConfig, OutboundPackage } from './types'
export { DidCommMimeType } from './types'
export type { FileSystem } from './storage/FileSystem'
export { InMemoryMessageRepository } from './storage/InMemoryMessageRepository'
export { getDirFromFilePath } from './utils/path'
export { InjectionSymbols } from './constants'
export type { Wallet } from './wallet/Wallet'
export { TransportSession } from './agent/TransportService'
export type { TransportSession } from './agent/TransportService'

export * from './transport'
export * from './modules/basic-messages'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import type { StorageService } from '../../../storage/StorageService'
import type { Wallet } from '../../../wallet/Wallet'
import type { BasicMessageReceivedEvent } from '../BasicMessageEvents'

import { Subject } from 'rxjs'

import { getAgentConfig, getMockConnection } from '../../../../tests/helpers'
import { EventEmitter } from '../../../agent/EventEmitter'
import { InboundMessageContext } from '../../../agent/models/InboundMessageContext'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { Wallet } from '../../../wallet/Wallet'
import type { Did } from 'indy-sdk'

import { Subject } from 'rxjs'

import { getAgentConfig, getMockConnection, mockFunction } from '../../../../tests/helpers'
import { EventEmitter } from '../../../agent/EventEmitter'
import { InboundMessageContext } from '../../../agent/models/InboundMessageContext'
Expand All @@ -29,8 +27,7 @@ const ConnectionRepositoryMock = ConnectionRepository as jest.Mock<ConnectionRep

describe('ConnectionService', () => {
const config = getAgentConfig('ConnectionServiceTest', {
host: 'http://agent.com',
port: 8080,
endpoint: 'http://agent.com:8080',
})

let wallet: Wallet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ export class ConnectionService {

public async returnWhenIsConnected(connectionId: string): Promise<ConnectionRecord> {
const isConnected = (connection: ConnectionRecord) => {
return connection.id === connectionId && connection.isReady
return connection.id === connectionId && connection.state === ConnectionState.Complete
}

const promise = new Promise<ConnectionRecord>((resolve) => {
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ export enum DidCommMimeType {
}

export interface InitConfig {
host?: string
port?: string | number
endpoint?: string
label: string
publicDidSeed?: string
Expand Down
5 changes: 0 additions & 5 deletions packages/core/tests/credentials.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import type { SubjectMessage } from '../../../tests/transport/SubjectInboundTransport'
import type { Agent } from '../src/agent/Agent'
import type { ConnectionRecord } from '../src/modules/connections'

import { Subject } from 'rxjs'

import { SubjectInboundTransporter } from '../../../tests/transport/SubjectInboundTransport'
import { SubjectOutboundTransporter } from '../../../tests/transport/SubjectOutboundTransport'
import { Attachment, AttachmentData } from '../src/decorators/attachment/Attachment'
import {
CredentialPreview,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tests/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ConnectionRecordProps } from '../src/modules/connections'
import type { CredentialRecord, CredentialOfferTemplate, CredentialStateChangedEvent } from '../src/modules/credentials'
import type { SchemaTemplate, CredentialDefinitionTemplate } from '../src/modules/ledger'
import type { ProofAttributeInfo, ProofPredicateInfo, ProofRecord, ProofStateChangedEvent } from '../src/modules/proofs'
import type { InitConfig, WireMessage } from '../src/types'
import type { InitConfig } from '../src/types'
import type { Schema, CredDef, Did } from 'indy-sdk'

import path from 'path'
Expand Down
6 changes: 6 additions & 0 deletions packages/react-native/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { agentDependencies } from '@aries-framework/react-native'

describe('@aries-framework/react-native', () => {
it.todo('React Native tests')

it('exports agentDependencies', () => {
expect(agentDependencies).toBeDefined()
})
})
8 changes: 4 additions & 4 deletions samples/mediator-ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import { WsInboundTransporter } from '../tests/transport/WsInboundTransport'
import { WsOutboundTransporter, Agent, ConnectionInvitationMessage, LogLevel, AgentConfig } from '@aries-framework/core'
import { agentDependencies } from '@aries-framework/node'

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

const agentConfig = {
host: process.env.AGENT_HOST || 'ws://localhost',
port: process.env.AGENT_PORT || 3002,
endpoint: process.env.AGENT_ENDPOINT?.replace('http', 'ws'),
endpoint: process.env.AGENT_ENDPOINT?.replace('http', 'ws') || `ws://localhost:${port}`,
label: process.env.AGENT_LABEL || 'Aries Framework JavaScript Mediator',
walletConfig: { id: process.env.WALLET_NAME || 'AriesFrameworkJavaScript' },
walletCredentials: { key: process.env.WALLET_KEY || 'AriesFrameworkJavaScript' },
Expand Down Expand Up @@ -42,7 +42,7 @@ app.get('/invitation', async (req, res) => {
}
})

const server = app.listen(agentConfig.port, async () => {
const server = app.listen(port, async () => {
await agent.initialize()
})

Expand Down
8 changes: 4 additions & 4 deletions samples/mediator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
} from '@aries-framework/core'
import { agentDependencies } from '@aries-framework/node'

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

const agentConfig = {
host: process.env.AGENT_HOST || 'http://localhost',
port: process.env.AGENT_PORT || 3001,
endpoint: process.env.AGENT_ENDPOINT || undefined,
endpoint: process.env.AGENT_ENDPOINT || `http://localhost:${port}`,
label: process.env.AGENT_LABEL || 'Aries Framework JavaScript Mediator',
walletConfig: { id: process.env.WALLET_NAME || 'AriesFrameworkJavaScript' },
walletCredentials: { key: process.env.WALLET_KEY || 'AriesFrameworkJavaScript' },
Expand All @@ -26,7 +26,7 @@ const agentConfig = {
// Set up agent
const agent = new Agent(agentConfig, agentDependencies)
const config = agent.injectionContainer.resolve(AgentConfig)
const inboundTransporter = new HttpInboundTransporter()
const inboundTransporter = new HttpInboundTransporter({ port })
const outboundTransporter = new HttpOutboundTransporter()

agent.setInboundTransporter(inboundTransporter)
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ describe('E2E tests', () => {
await recipientAgent.initialize()

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

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

Expand Down
12 changes: 7 additions & 5 deletions tests/transport/HttpInboundTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ import { uuid } from '../../packages/core/src/utils/uuid'

export class HttpInboundTransporter implements InboundTransporter {
public readonly app: Express
private port: number
private server?: Server

public constructor() {
public constructor({ app, port }: { app?: Express; port: number }) {
this.port = port

// Create Express App
this.app = express()
this.app = app ?? express()

this.app.use(
text({
Expand All @@ -31,8 +34,7 @@ export class HttpInboundTransporter implements InboundTransporter {
const config = agent.injectionContainer.resolve(AgentConfig)

config.logger.debug(`Starting HTTP inbound transporter`, {
host: config.host,
port: config.port,
port: this.port,
endpoint: config.getEndpoint(),
})

Expand All @@ -55,7 +57,7 @@ export class HttpInboundTransporter implements InboundTransporter {
}
})

this.server = this.app.listen(config.port)
this.server = this.app.listen(this.port)
}

public async stop(): Promise<void> {
Expand Down
1 change: 0 additions & 1 deletion tests/transport/WsInboundTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export class WsInboundTransporter implements InboundTransporter {
this.logger = config.logger

this.logger.debug(`Starting HTTP inbound transporter`, {
port: config.port,
endpoint: config.getEndpoint(),
})

Expand Down

0 comments on commit 534cfc4

Please sign in to comment.