-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtypes.d.ts
341 lines (340 loc) · 10.5 KB
/
types.d.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import { EventEmitter, ValidEventTypes } from "eventemitter3";
export interface UtilSupportsObj {
browser: boolean;
webRTC: boolean;
audioVideo: boolean;
data: boolean;
binaryBlob: boolean;
reliable: boolean;
}
declare class Util {
noop(): void;
readonly CLOUD_HOST = "0.peerjs.com";
readonly CLOUD_PORT = 443;
readonly chunkedBrowsers: {
Chrome: number;
chrome: number;
};
readonly chunkedMTU = 16300;
readonly defaultConfig: {
iceServers: ({
urls: string;
username?: undefined;
credential?: undefined;
} | {
urls: string[];
username: string;
credential: string;
})[];
sdpSemantics: string;
};
readonly browser: string;
readonly browserVersion: number;
readonly supports: UtilSupportsObj;
validateId(id: string): boolean;
pack: any;
unpack: any;
chunk(blob: Blob): {
__peerData: number;
n: number;
total: number;
data: Blob;
}[];
blobToArrayBuffer(blob: Blob, cb: (arg: ArrayBuffer | null) => void): FileReader;
binaryStringToArrayBuffer(binary: string): ArrayBuffer | SharedArrayBuffer;
randomToken(): string;
isSecure(): boolean;
}
export const util: Util;
export enum LogLevel {
Disabled = 0,
Errors = 1,
Warnings = 2,
All = 3
}
export enum ConnectionType {
Data = "data",
Media = "media"
}
export enum PeerErrorType {
BrowserIncompatible = "browser-incompatible",
Disconnected = "disconnected",
InvalidID = "invalid-id",
InvalidKey = "invalid-key",
Network = "network",
PeerUnavailable = "peer-unavailable",
SslUnavailable = "ssl-unavailable",
ServerError = "server-error",
SocketError = "socket-error",
SocketClosed = "socket-closed",
UnavailableID = "unavailable-id",
WebRTC = "webrtc"
}
export enum SerializationType {
Binary = "binary",
BinaryUTF8 = "binary-utf8",
JSON = "json"
}
export enum SocketEventType {
Message = "message",
Disconnected = "disconnected",
Error = "error",
Close = "close"
}
export enum ServerMessageType {
Heartbeat = "HEARTBEAT",
Candidate = "CANDIDATE",
Offer = "OFFER",
Answer = "ANSWER",
Open = "OPEN",
Error = "ERROR",
IdTaken = "ID-TAKEN",
InvalidKey = "INVALID-KEY",
Leave = "LEAVE",
Expire = "EXPIRE"
}
/**
* An abstraction on top of WebSockets to provide fastest
* possible connection for peers.
*/
declare class Socket extends EventEmitter {
constructor(secure: any, host: string, port: number, path: string, key: string, pingInterval?: number);
start(id: string, token: string): void;
/** Exposed send for DC & Peer. */
send(data: any): void;
close(): void;
}
declare class ServerMessage {
type: ServerMessageType;
payload: any;
src: string;
}
type BaseConnectionEvents = {
/**
* Emitted when either you or the remote peer closes the connection.
*/
close: () => void;
error: (error: Error) => void;
iceStateChanged: (state: RTCIceConnectionState) => void;
};
declare abstract class BaseConnection<T extends ValidEventTypes> extends EventEmitter<T & BaseConnectionEvents> {
readonly peer: string;
provider: Peer;
readonly options: any;
protected _open: boolean;
readonly metadata: any;
connectionId: string;
peerConnection: RTCPeerConnection;
abstract get type(): ConnectionType;
get open(): boolean;
constructor(peer: string, provider: Peer, options: any);
abstract close(): void;
abstract handleMessage(message: ServerMessage): void;
}
type DataConnectionEvents = {
/**
* Emitted when data is received from the remote peer.
*/
data: (data: unknown) => void;
/**
* Emitted when the connection is established and ready-to-use.
*/
open: () => void;
};
/**
* Wraps a DataChannel between two Peers.
*/
export class DataConnection extends BaseConnection<DataConnectionEvents> implements DataConnection {
readonly label: string;
readonly serialization: SerializationType;
readonly reliable: boolean;
stringify: (data: any) => string;
parse: (data: string) => any;
get type(): ConnectionType;
get dataChannel(): RTCDataChannel;
get bufferSize(): number;
constructor(peerId: string, provider: Peer, options: any);
/** Called by the Negotiator when the DataChannel is ready. */
initialize(dc: RTCDataChannel): void;
/**
* Exposed functionality for users.
*/
/** Allows user to close connection. */
close(): void;
/** Allows user to send data. */
send(data: any, chunked?: boolean): void;
handleMessage(message: ServerMessage): void;
}
export interface AnswerOption {
sdpTransform?: Function;
}
export interface PeerJSOption {
key?: string;
host?: string;
port?: number;
path?: string;
secure?: boolean;
token?: string;
config?: RTCConfiguration;
debug?: number;
referrerPolicy?: ReferrerPolicy;
}
export interface PeerConnectOption {
label?: string;
metadata?: any;
serialization?: string;
reliable?: boolean;
}
export interface CallOption {
metadata?: any;
sdpTransform?: Function;
}
type MediaConnectionEvents = {
/**
* Emitted when a connection to the PeerServer is established.
*/
stream: (stream: MediaStream) => void;
};
/**
* Wraps the streaming interface between two Peers.
*/
export class MediaConnection extends BaseConnection<MediaConnectionEvents> {
get type(): ConnectionType;
get localStream(): MediaStream;
get remoteStream(): MediaStream;
constructor(peerId: string, provider: Peer, options: any);
addStream(remoteStream: any): void;
handleMessage(message: ServerMessage): void;
answer(stream?: MediaStream, options?: AnswerOption): void;
/**
* Exposed functionality for users.
*/
/** Allows user to close connection. */
close(): void;
}
declare class PeerOptions implements PeerJSOption {
debug?: LogLevel;
host?: string;
port?: number;
path?: string;
key?: string;
token?: string;
config?: any;
secure?: boolean;
pingInterval?: number;
referrerPolicy?: ReferrerPolicy;
logFunction?: (logLevel: LogLevel, ...rest: any[]) => void;
}
type PeerEvents = {
/**
* Emitted when a connection to the PeerServer is established.
*/
open: (id: string) => void;
/**
* Emitted when a new data connection is established from a remote peer.
*/
connection: (dataConnection: DataConnection) => void;
/**
* Emitted when a remote peer attempts to call you.
*/
call: (mediaConnection: MediaConnection) => void;
/**
* Emitted when the peer is destroyed and can no longer accept or create any new connections.
*/
close: () => void;
/**
* Emitted when the peer is disconnected from the signalling server
*/
disconnected: (currentId: string) => void;
/**
* Errors on the peer are almost always fatal and will destroy the peer.
*/
error: (error: Error) => void;
};
/**
* A peer who can initiate connections with other peers.
*/
export class Peer extends EventEmitter<PeerEvents> {
/**
* The brokering ID of this peer
*/
get id(): string;
get options(): PeerOptions;
get open(): boolean;
get socket(): Socket;
/**
* A hash of all connections associated with this peer, keyed by the remote peer's ID.
* @deprecated
* Return type will change from Object to Map<string,[]>
*/
get connections(): Object;
/**
* true if this peer and all of its connections can no longer be used.
*/
get destroyed(): boolean;
/**
* false if there is an active connection to the PeerServer.
*/
get disconnected(): boolean;
/**
* A peer can connect to other peers and listen for connections.
*/
constructor();
/**
* A peer can connect to other peers and listen for connections.
* @param options for specifying details about PeerServer
*/
constructor(options: PeerOptions);
/**
* A peer can connect to other peers and listen for connections.
* @param id Other peers can connect to this peer using the provided ID.
* If no ID is given, one will be generated by the brokering server.
* @param options for specifying details about PeerServer
*/
constructor(id: string, options?: PeerOptions);
/** Retrieve messages from lost message store */
_getMessages(connectionId: string): ServerMessage[];
/**
* Connects to the remote peer specified by id and returns a data connection.
* @param peer The brokering ID of the remote peer (their peer.id).
* @param options for specifying details about Peer Connection
*/
connect(peer: string, options?: PeerConnectOption): DataConnection;
/**
* Calls the remote peer specified by id and returns a media connection.
* @param peer The brokering ID of the remote peer (their peer.id).
* @param stream The caller's media stream
* @param options Metadata associated with the connection, passed in by whoever initiated the connection.
*/
call(peer: string, stream: MediaStream, options?: CallOption): MediaConnection;
_removeConnection(connection: DataConnection | MediaConnection): void;
/** Retrieve a data/media connection for this peer. */
getConnection(peerId: string, connectionId: string): null | DataConnection | MediaConnection;
/** Emits a typed error message. */
emitError(type: PeerErrorType, err: string | Error): void;
/**
* Destroys the Peer: closes all active connections as well as the connection
* to the server.
* Warning: The peer can no longer create or accept connections after being
* destroyed.
*/
destroy(): void;
/**
* Disconnects the Peer's connection to the PeerServer. Does not close any
* active connections.
* Warning: The peer can no longer create or accept connections after being
* disconnected. It also cannot reconnect to the server.
*/
disconnect(): void;
/** Attempts to reconnect with the same ID. */
reconnect(): void;
/**
* Get a list of available peer IDs. If you're running your own server, you'll
* want to set allow_discovery: true in the PeerServer options. If you're using
* the cloud server, email [email protected] to get the functionality enabled for
* your key.
*/
listAllPeers(cb?: (_: any[]) => void): void;
}
export default Peer;
//# sourceMappingURL=types.d.ts.map