@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
|
@@ -2,6 +2,16 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SignalingClient = void 0;
|
|
4
4
|
const EventEmitter_1 = require("./EventEmitter");
|
|
5
|
+
class SignalingClientError extends Error {
|
|
6
|
+
constructor(payload) {
|
|
7
|
+
super(payload.message);
|
|
8
|
+
this.name = "SignalingClientError";
|
|
9
|
+
this.code = payload.code;
|
|
10
|
+
this.stage = payload.stage;
|
|
11
|
+
this.retryable = payload.retryable;
|
|
12
|
+
this.requestId = payload.requestId;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
5
15
|
/**
|
|
6
16
|
* SignalingClient manages WebSocket connection and signaling message exchange
|
|
7
17
|
* Extends EventEmitter to provide event-driven communication
|
|
@@ -15,6 +25,7 @@ class SignalingClient extends EventEmitter_1.EventEmitter {
|
|
|
15
25
|
this.url = "";
|
|
16
26
|
this.shouldReconnect = true;
|
|
17
27
|
this.pendingRequests = new Map();
|
|
28
|
+
this.requestCounter = 0;
|
|
18
29
|
this.reconnectConfig = reconnectConfig;
|
|
19
30
|
}
|
|
20
31
|
/**
|
|
@@ -60,17 +71,34 @@ class SignalingClient extends EventEmitter_1.EventEmitter {
|
|
|
60
71
|
clearTimeout(this.reconnectTimer);
|
|
61
72
|
this.reconnectTimer = null;
|
|
62
73
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
74
|
+
const ws = this.ws;
|
|
75
|
+
this.ws = null;
|
|
76
|
+
if (ws) {
|
|
77
|
+
ws.onclose = null;
|
|
78
|
+
ws.close();
|
|
66
79
|
}
|
|
80
|
+
this.rejectPendingRequests(new SignalingClientError({
|
|
81
|
+
message: "Signaling connection closed",
|
|
82
|
+
code: "SERVER_ERROR",
|
|
83
|
+
stage: "transport",
|
|
84
|
+
retryable: true,
|
|
85
|
+
}));
|
|
67
86
|
this.emit("stateChange", "disconnected");
|
|
68
87
|
}
|
|
88
|
+
isConnected() {
|
|
89
|
+
return this.ws?.readyState === WebSocket.OPEN;
|
|
90
|
+
}
|
|
69
91
|
/**
|
|
70
92
|
* Handle WebSocket disconnection and attempt reconnection
|
|
71
93
|
*/
|
|
72
94
|
handleDisconnect() {
|
|
73
95
|
this.ws = null;
|
|
96
|
+
this.rejectPendingRequests(new SignalingClientError({
|
|
97
|
+
message: "Signaling connection closed",
|
|
98
|
+
code: "SERVER_ERROR",
|
|
99
|
+
stage: "transport",
|
|
100
|
+
retryable: true,
|
|
101
|
+
}));
|
|
74
102
|
if (!this.shouldReconnect) {
|
|
75
103
|
return;
|
|
76
104
|
}
|
|
@@ -95,10 +123,19 @@ class SignalingClient extends EventEmitter_1.EventEmitter {
|
|
|
95
123
|
const message = JSON.parse(data);
|
|
96
124
|
switch (message.type) {
|
|
97
125
|
case "room-created":
|
|
98
|
-
this.resolvePendingRequest("create-room", message.roomId);
|
|
126
|
+
this.resolvePendingRequest(message.requestId, "create-room", message.roomId);
|
|
99
127
|
break;
|
|
100
128
|
case "room-joined":
|
|
101
|
-
this.resolvePendingRequest("join-room",
|
|
129
|
+
this.resolvePendingRequest(message.requestId, "join-room", {
|
|
130
|
+
participants: message.participants,
|
|
131
|
+
userId: message.userId,
|
|
132
|
+
});
|
|
133
|
+
break;
|
|
134
|
+
case "room-left":
|
|
135
|
+
this.resolvePendingRequest(message.requestId, "leave-room", {
|
|
136
|
+
roomId: message.roomId,
|
|
137
|
+
userId: message.userId,
|
|
138
|
+
});
|
|
102
139
|
break;
|
|
103
140
|
case "user-joined":
|
|
104
141
|
this.emit("userJoined", message.user);
|
|
@@ -116,8 +153,7 @@ class SignalingClient extends EventEmitter_1.EventEmitter {
|
|
|
116
153
|
this.emit("iceCandidate", message.fromId, message.candidate);
|
|
117
154
|
break;
|
|
118
155
|
case "error":
|
|
119
|
-
this.
|
|
120
|
-
this.emit("error", new Error(message.message));
|
|
156
|
+
this.handleErrorMessage(message);
|
|
121
157
|
break;
|
|
122
158
|
}
|
|
123
159
|
}
|
|
@@ -140,32 +176,37 @@ class SignalingClient extends EventEmitter_1.EventEmitter {
|
|
|
140
176
|
* @returns Promise that resolves with the room ID
|
|
141
177
|
*/
|
|
142
178
|
createRoom() {
|
|
179
|
+
const requestId = this.createRequestId("create-room");
|
|
143
180
|
return new Promise((resolve, reject) => {
|
|
144
|
-
this.pendingRequests.set(
|
|
181
|
+
this.pendingRequests.set(requestId, {
|
|
182
|
+
type: "create-room",
|
|
183
|
+
resolve,
|
|
184
|
+
reject,
|
|
185
|
+
});
|
|
145
186
|
try {
|
|
146
|
-
this.send({ type: "create-room" });
|
|
187
|
+
this.send({ type: "create-room", requestId });
|
|
147
188
|
}
|
|
148
189
|
catch (error) {
|
|
149
|
-
this.pendingRequests.delete(
|
|
190
|
+
this.pendingRequests.delete(requestId);
|
|
150
191
|
reject(error);
|
|
151
192
|
}
|
|
152
193
|
});
|
|
153
194
|
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
* @param userName - User name
|
|
159
|
-
* @returns Promise that resolves with the list of participants
|
|
160
|
-
*/
|
|
161
|
-
joinRoom(roomId, userId, userName) {
|
|
195
|
+
joinRoom(roomId, userIdOrName, maybeUserName) {
|
|
196
|
+
const requestId = this.createRequestId("join-room");
|
|
197
|
+
const userId = maybeUserName ? userIdOrName : undefined;
|
|
198
|
+
const userName = maybeUserName ?? userIdOrName;
|
|
162
199
|
return new Promise((resolve, reject) => {
|
|
163
|
-
this.pendingRequests.set(
|
|
200
|
+
this.pendingRequests.set(requestId, {
|
|
201
|
+
type: "join-room",
|
|
202
|
+
resolve,
|
|
203
|
+
reject,
|
|
204
|
+
});
|
|
164
205
|
try {
|
|
165
|
-
this.send({ type: "join-room", roomId, userId, userName });
|
|
206
|
+
this.send({ type: "join-room", roomId, userId, userName, requestId });
|
|
166
207
|
}
|
|
167
208
|
catch (error) {
|
|
168
|
-
this.pendingRequests.delete(
|
|
209
|
+
this.pendingRequests.delete(requestId);
|
|
169
210
|
reject(error);
|
|
170
211
|
}
|
|
171
212
|
});
|
|
@@ -173,10 +214,22 @@ class SignalingClient extends EventEmitter_1.EventEmitter {
|
|
|
173
214
|
/**
|
|
174
215
|
* Leave the current room
|
|
175
216
|
*/
|
|
176
|
-
leaveRoom() {
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
217
|
+
leaveRoom(userId) {
|
|
218
|
+
const requestId = this.createRequestId("leave-room");
|
|
219
|
+
return new Promise((resolve, reject) => {
|
|
220
|
+
this.pendingRequests.set(requestId, {
|
|
221
|
+
type: "leave-room",
|
|
222
|
+
resolve: () => resolve(),
|
|
223
|
+
reject,
|
|
224
|
+
});
|
|
225
|
+
try {
|
|
226
|
+
this.send({ type: "leave-room", userId, requestId });
|
|
227
|
+
}
|
|
228
|
+
catch (error) {
|
|
229
|
+
this.pendingRequests.delete(requestId);
|
|
230
|
+
reject(error);
|
|
231
|
+
}
|
|
232
|
+
});
|
|
180
233
|
}
|
|
181
234
|
/**
|
|
182
235
|
* Send an offer to a peer
|
|
@@ -210,12 +263,33 @@ class SignalingClient extends EventEmitter_1.EventEmitter {
|
|
|
210
263
|
* @param requestType - Type of request to resolve
|
|
211
264
|
* @param value - Value to resolve with
|
|
212
265
|
*/
|
|
213
|
-
resolvePendingRequest(requestType, value) {
|
|
214
|
-
const request =
|
|
266
|
+
resolvePendingRequest(requestId, requestType, value) {
|
|
267
|
+
const request = requestId
|
|
268
|
+
? this.pendingRequests.get(requestId)
|
|
269
|
+
: this.findPendingRequestByType(requestType);
|
|
215
270
|
if (request) {
|
|
216
271
|
request.resolve(value);
|
|
217
|
-
|
|
272
|
+
if (requestId) {
|
|
273
|
+
this.pendingRequests.delete(requestId);
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
this.deletePendingRequestByReference(request);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
handleErrorMessage(message) {
|
|
281
|
+
const error = new SignalingClientError(message);
|
|
282
|
+
if (message.requestId) {
|
|
283
|
+
const request = this.pendingRequests.get(message.requestId);
|
|
284
|
+
if (request) {
|
|
285
|
+
request.reject(error);
|
|
286
|
+
this.pendingRequests.delete(message.requestId);
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
this.rejectPendingRequests(error);
|
|
218
291
|
}
|
|
292
|
+
this.emit("error", error);
|
|
219
293
|
}
|
|
220
294
|
/**
|
|
221
295
|
* Reject all pending requests
|
|
@@ -227,5 +301,25 @@ class SignalingClient extends EventEmitter_1.EventEmitter {
|
|
|
227
301
|
}
|
|
228
302
|
this.pendingRequests.clear();
|
|
229
303
|
}
|
|
304
|
+
createRequestId(prefix) {
|
|
305
|
+
this.requestCounter += 1;
|
|
306
|
+
return `${prefix}-${Date.now()}-${this.requestCounter}`;
|
|
307
|
+
}
|
|
308
|
+
findPendingRequestByType(requestType) {
|
|
309
|
+
for (const request of this.pendingRequests.values()) {
|
|
310
|
+
if (request.type === requestType) {
|
|
311
|
+
return request;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return undefined;
|
|
315
|
+
}
|
|
316
|
+
deletePendingRequestByReference(request) {
|
|
317
|
+
for (const [requestId, candidate] of this.pendingRequests.entries()) {
|
|
318
|
+
if (candidate === request) {
|
|
319
|
+
this.pendingRequests.delete(requestId);
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
}
|
|
230
324
|
}
|
|
231
325
|
exports.SignalingClient = SignalingClient;
|
|
@@ -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
|
}
|