-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add internal http outbound transporter (#255)
Signed-off-by: Timo Glastra <[email protected]>
- Loading branch information
1 parent
2fef3aa
commit 4dd950e
Showing
17 changed files
with
113 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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,53 @@ | ||
import { OutboundTransporter } from './OutboundTransporter' | ||
import { Agent } from '../agent/Agent' | ||
import { Logger } from '../logger' | ||
import { OutboundPackage } from '../types' | ||
import { fetch } from '../utils/fetch' | ||
|
||
export class HttpOutboundTransporter implements OutboundTransporter { | ||
private agent: Agent | ||
private logger: Logger | ||
|
||
public supportedSchemes = ['http', 'https'] | ||
|
||
public constructor(agent: Agent) { | ||
this.agent = agent | ||
this.logger = agent.logger | ||
} | ||
|
||
public async sendMessage(outboundPackage: OutboundPackage) { | ||
const { payload, endpoint, responseRequested } = outboundPackage | ||
|
||
if (!endpoint) { | ||
throw new Error(`Missing endpoint. I don't know how and where to send the message.`) | ||
} | ||
|
||
this.logger.debug( | ||
`Sending outbound message to connection ${outboundPackage.connection.id}`, | ||
outboundPackage.payload | ||
) | ||
|
||
try { | ||
const response = await fetch(endpoint, { | ||
method: 'POST', | ||
body: JSON.stringify(payload), | ||
headers: { 'Content-Type': this.agent.agentConfig.didCommMimeType }, | ||
}) | ||
const responseMessage = await response.text() | ||
|
||
// TODO: do we just want to ignore messages that were | ||
// returned if we didn't request it? | ||
if (responseMessage && responseRequested) { | ||
this.logger.debug(`Response received:\n ${response}`) | ||
const wireMessage = JSON.parse(responseMessage) | ||
this.agent.receiveMessage(wireMessage) | ||
} | ||
} catch (error) { | ||
this.logger.error(`Error sending message to ${endpoint}`, { | ||
error, | ||
body: payload, | ||
didCommMimeType: this.agent.agentConfig.didCommMimeType, | ||
}) | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { OutboundPackage } from '../types' | ||
|
||
export interface OutboundTransporter { | ||
sendMessage(outboundPackage: OutboundPackage, receiveReply: boolean): Promise<any> | ||
supportedSchemes: string[] | ||
|
||
sendMessage(outboundPackage: OutboundPackage): Promise<any> | ||
} |
This file contains 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,3 @@ | ||
export * from './InboundTransporter' | ||
export * from './OutboundTransporter' | ||
export * from './HttpOutboundTransporter' |
This file contains 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 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,3 @@ | ||
export function isNodeJS() { | ||
return typeof process !== 'undefined' && process.release.name === 'node' | ||
} |
This file contains 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,20 @@ | ||
import { isNodeJS } from './environment' | ||
|
||
// RN exposes global fetch | ||
let fetch = global.fetch | ||
let Headers = global.Headers | ||
let Request = global.Request | ||
let Response = global.Response | ||
|
||
// NodeJS doesn't have fetch by default | ||
if (!fetch && isNodeJS()) { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const nodeFetch = require('node-fetch') | ||
|
||
fetch = nodeFetch.default | ||
Headers = nodeFetch.Headers | ||
Request = nodeFetch.Request | ||
Response = nodeFetch.Response | ||
} | ||
|
||
export { fetch, Headers, Request, Response } |
This file contains 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