@yrzhao/aves-core 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +109 -344
- package/dist/core/AvesClient.d.ts +15 -61
- package/dist/core/AvesClient.js +140 -137
- package/dist/core/SignalingClient.d.ts +10 -3
- package/dist/core/SignalingClient.js +122 -28
- package/dist/core/WebRTCManager.d.ts +108 -39
- package/dist/core/WebRTCManager.js +672 -182
- package/dist/esm/core/AvesClient.d.ts +15 -61
- package/dist/esm/core/AvesClient.js +140 -137
- package/dist/esm/core/SignalingClient.d.ts +10 -3
- package/dist/esm/core/SignalingClient.js +122 -28
- package/dist/esm/core/WebRTCManager.d.ts +108 -39
- package/dist/esm/core/WebRTCManager.js +672 -182
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/types/types.d.ts +63 -4
- package/dist/index.d.ts +1 -1
- package/dist/types/types.d.ts +63 -4
- package/package.json +1 -1
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
import { EventEmitter } from "./EventEmitter";
|
|
2
|
+
class SignalingClientError extends Error {
|
|
3
|
+
constructor(payload) {
|
|
4
|
+
super(payload.message);
|
|
5
|
+
this.name = "SignalingClientError";
|
|
6
|
+
this.code = payload.code;
|
|
7
|
+
this.stage = payload.stage;
|
|
8
|
+
this.retryable = payload.retryable;
|
|
9
|
+
this.requestId = payload.requestId;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
2
12
|
/**
|
|
3
13
|
* SignalingClient manages WebSocket connection and signaling message exchange
|
|
4
14
|
* Extends EventEmitter to provide event-driven communication
|
|
@@ -12,6 +22,7 @@ export class SignalingClient extends EventEmitter {
|
|
|
12
22
|
this.url = "";
|
|
13
23
|
this.shouldReconnect = true;
|
|
14
24
|
this.pendingRequests = new Map();
|
|
25
|
+
this.requestCounter = 0;
|
|
15
26
|
this.reconnectConfig = reconnectConfig;
|
|
16
27
|
}
|
|
17
28
|
/**
|
|
@@ -57,17 +68,34 @@ export class SignalingClient extends EventEmitter {
|
|
|
57
68
|
clearTimeout(this.reconnectTimer);
|
|
58
69
|
this.reconnectTimer = null;
|
|
59
70
|
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
71
|
+
const ws = this.ws;
|
|
72
|
+
this.ws = null;
|
|
73
|
+
if (ws) {
|
|
74
|
+
ws.onclose = null;
|
|
75
|
+
ws.close();
|
|
63
76
|
}
|
|
77
|
+
this.rejectPendingRequests(new SignalingClientError({
|
|
78
|
+
message: "Signaling connection closed",
|
|
79
|
+
code: "SERVER_ERROR",
|
|
80
|
+
stage: "transport",
|
|
81
|
+
retryable: true,
|
|
82
|
+
}));
|
|
64
83
|
this.emit("stateChange", "disconnected");
|
|
65
84
|
}
|
|
85
|
+
isConnected() {
|
|
86
|
+
return this.ws?.readyState === WebSocket.OPEN;
|
|
87
|
+
}
|
|
66
88
|
/**
|
|
67
89
|
* Handle WebSocket disconnection and attempt reconnection
|
|
68
90
|
*/
|
|
69
91
|
handleDisconnect() {
|
|
70
92
|
this.ws = null;
|
|
93
|
+
this.rejectPendingRequests(new SignalingClientError({
|
|
94
|
+
message: "Signaling connection closed",
|
|
95
|
+
code: "SERVER_ERROR",
|
|
96
|
+
stage: "transport",
|
|
97
|
+
retryable: true,
|
|
98
|
+
}));
|
|
71
99
|
if (!this.shouldReconnect) {
|
|
72
100
|
return;
|
|
73
101
|
}
|
|
@@ -92,10 +120,19 @@ export class SignalingClient extends EventEmitter {
|
|
|
92
120
|
const message = JSON.parse(data);
|
|
93
121
|
switch (message.type) {
|
|
94
122
|
case "room-created":
|
|
95
|
-
this.resolvePendingRequest("create-room", message.roomId);
|
|
123
|
+
this.resolvePendingRequest(message.requestId, "create-room", message.roomId);
|
|
96
124
|
break;
|
|
97
125
|
case "room-joined":
|
|
98
|
-
this.resolvePendingRequest("join-room",
|
|
126
|
+
this.resolvePendingRequest(message.requestId, "join-room", {
|
|
127
|
+
participants: message.participants,
|
|
128
|
+
userId: message.userId,
|
|
129
|
+
});
|
|
130
|
+
break;
|
|
131
|
+
case "room-left":
|
|
132
|
+
this.resolvePendingRequest(message.requestId, "leave-room", {
|
|
133
|
+
roomId: message.roomId,
|
|
134
|
+
userId: message.userId,
|
|
135
|
+
});
|
|
99
136
|
break;
|
|
100
137
|
case "user-joined":
|
|
101
138
|
this.emit("userJoined", message.user);
|
|
@@ -113,8 +150,7 @@ export class SignalingClient extends EventEmitter {
|
|
|
113
150
|
this.emit("iceCandidate", message.fromId, message.candidate);
|
|
114
151
|
break;
|
|
115
152
|
case "error":
|
|
116
|
-
this.
|
|
117
|
-
this.emit("error", new Error(message.message));
|
|
153
|
+
this.handleErrorMessage(message);
|
|
118
154
|
break;
|
|
119
155
|
}
|
|
120
156
|
}
|
|
@@ -137,32 +173,37 @@ export class SignalingClient extends EventEmitter {
|
|
|
137
173
|
* @returns Promise that resolves with the room ID
|
|
138
174
|
*/
|
|
139
175
|
createRoom() {
|
|
176
|
+
const requestId = this.createRequestId("create-room");
|
|
140
177
|
return new Promise((resolve, reject) => {
|
|
141
|
-
this.pendingRequests.set(
|
|
178
|
+
this.pendingRequests.set(requestId, {
|
|
179
|
+
type: "create-room",
|
|
180
|
+
resolve,
|
|
181
|
+
reject,
|
|
182
|
+
});
|
|
142
183
|
try {
|
|
143
|
-
this.send({ type: "create-room" });
|
|
184
|
+
this.send({ type: "create-room", requestId });
|
|
144
185
|
}
|
|
145
186
|
catch (error) {
|
|
146
|
-
this.pendingRequests.delete(
|
|
187
|
+
this.pendingRequests.delete(requestId);
|
|
147
188
|
reject(error);
|
|
148
189
|
}
|
|
149
190
|
});
|
|
150
191
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
* @param userName - User name
|
|
156
|
-
* @returns Promise that resolves with the list of participants
|
|
157
|
-
*/
|
|
158
|
-
joinRoom(roomId, userId, userName) {
|
|
192
|
+
joinRoom(roomId, userIdOrName, maybeUserName) {
|
|
193
|
+
const requestId = this.createRequestId("join-room");
|
|
194
|
+
const userId = maybeUserName ? userIdOrName : undefined;
|
|
195
|
+
const userName = maybeUserName ?? userIdOrName;
|
|
159
196
|
return new Promise((resolve, reject) => {
|
|
160
|
-
this.pendingRequests.set(
|
|
197
|
+
this.pendingRequests.set(requestId, {
|
|
198
|
+
type: "join-room",
|
|
199
|
+
resolve,
|
|
200
|
+
reject,
|
|
201
|
+
});
|
|
161
202
|
try {
|
|
162
|
-
this.send({ type: "join-room", roomId, userId, userName });
|
|
203
|
+
this.send({ type: "join-room", roomId, userId, userName, requestId });
|
|
163
204
|
}
|
|
164
205
|
catch (error) {
|
|
165
|
-
this.pendingRequests.delete(
|
|
206
|
+
this.pendingRequests.delete(requestId);
|
|
166
207
|
reject(error);
|
|
167
208
|
}
|
|
168
209
|
});
|
|
@@ -170,10 +211,22 @@ export class SignalingClient extends EventEmitter {
|
|
|
170
211
|
/**
|
|
171
212
|
* Leave the current room
|
|
172
213
|
*/
|
|
173
|
-
leaveRoom() {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
214
|
+
leaveRoom(userId) {
|
|
215
|
+
const requestId = this.createRequestId("leave-room");
|
|
216
|
+
return new Promise((resolve, reject) => {
|
|
217
|
+
this.pendingRequests.set(requestId, {
|
|
218
|
+
type: "leave-room",
|
|
219
|
+
resolve: () => resolve(),
|
|
220
|
+
reject,
|
|
221
|
+
});
|
|
222
|
+
try {
|
|
223
|
+
this.send({ type: "leave-room", userId, requestId });
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
this.pendingRequests.delete(requestId);
|
|
227
|
+
reject(error);
|
|
228
|
+
}
|
|
229
|
+
});
|
|
177
230
|
}
|
|
178
231
|
/**
|
|
179
232
|
* Send an offer to a peer
|
|
@@ -207,12 +260,33 @@ export class SignalingClient extends EventEmitter {
|
|
|
207
260
|
* @param requestType - Type of request to resolve
|
|
208
261
|
* @param value - Value to resolve with
|
|
209
262
|
*/
|
|
210
|
-
resolvePendingRequest(requestType, value) {
|
|
211
|
-
const request =
|
|
263
|
+
resolvePendingRequest(requestId, requestType, value) {
|
|
264
|
+
const request = requestId
|
|
265
|
+
? this.pendingRequests.get(requestId)
|
|
266
|
+
: this.findPendingRequestByType(requestType);
|
|
212
267
|
if (request) {
|
|
213
268
|
request.resolve(value);
|
|
214
|
-
|
|
269
|
+
if (requestId) {
|
|
270
|
+
this.pendingRequests.delete(requestId);
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
this.deletePendingRequestByReference(request);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
handleErrorMessage(message) {
|
|
278
|
+
const error = new SignalingClientError(message);
|
|
279
|
+
if (message.requestId) {
|
|
280
|
+
const request = this.pendingRequests.get(message.requestId);
|
|
281
|
+
if (request) {
|
|
282
|
+
request.reject(error);
|
|
283
|
+
this.pendingRequests.delete(message.requestId);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
else {
|
|
287
|
+
this.rejectPendingRequests(error);
|
|
215
288
|
}
|
|
289
|
+
this.emit("error", error);
|
|
216
290
|
}
|
|
217
291
|
/**
|
|
218
292
|
* Reject all pending requests
|
|
@@ -224,4 +298,24 @@ export class SignalingClient extends EventEmitter {
|
|
|
224
298
|
}
|
|
225
299
|
this.pendingRequests.clear();
|
|
226
300
|
}
|
|
301
|
+
createRequestId(prefix) {
|
|
302
|
+
this.requestCounter += 1;
|
|
303
|
+
return `${prefix}-${Date.now()}-${this.requestCounter}`;
|
|
304
|
+
}
|
|
305
|
+
findPendingRequestByType(requestType) {
|
|
306
|
+
for (const request of this.pendingRequests.values()) {
|
|
307
|
+
if (request.type === requestType) {
|
|
308
|
+
return request;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return undefined;
|
|
312
|
+
}
|
|
313
|
+
deletePendingRequestByReference(request) {
|
|
314
|
+
for (const [requestId, candidate] of this.pendingRequests.entries()) {
|
|
315
|
+
if (candidate === request) {
|
|
316
|
+
this.pendingRequests.delete(requestId);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
227
321
|
}
|
|
@@ -1,93 +1,162 @@
|
|
|
1
|
+
import { FileTransferInfo, FileTransferOptions, FileTransferProgress, FileTransferResult, LocalAudioState } from "../types/types";
|
|
1
2
|
/**
|
|
2
|
-
* WebRTCManager - Manages WebRTC PeerConnections and
|
|
3
|
-
*
|
|
4
|
-
* This class handles the low-level WebRTC connection management including:
|
|
5
|
-
* - Creating and managing RTCPeerConnection instances
|
|
6
|
-
* - Creating and managing RTCDataChannel instances
|
|
7
|
-
* - Handling ICE candidate exchange
|
|
8
|
-
* - Serializing and deserializing messages
|
|
9
|
-
* - Managing connection lifecycle and cleanup
|
|
3
|
+
* WebRTCManager - Manages WebRTC PeerConnections, DataChannels, and media tracks
|
|
10
4
|
*/
|
|
11
5
|
export declare class WebRTCManager {
|
|
12
6
|
private peerConnections;
|
|
13
7
|
private dataChannels;
|
|
14
8
|
private iceServers;
|
|
9
|
+
private fileChunkSize;
|
|
15
10
|
private messageCallbacks;
|
|
16
11
|
private connectionStateCallbacks;
|
|
17
12
|
private dataChannelStateCallbacks;
|
|
18
13
|
private iceCandidateCallbacks;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
14
|
+
private fileTransferStartedCallbacks;
|
|
15
|
+
private fileTransferProgressCallbacks;
|
|
16
|
+
private fileTransferCompletedCallbacks;
|
|
17
|
+
private fileTransferFailedCallbacks;
|
|
18
|
+
private remoteAudioTrackCallbacks;
|
|
19
|
+
private localAudioStateCallbacks;
|
|
20
|
+
private outgoingTransfers;
|
|
21
|
+
private incomingTransfers;
|
|
22
|
+
private readyResolvers;
|
|
23
|
+
private completionResolvers;
|
|
24
|
+
private audioSenders;
|
|
25
|
+
private remoteAudioStreams;
|
|
26
|
+
private localAudioStream;
|
|
27
|
+
private localAudioTrack;
|
|
28
|
+
private isMuted;
|
|
29
|
+
constructor(iceServers: RTCIceServer[], fileChunkSize?: number);
|
|
30
|
+
/**
|
|
31
|
+
* Create a new PeerConnection for the specified peer.
|
|
32
|
+
* If a connection already exists, returns the existing connection.
|
|
23
33
|
*/
|
|
24
34
|
createPeerConnection(peerId: string): RTCPeerConnection;
|
|
25
35
|
/**
|
|
26
|
-
* Create an offer for the specified peer
|
|
27
|
-
*
|
|
36
|
+
* Create an offer for the specified peer.
|
|
37
|
+
* The offer side creates both the message and file channels.
|
|
28
38
|
*/
|
|
29
39
|
createOffer(peerId: string): Promise<RTCSessionDescriptionInit>;
|
|
30
40
|
/**
|
|
31
|
-
* Create an answer for the specified peer
|
|
32
|
-
*
|
|
41
|
+
* Create an answer for the specified peer.
|
|
42
|
+
* The answer side receives data channels from the remote offer.
|
|
33
43
|
*/
|
|
34
44
|
createAnswer(peerId: string, offer: RTCSessionDescriptionInit): Promise<RTCSessionDescriptionInit>;
|
|
35
45
|
/**
|
|
36
|
-
* Set the remote answer for the specified peer
|
|
46
|
+
* Set the remote answer for the specified peer.
|
|
37
47
|
*/
|
|
38
48
|
setRemoteAnswer(peerId: string, answer: RTCSessionDescriptionInit): Promise<void>;
|
|
39
49
|
/**
|
|
40
|
-
* Add an ICE candidate for the specified peer
|
|
50
|
+
* Add an ICE candidate for the specified peer.
|
|
41
51
|
*/
|
|
42
52
|
addIceCandidate(peerId: string, candidate: RTCIceCandidateInit): Promise<void>;
|
|
43
53
|
/**
|
|
44
|
-
*
|
|
54
|
+
* Send a JSON message to all connected peers.
|
|
45
55
|
*/
|
|
46
|
-
|
|
56
|
+
sendMessage(message: any): void;
|
|
47
57
|
/**
|
|
48
|
-
*
|
|
58
|
+
* Send a JSON message to a specific peer.
|
|
49
59
|
*/
|
|
50
|
-
|
|
60
|
+
sendMessageToPeer(peerId: string, message: any): void;
|
|
51
61
|
/**
|
|
52
|
-
*
|
|
62
|
+
* Send a file to one peer or broadcast to every connected peer.
|
|
53
63
|
*/
|
|
54
|
-
|
|
64
|
+
sendFile(blob: Blob, options?: FileTransferOptions): Promise<FileTransferInfo[]>;
|
|
55
65
|
/**
|
|
56
|
-
*
|
|
66
|
+
* Start capturing local audio and bind it to current peer connections.
|
|
57
67
|
*/
|
|
58
|
-
|
|
68
|
+
startVoice(): Promise<MediaStream>;
|
|
59
69
|
/**
|
|
60
|
-
*
|
|
70
|
+
* Stop local audio capture and detach it from active peers.
|
|
61
71
|
*/
|
|
62
|
-
|
|
72
|
+
stopVoice(): void;
|
|
63
73
|
/**
|
|
64
|
-
*
|
|
65
|
-
* Throws error if any DataChannel is not ready
|
|
74
|
+
* Toggle local mute state.
|
|
66
75
|
*/
|
|
67
|
-
|
|
76
|
+
setMuted(muted: boolean): void;
|
|
68
77
|
/**
|
|
69
|
-
*
|
|
70
|
-
* Throws error if DataChannel is not ready
|
|
78
|
+
* Read current local audio state.
|
|
71
79
|
*/
|
|
72
|
-
|
|
80
|
+
getLocalAudioState(): LocalAudioState;
|
|
73
81
|
/**
|
|
74
|
-
*
|
|
82
|
+
* Read current remote audio stream for a peer.
|
|
83
|
+
*/
|
|
84
|
+
getRemoteAudioStream(peerId: string): MediaStream | null;
|
|
85
|
+
/**
|
|
86
|
+
* Register a callback for ICE candidates from the specified peer.
|
|
87
|
+
*/
|
|
88
|
+
onIceCandidate(peerId: string, callback: (candidate: RTCIceCandidateInit) => void): void;
|
|
89
|
+
/**
|
|
90
|
+
* Register a callback for connection state changes.
|
|
91
|
+
*/
|
|
92
|
+
onConnectionStateChange(peerId: string, callback: (state: RTCPeerConnectionState) => void): void;
|
|
93
|
+
/**
|
|
94
|
+
* Register a callback for main DataChannel state changes.
|
|
95
|
+
*/
|
|
96
|
+
onDataChannelStateChange(peerId: string, callback: (state: RTCDataChannelState) => void): void;
|
|
97
|
+
/**
|
|
98
|
+
* Register a callback for incoming user messages.
|
|
99
|
+
*/
|
|
100
|
+
onMessage(callback: (peerId: string, message: any) => void): void;
|
|
101
|
+
onFileTransferStarted(callback: (peerId: string, info: FileTransferInfo) => void): void;
|
|
102
|
+
onFileTransferProgress(callback: (peerId: string, progress: FileTransferProgress) => void): void;
|
|
103
|
+
onFileTransferCompleted(callback: (peerId: string, result: FileTransferResult) => void): void;
|
|
104
|
+
onFileTransferFailed(callback: (peerId: string, info: FileTransferInfo | null, error: Error) => void): void;
|
|
105
|
+
onRemoteAudioTrack(callback: (peerId: string, stream: MediaStream, track: MediaStreamTrack) => void): void;
|
|
106
|
+
onLocalAudioStateChange(callback: (state: LocalAudioState) => void): void;
|
|
107
|
+
/**
|
|
108
|
+
* Close the connection with a specific peer.
|
|
75
109
|
*/
|
|
76
110
|
closePeerConnection(peerId: string): void;
|
|
77
111
|
/**
|
|
78
|
-
* Close all connections and clean up resources
|
|
112
|
+
* Close all connections and clean up resources.
|
|
79
113
|
*/
|
|
80
114
|
closeAll(): void;
|
|
115
|
+
destroy(): void;
|
|
81
116
|
/**
|
|
82
|
-
* Get all active peer IDs
|
|
117
|
+
* Get all active peer IDs.
|
|
83
118
|
*/
|
|
84
119
|
getActivePeers(): string[];
|
|
85
120
|
/**
|
|
86
|
-
* Check if connected to a specific peer
|
|
121
|
+
* Check if connected to a specific peer.
|
|
87
122
|
*/
|
|
88
123
|
isConnected(peerId: string): boolean;
|
|
89
124
|
/**
|
|
90
|
-
* Check if DataChannel is ready for a specific peer
|
|
125
|
+
* Check if the main DataChannel is ready for a specific peer.
|
|
91
126
|
*/
|
|
92
127
|
isDataChannelReady(peerId: string): boolean;
|
|
128
|
+
/**
|
|
129
|
+
* Check if the file channel is ready for a specific peer.
|
|
130
|
+
*/
|
|
131
|
+
isFileChannelReady(peerId: string): boolean;
|
|
132
|
+
private setupPeerConnection;
|
|
133
|
+
private createAudioSender;
|
|
134
|
+
private syncLocalAudioTrack;
|
|
135
|
+
private ensureDataChannel;
|
|
136
|
+
private setupDataChannel;
|
|
137
|
+
private handleMessageChannelMessage;
|
|
138
|
+
private sendFileToPeer;
|
|
139
|
+
private handleFileControlMessage;
|
|
140
|
+
private handleFileChunk;
|
|
141
|
+
private emitFileTransferStarted;
|
|
142
|
+
private emitFileTransferProgress;
|
|
143
|
+
private emitFileTransferFailed;
|
|
144
|
+
private emitLocalAudioState;
|
|
145
|
+
private sendFileControl;
|
|
146
|
+
private awaitTransferReady;
|
|
147
|
+
private clearReadyResolver;
|
|
148
|
+
private awaitTransferCompletion;
|
|
149
|
+
private clearCompletionResolver;
|
|
150
|
+
private getPeerConnection;
|
|
151
|
+
private getMessageChannel;
|
|
152
|
+
private getFileChannel;
|
|
153
|
+
private isFileControlMessage;
|
|
154
|
+
private getChunkSize;
|
|
155
|
+
private getBlobName;
|
|
156
|
+
private getBlobLastModified;
|
|
157
|
+
private generateTransferId;
|
|
158
|
+
private validateChunkSize;
|
|
159
|
+
private failActiveTransfers;
|
|
160
|
+
private normalizePeerChannels;
|
|
161
|
+
private isPeerChannels;
|
|
93
162
|
}
|