mirror of
https://github.com/xtr-dev/rondevu-client.git
synced 2025-12-10 19:03:24 +00:00
Compare commits
27 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 557cc0a838 | |||
| 6e661f69bc | |||
| 00f4da7250 | |||
| 6c344ec8e1 | |||
| 5a5da124a6 | |||
| c8b7a2913f | |||
| 6ddf7cb7f0 | |||
| 35ce051a26 | |||
| 280c8c284f | |||
| 14d3f943da | |||
| 2989326a50 | |||
| 7b82f963a3 | |||
| d25141a765 | |||
| 9d9aba2cf5 | |||
| dd64a565aa | |||
| cd78a16c66 | |||
| c202e1c627 | |||
| f6004a9bc0 | |||
| 5a47e0a397 | |||
| e1ca8e1c16 | |||
| 2f47107018 | |||
| d200d73cd9 | |||
| c8e5e4d17a | |||
| 6466a6f52a | |||
| 2e4d0d6a54 | |||
| 2b73e6ba44 | |||
| a893c7d040 |
483
README.md
483
README.md
@@ -1,82 +1,453 @@
|
|||||||
# Rondevu
|
# @xtr-dev/rondevu-client
|
||||||
|
|
||||||
🎯 **Simple WebRTC peer signaling and discovery**
|
|
||||||
|
|
||||||
Meet peers by topic, by peer ID, or by connection ID.
|
|
||||||
|
|
||||||
**Related repositories:**
|
|
||||||
- [rondevu-server](https://github.com/xtr-dev/rondevu-server) - HTTP signaling server
|
|
||||||
- [rondevu-demo](https://github.com/xtr-dev/rondevu-demo) - Interactive demo
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## @xtr-dev/rondevu-client
|
|
||||||
|
|
||||||
[](https://www.npmjs.com/package/@xtr-dev/rondevu-client)
|
[](https://www.npmjs.com/package/@xtr-dev/rondevu-client)
|
||||||
|
|
||||||
TypeScript client library for Rondevu peer signaling and WebRTC connection management. Handles automatic signaling, ICE candidate exchange, and connection establishment.
|
🌐 **Topic-based peer discovery and WebRTC signaling client**
|
||||||
|
|
||||||
### Install
|
TypeScript/JavaScript client for Rondevu, providing topic-based peer discovery, stateless authentication, and complete WebRTC signaling.
|
||||||
|
|
||||||
|
**Related repositories:**
|
||||||
|
- [rondevu-server](https://github.com/xtr-dev/rondevu) - HTTP signaling server
|
||||||
|
- [rondevu-demo](https://rondevu-demo.pages.dev) - Interactive demo
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- **Topic-Based Discovery**: Find peers by topics (e.g., torrent infohashes)
|
||||||
|
- **Stateless Authentication**: No server-side sessions, portable credentials
|
||||||
|
- **Bloom Filters**: Efficient peer exclusion for repeated discoveries
|
||||||
|
- **Multi-Offer Management**: Create and manage multiple offers per peer
|
||||||
|
- **Complete WebRTC Signaling**: Full offer/answer and ICE candidate exchange
|
||||||
|
- **TypeScript**: Full type safety and autocomplete
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install @xtr-dev/rondevu-client
|
npm install @xtr-dev/rondevu-client
|
||||||
```
|
```
|
||||||
|
|
||||||
### Usage
|
## Quick Start
|
||||||
|
|
||||||
|
The easiest way to use Rondevu is with the high-level `RondevuConnection` class, which handles all WebRTC connection complexity including offer/answer exchange, ICE candidates, and connection lifecycle.
|
||||||
|
|
||||||
|
### Creating an Offer (Peer A)
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { Rondevu } from '@xtr-dev/rondevu-client';
|
import { Rondevu } from '@xtr-dev/rondevu-client';
|
||||||
|
|
||||||
const rdv = new Rondevu({
|
const client = new Rondevu({ baseUrl: 'https://api.ronde.vu' });
|
||||||
baseUrl: 'https://server.com',
|
await client.register();
|
||||||
rtcConfig: {
|
|
||||||
iceServers: [
|
// Create a connection
|
||||||
// your ICE servers here
|
const conn = client.createConnection();
|
||||||
{ urls: 'stun:stun.l.google.com:19302' },
|
|
||||||
{ urls: 'stun:stun1.l.google.com:19302' },
|
// Set up event listeners
|
||||||
{
|
conn.on('connected', () => {
|
||||||
urls: 'turn:relay1.example.com:3480',
|
console.log('Connected to peer!');
|
||||||
username: 'example',
|
|
||||||
credential: 'example'
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Connect by topic
|
conn.on('datachannel', (channel) => {
|
||||||
const conn = await rdv.join('room');
|
console.log('Data channel ready');
|
||||||
|
|
||||||
// Or connect by ID
|
channel.onmessage = (event) => {
|
||||||
const conn = await rdv.connect('meeting-123');
|
console.log('Received:', event.data);
|
||||||
|
};
|
||||||
|
|
||||||
// Use the connection
|
channel.send('Hello from peer A!');
|
||||||
conn.on('connect', () => {
|
});
|
||||||
const channel = conn.dataChannel('chat');
|
|
||||||
channel.send('Hello!');
|
// Create offer and advertise on topics
|
||||||
|
const offerId = await conn.createOffer({
|
||||||
|
topics: ['my-app', 'room-123'],
|
||||||
|
ttl: 300000 // 5 minutes
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('Offer created:', offerId);
|
||||||
|
console.log('Share these topics with peers:', ['my-app', 'room-123']);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Answering an Offer (Peer B)
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Rondevu } from '@xtr-dev/rondevu-client';
|
||||||
|
|
||||||
|
const client = new Rondevu({ baseUrl: 'https://api.ronde.vu' });
|
||||||
|
await client.register();
|
||||||
|
|
||||||
|
// Discover offers by topic
|
||||||
|
const offers = await client.offers.findByTopic('my-app', { limit: 10 });
|
||||||
|
|
||||||
|
if (offers.length > 0) {
|
||||||
|
const offer = offers[0];
|
||||||
|
|
||||||
|
// Create connection
|
||||||
|
const conn = client.createConnection();
|
||||||
|
|
||||||
|
// Set up event listeners
|
||||||
|
conn.on('connecting', () => {
|
||||||
|
console.log('Connecting...');
|
||||||
|
});
|
||||||
|
|
||||||
|
conn.on('connected', () => {
|
||||||
|
console.log('Connected!');
|
||||||
|
});
|
||||||
|
|
||||||
|
conn.on('datachannel', (channel) => {
|
||||||
|
console.log('Data channel ready');
|
||||||
|
|
||||||
|
channel.onmessage = (event) => {
|
||||||
|
console.log('Received:', event.data);
|
||||||
|
};
|
||||||
|
|
||||||
|
channel.send('Hello from peer B!');
|
||||||
|
});
|
||||||
|
|
||||||
|
// Answer the offer
|
||||||
|
await conn.answer(offer.id, offer.sdp);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Connection Events
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
conn.on('connecting', () => {
|
||||||
|
// Connection is being established
|
||||||
|
});
|
||||||
|
|
||||||
|
conn.on('connected', () => {
|
||||||
|
// Connection established successfully
|
||||||
|
});
|
||||||
|
|
||||||
|
conn.on('disconnected', () => {
|
||||||
|
// Connection lost or closed
|
||||||
|
});
|
||||||
|
|
||||||
|
conn.on('error', (error) => {
|
||||||
|
// An error occurred
|
||||||
|
console.error('Connection error:', error);
|
||||||
|
});
|
||||||
|
|
||||||
|
conn.on('datachannel', (channel) => {
|
||||||
|
// Data channel is ready to use
|
||||||
|
});
|
||||||
|
|
||||||
|
conn.on('track', (event) => {
|
||||||
|
// Media track received (for audio/video streaming)
|
||||||
|
const stream = event.streams[0];
|
||||||
|
videoElement.srcObject = stream;
|
||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
### API
|
### Adding Media Tracks
|
||||||
|
|
||||||
**Main Methods:**
|
```typescript
|
||||||
- `rdv.join(topic)` - Auto-connect to first peer in topic
|
// Get user's camera/microphone
|
||||||
- `rdv.join(topic, {filter})` - Connect to specific peer by ID
|
const stream = await navigator.mediaDevices.getUserMedia({
|
||||||
- `rdv.create(id, topic)` - Create connection for others to join
|
video: true,
|
||||||
- `rdv.connect(id)` - Join connection by ID
|
audio: true
|
||||||
|
});
|
||||||
|
|
||||||
**Connection Events:**
|
// Add tracks to connection
|
||||||
- `connect` - Connection established
|
stream.getTracks().forEach(track => {
|
||||||
- `disconnect` - Connection closed
|
conn.addTrack(track, stream);
|
||||||
- `datachannel` - Remote peer created data channel
|
});
|
||||||
- `stream` - Remote media stream received
|
```
|
||||||
- `error` - Error occurred
|
|
||||||
|
|
||||||
**Connection Methods:**
|
### Connection Properties
|
||||||
- `conn.dataChannel(label)` - Get or create data channel
|
|
||||||
- `conn.addStream(stream)` - Add media stream
|
|
||||||
- `conn.getPeerConnection()` - Get underlying RTCPeerConnection
|
|
||||||
- `conn.close()` - Close connection
|
|
||||||
|
|
||||||
### License
|
```typescript
|
||||||
|
// Get connection state
|
||||||
|
console.log(conn.connectionState); // 'connecting', 'connected', 'disconnected', etc.
|
||||||
|
|
||||||
|
// Get offer ID
|
||||||
|
console.log(conn.id);
|
||||||
|
|
||||||
|
// Get data channel
|
||||||
|
console.log(conn.channel);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Closing a Connection
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
conn.close();
|
||||||
|
```
|
||||||
|
|
||||||
|
## Platform-Specific Setup
|
||||||
|
|
||||||
|
### Node.js 18+ (with native fetch)
|
||||||
|
|
||||||
|
Works out of the box - no additional setup needed.
|
||||||
|
|
||||||
|
### Node.js < 18 (without native fetch)
|
||||||
|
|
||||||
|
Install node-fetch and provide it to the client:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm install node-fetch
|
||||||
|
```
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Rondevu } from '@xtr-dev/rondevu-client';
|
||||||
|
import fetch from 'node-fetch';
|
||||||
|
|
||||||
|
const client = new Rondevu({
|
||||||
|
baseUrl: 'https://api.ronde.vu',
|
||||||
|
fetch: fetch as any
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Deno
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Rondevu } from 'npm:@xtr-dev/rondevu-client';
|
||||||
|
|
||||||
|
const client = new Rondevu({
|
||||||
|
baseUrl: 'https://api.ronde.vu'
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### Bun
|
||||||
|
|
||||||
|
Works out of the box - no additional setup needed.
|
||||||
|
|
||||||
|
### Cloudflare Workers
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Rondevu } from '@xtr-dev/rondevu-client';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
async fetch(request: Request, env: Env) {
|
||||||
|
const client = new Rondevu({
|
||||||
|
baseUrl: 'https://api.ronde.vu'
|
||||||
|
});
|
||||||
|
|
||||||
|
const creds = await client.register();
|
||||||
|
return new Response(JSON.stringify(creds));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
## Low-Level API Usage
|
||||||
|
|
||||||
|
For advanced use cases where you need direct control over the signaling process, you can use the low-level API:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Rondevu, BloomFilter } from '@xtr-dev/rondevu-client';
|
||||||
|
|
||||||
|
const client = new Rondevu({ baseUrl: 'https://api.ronde.vu' });
|
||||||
|
|
||||||
|
// Register and get credentials
|
||||||
|
const creds = await client.register();
|
||||||
|
console.log('Peer ID:', creds.peerId);
|
||||||
|
|
||||||
|
// Save credentials for later use
|
||||||
|
localStorage.setItem('rondevu-creds', JSON.stringify(creds));
|
||||||
|
|
||||||
|
// Create offer with topics
|
||||||
|
const offers = await client.offers.create([{
|
||||||
|
sdp: 'v=0...', // Your WebRTC offer SDP
|
||||||
|
topics: ['movie-xyz', 'hd-content'],
|
||||||
|
ttl: 300000 // 5 minutes
|
||||||
|
}]);
|
||||||
|
|
||||||
|
// Discover peers by topic
|
||||||
|
const discovered = await client.offers.findByTopic('movie-xyz', {
|
||||||
|
limit: 50
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log(`Found ${discovered.length} peers`);
|
||||||
|
|
||||||
|
// Use bloom filter to exclude known peers
|
||||||
|
const knownPeers = new Set(['peer-id-1', 'peer-id-2']);
|
||||||
|
const bloom = new BloomFilter(1024, 3);
|
||||||
|
knownPeers.forEach(id => bloom.add(id));
|
||||||
|
|
||||||
|
const newPeers = await client.offers.findByTopic('movie-xyz', {
|
||||||
|
bloomFilter: bloom.toBytes(),
|
||||||
|
limit: 50
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## API Reference
|
||||||
|
|
||||||
|
### Authentication
|
||||||
|
|
||||||
|
#### `client.register()`
|
||||||
|
Register a new peer and receive credentials.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const creds = await client.register();
|
||||||
|
// { peerId: '...', secret: '...' }
|
||||||
|
```
|
||||||
|
|
||||||
|
### Topics
|
||||||
|
|
||||||
|
#### `client.offers.getTopics(options?)`
|
||||||
|
List all topics with active peer counts (paginated).
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const result = await client.offers.getTopics({
|
||||||
|
limit: 50,
|
||||||
|
offset: 0
|
||||||
|
});
|
||||||
|
|
||||||
|
// {
|
||||||
|
// topics: [
|
||||||
|
// { topic: 'movie-xyz', activePeers: 42 },
|
||||||
|
// { topic: 'torrent-abc', activePeers: 15 }
|
||||||
|
// ],
|
||||||
|
// total: 123,
|
||||||
|
// limit: 50,
|
||||||
|
// offset: 0
|
||||||
|
// }
|
||||||
|
```
|
||||||
|
|
||||||
|
### Offers
|
||||||
|
|
||||||
|
#### `client.offers.create(offers)`
|
||||||
|
Create one or more offers with topics.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const offers = await client.offers.create([
|
||||||
|
{
|
||||||
|
sdp: 'v=0...',
|
||||||
|
topics: ['topic-1', 'topic-2'],
|
||||||
|
ttl: 300000 // optional, default 5 minutes
|
||||||
|
}
|
||||||
|
]);
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `client.offers.findByTopic(topic, options?)`
|
||||||
|
Find offers by topic with optional bloom filter.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const offers = await client.offers.findByTopic('movie-xyz', {
|
||||||
|
limit: 50,
|
||||||
|
bloomFilter: bloomBytes // optional
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `client.offers.getMine()`
|
||||||
|
Get all offers owned by the authenticated peer.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const myOffers = await client.offers.getMine();
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `client.offers.heartbeat(offerId)`
|
||||||
|
Update last_seen timestamp for an offer.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
await client.offers.heartbeat(offerId);
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `client.offers.delete(offerId)`
|
||||||
|
Delete a specific offer.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
await client.offers.delete(offerId);
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `client.offers.answer(offerId, sdp)`
|
||||||
|
Answer an offer (locks it to answerer).
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
await client.offers.answer(offerId, answerSdp);
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `client.offers.getAnswers()`
|
||||||
|
Poll for answers to your offers.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const answers = await client.offers.getAnswers();
|
||||||
|
```
|
||||||
|
|
||||||
|
### ICE Candidates
|
||||||
|
|
||||||
|
#### `client.offers.addIceCandidates(offerId, candidates)`
|
||||||
|
Post ICE candidates for an offer.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
await client.offers.addIceCandidates(offerId, [
|
||||||
|
'candidate:1 1 UDP...'
|
||||||
|
]);
|
||||||
|
```
|
||||||
|
|
||||||
|
#### `client.offers.getIceCandidates(offerId, since?)`
|
||||||
|
Get ICE candidates from the other peer.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const candidates = await client.offers.getIceCandidates(offerId);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Bloom Filter
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { BloomFilter } from '@xtr-dev/rondevu-client';
|
||||||
|
|
||||||
|
// Create filter: size=1024 bits, hash=3 functions
|
||||||
|
const bloom = new BloomFilter(1024, 3);
|
||||||
|
|
||||||
|
// Add items
|
||||||
|
bloom.add('peer-id-1');
|
||||||
|
bloom.add('peer-id-2');
|
||||||
|
|
||||||
|
// Test membership
|
||||||
|
bloom.test('peer-id-1'); // true (probably)
|
||||||
|
bloom.test('unknown'); // false (definitely)
|
||||||
|
|
||||||
|
// Export for API
|
||||||
|
const bytes = bloom.toBytes();
|
||||||
|
```
|
||||||
|
|
||||||
|
## TypeScript
|
||||||
|
|
||||||
|
All types are exported:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import type {
|
||||||
|
Credentials,
|
||||||
|
Offer,
|
||||||
|
CreateOfferRequest,
|
||||||
|
TopicInfo,
|
||||||
|
IceCandidate,
|
||||||
|
FetchFunction,
|
||||||
|
RondevuOptions,
|
||||||
|
ConnectionOptions,
|
||||||
|
RondevuConnectionEvents
|
||||||
|
} from '@xtr-dev/rondevu-client';
|
||||||
|
```
|
||||||
|
|
||||||
|
## Environment Compatibility
|
||||||
|
|
||||||
|
The client library is designed to work across different JavaScript runtimes:
|
||||||
|
|
||||||
|
| Environment | Native Fetch | Custom Fetch Needed |
|
||||||
|
|-------------|--------------|---------------------|
|
||||||
|
| Modern Browsers | ✅ Yes | ❌ No |
|
||||||
|
| Node.js 18+ | ✅ Yes | ❌ No |
|
||||||
|
| Node.js < 18 | ❌ No | ✅ Yes (node-fetch) |
|
||||||
|
| Deno | ✅ Yes | ❌ No |
|
||||||
|
| Bun | ✅ Yes | ❌ No |
|
||||||
|
| Cloudflare Workers | ✅ Yes | ❌ No |
|
||||||
|
|
||||||
|
**If your environment doesn't have native fetch:**
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm install node-fetch
|
||||||
|
```
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Rondevu } from '@xtr-dev/rondevu-client';
|
||||||
|
import fetch from 'node-fetch';
|
||||||
|
|
||||||
|
const client = new Rondevu({
|
||||||
|
baseUrl: 'https://rondevu.xtrdev.workers.dev',
|
||||||
|
fetch: fetch as any
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
MIT
|
MIT
|
||||||
|
|||||||
39
package-lock.json
generated
Normal file
39
package-lock.json
generated
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
{
|
||||||
|
"name": "@xtr-dev/rondevu-client",
|
||||||
|
"version": "0.7.1",
|
||||||
|
"lockfileVersion": 3,
|
||||||
|
"requires": true,
|
||||||
|
"packages": {
|
||||||
|
"": {
|
||||||
|
"name": "@xtr-dev/rondevu-client",
|
||||||
|
"version": "0.7.1",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@xtr-dev/rondevu-client": "^0.5.1"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"typescript": "^5.9.3"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/@xtr-dev/rondevu-client": {
|
||||||
|
"version": "0.5.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@xtr-dev/rondevu-client/-/rondevu-client-0.5.1.tgz",
|
||||||
|
"integrity": "sha512-110ejMCizPUPkHwwwNvcdCSZceLaHeFbf1LNkXvbG6pnLBqCf2uoGOOaRkArb7HNNFABFB+HXzm/AVzNdadosw==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
|
"node_modules/typescript": {
|
||||||
|
"version": "5.9.3",
|
||||||
|
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz",
|
||||||
|
"integrity": "sha512-jl1vZzPDinLr9eUt3J/t7V6FgNEw9QjvBPdysz9KfQDD41fQrC2Y4vKQdiaUpFT4bXlb1RHhLpp8wtm6M5TgSw==",
|
||||||
|
"dev": true,
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"bin": {
|
||||||
|
"tsc": "bin/tsc",
|
||||||
|
"tsserver": "bin/tsserver"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">=14.17"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
12
package.json
12
package.json
@@ -1,15 +1,14 @@
|
|||||||
{
|
{
|
||||||
"name": "@xtr-dev/rondevu-client",
|
"name": "@xtr-dev/rondevu-client",
|
||||||
"version": "0.1.2",
|
"version": "0.7.1",
|
||||||
"description": "TypeScript client for Rondevu peer signaling and discovery server",
|
"description": "TypeScript client for Rondevu topic-based peer discovery and signaling server",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"typecheck": "tsc --noEmit",
|
"typecheck": "tsc --noEmit",
|
||||||
"prepublishOnly": "npm run build",
|
"prepublishOnly": "npm run build"
|
||||||
"publish": "npm publish"
|
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"webrtc",
|
"webrtc",
|
||||||
@@ -26,5 +25,8 @@
|
|||||||
"files": [
|
"files": [
|
||||||
"dist",
|
"dist",
|
||||||
"README.md"
|
"README.md"
|
||||||
]
|
],
|
||||||
|
"dependencies": {
|
||||||
|
"@xtr-dev/rondevu-client": "^0.5.1"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
60
src/auth.ts
Normal file
60
src/auth.ts
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
export interface Credentials {
|
||||||
|
peerId: string;
|
||||||
|
secret: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fetch-compatible function type
|
||||||
|
export type FetchFunction = (
|
||||||
|
input: RequestInfo | URL,
|
||||||
|
init?: RequestInit
|
||||||
|
) => Promise<Response>;
|
||||||
|
|
||||||
|
export class RondevuAuth {
|
||||||
|
private fetchFn: FetchFunction;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private baseUrl: string,
|
||||||
|
fetchFn?: FetchFunction
|
||||||
|
) {
|
||||||
|
// Use provided fetch or fall back to global fetch
|
||||||
|
this.fetchFn = fetchFn || ((...args) => {
|
||||||
|
if (typeof globalThis.fetch === 'function') {
|
||||||
|
return globalThis.fetch(...args);
|
||||||
|
}
|
||||||
|
throw new Error(
|
||||||
|
'fetch is not available. Please provide a fetch implementation in the constructor options.'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a new peer and receive credentials
|
||||||
|
*/
|
||||||
|
async register(): Promise<Credentials> {
|
||||||
|
const response = await this.fetchFn(`${this.baseUrl}/register`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({}),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
||||||
|
throw new Error(`Registration failed: ${error.error || response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
return {
|
||||||
|
peerId: data.peerId,
|
||||||
|
secret: data.secret,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create Authorization header value
|
||||||
|
*/
|
||||||
|
static createAuthHeader(credentials: Credentials): string {
|
||||||
|
return `Bearer ${credentials.peerId}:${credentials.secret}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
83
src/bloom.ts
Normal file
83
src/bloom.ts
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
// Declare Buffer for Node.js compatibility
|
||||||
|
declare const Buffer: any;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple bloom filter implementation for peer ID exclusion
|
||||||
|
* Uses multiple hash functions for better distribution
|
||||||
|
*/
|
||||||
|
export class BloomFilter {
|
||||||
|
private bits: Uint8Array;
|
||||||
|
private size: number;
|
||||||
|
private numHashes: number;
|
||||||
|
|
||||||
|
constructor(size: number = 1024, numHashes: number = 3) {
|
||||||
|
this.size = size;
|
||||||
|
this.numHashes = numHashes;
|
||||||
|
this.bits = new Uint8Array(Math.ceil(size / 8));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a peer ID to the filter
|
||||||
|
*/
|
||||||
|
add(peerId: string): void {
|
||||||
|
for (let i = 0; i < this.numHashes; i++) {
|
||||||
|
const hash = this.hash(peerId, i);
|
||||||
|
const index = hash % this.size;
|
||||||
|
const byteIndex = Math.floor(index / 8);
|
||||||
|
const bitIndex = index % 8;
|
||||||
|
this.bits[byteIndex] |= 1 << bitIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test if peer ID might be in the filter
|
||||||
|
*/
|
||||||
|
test(peerId: string): boolean {
|
||||||
|
for (let i = 0; i < this.numHashes; i++) {
|
||||||
|
const hash = this.hash(peerId, i);
|
||||||
|
const index = hash % this.size;
|
||||||
|
const byteIndex = Math.floor(index / 8);
|
||||||
|
const bitIndex = index % 8;
|
||||||
|
if (!(this.bits[byteIndex] & (1 << bitIndex))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get raw bits for transmission
|
||||||
|
*/
|
||||||
|
toBytes(): Uint8Array {
|
||||||
|
return this.bits;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert to base64 for URL parameters
|
||||||
|
*/
|
||||||
|
toBase64(): string {
|
||||||
|
// Convert Uint8Array to regular array then to string
|
||||||
|
const binaryString = String.fromCharCode(...Array.from(this.bits));
|
||||||
|
// Use btoa for browser, or Buffer for Node.js
|
||||||
|
if (typeof btoa !== 'undefined') {
|
||||||
|
return btoa(binaryString);
|
||||||
|
} else if (typeof Buffer !== 'undefined') {
|
||||||
|
return Buffer.from(this.bits).toString('base64');
|
||||||
|
} else {
|
||||||
|
// Fallback: manual base64 encoding
|
||||||
|
throw new Error('No base64 encoding available');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Simple hash function (FNV-1a variant)
|
||||||
|
*/
|
||||||
|
private hash(str: string, seed: number): number {
|
||||||
|
let hash = 2166136261 ^ seed;
|
||||||
|
for (let i = 0; i < str.length; i++) {
|
||||||
|
hash ^= str.charCodeAt(i);
|
||||||
|
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
|
||||||
|
}
|
||||||
|
return hash >>> 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
141
src/client.ts
141
src/client.ts
@@ -1,7 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
RondevuClientOptions,
|
RondevuClientOptions,
|
||||||
ListTopicsResponse,
|
|
||||||
ListSessionsResponse,
|
|
||||||
CreateOfferRequest,
|
CreateOfferRequest,
|
||||||
CreateOfferResponse,
|
CreateOfferResponse,
|
||||||
AnswerRequest,
|
AnswerRequest,
|
||||||
@@ -13,17 +11,17 @@ import {
|
|||||||
HealthResponse,
|
HealthResponse,
|
||||||
ErrorResponse,
|
ErrorResponse,
|
||||||
Side,
|
Side,
|
||||||
} from './types';
|
} from './types.js';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HTTP client for Rondevu peer signaling and discovery server
|
* HTTP API client for Rondevu peer signaling server
|
||||||
*/
|
*/
|
||||||
export class RondevuClient {
|
export class RondevuAPI {
|
||||||
private readonly baseUrl: string;
|
private readonly baseUrl: string;
|
||||||
private readonly fetchImpl: typeof fetch;
|
private readonly fetchImpl: typeof fetch;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new Rondevu client instance
|
* Creates a new Rondevu API client instance
|
||||||
* @param options - Client configuration options
|
* @param options - Client configuration options
|
||||||
*/
|
*/
|
||||||
constructor(options: RondevuClientOptions) {
|
constructor(options: RondevuClientOptions) {
|
||||||
@@ -66,12 +64,12 @@ export class RondevuClient {
|
|||||||
/**
|
/**
|
||||||
* Gets server version information
|
* Gets server version information
|
||||||
*
|
*
|
||||||
* @returns Server version (git commit hash)
|
* @returns Server version
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* ```typescript
|
* ```typescript
|
||||||
* const client = new RondevuClient({ baseUrl: 'https://example.com' });
|
* const api = new RondevuAPI({ baseUrl: 'https://example.com' });
|
||||||
* const { version } = await client.getVersion();
|
* const { version } = await api.getVersion();
|
||||||
* console.log('Server version:', version);
|
* console.log('Server version:', version);
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
@@ -82,98 +80,49 @@ export class RondevuClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lists all topics with peer counts
|
* Creates a new offer
|
||||||
*
|
*
|
||||||
* @param page - Page number (starting from 1)
|
* @param request - Offer details including peer ID, signaling data, and optional custom code
|
||||||
* @param limit - Results per page (max 1000)
|
* @returns Unique offer code (UUID or custom code)
|
||||||
* @returns List of topics with pagination info
|
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* ```typescript
|
* ```typescript
|
||||||
* const client = new RondevuClient({ baseUrl: 'https://example.com' });
|
* const api = new RondevuAPI({ baseUrl: 'https://example.com' });
|
||||||
* const { topics, pagination } = await client.listTopics();
|
* const { code } = await api.createOffer({
|
||||||
* console.log(`Found ${topics.length} topics`);
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
async listTopics(page = 1, limit = 100): Promise<ListTopicsResponse> {
|
|
||||||
const params = new URLSearchParams({
|
|
||||||
page: page.toString(),
|
|
||||||
limit: limit.toString(),
|
|
||||||
});
|
|
||||||
return this.request<ListTopicsResponse>(`/topics?${params}`, {
|
|
||||||
method: 'GET',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Discovers available peers for a given topic
|
|
||||||
*
|
|
||||||
* @param topic - Topic identifier
|
|
||||||
* @returns List of available sessions
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* ```typescript
|
|
||||||
* const client = new RondevuClient({ baseUrl: 'https://example.com' });
|
|
||||||
* const { sessions } = await client.listSessions('my-room');
|
|
||||||
* const otherPeers = sessions.filter(s => s.peerId !== myPeerId);
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
async listSessions(topic: string): Promise<ListSessionsResponse> {
|
|
||||||
return this.request<ListSessionsResponse>(`/${encodeURIComponent(topic)}/sessions`, {
|
|
||||||
method: 'GET',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Announces peer availability and creates a new session
|
|
||||||
*
|
|
||||||
* @param topic - Topic identifier for grouping peers (max 1024 characters)
|
|
||||||
* @param request - Offer details including peer ID and signaling data
|
|
||||||
* @returns Unique session code (UUID)
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* ```typescript
|
|
||||||
* const client = new RondevuClient({ baseUrl: 'https://example.com' });
|
|
||||||
* const { code } = await client.createOffer('my-room', {
|
|
||||||
* peerId: 'peer-123',
|
* peerId: 'peer-123',
|
||||||
* offer: signalingData
|
* offer: signalingData,
|
||||||
|
* code: 'my-custom-code' // optional
|
||||||
* });
|
* });
|
||||||
* console.log('Session code:', code);
|
* console.log('Offer code:', code);
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
async createOffer(
|
async createOffer(request: CreateOfferRequest): Promise<CreateOfferResponse> {
|
||||||
topic: string,
|
return this.request<CreateOfferResponse>('/offer', {
|
||||||
request: CreateOfferRequest
|
|
||||||
): Promise<CreateOfferResponse> {
|
|
||||||
return this.request<CreateOfferResponse>(
|
|
||||||
`/${encodeURIComponent(topic)}/offer`,
|
|
||||||
{
|
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify(request),
|
body: JSON.stringify(request),
|
||||||
}
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sends an answer or candidate to an existing session
|
* Sends an answer or candidate to an existing offer
|
||||||
*
|
*
|
||||||
* @param request - Answer details including session code and signaling data
|
* @param request - Answer details including offer code and signaling data
|
||||||
* @returns Success confirmation
|
* @returns Success confirmation
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* ```typescript
|
* ```typescript
|
||||||
* const client = new RondevuClient({ baseUrl: 'https://example.com' });
|
* const api = new RondevuAPI({ baseUrl: 'https://example.com' });
|
||||||
*
|
*
|
||||||
* // Send answer
|
* // Send answer
|
||||||
* await client.sendAnswer({
|
* await api.sendAnswer({
|
||||||
* code: sessionCode,
|
* code: offerCode,
|
||||||
* answer: answerData,
|
* answer: answerData,
|
||||||
* side: 'answerer'
|
* side: 'answerer'
|
||||||
* });
|
* });
|
||||||
*
|
*
|
||||||
* // Send candidate
|
* // Send candidate
|
||||||
* await client.sendAnswer({
|
* await api.sendAnswer({
|
||||||
* code: sessionCode,
|
* code: offerCode,
|
||||||
* candidate: candidateData,
|
* candidate: candidateData,
|
||||||
* side: 'offerer'
|
* side: 'offerer'
|
||||||
* });
|
* });
|
||||||
@@ -187,24 +136,24 @@ export class RondevuClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Polls for session data from the other peer
|
* Polls for offer data from the other peer
|
||||||
*
|
*
|
||||||
* @param code - Session UUID
|
* @param code - Offer code
|
||||||
* @param side - Which side is polling ('offerer' or 'answerer')
|
* @param side - Which side is polling ('offerer' or 'answerer')
|
||||||
* @returns Session data including offers, answers, and candidates
|
* @returns Offer data including offers, answers, and candidates
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* ```typescript
|
* ```typescript
|
||||||
* const client = new RondevuClient({ baseUrl: 'https://example.com' });
|
* const api = new RondevuAPI({ baseUrl: 'https://example.com' });
|
||||||
*
|
*
|
||||||
* // Offerer polls for answer
|
* // Offerer polls for answer
|
||||||
* const offererData = await client.poll(sessionCode, 'offerer');
|
* const offererData = await api.poll(offerCode, 'offerer');
|
||||||
* if (offererData.answer) {
|
* if (offererData.answer) {
|
||||||
* console.log('Received answer:', offererData.answer);
|
* console.log('Received answer:', offererData.answer);
|
||||||
* }
|
* }
|
||||||
*
|
*
|
||||||
* // Answerer polls for offer
|
* // Answerer polls for offer
|
||||||
* const answererData = await client.poll(sessionCode, 'answerer');
|
* const answererData = await api.poll(offerCode, 'answerer');
|
||||||
* console.log('Received offer:', answererData.offer);
|
* console.log('Received offer:', answererData.offer);
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
@@ -220,15 +169,16 @@ export class RondevuClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks server health
|
* Checks server health and version
|
||||||
*
|
*
|
||||||
* @returns Health status and timestamp
|
* @returns Health status, timestamp, and version
|
||||||
*
|
*
|
||||||
* @example
|
* @example
|
||||||
* ```typescript
|
* ```typescript
|
||||||
* const client = new RondevuClient({ baseUrl: 'https://example.com' });
|
* const api = new RondevuAPI({ baseUrl: 'https://example.com' });
|
||||||
* const health = await client.health();
|
* const health = await api.health();
|
||||||
* console.log('Server status:', health.status);
|
* console.log('Server status:', health.status);
|
||||||
|
* console.log('Server version:', health.version);
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
async health(): Promise<HealthResponse> {
|
async health(): Promise<HealthResponse> {
|
||||||
@@ -236,4 +186,23 @@ export class RondevuClient {
|
|||||||
method: 'GET',
|
method: 'GET',
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ends a session by deleting the offer from the server
|
||||||
|
*
|
||||||
|
* @param code - The offer code
|
||||||
|
* @returns Success confirmation
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```typescript
|
||||||
|
* const api = new RondevuAPI({ baseUrl: 'https://example.com' });
|
||||||
|
* await api.leave('my-offer-code');
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
async leave(code: string): Promise<{ success: boolean }> {
|
||||||
|
return this.request<{ success: boolean }>('/leave', {
|
||||||
|
method: 'POST',
|
||||||
|
body: JSON.stringify({ code }),
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,310 +0,0 @@
|
|||||||
import { EventEmitter } from './event-emitter';
|
|
||||||
import { RondevuClient } from './client';
|
|
||||||
import { RondevuConnectionParams } from './types';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents a WebRTC connection with automatic signaling and ICE exchange
|
|
||||||
*/
|
|
||||||
export class RondevuConnection extends EventEmitter {
|
|
||||||
readonly id: string;
|
|
||||||
readonly topic: string;
|
|
||||||
readonly role: 'offerer' | 'answerer';
|
|
||||||
readonly remotePeerId: string;
|
|
||||||
|
|
||||||
private pc: RTCPeerConnection;
|
|
||||||
private client: RondevuClient;
|
|
||||||
private localPeerId: string;
|
|
||||||
private dataChannels: Map<string, RTCDataChannel>;
|
|
||||||
private pollingInterval?: ReturnType<typeof setInterval>;
|
|
||||||
private pollingIntervalMs: number;
|
|
||||||
private connectionTimeoutMs: number;
|
|
||||||
private connectionTimer?: ReturnType<typeof setTimeout>;
|
|
||||||
private isPolling: boolean = false;
|
|
||||||
private isClosed: boolean = false;
|
|
||||||
|
|
||||||
constructor(params: RondevuConnectionParams, client: RondevuClient) {
|
|
||||||
super();
|
|
||||||
this.id = params.id;
|
|
||||||
this.topic = params.topic;
|
|
||||||
this.role = params.role;
|
|
||||||
this.pc = params.pc;
|
|
||||||
this.localPeerId = params.localPeerId;
|
|
||||||
this.remotePeerId = params.remotePeerId;
|
|
||||||
this.client = client;
|
|
||||||
this.dataChannels = new Map();
|
|
||||||
this.pollingIntervalMs = params.pollingInterval;
|
|
||||||
this.connectionTimeoutMs = params.connectionTimeout;
|
|
||||||
|
|
||||||
this.setupEventHandlers();
|
|
||||||
this.startConnectionTimeout();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Setup RTCPeerConnection event handlers
|
|
||||||
*/
|
|
||||||
private setupEventHandlers(): void {
|
|
||||||
// ICE candidate gathering
|
|
||||||
this.pc.onicecandidate = (event) => {
|
|
||||||
if (event.candidate && !this.isClosed) {
|
|
||||||
this.sendIceCandidate(event.candidate).catch((err) => {
|
|
||||||
this.emit('error', new Error(`Failed to send ICE candidate: ${err.message}`));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Connection state changes
|
|
||||||
this.pc.onconnectionstatechange = () => {
|
|
||||||
this.handleConnectionStateChange();
|
|
||||||
};
|
|
||||||
|
|
||||||
// Remote data channels
|
|
||||||
this.pc.ondatachannel = (event) => {
|
|
||||||
this.handleRemoteDataChannel(event.channel);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Remote media streams
|
|
||||||
this.pc.ontrack = (event) => {
|
|
||||||
if (event.streams && event.streams[0]) {
|
|
||||||
this.emit('stream', event.streams[0]);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// ICE connection state changes
|
|
||||||
this.pc.oniceconnectionstatechange = () => {
|
|
||||||
const state = this.pc.iceConnectionState;
|
|
||||||
|
|
||||||
if (state === 'failed' || state === 'closed') {
|
|
||||||
this.emit('error', new Error(`ICE connection ${state}`));
|
|
||||||
if (state === 'failed') {
|
|
||||||
this.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle RTCPeerConnection state changes
|
|
||||||
*/
|
|
||||||
private handleConnectionStateChange(): void {
|
|
||||||
const state = this.pc.connectionState;
|
|
||||||
|
|
||||||
switch (state) {
|
|
||||||
case 'connected':
|
|
||||||
this.clearConnectionTimeout();
|
|
||||||
this.stopPolling();
|
|
||||||
this.emit('connect');
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'disconnected':
|
|
||||||
this.emit('disconnect');
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'failed':
|
|
||||||
this.emit('error', new Error('Connection failed'));
|
|
||||||
this.close();
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'closed':
|
|
||||||
this.emit('disconnect');
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Send an ICE candidate to the remote peer via signaling server
|
|
||||||
*/
|
|
||||||
private async sendIceCandidate(candidate: RTCIceCandidate): Promise<void> {
|
|
||||||
try {
|
|
||||||
await this.client.sendAnswer({
|
|
||||||
code: this.id,
|
|
||||||
candidate: JSON.stringify(candidate.toJSON()),
|
|
||||||
side: this.role,
|
|
||||||
});
|
|
||||||
} catch (err: any) {
|
|
||||||
throw new Error(`Failed to send ICE candidate: ${err.message}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Start polling for remote session data (answer/candidates)
|
|
||||||
*/
|
|
||||||
startPolling(): void {
|
|
||||||
if (this.isPolling || this.isClosed) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.isPolling = true;
|
|
||||||
|
|
||||||
// Poll immediately
|
|
||||||
this.poll().catch((err) => {
|
|
||||||
this.emit('error', new Error(`Poll error: ${err.message}`));
|
|
||||||
});
|
|
||||||
|
|
||||||
// Set up interval polling
|
|
||||||
this.pollingInterval = setInterval(() => {
|
|
||||||
this.poll().catch((err) => {
|
|
||||||
this.emit('error', new Error(`Poll error: ${err.message}`));
|
|
||||||
});
|
|
||||||
}, this.pollingIntervalMs);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop polling
|
|
||||||
*/
|
|
||||||
private stopPolling(): void {
|
|
||||||
this.isPolling = false;
|
|
||||||
if (this.pollingInterval) {
|
|
||||||
clearInterval(this.pollingInterval);
|
|
||||||
this.pollingInterval = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Poll the signaling server for remote data
|
|
||||||
*/
|
|
||||||
private async poll(): Promise<void> {
|
|
||||||
if (this.isClosed) {
|
|
||||||
this.stopPolling();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
const response = await this.client.poll(this.id, this.role);
|
|
||||||
|
|
||||||
if (this.role === 'offerer') {
|
|
||||||
const offererResponse = response as { answer: string | null; answerCandidates: string[] };
|
|
||||||
|
|
||||||
// Apply answer if received and not yet applied
|
|
||||||
if (offererResponse.answer && !this.pc.currentRemoteDescription) {
|
|
||||||
await this.pc.setRemoteDescription({
|
|
||||||
type: 'answer',
|
|
||||||
sdp: offererResponse.answer,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply ICE candidates
|
|
||||||
if (offererResponse.answerCandidates && offererResponse.answerCandidates.length > 0) {
|
|
||||||
for (const candidateStr of offererResponse.answerCandidates) {
|
|
||||||
try {
|
|
||||||
const candidate = JSON.parse(candidateStr);
|
|
||||||
await this.pc.addIceCandidate(new RTCIceCandidate(candidate));
|
|
||||||
} catch (err) {
|
|
||||||
console.warn('Failed to add ICE candidate:', err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Answerer role
|
|
||||||
const answererResponse = response as { offer: string; offerCandidates: string[] };
|
|
||||||
|
|
||||||
// Apply ICE candidates from offerer
|
|
||||||
if (answererResponse.offerCandidates && answererResponse.offerCandidates.length > 0) {
|
|
||||||
for (const candidateStr of answererResponse.offerCandidates) {
|
|
||||||
try {
|
|
||||||
const candidate = JSON.parse(candidateStr);
|
|
||||||
await this.pc.addIceCandidate(new RTCIceCandidate(candidate));
|
|
||||||
} catch (err) {
|
|
||||||
console.warn('Failed to add ICE candidate:', err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (err: any) {
|
|
||||||
// Session not found or expired
|
|
||||||
if (err.message.includes('404') || err.message.includes('not found')) {
|
|
||||||
this.emit('error', new Error('Session not found or expired'));
|
|
||||||
this.close();
|
|
||||||
}
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle remotely created data channel
|
|
||||||
*/
|
|
||||||
private handleRemoteDataChannel(channel: RTCDataChannel): void {
|
|
||||||
this.dataChannels.set(channel.label, channel);
|
|
||||||
this.emit('datachannel', channel);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get or create a data channel
|
|
||||||
*/
|
|
||||||
dataChannel(label: string, options?: RTCDataChannelInit): RTCDataChannel {
|
|
||||||
let channel = this.dataChannels.get(label);
|
|
||||||
|
|
||||||
if (!channel) {
|
|
||||||
channel = this.pc.createDataChannel(label, options);
|
|
||||||
this.dataChannels.set(label, channel);
|
|
||||||
}
|
|
||||||
|
|
||||||
return channel;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add a local media stream to the connection
|
|
||||||
*/
|
|
||||||
addStream(stream: MediaStream): void {
|
|
||||||
stream.getTracks().forEach(track => {
|
|
||||||
this.pc.addTrack(track, stream);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the underlying RTCPeerConnection for advanced usage
|
|
||||||
*/
|
|
||||||
getPeerConnection(): RTCPeerConnection {
|
|
||||||
return this.pc;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Start connection timeout
|
|
||||||
*/
|
|
||||||
private startConnectionTimeout(): void {
|
|
||||||
this.connectionTimer = setTimeout(() => {
|
|
||||||
if (this.pc.connectionState !== 'connected') {
|
|
||||||
this.emit('error', new Error('Connection timeout'));
|
|
||||||
this.close();
|
|
||||||
}
|
|
||||||
}, this.connectionTimeoutMs);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Clear connection timeout
|
|
||||||
*/
|
|
||||||
private clearConnectionTimeout(): void {
|
|
||||||
if (this.connectionTimer) {
|
|
||||||
clearTimeout(this.connectionTimer);
|
|
||||||
this.connectionTimer = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Close the connection and cleanup resources
|
|
||||||
*/
|
|
||||||
close(): void {
|
|
||||||
if (this.isClosed) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.isClosed = true;
|
|
||||||
|
|
||||||
this.stopPolling();
|
|
||||||
this.clearConnectionTimeout();
|
|
||||||
|
|
||||||
// Close all data channels
|
|
||||||
this.dataChannels.forEach(dc => {
|
|
||||||
if (dc.readyState === 'open' || dc.readyState === 'connecting') {
|
|
||||||
dc.close();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
this.dataChannels.clear();
|
|
||||||
|
|
||||||
// Close peer connection
|
|
||||||
if (this.pc.connectionState !== 'closed') {
|
|
||||||
this.pc.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
this.emit('disconnect');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,17 +1,37 @@
|
|||||||
/**
|
/**
|
||||||
* Simple EventEmitter implementation for browser and Node.js compatibility
|
* Type-safe EventEmitter implementation for browser and Node.js compatibility
|
||||||
|
*
|
||||||
|
* @template EventMap - A type mapping event names to their handler signatures
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```typescript
|
||||||
|
* interface MyEvents {
|
||||||
|
* 'data': (value: string) => void;
|
||||||
|
* 'error': (error: Error) => void;
|
||||||
|
* 'ready': () => void;
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* class MyClass extends EventEmitter<MyEvents> {
|
||||||
|
* doSomething() {
|
||||||
|
* this.emit('data', 'hello'); // Type-safe!
|
||||||
|
* this.emit('error', new Error('oops')); // Type-safe!
|
||||||
|
* this.emit('ready'); // Type-safe!
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* const instance = new MyClass();
|
||||||
|
* instance.on('data', (value) => {
|
||||||
|
* console.log(value.toUpperCase()); // 'value' is typed as string
|
||||||
|
* });
|
||||||
|
* ```
|
||||||
*/
|
*/
|
||||||
export class EventEmitter {
|
export class EventEmitter<EventMap extends Record<string, (...args: any[]) => void>> {
|
||||||
private events: Map<string, Set<Function>>;
|
private events: Map<keyof EventMap, Set<Function>> = new Map();
|
||||||
|
|
||||||
constructor() {
|
|
||||||
this.events = new Map();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register an event listener
|
* Register an event listener
|
||||||
*/
|
*/
|
||||||
on(event: string, listener: Function): this {
|
on<K extends keyof EventMap>(event: K, listener: EventMap[K]): this {
|
||||||
if (!this.events.has(event)) {
|
if (!this.events.has(event)) {
|
||||||
this.events.set(event, new Set());
|
this.events.set(event, new Set());
|
||||||
}
|
}
|
||||||
@@ -22,18 +42,18 @@ export class EventEmitter {
|
|||||||
/**
|
/**
|
||||||
* Register a one-time event listener
|
* Register a one-time event listener
|
||||||
*/
|
*/
|
||||||
once(event: string, listener: Function): this {
|
once<K extends keyof EventMap>(event: K, listener: EventMap[K]): this {
|
||||||
const onceWrapper = (...args: any[]) => {
|
const onceWrapper = (...args: Parameters<EventMap[K]>) => {
|
||||||
this.off(event, onceWrapper);
|
this.off(event, onceWrapper as EventMap[K]);
|
||||||
listener.apply(this, args);
|
listener(...args);
|
||||||
};
|
};
|
||||||
return this.on(event, onceWrapper);
|
return this.on(event, onceWrapper as EventMap[K]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove an event listener
|
* Remove an event listener
|
||||||
*/
|
*/
|
||||||
off(event: string, listener: Function): this {
|
off<K extends keyof EventMap>(event: K, listener: EventMap[K]): this {
|
||||||
const listeners = this.events.get(event);
|
const listeners = this.events.get(event);
|
||||||
if (listeners) {
|
if (listeners) {
|
||||||
listeners.delete(listener);
|
listeners.delete(listener);
|
||||||
@@ -47,7 +67,10 @@ export class EventEmitter {
|
|||||||
/**
|
/**
|
||||||
* Emit an event
|
* Emit an event
|
||||||
*/
|
*/
|
||||||
emit(event: string, ...args: any[]): boolean {
|
protected emit<K extends keyof EventMap>(
|
||||||
|
event: K,
|
||||||
|
...args: Parameters<EventMap[K]>
|
||||||
|
): boolean {
|
||||||
const listeners = this.events.get(event);
|
const listeners = this.events.get(event);
|
||||||
if (!listeners || listeners.size === 0) {
|
if (!listeners || listeners.size === 0) {
|
||||||
return false;
|
return false;
|
||||||
@@ -55,9 +78,9 @@ export class EventEmitter {
|
|||||||
|
|
||||||
listeners.forEach(listener => {
|
listeners.forEach(listener => {
|
||||||
try {
|
try {
|
||||||
listener.apply(this, args);
|
(listener as EventMap[K])(...args);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(`Error in ${event} event listener:`, err);
|
console.error(`Error in ${String(event)} event listener:`, err);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -67,8 +90,8 @@ export class EventEmitter {
|
|||||||
/**
|
/**
|
||||||
* Remove all listeners for an event (or all events if not specified)
|
* Remove all listeners for an event (or all events if not specified)
|
||||||
*/
|
*/
|
||||||
removeAllListeners(event?: string): this {
|
removeAllListeners<K extends keyof EventMap>(event?: K): this {
|
||||||
if (event) {
|
if (event !== undefined) {
|
||||||
this.events.delete(event);
|
this.events.delete(event);
|
||||||
} else {
|
} else {
|
||||||
this.events.clear();
|
this.events.clear();
|
||||||
@@ -79,7 +102,7 @@ export class EventEmitter {
|
|||||||
/**
|
/**
|
||||||
* Get listener count for an event
|
* Get listener count for an event
|
||||||
*/
|
*/
|
||||||
listenerCount(event: string): number {
|
listenerCount<K extends keyof EventMap>(event: K): number {
|
||||||
const listeners = this.events.get(event);
|
const listeners = this.events.get(event);
|
||||||
return listeners ? listeners.size : 0;
|
return listeners ? listeners.size : 0;
|
||||||
}
|
}
|
||||||
|
|||||||
58
src/index.ts
58
src/index.ts
@@ -1,42 +1,32 @@
|
|||||||
/**
|
/**
|
||||||
* @xtr-dev/rondevu-client
|
* @xtr-dev/rondevu-client
|
||||||
* WebRTC peer signaling and discovery client
|
* WebRTC peer signaling and discovery client with topic-based discovery
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Export main WebRTC client class
|
// Export main client class
|
||||||
export { Rondevu } from './rondevu';
|
export { Rondevu } from './rondevu.js';
|
||||||
|
export type { RondevuOptions } from './rondevu.js';
|
||||||
|
|
||||||
// Export connection class
|
// Export authentication
|
||||||
export { RondevuConnection } from './connection';
|
export { RondevuAuth } from './auth.js';
|
||||||
|
export type { Credentials, FetchFunction } from './auth.js';
|
||||||
|
|
||||||
// Export low-level signaling client (for advanced usage)
|
// Export offers API
|
||||||
export { RondevuClient } from './client';
|
export { RondevuOffers } from './offers.js';
|
||||||
|
|
||||||
// Export all types
|
|
||||||
export type {
|
export type {
|
||||||
// WebRTC types
|
|
||||||
RondevuOptions,
|
|
||||||
JoinOptions,
|
|
||||||
ConnectionRole,
|
|
||||||
RondevuConnectionParams,
|
|
||||||
RondevuConnectionEvents,
|
|
||||||
// Signaling types
|
|
||||||
Side,
|
|
||||||
Session,
|
|
||||||
TopicInfo,
|
|
||||||
Pagination,
|
|
||||||
ListTopicsResponse,
|
|
||||||
ListSessionsResponse,
|
|
||||||
CreateOfferRequest,
|
CreateOfferRequest,
|
||||||
CreateOfferResponse,
|
Offer,
|
||||||
AnswerRequest,
|
IceCandidate,
|
||||||
AnswerResponse,
|
TopicInfo
|
||||||
PollRequest,
|
} from './offers.js';
|
||||||
PollOffererResponse,
|
|
||||||
PollAnswererResponse,
|
// Export bloom filter
|
||||||
PollResponse,
|
export { BloomFilter } from './bloom.js';
|
||||||
VersionResponse,
|
|
||||||
HealthResponse,
|
// Export peer manager
|
||||||
ErrorResponse,
|
export { default as RondevuPeer } from './peer/index.js';
|
||||||
RondevuClientOptions,
|
export type {
|
||||||
} from './types';
|
PeerOptions,
|
||||||
|
PeerEvents,
|
||||||
|
PeerTimeouts
|
||||||
|
} from './peer/index.js';
|
||||||
|
|||||||
327
src/offers.ts
Normal file
327
src/offers.ts
Normal file
@@ -0,0 +1,327 @@
|
|||||||
|
import { Credentials, FetchFunction } from './auth.js';
|
||||||
|
import { RondevuAuth } from './auth.js';
|
||||||
|
|
||||||
|
// Declare Buffer for Node.js compatibility
|
||||||
|
declare const Buffer: any;
|
||||||
|
|
||||||
|
export interface CreateOfferRequest {
|
||||||
|
sdp: string;
|
||||||
|
topics: string[];
|
||||||
|
ttl?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Offer {
|
||||||
|
id: string;
|
||||||
|
peerId: string;
|
||||||
|
sdp: string;
|
||||||
|
topics: string[];
|
||||||
|
createdAt?: number;
|
||||||
|
expiresAt: number;
|
||||||
|
lastSeen: number;
|
||||||
|
answererPeerId?: string;
|
||||||
|
answerSdp?: string;
|
||||||
|
answeredAt?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IceCandidate {
|
||||||
|
candidate: any; // Full candidate object as plain JSON - don't enforce structure
|
||||||
|
peerId: string;
|
||||||
|
role: 'offerer' | 'answerer';
|
||||||
|
createdAt: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface TopicInfo {
|
||||||
|
topic: string;
|
||||||
|
activePeers: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class RondevuOffers {
|
||||||
|
private fetchFn: FetchFunction;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private baseUrl: string,
|
||||||
|
private credentials: Credentials,
|
||||||
|
fetchFn?: FetchFunction
|
||||||
|
) {
|
||||||
|
// Use provided fetch or fall back to global fetch
|
||||||
|
this.fetchFn = fetchFn || ((...args) => {
|
||||||
|
if (typeof globalThis.fetch === 'function') {
|
||||||
|
return globalThis.fetch(...args);
|
||||||
|
}
|
||||||
|
throw new Error(
|
||||||
|
'fetch is not available. Please provide a fetch implementation in the constructor options.'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create one or more offers
|
||||||
|
*/
|
||||||
|
async create(offers: CreateOfferRequest[]): Promise<Offer[]> {
|
||||||
|
const response = await this.fetchFn(`${this.baseUrl}/offers`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: RondevuAuth.createAuthHeader(this.credentials),
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ offers }),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
||||||
|
throw new Error(`Failed to create offers: ${error.error || response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
return data.offers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find offers by topic with optional bloom filter
|
||||||
|
*/
|
||||||
|
async findByTopic(
|
||||||
|
topic: string,
|
||||||
|
options?: {
|
||||||
|
bloomFilter?: Uint8Array;
|
||||||
|
limit?: number;
|
||||||
|
}
|
||||||
|
): Promise<Offer[]> {
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
|
||||||
|
if (options?.bloomFilter) {
|
||||||
|
// Convert to base64
|
||||||
|
const binaryString = String.fromCharCode(...Array.from(options.bloomFilter));
|
||||||
|
const base64 = typeof btoa !== 'undefined'
|
||||||
|
? btoa(binaryString)
|
||||||
|
: (typeof Buffer !== 'undefined' ? Buffer.from(options.bloomFilter).toString('base64') : '');
|
||||||
|
params.set('bloom', base64);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options?.limit) {
|
||||||
|
params.set('limit', options.limit.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = `${this.baseUrl}/offers/by-topic/${encodeURIComponent(topic)}${
|
||||||
|
params.toString() ? '?' + params.toString() : ''
|
||||||
|
}`;
|
||||||
|
|
||||||
|
const response = await this.fetchFn(url, {
|
||||||
|
method: 'GET',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
||||||
|
throw new Error(`Failed to find offers: ${error.error || response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
return data.offers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all offers from a specific peer
|
||||||
|
*/
|
||||||
|
async getByPeerId(peerId: string): Promise<{
|
||||||
|
offers: Offer[];
|
||||||
|
topics: string[];
|
||||||
|
}> {
|
||||||
|
const response = await this.fetchFn(`${this.baseUrl}/peers/${encodeURIComponent(peerId)}/offers`, {
|
||||||
|
method: 'GET',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
||||||
|
throw new Error(`Failed to get peer offers: ${error.error || response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get topics with active peer counts (paginated)
|
||||||
|
*/
|
||||||
|
async getTopics(options?: {
|
||||||
|
limit?: number;
|
||||||
|
offset?: number;
|
||||||
|
}): Promise<{
|
||||||
|
topics: TopicInfo[];
|
||||||
|
total: number;
|
||||||
|
limit: number;
|
||||||
|
offset: number;
|
||||||
|
}> {
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
|
||||||
|
if (options?.limit) {
|
||||||
|
params.set('limit', options.limit.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options?.offset) {
|
||||||
|
params.set('offset', options.offset.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = `${this.baseUrl}/topics${
|
||||||
|
params.toString() ? '?' + params.toString() : ''
|
||||||
|
}`;
|
||||||
|
|
||||||
|
const response = await this.fetchFn(url, {
|
||||||
|
method: 'GET',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
||||||
|
throw new Error(`Failed to get topics: ${error.error || response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get own offers
|
||||||
|
*/
|
||||||
|
async getMine(): Promise<Offer[]> {
|
||||||
|
const response = await this.fetchFn(`${this.baseUrl}/offers/mine`, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
Authorization: RondevuAuth.createAuthHeader(this.credentials),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
||||||
|
throw new Error(`Failed to get own offers: ${error.error || response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
return data.offers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update offer heartbeat
|
||||||
|
*/
|
||||||
|
async heartbeat(offerId: string): Promise<void> {
|
||||||
|
const response = await this.fetchFn(`${this.baseUrl}/offers/${encodeURIComponent(offerId)}/heartbeat`, {
|
||||||
|
method: 'PUT',
|
||||||
|
headers: {
|
||||||
|
Authorization: RondevuAuth.createAuthHeader(this.credentials),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
||||||
|
throw new Error(`Failed to update heartbeat: ${error.error || response.statusText}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete an offer
|
||||||
|
*/
|
||||||
|
async delete(offerId: string): Promise<void> {
|
||||||
|
const response = await this.fetchFn(`${this.baseUrl}/offers/${encodeURIComponent(offerId)}`, {
|
||||||
|
method: 'DELETE',
|
||||||
|
headers: {
|
||||||
|
Authorization: RondevuAuth.createAuthHeader(this.credentials),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
||||||
|
throw new Error(`Failed to delete offer: ${error.error || response.statusText}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Answer an offer
|
||||||
|
*/
|
||||||
|
async answer(offerId: string, sdp: string): Promise<void> {
|
||||||
|
const response = await this.fetchFn(`${this.baseUrl}/offers/${encodeURIComponent(offerId)}/answer`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: RondevuAuth.createAuthHeader(this.credentials),
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ sdp }),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
||||||
|
throw new Error(`Failed to answer offer: ${error.error || response.statusText}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get answers to your offers
|
||||||
|
*/
|
||||||
|
async getAnswers(): Promise<Array<{
|
||||||
|
offerId: string;
|
||||||
|
answererId: string;
|
||||||
|
sdp: string;
|
||||||
|
answeredAt: number;
|
||||||
|
topics: string[];
|
||||||
|
}>> {
|
||||||
|
const response = await this.fetchFn(`${this.baseUrl}/offers/answers`, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
Authorization: RondevuAuth.createAuthHeader(this.credentials),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
||||||
|
throw new Error(`Failed to get answers: ${error.error || response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
return data.answers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Post ICE candidates for an offer
|
||||||
|
*/
|
||||||
|
async addIceCandidates(
|
||||||
|
offerId: string,
|
||||||
|
candidates: any[]
|
||||||
|
): Promise<void> {
|
||||||
|
const response = await this.fetchFn(`${this.baseUrl}/offers/${encodeURIComponent(offerId)}/ice-candidates`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
Authorization: RondevuAuth.createAuthHeader(this.credentials),
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ candidates }),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
||||||
|
throw new Error(`Failed to add ICE candidates: ${error.error || response.statusText}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get ICE candidates for an offer
|
||||||
|
*/
|
||||||
|
async getIceCandidates(offerId: string, since?: number): Promise<IceCandidate[]> {
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
if (since !== undefined) {
|
||||||
|
params.set('since', since.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = `${this.baseUrl}/offers/${encodeURIComponent(offerId)}/ice-candidates${
|
||||||
|
params.toString() ? '?' + params.toString() : ''
|
||||||
|
}`;
|
||||||
|
|
||||||
|
const response = await this.fetchFn(url, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
Authorization: RondevuAuth.createAuthHeader(this.credentials),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
||||||
|
throw new Error(`Failed to get ICE candidates: ${error.error || response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
return data.candidates;
|
||||||
|
}
|
||||||
|
}
|
||||||
45
src/peer/answering-state.ts
Normal file
45
src/peer/answering-state.ts
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import { PeerState } from './state.js';
|
||||||
|
import type { PeerOptions } from './types.js';
|
||||||
|
import type RondevuPeer from './index.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Answering an offer and sending to server
|
||||||
|
*/
|
||||||
|
export class AnsweringState extends PeerState {
|
||||||
|
constructor(peer: RondevuPeer) {
|
||||||
|
super(peer);
|
||||||
|
}
|
||||||
|
|
||||||
|
get name() { return 'answering'; }
|
||||||
|
|
||||||
|
async answer(offerId: string, offerSdp: string, options: PeerOptions): Promise<void> {
|
||||||
|
try {
|
||||||
|
this.peer.role = 'answerer';
|
||||||
|
this.peer.offerId = offerId;
|
||||||
|
|
||||||
|
// Set remote description
|
||||||
|
await this.peer.pc.setRemoteDescription({
|
||||||
|
type: 'offer',
|
||||||
|
sdp: offerSdp
|
||||||
|
});
|
||||||
|
|
||||||
|
// Create answer
|
||||||
|
const answer = await this.peer.pc.createAnswer();
|
||||||
|
await this.peer.pc.setLocalDescription(answer);
|
||||||
|
|
||||||
|
// Send answer to server immediately (don't wait for ICE)
|
||||||
|
await this.peer.offersApi.answer(offerId, answer.sdp!);
|
||||||
|
|
||||||
|
// Enable trickle ICE - send candidates as they arrive
|
||||||
|
this.setupIceCandidateHandler(offerId);
|
||||||
|
|
||||||
|
// Transition to exchanging ICE
|
||||||
|
const { ExchangingIceState } = await import('./exchanging-ice-state.js');
|
||||||
|
this.peer.setState(new ExchangingIceState(this.peer, offerId, options));
|
||||||
|
} catch (error) {
|
||||||
|
const { FailedState } = await import('./failed-state.js');
|
||||||
|
this.peer.setState(new FailedState(this.peer, error as Error));
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
12
src/peer/closed-state.ts
Normal file
12
src/peer/closed-state.ts
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
import { PeerState } from './state.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Closed state - connection has been terminated
|
||||||
|
*/
|
||||||
|
export class ClosedState extends PeerState {
|
||||||
|
get name() { return 'closed'; }
|
||||||
|
|
||||||
|
cleanup(): void {
|
||||||
|
this.peer.pc.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
13
src/peer/connected-state.ts
Normal file
13
src/peer/connected-state.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
import { PeerState } from './state.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Connected state - peer connection is established
|
||||||
|
*/
|
||||||
|
export class ConnectedState extends PeerState {
|
||||||
|
get name() { return 'connected'; }
|
||||||
|
|
||||||
|
cleanup(): void {
|
||||||
|
// Keep connection alive, but stop any polling
|
||||||
|
// The peer connection will handle disconnects via onconnectionstatechange
|
||||||
|
}
|
||||||
|
}
|
||||||
55
src/peer/creating-offer-state.ts
Normal file
55
src/peer/creating-offer-state.ts
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
import { PeerState } from './state.js';
|
||||||
|
import type { PeerOptions } from './types.js';
|
||||||
|
import type RondevuPeer from './index.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creating offer and sending to server
|
||||||
|
*/
|
||||||
|
export class CreatingOfferState extends PeerState {
|
||||||
|
constructor(peer: RondevuPeer, private options: PeerOptions) {
|
||||||
|
super(peer);
|
||||||
|
}
|
||||||
|
|
||||||
|
get name() { return 'creating-offer'; }
|
||||||
|
|
||||||
|
async createOffer(options: PeerOptions): Promise<string> {
|
||||||
|
try {
|
||||||
|
this.peer.role = 'offerer';
|
||||||
|
|
||||||
|
// Create data channel if requested
|
||||||
|
if (options.createDataChannel !== false) {
|
||||||
|
const channel = this.peer.pc.createDataChannel(
|
||||||
|
options.dataChannelLabel || 'data'
|
||||||
|
);
|
||||||
|
this.peer.emitEvent('datachannel', channel);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create WebRTC offer
|
||||||
|
const offer = await this.peer.pc.createOffer();
|
||||||
|
await this.peer.pc.setLocalDescription(offer);
|
||||||
|
|
||||||
|
// Send offer to server immediately (don't wait for ICE)
|
||||||
|
const offers = await this.peer.offersApi.create([{
|
||||||
|
sdp: offer.sdp!,
|
||||||
|
topics: options.topics,
|
||||||
|
ttl: options.ttl || 300000
|
||||||
|
}]);
|
||||||
|
|
||||||
|
const offerId = offers[0].id;
|
||||||
|
this.peer.offerId = offerId;
|
||||||
|
|
||||||
|
// Enable trickle ICE - send candidates as they arrive
|
||||||
|
this.setupIceCandidateHandler(offerId);
|
||||||
|
|
||||||
|
// Transition to waiting for answer
|
||||||
|
const { WaitingForAnswerState } = await import('./waiting-for-answer-state.js');
|
||||||
|
this.peer.setState(new WaitingForAnswerState(this.peer, offerId, options));
|
||||||
|
|
||||||
|
return offerId;
|
||||||
|
} catch (error) {
|
||||||
|
const { FailedState } = await import('./failed-state.js');
|
||||||
|
this.peer.setState(new FailedState(this.peer, error as Error));
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
74
src/peer/exchanging-ice-state.ts
Normal file
74
src/peer/exchanging-ice-state.ts
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
import { PeerState } from './state.js';
|
||||||
|
import type { PeerOptions } from './types.js';
|
||||||
|
import type RondevuPeer from './index.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exchanging ICE candidates and waiting for connection
|
||||||
|
*/
|
||||||
|
export class ExchangingIceState extends PeerState {
|
||||||
|
private pollingInterval?: ReturnType<typeof setInterval>;
|
||||||
|
private timeout?: ReturnType<typeof setTimeout>;
|
||||||
|
private lastIceTimestamp = 0;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
peer: RondevuPeer,
|
||||||
|
private offerId: string,
|
||||||
|
private options: PeerOptions
|
||||||
|
) {
|
||||||
|
super(peer);
|
||||||
|
this.startPolling();
|
||||||
|
}
|
||||||
|
|
||||||
|
get name() { return 'exchanging-ice'; }
|
||||||
|
|
||||||
|
private startPolling(): void {
|
||||||
|
const connectionTimeout = this.options.timeouts?.iceConnection || 30000;
|
||||||
|
|
||||||
|
this.timeout = setTimeout(async () => {
|
||||||
|
this.cleanup();
|
||||||
|
const { FailedState } = await import('./failed-state.js');
|
||||||
|
this.peer.setState(new FailedState(
|
||||||
|
this.peer,
|
||||||
|
new Error('ICE connection timeout')
|
||||||
|
));
|
||||||
|
}, connectionTimeout);
|
||||||
|
|
||||||
|
this.pollingInterval = setInterval(async () => {
|
||||||
|
try {
|
||||||
|
const candidates = await this.peer.offersApi.getIceCandidates(
|
||||||
|
this.offerId,
|
||||||
|
this.lastIceTimestamp
|
||||||
|
);
|
||||||
|
|
||||||
|
for (const cand of candidates) {
|
||||||
|
if (cand.candidate && cand.candidate.candidate && cand.candidate.candidate !== '') {
|
||||||
|
try {
|
||||||
|
await this.peer.pc.addIceCandidate(new RTCIceCandidate(cand.candidate));
|
||||||
|
this.lastIceTimestamp = cand.createdAt;
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('Failed to add ICE candidate:', err);
|
||||||
|
this.lastIceTimestamp = cand.createdAt;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.lastIceTimestamp = cand.createdAt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error polling for ICE candidates:', err);
|
||||||
|
if (err instanceof Error && err.message.includes('not found')) {
|
||||||
|
this.cleanup();
|
||||||
|
const { FailedState } = await import('./failed-state.js');
|
||||||
|
this.peer.setState(new FailedState(
|
||||||
|
this.peer,
|
||||||
|
new Error('Offer expired or not found')
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup(): void {
|
||||||
|
if (this.pollingInterval) clearInterval(this.pollingInterval);
|
||||||
|
if (this.timeout) clearTimeout(this.timeout);
|
||||||
|
}
|
||||||
|
}
|
||||||
18
src/peer/failed-state.ts
Normal file
18
src/peer/failed-state.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import { PeerState } from './state.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Failed state - connection attempt failed
|
||||||
|
*/
|
||||||
|
export class FailedState extends PeerState {
|
||||||
|
constructor(peer: any, private error: Error) {
|
||||||
|
super(peer);
|
||||||
|
peer.emitEvent('failed', error);
|
||||||
|
}
|
||||||
|
|
||||||
|
get name() { return 'failed'; }
|
||||||
|
|
||||||
|
cleanup(): void {
|
||||||
|
// Connection is failed, clean up resources
|
||||||
|
this.peer.pc.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
18
src/peer/idle-state.ts
Normal file
18
src/peer/idle-state.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import { PeerState } from './state.js';
|
||||||
|
import type { PeerOptions } from './types.js';
|
||||||
|
|
||||||
|
export class IdleState extends PeerState {
|
||||||
|
get name() { return 'idle'; }
|
||||||
|
|
||||||
|
async createOffer(options: PeerOptions): Promise<string> {
|
||||||
|
const { CreatingOfferState } = await import('./creating-offer-state.js');
|
||||||
|
this.peer.setState(new CreatingOfferState(this.peer, options));
|
||||||
|
return this.peer.state.createOffer(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
async answer(offerId: string, offerSdp: string, options: PeerOptions): Promise<void> {
|
||||||
|
const { AnsweringState } = await import('./answering-state.js');
|
||||||
|
this.peer.setState(new AnsweringState(this.peer));
|
||||||
|
return this.peer.state.answer(offerId, offerSdp, options);
|
||||||
|
}
|
||||||
|
}
|
||||||
175
src/peer/index.ts
Normal file
175
src/peer/index.ts
Normal file
@@ -0,0 +1,175 @@
|
|||||||
|
import { RondevuOffers } from '../offers.js';
|
||||||
|
import { EventEmitter } from '../event-emitter.js';
|
||||||
|
import type { PeerOptions, PeerEvents } from './types.js';
|
||||||
|
import { PeerState } from './state.js';
|
||||||
|
import { IdleState } from './idle-state.js';
|
||||||
|
import { CreatingOfferState } from './creating-offer-state.js';
|
||||||
|
import { WaitingForAnswerState } from './waiting-for-answer-state.js';
|
||||||
|
import { AnsweringState } from './answering-state.js';
|
||||||
|
import { ExchangingIceState } from './exchanging-ice-state.js';
|
||||||
|
import { ConnectedState } from './connected-state.js';
|
||||||
|
import { FailedState } from './failed-state.js';
|
||||||
|
import { ClosedState } from './closed-state.js';
|
||||||
|
|
||||||
|
// Re-export types for external consumers
|
||||||
|
export type { PeerTimeouts, PeerOptions, PeerEvents } from './types.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* High-level WebRTC peer connection manager with state-based lifecycle
|
||||||
|
* Handles offer/answer exchange, ICE candidates, timeouts, and error recovery
|
||||||
|
*/
|
||||||
|
export default class RondevuPeer extends EventEmitter<PeerEvents> {
|
||||||
|
pc: RTCPeerConnection;
|
||||||
|
offersApi: RondevuOffers;
|
||||||
|
offerId?: string;
|
||||||
|
role?: 'offerer' | 'answerer';
|
||||||
|
|
||||||
|
private _state: PeerState;
|
||||||
|
|
||||||
|
// Event handler references for cleanup
|
||||||
|
private connectionStateChangeHandler?: () => void;
|
||||||
|
private dataChannelHandler?: (event: RTCDataChannelEvent) => void;
|
||||||
|
private trackHandler?: (event: RTCTrackEvent) => void;
|
||||||
|
private iceCandidateErrorHandler?: (event: Event) => void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Current connection state name
|
||||||
|
*/
|
||||||
|
get stateName(): string {
|
||||||
|
return this._state.name;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Current state object (internal use)
|
||||||
|
*/
|
||||||
|
get state(): PeerState {
|
||||||
|
return this._state;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RTCPeerConnection state
|
||||||
|
*/
|
||||||
|
get connectionState(): RTCPeerConnectionState {
|
||||||
|
return this.pc.connectionState;
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
offersApi: RondevuOffers,
|
||||||
|
rtcConfig: RTCConfiguration = {
|
||||||
|
iceServers: [
|
||||||
|
{ urls: 'stun:stun.l.google.com:19302' },
|
||||||
|
{ urls: 'stun:stun1.l.google.com:19302' }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
super();
|
||||||
|
this.offersApi = offersApi;
|
||||||
|
this.pc = new RTCPeerConnection(rtcConfig);
|
||||||
|
this._state = new IdleState(this);
|
||||||
|
|
||||||
|
this.setupPeerConnection();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up peer connection event handlers
|
||||||
|
*/
|
||||||
|
private setupPeerConnection(): void {
|
||||||
|
this.connectionStateChangeHandler = () => {
|
||||||
|
switch (this.pc.connectionState) {
|
||||||
|
case 'connected':
|
||||||
|
this.setState(new ConnectedState(this));
|
||||||
|
this.emitEvent('connected');
|
||||||
|
break;
|
||||||
|
case 'disconnected':
|
||||||
|
this.emitEvent('disconnected');
|
||||||
|
break;
|
||||||
|
case 'failed':
|
||||||
|
this.setState(new FailedState(this, new Error('Connection failed')));
|
||||||
|
break;
|
||||||
|
case 'closed':
|
||||||
|
this.setState(new ClosedState(this));
|
||||||
|
this.emitEvent('disconnected');
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
this.pc.addEventListener('connectionstatechange', this.connectionStateChangeHandler);
|
||||||
|
|
||||||
|
this.dataChannelHandler = (event: RTCDataChannelEvent) => {
|
||||||
|
this.emitEvent('datachannel', event.channel);
|
||||||
|
};
|
||||||
|
this.pc.addEventListener('datachannel', this.dataChannelHandler);
|
||||||
|
|
||||||
|
this.trackHandler = (event: RTCTrackEvent) => {
|
||||||
|
this.emitEvent('track', event);
|
||||||
|
};
|
||||||
|
this.pc.addEventListener('track', this.trackHandler);
|
||||||
|
|
||||||
|
this.iceCandidateErrorHandler = (event: Event) => {
|
||||||
|
console.error('ICE candidate error:', event);
|
||||||
|
};
|
||||||
|
this.pc.addEventListener('icecandidateerror', this.iceCandidateErrorHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set new state and emit state change event
|
||||||
|
*/
|
||||||
|
setState(newState: PeerState): void {
|
||||||
|
this._state.cleanup();
|
||||||
|
this._state = newState;
|
||||||
|
this.emitEvent('state', newState.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Emit event (exposed for PeerState classes)
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
emitEvent<K extends keyof PeerEvents>(
|
||||||
|
event: K,
|
||||||
|
...args: Parameters<PeerEvents[K]>
|
||||||
|
): void {
|
||||||
|
this.emit(event, ...args);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create an offer and advertise on topics
|
||||||
|
*/
|
||||||
|
async createOffer(options: PeerOptions): Promise<string> {
|
||||||
|
return this._state.createOffer(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Answer an existing offer
|
||||||
|
*/
|
||||||
|
async answer(offerId: string, offerSdp: string, options: PeerOptions): Promise<void> {
|
||||||
|
return this._state.answer(offerId, offerSdp, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a media track to the connection
|
||||||
|
*/
|
||||||
|
addTrack(track: MediaStreamTrack, ...streams: MediaStream[]): RTCRtpSender {
|
||||||
|
return this.pc.addTrack(track, ...streams);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Close the connection and clean up
|
||||||
|
*/
|
||||||
|
async close(): Promise<void> {
|
||||||
|
// Remove RTCPeerConnection event listeners
|
||||||
|
if (this.connectionStateChangeHandler) {
|
||||||
|
this.pc.removeEventListener('connectionstatechange', this.connectionStateChangeHandler);
|
||||||
|
}
|
||||||
|
if (this.dataChannelHandler) {
|
||||||
|
this.pc.removeEventListener('datachannel', this.dataChannelHandler);
|
||||||
|
}
|
||||||
|
if (this.trackHandler) {
|
||||||
|
this.pc.removeEventListener('track', this.trackHandler);
|
||||||
|
}
|
||||||
|
if (this.iceCandidateErrorHandler) {
|
||||||
|
this.pc.removeEventListener('icecandidateerror', this.iceCandidateErrorHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
await this._state.close();
|
||||||
|
this.removeAllListeners();
|
||||||
|
}
|
||||||
|
}
|
||||||
66
src/peer/state.ts
Normal file
66
src/peer/state.ts
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
import type { PeerOptions } from './types.js';
|
||||||
|
import type RondevuPeer from './index.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base class for peer connection states
|
||||||
|
* Implements the State pattern for managing WebRTC connection lifecycle
|
||||||
|
*/
|
||||||
|
export abstract class PeerState {
|
||||||
|
protected iceCandidateHandler?: (event: RTCPeerConnectionIceEvent) => void;
|
||||||
|
|
||||||
|
constructor(protected peer: RondevuPeer) {}
|
||||||
|
|
||||||
|
abstract get name(): string;
|
||||||
|
|
||||||
|
async createOffer(options: PeerOptions): Promise<string> {
|
||||||
|
throw new Error(`Cannot create offer in ${this.name} state`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async answer(offerId: string, offerSdp: string, options: PeerOptions): Promise<void> {
|
||||||
|
throw new Error(`Cannot answer in ${this.name} state`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async handleAnswer(sdp: string): Promise<void> {
|
||||||
|
throw new Error(`Cannot handle answer in ${this.name} state`);
|
||||||
|
}
|
||||||
|
|
||||||
|
async handleIceCandidate(candidate: any): Promise<void> {
|
||||||
|
// ICE candidates can arrive in multiple states, so default is to add them
|
||||||
|
if (this.peer.pc.remoteDescription) {
|
||||||
|
await this.peer.pc.addIceCandidate(new RTCIceCandidate(candidate));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup trickle ICE candidate handler
|
||||||
|
* Sends local ICE candidates to server as they are discovered
|
||||||
|
*/
|
||||||
|
protected setupIceCandidateHandler(offerId: string): void {
|
||||||
|
this.iceCandidateHandler = async (event: RTCPeerConnectionIceEvent) => {
|
||||||
|
if (event.candidate && offerId) {
|
||||||
|
const candidateData = event.candidate.toJSON();
|
||||||
|
if (candidateData.candidate && candidateData.candidate !== '') {
|
||||||
|
try {
|
||||||
|
await this.peer.offersApi.addIceCandidates(offerId, [candidateData]);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error sending ICE candidate:', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
this.peer.pc.addEventListener('icecandidate', this.iceCandidateHandler);
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup(): void {
|
||||||
|
// Clean up ICE candidate handler if it exists
|
||||||
|
if (this.iceCandidateHandler) {
|
||||||
|
this.peer.pc.removeEventListener('icecandidate', this.iceCandidateHandler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async close(): Promise<void> {
|
||||||
|
this.cleanup();
|
||||||
|
const { ClosedState } = await import('./closed-state.js');
|
||||||
|
this.peer.setState(new ClosedState(this.peer));
|
||||||
|
}
|
||||||
|
}
|
||||||
43
src/peer/types.ts
Normal file
43
src/peer/types.ts
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
/**
|
||||||
|
* Timeout configurations for different connection phases
|
||||||
|
*/
|
||||||
|
export interface PeerTimeouts {
|
||||||
|
/** Timeout for ICE gathering (default: 10000ms) */
|
||||||
|
iceGathering?: number;
|
||||||
|
/** Timeout for waiting for answer (default: 30000ms) */
|
||||||
|
waitingForAnswer?: number;
|
||||||
|
/** Timeout for creating answer (default: 10000ms) */
|
||||||
|
creatingAnswer?: number;
|
||||||
|
/** Timeout for ICE connection (default: 30000ms) */
|
||||||
|
iceConnection?: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Options for creating a peer connection
|
||||||
|
*/
|
||||||
|
export interface PeerOptions {
|
||||||
|
/** RTCConfiguration for the peer connection */
|
||||||
|
rtcConfig?: RTCConfiguration;
|
||||||
|
/** Topics to advertise this connection under */
|
||||||
|
topics: string[];
|
||||||
|
/** How long the offer should live (milliseconds) */
|
||||||
|
ttl?: number;
|
||||||
|
/** Whether to create a data channel automatically (for offerer) */
|
||||||
|
createDataChannel?: boolean;
|
||||||
|
/** Label for the automatically created data channel */
|
||||||
|
dataChannelLabel?: string;
|
||||||
|
/** Timeout configurations */
|
||||||
|
timeouts?: PeerTimeouts;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Events emitted by RondevuPeer
|
||||||
|
*/
|
||||||
|
export interface PeerEvents extends Record<string, (...args: any[]) => void> {
|
||||||
|
'state': (state: string) => void;
|
||||||
|
'connected': () => void;
|
||||||
|
'disconnected': () => void;
|
||||||
|
'failed': (error: Error) => void;
|
||||||
|
'datachannel': (channel: RTCDataChannel) => void;
|
||||||
|
'track': (event: RTCTrackEvent) => void;
|
||||||
|
}
|
||||||
78
src/peer/waiting-for-answer-state.ts
Normal file
78
src/peer/waiting-for-answer-state.ts
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
import { PeerState } from './state.js';
|
||||||
|
import type { PeerOptions } from './types.js';
|
||||||
|
import type RondevuPeer from './index.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Waiting for answer from another peer
|
||||||
|
*/
|
||||||
|
export class WaitingForAnswerState extends PeerState {
|
||||||
|
private pollingInterval?: ReturnType<typeof setInterval>;
|
||||||
|
private timeout?: ReturnType<typeof setTimeout>;
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
peer: RondevuPeer,
|
||||||
|
private offerId: string,
|
||||||
|
private options: PeerOptions
|
||||||
|
) {
|
||||||
|
super(peer);
|
||||||
|
this.startPolling();
|
||||||
|
}
|
||||||
|
|
||||||
|
get name() { return 'waiting-for-answer'; }
|
||||||
|
|
||||||
|
private startPolling(): void {
|
||||||
|
const answerTimeout = this.options.timeouts?.waitingForAnswer || 30000;
|
||||||
|
|
||||||
|
this.timeout = setTimeout(async () => {
|
||||||
|
this.cleanup();
|
||||||
|
const { FailedState } = await import('./failed-state.js');
|
||||||
|
this.peer.setState(new FailedState(
|
||||||
|
this.peer,
|
||||||
|
new Error('Timeout waiting for answer')
|
||||||
|
));
|
||||||
|
}, answerTimeout);
|
||||||
|
|
||||||
|
this.pollingInterval = setInterval(async () => {
|
||||||
|
try {
|
||||||
|
const answers = await this.peer.offersApi.getAnswers();
|
||||||
|
const myAnswer = answers.find((a: any) => a.offerId === this.offerId);
|
||||||
|
|
||||||
|
if (myAnswer) {
|
||||||
|
this.cleanup();
|
||||||
|
await this.handleAnswer(myAnswer.sdp);
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error polling for answers:', err);
|
||||||
|
if (err instanceof Error && err.message.includes('not found')) {
|
||||||
|
this.cleanup();
|
||||||
|
const { FailedState } = await import('./failed-state.js');
|
||||||
|
this.peer.setState(new FailedState(
|
||||||
|
this.peer,
|
||||||
|
new Error('Offer expired or not found')
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 2000);
|
||||||
|
}
|
||||||
|
|
||||||
|
async handleAnswer(sdp: string): Promise<void> {
|
||||||
|
try {
|
||||||
|
await this.peer.pc.setRemoteDescription({
|
||||||
|
type: 'answer',
|
||||||
|
sdp
|
||||||
|
});
|
||||||
|
|
||||||
|
// Transition to exchanging ICE
|
||||||
|
const { ExchangingIceState } = await import('./exchanging-ice-state.js');
|
||||||
|
this.peer.setState(new ExchangingIceState(this.peer, this.offerId, this.options));
|
||||||
|
} catch (error) {
|
||||||
|
const { FailedState } = await import('./failed-state.js');
|
||||||
|
this.peer.setState(new FailedState(this.peer, error as Error));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cleanup(): void {
|
||||||
|
if (this.pollingInterval) clearInterval(this.pollingInterval);
|
||||||
|
if (this.timeout) clearTimeout(this.timeout);
|
||||||
|
}
|
||||||
|
}
|
||||||
306
src/rondevu.ts
306
src/rondevu.ts
@@ -1,273 +1,103 @@
|
|||||||
import { RondevuClient } from './client';
|
import { RondevuAuth, Credentials, FetchFunction } from './auth.js';
|
||||||
import { RondevuConnection } from './connection';
|
import { RondevuOffers } from './offers.js';
|
||||||
import { RondevuOptions, JoinOptions, RondevuConnectionParams } from './types';
|
import RondevuPeer from './peer/index.js';
|
||||||
|
|
||||||
/**
|
export interface RondevuOptions {
|
||||||
* Main Rondevu WebRTC client with automatic connection management
|
/**
|
||||||
|
* Base URL of the Rondevu server
|
||||||
|
* @default 'https://api.ronde.vu'
|
||||||
*/
|
*/
|
||||||
|
baseUrl?: string;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Existing credentials (peerId + secret) to skip registration
|
||||||
|
*/
|
||||||
|
credentials?: Credentials;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom fetch implementation for environments without native fetch
|
||||||
|
* (Node.js < 18, some Workers environments, etc.)
|
||||||
|
*
|
||||||
|
* @example Node.js
|
||||||
|
* ```typescript
|
||||||
|
* import fetch from 'node-fetch';
|
||||||
|
* const client = new Rondevu({ fetch });
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
fetch?: FetchFunction;
|
||||||
|
}
|
||||||
|
|
||||||
export class Rondevu {
|
export class Rondevu {
|
||||||
readonly peerId: string;
|
readonly auth: RondevuAuth;
|
||||||
|
private _offers?: RondevuOffers;
|
||||||
private client: RondevuClient;
|
private credentials?: Credentials;
|
||||||
private baseUrl: string;
|
private baseUrl: string;
|
||||||
private fetchImpl?: typeof fetch;
|
private fetchFn?: FetchFunction;
|
||||||
private rtcConfig?: RTCConfiguration;
|
|
||||||
private pollingInterval: number;
|
|
||||||
private connectionTimeout: number;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new Rondevu client instance
|
|
||||||
* @param options - Client configuration options
|
|
||||||
*/
|
|
||||||
constructor(options: RondevuOptions = {}) {
|
constructor(options: RondevuOptions = {}) {
|
||||||
this.baseUrl = options.baseUrl || 'https://rondevu.xtrdev.workers.dev';
|
this.baseUrl = options.baseUrl || 'https://api.ronde.vu';
|
||||||
this.fetchImpl = options.fetch;
|
this.fetchFn = options.fetch;
|
||||||
|
|
||||||
this.client = new RondevuClient({
|
this.auth = new RondevuAuth(this.baseUrl, this.fetchFn);
|
||||||
baseUrl: this.baseUrl,
|
|
||||||
fetch: options.fetch,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Auto-generate peer ID if not provided
|
if (options.credentials) {
|
||||||
this.peerId = options.peerId || this.generatePeerId();
|
this.credentials = options.credentials;
|
||||||
this.rtcConfig = options.rtcConfig;
|
this._offers = new RondevuOffers(this.baseUrl, this.credentials, this.fetchFn);
|
||||||
this.pollingInterval = options.pollingInterval || 1000;
|
}
|
||||||
this.connectionTimeout = options.connectionTimeout || 30000;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a unique peer ID
|
* Get offers API (requires authentication)
|
||||||
*/
|
*/
|
||||||
private generatePeerId(): string {
|
get offers(): RondevuOffers {
|
||||||
return `rdv_${Math.random().toString(36).substring(2, 14)}`;
|
if (!this._offers) {
|
||||||
|
throw new Error('Not authenticated. Call register() first or provide credentials.');
|
||||||
|
}
|
||||||
|
return this._offers;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Update the peer ID (useful when user identity changes)
|
* Register and initialize authenticated client
|
||||||
*/
|
*/
|
||||||
updatePeerId(newPeerId: string): void {
|
async register(): Promise<Credentials> {
|
||||||
(this as any).peerId = newPeerId;
|
this.credentials = await this.auth.register();
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// Create offers API instance
|
||||||
* Create a new connection (offerer role)
|
this._offers = new RondevuOffers(
|
||||||
* @param id - Connection identifier
|
this.baseUrl,
|
||||||
* @param topic - Topic name for grouping connections
|
this.credentials,
|
||||||
* @returns Promise that resolves to RondevuConnection
|
this.fetchFn
|
||||||
*/
|
|
||||||
async create(id: string, topic: string): Promise<RondevuConnection> {
|
|
||||||
// Create peer connection
|
|
||||||
const pc = new RTCPeerConnection(this.rtcConfig);
|
|
||||||
|
|
||||||
// Create initial data channel for negotiation (required for offer creation)
|
|
||||||
pc.createDataChannel('_negotiation');
|
|
||||||
|
|
||||||
// Generate offer
|
|
||||||
const offer = await pc.createOffer();
|
|
||||||
await pc.setLocalDescription(offer);
|
|
||||||
|
|
||||||
// Wait for ICE gathering to complete
|
|
||||||
await this.waitForIceGathering(pc);
|
|
||||||
|
|
||||||
// Create session on server with custom code
|
|
||||||
await this.client.createOffer(topic, {
|
|
||||||
peerId: this.peerId,
|
|
||||||
offer: pc.localDescription!.sdp,
|
|
||||||
code: id,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create connection object
|
|
||||||
const connectionParams: RondevuConnectionParams = {
|
|
||||||
id,
|
|
||||||
topic,
|
|
||||||
role: 'offerer',
|
|
||||||
pc,
|
|
||||||
localPeerId: this.peerId,
|
|
||||||
remotePeerId: '', // Will be populated when answer is received
|
|
||||||
pollingInterval: this.pollingInterval,
|
|
||||||
connectionTimeout: this.connectionTimeout,
|
|
||||||
};
|
|
||||||
|
|
||||||
const connection = new RondevuConnection(connectionParams, this.client);
|
|
||||||
|
|
||||||
// Start polling for answer
|
|
||||||
connection.startPolling();
|
|
||||||
|
|
||||||
return connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Connect to an existing connection by ID (answerer role)
|
|
||||||
* @param id - Connection identifier
|
|
||||||
* @returns Promise that resolves to RondevuConnection
|
|
||||||
*/
|
|
||||||
async connect(id: string): Promise<RondevuConnection> {
|
|
||||||
// Poll server to get session by ID
|
|
||||||
const sessionData = await this.findSessionByIdWithClient(id, this.client);
|
|
||||||
|
|
||||||
if (!sessionData) {
|
|
||||||
throw new Error(`Connection ${id} not found or expired`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create peer connection
|
|
||||||
const pc = new RTCPeerConnection(this.rtcConfig);
|
|
||||||
|
|
||||||
// Set remote offer
|
|
||||||
await pc.setRemoteDescription({
|
|
||||||
type: 'offer',
|
|
||||||
sdp: sessionData.offer,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Generate answer
|
|
||||||
const answer = await pc.createAnswer();
|
|
||||||
await pc.setLocalDescription(answer);
|
|
||||||
|
|
||||||
// Wait for ICE gathering
|
|
||||||
await this.waitForIceGathering(pc);
|
|
||||||
|
|
||||||
// Send answer to server
|
|
||||||
await this.client.sendAnswer({
|
|
||||||
code: id,
|
|
||||||
answer: pc.localDescription!.sdp,
|
|
||||||
side: 'answerer',
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create connection object
|
|
||||||
const connectionParams: RondevuConnectionParams = {
|
|
||||||
id,
|
|
||||||
topic: sessionData.topic || 'unknown',
|
|
||||||
role: 'answerer',
|
|
||||||
pc,
|
|
||||||
localPeerId: this.peerId,
|
|
||||||
remotePeerId: sessionData.peerId,
|
|
||||||
pollingInterval: this.pollingInterval,
|
|
||||||
connectionTimeout: this.connectionTimeout,
|
|
||||||
};
|
|
||||||
|
|
||||||
const connection = new RondevuConnection(connectionParams, this.client);
|
|
||||||
|
|
||||||
// Start polling for ICE candidates
|
|
||||||
connection.startPolling();
|
|
||||||
|
|
||||||
return connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Join a topic and discover available peers (answerer role)
|
|
||||||
* @param topic - Topic name
|
|
||||||
* @param options - Optional join options for filtering and selection
|
|
||||||
* @returns Promise that resolves to RondevuConnection
|
|
||||||
*/
|
|
||||||
async join(topic: string, options?: JoinOptions): Promise<RondevuConnection> {
|
|
||||||
// List sessions in topic
|
|
||||||
const { sessions } = await this.client.listSessions(topic);
|
|
||||||
|
|
||||||
// Filter out self (sessions with our peer ID)
|
|
||||||
let availableSessions = sessions.filter(
|
|
||||||
session => session.peerId !== this.peerId
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// Apply custom filter if provided
|
return this.credentials;
|
||||||
if (options?.filter) {
|
|
||||||
availableSessions = availableSessions.filter(options.filter);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (availableSessions.length === 0) {
|
|
||||||
throw new Error(`No available peers in topic: ${topic}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Select session based on strategy
|
|
||||||
const selectedSession = this.selectSession(
|
|
||||||
availableSessions,
|
|
||||||
options?.select || 'first'
|
|
||||||
);
|
|
||||||
|
|
||||||
// Connect to selected session
|
|
||||||
return this.connect(selectedSession.code);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Select a session based on strategy
|
* Check if client is authenticated
|
||||||
*/
|
*/
|
||||||
private selectSession(
|
isAuthenticated(): boolean {
|
||||||
sessions: Array<{ code: string; peerId: string; createdAt: number }>,
|
return !!this.credentials;
|
||||||
strategy: 'first' | 'newest' | 'oldest' | 'random'
|
|
||||||
): { code: string; peerId: string; createdAt: number } {
|
|
||||||
switch (strategy) {
|
|
||||||
case 'first':
|
|
||||||
return sessions[0];
|
|
||||||
case 'newest':
|
|
||||||
return sessions.reduce((newest, session) =>
|
|
||||||
session.createdAt > newest.createdAt ? session : newest
|
|
||||||
);
|
|
||||||
case 'oldest':
|
|
||||||
return sessions.reduce((oldest, session) =>
|
|
||||||
session.createdAt < oldest.createdAt ? session : oldest
|
|
||||||
);
|
|
||||||
case 'random':
|
|
||||||
return sessions[Math.floor(Math.random() * sessions.length)];
|
|
||||||
default:
|
|
||||||
return sessions[0];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wait for ICE gathering to complete
|
* Get current credentials
|
||||||
*/
|
*/
|
||||||
private async waitForIceGathering(pc: RTCPeerConnection): Promise<void> {
|
getCredentials(): Credentials | undefined {
|
||||||
if (pc.iceGatheringState === 'complete') {
|
return this.credentials;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
const checkState = () => {
|
|
||||||
if (pc.iceGatheringState === 'complete') {
|
|
||||||
pc.removeEventListener('icegatheringstatechange', checkState);
|
|
||||||
resolve();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
pc.addEventListener('icegatheringstatechange', checkState);
|
|
||||||
|
|
||||||
// Also set a timeout in case gathering takes too long
|
|
||||||
setTimeout(() => {
|
|
||||||
pc.removeEventListener('icegatheringstatechange', checkState);
|
|
||||||
resolve();
|
|
||||||
}, 5000);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a session by connection ID
|
* Create a new WebRTC peer connection (requires authentication)
|
||||||
* This requires polling since we don't know which topic it's in
|
* This is a high-level helper that creates and manages WebRTC connections with state management
|
||||||
|
*
|
||||||
|
* @param rtcConfig Optional RTCConfiguration for the peer connection
|
||||||
|
* @returns RondevuPeer instance
|
||||||
*/
|
*/
|
||||||
private async findSessionByIdWithClient(
|
createPeer(rtcConfig?: RTCConfiguration): RondevuPeer {
|
||||||
id: string,
|
if (!this._offers) {
|
||||||
client: RondevuClient
|
throw new Error('Not authenticated. Call register() first or provide credentials.');
|
||||||
): Promise<{
|
|
||||||
code: string;
|
|
||||||
peerId: string;
|
|
||||||
offer: string;
|
|
||||||
topic?: string;
|
|
||||||
} | null> {
|
|
||||||
try {
|
|
||||||
// Try to poll for the session directly
|
|
||||||
// The poll endpoint should return the session data
|
|
||||||
const response = await client.poll(id, 'answerer');
|
|
||||||
const answererResponse = response as { offer: string; offerCandidates: string[] };
|
|
||||||
|
|
||||||
if (answererResponse.offer) {
|
|
||||||
return {
|
|
||||||
code: id,
|
|
||||||
peerId: '', // Will be populated from session data
|
|
||||||
offer: answererResponse.offer,
|
|
||||||
topic: undefined,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return new RondevuPeer(this._offers, rtcConfig);
|
||||||
} catch (err) {
|
|
||||||
throw new Error(`Failed to find session ${id}: ${(err as Error).message}`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
88
src/types.ts
88
src/types.ts
@@ -8,64 +8,7 @@
|
|||||||
export type Side = 'offerer' | 'answerer';
|
export type Side = 'offerer' | 'answerer';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Session information returned from discovery endpoints
|
* Request body for POST /offer
|
||||||
*/
|
|
||||||
export interface Session {
|
|
||||||
/** Unique session identifier (UUID) */
|
|
||||||
code: string;
|
|
||||||
/** Peer identifier/metadata */
|
|
||||||
peerId: string;
|
|
||||||
/** Signaling data for peer connection */
|
|
||||||
offer: string;
|
|
||||||
/** Additional signaling data from offerer */
|
|
||||||
offerCandidates: string[];
|
|
||||||
/** Unix timestamp when session was created */
|
|
||||||
createdAt: number;
|
|
||||||
/** Unix timestamp when session expires */
|
|
||||||
expiresAt: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Topic information with peer count
|
|
||||||
*/
|
|
||||||
export interface TopicInfo {
|
|
||||||
/** Topic identifier */
|
|
||||||
topic: string;
|
|
||||||
/** Number of available peers in this topic */
|
|
||||||
count: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Pagination information
|
|
||||||
*/
|
|
||||||
export interface Pagination {
|
|
||||||
/** Current page number */
|
|
||||||
page: number;
|
|
||||||
/** Results per page */
|
|
||||||
limit: number;
|
|
||||||
/** Total number of results */
|
|
||||||
total: number;
|
|
||||||
/** Whether there are more results available */
|
|
||||||
hasMore: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from GET / - list all topics
|
|
||||||
*/
|
|
||||||
export interface ListTopicsResponse {
|
|
||||||
topics: TopicInfo[];
|
|
||||||
pagination: Pagination;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from GET /:topic/sessions - list sessions in a topic
|
|
||||||
*/
|
|
||||||
export interface ListSessionsResponse {
|
|
||||||
sessions: Session[];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Request body for POST /:topic/offer
|
|
||||||
*/
|
*/
|
||||||
export interface CreateOfferRequest {
|
export interface CreateOfferRequest {
|
||||||
/** Peer identifier/metadata (max 1024 characters) */
|
/** Peer identifier/metadata (max 1024 characters) */
|
||||||
@@ -77,7 +20,7 @@ export interface CreateOfferRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Response from POST /:topic/offer
|
* Response from POST /offer
|
||||||
*/
|
*/
|
||||||
export interface CreateOfferResponse {
|
export interface CreateOfferResponse {
|
||||||
/** Unique session identifier (UUID) */
|
/** Unique session identifier (UUID) */
|
||||||
@@ -154,6 +97,7 @@ export interface VersionResponse {
|
|||||||
export interface HealthResponse {
|
export interface HealthResponse {
|
||||||
status: 'ok';
|
status: 'ok';
|
||||||
timestamp: number;
|
timestamp: number;
|
||||||
|
version: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -177,11 +121,20 @@ export interface RondevuClientOptions {
|
|||||||
// WebRTC Types
|
// WebRTC Types
|
||||||
// ============================================================================
|
// ============================================================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WebRTC polyfill for Node.js and other non-browser platforms
|
||||||
|
*/
|
||||||
|
export interface WebRTCPolyfill {
|
||||||
|
RTCPeerConnection: typeof RTCPeerConnection;
|
||||||
|
RTCSessionDescription: typeof RTCSessionDescription;
|
||||||
|
RTCIceCandidate: typeof RTCIceCandidate;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Configuration options for Rondevu WebRTC client
|
* Configuration options for Rondevu WebRTC client
|
||||||
*/
|
*/
|
||||||
export interface RondevuOptions {
|
export interface RondevuOptions {
|
||||||
/** Base URL of the Rondevu server (defaults to 'https://rondevu.xtrdev.workers.dev') */
|
/** Base URL of the Rondevu server (defaults to 'https://api.ronde.vu') */
|
||||||
baseUrl?: string;
|
baseUrl?: string;
|
||||||
/** Peer identifier (optional, auto-generated if not provided) */
|
/** Peer identifier (optional, auto-generated if not provided) */
|
||||||
peerId?: string;
|
peerId?: string;
|
||||||
@@ -193,16 +146,8 @@ export interface RondevuOptions {
|
|||||||
pollingInterval?: number;
|
pollingInterval?: number;
|
||||||
/** Connection timeout in milliseconds (default: 30000) */
|
/** Connection timeout in milliseconds (default: 30000) */
|
||||||
connectionTimeout?: number;
|
connectionTimeout?: number;
|
||||||
}
|
/** WebRTC polyfill for Node.js (e.g., wrtc or @roamhq/wrtc) */
|
||||||
|
wrtc?: WebRTCPolyfill;
|
||||||
/**
|
|
||||||
* Options for joining a topic
|
|
||||||
*/
|
|
||||||
export interface JoinOptions {
|
|
||||||
/** Filter function to select specific sessions */
|
|
||||||
filter?: (session: { code: string; peerId: string }) => boolean;
|
|
||||||
/** Selection strategy for choosing a session */
|
|
||||||
select?: 'first' | 'newest' | 'oldest' | 'random';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -215,13 +160,14 @@ export type ConnectionRole = 'offerer' | 'answerer';
|
|||||||
*/
|
*/
|
||||||
export interface RondevuConnectionParams {
|
export interface RondevuConnectionParams {
|
||||||
id: string;
|
id: string;
|
||||||
topic: string;
|
topic?: string;
|
||||||
role: ConnectionRole;
|
role: ConnectionRole;
|
||||||
pc: RTCPeerConnection;
|
pc: RTCPeerConnection;
|
||||||
localPeerId: string;
|
localPeerId: string;
|
||||||
remotePeerId: string;
|
remotePeerId: string;
|
||||||
pollingInterval: number;
|
pollingInterval: number;
|
||||||
connectionTimeout: number;
|
connectionTimeout: number;
|
||||||
|
wrtc?: WebRTCPolyfill;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user