-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhandler-step-definitions.ts
202 lines (144 loc) · 5.24 KB
/
handler-step-definitions.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* SPDX-FileCopyrightText: 2022 Tim Perry <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/
import * as PluggableAdmin from 'mockttp/dist/pluggable-admin-api/pluggable-admin.browser';
import { MockRTCSessionDescription } from '../mockrtc';
export type Serializable = PluggableAdmin.Serialization.Serializable;
export const { Serializable } = PluggableAdmin.Serialization;
type ClientServerChannel = PluggableAdmin.Serialization.ClientServerChannel;
export interface HandlerStepDefinition extends Serializable {
readonly type: keyof typeof StepDefinitionLookup;
}
export class WaitForDurationStepDefinition extends Serializable implements HandlerStepDefinition {
readonly type = 'wait-for-duration';
constructor(
public readonly durationMs: number
) {
super();
}
explain() {
return `wait for ${this.durationMs}ms`;
}
}
export class WaitForChannelStepDefinition extends Serializable implements HandlerStepDefinition {
readonly type = 'wait-for-rtc-data-channel';
constructor(
public readonly channelLabel?: string
) {
super();
}
explain() {
return `wait for an RTC channel${this.channelLabel ? ` labelled '${this.channelLabel}'` : ''}`;
}
}
export class WaitForMessageStepDefinition extends Serializable implements HandlerStepDefinition {
readonly type = 'wait-for-rtc-message';
constructor(
public readonly channelLabel?: string
) {
super();
}
explain() {
return `wait for an RTC message${this.channelLabel ? ` on channel '${this.channelLabel}'` : ''}`;
}
}
export class WaitForTrackStepDefinition extends Serializable implements HandlerStepDefinition {
readonly type = 'wait-for-rtc-track';
explain() {
return `wait for an RTC track`;
}
}
export class WaitForMediaStepDefinition extends Serializable implements HandlerStepDefinition {
readonly type = 'wait-for-rtc-media';
explain() {
return `wait for RTC media data`;
}
}
export class CreateChannelStepDefinition extends Serializable implements HandlerStepDefinition {
readonly type = 'create-rtc-data-channel';
constructor(
public readonly channelLabel: string
) {
super();
}
explain() {
return `create an RTC data channel labelled '${this.channelLabel}'`;
}
}
export class SendStepDefinition extends Serializable implements HandlerStepDefinition {
readonly type = 'send-rtc-data-message';
constructor(
public readonly channelLabel: string | undefined,
public readonly message: string | Buffer
) {
super();
}
explain() {
return `send an RTC data message${this.channelLabel ? ` on channel '${this.channelLabel}'` : ''}`;
}
}
export class CloseStepDefinition extends Serializable implements HandlerStepDefinition {
readonly type = 'close-rtc-connection';
explain() {
return `close the RTC connection`;
}
}
export class EchoStepDefinition extends Serializable implements HandlerStepDefinition {
readonly type = 'echo-rtc';
explain() {
return `echo all RTC media & data`;
}
}
export class PeerProxyStepDefinition extends Serializable implements HandlerStepDefinition {
readonly type = 'rtc-peer-proxy';
protected getAnswer: (offer: MockRTCSessionDescription) => Promise<RTCSessionDescriptionInit>;
constructor(
connectionTarget:
| RTCPeerConnection
| ((offer: MockRTCSessionDescription) => Promise<RTCSessionDescriptionInit>)
) {
super();
if (connectionTarget instanceof Function) {
this.getAnswer = connectionTarget;
} else {
this.getAnswer = async (offer: MockRTCSessionDescription) => {
await connectionTarget.setRemoteDescription(offer);
const answer = await connectionTarget.createAnswer();
await connectionTarget.setLocalDescription(answer);
return answer;
};
}
}
explain() {
return `proxy the RTC connection to the configured peer`;
}
serialize(channel: ClientServerChannel): {} {
channel.onRequest<
{ offer: MockRTCSessionDescription },
{ answer: RTCSessionDescriptionInit }
>(async (msg) => {
return { answer: await this.getAnswer(msg.offer) };
});
return { type: this.type };
}
}
export class DynamicProxyStepDefinition extends Serializable implements HandlerStepDefinition {
readonly type = 'rtc-dynamic-proxy';
explain() {
return `proxy the RTC connection to a remote peer`;
}
}
export const StepDefinitionLookup = {
'wait-for-duration': WaitForDurationStepDefinition,
'wait-for-rtc-data-channel': WaitForChannelStepDefinition,
'wait-for-rtc-track': WaitForTrackStepDefinition,
'wait-for-rtc-media': WaitForMediaStepDefinition,
'wait-for-rtc-message': WaitForMessageStepDefinition,
'create-rtc-data-channel': CreateChannelStepDefinition,
'send-rtc-data-message': SendStepDefinition,
'close-rtc-connection': CloseStepDefinition,
'echo-rtc': EchoStepDefinition,
'rtc-peer-proxy': PeerProxyStepDefinition,
'rtc-dynamic-proxy': DynamicProxyStepDefinition
};