@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
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# Aves Core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Production-grade browser WebRTC client library for peer-to-peer rooms. `@yrzhao/aves-core` connects to an `@yrzhao/aves-node` signaling server, builds a full-mesh WebRTC topology with Perfect Negotiation, ICE candidate buffering, backpressured file transfer with SHA-256 verification, voice/video/screen sharing, RTCStats observability, and resilient reconnection.
|
|
4
4
|
|
|
5
|
-
Version: `1.
|
|
5
|
+
Version: `1.2.0`
|
|
6
6
|
|
|
7
7
|
## Install
|
|
8
8
|
|
|
@@ -17,6 +17,7 @@ import { AvesClient } from "@yrzhao/aves-core";
|
|
|
17
17
|
|
|
18
18
|
const client = new AvesClient({
|
|
19
19
|
signalingUrl: "wss://signal.example.com",
|
|
20
|
+
authToken: "optional-jwt-token",
|
|
20
21
|
iceServers: [
|
|
21
22
|
{ urls: "stun:stun.l.google.com:19302" },
|
|
22
23
|
{
|
|
@@ -25,6 +26,8 @@ const client = new AvesClient({
|
|
|
25
26
|
credential: "turn-password",
|
|
26
27
|
},
|
|
27
28
|
],
|
|
29
|
+
heartbeatIntervalMs: 25_000,
|
|
30
|
+
iceDisconnectGraceMs: 5_000,
|
|
28
31
|
});
|
|
29
32
|
|
|
30
33
|
client.on("message", (peerId, message) => {
|
|
@@ -35,39 +38,34 @@ client.on("error", (error) => {
|
|
|
35
38
|
console.error(error.code, error.stage, error.message);
|
|
36
39
|
});
|
|
37
40
|
|
|
38
|
-
const roomId = await client.createRoom(
|
|
39
|
-
|
|
41
|
+
const roomId = await client.createRoom({
|
|
42
|
+
name: "Team Sync",
|
|
43
|
+
maxCapacity: 6,
|
|
44
|
+
password: "room-secret",
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
await client.joinRoom(roomId, "Alice", { password: "room-secret" });
|
|
40
48
|
|
|
41
|
-
await client.waitForPeer("peer-user-id");
|
|
49
|
+
await client.waitForPeer("peer-user-id", { requireFileChannel: true });
|
|
42
50
|
client.sendMessage({ text: "hello" });
|
|
43
51
|
```
|
|
44
52
|
|
|
45
|
-
##
|
|
46
|
-
|
|
47
|
-
- Creates and joins signaling rooms through `aves-node`.
|
|
48
|
-
- Builds direct WebRTC peer connections between every participant in a room.
|
|
49
|
-
- Sends JSON messages over a data channel.
|
|
50
|
-
- Sends files over a dedicated file data channel.
|
|
51
|
-
- Captures and forwards microphone, camera, and screen-share tracks.
|
|
52
|
-
- Restores room membership after transient signaling disconnects.
|
|
53
|
-
- Emits typed events for application state and errors.
|
|
54
|
-
- Exposes connection snapshots for diagnostics panels and smoke tests.
|
|
53
|
+
## Key Capabilities
|
|
55
54
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
-
|
|
59
|
-
-
|
|
60
|
-
-
|
|
61
|
-
-
|
|
62
|
-
- It is not an SFU. Full-mesh rooms are best for small groups.
|
|
55
|
+
- **Perfect Negotiation & Unified Plan**: Polite/impolite glare resolution, dynamic `onnegotiationneeded` renegotiation, and answer-side transceiver binding without orphan m-lines.
|
|
56
|
+
- **ICE Resilience**: Early ICE candidate queue buffering (flushed once `remoteDescription` is applied), transient `disconnected` grace window with automatic ICE restart, and dynamic `updateIceServers()` for ephemeral TURN credential rotation.
|
|
57
|
+
- **Reliable File Transfer**: DataChannel `bufferedAmount` backpressure flow control, configurable `maxReceiveFileSize`, incoming transfer accept/reject hook (`onIncomingTransfer`), byte-count and SHA-256 integrity checks, parallel multi-peer broadcast, and `cancelFileTransfer()`.
|
|
58
|
+
- **Media & Devices**: Voice (with echoCancellation/noiseSuppression constraints), camera video, screen sharing (with optional system audio), live `switchAudioDevice()` / `switchVideoDevice()`, and `remoteTrackEnded` cleanup events.
|
|
59
|
+
- **Observability**: Synchronous `getConnectionSnapshot()` plus async `getPeerStats()` / `getAllPeerStats()` wrapping `RTCPeerConnection.getStats()` (RTT, packet loss, bytes, candidate pair type).
|
|
60
|
+
- **Resilient Signaling**: Exponential backoff with jitter, optional token auth handshake, and optional application heartbeat (`ping` / `pong`).
|
|
63
61
|
|
|
64
62
|
## Core API
|
|
65
63
|
|
|
66
64
|
### Rooms
|
|
67
65
|
|
|
68
66
|
```ts
|
|
69
|
-
const roomId = await client.createRoom();
|
|
70
|
-
const existingParticipants = await client.joinRoom(roomId, "Alice");
|
|
67
|
+
const roomId = await client.createRoom({ name: "Standup", maxCapacity: 5, password: "secret" });
|
|
68
|
+
const existingParticipants = await client.joinRoom(roomId, "Alice", { password: "secret" });
|
|
71
69
|
await client.leaveRoom();
|
|
72
70
|
client.destroy();
|
|
73
71
|
```
|
|
@@ -79,13 +77,14 @@ client.sendMessage({ kind: "chat", text: "hello" });
|
|
|
79
77
|
client.sendMessageToPeer(peerId, { kind: "private", text: "hi" });
|
|
80
78
|
```
|
|
81
79
|
|
|
82
|
-
### Peer Readiness
|
|
80
|
+
### Peer Readiness, Stats & TURN Rotation
|
|
83
81
|
|
|
84
82
|
```ts
|
|
85
|
-
await client.waitForPeer(peerId, { timeoutMs:
|
|
83
|
+
await client.waitForPeer(peerId, { timeoutMs: 10_000, requireFileChannel: true });
|
|
86
84
|
|
|
87
85
|
const snapshot = client.getConnectionSnapshot();
|
|
88
|
-
|
|
86
|
+
const stats = await client.getPeerStats(peerId);
|
|
87
|
+
client.updateIceServers(freshTurnServers);
|
|
89
88
|
```
|
|
90
89
|
|
|
91
90
|
### Files
|
|
@@ -97,22 +96,27 @@ await client.sendFile(file, {
|
|
|
97
96
|
});
|
|
98
97
|
|
|
99
98
|
client.on("fileTransferProgress", (_peerId, progress) => {
|
|
100
|
-
|
|
99
|
+
// progress.progress is already a percentage in the range 0..100
|
|
100
|
+
console.log(`${Math.round(progress.progress)}%`);
|
|
101
101
|
});
|
|
102
|
+
|
|
103
|
+
client.cancelFileTransfer(peerId);
|
|
102
104
|
```
|
|
103
105
|
|
|
104
106
|
### Media
|
|
105
107
|
|
|
106
108
|
```ts
|
|
107
|
-
await client.startVoice();
|
|
109
|
+
await client.startVoice({ echoCancellation: true, noiseSuppression: true });
|
|
110
|
+
await client.switchAudioDevice("mic-device-id");
|
|
108
111
|
client.setMuted(true);
|
|
109
112
|
client.stopVoice();
|
|
110
113
|
|
|
111
114
|
await client.startVideo({ width: 1280, height: 720, frameRate: 30 });
|
|
115
|
+
await client.switchVideoDevice("camera-device-id");
|
|
112
116
|
client.setVideoMuted(true);
|
|
113
117
|
client.stopVideo();
|
|
114
118
|
|
|
115
|
-
await client.startScreenShare();
|
|
119
|
+
await client.startScreenShare({ audio: true });
|
|
116
120
|
client.stopScreenShare();
|
|
117
121
|
```
|
|
118
122
|
|
|
@@ -121,12 +125,28 @@ client.stopScreenShare();
|
|
|
121
125
|
```ts
|
|
122
126
|
new AvesClient({
|
|
123
127
|
signalingUrl: "wss://signal.example.com",
|
|
128
|
+
authToken: "optional-jwt-token",
|
|
124
129
|
iceServers: [{ urls: "stun:stun.l.google.com:19302" }],
|
|
130
|
+
rtcConfiguration: { iceTransportPolicy: "all", bundlePolicy: "max-bundle" },
|
|
131
|
+
heartbeatIntervalMs: 25_000,
|
|
132
|
+
heartbeatTimeoutMs: 10_000,
|
|
133
|
+
iceDisconnectGraceMs: 5_000,
|
|
125
134
|
fileChunkSize: 16 * 1024,
|
|
135
|
+
fileTransfer: {
|
|
136
|
+
maxReceiveFileSize: 100 * 1024 * 1024,
|
|
137
|
+
highWaterMark: 256 * 1024,
|
|
138
|
+
lowWaterMark: 64 * 1024,
|
|
139
|
+
verifyChecksum: true,
|
|
140
|
+
onIncomingTransfer: (_peerId, info) => info.size <= 50 * 1024 * 1024,
|
|
141
|
+
},
|
|
142
|
+
audio: { echoCancellation: true, noiseSuppression: true },
|
|
126
143
|
video: { width: 1280, height: 720, frameRate: 30 },
|
|
127
144
|
reconnect: {
|
|
128
145
|
maxAttempts: 5,
|
|
129
146
|
delay: 3000,
|
|
147
|
+
maxDelay: 30000,
|
|
148
|
+
backoffFactor: 2,
|
|
149
|
+
jitter: true,
|
|
130
150
|
requestTimeoutMs: 30000,
|
|
131
151
|
},
|
|
132
152
|
debug: false,
|
|
@@ -142,8 +162,10 @@ new AvesClient({
|
|
|
142
162
|
| `userLeft` | `(userId)` |
|
|
143
163
|
| `connectionStateChange` | `(peerId, state)` |
|
|
144
164
|
| `dataChannelStateChange` | `(peerId, state)` |
|
|
165
|
+
| `fileChannelStateChange` | `(peerId, state)` |
|
|
145
166
|
| `message` | `(peerId, message)` |
|
|
146
167
|
| `remoteAudioTrack` / `remoteVideoTrack` | `(peerId, stream, track)` |
|
|
168
|
+
| `remoteTrackEnded` | `(peerId, kind, track)` |
|
|
147
169
|
| `localAudioStateChange` / `localVideoStateChange` | `(state)` |
|
|
148
170
|
| `screenShareStateChange` | `(state)` |
|
|
149
171
|
| `fileTransferStarted` / `fileTransferProgress` / `fileTransferCompleted` / `fileTransferFailed` | transfer lifecycle payloads |
|
|
@@ -152,15 +174,8 @@ new AvesClient({
|
|
|
152
174
|
## Production Notes
|
|
153
175
|
|
|
154
176
|
- Use HTTPS for the web app and WSS for signaling.
|
|
155
|
-
- Configure TURN for restrictive NATs, enterprise networks, and mobile carriers
|
|
156
|
-
-
|
|
157
|
-
-
|
|
158
|
-
- Test Safari and mobile browsers separately if they are target platforms.
|
|
159
|
-
|
|
160
|
-
See [PRODUCTION.md](docs/PRODUCTION.md) and [TROUBLESHOOTING.md](docs/TROUBLESHOOTING.md).
|
|
161
|
-
|
|
162
|
-
## Browser Support
|
|
163
|
-
|
|
164
|
-
Modern Chromium, Firefox, and Safari versions with WebRTC, DataChannel, `getUserMedia`, and `getDisplayMedia` support. Screen sharing and media permissions vary by browser and platform.
|
|
177
|
+
- Configure TURN for restrictive NATs, enterprise networks, and mobile carriers; rotate short-lived credentials with `updateIceServers()`.
|
|
178
|
+
- Full mesh is best suited for small groups (typically 2–6 active video peers).
|
|
179
|
+
- See [API.md](docs/API.md), [FILE_PROTOCOL.md](docs/FILE_PROTOCOL.md), [PRODUCTION.md](docs/PRODUCTION.md), and [TROUBLESHOOTING.md](docs/TROUBLESHOOTING.md).
|
|
165
180
|
|
|
166
181
|
MIT
|
|
@@ -1,16 +1,20 @@
|
|
|
1
1
|
import { EventEmitter } from "./EventEmitter";
|
|
2
2
|
import { AvesError } from "./AvesError";
|
|
3
|
-
import { AvesClientConfig, AvesConnectionSnapshot, AvesMessage, AvesPeerSnapshot, AvesVideoConstraints, FileTransferInfo, FileTransferOptions, FileTransferProgress, FileTransferResult, LocalAudioState, LocalVideoState, Participant, ScreenShareState, WaitForPeerOptions } from "../types/types";
|
|
3
|
+
import { AvesAudioConstraints, AvesClientConfig, AvesConnectionSnapshot, AvesMessage, AvesPeerSnapshot, AvesPeerStats, AvesVideoConstraints, CreateRoomOptions, FileTransferInfo, FileTransferOptions, FileTransferProgress, FileTransferResult, JoinRoomOptions, LocalAudioState, LocalVideoState, Participant, ScreenShareOptions, ScreenShareState, WaitForPeerOptions } from "../types/types";
|
|
4
4
|
export type AvesClientEvents = {
|
|
5
5
|
signalingStateChange: [state: string];
|
|
6
|
+
sessionRestored: [participants: Participant[]];
|
|
6
7
|
error: [error: AvesError];
|
|
7
8
|
userJoined: [user: Participant];
|
|
8
9
|
userLeft: [userId: string];
|
|
9
10
|
connectionStateChange: [peerId: string, state: RTCPeerConnectionState];
|
|
10
11
|
dataChannelStateChange: [peerId: string, state: RTCDataChannelState];
|
|
12
|
+
fileChannelStateChange: [peerId: string, state: RTCDataChannelState];
|
|
11
13
|
message: [peerId: string, message: AvesMessage];
|
|
12
14
|
remoteAudioTrack: [peerId: string, stream: MediaStream, track: MediaStreamTrack];
|
|
13
15
|
remoteVideoTrack: [peerId: string, stream: MediaStream, track: MediaStreamTrack];
|
|
16
|
+
remoteTrackEnded: [peerId: string, kind: "audio" | "video", track: MediaStreamTrack];
|
|
17
|
+
remoteTrackMuted: [peerId: string, kind: "audio" | "video", muted: boolean, track: MediaStreamTrack];
|
|
14
18
|
localAudioStateChange: [state: LocalAudioState];
|
|
15
19
|
localVideoStateChange: [state: LocalVideoState];
|
|
16
20
|
screenShareStateChange: [state: ScreenShareState];
|
|
@@ -20,7 +24,7 @@ export type AvesClientEvents = {
|
|
|
20
24
|
fileTransferFailed: [peerId: string, info: FileTransferInfo | null, error: AvesError];
|
|
21
25
|
};
|
|
22
26
|
/**
|
|
23
|
-
* AvesClient - Main client
|
|
27
|
+
* AvesClient - Main browser WebRTC client for P2P rooms.
|
|
24
28
|
*/
|
|
25
29
|
export declare class AvesClient extends EventEmitter<AvesClientEvents> {
|
|
26
30
|
private config;
|
|
@@ -29,35 +33,55 @@ export declare class AvesClient extends EventEmitter<AvesClientEvents> {
|
|
|
29
33
|
private currentRoomId;
|
|
30
34
|
private currentUserId;
|
|
31
35
|
private currentUserName;
|
|
36
|
+
private currentRoomPassword;
|
|
37
|
+
private currentReconnectToken;
|
|
32
38
|
private participants;
|
|
33
|
-
private
|
|
39
|
+
private preparedPeerConnections;
|
|
34
40
|
private shouldRestoreSession;
|
|
35
41
|
constructor(config: AvesClientConfig);
|
|
42
|
+
private isPoliteForPeer;
|
|
36
43
|
private setupEventHandlers;
|
|
37
44
|
private restoreRoomSession;
|
|
38
45
|
private preparePeerConnection;
|
|
39
46
|
private initiateConnection;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Update authentication token for subsequent signaling connections.
|
|
49
|
+
*/
|
|
50
|
+
setAuthToken(token?: string): void;
|
|
51
|
+
/**
|
|
52
|
+
* Update ICE/TURN servers dynamically (e.g. rotating time-limited TURN credentials).
|
|
53
|
+
*/
|
|
54
|
+
updateIceServers(iceServers: RTCIceServer[]): void;
|
|
55
|
+
/**
|
|
56
|
+
* Create a new room with optional name, capacity, and password.
|
|
57
|
+
*/
|
|
58
|
+
createRoom(options?: CreateRoomOptions): Promise<string>;
|
|
59
|
+
joinRoom(roomId: string, userName: string, options?: JoinRoomOptions): Promise<Participant[]>;
|
|
60
|
+
joinRoom(roomId: string, userId: string, userName: string, password?: string): Promise<Participant[]>;
|
|
43
61
|
leaveRoom(): Promise<void>;
|
|
44
62
|
sendMessage(message: AvesMessage): void;
|
|
45
63
|
sendMessageToPeer(peerId: string, message: AvesMessage): void;
|
|
46
64
|
sendFile(blob: Blob, options?: FileTransferOptions): Promise<FileTransferInfo[]>;
|
|
47
|
-
|
|
65
|
+
cancelFileTransfer(peerId: string, transferId?: string): boolean;
|
|
66
|
+
startVoice(constraints?: AvesAudioConstraints): Promise<MediaStream>;
|
|
67
|
+
switchAudioDevice(deviceId: string): Promise<MediaStream>;
|
|
48
68
|
stopVoice(): void;
|
|
49
69
|
setMuted(muted: boolean): void;
|
|
50
70
|
getLocalAudioState(): LocalAudioState;
|
|
51
71
|
getRemoteAudioStream(peerId: string): MediaStream | null;
|
|
52
72
|
startVideo(constraints?: AvesVideoConstraints): Promise<MediaStream>;
|
|
73
|
+
switchVideoDevice(deviceId: string): Promise<MediaStream>;
|
|
53
74
|
stopVideo(): void;
|
|
54
75
|
setVideoMuted(muted: boolean): void;
|
|
55
76
|
getLocalVideoState(): LocalVideoState;
|
|
56
77
|
getRemoteVideoStream(peerId: string): MediaStream | null;
|
|
57
|
-
startScreenShare(): Promise<MediaStream>;
|
|
78
|
+
startScreenShare(options?: ScreenShareOptions): Promise<MediaStream>;
|
|
58
79
|
stopScreenShare(): void;
|
|
59
80
|
getScreenShareState(): ScreenShareState;
|
|
60
81
|
getConnectionState(peerId: string): RTCPeerConnectionState;
|
|
82
|
+
getPeerStats(peerId: string): Promise<AvesPeerStats | null>;
|
|
83
|
+
getAllPeerStats(): Promise<AvesPeerStats[]>;
|
|
84
|
+
restartPeerIce(peerId: string): Promise<void>;
|
|
61
85
|
getConnectionSnapshot(): AvesConnectionSnapshot;
|
|
62
86
|
waitForPeer(peerId: string, options?: WaitForPeerOptions): Promise<AvesPeerSnapshot>;
|
|
63
87
|
getParticipants(): Participant[];
|