@yrzhao/aves-core 0.1.0 → 0.3.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 +157 -343
- package/dist/core/AvesClient.d.ts +45 -64
- package/dist/core/AvesClient.js +190 -142
- package/dist/core/AvesError.d.ts +39 -0
- package/dist/core/AvesError.js +42 -0
- package/dist/core/EventEmitter.d.ts +26 -24
- package/dist/core/EventEmitter.js +12 -23
- package/dist/core/SignalingClient.d.ts +30 -7
- package/dist/core/SignalingClient.js +177 -41
- package/dist/core/WebRTCManager.d.ts +152 -39
- package/dist/core/WebRTCManager.js +409 -196
- package/dist/core/webrtc/channels.d.ts +10 -0
- package/dist/core/webrtc/channels.js +25 -0
- package/dist/core/webrtc/fileTransfer.d.ts +84 -0
- package/dist/core/webrtc/fileTransfer.js +399 -0
- package/dist/core/webrtc/mediaTracks.d.ts +53 -0
- package/dist/core/webrtc/mediaTracks.js +385 -0
- package/dist/core/webrtc/messages.d.ts +4 -0
- package/dist/core/webrtc/messages.js +53 -0
- package/dist/esm/core/AvesClient.d.ts +45 -64
- package/dist/esm/core/AvesClient.js +193 -145
- package/dist/esm/core/AvesError.d.ts +39 -0
- package/dist/esm/core/AvesError.js +38 -0
- package/dist/esm/core/EventEmitter.d.ts +26 -24
- package/dist/esm/core/EventEmitter.js +12 -23
- package/dist/esm/core/SignalingClient.d.ts +30 -7
- package/dist/esm/core/SignalingClient.js +178 -42
- package/dist/esm/core/WebRTCManager.d.ts +152 -39
- package/dist/esm/core/WebRTCManager.js +409 -196
- package/dist/esm/core/webrtc/channels.d.ts +10 -0
- package/dist/esm/core/webrtc/channels.js +19 -0
- package/dist/esm/core/webrtc/fileTransfer.d.ts +84 -0
- package/dist/esm/core/webrtc/fileTransfer.js +393 -0
- package/dist/esm/core/webrtc/mediaTracks.d.ts +53 -0
- package/dist/esm/core/webrtc/mediaTracks.js +381 -0
- package/dist/esm/core/webrtc/messages.d.ts +4 -0
- package/dist/esm/core/webrtc/messages.js +48 -0
- package/dist/esm/index.d.ts +4 -1
- package/dist/esm/index.js +5 -4
- package/dist/esm/package.json +3 -0
- package/dist/esm/types/types.d.ts +84 -4
- package/dist/index.cjs +3 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +3 -1
- package/dist/types/types.d.ts +84 -4
- package/package.json +4 -3
|
@@ -1,271 +1,319 @@
|
|
|
1
|
-
import { EventEmitter } from "./EventEmitter";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
1
|
+
import { EventEmitter } from "./EventEmitter.js";
|
|
2
|
+
import { AvesError } from "./AvesError.js";
|
|
3
|
+
import { WebRTCManager } from "./WebRTCManager.js";
|
|
4
|
+
import { SignalingClient } from "./SignalingClient.js";
|
|
5
|
+
function errorMessage(error) {
|
|
6
|
+
return error instanceof Error ? error.message : String(error);
|
|
7
|
+
}
|
|
4
8
|
/**
|
|
5
|
-
* AvesClient - Main client class for WebRTC communication
|
|
6
|
-
*
|
|
7
|
-
* This class coordinates WebRTCManager and SignalingClient to provide
|
|
8
|
-
* a simple API for WebRTC-based real-time communication.
|
|
9
|
-
*
|
|
10
|
-
* Features:
|
|
11
|
-
* - Room management (create, join, leave)
|
|
12
|
-
* - Automatic WebRTC connection establishment
|
|
13
|
-
* - Message sending and receiving
|
|
14
|
-
* - Connection state monitoring
|
|
15
|
-
* - Automatic reconnection
|
|
9
|
+
* AvesClient - Main client class for WebRTC communication.
|
|
16
10
|
*/
|
|
17
11
|
export class AvesClient extends EventEmitter {
|
|
18
12
|
constructor(config) {
|
|
19
13
|
super();
|
|
20
14
|
this.currentRoomId = null;
|
|
21
15
|
this.currentUserId = null;
|
|
16
|
+
this.currentUserName = null;
|
|
22
17
|
this.participants = new Map();
|
|
23
|
-
|
|
18
|
+
this.preparedPeers = new Set();
|
|
19
|
+
this.shouldRestoreSession = false;
|
|
24
20
|
const maxAttempts = config.reconnect?.maxAttempts ?? 5;
|
|
25
21
|
const delay = config.reconnect?.delay ?? 3000;
|
|
22
|
+
const requestTimeoutMs = config.reconnect?.requestTimeoutMs ?? 30000;
|
|
26
23
|
this.config = {
|
|
27
24
|
signalingUrl: config.signalingUrl,
|
|
28
25
|
iceServers: config.iceServers || [
|
|
29
26
|
{ urls: "stun:stun.l.google.com:19302" },
|
|
30
27
|
],
|
|
28
|
+
fileChunkSize: config.fileChunkSize ?? 16 * 1024,
|
|
31
29
|
reconnect: {
|
|
32
30
|
maxAttempts,
|
|
33
31
|
delay,
|
|
32
|
+
requestTimeoutMs,
|
|
34
33
|
},
|
|
35
34
|
debug: config.debug ?? false,
|
|
36
35
|
};
|
|
37
|
-
|
|
38
|
-
this.webrtcManager = new WebRTCManager(this.config.iceServers);
|
|
39
|
-
// Initialize signaling client
|
|
36
|
+
this.webrtcManager = new WebRTCManager(this.config.iceServers, this.config.fileChunkSize, config.video);
|
|
40
37
|
this.signalingClient = new SignalingClient({
|
|
41
38
|
maxAttempts,
|
|
42
39
|
delay,
|
|
40
|
+
requestTimeoutMs,
|
|
43
41
|
});
|
|
44
|
-
// Setup event forwarding and coordination
|
|
45
42
|
this.setupEventHandlers();
|
|
46
43
|
}
|
|
47
|
-
/**
|
|
48
|
-
* Setup event handlers to coordinate between components
|
|
49
|
-
*/
|
|
50
44
|
setupEventHandlers() {
|
|
51
|
-
// Forward signaling state changes
|
|
52
45
|
this.signalingClient.on("stateChange", (state) => {
|
|
53
46
|
this.emit("signalingStateChange", state);
|
|
47
|
+
if (state === "disconnected" &&
|
|
48
|
+
this.currentRoomId &&
|
|
49
|
+
this.currentUserId &&
|
|
50
|
+
this.currentUserName) {
|
|
51
|
+
this.shouldRestoreSession = true;
|
|
52
|
+
}
|
|
53
|
+
if (state === "connected" && this.shouldRestoreSession) {
|
|
54
|
+
this.shouldRestoreSession = false;
|
|
55
|
+
void this.restoreRoomSession();
|
|
56
|
+
}
|
|
54
57
|
});
|
|
55
|
-
// Forward errors
|
|
56
58
|
this.signalingClient.on("error", (error) => {
|
|
57
59
|
this.emit("error", error);
|
|
58
60
|
});
|
|
59
|
-
// Handle user joined - establish WebRTC connection
|
|
60
61
|
this.signalingClient.on("userJoined", async (user) => {
|
|
61
62
|
this.participants.set(user.id, user);
|
|
62
63
|
this.emit("userJoined", user);
|
|
63
|
-
// Initiate WebRTC connection only if our ID is smaller (to avoid both sides initiating)
|
|
64
64
|
if (this.currentUserId && this.currentUserId < user.id) {
|
|
65
65
|
await this.initiateConnection(user.id);
|
|
66
66
|
}
|
|
67
67
|
});
|
|
68
|
-
// Handle user left - cleanup connection
|
|
69
68
|
this.signalingClient.on("userLeft", (userId) => {
|
|
70
69
|
this.participants.delete(userId);
|
|
70
|
+
this.preparedPeers.delete(userId);
|
|
71
71
|
this.webrtcManager.closePeerConnection(userId);
|
|
72
72
|
this.emit("userLeft", userId);
|
|
73
73
|
});
|
|
74
|
-
// Handle incoming offer - create answer
|
|
75
74
|
this.signalingClient.on("offer", async (fromId, offer) => {
|
|
76
75
|
try {
|
|
77
|
-
|
|
78
|
-
const pc = this.webrtcManager.createPeerConnection(fromId);
|
|
79
|
-
// Setup ICE candidate forwarding
|
|
80
|
-
this.webrtcManager.onIceCandidate(fromId, (candidate) => {
|
|
81
|
-
if (this.currentUserId) {
|
|
82
|
-
this.signalingClient.sendIceCandidate(fromId, this.currentUserId, candidate);
|
|
83
|
-
}
|
|
84
|
-
});
|
|
85
|
-
// Setup connection state monitoring
|
|
86
|
-
this.webrtcManager.onConnectionStateChange(fromId, (state) => {
|
|
87
|
-
console.log(`[AvesClient] Connection state with ${fromId}: ${state}`);
|
|
88
|
-
this.emit("connectionStateChange", fromId, state);
|
|
89
|
-
});
|
|
90
|
-
// Setup DataChannel state monitoring
|
|
91
|
-
this.webrtcManager.onDataChannelStateChange(fromId, (state) => {
|
|
92
|
-
console.log(`[AvesClient] DataChannel state with ${fromId}: ${state}`);
|
|
93
|
-
this.emit("dataChannelStateChange", fromId, state);
|
|
94
|
-
});
|
|
95
|
-
// Create and send answer
|
|
76
|
+
this.preparePeerConnection(fromId);
|
|
96
77
|
const answer = await this.webrtcManager.createAnswer(fromId, offer);
|
|
97
78
|
if (this.currentUserId) {
|
|
98
|
-
console.log(`[AvesClient] Sending answer to ${fromId} from ${this.currentUserId}`);
|
|
99
79
|
this.signalingClient.sendAnswer(fromId, this.currentUserId, answer);
|
|
100
80
|
}
|
|
101
81
|
}
|
|
102
82
|
catch (error) {
|
|
103
|
-
this.emit("error", new
|
|
83
|
+
this.emit("error", new AvesError({ message: `Failed to handle offer from ${fromId}: ${errorMessage(error)}`, code: "SERVER_ERROR", stage: "signaling", retryable: true, peerId: fromId }));
|
|
104
84
|
}
|
|
105
85
|
});
|
|
106
|
-
// Handle incoming answer - set remote description
|
|
107
86
|
this.signalingClient.on("answer", async (fromId, answer) => {
|
|
108
87
|
try {
|
|
109
|
-
console.log(`[AvesClient] Received answer from ${fromId}`);
|
|
110
88
|
await this.webrtcManager.setRemoteAnswer(fromId, answer);
|
|
111
|
-
console.log(`[AvesClient] Successfully set remote answer from ${fromId}`);
|
|
112
89
|
}
|
|
113
90
|
catch (error) {
|
|
114
|
-
|
|
115
|
-
this.emit("error", new Error(`Failed to handle answer from ${fromId}: ${error}`));
|
|
91
|
+
this.emit("error", new AvesError({ message: `Failed to handle answer from ${fromId}: ${errorMessage(error)}`, code: "SERVER_ERROR", stage: "signaling", retryable: true, peerId: fromId }));
|
|
116
92
|
}
|
|
117
93
|
});
|
|
118
|
-
// Handle incoming ICE candidate
|
|
119
94
|
this.signalingClient.on("iceCandidate", async (fromId, candidate) => {
|
|
120
95
|
try {
|
|
121
96
|
await this.webrtcManager.addIceCandidate(fromId, candidate);
|
|
122
97
|
}
|
|
123
98
|
catch (error) {
|
|
124
|
-
this.emit("error", new
|
|
99
|
+
this.emit("error", new AvesError({ message: `Failed to add ICE candidate from ${fromId}: ${errorMessage(error)}`, code: "WEBRTC_ICE_FAILED", stage: "transport", retryable: true, peerId: fromId }));
|
|
125
100
|
}
|
|
126
101
|
});
|
|
127
|
-
// Forward messages from WebRTC
|
|
128
102
|
this.webrtcManager.onMessage((peerId, message) => {
|
|
129
103
|
this.emit("message", peerId, message);
|
|
130
104
|
});
|
|
105
|
+
this.webrtcManager.onFileTransferStarted((peerId, info) => {
|
|
106
|
+
this.emit("fileTransferStarted", peerId, info);
|
|
107
|
+
});
|
|
108
|
+
this.webrtcManager.onFileTransferProgress((peerId, progress) => {
|
|
109
|
+
this.emit("fileTransferProgress", peerId, progress);
|
|
110
|
+
});
|
|
111
|
+
this.webrtcManager.onFileTransferCompleted((peerId, result) => {
|
|
112
|
+
this.emit("fileTransferCompleted", peerId, result);
|
|
113
|
+
});
|
|
114
|
+
this.webrtcManager.onFileTransferFailed((peerId, info, error) => {
|
|
115
|
+
this.emit("fileTransferFailed", peerId, info, error);
|
|
116
|
+
});
|
|
117
|
+
this.webrtcManager.onRemoteAudioTrack((peerId, stream, track) => {
|
|
118
|
+
this.emit("remoteAudioTrack", peerId, stream, track);
|
|
119
|
+
});
|
|
120
|
+
this.webrtcManager.onLocalAudioStateChange((state) => {
|
|
121
|
+
this.emit("localAudioStateChange", state);
|
|
122
|
+
});
|
|
123
|
+
this.webrtcManager.onRemoteVideoTrack((peerId, stream, track) => {
|
|
124
|
+
this.emit("remoteVideoTrack", peerId, stream, track);
|
|
125
|
+
});
|
|
126
|
+
this.webrtcManager.onLocalVideoStateChange((state) => {
|
|
127
|
+
this.emit("localVideoStateChange", state);
|
|
128
|
+
});
|
|
129
|
+
this.webrtcManager.onScreenShareStateChange((state) => {
|
|
130
|
+
this.emit("screenShareStateChange", state);
|
|
131
|
+
});
|
|
132
|
+
this.webrtcManager.onError((err) => {
|
|
133
|
+
this.emit("error", err);
|
|
134
|
+
});
|
|
131
135
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
+
async restoreRoomSession() {
|
|
137
|
+
if (!this.currentRoomId || !this.currentUserId || !this.currentUserName) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
136
140
|
try {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
this.
|
|
141
|
-
|
|
142
|
-
|
|
141
|
+
this.webrtcManager.closeAll();
|
|
142
|
+
this.participants.clear();
|
|
143
|
+
this.preparedPeers.clear();
|
|
144
|
+
const joinResult = await this.signalingClient.joinRoom(this.currentRoomId, this.currentUserId, this.currentUserName);
|
|
145
|
+
this.currentUserId = joinResult.userId;
|
|
146
|
+
const participants = joinResult.participants;
|
|
147
|
+
this.participants.clear();
|
|
148
|
+
participants.forEach((participant) => this.participants.set(participant.id, participant));
|
|
149
|
+
for (const participant of participants) {
|
|
150
|
+
if (participant.id !== this.currentUserId &&
|
|
151
|
+
this.currentUserId < participant.id) {
|
|
152
|
+
await this.initiateConnection(participant.id);
|
|
143
153
|
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
this.
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
this.currentRoomId = null;
|
|
158
|
+
this.currentUserId = null;
|
|
159
|
+
this.currentUserName = null;
|
|
160
|
+
this.participants.clear();
|
|
161
|
+
this.preparedPeers.clear();
|
|
162
|
+
this.emit("error", new AvesError({ message: `Failed to restore room session: ${errorMessage(error)}`, code: "SERVER_ERROR", stage: "room", retryable: true, roomId: this.currentRoomId ?? undefined }));
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
preparePeerConnection(peerId) {
|
|
166
|
+
const pc = this.webrtcManager.createPeerConnection(peerId);
|
|
167
|
+
if (this.preparedPeers.has(peerId)) {
|
|
168
|
+
return pc;
|
|
169
|
+
}
|
|
170
|
+
this.preparedPeers.add(peerId);
|
|
171
|
+
this.webrtcManager.onIceCandidate(peerId, (candidate) => {
|
|
172
|
+
if (this.currentUserId) {
|
|
173
|
+
this.signalingClient.sendIceCandidate(peerId, this.currentUserId, candidate);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
this.webrtcManager.onConnectionStateChange(peerId, (state) => {
|
|
177
|
+
if (state === "failed" ||
|
|
178
|
+
state === "disconnected" ||
|
|
179
|
+
state === "closed") {
|
|
180
|
+
this.preparedPeers.delete(peerId);
|
|
181
|
+
}
|
|
182
|
+
this.emit("connectionStateChange", peerId, state);
|
|
183
|
+
});
|
|
184
|
+
this.webrtcManager.onDataChannelStateChange(peerId, (state) => {
|
|
185
|
+
this.emit("dataChannelStateChange", peerId, state);
|
|
186
|
+
});
|
|
187
|
+
return pc;
|
|
188
|
+
}
|
|
189
|
+
async initiateConnection(peerId) {
|
|
190
|
+
try {
|
|
191
|
+
this.preparePeerConnection(peerId);
|
|
156
192
|
const offer = await this.webrtcManager.createOffer(peerId);
|
|
157
193
|
if (this.currentUserId) {
|
|
158
|
-
console.log(`[AvesClient] Sending offer to ${peerId} from ${this.currentUserId}`);
|
|
159
194
|
this.signalingClient.sendOffer(peerId, this.currentUserId, offer);
|
|
160
195
|
}
|
|
161
196
|
}
|
|
162
197
|
catch (error) {
|
|
163
|
-
this.emit("error", new
|
|
198
|
+
this.emit("error", new AvesError({ message: `Failed to initiate connection with ${peerId}: ${errorMessage(error)}`, code: "WEBRTC_CONNECTION_FAILED", stage: "transport", retryable: true, peerId }));
|
|
164
199
|
}
|
|
165
200
|
}
|
|
166
|
-
/**
|
|
167
|
-
* Create a new room
|
|
168
|
-
* @returns Promise that resolves with the room ID
|
|
169
|
-
*/
|
|
170
201
|
async createRoom() {
|
|
171
|
-
|
|
172
|
-
if (!this.isConnected()) {
|
|
202
|
+
if (!this.signalingClient.isConnected()) {
|
|
173
203
|
await this.signalingClient.connect(this.config.signalingUrl);
|
|
174
204
|
}
|
|
175
205
|
const roomId = await this.signalingClient.createRoom();
|
|
176
206
|
this.currentRoomId = roomId;
|
|
177
207
|
return roomId;
|
|
178
208
|
}
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
* @param roomId - Room ID to join
|
|
182
|
-
* @param userId - User ID
|
|
183
|
-
* @param userName - User name
|
|
184
|
-
* @returns Promise that resolves with the list of current participants
|
|
185
|
-
*/
|
|
186
|
-
async joinRoom(roomId, userId, userName) {
|
|
187
|
-
// Connect to signaling server if not connected
|
|
188
|
-
if (!this.isConnected()) {
|
|
209
|
+
async joinRoom(roomId, userIdOrName, maybeUserName) {
|
|
210
|
+
if (!this.signalingClient.isConnected()) {
|
|
189
211
|
await this.signalingClient.connect(this.config.signalingUrl);
|
|
190
212
|
}
|
|
191
|
-
|
|
213
|
+
const requestedUserId = maybeUserName ? userIdOrName : null;
|
|
214
|
+
const userName = maybeUserName ?? userIdOrName;
|
|
215
|
+
this.currentUserId = requestedUserId;
|
|
192
216
|
this.currentRoomId = roomId;
|
|
193
|
-
|
|
194
|
-
|
|
217
|
+
this.currentUserName = userName;
|
|
218
|
+
const joinResult = requestedUserId
|
|
219
|
+
? await this.signalingClient.joinRoom(roomId, requestedUserId, userName)
|
|
220
|
+
: await this.signalingClient.joinRoom(roomId, userName);
|
|
221
|
+
const participants = joinResult.participants;
|
|
222
|
+
this.currentUserId = joinResult.userId;
|
|
195
223
|
this.participants.clear();
|
|
196
|
-
participants.forEach((
|
|
197
|
-
// Initiate WebRTC connections with existing participants
|
|
198
|
-
// Only initiate if our ID is smaller to avoid both sides initiating
|
|
224
|
+
participants.forEach((participant) => this.participants.set(participant.id, participant));
|
|
199
225
|
for (const participant of participants) {
|
|
200
|
-
if (
|
|
226
|
+
if (this.currentUserId &&
|
|
227
|
+
participant.id !== this.currentUserId &&
|
|
228
|
+
this.currentUserId < participant.id) {
|
|
201
229
|
await this.initiateConnection(participant.id);
|
|
202
230
|
}
|
|
203
231
|
}
|
|
204
232
|
return participants;
|
|
205
233
|
}
|
|
206
|
-
/**
|
|
207
|
-
* Leave the current room
|
|
208
|
-
* Disconnects from signaling server and closes all WebRTC connections
|
|
209
|
-
*/
|
|
210
234
|
async leaveRoom() {
|
|
211
|
-
this.
|
|
235
|
+
if (this.currentUserId) {
|
|
236
|
+
await this.signalingClient.leaveRoom(this.currentUserId);
|
|
237
|
+
}
|
|
212
238
|
this.webrtcManager.closeAll();
|
|
213
239
|
this.participants.clear();
|
|
240
|
+
this.preparedPeers.clear();
|
|
214
241
|
this.currentRoomId = null;
|
|
215
242
|
this.currentUserId = null;
|
|
216
|
-
this.
|
|
243
|
+
this.currentUserName = null;
|
|
244
|
+
this.shouldRestoreSession = false;
|
|
217
245
|
}
|
|
218
|
-
/**
|
|
219
|
-
* Send a message to all connected peers
|
|
220
|
-
* @param message - Message to send (will be JSON serialized)
|
|
221
|
-
* @throws Error if any DataChannel is not ready
|
|
222
|
-
*/
|
|
223
246
|
sendMessage(message) {
|
|
224
247
|
this.webrtcManager.sendMessage(message);
|
|
225
248
|
}
|
|
226
|
-
/**
|
|
227
|
-
* Send a message to a specific peer
|
|
228
|
-
* @param peerId - Target peer ID
|
|
229
|
-
* @param message - Message to send (will be JSON serialized)
|
|
230
|
-
* @throws Error if DataChannel is not ready
|
|
231
|
-
*/
|
|
232
249
|
sendMessageToPeer(peerId, message) {
|
|
233
250
|
this.webrtcManager.sendMessageToPeer(peerId, message);
|
|
234
251
|
}
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
252
|
+
async sendFile(blob, options = {}) {
|
|
253
|
+
return this.webrtcManager.sendFile(blob, options);
|
|
254
|
+
}
|
|
255
|
+
async startVoice() {
|
|
256
|
+
return this.webrtcManager.startVoice();
|
|
257
|
+
}
|
|
258
|
+
stopVoice() {
|
|
259
|
+
this.webrtcManager.stopVoice();
|
|
260
|
+
}
|
|
261
|
+
setMuted(muted) {
|
|
262
|
+
this.webrtcManager.setMuted(muted);
|
|
263
|
+
}
|
|
264
|
+
getLocalAudioState() {
|
|
265
|
+
return this.webrtcManager.getLocalAudioState();
|
|
266
|
+
}
|
|
267
|
+
getRemoteAudioStream(peerId) {
|
|
268
|
+
return this.webrtcManager.getRemoteAudioStream(peerId);
|
|
269
|
+
}
|
|
270
|
+
// --- Video ---
|
|
271
|
+
async startVideo(constraints) {
|
|
272
|
+
return this.webrtcManager.startVideo(constraints);
|
|
273
|
+
}
|
|
274
|
+
stopVideo() {
|
|
275
|
+
this.webrtcManager.stopVideo();
|
|
276
|
+
}
|
|
277
|
+
setVideoMuted(muted) {
|
|
278
|
+
this.webrtcManager.setVideoMuted(muted);
|
|
279
|
+
}
|
|
280
|
+
getLocalVideoState() {
|
|
281
|
+
return this.webrtcManager.getLocalVideoState();
|
|
282
|
+
}
|
|
283
|
+
getRemoteVideoStream(peerId) {
|
|
284
|
+
return this.webrtcManager.getRemoteVideoStream(peerId);
|
|
285
|
+
}
|
|
286
|
+
// --- Screen Share ---
|
|
287
|
+
async startScreenShare() {
|
|
288
|
+
return this.webrtcManager.startScreenShare();
|
|
289
|
+
}
|
|
290
|
+
stopScreenShare() {
|
|
291
|
+
this.webrtcManager.stopScreenShare();
|
|
292
|
+
}
|
|
293
|
+
getScreenShareState() {
|
|
294
|
+
return this.webrtcManager.getScreenShareState();
|
|
295
|
+
}
|
|
240
296
|
getConnectionState(peerId) {
|
|
241
|
-
return this.webrtcManager.
|
|
297
|
+
return this.webrtcManager.getConnectionState(peerId);
|
|
242
298
|
}
|
|
243
|
-
/**
|
|
244
|
-
* Get the list of current participants in the room
|
|
245
|
-
* @returns Array of participants
|
|
246
|
-
*/
|
|
247
299
|
getParticipants() {
|
|
248
300
|
return Array.from(this.participants.values());
|
|
249
301
|
}
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
*/
|
|
302
|
+
getCurrentUserId() {
|
|
303
|
+
return this.currentUserId;
|
|
304
|
+
}
|
|
254
305
|
isConnected() {
|
|
255
|
-
|
|
256
|
-
// This is a simplified check - in production you might want more sophisticated state tracking
|
|
257
|
-
return this.currentRoomId !== null;
|
|
306
|
+
return this.signalingClient.isConnected();
|
|
258
307
|
}
|
|
259
|
-
/**
|
|
260
|
-
* Destroy the client and clean up all resources
|
|
261
|
-
* Closes all connections and removes all event listeners
|
|
262
|
-
*/
|
|
263
308
|
destroy() {
|
|
264
|
-
this.signalingClient.disconnect();
|
|
265
|
-
this.webrtcManager.closeAll();
|
|
266
|
-
this.participants.clear();
|
|
267
309
|
this.currentRoomId = null;
|
|
268
310
|
this.currentUserId = null;
|
|
311
|
+
this.currentUserName = null;
|
|
312
|
+
this.shouldRestoreSession = false;
|
|
313
|
+
this.participants.clear();
|
|
314
|
+
this.preparedPeers.clear();
|
|
315
|
+
this.signalingClient.disconnect();
|
|
316
|
+
this.webrtcManager.destroy();
|
|
269
317
|
this.removeAllListeners();
|
|
270
318
|
}
|
|
271
319
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { SignalingErrorCode, SignalingErrorStage } from "../types/types";
|
|
2
|
+
/**
|
|
3
|
+
* All error codes used across the aves ecosystem.
|
|
4
|
+
* Signaling codes come from the shared type; runtime codes extend it.
|
|
5
|
+
*/
|
|
6
|
+
export type AvesErrorCode = SignalingErrorCode | "WEBRTC_CONNECTION_FAILED" | "WEBRTC_DATACHANNEL_FAILED" | "WEBRTC_ICE_FAILED" | "MEDIA_CAPTURE_FAILED" | "MEDIA_NOT_AVAILABLE" | "FILE_TRANSFER_FAILED" | "FILE_TRANSFER_TIMEOUT" | "FILE_TRANSFER_REJECTED" | "FILE_CHANNEL_NOT_READY" | "MESSAGE_CHANNEL_NOT_READY" | "MESSAGE_PARSE_FAILED" | "MESSAGE_SEND_FAILED" | "MESSAGE_SERIALIZE_FAILED" | "UNKNOWN_ERROR" | (string & {});
|
|
7
|
+
/**
|
|
8
|
+
* Structured error class used throughout aves-core and aves-node.
|
|
9
|
+
*
|
|
10
|
+
* Every error carries the operation context (peerId, roomId, requestId)
|
|
11
|
+
* so callers can react programmatically instead of string-matching error messages.
|
|
12
|
+
*
|
|
13
|
+
* The `cause` field preserves the original error (e.g. from a WebSocket
|
|
14
|
+
* or getUserMedia failure) for debugging.
|
|
15
|
+
*/
|
|
16
|
+
export declare class AvesError extends Error {
|
|
17
|
+
readonly code: AvesErrorCode;
|
|
18
|
+
readonly stage: SignalingErrorStage;
|
|
19
|
+
readonly retryable: boolean;
|
|
20
|
+
readonly peerId?: string;
|
|
21
|
+
readonly roomId?: string;
|
|
22
|
+
readonly requestId?: string;
|
|
23
|
+
readonly cause?: unknown;
|
|
24
|
+
constructor(opts: {
|
|
25
|
+
message: string;
|
|
26
|
+
code: AvesErrorCode;
|
|
27
|
+
stage: SignalingErrorStage;
|
|
28
|
+
retryable: boolean;
|
|
29
|
+
peerId?: string;
|
|
30
|
+
roomId?: string;
|
|
31
|
+
requestId?: string;
|
|
32
|
+
cause?: unknown;
|
|
33
|
+
});
|
|
34
|
+
/**
|
|
35
|
+
* Serialise to a plain object for logging or transmission over the wire.
|
|
36
|
+
* `cause` is deliberately excluded — it may contain non-serialisable objects.
|
|
37
|
+
*/
|
|
38
|
+
toJSON(): Record<string, unknown>;
|
|
39
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structured error class used throughout aves-core and aves-node.
|
|
3
|
+
*
|
|
4
|
+
* Every error carries the operation context (peerId, roomId, requestId)
|
|
5
|
+
* so callers can react programmatically instead of string-matching error messages.
|
|
6
|
+
*
|
|
7
|
+
* The `cause` field preserves the original error (e.g. from a WebSocket
|
|
8
|
+
* or getUserMedia failure) for debugging.
|
|
9
|
+
*/
|
|
10
|
+
export class AvesError extends Error {
|
|
11
|
+
constructor(opts) {
|
|
12
|
+
super(opts.message);
|
|
13
|
+
this.name = "AvesError";
|
|
14
|
+
this.code = opts.code;
|
|
15
|
+
this.stage = opts.stage;
|
|
16
|
+
this.retryable = opts.retryable;
|
|
17
|
+
this.peerId = opts.peerId;
|
|
18
|
+
this.roomId = opts.roomId;
|
|
19
|
+
this.requestId = opts.requestId;
|
|
20
|
+
this.cause = opts.cause;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Serialise to a plain object for logging or transmission over the wire.
|
|
24
|
+
* `cause` is deliberately excluded — it may contain non-serialisable objects.
|
|
25
|
+
*/
|
|
26
|
+
toJSON() {
|
|
27
|
+
return {
|
|
28
|
+
name: this.name,
|
|
29
|
+
message: this.message,
|
|
30
|
+
code: this.code,
|
|
31
|
+
stage: this.stage,
|
|
32
|
+
retryable: this.retryable,
|
|
33
|
+
peerId: this.peerId,
|
|
34
|
+
roomId: this.roomId,
|
|
35
|
+
requestId: this.requestId,
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -1,40 +1,42 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* EventEmitter base class
|
|
3
|
-
*
|
|
2
|
+
* EventEmitter base class with optional type-safe event maps.
|
|
3
|
+
*
|
|
4
|
+
* When no type parameter is supplied the emitter is untyped (backward-compatible).
|
|
5
|
+
* Supply an event map to get full type safety on `on`, `emit`, and `off`:
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* interface MyEvents {
|
|
9
|
+
* data: [payload: string, count: number];
|
|
10
|
+
* close: [];
|
|
11
|
+
* }
|
|
12
|
+
* const emitter = new EventEmitter<MyEvents>();
|
|
13
|
+
* emitter.on("data", (payload, count) => { ... }); // payload: string, count: number
|
|
14
|
+
* emitter.emit("data", "hello", 42); // type-safe args
|
|
15
|
+
* ```
|
|
4
16
|
*/
|
|
5
|
-
|
|
17
|
+
type EventArgs<TEvents, TEvent extends keyof TEvents> = TEvents[TEvent] extends unknown[] ? TEvents[TEvent] : never;
|
|
18
|
+
export declare class EventEmitter<TEvents extends object = Record<string, unknown[]>> {
|
|
6
19
|
private events;
|
|
7
20
|
constructor();
|
|
8
21
|
/**
|
|
9
|
-
* Register an event listener
|
|
10
|
-
* @param event - Event name
|
|
11
|
-
* @param callback - Callback function to be invoked when event is emitted
|
|
12
|
-
* @returns this for chaining
|
|
22
|
+
* Register an event listener.
|
|
13
23
|
*/
|
|
14
|
-
on(event:
|
|
24
|
+
on<E extends keyof TEvents & string>(event: E, callback: (...args: EventArgs<TEvents, E>) => void): this;
|
|
15
25
|
/**
|
|
16
|
-
* Remove an event listener
|
|
17
|
-
* @param event - Event name
|
|
18
|
-
* @param callback - Callback function to remove
|
|
19
|
-
* @returns this for chaining
|
|
26
|
+
* Remove an event listener.
|
|
20
27
|
*/
|
|
21
|
-
off(event:
|
|
28
|
+
off<E extends keyof TEvents & string>(event: E, callback: (...args: EventArgs<TEvents, E>) => void): this;
|
|
22
29
|
/**
|
|
23
|
-
* Register a one-time event listener
|
|
24
|
-
* @param event - Event name
|
|
25
|
-
* @param callback - Callback function to be invoked once when event is emitted
|
|
26
|
-
* @returns this for chaining
|
|
30
|
+
* Register a one-time event listener.
|
|
27
31
|
*/
|
|
28
|
-
once(event:
|
|
32
|
+
once<E extends keyof TEvents & string>(event: E, callback: (...args: EventArgs<TEvents, E>) => void): this;
|
|
29
33
|
/**
|
|
30
|
-
* Emit an event with arguments
|
|
31
|
-
* @param event - Event name
|
|
32
|
-
* @param args - Arguments to pass to listeners
|
|
34
|
+
* Emit an event with typed arguments.
|
|
33
35
|
*/
|
|
34
|
-
emit(event:
|
|
36
|
+
emit<E extends keyof TEvents & string>(event: E, ...args: EventArgs<TEvents, E>): void;
|
|
35
37
|
/**
|
|
36
|
-
* Remove all listeners for a specific event or all events
|
|
37
|
-
* @param event - Optional event name. If not provided, removes all listeners for all events
|
|
38
|
+
* Remove all listeners for a specific event or all events.
|
|
38
39
|
*/
|
|
39
40
|
removeAllListeners(event?: string): void;
|
|
40
41
|
}
|
|
42
|
+
export {};
|