@yrzhao/aves-core 1.0.1 → 1.1.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 +108 -67
- package/dist/core/AvesClient.d.ts +4 -1
- package/dist/core/AvesClient.js +100 -0
- package/dist/core/WebRTCManager.d.ts +4 -0
- package/dist/core/WebRTCManager.js +7 -0
- package/dist/esm/core/AvesClient.d.ts +4 -1
- package/dist/esm/core/AvesClient.js +100 -0
- package/dist/esm/core/WebRTCManager.d.ts +4 -0
- package/dist/esm/core/WebRTCManager.js +7 -0
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/types/types.d.ts +20 -0
- package/dist/index.d.ts +1 -1
- package/dist/types/types.d.ts +20 -0
- package/package.json +18 -4
package/README.md
CHANGED
|
@@ -1,125 +1,166 @@
|
|
|
1
1
|
# Aves Core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Browser WebRTC client library for peer-to-peer rooms. `@yrzhao/aves-core` connects to an `aves-node` signaling server, builds a full-mesh WebRTC topology, and provides typed APIs for messages, file transfer, voice, video, screen sharing, reconnect restore, and diagnostics.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Version: `1.1.0`
|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## Install
|
|
8
8
|
|
|
9
9
|
```bash
|
|
10
10
|
npm install @yrzhao/aves-core
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
##
|
|
13
|
+
## Quick Start
|
|
14
14
|
|
|
15
|
-
```
|
|
15
|
+
```ts
|
|
16
16
|
import { AvesClient } from "@yrzhao/aves-core";
|
|
17
17
|
|
|
18
|
-
const client = new AvesClient({
|
|
18
|
+
const client = new AvesClient({
|
|
19
|
+
signalingUrl: "wss://signal.example.com",
|
|
20
|
+
iceServers: [
|
|
21
|
+
{ urls: "stun:stun.l.google.com:19302" },
|
|
22
|
+
{
|
|
23
|
+
urls: "turn:turn.example.com:3478",
|
|
24
|
+
username: "turn-user",
|
|
25
|
+
credential: "turn-password",
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
});
|
|
19
29
|
|
|
20
|
-
client.on("message", (peerId, message) =>
|
|
21
|
-
|
|
30
|
+
client.on("message", (peerId, message) => {
|
|
31
|
+
console.log("message from", peerId, message);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
client.on("error", (error) => {
|
|
35
|
+
console.error(error.code, error.stage, error.message);
|
|
36
|
+
});
|
|
22
37
|
|
|
23
38
|
const roomId = await client.createRoom();
|
|
24
39
|
await client.joinRoom(roomId, "Alice");
|
|
25
40
|
|
|
26
|
-
client.
|
|
27
|
-
|
|
41
|
+
await client.waitForPeer("peer-user-id");
|
|
42
|
+
client.sendMessage({ text: "hello" });
|
|
28
43
|
```
|
|
29
44
|
|
|
30
|
-
##
|
|
45
|
+
## What It Does
|
|
31
46
|
|
|
32
|
-
|
|
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.
|
|
33
55
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
56
|
+
## What It Does Not Do
|
|
57
|
+
|
|
58
|
+
- It does not host a signaling server. Use `@yrzhao/aves-node`.
|
|
59
|
+
- It does not relay media or data traffic. Peers communicate directly.
|
|
60
|
+
- It does not replace TURN. Production networks still need TURN fallback.
|
|
61
|
+
- It does not encrypt beyond WebRTC/WebSocket transport guarantees. Use HTTPS/WSS.
|
|
62
|
+
- It is not an SFU. Full-mesh rooms are best for small groups.
|
|
63
|
+
|
|
64
|
+
## Core API
|
|
65
|
+
|
|
66
|
+
### Rooms
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
const roomId = await client.createRoom();
|
|
70
|
+
const existingParticipants = await client.joinRoom(roomId, "Alice");
|
|
71
|
+
await client.leaveRoom();
|
|
72
|
+
client.destroy();
|
|
73
|
+
```
|
|
40
74
|
|
|
41
|
-
###
|
|
75
|
+
### Messages
|
|
42
76
|
|
|
43
|
-
```
|
|
44
|
-
client.sendMessage({ text: "
|
|
45
|
-
client.sendMessageToPeer(peerId, { text: "hi" });
|
|
77
|
+
```ts
|
|
78
|
+
client.sendMessage({ kind: "chat", text: "hello" });
|
|
79
|
+
client.sendMessageToPeer(peerId, { kind: "private", text: "hi" });
|
|
46
80
|
```
|
|
47
81
|
|
|
48
|
-
###
|
|
82
|
+
### Peer Readiness And Diagnostics
|
|
49
83
|
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
await client.sendFile(fileBlob, { fileName: "photo.jpg" });
|
|
84
|
+
```ts
|
|
85
|
+
await client.waitForPeer(peerId, { timeoutMs: 10000 });
|
|
53
86
|
|
|
54
|
-
|
|
55
|
-
|
|
87
|
+
const snapshot = client.getConnectionSnapshot();
|
|
88
|
+
console.log(snapshot.signalingConnected, snapshot.peers);
|
|
89
|
+
```
|
|
56
90
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
91
|
+
### Files
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
await client.sendFile(file, {
|
|
95
|
+
fileName: file.name,
|
|
96
|
+
mimeType: file.type || "application/octet-stream",
|
|
60
97
|
});
|
|
61
|
-
|
|
62
|
-
|
|
98
|
+
|
|
99
|
+
client.on("fileTransferProgress", (_peerId, progress) => {
|
|
100
|
+
console.log(Math.round(progress.progress * 100));
|
|
63
101
|
});
|
|
64
102
|
```
|
|
65
103
|
|
|
66
|
-
###
|
|
104
|
+
### Media
|
|
67
105
|
|
|
68
|
-
```
|
|
106
|
+
```ts
|
|
69
107
|
await client.startVoice();
|
|
70
108
|
client.setMuted(true);
|
|
71
109
|
client.stopVoice();
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
### 视频
|
|
75
110
|
|
|
76
|
-
```typescript
|
|
77
111
|
await client.startVideo({ width: 1280, height: 720, frameRate: 30 });
|
|
78
112
|
client.setVideoMuted(true);
|
|
79
113
|
client.stopVideo();
|
|
80
|
-
```
|
|
81
114
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
```typescript
|
|
85
|
-
await client.startScreenShare(); // 自动恢复摄像头
|
|
115
|
+
await client.startScreenShare();
|
|
86
116
|
client.stopScreenShare();
|
|
87
117
|
```
|
|
88
118
|
|
|
89
|
-
##
|
|
119
|
+
## Configuration
|
|
90
120
|
|
|
91
|
-
```
|
|
121
|
+
```ts
|
|
92
122
|
new AvesClient({
|
|
93
|
-
signalingUrl: "
|
|
123
|
+
signalingUrl: "wss://signal.example.com",
|
|
94
124
|
iceServers: [{ urls: "stun:stun.l.google.com:19302" }],
|
|
95
|
-
fileChunkSize:
|
|
96
|
-
|
|
125
|
+
fileChunkSize: 16 * 1024,
|
|
126
|
+
video: { width: 1280, height: 720, frameRate: 30 },
|
|
127
|
+
reconnect: {
|
|
128
|
+
maxAttempts: 5,
|
|
129
|
+
delay: 3000,
|
|
130
|
+
requestTimeoutMs: 30000,
|
|
131
|
+
},
|
|
97
132
|
debug: false,
|
|
98
133
|
});
|
|
99
134
|
```
|
|
100
135
|
|
|
101
|
-
##
|
|
136
|
+
## Events
|
|
137
|
+
|
|
138
|
+
| Event | Arguments |
|
|
139
|
+
| --- | --- |
|
|
140
|
+
| `signalingStateChange` | `(state)` |
|
|
141
|
+
| `userJoined` | `(participant)` |
|
|
142
|
+
| `userLeft` | `(userId)` |
|
|
143
|
+
| `connectionStateChange` | `(peerId, state)` |
|
|
144
|
+
| `dataChannelStateChange` | `(peerId, state)` |
|
|
145
|
+
| `message` | `(peerId, message)` |
|
|
146
|
+
| `remoteAudioTrack` / `remoteVideoTrack` | `(peerId, stream, track)` |
|
|
147
|
+
| `localAudioStateChange` / `localVideoStateChange` | `(state)` |
|
|
148
|
+
| `screenShareStateChange` | `(state)` |
|
|
149
|
+
| `fileTransferStarted` / `fileTransferProgress` / `fileTransferCompleted` / `fileTransferFailed` | transfer lifecycle payloads |
|
|
150
|
+
| `error` | `(AvesError)` |
|
|
102
151
|
|
|
103
|
-
|
|
104
|
-
|------|------|------|
|
|
105
|
-
| `message` | `(peerId, message)` | 收到消息 |
|
|
106
|
-
| `userJoined` / `userLeft` | `(user)` / `(userId)` | 用户进出 |
|
|
107
|
-
| `connectionStateChange` | `(peerId, state)` | 连接状态 |
|
|
108
|
-
| `localAudioStateChange` | `(state)` | 本地音频状态 |
|
|
109
|
-
| `localVideoStateChange` | `(state)` | 本地视频状态 |
|
|
110
|
-
| `screenShareStateChange` | `(state)` | 屏幕共享状态 |
|
|
111
|
-
| `fileTransferStarted/Progress/Completed/Failed` | — | 文件传输各阶段 |
|
|
112
|
-
| `remoteAudioTrack` / `remoteVideoTrack` | `(peerId, stream, track)` | 收到远端媒体 |
|
|
113
|
-
| `error` | `(error)` | 错误 |
|
|
152
|
+
## Production Notes
|
|
114
153
|
|
|
115
|
-
|
|
154
|
+
- Use HTTPS for the web app and WSS for signaling.
|
|
155
|
+
- Configure TURN for restrictive NATs, enterprise networks, and mobile carriers.
|
|
156
|
+
- Keep rooms small. Full mesh requires each peer to maintain one connection to every other peer.
|
|
157
|
+
- Prefer small to medium files over data channels. Very large files need application-level resume/retry UX.
|
|
158
|
+
- Test Safari and mobile browsers separately if they are target platforms.
|
|
116
159
|
|
|
117
|
-
|
|
160
|
+
See [PRODUCTION.md](docs/PRODUCTION.md) and [TROUBLESHOOTING.md](docs/TROUBLESHOOTING.md).
|
|
118
161
|
|
|
119
|
-
##
|
|
162
|
+
## Browser Support
|
|
120
163
|
|
|
121
|
-
|
|
122
|
-
- 全网状拓扑:O(n) 连接数
|
|
123
|
-
- 零运行时依赖
|
|
164
|
+
Modern Chromium, Firefox, and Safari versions with WebRTC, DataChannel, `getUserMedia`, and `getDisplayMedia` support. Screen sharing and media permissions vary by browser and platform.
|
|
124
165
|
|
|
125
166
|
MIT
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EventEmitter } from "./EventEmitter";
|
|
2
2
|
import { AvesError } from "./AvesError";
|
|
3
|
-
import { AvesClientConfig, AvesMessage, AvesVideoConstraints, FileTransferInfo, FileTransferOptions, FileTransferProgress, FileTransferResult, LocalAudioState, LocalVideoState, Participant, ScreenShareState } from "../types/types";
|
|
3
|
+
import { AvesClientConfig, AvesConnectionSnapshot, AvesMessage, AvesPeerSnapshot, AvesVideoConstraints, FileTransferInfo, FileTransferOptions, FileTransferProgress, FileTransferResult, LocalAudioState, LocalVideoState, Participant, ScreenShareState, WaitForPeerOptions } from "../types/types";
|
|
4
4
|
export type AvesClientEvents = {
|
|
5
5
|
signalingStateChange: [state: string];
|
|
6
6
|
error: [error: AvesError];
|
|
@@ -58,7 +58,10 @@ export declare class AvesClient extends EventEmitter<AvesClientEvents> {
|
|
|
58
58
|
stopScreenShare(): void;
|
|
59
59
|
getScreenShareState(): ScreenShareState;
|
|
60
60
|
getConnectionState(peerId: string): RTCPeerConnectionState;
|
|
61
|
+
getConnectionSnapshot(): AvesConnectionSnapshot;
|
|
62
|
+
waitForPeer(peerId: string, options?: WaitForPeerOptions): Promise<AvesPeerSnapshot>;
|
|
61
63
|
getParticipants(): Participant[];
|
|
64
|
+
private createPeerSnapshot;
|
|
62
65
|
getCurrentUserId(): string | null;
|
|
63
66
|
isConnected(): boolean;
|
|
64
67
|
destroy(): void;
|
package/dist/core/AvesClient.js
CHANGED
|
@@ -299,9 +299,109 @@ class AvesClient extends EventEmitter_1.EventEmitter {
|
|
|
299
299
|
getConnectionState(peerId) {
|
|
300
300
|
return this.webrtcManager.getConnectionState(peerId);
|
|
301
301
|
}
|
|
302
|
+
getConnectionSnapshot() {
|
|
303
|
+
const participants = this.getParticipants();
|
|
304
|
+
const participantById = new Map(participants.map((participant) => [participant.id, participant]));
|
|
305
|
+
const peerIds = new Set([
|
|
306
|
+
...this.webrtcManager.getActivePeers(),
|
|
307
|
+
...participants.map((participant) => participant.id),
|
|
308
|
+
]);
|
|
309
|
+
if (this.currentUserId) {
|
|
310
|
+
peerIds.delete(this.currentUserId);
|
|
311
|
+
}
|
|
312
|
+
const peers = Array.from(peerIds).map((peerId) => this.createPeerSnapshot(peerId, participantById.get(peerId)));
|
|
313
|
+
return {
|
|
314
|
+
roomId: this.currentRoomId,
|
|
315
|
+
currentUserId: this.currentUserId,
|
|
316
|
+
signalingConnected: this.signalingClient.isConnected(),
|
|
317
|
+
participantCount: participants.length,
|
|
318
|
+
participants,
|
|
319
|
+
peers,
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
waitForPeer(peerId, options = {}) {
|
|
323
|
+
const timeoutMs = options.timeoutMs ?? 30000;
|
|
324
|
+
const getSnapshot = () => this.createPeerSnapshot(peerId, this.participants.get(peerId));
|
|
325
|
+
const isReady = () => {
|
|
326
|
+
const messageReady = this.webrtcManager.isDataChannelReady(peerId);
|
|
327
|
+
const fileReady = !options.requireFileChannel ||
|
|
328
|
+
this.webrtcManager.isFileChannelReady(peerId);
|
|
329
|
+
return messageReady && fileReady;
|
|
330
|
+
};
|
|
331
|
+
if (isReady()) {
|
|
332
|
+
return Promise.resolve(getSnapshot());
|
|
333
|
+
}
|
|
334
|
+
return new Promise((resolve, reject) => {
|
|
335
|
+
let timeoutId = null;
|
|
336
|
+
const cleanup = () => {
|
|
337
|
+
this.off("dataChannelStateChange", handleDataChannelStateChange);
|
|
338
|
+
this.off("connectionStateChange", handleConnectionStateChange);
|
|
339
|
+
if (timeoutId) {
|
|
340
|
+
clearTimeout(timeoutId);
|
|
341
|
+
timeoutId = null;
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
const resolveIfReady = () => {
|
|
345
|
+
if (!isReady()) {
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
cleanup();
|
|
349
|
+
resolve(getSnapshot());
|
|
350
|
+
};
|
|
351
|
+
const handleDataChannelStateChange = (changedPeerId) => {
|
|
352
|
+
if (changedPeerId === peerId) {
|
|
353
|
+
resolveIfReady();
|
|
354
|
+
}
|
|
355
|
+
};
|
|
356
|
+
const handleConnectionStateChange = (changedPeerId, state) => {
|
|
357
|
+
if (changedPeerId !== peerId) {
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
if (state === "failed" || state === "closed") {
|
|
361
|
+
cleanup();
|
|
362
|
+
reject(new AvesError_1.AvesError({
|
|
363
|
+
message: `Peer ${peerId} connection ${state}`,
|
|
364
|
+
code: "WEBRTC_CONNECTION_FAILED",
|
|
365
|
+
stage: "transport",
|
|
366
|
+
retryable: state === "failed",
|
|
367
|
+
peerId,
|
|
368
|
+
}));
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
resolveIfReady();
|
|
372
|
+
};
|
|
373
|
+
this.on("dataChannelStateChange", handleDataChannelStateChange);
|
|
374
|
+
this.on("connectionStateChange", handleConnectionStateChange);
|
|
375
|
+
timeoutId = setTimeout(() => {
|
|
376
|
+
cleanup();
|
|
377
|
+
reject(new AvesError_1.AvesError({
|
|
378
|
+
message: `Timed out waiting for peer ${peerId}`,
|
|
379
|
+
code: "WEBRTC_CONNECTION_FAILED",
|
|
380
|
+
stage: "transport",
|
|
381
|
+
retryable: true,
|
|
382
|
+
peerId,
|
|
383
|
+
}));
|
|
384
|
+
}, timeoutMs);
|
|
385
|
+
resolveIfReady();
|
|
386
|
+
});
|
|
387
|
+
}
|
|
302
388
|
getParticipants() {
|
|
303
389
|
return Array.from(this.participants.values());
|
|
304
390
|
}
|
|
391
|
+
createPeerSnapshot(peerId, participant) {
|
|
392
|
+
const messageChannelReady = this.webrtcManager.isDataChannelReady(peerId);
|
|
393
|
+
const fileChannelReady = this.webrtcManager.isFileChannelReady(peerId);
|
|
394
|
+
return {
|
|
395
|
+
peerId,
|
|
396
|
+
participant,
|
|
397
|
+
connectionState: this.webrtcManager.getConnectionState(peerId),
|
|
398
|
+
dataChannelState: messageChannelReady
|
|
399
|
+
? "open"
|
|
400
|
+
: this.webrtcManager.getDataChannelState(peerId),
|
|
401
|
+
messageChannelReady,
|
|
402
|
+
fileChannelReady,
|
|
403
|
+
};
|
|
404
|
+
}
|
|
305
405
|
getCurrentUserId() {
|
|
306
406
|
return this.currentUserId;
|
|
307
407
|
}
|
|
@@ -187,6 +187,10 @@ export declare class WebRTCManager {
|
|
|
187
187
|
* Get the actual RTCPeerConnectionState for a peer.
|
|
188
188
|
*/
|
|
189
189
|
getConnectionState(peerId: string): RTCPeerConnectionState;
|
|
190
|
+
/**
|
|
191
|
+
* Get the primary message DataChannel state for a peer.
|
|
192
|
+
*/
|
|
193
|
+
getDataChannelState(peerId: string): RTCDataChannelState | "closed";
|
|
190
194
|
/**
|
|
191
195
|
* Check if the main DataChannel is ready for a specific peer.
|
|
192
196
|
*/
|
|
@@ -380,6 +380,13 @@ class WebRTCManager {
|
|
|
380
380
|
const pc = this.peerConnections.get(peerId);
|
|
381
381
|
return pc?.connectionState ?? "closed";
|
|
382
382
|
}
|
|
383
|
+
/**
|
|
384
|
+
* Get the primary message DataChannel state for a peer.
|
|
385
|
+
*/
|
|
386
|
+
getDataChannelState(peerId) {
|
|
387
|
+
return ((0, channels_1.normalizePeerChannels)(this.dataChannels.get(peerId)).message
|
|
388
|
+
?.readyState ?? "closed");
|
|
389
|
+
}
|
|
383
390
|
/**
|
|
384
391
|
* Check if the main DataChannel is ready for a specific peer.
|
|
385
392
|
*/
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EventEmitter } from "./EventEmitter";
|
|
2
2
|
import { AvesError } from "./AvesError";
|
|
3
|
-
import { AvesClientConfig, AvesMessage, AvesVideoConstraints, FileTransferInfo, FileTransferOptions, FileTransferProgress, FileTransferResult, LocalAudioState, LocalVideoState, Participant, ScreenShareState } from "../types/types";
|
|
3
|
+
import { AvesClientConfig, AvesConnectionSnapshot, AvesMessage, AvesPeerSnapshot, AvesVideoConstraints, FileTransferInfo, FileTransferOptions, FileTransferProgress, FileTransferResult, LocalAudioState, LocalVideoState, Participant, ScreenShareState, WaitForPeerOptions } from "../types/types";
|
|
4
4
|
export type AvesClientEvents = {
|
|
5
5
|
signalingStateChange: [state: string];
|
|
6
6
|
error: [error: AvesError];
|
|
@@ -58,7 +58,10 @@ export declare class AvesClient extends EventEmitter<AvesClientEvents> {
|
|
|
58
58
|
stopScreenShare(): void;
|
|
59
59
|
getScreenShareState(): ScreenShareState;
|
|
60
60
|
getConnectionState(peerId: string): RTCPeerConnectionState;
|
|
61
|
+
getConnectionSnapshot(): AvesConnectionSnapshot;
|
|
62
|
+
waitForPeer(peerId: string, options?: WaitForPeerOptions): Promise<AvesPeerSnapshot>;
|
|
61
63
|
getParticipants(): Participant[];
|
|
64
|
+
private createPeerSnapshot;
|
|
62
65
|
getCurrentUserId(): string | null;
|
|
63
66
|
isConnected(): boolean;
|
|
64
67
|
destroy(): void;
|
|
@@ -296,9 +296,109 @@ export class AvesClient extends EventEmitter {
|
|
|
296
296
|
getConnectionState(peerId) {
|
|
297
297
|
return this.webrtcManager.getConnectionState(peerId);
|
|
298
298
|
}
|
|
299
|
+
getConnectionSnapshot() {
|
|
300
|
+
const participants = this.getParticipants();
|
|
301
|
+
const participantById = new Map(participants.map((participant) => [participant.id, participant]));
|
|
302
|
+
const peerIds = new Set([
|
|
303
|
+
...this.webrtcManager.getActivePeers(),
|
|
304
|
+
...participants.map((participant) => participant.id),
|
|
305
|
+
]);
|
|
306
|
+
if (this.currentUserId) {
|
|
307
|
+
peerIds.delete(this.currentUserId);
|
|
308
|
+
}
|
|
309
|
+
const peers = Array.from(peerIds).map((peerId) => this.createPeerSnapshot(peerId, participantById.get(peerId)));
|
|
310
|
+
return {
|
|
311
|
+
roomId: this.currentRoomId,
|
|
312
|
+
currentUserId: this.currentUserId,
|
|
313
|
+
signalingConnected: this.signalingClient.isConnected(),
|
|
314
|
+
participantCount: participants.length,
|
|
315
|
+
participants,
|
|
316
|
+
peers,
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
waitForPeer(peerId, options = {}) {
|
|
320
|
+
const timeoutMs = options.timeoutMs ?? 30000;
|
|
321
|
+
const getSnapshot = () => this.createPeerSnapshot(peerId, this.participants.get(peerId));
|
|
322
|
+
const isReady = () => {
|
|
323
|
+
const messageReady = this.webrtcManager.isDataChannelReady(peerId);
|
|
324
|
+
const fileReady = !options.requireFileChannel ||
|
|
325
|
+
this.webrtcManager.isFileChannelReady(peerId);
|
|
326
|
+
return messageReady && fileReady;
|
|
327
|
+
};
|
|
328
|
+
if (isReady()) {
|
|
329
|
+
return Promise.resolve(getSnapshot());
|
|
330
|
+
}
|
|
331
|
+
return new Promise((resolve, reject) => {
|
|
332
|
+
let timeoutId = null;
|
|
333
|
+
const cleanup = () => {
|
|
334
|
+
this.off("dataChannelStateChange", handleDataChannelStateChange);
|
|
335
|
+
this.off("connectionStateChange", handleConnectionStateChange);
|
|
336
|
+
if (timeoutId) {
|
|
337
|
+
clearTimeout(timeoutId);
|
|
338
|
+
timeoutId = null;
|
|
339
|
+
}
|
|
340
|
+
};
|
|
341
|
+
const resolveIfReady = () => {
|
|
342
|
+
if (!isReady()) {
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
cleanup();
|
|
346
|
+
resolve(getSnapshot());
|
|
347
|
+
};
|
|
348
|
+
const handleDataChannelStateChange = (changedPeerId) => {
|
|
349
|
+
if (changedPeerId === peerId) {
|
|
350
|
+
resolveIfReady();
|
|
351
|
+
}
|
|
352
|
+
};
|
|
353
|
+
const handleConnectionStateChange = (changedPeerId, state) => {
|
|
354
|
+
if (changedPeerId !== peerId) {
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
if (state === "failed" || state === "closed") {
|
|
358
|
+
cleanup();
|
|
359
|
+
reject(new AvesError({
|
|
360
|
+
message: `Peer ${peerId} connection ${state}`,
|
|
361
|
+
code: "WEBRTC_CONNECTION_FAILED",
|
|
362
|
+
stage: "transport",
|
|
363
|
+
retryable: state === "failed",
|
|
364
|
+
peerId,
|
|
365
|
+
}));
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
resolveIfReady();
|
|
369
|
+
};
|
|
370
|
+
this.on("dataChannelStateChange", handleDataChannelStateChange);
|
|
371
|
+
this.on("connectionStateChange", handleConnectionStateChange);
|
|
372
|
+
timeoutId = setTimeout(() => {
|
|
373
|
+
cleanup();
|
|
374
|
+
reject(new AvesError({
|
|
375
|
+
message: `Timed out waiting for peer ${peerId}`,
|
|
376
|
+
code: "WEBRTC_CONNECTION_FAILED",
|
|
377
|
+
stage: "transport",
|
|
378
|
+
retryable: true,
|
|
379
|
+
peerId,
|
|
380
|
+
}));
|
|
381
|
+
}, timeoutMs);
|
|
382
|
+
resolveIfReady();
|
|
383
|
+
});
|
|
384
|
+
}
|
|
299
385
|
getParticipants() {
|
|
300
386
|
return Array.from(this.participants.values());
|
|
301
387
|
}
|
|
388
|
+
createPeerSnapshot(peerId, participant) {
|
|
389
|
+
const messageChannelReady = this.webrtcManager.isDataChannelReady(peerId);
|
|
390
|
+
const fileChannelReady = this.webrtcManager.isFileChannelReady(peerId);
|
|
391
|
+
return {
|
|
392
|
+
peerId,
|
|
393
|
+
participant,
|
|
394
|
+
connectionState: this.webrtcManager.getConnectionState(peerId),
|
|
395
|
+
dataChannelState: messageChannelReady
|
|
396
|
+
? "open"
|
|
397
|
+
: this.webrtcManager.getDataChannelState(peerId),
|
|
398
|
+
messageChannelReady,
|
|
399
|
+
fileChannelReady,
|
|
400
|
+
};
|
|
401
|
+
}
|
|
302
402
|
getCurrentUserId() {
|
|
303
403
|
return this.currentUserId;
|
|
304
404
|
}
|
|
@@ -187,6 +187,10 @@ export declare class WebRTCManager {
|
|
|
187
187
|
* Get the actual RTCPeerConnectionState for a peer.
|
|
188
188
|
*/
|
|
189
189
|
getConnectionState(peerId: string): RTCPeerConnectionState;
|
|
190
|
+
/**
|
|
191
|
+
* Get the primary message DataChannel state for a peer.
|
|
192
|
+
*/
|
|
193
|
+
getDataChannelState(peerId: string): RTCDataChannelState | "closed";
|
|
190
194
|
/**
|
|
191
195
|
* Check if the main DataChannel is ready for a specific peer.
|
|
192
196
|
*/
|
|
@@ -377,6 +377,13 @@ export class WebRTCManager {
|
|
|
377
377
|
const pc = this.peerConnections.get(peerId);
|
|
378
378
|
return pc?.connectionState ?? "closed";
|
|
379
379
|
}
|
|
380
|
+
/**
|
|
381
|
+
* Get the primary message DataChannel state for a peer.
|
|
382
|
+
*/
|
|
383
|
+
getDataChannelState(peerId) {
|
|
384
|
+
return (normalizePeerChannels(this.dataChannels.get(peerId)).message
|
|
385
|
+
?.readyState ?? "closed");
|
|
386
|
+
}
|
|
380
387
|
/**
|
|
381
388
|
* Check if the main DataChannel is ready for a specific peer.
|
|
382
389
|
*/
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -10,4 +10,4 @@ export type { AvesErrorCode } from "./core/AvesError";
|
|
|
10
10
|
export { EventEmitter } from "./core/EventEmitter";
|
|
11
11
|
export { WebRTCManager } from "./core/WebRTCManager";
|
|
12
12
|
export { SignalingClient } from "./core/SignalingClient";
|
|
13
|
-
export type { Participant, AvesMessage, AvesClientConfig, AvesVideoConstraints, FileTransferOptions, FileTransferInfo, FileTransferProgress, FileTransferResult, LocalAudioState, LocalVideoState, ScreenShareState, ScreenShareSource, JsonObject, JsonPrimitive, JsonValue, SignalingMessage, SignalingErrorCode, SignalingErrorStage, SignalingErrorPayload, JoinRoomResult, ReconnectConfig, } from "./types/types";
|
|
13
|
+
export type { Participant, AvesMessage, AvesClientConfig, AvesConnectionSnapshot, AvesPeerSnapshot, AvesVideoConstraints, FileTransferOptions, FileTransferInfo, FileTransferProgress, FileTransferResult, LocalAudioState, LocalVideoState, ScreenShareState, ScreenShareSource, JsonObject, JsonPrimitive, JsonValue, SignalingMessage, SignalingErrorCode, SignalingErrorStage, SignalingErrorPayload, JoinRoomResult, ReconnectConfig, WaitForPeerOptions, } from "./types/types";
|
|
@@ -33,6 +33,26 @@ export interface AvesClientConfig {
|
|
|
33
33
|
};
|
|
34
34
|
debug?: boolean;
|
|
35
35
|
}
|
|
36
|
+
export interface AvesPeerSnapshot {
|
|
37
|
+
peerId: string;
|
|
38
|
+
participant?: Participant;
|
|
39
|
+
connectionState: RTCPeerConnectionState;
|
|
40
|
+
dataChannelState: RTCDataChannelState | "closed";
|
|
41
|
+
messageChannelReady: boolean;
|
|
42
|
+
fileChannelReady: boolean;
|
|
43
|
+
}
|
|
44
|
+
export interface AvesConnectionSnapshot {
|
|
45
|
+
roomId: string | null;
|
|
46
|
+
currentUserId: string | null;
|
|
47
|
+
signalingConnected: boolean;
|
|
48
|
+
participantCount: number;
|
|
49
|
+
participants: Participant[];
|
|
50
|
+
peers: AvesPeerSnapshot[];
|
|
51
|
+
}
|
|
52
|
+
export interface WaitForPeerOptions {
|
|
53
|
+
timeoutMs?: number;
|
|
54
|
+
requireFileChannel?: boolean;
|
|
55
|
+
}
|
|
36
56
|
export interface FileTransferOptions {
|
|
37
57
|
peerId?: string;
|
|
38
58
|
fileName?: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -10,4 +10,4 @@ export type { AvesErrorCode } from "./core/AvesError";
|
|
|
10
10
|
export { EventEmitter } from "./core/EventEmitter";
|
|
11
11
|
export { WebRTCManager } from "./core/WebRTCManager";
|
|
12
12
|
export { SignalingClient } from "./core/SignalingClient";
|
|
13
|
-
export type { Participant, AvesMessage, AvesClientConfig, AvesVideoConstraints, FileTransferOptions, FileTransferInfo, FileTransferProgress, FileTransferResult, LocalAudioState, LocalVideoState, ScreenShareState, ScreenShareSource, JsonObject, JsonPrimitive, JsonValue, SignalingMessage, SignalingErrorCode, SignalingErrorStage, SignalingErrorPayload, JoinRoomResult, ReconnectConfig, } from "./types/types";
|
|
13
|
+
export type { Participant, AvesMessage, AvesClientConfig, AvesConnectionSnapshot, AvesPeerSnapshot, AvesVideoConstraints, FileTransferOptions, FileTransferInfo, FileTransferProgress, FileTransferResult, LocalAudioState, LocalVideoState, ScreenShareState, ScreenShareSource, JsonObject, JsonPrimitive, JsonValue, SignalingMessage, SignalingErrorCode, SignalingErrorStage, SignalingErrorPayload, JoinRoomResult, ReconnectConfig, WaitForPeerOptions, } from "./types/types";
|
package/dist/types/types.d.ts
CHANGED
|
@@ -33,6 +33,26 @@ export interface AvesClientConfig {
|
|
|
33
33
|
};
|
|
34
34
|
debug?: boolean;
|
|
35
35
|
}
|
|
36
|
+
export interface AvesPeerSnapshot {
|
|
37
|
+
peerId: string;
|
|
38
|
+
participant?: Participant;
|
|
39
|
+
connectionState: RTCPeerConnectionState;
|
|
40
|
+
dataChannelState: RTCDataChannelState | "closed";
|
|
41
|
+
messageChannelReady: boolean;
|
|
42
|
+
fileChannelReady: boolean;
|
|
43
|
+
}
|
|
44
|
+
export interface AvesConnectionSnapshot {
|
|
45
|
+
roomId: string | null;
|
|
46
|
+
currentUserId: string | null;
|
|
47
|
+
signalingConnected: boolean;
|
|
48
|
+
participantCount: number;
|
|
49
|
+
participants: Participant[];
|
|
50
|
+
peers: AvesPeerSnapshot[];
|
|
51
|
+
}
|
|
52
|
+
export interface WaitForPeerOptions {
|
|
53
|
+
timeoutMs?: number;
|
|
54
|
+
requireFileChannel?: boolean;
|
|
55
|
+
}
|
|
36
56
|
export interface FileTransferOptions {
|
|
37
57
|
peerId?: string;
|
|
38
58
|
fileName?: string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yrzhao/aves-core",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "WebRTC client library for real-time communication",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -25,7 +25,6 @@
|
|
|
25
25
|
"test:watch": "jest --watch",
|
|
26
26
|
"test:coverage": "jest --coverage"
|
|
27
27
|
},
|
|
28
|
-
"peerDependencies": {},
|
|
29
28
|
"devDependencies": {
|
|
30
29
|
"@types/jest": "^29.5.0",
|
|
31
30
|
"@types/node": "^20.0.0",
|
|
@@ -40,6 +39,21 @@
|
|
|
40
39
|
"communication",
|
|
41
40
|
"peer-to-peer"
|
|
42
41
|
],
|
|
43
|
-
"author": "",
|
|
44
|
-
"license": "MIT"
|
|
42
|
+
"author": "Yrzhao",
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"sideEffects": false,
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=18"
|
|
47
|
+
},
|
|
48
|
+
"publishConfig": {
|
|
49
|
+
"access": "public"
|
|
50
|
+
},
|
|
51
|
+
"repository": {
|
|
52
|
+
"type": "git",
|
|
53
|
+
"url": "git+https://github.com/yrzhao/aves.git"
|
|
54
|
+
},
|
|
55
|
+
"bugs": {
|
|
56
|
+
"url": "https://github.com/yrzhao/aves/issues"
|
|
57
|
+
},
|
|
58
|
+
"homepage": "https://github.com/yrzhao/aves#readme"
|
|
45
59
|
}
|