-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmockrtc-peer.ts
218 lines (194 loc) · 7.87 KB
/
mockrtc-peer.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* SPDX-FileCopyrightText: 2022 Tim Perry <[email protected]>
* SPDX-License-Identifier: Apache-2.0
*/
import { MockRTCSessionDescription } from './mockrtc';
export interface MockRTCPeerOptions {
debug?: boolean;
recordMessages?: boolean;
}
/**
* A MockRTC peer represents a target you can connect to, and exposes an API to create
* offers or answers to create new connections.
*
* Peers have defined behaviour, and each connection will be handled accordingly and
* independently.
*
* Peers can also optionally track all the messages and metadata across all their
* connections.
*/
export interface MockRTCPeer {
readonly peerId: string;
/**
* Creates an offer for a new connection to this mock peer.
*
* Returns a set of offer parameters: the offer itself, a session to renegotiate
* the connection in future, and a setAnswer callback to call with an answer
* once you have one.
*/
createOffer(options?: OfferOptions): Promise<MockRTCOfferParams>;
/**
* Takes an offer for a WebRTC connection elsewhere, and creates an answer to
* connect that to this peer.
*
* Returns a set of answer parameters: the answer itself, and a session to renegotiate
* the connection in future.
*/
answerOffer(
offer: RTCSessionDescriptionInit,
options?: AnswerOptions
): Promise<MockRTCAnswerParams>;
/**
* Creates an offer for a new external connection to this mock peer.
*
* External connections are used for proxying traffic. They do not do anything
* by default (so they ignore this peer's configured steps) but a mock connection
* can be connected to an external connection using methods like
* {@link MockRTCHandlerBuilder.thenPassThrough thenPassThrough}.
*
* Returns a set of offer parameters: an external connection id, the offer itself,
* a session to renegotiate the connection in future, and a setAnswer callback to
* call with an answer once you have one.
*/
createExternalOffer(options?: OfferOptions): Promise<MockRTCExternalOfferParams>;
/**
* Takes an offer for a WebRTC connection elsewhere, and creates an answer to create
* an external connection to this peer.
*
* External connections are used for proxying traffic. They do not do anything
* by default (so they ignore this peer's configured steps) but a mock connection
* can be connected to an external connection using methods like
* {@link MockRTCHandlerBuilder.thenPassThrough thenPassThrough}.
*
* Returns a set of answer parameters: an external connection id, the answer itself,
* and a session to renegotiate the connection in future.
*/
answerExternalOffer(
offer: RTCSessionDescriptionInit,
options?: AnswerOptions
): Promise<MockRTCExternalAnswerParams>;
/**
* Takes a connection id, and returns the associated session.
*
* This is useful for advanced use cases, where keeping the session returned by other
* setup methods is inconvenient, and it's easier to keep ids and look up sessions
* on demand instead.
*/
getSession(id: string): MockRTCSession;
/**
* Retrieve an array of all data channel messages that this peer has received on
* all connections.
*/
getAllMessages(): Promise<Array<string | Buffer>>;
/**
* Retrieve an array of all data channel messages on a specific channel that this
* peer has received on all connections.
*/
getMessagesOnChannel(channelName: string): Promise<Array<string | Buffer>>;
}
/**
* Once a connection has been created, you can access its session API. This allows
* for renegotiation of an existing session, while persisting the same connection
* and ongoing handling process.
*/
export interface MockRTCSession {
/**
* For most use cases explicitly using the session ID isn't necessary.
*
* For some advanced use cases though, it's more convenient to store session ids and use
* peer.getSession, rather than using the session property from the setup methods directly.
*/
readonly sessionId: string;
/**
* Create a new offer for this session, to renegotiate the existing connection.
*/
createOffer(options?: OfferOptions): Promise<MockRTCSessionDescription>;
/**
* Provide an answer to complete an offer for this session, to renegotiate the existing connection.
*/
completeOffer(answer: RTCSessionDescriptionInit): Promise<void>;
/**
* Get an answer given an offer from elsewhere, to renegotiate the existing connection.
*/
answerOffer(offer: RTCSessionDescriptionInit, options?: AnswerOptions): Promise<MockRTCSessionDescription>;
}
export interface MockRTCOfferParams {
offer: MockRTCSessionDescription;
setAnswer: (answer: RTCSessionDescriptionInit) => Promise<void>;
session: MockRTCSession;
}
export interface MockRTCAnswerParams {
answer: MockRTCSessionDescription;
session: MockRTCSession;
}
export interface OfferOptions {
/**
* A raw SDP string that should be mirrored (best efforts) where possible to
* create an equivalent offer, including the same media with the same params.
*/
mirrorSDP?: string;
/**
* When using mirrorSDP, for SDP that only defines video/audio media we will
* receive an offer with no data stream attached. This can be a problem for
* proxied connections, which need a data stream to hook up the external
* connection later. If addDataStream is set to true, a data stream will always
* be created even if not present in the mirrored SDP.
*
* This option has no effect if mirrorSDP is not set.
*/
addDataStream?: boolean;
/**
* Extra metadata to associate with the connection. This will be exposed on
* events like peer-connected, and can be used to add context to connections.
*
* If this value is provided during renegotiation, it is merged key-wise with
* any existing metadata value for the connection (i.e. existing metadata
* values will not change, unless a new value for the same key is provided).
*/
connectionMetadata?: ConnectionMetadata;
}
export interface AnswerOptions {
/**
* A raw SDP string that should be mirrored (best efforts) where possible to
* create an equivalent answer, including the same media params.
*/
mirrorSDP?: string;
/**
* Extra metadata to associate with the connection. This will be exposed on
* events like peer-connected, and can be used to add context to connections.
*
* If this value is provided during renegotiation, it is merged key-wise with
* any existing metadata value for the connection (i.e. existing metadata
* values will not change, unless a new value for the same key is provided).
*/
connectionMetadata?: ConnectionMetadata;
}
/**
* Extra metadata to associate with the connection. This will be exposed on
* events like peer-connected, and can be used to add context to connections.
*
* The defined fields may only be used as defined here, but all values are
* optional, and any other metadata may be attached in any format here.
*
* The only defined values are:
* - `userAgent` - a client user-agent string (in a browser, the value of
* `navigator.userAgent`)
* - `sourceURL` - the URL of the referring page, when the request is sent by
* a browser
*/
export interface ConnectionMetadata {
userAgent?: string;
sourceURL?: string;
[k: string]: any;
}
export interface MockRTCExternalOfferParams {
id: string; // Used for external attach control messages
offer: MockRTCSessionDescription;
setAnswer: (answer: RTCSessionDescriptionInit) => Promise<void>;
session: MockRTCSession;
}
export interface MockRTCExternalAnswerParams {
id: string; // Used for external attach control messagesz
answer: MockRTCSessionDescription;
session: MockRTCSession;
}