@yrzhao/aves-core 1.1.0 → 1.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 +55 -40
- package/dist/core/AvesClient.d.ts +32 -8
- package/dist/core/AvesClient.js +254 -55
- package/dist/core/AvesError.d.ts +1 -1
- package/dist/core/EventEmitter.d.ts +8 -16
- package/dist/core/EventEmitter.js +41 -10
- package/dist/core/SignalingClient.d.ts +39 -44
- package/dist/core/SignalingClient.js +434 -78
- package/dist/core/WebRTCManager.d.ts +67 -102
- package/dist/core/WebRTCManager.js +607 -136
- package/dist/core/webrtc/channels.d.ts +6 -0
- package/dist/core/webrtc/channels.js +15 -0
- package/dist/core/webrtc/fileTransfer.d.ts +46 -3
- package/dist/core/webrtc/fileTransfer.js +725 -78
- package/dist/core/webrtc/mediaTracks.d.ts +38 -5
- package/dist/core/webrtc/mediaTracks.js +269 -58
- package/dist/esm/core/AvesClient.d.ts +32 -8
- package/dist/esm/core/AvesClient.js +254 -55
- package/dist/esm/core/AvesError.d.ts +1 -1
- package/dist/esm/core/EventEmitter.d.ts +8 -16
- package/dist/esm/core/EventEmitter.js +41 -10
- package/dist/esm/core/SignalingClient.d.ts +39 -44
- package/dist/esm/core/SignalingClient.js +434 -78
- package/dist/esm/core/WebRTCManager.d.ts +67 -102
- package/dist/esm/core/WebRTCManager.js +608 -137
- package/dist/esm/core/webrtc/channels.d.ts +6 -0
- package/dist/esm/core/webrtc/channels.js +14 -0
- package/dist/esm/core/webrtc/fileTransfer.d.ts +46 -3
- package/dist/esm/core/webrtc/fileTransfer.js +722 -77
- package/dist/esm/core/webrtc/mediaTracks.d.ts +38 -5
- package/dist/esm/core/webrtc/mediaTracks.js +269 -58
- package/dist/esm/index.d.ts +2 -1
- package/dist/esm/types/types.d.ts +106 -9
- package/dist/index.d.ts +2 -1
- package/dist/types/types.d.ts +106 -9
- package/package.json +8 -6
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export class EventEmitter {
|
|
2
2
|
constructor() {
|
|
3
3
|
this.events = new Map();
|
|
4
|
+
this.onceWrappers = new Map();
|
|
4
5
|
}
|
|
5
6
|
/**
|
|
6
7
|
* Register an event listener.
|
|
@@ -13,12 +14,22 @@ export class EventEmitter {
|
|
|
13
14
|
return this;
|
|
14
15
|
}
|
|
15
16
|
/**
|
|
16
|
-
* Remove an event listener.
|
|
17
|
+
* Remove an event listener (works for both `on` and `once` registrations).
|
|
17
18
|
*/
|
|
18
19
|
off(event, callback) {
|
|
19
20
|
const callbacks = this.events.get(event);
|
|
21
|
+
const fn = callback;
|
|
20
22
|
if (callbacks) {
|
|
21
|
-
callbacks.delete(
|
|
23
|
+
callbacks.delete(fn);
|
|
24
|
+
const wrappers = this.onceWrappers.get(event);
|
|
25
|
+
const wrapped = wrappers?.get(fn);
|
|
26
|
+
if (wrapped) {
|
|
27
|
+
callbacks.delete(wrapped);
|
|
28
|
+
wrappers.delete(fn);
|
|
29
|
+
if (wrappers.size === 0) {
|
|
30
|
+
this.onceWrappers.delete(event);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
22
33
|
if (callbacks.size === 0) {
|
|
23
34
|
this.events.delete(event);
|
|
24
35
|
}
|
|
@@ -29,28 +40,43 @@ export class EventEmitter {
|
|
|
29
40
|
* Register a one-time event listener.
|
|
30
41
|
*/
|
|
31
42
|
once(event, callback) {
|
|
43
|
+
const fn = callback;
|
|
44
|
+
const existingWrappers = this.onceWrappers.get(event);
|
|
45
|
+
if (existingWrappers?.has(fn)) {
|
|
46
|
+
return this;
|
|
47
|
+
}
|
|
32
48
|
const onceWrapper = (...args) => {
|
|
33
|
-
this.off(event,
|
|
34
|
-
|
|
49
|
+
this.off(event, callback);
|
|
50
|
+
fn(...args);
|
|
35
51
|
};
|
|
36
52
|
if (!this.events.has(event)) {
|
|
37
|
-
this.events.set(event, new Set(
|
|
53
|
+
this.events.set(event, new Set());
|
|
38
54
|
}
|
|
39
|
-
|
|
40
|
-
|
|
55
|
+
this.events.get(event).add(onceWrapper);
|
|
56
|
+
if (!this.onceWrappers.has(event)) {
|
|
57
|
+
this.onceWrappers.set(event, new Map());
|
|
41
58
|
}
|
|
59
|
+
this.onceWrappers.get(event).set(fn, onceWrapper);
|
|
42
60
|
return this;
|
|
43
61
|
}
|
|
44
62
|
/**
|
|
45
63
|
* Emit an event with typed arguments.
|
|
64
|
+
* Individual listener errors are isolated so one faulty handler cannot
|
|
65
|
+
* break subsequent listeners or internal state transitions.
|
|
46
66
|
*/
|
|
47
67
|
emit(event, ...args) {
|
|
48
68
|
const callbacks = this.events.get(event);
|
|
49
|
-
if (callbacks) {
|
|
50
|
-
|
|
51
|
-
|
|
69
|
+
if (!callbacks) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
const callbackArray = Array.from(callbacks);
|
|
73
|
+
for (const callback of callbackArray) {
|
|
74
|
+
try {
|
|
52
75
|
callback(...args);
|
|
53
76
|
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
this.handleListenerError(event, error);
|
|
79
|
+
}
|
|
54
80
|
}
|
|
55
81
|
}
|
|
56
82
|
/**
|
|
@@ -59,9 +85,14 @@ export class EventEmitter {
|
|
|
59
85
|
removeAllListeners(event) {
|
|
60
86
|
if (event) {
|
|
61
87
|
this.events.delete(event);
|
|
88
|
+
this.onceWrappers.delete(event);
|
|
62
89
|
}
|
|
63
90
|
else {
|
|
64
91
|
this.events.clear();
|
|
92
|
+
this.onceWrappers.clear();
|
|
65
93
|
}
|
|
66
94
|
}
|
|
95
|
+
handleListenerError(_event, _error) {
|
|
96
|
+
// Subclasses can override to route listener errors; default isolates safely.
|
|
97
|
+
}
|
|
67
98
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EventEmitter } from "./EventEmitter";
|
|
2
2
|
import { AvesError } from "./AvesError";
|
|
3
|
-
import { JoinRoomResult, Participant, ReconnectConfig } from "../types/types";
|
|
3
|
+
import { CreateRoomOptions, JoinRoomOptions, JoinRoomResult, Participant, ReconnectConfig } from "../types/types";
|
|
4
4
|
type SignalingClientEvents = {
|
|
5
5
|
stateChange: [state: string];
|
|
6
6
|
error: [error: AvesError];
|
|
@@ -11,8 +11,8 @@ type SignalingClientEvents = {
|
|
|
11
11
|
iceCandidate: [fromId: string, candidate: RTCIceCandidateInit];
|
|
12
12
|
};
|
|
13
13
|
/**
|
|
14
|
-
* SignalingClient manages WebSocket connection
|
|
15
|
-
*
|
|
14
|
+
* SignalingClient manages WebSocket connection, optional token auth handshake,
|
|
15
|
+
* heartbeat liveness, exponential backoff reconnection, and signaling messages.
|
|
16
16
|
*/
|
|
17
17
|
export declare class SignalingClient extends EventEmitter<SignalingClientEvents> {
|
|
18
18
|
private ws;
|
|
@@ -20,92 +20,87 @@ export declare class SignalingClient extends EventEmitter<SignalingClientEvents>
|
|
|
20
20
|
private requestTimeoutMs;
|
|
21
21
|
private reconnectAttempts;
|
|
22
22
|
private reconnectTimer;
|
|
23
|
+
private stableResetTimer;
|
|
24
|
+
private heartbeatTimer;
|
|
25
|
+
private heartbeatTimeoutTimer;
|
|
23
26
|
private url;
|
|
24
27
|
private shouldReconnect;
|
|
28
|
+
private authenticated;
|
|
25
29
|
private pendingRequests;
|
|
26
30
|
private requestCounter;
|
|
27
31
|
constructor(reconnectConfig: ReconnectConfig);
|
|
28
32
|
/**
|
|
29
|
-
*
|
|
30
|
-
|
|
31
|
-
|
|
33
|
+
* Update auth token used for subsequent connections/reconnects.
|
|
34
|
+
*/
|
|
35
|
+
setAuthToken(token?: string): void;
|
|
36
|
+
/**
|
|
37
|
+
* Connect to the signaling server.
|
|
38
|
+
* Cleans up any stale socket or pending reconnect timer before opening a new one.
|
|
32
39
|
*/
|
|
33
40
|
connect(url: string): Promise<void>;
|
|
34
41
|
/**
|
|
35
|
-
* Disconnect from the signaling server
|
|
42
|
+
* Disconnect from the signaling server.
|
|
36
43
|
*/
|
|
37
44
|
disconnect(): void;
|
|
38
45
|
isConnected(): boolean;
|
|
46
|
+
private scheduleStableReconnectReset;
|
|
47
|
+
private clearStableResetTimer;
|
|
48
|
+
/**
|
|
49
|
+
* Calculate reconnect delay with exponential backoff and optional jitter.
|
|
50
|
+
*/
|
|
51
|
+
computeReconnectDelay(attempt: number): number;
|
|
52
|
+
private authenticate;
|
|
53
|
+
private cleanupSocket;
|
|
54
|
+
private clearReconnectTimer;
|
|
55
|
+
private startHeartbeat;
|
|
56
|
+
private stopHeartbeat;
|
|
57
|
+
private handlePong;
|
|
39
58
|
/**
|
|
40
|
-
* Handle WebSocket disconnection and attempt reconnection
|
|
59
|
+
* Handle WebSocket disconnection and attempt reconnection.
|
|
41
60
|
*/
|
|
42
61
|
private handleDisconnect;
|
|
43
62
|
/**
|
|
44
|
-
* Handle incoming WebSocket messages
|
|
45
|
-
* @param data - Raw message data
|
|
63
|
+
* Handle incoming WebSocket messages.
|
|
46
64
|
*/
|
|
47
65
|
private handleMessage;
|
|
48
66
|
/**
|
|
49
|
-
* Send a message to the signaling server
|
|
50
|
-
* @param message - Signaling message to send
|
|
67
|
+
* Send a message to the signaling server.
|
|
51
68
|
*/
|
|
52
69
|
private send;
|
|
53
70
|
/**
|
|
54
|
-
* Create a new room
|
|
55
|
-
* @returns Promise that resolves with the room ID
|
|
71
|
+
* Create a new room with optional name, maxCapacity, and password.
|
|
56
72
|
*/
|
|
57
|
-
createRoom(): Promise<string>;
|
|
73
|
+
createRoom(options?: CreateRoomOptions): Promise<string>;
|
|
58
74
|
/**
|
|
59
|
-
* Join an existing room
|
|
60
|
-
*
|
|
61
|
-
* @param userId - User ID
|
|
62
|
-
* @param userName - User name
|
|
63
|
-
* @returns Promise that resolves with the list of participants
|
|
75
|
+
* Join an existing room.
|
|
76
|
+
* Supports optional userId and password (via options object or legacy overload).
|
|
64
77
|
*/
|
|
65
|
-
joinRoom(roomId: string, userName: string): Promise<JoinRoomResult>;
|
|
66
|
-
joinRoom(roomId: string, userId: string, userName: string): Promise<JoinRoomResult>;
|
|
78
|
+
joinRoom(roomId: string, userName: string, options?: JoinRoomOptions): Promise<JoinRoomResult>;
|
|
79
|
+
joinRoom(roomId: string, userId: string, userName: string, password?: string): Promise<JoinRoomResult>;
|
|
67
80
|
/**
|
|
68
|
-
* Leave the current room
|
|
81
|
+
* Leave the current room.
|
|
69
82
|
*/
|
|
70
83
|
leaveRoom(userId: string): Promise<void>;
|
|
71
84
|
/**
|
|
72
|
-
* Send an offer to a peer
|
|
73
|
-
* @param targetId - Target peer ID
|
|
74
|
-
* @param fromId - Sender ID
|
|
75
|
-
* @param offer - RTC session description (offer)
|
|
85
|
+
* Send an offer to a peer.
|
|
76
86
|
*/
|
|
77
87
|
sendOffer(targetId: string, fromId: string, offer: RTCSessionDescriptionInit): void;
|
|
78
88
|
/**
|
|
79
|
-
* Send an answer to a peer
|
|
80
|
-
* @param targetId - Target peer ID
|
|
81
|
-
* @param fromId - Sender ID
|
|
82
|
-
* @param answer - RTC session description (answer)
|
|
89
|
+
* Send an answer to a peer.
|
|
83
90
|
*/
|
|
84
91
|
sendAnswer(targetId: string, fromId: string, answer: RTCSessionDescriptionInit): void;
|
|
85
92
|
/**
|
|
86
|
-
* Send an ICE candidate to a peer
|
|
87
|
-
* @param targetId - Target peer ID
|
|
88
|
-
* @param fromId - Sender ID
|
|
89
|
-
* @param candidate - ICE candidate
|
|
93
|
+
* Send an ICE candidate to a peer.
|
|
90
94
|
*/
|
|
91
95
|
sendIceCandidate(targetId: string, fromId: string, candidate: RTCIceCandidateInit): void;
|
|
92
96
|
/**
|
|
93
97
|
* Resolve a pending request by its requestId.
|
|
94
|
-
* If the server response omitted the requestId, we reject all pending
|
|
95
|
-
* requests of that type rather than guessing which one to resolve.
|
|
96
98
|
*/
|
|
97
99
|
private resolvePendingRequest;
|
|
98
100
|
private handleErrorMessage;
|
|
99
101
|
private trackPendingRequest;
|
|
100
102
|
private deletePendingRequest;
|
|
101
|
-
/**
|
|
102
|
-
* Reject all pending requests
|
|
103
|
-
* @param error - Error to reject with
|
|
104
|
-
*/
|
|
105
103
|
private rejectPendingRequests;
|
|
106
|
-
/**
|
|
107
|
-
* Reject all pending requests of a specific type.
|
|
108
|
-
*/
|
|
109
104
|
private rejectPendingRequestsByType;
|
|
110
105
|
private createRequestId;
|
|
111
106
|
}
|