-
Notifications
You must be signed in to change notification settings - Fork 200
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 polling inbound transporter (#323)
- Loading branch information
1 parent
f2e3a06
commit 6dd273b
Showing
7 changed files
with
64 additions
and
93 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type { Agent } from '../agent/Agent' | ||
import type { InboundTransporter } from './InboundTransporter' | ||
|
||
import { AriesFrameworkError } from '../error/AriesFrameworkError' | ||
import { fetch } from '../utils/fetch' | ||
import { sleep } from '../utils/sleep' | ||
|
||
export class PollingInboundTransporter implements InboundTransporter { | ||
public stop: boolean | ||
private pollingInterval: number | ||
|
||
public constructor(pollingInterval = 5000) { | ||
this.stop = false | ||
this.pollingInterval = pollingInterval | ||
} | ||
|
||
public async start(agent: Agent) { | ||
await this.registerMediator(agent) | ||
} | ||
|
||
public async registerMediator(agent: Agent) { | ||
const mediatorUrl = agent.getMediatorUrl() | ||
|
||
if (!mediatorUrl) { | ||
throw new AriesFrameworkError( | ||
'Agent has no mediator URL. Make sure to provide the `mediatorUrl` in the agent config.' | ||
) | ||
} | ||
|
||
const invitationResponse = await fetch(`${mediatorUrl}/invitation`) | ||
const invitationUrl = await invitationResponse.text() | ||
|
||
const mediatorDidResponse = await fetch(`${mediatorUrl}`) | ||
const { verkey } = await mediatorDidResponse.json() | ||
|
||
await agent.routing.provision({ | ||
verkey, | ||
invitationUrl, | ||
}) | ||
this.pollDownloadMessages(agent) | ||
} | ||
|
||
private async pollDownloadMessages(agent: Agent) { | ||
while (!this.stop) { | ||
await agent.routing.downloadMessages() | ||
await sleep(this.pollingInterval) | ||
} | ||
} | ||
} |
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 sleep(ms: number) { | ||
return new Promise((res) => setTimeout(res, ms)) | ||
} |
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