-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmockrtc-client.ts
101 lines (78 loc) · 3.76 KB
/
mockrtc-client.ts
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* SPDX-FileCopyrightText: 2022 Tim Perry <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/
// Long-term, it'd be great to use the 'official' export path of mockttp/pluggable-admin, but
// if we do so, then TypeScript <4.7 doesn't understand it here or downstream, so we get errors.
// We don't want to use the main-exported version to avoid bundling all of Mockttp in browsers.
// For now we have to use the direct import. We can update once TS 4.7 is widely used.
import * as BrowserPluggableAdmin from 'mockttp/dist/pluggable-admin-api/pluggable-admin.browser';
import type { PluggableAdmin } from 'mockttp';
import { MockRTC, MockRTCEvent, MockRTCOptions, MockRTCRuleDefinition } from "../mockrtc";
import { MockRTCBase } from '../mockrtc-base';
import type { MockRTCPeer } from '../mockrtc-peer';
import { MockRTCRemotePeer } from './mockrtc-remote-peer';
import type { MockRTCAdminPlugin } from "../server/mockrtc-admin-plugin";
import { MockRTCAdminRequestBuilder } from './mockrtc-admin-request-builder';
import { HandlerStepDefinition } from '../handling/handler-step-definitions';
import { MatcherDefinition } from '../matching/matcher-definitions';
export type MockRTCClientOptions =
PluggableAdmin.AdminClientOptions &
MockRTCOptions;
export class MockRTCClient extends MockRTCBase implements MockRTC {
private adminClient: PluggableAdmin.AdminClient<{ webrtc: MockRTCAdminPlugin }>;
private requestBuilder: MockRTCAdminRequestBuilder;
constructor(
private options: MockRTCClientOptions = {}
) {
super();
this.adminClient = new BrowserPluggableAdmin.AdminClient(options);
this.requestBuilder = new MockRTCAdminRequestBuilder();
}
getMatchingPeer(): MockRTCPeer {
return new MockRTCRemotePeer('matching-peer', this.adminClient);
}
async buildPeerFromDefinition(handlerSteps: HandlerStepDefinition[]): Promise<MockRTCPeer> {
const { adminStream } = this.adminClient;
const peerData = await this.adminClient.sendQuery(
this.requestBuilder.buildCreatePeerQuery(handlerSteps, adminStream)
);
const { peerId } = peerData;
return new MockRTCRemotePeer(peerId, this.adminClient);
}
async addRuleFromDefinition(
matchers: MatcherDefinition[],
handlerSteps: HandlerStepDefinition[]
) {
const { adminStream } = this.adminClient;
await this.adminClient.sendQuery(
this.requestBuilder.buildAddRuleQuery(matchers, handlerSteps, adminStream)
);
}
async setRulesFromDefinitions(rules: Array<MockRTCRuleDefinition>) {
const { adminStream } = this.adminClient;
await this.adminClient.sendQuery(
this.requestBuilder.buildSetRulesQuery(rules, adminStream)
);
}
async start(): Promise<void> {
await this.adminClient.start({
webrtc: this.options
});
}
async stop(): Promise<void> {
await this.adminClient.stop();
}
async on(event: MockRTCEvent, callback: any): Promise<void> {
const subscriptionRequest = this.requestBuilder.buildSubscriptionRequest(event);
if (!subscriptionRequest) {
// We just return an immediately promise if we don't recognize the event, which will quietly
// succeed but never call the corresponding callback (the same as the server and most event
// sources in the same kind of situation). This is what happens when the *client* doesn't
// recognize the event. Subscribe() below handles the unknown-to-server case.
console.warn(`Ignoring subscription for event unrecognized by MockRTC client: ${event}`);
return;
}
return this.adminClient.subscribe(subscriptionRequest, callback);
}
}