forked from openwallet-foundation/credo-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.ts
More file actions
57 lines (48 loc) · 1.8 KB
/
helpers.ts
File metadata and controls
57 lines (48 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* eslint-disable no-console */
import { Subject } from 'rxjs';
import { ConnectionRecord } from '../storage/ConnectionRecord';
import { Agent, InboundTransporter, OutboundTransporter } from '..';
import { OutboundPackage, WireMessage } from '../types';
// Custom matchers which can be used to extend Jest matchers via extend, e. g. `expect.extend({ toBeConnectedWith })`.
export function toBeConnectedWith(received: ConnectionRecord, connection: ConnectionRecord) {
const pass = received.theirDid === connection.did && received.theirKey === connection.verkey;
if (pass) {
return {
message: () =>
`expected connection ${received.did}, ${received.verkey} not to be connected to with ${connection.did}, ${connection.verkey}`,
pass: true,
};
} else {
return {
message: () =>
`expected connection ${received.did}, ${received.verkey} to be connected to with ${connection.did}, ${connection.verkey}`,
pass: false,
};
}
}
export class SubjectInboundTransporter implements InboundTransporter {
private subject: Subject<WireMessage>;
public constructor(subject: Subject<WireMessage>) {
this.subject = subject;
}
public start(agent: Agent) {
this.subscribe(agent, this.subject);
}
private subscribe(agent: Agent, subject: Subject<WireMessage>) {
subject.subscribe({
next: (message: WireMessage) => agent.receiveMessage(message),
});
}
}
export class SubjectOutboundTransporter implements OutboundTransporter {
private subject: Subject<WireMessage>;
public constructor(subject: Subject<WireMessage>) {
this.subject = subject;
}
public async sendMessage(outboundPackage: OutboundPackage) {
console.log('Sending message...');
const { payload } = outboundPackage;
console.log(payload);
this.subject.next(payload);
}
}