mirror of
https://github.com/xtr-dev/rondevu-client.git
synced 2025-12-10 10:53:24 +00:00
Compare commits
55 Commits
v0.1.2
...
b2d42fa776
| Author | SHA1 | Date | |
|---|---|---|---|
| b2d42fa776 | |||
| 63e14ddc5b | |||
| c9f6119148 | |||
| 15f821f08a | |||
| 895e7765f9 | |||
| 49d3984640 | |||
| 6057c3c582 | |||
| 255fe42a43 | |||
| 83fd0f84a4 | |||
| aa53d5bc3d | |||
| f5aa6e2189 | |||
| afdca83640 | |||
| c7ea1b9b8f | |||
| 660663945e | |||
| f119a42fcd | |||
| cd55072acb | |||
| 26f71e7a2b | |||
| 0ac1f94502 | |||
| 3530213870 | |||
| e052464482 | |||
| 53206d306b | |||
| c860419e66 | |||
| e22e74fb74 | |||
| 135eda73cf | |||
| 8d7075ccc4 | |||
| db8f0f4ced | |||
| 3a227a21ac | |||
| de1f3eac9c | |||
| 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 |
721
README.md
721
README.md
@@ -1,82 +1,693 @@
|
||||
# Rondevu
|
||||
|
||||
🎯 **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
|
||||
# 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.
|
||||
🌐 **DNS-like WebRTC client with username claiming and service discovery**
|
||||
|
||||
### Install
|
||||
TypeScript/JavaScript client for Rondevu, providing cryptographic username claiming, service publishing, and privacy-preserving discovery.
|
||||
|
||||
**Related repositories:**
|
||||
- [@xtr-dev/rondevu-client](https://github.com/xtr-dev/rondevu-client) - TypeScript client library ([npm](https://www.npmjs.com/package/@xtr-dev/rondevu-client))
|
||||
- [@xtr-dev/rondevu-server](https://github.com/xtr-dev/rondevu-server) - HTTP signaling server ([npm](https://www.npmjs.com/package/@xtr-dev/rondevu-server), [live](https://api.ronde.vu))
|
||||
- [@xtr-dev/rondevu-demo](https://github.com/xtr-dev/rondevu-demo) - Interactive demo ([live](https://ronde.vu))
|
||||
|
||||
---
|
||||
|
||||
## Features
|
||||
|
||||
- **Username Claiming**: Cryptographic ownership with Ed25519 signatures
|
||||
- **Service Publishing**: Package-style naming (com.example.chat@1.0.0)
|
||||
- **Privacy-Preserving Discovery**: UUID-based service index
|
||||
- **Public/Private Services**: Control service visibility
|
||||
- **Complete WebRTC Signaling**: Full offer/answer and ICE candidate exchange
|
||||
- **Trickle ICE**: Send ICE candidates as they're discovered
|
||||
- **TypeScript**: Full type safety and autocomplete
|
||||
|
||||
## Install
|
||||
|
||||
```bash
|
||||
npm install @xtr-dev/rondevu-client
|
||||
```
|
||||
|
||||
### Usage
|
||||
## Quick Start
|
||||
|
||||
### Publishing a Service (Alice)
|
||||
|
||||
```typescript
|
||||
import { Rondevu } from '@xtr-dev/rondevu-client';
|
||||
|
||||
const rdv = new Rondevu({
|
||||
baseUrl: 'https://server.com',
|
||||
rtcConfig: {
|
||||
iceServers: [
|
||||
// your ICE servers here
|
||||
{ urls: 'stun:stun.l.google.com:19302' },
|
||||
{ urls: 'stun:stun1.l.google.com:19302' },
|
||||
{
|
||||
urls: 'turn:relay1.example.com:3480',
|
||||
username: 'example',
|
||||
credential: 'example'
|
||||
}
|
||||
]
|
||||
// Initialize client and register
|
||||
const client = new Rondevu({ baseUrl: 'https://api.ronde.vu' });
|
||||
await client.register();
|
||||
|
||||
// Step 1: Claim username (one-time)
|
||||
const claim = await client.usernames.claimUsername('alice');
|
||||
client.usernames.saveKeypairToStorage('alice', claim.publicKey, claim.privateKey);
|
||||
|
||||
console.log(`Username claimed: ${claim.username}`);
|
||||
console.log(`Expires: ${new Date(claim.expiresAt)}`);
|
||||
|
||||
// Step 2: Expose service with handler
|
||||
const keypair = client.usernames.loadKeypairFromStorage('alice');
|
||||
|
||||
const handle = await client.services.exposeService({
|
||||
username: 'alice',
|
||||
privateKey: keypair.privateKey,
|
||||
serviceFqn: 'com.example.chat@1.0.0',
|
||||
isPublic: true,
|
||||
handler: (channel, peer) => {
|
||||
console.log('📡 New connection established');
|
||||
|
||||
channel.onmessage = (e) => {
|
||||
console.log('📥 Received:', e.data);
|
||||
channel.send(`Echo: ${e.data}`);
|
||||
};
|
||||
|
||||
channel.onopen = () => {
|
||||
console.log('✅ Data channel open');
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// Connect by topic
|
||||
const conn = await rdv.join('room');
|
||||
console.log(`Service published with UUID: ${handle.uuid}`);
|
||||
console.log('Waiting for connections...');
|
||||
|
||||
// Or connect by ID
|
||||
const conn = await rdv.connect('meeting-123');
|
||||
// Later: unpublish
|
||||
await handle.unpublish();
|
||||
```
|
||||
|
||||
// Use the connection
|
||||
conn.on('connect', () => {
|
||||
const channel = conn.dataChannel('chat');
|
||||
channel.send('Hello!');
|
||||
### Connecting to a Service (Bob)
|
||||
|
||||
```typescript
|
||||
import { Rondevu } from '@xtr-dev/rondevu-client';
|
||||
|
||||
// Initialize client and register
|
||||
const client = new Rondevu({ baseUrl: 'https://api.ronde.vu' });
|
||||
await client.register();
|
||||
|
||||
// Option 1: Connect by username + FQN
|
||||
const { peer, channel } = await client.discovery.connect(
|
||||
'alice',
|
||||
'com.example.chat@1.0.0'
|
||||
);
|
||||
|
||||
channel.onmessage = (e) => {
|
||||
console.log('📥 Received:', e.data);
|
||||
};
|
||||
|
||||
channel.onopen = () => {
|
||||
console.log('✅ Connected!');
|
||||
channel.send('Hello Alice!');
|
||||
};
|
||||
|
||||
peer.on('connected', () => {
|
||||
console.log('🎉 WebRTC connection established');
|
||||
});
|
||||
|
||||
peer.on('failed', (error) => {
|
||||
console.error('❌ Connection failed:', error);
|
||||
});
|
||||
|
||||
// Option 2: List services first, then connect
|
||||
const services = await client.discovery.listServices('alice');
|
||||
console.log(`Found ${services.services.length} services`);
|
||||
|
||||
for (const service of services.services) {
|
||||
console.log(`- UUID: ${service.uuid}`);
|
||||
if (service.isPublic) {
|
||||
console.log(` FQN: ${service.serviceFqn}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Connect by UUID
|
||||
const { peer: peer2, channel: channel2 } = await client.discovery.connectByUuid(
|
||||
services.services[0].uuid
|
||||
);
|
||||
```
|
||||
|
||||
## API Reference
|
||||
|
||||
### Main Client
|
||||
|
||||
```typescript
|
||||
const client = new Rondevu({
|
||||
baseUrl: 'https://api.ronde.vu', // optional, default shown
|
||||
credentials?: { peerId, secret }, // optional, skip registration
|
||||
fetch?: customFetch, // optional, for Node.js < 18
|
||||
RTCPeerConnection?: RTCPeerConnection, // optional, for Node.js
|
||||
RTCSessionDescription?: RTCSessionDescription,
|
||||
RTCIceCandidate?: RTCIceCandidate
|
||||
});
|
||||
|
||||
// Register and get credentials
|
||||
const creds = await client.register();
|
||||
// { peerId: '...', secret: '...' }
|
||||
|
||||
// Check if authenticated
|
||||
client.isAuthenticated(); // boolean
|
||||
|
||||
// Get current credentials
|
||||
client.getCredentials(); // { peerId, secret } | undefined
|
||||
```
|
||||
|
||||
### Username API
|
||||
|
||||
```typescript
|
||||
// Check username availability
|
||||
const check = await client.usernames.checkUsername('alice');
|
||||
// { available: true } or { available: false, expiresAt: number, publicKey: string }
|
||||
|
||||
// Claim username with new keypair
|
||||
const claim = await client.usernames.claimUsername('alice');
|
||||
// { username, publicKey, privateKey, claimedAt, expiresAt }
|
||||
|
||||
// Claim with existing keypair
|
||||
const keypair = await client.usernames.generateKeypair();
|
||||
const claim2 = await client.usernames.claimUsername('bob', keypair);
|
||||
|
||||
// Save keypair to localStorage
|
||||
client.usernames.saveKeypairToStorage('alice', publicKey, privateKey);
|
||||
|
||||
// Load keypair from localStorage
|
||||
const stored = client.usernames.loadKeypairFromStorage('alice');
|
||||
// { publicKey, privateKey } | null
|
||||
|
||||
// Export keypair for backup
|
||||
const exported = client.usernames.exportKeypair('alice');
|
||||
// { username, publicKey, privateKey }
|
||||
|
||||
// Import keypair from backup
|
||||
client.usernames.importKeypair({ username: 'alice', publicKey, privateKey });
|
||||
|
||||
// Low-level: Generate keypair
|
||||
const { publicKey, privateKey } = await client.usernames.generateKeypair();
|
||||
|
||||
// Low-level: Sign message
|
||||
const signature = await client.usernames.signMessage(
|
||||
'claim:alice:1234567890',
|
||||
privateKey
|
||||
);
|
||||
|
||||
// Low-level: Verify signature
|
||||
const valid = await client.usernames.verifySignature(
|
||||
'claim:alice:1234567890',
|
||||
signature,
|
||||
publicKey
|
||||
);
|
||||
```
|
||||
|
||||
**Username Rules:**
|
||||
- Format: Lowercase alphanumeric + dash (`a-z`, `0-9`, `-`)
|
||||
- Length: 3-32 characters
|
||||
- Pattern: `^[a-z0-9][a-z0-9-]*[a-z0-9]$`
|
||||
- Validity: 365 days from claim/last use
|
||||
- Ownership: Secured by Ed25519 public key
|
||||
|
||||
### Services API
|
||||
|
||||
```typescript
|
||||
// Publish service (returns UUID)
|
||||
const service = await client.services.publishService({
|
||||
username: 'alice',
|
||||
privateKey: keypair.privateKey,
|
||||
serviceFqn: 'com.example.chat@1.0.0',
|
||||
isPublic: false, // optional, default false
|
||||
metadata: { description: '...' }, // optional
|
||||
ttl: 5 * 60 * 1000, // optional, default 5 minutes
|
||||
rtcConfig: { ... } // optional RTCConfiguration
|
||||
});
|
||||
// { serviceId, uuid, offerId, expiresAt }
|
||||
|
||||
console.log(`Service UUID: ${service.uuid}`);
|
||||
console.log('Share this UUID to allow connections');
|
||||
|
||||
// Expose service with automatic connection handling
|
||||
const handle = await client.services.exposeService({
|
||||
username: 'alice',
|
||||
privateKey: keypair.privateKey,
|
||||
serviceFqn: 'com.example.echo@1.0.0',
|
||||
isPublic: true,
|
||||
handler: (channel, peer) => {
|
||||
channel.onmessage = (e) => {
|
||||
console.log('Received:', e.data);
|
||||
channel.send(`Echo: ${e.data}`);
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// Later: unpublish
|
||||
await handle.unpublish();
|
||||
|
||||
// Unpublish service manually
|
||||
await client.services.unpublishService(serviceId, username);
|
||||
```
|
||||
|
||||
#### Multi-Connection Service Hosting (Offer Pooling)
|
||||
|
||||
By default, `exposeService()` creates a single offer and can only accept one connection. To handle multiple concurrent connections, use the `poolSize` option to enable **offer pooling**:
|
||||
|
||||
```typescript
|
||||
// Expose service with offer pooling for multiple concurrent connections
|
||||
const handle = await client.services.exposeService({
|
||||
username: 'alice',
|
||||
privateKey: keypair.privateKey,
|
||||
serviceFqn: 'com.example.chat@1.0.0',
|
||||
isPublic: true,
|
||||
poolSize: 5, // Maintain 5 simultaneous open offers
|
||||
pollingInterval: 2000, // Optional: polling interval in ms (default: 2000)
|
||||
handler: (channel, peer, connectionId) => {
|
||||
console.log(`📡 New connection: ${connectionId}`);
|
||||
|
||||
channel.onmessage = (e) => {
|
||||
console.log(`📥 [${connectionId}] Received:`, e.data);
|
||||
channel.send(`Echo: ${e.data}`);
|
||||
};
|
||||
|
||||
channel.onclose = () => {
|
||||
console.log(`👋 [${connectionId}] Connection closed`);
|
||||
};
|
||||
},
|
||||
onPoolStatus: (status) => {
|
||||
console.log('Pool status:', {
|
||||
activeOffers: status.activeOffers,
|
||||
activeConnections: status.activeConnections,
|
||||
totalHandled: status.totalConnectionsHandled
|
||||
});
|
||||
},
|
||||
onError: (error, context) => {
|
||||
console.error(`Pool error (${context}):`, error);
|
||||
}
|
||||
});
|
||||
|
||||
// Get current pool status
|
||||
const status = handle.getStatus();
|
||||
console.log(`Active offers: ${status.activeOffers}`);
|
||||
console.log(`Active connections: ${status.activeConnections}`);
|
||||
|
||||
// Manually add more offers if needed
|
||||
await handle.addOffers(3);
|
||||
```
|
||||
|
||||
**How Offer Pooling Works:**
|
||||
1. The pool maintains `poolSize` simultaneous open offers at all times
|
||||
2. When an offer is answered (connection established), a new offer is automatically created
|
||||
3. Polling checks for answers every `pollingInterval` milliseconds (default: 2000ms)
|
||||
4. Each connection gets a unique `connectionId` passed to the handler
|
||||
5. No limit on total concurrent connections - only pool size (open offers) is controlled
|
||||
|
||||
**Use Cases:**
|
||||
- Chat servers handling multiple clients
|
||||
- File sharing services with concurrent downloads
|
||||
- Multiplayer game lobbies
|
||||
- Collaborative editing sessions
|
||||
- Any service that needs to accept multiple simultaneous connections
|
||||
|
||||
**Pool Status Interface:**
|
||||
```typescript
|
||||
interface PoolStatus {
|
||||
activeOffers: number; // Current number of open offers
|
||||
activeConnections: number; // Current number of connected peers
|
||||
totalConnectionsHandled: number; // Total connections since start
|
||||
failedOfferCreations: number; // Failed offer creation attempts
|
||||
}
|
||||
```
|
||||
|
||||
**Pooled Service Handle:**
|
||||
```typescript
|
||||
interface PooledServiceHandle extends ServiceHandle {
|
||||
getStatus: () => PoolStatus; // Get current pool status
|
||||
addOffers: (count: number) => Promise<void>; // Manually add offers
|
||||
}
|
||||
```
|
||||
|
||||
**Service FQN Format:**
|
||||
- Service name: Reverse domain notation (e.g., `com.example.chat`)
|
||||
- Version: Semantic versioning (e.g., `1.0.0`, `2.1.3-beta`)
|
||||
- Complete FQN: `service-name@version`
|
||||
- Examples: `com.example.chat@1.0.0`, `io.github.alice.notes@0.1.0-beta`
|
||||
|
||||
**Validation Rules:**
|
||||
- Service name pattern: `^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$`
|
||||
- Length: 3-128 characters
|
||||
- Minimum 2 components (at least one dot)
|
||||
- Version pattern: `^[0-9]+\.[0-9]+\.[0-9]+(-[a-z0-9.-]+)?$`
|
||||
|
||||
### Discovery API
|
||||
|
||||
```typescript
|
||||
// List all services for a username
|
||||
const services = await client.discovery.listServices('alice');
|
||||
// {
|
||||
// username: 'alice',
|
||||
// services: [
|
||||
// { uuid: 'abc123', isPublic: false },
|
||||
// { uuid: 'def456', isPublic: true, serviceFqn: '...', metadata: {...} }
|
||||
// ]
|
||||
// }
|
||||
|
||||
// Query service by FQN
|
||||
const query = await client.discovery.queryService('alice', 'com.example.chat@1.0.0');
|
||||
// { uuid: 'abc123', allowed: true }
|
||||
|
||||
// Get service details by UUID
|
||||
const details = await client.discovery.getServiceDetails('abc123');
|
||||
// { serviceId, username, serviceFqn, offerId, sdp, isPublic, metadata, ... }
|
||||
|
||||
// Connect to service by UUID
|
||||
const peer = await client.discovery.connectToService('abc123', {
|
||||
rtcConfig: { ... }, // optional
|
||||
onConnected: () => { ... }, // optional
|
||||
onData: (data) => { ... } // optional
|
||||
});
|
||||
|
||||
// Connect by username + FQN (convenience method)
|
||||
const { peer, channel } = await client.discovery.connect(
|
||||
'alice',
|
||||
'com.example.chat@1.0.0',
|
||||
{ rtcConfig: { ... } } // optional
|
||||
);
|
||||
|
||||
// Connect by UUID with channel
|
||||
const { peer, channel } = await client.discovery.connectByUuid('abc123');
|
||||
```
|
||||
|
||||
### Low-Level Peer Connection
|
||||
|
||||
```typescript
|
||||
// Create peer connection
|
||||
const peer = client.createPeer({
|
||||
iceServers: [
|
||||
{ urls: 'stun:stun.l.google.com:19302' },
|
||||
{
|
||||
urls: 'turn:turn.example.com:3478',
|
||||
username: 'user',
|
||||
credential: 'pass'
|
||||
}
|
||||
],
|
||||
iceTransportPolicy: 'relay' // optional: force TURN relay
|
||||
});
|
||||
|
||||
// Event listeners
|
||||
peer.on('state', (state) => {
|
||||
console.log('Peer state:', state);
|
||||
});
|
||||
|
||||
peer.on('connected', () => {
|
||||
console.log('✅ Connected');
|
||||
});
|
||||
|
||||
peer.on('disconnected', () => {
|
||||
console.log('🔌 Disconnected');
|
||||
});
|
||||
|
||||
peer.on('failed', (error) => {
|
||||
console.error('❌ Failed:', error);
|
||||
});
|
||||
|
||||
peer.on('datachannel', (channel) => {
|
||||
console.log('📡 Data channel ready');
|
||||
});
|
||||
|
||||
peer.on('track', (event) => {
|
||||
// Media track received
|
||||
const stream = event.streams[0];
|
||||
videoElement.srcObject = stream;
|
||||
});
|
||||
|
||||
// Create offer
|
||||
const offerId = await peer.createOffer({
|
||||
ttl: 300000, // optional
|
||||
timeouts: { // optional
|
||||
iceGathering: 10000,
|
||||
waitingForAnswer: 30000,
|
||||
creatingAnswer: 10000,
|
||||
iceConnection: 30000
|
||||
}
|
||||
});
|
||||
|
||||
// Answer offer
|
||||
await peer.answer(offerId, sdp);
|
||||
|
||||
// Add media tracks
|
||||
const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
|
||||
stream.getTracks().forEach(track => {
|
||||
peer.addTrack(track, stream);
|
||||
});
|
||||
|
||||
// Close connection
|
||||
await peer.close();
|
||||
|
||||
// Properties
|
||||
peer.stateName; // 'idle', 'creating-offer', 'connected', etc.
|
||||
peer.connectionState; // RTCPeerConnectionState
|
||||
peer.offerId; // string | undefined
|
||||
peer.role; // 'offerer' | 'answerer' | undefined
|
||||
```
|
||||
|
||||
## Connection Lifecycle
|
||||
|
||||
### Service Publisher (Offerer)
|
||||
1. **idle** - Initial state
|
||||
2. **creating-offer** - Creating WebRTC offer
|
||||
3. **waiting-for-answer** - Polling for answer from peer
|
||||
4. **exchanging-ice** - Exchanging ICE candidates
|
||||
5. **connected** - Successfully connected
|
||||
6. **failed** - Connection failed
|
||||
7. **closed** - Connection closed
|
||||
|
||||
### Service Consumer (Answerer)
|
||||
1. **idle** - Initial state
|
||||
2. **answering** - Creating WebRTC answer
|
||||
3. **exchanging-ice** - Exchanging ICE candidates
|
||||
4. **connected** - Successfully connected
|
||||
5. **failed** - Connection failed
|
||||
6. **closed** - Connection closed
|
||||
|
||||
## Platform-Specific Setup
|
||||
|
||||
### Modern Browsers
|
||||
Works out of the box - no additional setup needed.
|
||||
|
||||
### Node.js 18+
|
||||
Native fetch is available, but you need WebRTC polyfills:
|
||||
|
||||
```bash
|
||||
npm install wrtc
|
||||
```
|
||||
|
||||
```typescript
|
||||
import { Rondevu } from '@xtr-dev/rondevu-client';
|
||||
import { RTCPeerConnection, RTCSessionDescription, RTCIceCandidate } from 'wrtc';
|
||||
|
||||
const client = new Rondevu({
|
||||
baseUrl: 'https://api.ronde.vu',
|
||||
RTCPeerConnection,
|
||||
RTCSessionDescription,
|
||||
RTCIceCandidate
|
||||
});
|
||||
```
|
||||
|
||||
### API
|
||||
### Node.js < 18
|
||||
Install both fetch and WebRTC polyfills:
|
||||
|
||||
**Main Methods:**
|
||||
- `rdv.join(topic)` - Auto-connect to first peer in topic
|
||||
- `rdv.join(topic, {filter})` - Connect to specific peer by ID
|
||||
- `rdv.create(id, topic)` - Create connection for others to join
|
||||
- `rdv.connect(id)` - Join connection by ID
|
||||
```bash
|
||||
npm install node-fetch wrtc
|
||||
```
|
||||
|
||||
**Connection Events:**
|
||||
- `connect` - Connection established
|
||||
- `disconnect` - Connection closed
|
||||
- `datachannel` - Remote peer created data channel
|
||||
- `stream` - Remote media stream received
|
||||
- `error` - Error occurred
|
||||
```typescript
|
||||
import { Rondevu } from '@xtr-dev/rondevu-client';
|
||||
import fetch from 'node-fetch';
|
||||
import { RTCPeerConnection, RTCSessionDescription, RTCIceCandidate } from 'wrtc';
|
||||
|
||||
**Connection Methods:**
|
||||
- `conn.dataChannel(label)` - Get or create data channel
|
||||
- `conn.addStream(stream)` - Add media stream
|
||||
- `conn.getPeerConnection()` - Get underlying RTCPeerConnection
|
||||
- `conn.close()` - Close connection
|
||||
const client = new Rondevu({
|
||||
baseUrl: 'https://api.ronde.vu',
|
||||
fetch: fetch as any,
|
||||
RTCPeerConnection,
|
||||
RTCSessionDescription,
|
||||
RTCIceCandidate
|
||||
});
|
||||
```
|
||||
|
||||
### License
|
||||
### 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));
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
### Echo Service
|
||||
|
||||
```typescript
|
||||
// Publisher
|
||||
const client1 = new Rondevu();
|
||||
await client1.register();
|
||||
|
||||
const claim = await client1.usernames.claimUsername('alice');
|
||||
client1.usernames.saveKeypairToStorage('alice', claim.publicKey, claim.privateKey);
|
||||
|
||||
const keypair = client1.usernames.loadKeypairFromStorage('alice');
|
||||
|
||||
await client1.services.exposeService({
|
||||
username: 'alice',
|
||||
privateKey: keypair.privateKey,
|
||||
serviceFqn: 'com.example.echo@1.0.0',
|
||||
isPublic: true,
|
||||
handler: (channel, peer) => {
|
||||
channel.onmessage = (e) => {
|
||||
console.log('Received:', e.data);
|
||||
channel.send(`Echo: ${e.data}`);
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// Consumer
|
||||
const client2 = new Rondevu();
|
||||
await client2.register();
|
||||
|
||||
const { peer, channel } = await client2.discovery.connect(
|
||||
'alice',
|
||||
'com.example.echo@1.0.0'
|
||||
);
|
||||
|
||||
channel.onmessage = (e) => console.log('Received:', e.data);
|
||||
channel.send('Hello!');
|
||||
```
|
||||
|
||||
### File Transfer Service
|
||||
|
||||
```typescript
|
||||
// Publisher
|
||||
await client.services.exposeService({
|
||||
username: 'alice',
|
||||
privateKey: keypair.privateKey,
|
||||
serviceFqn: 'com.example.files@1.0.0',
|
||||
isPublic: false,
|
||||
handler: (channel, peer) => {
|
||||
channel.binaryType = 'arraybuffer';
|
||||
|
||||
channel.onmessage = (e) => {
|
||||
if (typeof e.data === 'string') {
|
||||
console.log('Request:', JSON.parse(e.data));
|
||||
} else {
|
||||
console.log('Received file chunk:', e.data.byteLength, 'bytes');
|
||||
}
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// Consumer
|
||||
const { peer, channel } = await client.discovery.connect(
|
||||
'alice',
|
||||
'com.example.files@1.0.0'
|
||||
);
|
||||
|
||||
channel.binaryType = 'arraybuffer';
|
||||
|
||||
// Request file
|
||||
channel.send(JSON.stringify({ action: 'get', path: '/readme.txt' }));
|
||||
|
||||
channel.onmessage = (e) => {
|
||||
if (e.data instanceof ArrayBuffer) {
|
||||
console.log('Received file:', e.data.byteLength, 'bytes');
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
### Video Chat Service
|
||||
|
||||
```typescript
|
||||
// Publisher
|
||||
const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: true });
|
||||
|
||||
const peer = client.createPeer();
|
||||
stream.getTracks().forEach(track => peer.addTrack(track, stream));
|
||||
|
||||
const offerId = await peer.createOffer({ ttl: 300000 });
|
||||
|
||||
await client.services.publishService({
|
||||
username: 'alice',
|
||||
privateKey: keypair.privateKey,
|
||||
serviceFqn: 'com.example.videochat@1.0.0',
|
||||
isPublic: true
|
||||
});
|
||||
|
||||
// Consumer
|
||||
const { peer, channel } = await client.discovery.connect(
|
||||
'alice',
|
||||
'com.example.videochat@1.0.0'
|
||||
);
|
||||
|
||||
peer.on('track', (event) => {
|
||||
const remoteStream = event.streams[0];
|
||||
videoElement.srcObject = remoteStream;
|
||||
});
|
||||
```
|
||||
|
||||
## TypeScript
|
||||
|
||||
All types are exported:
|
||||
|
||||
```typescript
|
||||
import type {
|
||||
Credentials,
|
||||
RondevuOptions,
|
||||
|
||||
// Username types
|
||||
UsernameCheckResult,
|
||||
UsernameClaimResult,
|
||||
Keypair,
|
||||
|
||||
// Service types
|
||||
ServicePublishResult,
|
||||
PublishServiceOptions,
|
||||
ServiceHandle,
|
||||
|
||||
// Discovery types
|
||||
ServiceInfo,
|
||||
ServiceListResult,
|
||||
ServiceQueryResult,
|
||||
ServiceDetails,
|
||||
ConnectResult,
|
||||
|
||||
// Peer types
|
||||
PeerOptions,
|
||||
PeerEvents,
|
||||
PeerTimeouts
|
||||
} from '@xtr-dev/rondevu-client';
|
||||
```
|
||||
|
||||
## Migration from V1
|
||||
|
||||
V2 is a **breaking change** that replaces topic-based discovery with username claiming and service publishing. See the main [MIGRATION.md](../MIGRATION.md) for detailed migration guide.
|
||||
|
||||
**Key Changes:**
|
||||
- ❌ Removed: `offers.findByTopic()`, `offers.getTopics()`, bloom filters
|
||||
- ✅ Added: `usernames.*`, `services.*`, `discovery.*` APIs
|
||||
- ✅ Changed: Focus on service-based discovery instead of topics
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
|
||||
49
package-lock.json
generated
Normal file
49
package-lock.json
generated
Normal file
@@ -0,0 +1,49 @@
|
||||
{
|
||||
"name": "@xtr-dev/rondevu-client",
|
||||
"version": "0.7.12",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "@xtr-dev/rondevu-client",
|
||||
"version": "0.7.12",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@noble/ed25519": "^3.0.0",
|
||||
"@xtr-dev/rondevu-client": "^0.5.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.9.3"
|
||||
}
|
||||
},
|
||||
"node_modules/@noble/ed25519": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/@noble/ed25519/-/ed25519-3.0.0.tgz",
|
||||
"integrity": "sha512-QyteqMNm0GLqfa5SoYbSC3+Pvykwpn95Zgth4MFVSMKBB75ELl9tX1LAVsN4c3HXOrakHsF2gL4zWDAYCcsnzg==",
|
||||
"license": "MIT",
|
||||
"funding": {
|
||||
"url": "https://paulmillr.com/funding/"
|
||||
}
|
||||
},
|
||||
"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",
|
||||
"version": "0.1.2",
|
||||
"description": "TypeScript client for Rondevu peer signaling and discovery server",
|
||||
"version": "0.8.3",
|
||||
"description": "TypeScript client for Rondevu DNS-like WebRTC with username claiming and service discovery",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"prepublishOnly": "npm run build",
|
||||
"publish": "npm publish"
|
||||
"prepublishOnly": "npm run build"
|
||||
},
|
||||
"keywords": [
|
||||
"webrtc",
|
||||
@@ -26,5 +25,8 @@
|
||||
"files": [
|
||||
"dist",
|
||||
"README.md"
|
||||
]
|
||||
],
|
||||
"dependencies": {
|
||||
"@noble/ed25519": "^3.0.0"
|
||||
}
|
||||
}
|
||||
|
||||
62
src/auth.ts
Normal file
62
src/auth.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
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
|
||||
* Generates a cryptographically random peer ID (128-bit)
|
||||
* @throws Error if registration fails
|
||||
*/
|
||||
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}`;
|
||||
}
|
||||
}
|
||||
239
src/client.ts
239
src/client.ts
@@ -1,239 +0,0 @@
|
||||
import {
|
||||
RondevuClientOptions,
|
||||
ListTopicsResponse,
|
||||
ListSessionsResponse,
|
||||
CreateOfferRequest,
|
||||
CreateOfferResponse,
|
||||
AnswerRequest,
|
||||
AnswerResponse,
|
||||
PollRequest,
|
||||
PollOffererResponse,
|
||||
PollAnswererResponse,
|
||||
VersionResponse,
|
||||
HealthResponse,
|
||||
ErrorResponse,
|
||||
Side,
|
||||
} from './types';
|
||||
|
||||
/**
|
||||
* HTTP client for Rondevu peer signaling and discovery server
|
||||
*/
|
||||
export class RondevuClient {
|
||||
private readonly baseUrl: string;
|
||||
private readonly fetchImpl: typeof fetch;
|
||||
|
||||
/**
|
||||
* Creates a new Rondevu client instance
|
||||
* @param options - Client configuration options
|
||||
*/
|
||||
constructor(options: RondevuClientOptions) {
|
||||
this.baseUrl = options.baseUrl.replace(/\/$/, ''); // Remove trailing slash
|
||||
this.fetchImpl = options.fetch || globalThis.fetch.bind(globalThis);
|
||||
}
|
||||
|
||||
/**
|
||||
* Makes an HTTP request to the Rondevu server
|
||||
*/
|
||||
private async request<T>(
|
||||
endpoint: string,
|
||||
options: RequestInit = {}
|
||||
): Promise<T> {
|
||||
const url = `${this.baseUrl}${endpoint}`;
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
...(options.headers as Record<string, string>),
|
||||
};
|
||||
|
||||
if (options.body) {
|
||||
headers['Content-Type'] = 'application/json';
|
||||
}
|
||||
|
||||
const response = await this.fetchImpl(url, {
|
||||
...options,
|
||||
headers,
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
const error = data as ErrorResponse;
|
||||
throw new Error(error.error || `HTTP ${response.status}: ${response.statusText}`);
|
||||
}
|
||||
|
||||
return data as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets server version information
|
||||
*
|
||||
* @returns Server version (git commit hash)
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const client = new RondevuClient({ baseUrl: 'https://example.com' });
|
||||
* const { version } = await client.getVersion();
|
||||
* console.log('Server version:', version);
|
||||
* ```
|
||||
*/
|
||||
async getVersion(): Promise<VersionResponse> {
|
||||
return this.request<VersionResponse>('/', {
|
||||
method: 'GET',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all topics with peer counts
|
||||
*
|
||||
* @param page - Page number (starting from 1)
|
||||
* @param limit - Results per page (max 1000)
|
||||
* @returns List of topics with pagination info
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const client = new RondevuClient({ baseUrl: 'https://example.com' });
|
||||
* const { topics, pagination } = await client.listTopics();
|
||||
* 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',
|
||||
* offer: signalingData
|
||||
* });
|
||||
* console.log('Session code:', code);
|
||||
* ```
|
||||
*/
|
||||
async createOffer(
|
||||
topic: string,
|
||||
request: CreateOfferRequest
|
||||
): Promise<CreateOfferResponse> {
|
||||
return this.request<CreateOfferResponse>(
|
||||
`/${encodeURIComponent(topic)}/offer`,
|
||||
{
|
||||
method: 'POST',
|
||||
body: JSON.stringify(request),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends an answer or candidate to an existing session
|
||||
*
|
||||
* @param request - Answer details including session code and signaling data
|
||||
* @returns Success confirmation
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const client = new RondevuClient({ baseUrl: 'https://example.com' });
|
||||
*
|
||||
* // Send answer
|
||||
* await client.sendAnswer({
|
||||
* code: sessionCode,
|
||||
* answer: answerData,
|
||||
* side: 'answerer'
|
||||
* });
|
||||
*
|
||||
* // Send candidate
|
||||
* await client.sendAnswer({
|
||||
* code: sessionCode,
|
||||
* candidate: candidateData,
|
||||
* side: 'offerer'
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
async sendAnswer(request: AnswerRequest): Promise<AnswerResponse> {
|
||||
return this.request<AnswerResponse>('/answer', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(request),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Polls for session data from the other peer
|
||||
*
|
||||
* @param code - Session UUID
|
||||
* @param side - Which side is polling ('offerer' or 'answerer')
|
||||
* @returns Session data including offers, answers, and candidates
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const client = new RondevuClient({ baseUrl: 'https://example.com' });
|
||||
*
|
||||
* // Offerer polls for answer
|
||||
* const offererData = await client.poll(sessionCode, 'offerer');
|
||||
* if (offererData.answer) {
|
||||
* console.log('Received answer:', offererData.answer);
|
||||
* }
|
||||
*
|
||||
* // Answerer polls for offer
|
||||
* const answererData = await client.poll(sessionCode, 'answerer');
|
||||
* console.log('Received offer:', answererData.offer);
|
||||
* ```
|
||||
*/
|
||||
async poll(
|
||||
code: string,
|
||||
side: Side
|
||||
): Promise<PollOffererResponse | PollAnswererResponse> {
|
||||
const request: PollRequest = { code, side };
|
||||
return this.request<PollOffererResponse | PollAnswererResponse>('/poll', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(request),
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks server health
|
||||
*
|
||||
* @returns Health status and timestamp
|
||||
*
|
||||
* @example
|
||||
* ```typescript
|
||||
* const client = new RondevuClient({ baseUrl: 'https://example.com' });
|
||||
* const health = await client.health();
|
||||
* console.log('Server status:', health.status);
|
||||
* ```
|
||||
*/
|
||||
async health(): Promise<HealthResponse> {
|
||||
return this.request<HealthResponse>('/health', {
|
||||
method: 'GET',
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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');
|
||||
}
|
||||
}
|
||||
276
src/discovery.ts
Normal file
276
src/discovery.ts
Normal file
@@ -0,0 +1,276 @@
|
||||
import RondevuPeer from './peer/index.js';
|
||||
import { RondevuOffers } from './offers.js';
|
||||
|
||||
/**
|
||||
* Service info from discovery
|
||||
*/
|
||||
export interface ServiceInfo {
|
||||
uuid: string;
|
||||
isPublic: boolean;
|
||||
serviceFqn?: string;
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Service list result
|
||||
*/
|
||||
export interface ServiceListResult {
|
||||
username: string;
|
||||
services: ServiceInfo[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Service query result
|
||||
*/
|
||||
export interface ServiceQueryResult {
|
||||
uuid: string;
|
||||
allowed: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Service details
|
||||
*/
|
||||
export interface ServiceDetails {
|
||||
serviceId: string;
|
||||
username: string;
|
||||
serviceFqn: string;
|
||||
offerId: string;
|
||||
sdp: string;
|
||||
isPublic: boolean;
|
||||
metadata?: Record<string, any>;
|
||||
createdAt: number;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect result
|
||||
*/
|
||||
export interface ConnectResult {
|
||||
peer: RondevuPeer;
|
||||
channel: RTCDataChannel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rondevu Discovery API
|
||||
* Handles service discovery and connections
|
||||
*/
|
||||
export class RondevuDiscovery {
|
||||
private offersApi: RondevuOffers;
|
||||
|
||||
constructor(
|
||||
private baseUrl: string,
|
||||
private credentials: { peerId: string; secret: string }
|
||||
) {
|
||||
this.offersApi = new RondevuOffers(baseUrl, credentials);
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists all services for a username
|
||||
* Returns UUIDs only for private services, full details for public
|
||||
*/
|
||||
async listServices(username: string): Promise<ServiceListResult> {
|
||||
const response = await fetch(`${this.baseUrl}/usernames/${username}/services`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to list services');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
return {
|
||||
username: data.username,
|
||||
services: data.services
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Queries a service by FQN
|
||||
* Returns UUID if service exists and is allowed
|
||||
*/
|
||||
async queryService(username: string, serviceFqn: string): Promise<ServiceQueryResult> {
|
||||
const response = await fetch(`${this.baseUrl}/index/${username}/query`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ serviceFqn })
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
throw new Error(error.error || 'Service not found');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
return {
|
||||
uuid: data.uuid,
|
||||
allowed: data.allowed
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets service details by UUID
|
||||
*/
|
||||
async getServiceDetails(uuid: string): Promise<ServiceDetails> {
|
||||
const response = await fetch(`${this.baseUrl}/services/${uuid}`);
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
throw new Error(error.error || 'Service not found');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
return {
|
||||
serviceId: data.serviceId,
|
||||
username: data.username,
|
||||
serviceFqn: data.serviceFqn,
|
||||
offerId: data.offerId,
|
||||
sdp: data.sdp,
|
||||
isPublic: data.isPublic,
|
||||
metadata: data.metadata,
|
||||
createdAt: data.createdAt,
|
||||
expiresAt: data.expiresAt
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Connects to a service by UUID
|
||||
*/
|
||||
async connectToService(
|
||||
uuid: string,
|
||||
options?: {
|
||||
rtcConfig?: RTCConfiguration;
|
||||
onConnected?: () => void;
|
||||
onData?: (data: any) => void;
|
||||
}
|
||||
): Promise<RondevuPeer> {
|
||||
// Get service details
|
||||
const service = await this.getServiceDetails(uuid);
|
||||
|
||||
// Create peer with the offer
|
||||
const peer = new RondevuPeer(
|
||||
this.offersApi,
|
||||
options?.rtcConfig || {
|
||||
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
|
||||
}
|
||||
);
|
||||
|
||||
// Set up event handlers
|
||||
if (options?.onConnected) {
|
||||
peer.on('connected', options.onConnected);
|
||||
}
|
||||
|
||||
if (options?.onData) {
|
||||
peer.on('datachannel', (channel: RTCDataChannel) => {
|
||||
channel.onmessage = (e) => options.onData!(e.data);
|
||||
});
|
||||
}
|
||||
|
||||
// Answer the offer
|
||||
await peer.answer(service.offerId, service.sdp, {
|
||||
topics: [], // V2 doesn't use topics
|
||||
rtcConfig: options?.rtcConfig
|
||||
});
|
||||
|
||||
return peer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method: Query and connect in one call
|
||||
* Returns both peer and data channel
|
||||
*/
|
||||
async connect(
|
||||
username: string,
|
||||
serviceFqn: string,
|
||||
options?: {
|
||||
rtcConfig?: RTCConfiguration;
|
||||
}
|
||||
): Promise<ConnectResult> {
|
||||
// Query service
|
||||
const query = await this.queryService(username, serviceFqn);
|
||||
|
||||
if (!query.allowed) {
|
||||
throw new Error('Service access denied');
|
||||
}
|
||||
|
||||
// Get service details
|
||||
const service = await this.getServiceDetails(query.uuid);
|
||||
|
||||
// Create peer
|
||||
const peer = new RondevuPeer(
|
||||
this.offersApi,
|
||||
options?.rtcConfig || {
|
||||
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
|
||||
}
|
||||
);
|
||||
|
||||
// Answer the offer
|
||||
await peer.answer(service.offerId, service.sdp, {
|
||||
topics: [], // V2 doesn't use topics
|
||||
rtcConfig: options?.rtcConfig
|
||||
});
|
||||
|
||||
// Wait for data channel
|
||||
const channel = await new Promise<RTCDataChannel>((resolve, reject) => {
|
||||
const timeout = setTimeout(() => {
|
||||
reject(new Error('Timeout waiting for data channel'));
|
||||
}, 30000);
|
||||
|
||||
peer.on('datachannel', (ch: RTCDataChannel) => {
|
||||
clearTimeout(timeout);
|
||||
resolve(ch);
|
||||
});
|
||||
|
||||
peer.on('failed', (error: Error) => {
|
||||
clearTimeout(timeout);
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
|
||||
return { peer, channel };
|
||||
}
|
||||
|
||||
/**
|
||||
* Convenience method: Connect to service by UUID with channel
|
||||
*/
|
||||
async connectByUuid(
|
||||
uuid: string,
|
||||
options?: { rtcConfig?: RTCConfiguration }
|
||||
): Promise<ConnectResult> {
|
||||
// Get service details
|
||||
const service = await this.getServiceDetails(uuid);
|
||||
|
||||
// Create peer
|
||||
const peer = new RondevuPeer(
|
||||
this.offersApi,
|
||||
options?.rtcConfig || {
|
||||
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
|
||||
}
|
||||
);
|
||||
|
||||
// Answer the offer
|
||||
await peer.answer(service.offerId, service.sdp, {
|
||||
topics: [], // V2 doesn't use topics
|
||||
rtcConfig: options?.rtcConfig
|
||||
});
|
||||
|
||||
// Wait for data channel
|
||||
const channel = await new Promise<RTCDataChannel>((resolve, reject) => {
|
||||
const timeout = setTimeout(() => {
|
||||
reject(new Error('Timeout waiting for data channel'));
|
||||
}, 30000);
|
||||
|
||||
peer.on('datachannel', (ch: RTCDataChannel) => {
|
||||
clearTimeout(timeout);
|
||||
resolve(ch);
|
||||
});
|
||||
|
||||
peer.on('failed', (error: Error) => {
|
||||
clearTimeout(timeout);
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
|
||||
return { peer, channel };
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
private events: Map<string, Set<Function>>;
|
||||
|
||||
constructor() {
|
||||
this.events = new Map();
|
||||
}
|
||||
export class EventEmitter<EventMap extends Record<string, (...args: any[]) => void>> {
|
||||
private events: Map<keyof EventMap, Set<Function>> = new Map();
|
||||
|
||||
/**
|
||||
* 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)) {
|
||||
this.events.set(event, new Set());
|
||||
}
|
||||
@@ -22,18 +42,18 @@ export class EventEmitter {
|
||||
/**
|
||||
* Register a one-time event listener
|
||||
*/
|
||||
once(event: string, listener: Function): this {
|
||||
const onceWrapper = (...args: any[]) => {
|
||||
this.off(event, onceWrapper);
|
||||
listener.apply(this, args);
|
||||
once<K extends keyof EventMap>(event: K, listener: EventMap[K]): this {
|
||||
const onceWrapper = (...args: Parameters<EventMap[K]>) => {
|
||||
this.off(event, onceWrapper as EventMap[K]);
|
||||
listener(...args);
|
||||
};
|
||||
return this.on(event, onceWrapper);
|
||||
return this.on(event, onceWrapper as EventMap[K]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
if (listeners) {
|
||||
listeners.delete(listener);
|
||||
@@ -47,7 +67,10 @@ export class EventEmitter {
|
||||
/**
|
||||
* 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);
|
||||
if (!listeners || listeners.size === 0) {
|
||||
return false;
|
||||
@@ -55,9 +78,9 @@ export class EventEmitter {
|
||||
|
||||
listeners.forEach(listener => {
|
||||
try {
|
||||
listener.apply(this, args);
|
||||
(listener as EventMap[K])(...args);
|
||||
} 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)
|
||||
*/
|
||||
removeAllListeners(event?: string): this {
|
||||
if (event) {
|
||||
removeAllListeners<K extends keyof EventMap>(event?: K): this {
|
||||
if (event !== undefined) {
|
||||
this.events.delete(event);
|
||||
} else {
|
||||
this.events.clear();
|
||||
@@ -79,7 +102,7 @@ export class EventEmitter {
|
||||
/**
|
||||
* Get listener count for an event
|
||||
*/
|
||||
listenerCount(event: string): number {
|
||||
listenerCount<K extends keyof EventMap>(event: K): number {
|
||||
const listeners = this.events.get(event);
|
||||
return listeners ? listeners.size : 0;
|
||||
}
|
||||
|
||||
80
src/index.ts
80
src/index.ts
@@ -1,42 +1,54 @@
|
||||
/**
|
||||
* @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 { Rondevu } from './rondevu';
|
||||
// Export main client class
|
||||
export { Rondevu } from './rondevu.js';
|
||||
export type { RondevuOptions } from './rondevu.js';
|
||||
|
||||
// Export connection class
|
||||
export { RondevuConnection } from './connection';
|
||||
// Export authentication
|
||||
export { RondevuAuth } from './auth.js';
|
||||
export type { Credentials, FetchFunction } from './auth.js';
|
||||
|
||||
// Export low-level signaling client (for advanced usage)
|
||||
export { RondevuClient } from './client';
|
||||
|
||||
// Export all types
|
||||
// Export offers API
|
||||
export { RondevuOffers } from './offers.js';
|
||||
export type {
|
||||
// WebRTC types
|
||||
RondevuOptions,
|
||||
JoinOptions,
|
||||
ConnectionRole,
|
||||
RondevuConnectionParams,
|
||||
RondevuConnectionEvents,
|
||||
// Signaling types
|
||||
Side,
|
||||
Session,
|
||||
TopicInfo,
|
||||
Pagination,
|
||||
ListTopicsResponse,
|
||||
ListSessionsResponse,
|
||||
CreateOfferRequest,
|
||||
CreateOfferResponse,
|
||||
AnswerRequest,
|
||||
AnswerResponse,
|
||||
PollRequest,
|
||||
PollOffererResponse,
|
||||
PollAnswererResponse,
|
||||
PollResponse,
|
||||
VersionResponse,
|
||||
HealthResponse,
|
||||
ErrorResponse,
|
||||
RondevuClientOptions,
|
||||
} from './types';
|
||||
Offer,
|
||||
IceCandidate,
|
||||
TopicInfo
|
||||
} from './offers.js';
|
||||
|
||||
// Export peer manager
|
||||
export { default as RondevuPeer } from './peer/index.js';
|
||||
export type {
|
||||
PeerOptions,
|
||||
PeerEvents,
|
||||
PeerTimeouts
|
||||
} from './peer/index.js';
|
||||
|
||||
// Export username API
|
||||
export { RondevuUsername } from './usernames.js';
|
||||
export type { UsernameClaimResult, UsernameCheckResult } from './usernames.js';
|
||||
|
||||
// Export services API
|
||||
export { RondevuServices } from './services.js';
|
||||
export type {
|
||||
ServicePublishResult,
|
||||
PublishServiceOptions,
|
||||
ServiceHandle
|
||||
} from './services.js';
|
||||
|
||||
// Export discovery API
|
||||
export { RondevuDiscovery } from './discovery.js';
|
||||
export type {
|
||||
ServiceInfo,
|
||||
ServiceListResult,
|
||||
ServiceQueryResult,
|
||||
ServiceDetails,
|
||||
ConnectResult
|
||||
} from './discovery.js';
|
||||
|
||||
// Export pool types
|
||||
export type { PoolStatus, PooledServiceHandle } from './service-pool.js';
|
||||
|
||||
174
src/offer-pool.ts
Normal file
174
src/offer-pool.ts
Normal file
@@ -0,0 +1,174 @@
|
||||
import { RondevuOffers, Offer } from './offers.js';
|
||||
|
||||
/**
|
||||
* Represents an offer that has been answered
|
||||
*/
|
||||
export interface AnsweredOffer {
|
||||
offerId: string;
|
||||
answererId: string;
|
||||
sdp: string;
|
||||
answeredAt: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration options for the offer pool
|
||||
*/
|
||||
export interface OfferPoolOptions {
|
||||
/** Number of simultaneous open offers to maintain */
|
||||
poolSize: number;
|
||||
|
||||
/** Polling interval in milliseconds (default: 2000ms) */
|
||||
pollingInterval?: number;
|
||||
|
||||
/** Callback invoked when an offer is answered */
|
||||
onAnswered: (answer: AnsweredOffer) => Promise<void>;
|
||||
|
||||
/** Callback to create new offers when refilling the pool */
|
||||
onRefill: (count: number) => Promise<Offer[]>;
|
||||
|
||||
/** Error handler for pool operations */
|
||||
onError: (error: Error, context: string) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Manages a pool of offers with automatic polling and refill
|
||||
*
|
||||
* The OfferPool maintains a configurable number of simultaneous offers,
|
||||
* polls for answers periodically, and automatically refills the pool
|
||||
* when offers are consumed.
|
||||
*/
|
||||
export class OfferPool {
|
||||
private offers: Map<string, Offer> = new Map();
|
||||
private polling: boolean = false;
|
||||
private pollingTimer?: ReturnType<typeof setInterval>;
|
||||
private lastPollTime: number = 0;
|
||||
private readonly pollingInterval: number;
|
||||
|
||||
constructor(
|
||||
private offersApi: RondevuOffers,
|
||||
private options: OfferPoolOptions
|
||||
) {
|
||||
this.pollingInterval = options.pollingInterval || 2000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add offers to the pool
|
||||
*/
|
||||
async addOffers(offers: Offer[]): Promise<void> {
|
||||
for (const offer of offers) {
|
||||
this.offers.set(offer.id, offer);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Start polling for answers
|
||||
*/
|
||||
async start(): Promise<void> {
|
||||
if (this.polling) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.polling = true;
|
||||
|
||||
// Do an immediate poll
|
||||
await this.poll().catch((error) => {
|
||||
this.options.onError(error, 'initial-poll');
|
||||
});
|
||||
|
||||
// Start polling interval
|
||||
this.pollingTimer = setInterval(async () => {
|
||||
if (this.polling) {
|
||||
await this.poll().catch((error) => {
|
||||
this.options.onError(error, 'poll');
|
||||
});
|
||||
}
|
||||
}, this.pollingInterval);
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop polling for answers
|
||||
*/
|
||||
async stop(): Promise<void> {
|
||||
this.polling = false;
|
||||
|
||||
if (this.pollingTimer) {
|
||||
clearInterval(this.pollingTimer);
|
||||
this.pollingTimer = undefined;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Poll for answers and refill the pool if needed
|
||||
*/
|
||||
private async poll(): Promise<void> {
|
||||
try {
|
||||
// Get all answers from server
|
||||
const answers = await this.offersApi.getAnswers();
|
||||
|
||||
// Filter for our pool's offers
|
||||
const myAnswers = answers.filter(a => this.offers.has(a.offerId));
|
||||
|
||||
// Process each answer
|
||||
for (const answer of myAnswers) {
|
||||
// Notify ServicePool
|
||||
await this.options.onAnswered({
|
||||
offerId: answer.offerId,
|
||||
answererId: answer.answererId,
|
||||
sdp: answer.sdp,
|
||||
answeredAt: answer.answeredAt
|
||||
});
|
||||
|
||||
// Remove consumed offer from pool
|
||||
this.offers.delete(answer.offerId);
|
||||
}
|
||||
|
||||
// Immediate refill if below pool size
|
||||
if (this.offers.size < this.options.poolSize) {
|
||||
const needed = this.options.poolSize - this.offers.size;
|
||||
|
||||
try {
|
||||
const newOffers = await this.options.onRefill(needed);
|
||||
await this.addOffers(newOffers);
|
||||
} catch (refillError) {
|
||||
this.options.onError(
|
||||
refillError as Error,
|
||||
'refill'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
this.lastPollTime = Date.now();
|
||||
} catch (error) {
|
||||
// Don't crash the pool on errors - let error handler deal with it
|
||||
this.options.onError(error as Error, 'poll');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current number of active offers in the pool
|
||||
*/
|
||||
getActiveOfferCount(): number {
|
||||
return this.offers.size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all active offer IDs
|
||||
*/
|
||||
getActiveOfferIds(): string[] {
|
||||
return Array.from(this.offers.keys());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the last poll timestamp
|
||||
*/
|
||||
getLastPollTime(): number {
|
||||
return this.lastPollTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the pool is currently polling
|
||||
*/
|
||||
isPolling(): boolean {
|
||||
return this.polling;
|
||||
}
|
||||
}
|
||||
321
src/offers.ts
Normal file
321
src/offers.ts
Normal file
@@ -0,0 +1,321 @@
|
||||
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;
|
||||
secret?: string;
|
||||
info?: string;
|
||||
}
|
||||
|
||||
export interface Offer {
|
||||
id: string;
|
||||
peerId: string;
|
||||
sdp: string;
|
||||
topics: string[];
|
||||
createdAt?: number;
|
||||
expiresAt: number;
|
||||
lastSeen: number;
|
||||
secret?: string;
|
||||
hasSecret?: boolean;
|
||||
info?: string;
|
||||
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;
|
||||
startsWith?: string;
|
||||
}): Promise<{
|
||||
topics: TopicInfo[];
|
||||
total: number;
|
||||
limit: number;
|
||||
offset: number;
|
||||
startsWith?: string;
|
||||
}> {
|
||||
const params = new URLSearchParams();
|
||||
|
||||
if (options?.limit) {
|
||||
params.set('limit', options.limit.toString());
|
||||
}
|
||||
|
||||
if (options?.offset) {
|
||||
params.set('offset', options.offset.toString());
|
||||
}
|
||||
|
||||
if (options?.startsWith) {
|
||||
params.set('startsWith', options.startsWith);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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, secret?: 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, secret }),
|
||||
});
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
49
src/peer/answering-state.ts
Normal file
49
src/peer/answering-state.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
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();
|
||||
|
||||
// Send answer to server BEFORE setLocalDescription
|
||||
// This registers us as the answerer so ICE candidates will be accepted
|
||||
await this.peer.offersApi.answer(offerId, answer.sdp!, options.secret);
|
||||
|
||||
// Enable trickle ICE - set up handler before ICE gathering starts
|
||||
this.setupIceCandidateHandler();
|
||||
|
||||
// Set local description - ICE gathering starts here
|
||||
// Server already knows we're the answerer, so candidates will be accepted
|
||||
await this.peer.pc.setLocalDescription(answer);
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
57
src/peer/creating-offer-state.ts
Normal file
57
src/peer/creating-offer-state.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
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);
|
||||
}
|
||||
|
||||
// Enable trickle ICE - set up handler before ICE gathering starts
|
||||
// Handler will check this.peer.offerId before sending
|
||||
this.setupIceCandidateHandler();
|
||||
|
||||
// Create WebRTC offer
|
||||
const offer = await this.peer.pc.createOffer();
|
||||
await this.peer.pc.setLocalDescription(offer); // ICE gathering starts here
|
||||
|
||||
// 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,
|
||||
secret: options.secret
|
||||
}]);
|
||||
|
||||
const offerId = offers[0].id;
|
||||
this.peer.offerId = offerId; // Now handler can send candidates
|
||||
|
||||
// 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 this.peer.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);
|
||||
}
|
||||
}
|
||||
212
src/peer/index.ts
Normal file
212
src/peer/index.ts
Normal file
@@ -0,0 +1,212 @@
|
||||
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';
|
||||
|
||||
// WebRTC polyfills for Node.js compatibility
|
||||
RTCPeerConnection: typeof RTCPeerConnection;
|
||||
RTCSessionDescription: typeof RTCSessionDescription;
|
||||
RTCIceCandidate: typeof RTCIceCandidate;
|
||||
|
||||
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' }
|
||||
]
|
||||
},
|
||||
rtcPeerConnection?: typeof RTCPeerConnection,
|
||||
rtcSessionDescription?: typeof RTCSessionDescription,
|
||||
rtcIceCandidate?: typeof RTCIceCandidate
|
||||
) {
|
||||
super();
|
||||
this.offersApi = offersApi;
|
||||
|
||||
// Use provided polyfills or fall back to globals
|
||||
this.RTCPeerConnection = rtcPeerConnection || (typeof globalThis.RTCPeerConnection !== 'undefined'
|
||||
? globalThis.RTCPeerConnection
|
||||
: (() => {
|
||||
throw new Error('RTCPeerConnection is not available. Please provide it in the Rondevu constructor options for Node.js environments.');
|
||||
}) as any);
|
||||
|
||||
this.RTCSessionDescription = rtcSessionDescription || (typeof globalThis.RTCSessionDescription !== 'undefined'
|
||||
? globalThis.RTCSessionDescription
|
||||
: (() => {
|
||||
throw new Error('RTCSessionDescription is not available. Please provide it in the Rondevu constructor options for Node.js environments.');
|
||||
}) as any);
|
||||
|
||||
this.RTCIceCandidate = rtcIceCandidate || (typeof globalThis.RTCIceCandidate !== 'undefined'
|
||||
? globalThis.RTCIceCandidate
|
||||
: (() => {
|
||||
throw new Error('RTCIceCandidate is not available. Please provide it in the Rondevu constructor options for Node.js environments.');
|
||||
}) as any);
|
||||
|
||||
this.pc = new this.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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a data channel for sending and receiving arbitrary data
|
||||
* This should typically be called by the offerer before creating the offer
|
||||
* The answerer will receive the channel via the 'datachannel' event
|
||||
*/
|
||||
createDataChannel(label: string, options?: RTCDataChannelInit): RTCDataChannel {
|
||||
return this.pc.createDataChannel(label, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 this.peer.RTCIceCandidate(candidate));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Setup trickle ICE candidate handler
|
||||
* Sends local ICE candidates to server as they are discovered
|
||||
*/
|
||||
protected setupIceCandidateHandler(): void {
|
||||
this.iceCandidateHandler = async (event: RTCPeerConnectionIceEvent) => {
|
||||
if (event.candidate && this.peer.offerId) {
|
||||
const candidateData = event.candidate.toJSON();
|
||||
if (candidateData.candidate && candidateData.candidate !== '') {
|
||||
try {
|
||||
await this.peer.offersApi.addIceCandidates(this.peer.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));
|
||||
}
|
||||
}
|
||||
45
src/peer/types.ts
Normal file
45
src/peer/types.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* 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;
|
||||
/** Optional secret to protect the offer (max 128 characters) */
|
||||
secret?: string;
|
||||
/** 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);
|
||||
}
|
||||
}
|
||||
416
src/rondevu.ts
416
src/rondevu.ts
@@ -1,273 +1,185 @@
|
||||
import { RondevuClient } from './client';
|
||||
import { RondevuConnection } from './connection';
|
||||
import { RondevuOptions, JoinOptions, RondevuConnectionParams } from './types';
|
||||
import { RondevuAuth, Credentials, FetchFunction } from './auth.js';
|
||||
import { RondevuOffers } from './offers.js';
|
||||
import { RondevuUsername } from './usernames.js';
|
||||
import { RondevuServices } from './services.js';
|
||||
import { RondevuDiscovery } from './discovery.js';
|
||||
import RondevuPeer from './peer/index.js';
|
||||
|
||||
export interface RondevuOptions {
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* Custom RTCPeerConnection implementation for Node.js environments
|
||||
* Required when using in Node.js with wrtc or similar polyfills
|
||||
*
|
||||
* @example Node.js with wrtc
|
||||
* ```typescript
|
||||
* import { RTCPeerConnection } from 'wrtc';
|
||||
* const client = new Rondevu({ RTCPeerConnection });
|
||||
* ```
|
||||
*/
|
||||
RTCPeerConnection?: typeof RTCPeerConnection;
|
||||
|
||||
/**
|
||||
* Custom RTCSessionDescription implementation for Node.js environments
|
||||
* Required when using in Node.js with wrtc or similar polyfills
|
||||
*
|
||||
* @example Node.js with wrtc
|
||||
* ```typescript
|
||||
* import { RTCSessionDescription } from 'wrtc';
|
||||
* const client = new Rondevu({ RTCSessionDescription });
|
||||
* ```
|
||||
*/
|
||||
RTCSessionDescription?: typeof RTCSessionDescription;
|
||||
|
||||
/**
|
||||
* Custom RTCIceCandidate implementation for Node.js environments
|
||||
* Required when using in Node.js with wrtc or similar polyfills
|
||||
*
|
||||
* @example Node.js with wrtc
|
||||
* ```typescript
|
||||
* import { RTCIceCandidate } from 'wrtc';
|
||||
* const client = new Rondevu({ RTCIceCandidate });
|
||||
* ```
|
||||
*/
|
||||
RTCIceCandidate?: typeof RTCIceCandidate;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main Rondevu WebRTC client with automatic connection management
|
||||
*/
|
||||
export class Rondevu {
|
||||
readonly peerId: string;
|
||||
readonly auth: RondevuAuth;
|
||||
readonly usernames: RondevuUsername;
|
||||
|
||||
private client: RondevuClient;
|
||||
private _offers?: RondevuOffers;
|
||||
private _services?: RondevuServices;
|
||||
private _discovery?: RondevuDiscovery;
|
||||
private credentials?: Credentials;
|
||||
private baseUrl: string;
|
||||
private fetchImpl?: typeof fetch;
|
||||
private rtcConfig?: RTCConfiguration;
|
||||
private pollingInterval: number;
|
||||
private connectionTimeout: number;
|
||||
private fetchFn?: FetchFunction;
|
||||
private rtcPeerConnection?: typeof RTCPeerConnection;
|
||||
private rtcSessionDescription?: typeof RTCSessionDescription;
|
||||
private rtcIceCandidate?: typeof RTCIceCandidate;
|
||||
|
||||
/**
|
||||
* Creates a new Rondevu client instance
|
||||
* @param options - Client configuration options
|
||||
*/
|
||||
constructor(options: RondevuOptions = {}) {
|
||||
this.baseUrl = options.baseUrl || 'https://rondevu.xtrdev.workers.dev';
|
||||
this.fetchImpl = options.fetch;
|
||||
this.baseUrl = options.baseUrl || 'https://api.ronde.vu';
|
||||
this.fetchFn = options.fetch;
|
||||
this.rtcPeerConnection = options.RTCPeerConnection;
|
||||
this.rtcSessionDescription = options.RTCSessionDescription;
|
||||
this.rtcIceCandidate = options.RTCIceCandidate;
|
||||
|
||||
this.client = new RondevuClient({
|
||||
baseUrl: this.baseUrl,
|
||||
fetch: options.fetch,
|
||||
});
|
||||
this.auth = new RondevuAuth(this.baseUrl, this.fetchFn);
|
||||
this.usernames = new RondevuUsername(this.baseUrl);
|
||||
|
||||
// Auto-generate peer ID if not provided
|
||||
this.peerId = options.peerId || this.generatePeerId();
|
||||
this.rtcConfig = options.rtcConfig;
|
||||
this.pollingInterval = options.pollingInterval || 1000;
|
||||
this.connectionTimeout = options.connectionTimeout || 30000;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique peer ID
|
||||
*/
|
||||
private generatePeerId(): string {
|
||||
return `rdv_${Math.random().toString(36).substring(2, 14)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the peer ID (useful when user identity changes)
|
||||
*/
|
||||
updatePeerId(newPeerId: string): void {
|
||||
(this as any).peerId = newPeerId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new connection (offerer role)
|
||||
* @param id - Connection identifier
|
||||
* @param topic - Topic name for grouping connections
|
||||
* @returns Promise that resolves to RondevuConnection
|
||||
*/
|
||||
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`);
|
||||
if (options.credentials) {
|
||||
this.credentials = options.credentials;
|
||||
this._offers = new RondevuOffers(this.baseUrl, this.credentials, this.fetchFn);
|
||||
this._services = new RondevuServices(this.baseUrl, this.credentials);
|
||||
this._discovery = new RondevuDiscovery(this.baseUrl, this.credentials);
|
||||
}
|
||||
|
||||
// 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
|
||||
* Get offers API (low-level access, requires authentication)
|
||||
* For most use cases, use services and discovery APIs instead
|
||||
*/
|
||||
async join(topic: string, options?: JoinOptions): Promise<RondevuConnection> {
|
||||
// List sessions in topic
|
||||
const { sessions } = await this.client.listSessions(topic);
|
||||
get offers(): RondevuOffers {
|
||||
if (!this._offers) {
|
||||
throw new Error('Not authenticated. Call register() first or provide credentials.');
|
||||
}
|
||||
return this._offers;
|
||||
}
|
||||
|
||||
// Filter out self (sessions with our peer ID)
|
||||
let availableSessions = sessions.filter(
|
||||
session => session.peerId !== this.peerId
|
||||
/**
|
||||
* Get services API (requires authentication)
|
||||
*/
|
||||
get services(): RondevuServices {
|
||||
if (!this._services) {
|
||||
throw new Error('Not authenticated. Call register() first or provide credentials.');
|
||||
}
|
||||
return this._services;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get discovery API (requires authentication)
|
||||
*/
|
||||
get discovery(): RondevuDiscovery {
|
||||
if (!this._discovery) {
|
||||
throw new Error('Not authenticated. Call register() first or provide credentials.');
|
||||
}
|
||||
return this._discovery;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register and initialize authenticated client
|
||||
* Generates a cryptographically random peer ID (128-bit)
|
||||
*/
|
||||
async register(): Promise<Credentials> {
|
||||
this.credentials = await this.auth.register();
|
||||
|
||||
// Create API instances
|
||||
this._offers = new RondevuOffers(
|
||||
this.baseUrl,
|
||||
this.credentials,
|
||||
this.fetchFn
|
||||
);
|
||||
this._services = new RondevuServices(this.baseUrl, this.credentials);
|
||||
this._discovery = new RondevuDiscovery(this.baseUrl, this.credentials);
|
||||
|
||||
// Apply custom filter if provided
|
||||
if (options?.filter) {
|
||||
availableSessions = availableSessions.filter(options.filter);
|
||||
return this.credentials;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if client is authenticated
|
||||
*/
|
||||
isAuthenticated(): boolean {
|
||||
return !!this.credentials;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current credentials
|
||||
*/
|
||||
getCredentials(): Credentials | undefined {
|
||||
return this.credentials;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new WebRTC peer connection (requires authentication)
|
||||
* 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
|
||||
*/
|
||||
createPeer(rtcConfig?: RTCConfiguration): RondevuPeer {
|
||||
if (!this._offers) {
|
||||
throw new Error('Not authenticated. Call register() first or provide credentials.');
|
||||
}
|
||||
|
||||
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'
|
||||
return new RondevuPeer(
|
||||
this._offers,
|
||||
rtcConfig,
|
||||
this.rtcPeerConnection,
|
||||
this.rtcSessionDescription,
|
||||
this.rtcIceCandidate
|
||||
);
|
||||
|
||||
// Connect to selected session
|
||||
return this.connect(selectedSession.code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Select a session based on strategy
|
||||
*/
|
||||
private selectSession(
|
||||
sessions: Array<{ code: string; peerId: string; createdAt: number }>,
|
||||
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
|
||||
*/
|
||||
private async waitForIceGathering(pc: RTCPeerConnection): Promise<void> {
|
||||
if (pc.iceGatheringState === 'complete') {
|
||||
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
|
||||
* This requires polling since we don't know which topic it's in
|
||||
*/
|
||||
private async findSessionByIdWithClient(
|
||||
id: string,
|
||||
client: RondevuClient
|
||||
): 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;
|
||||
} catch (err) {
|
||||
throw new Error(`Failed to find session ${id}: ${(err as Error).message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
490
src/service-pool.ts
Normal file
490
src/service-pool.ts
Normal file
@@ -0,0 +1,490 @@
|
||||
import { RondevuOffers, Offer } from './offers.js';
|
||||
import { RondevuUsername } from './usernames.js';
|
||||
import RondevuPeer from './peer/index.js';
|
||||
import { OfferPool, AnsweredOffer } from './offer-pool.js';
|
||||
import { ServiceHandle } from './services.js';
|
||||
|
||||
/**
|
||||
* Connection information for tracking active connections
|
||||
*/
|
||||
interface ConnectionInfo {
|
||||
peer: RondevuPeer;
|
||||
channel: RTCDataChannel;
|
||||
connectedAt: number;
|
||||
offerId: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Status information about the pool
|
||||
*/
|
||||
export interface PoolStatus {
|
||||
/** Number of active offers in the pool */
|
||||
activeOffers: number;
|
||||
|
||||
/** Number of currently connected peers */
|
||||
activeConnections: number;
|
||||
|
||||
/** Total number of connections handled since start */
|
||||
totalConnectionsHandled: number;
|
||||
|
||||
/** Number of failed offer creation attempts */
|
||||
failedOfferCreations: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configuration options for a pooled service
|
||||
*/
|
||||
export interface ServicePoolOptions {
|
||||
/** Username that owns the service */
|
||||
username: string;
|
||||
|
||||
/** Private key for signing service operations */
|
||||
privateKey: string;
|
||||
|
||||
/** Fully qualified service name (e.g., com.example.chat@1.0.0) */
|
||||
serviceFqn: string;
|
||||
|
||||
/** WebRTC configuration */
|
||||
rtcConfig?: RTCConfiguration;
|
||||
|
||||
/** Whether the service is publicly discoverable */
|
||||
isPublic?: boolean;
|
||||
|
||||
/** Optional metadata for the service */
|
||||
metadata?: Record<string, any>;
|
||||
|
||||
/** Time-to-live for offers in milliseconds */
|
||||
ttl?: number;
|
||||
|
||||
/** Handler invoked for each new connection */
|
||||
handler: (channel: RTCDataChannel, peer: RondevuPeer, connectionId: string) => void;
|
||||
|
||||
/** Number of simultaneous open offers to maintain (default: 1) */
|
||||
poolSize?: number;
|
||||
|
||||
/** Polling interval in milliseconds (default: 2000ms) */
|
||||
pollingInterval?: number;
|
||||
|
||||
/** Callback for pool status updates */
|
||||
onPoolStatus?: (status: PoolStatus) => void;
|
||||
|
||||
/** Error handler for pool operations */
|
||||
onError?: (error: Error, context: string) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extended service handle with pool-specific methods
|
||||
*/
|
||||
export interface PooledServiceHandle extends ServiceHandle {
|
||||
/** Get current pool status */
|
||||
getStatus: () => PoolStatus;
|
||||
|
||||
/** Manually add offers to the pool */
|
||||
addOffers: (count: number) => Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Manages a pooled service with multiple concurrent connections
|
||||
*
|
||||
* ServicePool coordinates offer creation, answer polling, and connection
|
||||
* management for services that need to handle multiple simultaneous connections.
|
||||
*/
|
||||
export class ServicePool {
|
||||
private offerPool?: OfferPool;
|
||||
private connections: Map<string, ConnectionInfo> = new Map();
|
||||
private status: PoolStatus = {
|
||||
activeOffers: 0,
|
||||
activeConnections: 0,
|
||||
totalConnectionsHandled: 0,
|
||||
failedOfferCreations: 0
|
||||
};
|
||||
private serviceId?: string;
|
||||
private uuid?: string;
|
||||
private offersApi: RondevuOffers;
|
||||
private usernameApi: RondevuUsername;
|
||||
|
||||
constructor(
|
||||
private baseUrl: string,
|
||||
private credentials: { peerId: string; secret: string },
|
||||
private options: ServicePoolOptions
|
||||
) {
|
||||
this.offersApi = new RondevuOffers(baseUrl, credentials);
|
||||
this.usernameApi = new RondevuUsername(baseUrl);
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the pooled service
|
||||
*/
|
||||
async start(): Promise<PooledServiceHandle> {
|
||||
const poolSize = this.options.poolSize || 1;
|
||||
|
||||
// 1. Create initial service (publishes first offer)
|
||||
const service = await this.publishInitialService();
|
||||
this.serviceId = service.serviceId;
|
||||
this.uuid = service.uuid;
|
||||
|
||||
// 2. Create additional offers for pool (poolSize - 1)
|
||||
const additionalOffers: Offer[] = [];
|
||||
if (poolSize > 1) {
|
||||
try {
|
||||
const offers = await this.createOffers(poolSize - 1);
|
||||
additionalOffers.push(...offers);
|
||||
} catch (error) {
|
||||
this.handleError(error as Error, 'initial-offer-creation');
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Initialize OfferPool with all offers
|
||||
this.offerPool = new OfferPool(this.offersApi, {
|
||||
poolSize,
|
||||
pollingInterval: this.options.pollingInterval || 2000,
|
||||
onAnswered: (answer) => this.handleConnection(answer),
|
||||
onRefill: (count) => this.createOffers(count),
|
||||
onError: (err, ctx) => this.handleError(err, ctx)
|
||||
});
|
||||
|
||||
// Add all offers to pool
|
||||
const allOffers = [
|
||||
{ id: service.offerId, peerId: this.credentials.peerId, sdp: '', topics: [], expiresAt: service.expiresAt, lastSeen: Date.now() },
|
||||
...additionalOffers
|
||||
];
|
||||
await this.offerPool.addOffers(allOffers);
|
||||
|
||||
// 4. Start polling
|
||||
await this.offerPool.start();
|
||||
|
||||
// Update status
|
||||
this.updateStatus();
|
||||
|
||||
// 5. Return handle
|
||||
return {
|
||||
serviceId: this.serviceId,
|
||||
uuid: this.uuid,
|
||||
offerId: service.offerId,
|
||||
unpublish: () => this.stop(),
|
||||
getStatus: () => this.getStatus(),
|
||||
addOffers: (count) => this.manualRefill(count)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the pooled service and clean up
|
||||
*/
|
||||
async stop(): Promise<void> {
|
||||
// 1. Stop accepting new connections
|
||||
if (this.offerPool) {
|
||||
await this.offerPool.stop();
|
||||
}
|
||||
|
||||
// 2. Delete remaining offers
|
||||
if (this.offerPool) {
|
||||
const offerIds = this.offerPool.getActiveOfferIds();
|
||||
await Promise.allSettled(
|
||||
offerIds.map(id => this.offersApi.delete(id).catch(() => {}))
|
||||
);
|
||||
}
|
||||
|
||||
// 3. Close active connections
|
||||
const closePromises = Array.from(this.connections.values()).map(
|
||||
async (conn) => {
|
||||
try {
|
||||
// Give a brief moment for graceful closure
|
||||
await new Promise(resolve => setTimeout(resolve, 100));
|
||||
conn.peer.pc.close();
|
||||
} catch {
|
||||
// Ignore errors during cleanup
|
||||
}
|
||||
}
|
||||
);
|
||||
await Promise.allSettled(closePromises);
|
||||
|
||||
// 4. Delete service if we have a serviceId
|
||||
if (this.serviceId) {
|
||||
try {
|
||||
const response = await fetch(`${this.baseUrl}/services/${this.serviceId}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${this.credentials.peerId}:${this.credentials.secret}`
|
||||
},
|
||||
body: JSON.stringify({ username: this.options.username })
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
console.error('Failed to delete service:', await response.text());
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error deleting service:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Clear all state
|
||||
this.connections.clear();
|
||||
this.offerPool = undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an answered offer by setting up the connection
|
||||
*/
|
||||
private async handleConnection(answer: AnsweredOffer): Promise<void> {
|
||||
const connectionId = this.generateConnectionId();
|
||||
|
||||
try {
|
||||
// Create peer connection
|
||||
const peer = new RondevuPeer(
|
||||
this.offersApi,
|
||||
this.options.rtcConfig || {
|
||||
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
|
||||
}
|
||||
);
|
||||
|
||||
peer.role = 'offerer';
|
||||
peer.offerId = answer.offerId;
|
||||
|
||||
// Set remote description (the answer)
|
||||
await peer.pc.setRemoteDescription({
|
||||
type: 'answer',
|
||||
sdp: answer.sdp
|
||||
});
|
||||
|
||||
// Wait for data channel (answerer creates it, we receive it)
|
||||
const channel = await new Promise<RTCDataChannel>((resolve, reject) => {
|
||||
const timeout = setTimeout(
|
||||
() => reject(new Error('Timeout waiting for data channel')),
|
||||
30000
|
||||
);
|
||||
|
||||
peer.on('datachannel', (ch: RTCDataChannel) => {
|
||||
clearTimeout(timeout);
|
||||
resolve(ch);
|
||||
});
|
||||
|
||||
// Also check if channel already exists
|
||||
if (peer.pc.ondatachannel) {
|
||||
const existingHandler = peer.pc.ondatachannel;
|
||||
peer.pc.ondatachannel = (event) => {
|
||||
clearTimeout(timeout);
|
||||
resolve(event.channel);
|
||||
if (existingHandler) existingHandler.call(peer.pc, event);
|
||||
};
|
||||
} else {
|
||||
peer.pc.ondatachannel = (event) => {
|
||||
clearTimeout(timeout);
|
||||
resolve(event.channel);
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
// Register connection
|
||||
this.connections.set(connectionId, {
|
||||
peer,
|
||||
channel,
|
||||
connectedAt: Date.now(),
|
||||
offerId: answer.offerId
|
||||
});
|
||||
|
||||
this.status.activeConnections++;
|
||||
this.status.totalConnectionsHandled++;
|
||||
|
||||
// Setup cleanup on disconnect
|
||||
peer.on('disconnected', () => {
|
||||
this.connections.delete(connectionId);
|
||||
this.status.activeConnections--;
|
||||
this.updateStatus();
|
||||
});
|
||||
|
||||
peer.on('failed', () => {
|
||||
this.connections.delete(connectionId);
|
||||
this.status.activeConnections--;
|
||||
this.updateStatus();
|
||||
});
|
||||
|
||||
// Update status
|
||||
this.updateStatus();
|
||||
|
||||
// Invoke user handler (wrapped in try-catch)
|
||||
try {
|
||||
this.options.handler(channel, peer, connectionId);
|
||||
} catch (handlerError) {
|
||||
this.handleError(handlerError as Error, 'handler');
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
this.handleError(error as Error, 'connection-setup');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create multiple offers
|
||||
*/
|
||||
private async createOffers(count: number): Promise<Offer[]> {
|
||||
if (count <= 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Server supports max 10 offers per request
|
||||
const batchSize = Math.min(count, 10);
|
||||
const offers: Offer[] = [];
|
||||
|
||||
try {
|
||||
// Create peer connections and generate offers
|
||||
const offerRequests = [];
|
||||
for (let i = 0; i < batchSize; i++) {
|
||||
const pc = new RTCPeerConnection(this.options.rtcConfig || {
|
||||
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
|
||||
});
|
||||
|
||||
// Create data channel (required for offers)
|
||||
pc.createDataChannel('rondevu-service');
|
||||
|
||||
// Create offer
|
||||
const offer = await pc.createOffer();
|
||||
await pc.setLocalDescription(offer);
|
||||
|
||||
if (!offer.sdp) {
|
||||
pc.close();
|
||||
throw new Error('Failed to generate SDP');
|
||||
}
|
||||
|
||||
offerRequests.push({
|
||||
sdp: offer.sdp,
|
||||
topics: [], // V2 doesn't use topics
|
||||
ttl: this.options.ttl
|
||||
});
|
||||
|
||||
// Close the PC immediately - we only needed the SDP
|
||||
pc.close();
|
||||
}
|
||||
|
||||
// Batch create offers
|
||||
const createdOffers = await this.offersApi.create(offerRequests);
|
||||
offers.push(...createdOffers);
|
||||
|
||||
} catch (error) {
|
||||
this.status.failedOfferCreations++;
|
||||
this.handleError(error as Error, 'offer-creation');
|
||||
throw error;
|
||||
}
|
||||
|
||||
return offers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Publish the initial service (creates first offer)
|
||||
*/
|
||||
private async publishInitialService(): Promise<{
|
||||
serviceId: string;
|
||||
uuid: string;
|
||||
offerId: string;
|
||||
expiresAt: number;
|
||||
}> {
|
||||
const { username, privateKey, serviceFqn, rtcConfig, isPublic, metadata, ttl } = this.options;
|
||||
|
||||
// Create peer connection for initial offer
|
||||
const pc = new RTCPeerConnection(rtcConfig || {
|
||||
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
|
||||
});
|
||||
|
||||
pc.createDataChannel('rondevu-service');
|
||||
|
||||
// Create offer
|
||||
const offer = await pc.createOffer();
|
||||
await pc.setLocalDescription(offer);
|
||||
|
||||
if (!offer.sdp) {
|
||||
pc.close();
|
||||
throw new Error('Failed to generate SDP');
|
||||
}
|
||||
|
||||
// Create signature
|
||||
const timestamp = Date.now();
|
||||
const message = `publish:${username}:${serviceFqn}:${timestamp}`;
|
||||
const signature = await this.usernameApi.signMessage(message, privateKey);
|
||||
|
||||
// Publish service
|
||||
const response = await fetch(`${this.baseUrl}/services`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${this.credentials.peerId}:${this.credentials.secret}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
username,
|
||||
serviceFqn,
|
||||
sdp: offer.sdp,
|
||||
ttl,
|
||||
isPublic,
|
||||
metadata,
|
||||
signature,
|
||||
message
|
||||
})
|
||||
});
|
||||
|
||||
pc.close();
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
throw new Error(error.error || 'Failed to publish service');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
return {
|
||||
serviceId: data.serviceId,
|
||||
uuid: data.uuid,
|
||||
offerId: data.offerId,
|
||||
expiresAt: data.expiresAt
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Manually add offers to the pool
|
||||
*/
|
||||
private async manualRefill(count: number): Promise<void> {
|
||||
if (!this.offerPool) {
|
||||
throw new Error('Pool not started');
|
||||
}
|
||||
|
||||
const offers = await this.createOffers(count);
|
||||
await this.offerPool.addOffers(offers);
|
||||
this.updateStatus();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current pool status
|
||||
*/
|
||||
private getStatus(): PoolStatus {
|
||||
return { ...this.status };
|
||||
}
|
||||
|
||||
/**
|
||||
* Update status and notify listeners
|
||||
*/
|
||||
private updateStatus(): void {
|
||||
if (this.offerPool) {
|
||||
this.status.activeOffers = this.offerPool.getActiveOfferCount();
|
||||
}
|
||||
|
||||
if (this.options.onPoolStatus) {
|
||||
this.options.onPoolStatus(this.getStatus());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle errors
|
||||
*/
|
||||
private handleError(error: Error, context: string): void {
|
||||
if (this.options.onError) {
|
||||
this.options.onError(error, context);
|
||||
} else {
|
||||
console.error(`ServicePool error (${context}):`, error);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a unique connection ID
|
||||
*/
|
||||
private generateConnectionId(): string {
|
||||
return `conn-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
}
|
||||
}
|
||||
308
src/services.ts
Normal file
308
src/services.ts
Normal file
@@ -0,0 +1,308 @@
|
||||
import { RondevuUsername } from './usernames.js';
|
||||
import RondevuPeer from './peer/index.js';
|
||||
import { RondevuOffers } from './offers.js';
|
||||
import { ServicePool, ServicePoolOptions, PooledServiceHandle, PoolStatus } from './service-pool.js';
|
||||
|
||||
/**
|
||||
* Service publish result
|
||||
*/
|
||||
export interface ServicePublishResult {
|
||||
serviceId: string;
|
||||
uuid: string;
|
||||
offerId: string;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Service publish options
|
||||
*/
|
||||
export interface PublishServiceOptions {
|
||||
username: string;
|
||||
privateKey: string;
|
||||
serviceFqn: string;
|
||||
rtcConfig?: RTCConfiguration;
|
||||
isPublic?: boolean;
|
||||
metadata?: Record<string, any>;
|
||||
ttl?: number;
|
||||
onConnection?: (peer: RondevuPeer) => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Service handle for managing an exposed service
|
||||
*/
|
||||
export interface ServiceHandle {
|
||||
serviceId: string;
|
||||
uuid: string;
|
||||
offerId: string;
|
||||
unpublish: () => Promise<void>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rondevu Services API
|
||||
* Handles service publishing and management
|
||||
*/
|
||||
export class RondevuServices {
|
||||
private usernameApi: RondevuUsername;
|
||||
private offersApi: RondevuOffers;
|
||||
|
||||
constructor(
|
||||
private baseUrl: string,
|
||||
private credentials: { peerId: string; secret: string }
|
||||
) {
|
||||
this.usernameApi = new RondevuUsername(baseUrl);
|
||||
this.offersApi = new RondevuOffers(baseUrl, credentials);
|
||||
}
|
||||
|
||||
/**
|
||||
* Publishes a service
|
||||
*/
|
||||
async publishService(options: PublishServiceOptions): Promise<ServicePublishResult> {
|
||||
const {
|
||||
username,
|
||||
privateKey,
|
||||
serviceFqn,
|
||||
rtcConfig,
|
||||
isPublic = false,
|
||||
metadata,
|
||||
ttl
|
||||
} = options;
|
||||
|
||||
// Validate FQN format
|
||||
this.validateServiceFqn(serviceFqn);
|
||||
|
||||
// Create WebRTC peer connection to generate offer
|
||||
const pc = new RTCPeerConnection(rtcConfig || {
|
||||
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
|
||||
});
|
||||
|
||||
// Add a data channel (required for datachannel-based services)
|
||||
pc.createDataChannel('rondevu-service');
|
||||
|
||||
// Create offer
|
||||
const offer = await pc.createOffer();
|
||||
await pc.setLocalDescription(offer);
|
||||
|
||||
if (!offer.sdp) {
|
||||
throw new Error('Failed to generate SDP');
|
||||
}
|
||||
|
||||
// Create signature for username verification
|
||||
const timestamp = Date.now();
|
||||
const message = `publish:${username}:${serviceFqn}:${timestamp}`;
|
||||
const signature = await this.usernameApi.signMessage(message, privateKey);
|
||||
|
||||
// Publish service
|
||||
const response = await fetch(`${this.baseUrl}/services`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${this.credentials.peerId}:${this.credentials.secret}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
username,
|
||||
serviceFqn,
|
||||
sdp: offer.sdp,
|
||||
ttl,
|
||||
isPublic,
|
||||
metadata,
|
||||
signature,
|
||||
message
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
pc.close();
|
||||
throw new Error(error.error || 'Failed to publish service');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
// Close the connection for now (would be kept open in a real implementation)
|
||||
pc.close();
|
||||
|
||||
return {
|
||||
serviceId: data.serviceId,
|
||||
uuid: data.uuid,
|
||||
offerId: data.offerId,
|
||||
expiresAt: data.expiresAt
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Unpublishes a service
|
||||
*/
|
||||
async unpublishService(serviceId: string, username: string): Promise<void> {
|
||||
const response = await fetch(`${this.baseUrl}/services/${serviceId}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${this.credentials.peerId}:${this.credentials.secret}`
|
||||
},
|
||||
body: JSON.stringify({ username })
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
throw new Error(error.error || 'Failed to unpublish service');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exposes a service with an automatic connection handler
|
||||
* This is a convenience method that publishes the service and manages connections
|
||||
*
|
||||
* Set poolSize > 1 to enable offer pooling for handling multiple concurrent connections
|
||||
*/
|
||||
async exposeService(options: Omit<PublishServiceOptions, 'onConnection'> & {
|
||||
handler: (channel: RTCDataChannel, peer: RondevuPeer, connectionId?: string) => void;
|
||||
poolSize?: number;
|
||||
pollingInterval?: number;
|
||||
onPoolStatus?: (status: PoolStatus) => void;
|
||||
onError?: (error: Error, context: string) => void;
|
||||
}): Promise<ServiceHandle | PooledServiceHandle> {
|
||||
const {
|
||||
username,
|
||||
privateKey,
|
||||
serviceFqn,
|
||||
rtcConfig,
|
||||
isPublic,
|
||||
metadata,
|
||||
ttl,
|
||||
handler,
|
||||
poolSize,
|
||||
pollingInterval,
|
||||
onPoolStatus,
|
||||
onError
|
||||
} = options;
|
||||
|
||||
// If poolSize > 1, use pooled implementation
|
||||
if (poolSize && poolSize > 1) {
|
||||
const pool = new ServicePool(this.baseUrl, this.credentials, {
|
||||
username,
|
||||
privateKey,
|
||||
serviceFqn,
|
||||
rtcConfig,
|
||||
isPublic,
|
||||
metadata,
|
||||
ttl,
|
||||
handler: (channel, peer, connectionId) => handler(channel, peer, connectionId),
|
||||
poolSize,
|
||||
pollingInterval,
|
||||
onPoolStatus,
|
||||
onError
|
||||
});
|
||||
return await pool.start();
|
||||
}
|
||||
|
||||
// Otherwise, use existing single-offer logic (UNCHANGED)
|
||||
// Validate FQN
|
||||
this.validateServiceFqn(serviceFqn);
|
||||
|
||||
// Create peer connection
|
||||
const pc = new RTCPeerConnection(rtcConfig || {
|
||||
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
|
||||
});
|
||||
|
||||
// Create data channel
|
||||
const channel = pc.createDataChannel('rondevu-service');
|
||||
|
||||
// Set up handler
|
||||
channel.onopen = () => {
|
||||
const peer = new RondevuPeer(
|
||||
this.offersApi,
|
||||
rtcConfig || {
|
||||
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
|
||||
}
|
||||
);
|
||||
handler(channel, peer);
|
||||
};
|
||||
|
||||
// Create offer
|
||||
const offer = await pc.createOffer();
|
||||
await pc.setLocalDescription(offer);
|
||||
|
||||
if (!offer.sdp) {
|
||||
pc.close();
|
||||
throw new Error('Failed to generate SDP');
|
||||
}
|
||||
|
||||
// Create signature
|
||||
const timestamp = Date.now();
|
||||
const message = `publish:${username}:${serviceFqn}:${timestamp}`;
|
||||
const signature = await this.usernameApi.signMessage(message, privateKey);
|
||||
|
||||
// Publish service
|
||||
const response = await fetch(`${this.baseUrl}/services`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${this.credentials.peerId}:${this.credentials.secret}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
username,
|
||||
serviceFqn,
|
||||
sdp: offer.sdp,
|
||||
ttl,
|
||||
isPublic,
|
||||
metadata,
|
||||
signature,
|
||||
message
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
pc.close();
|
||||
throw new Error(error.error || 'Failed to expose service');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
return {
|
||||
serviceId: data.serviceId,
|
||||
uuid: data.uuid,
|
||||
offerId: data.offerId,
|
||||
unpublish: () => this.unpublishService(data.serviceId, username)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates service FQN format
|
||||
*/
|
||||
private validateServiceFqn(fqn: string): void {
|
||||
const parts = fqn.split('@');
|
||||
if (parts.length !== 2) {
|
||||
throw new Error('Service FQN must be in format: service-name@version');
|
||||
}
|
||||
|
||||
const [serviceName, version] = parts;
|
||||
|
||||
// Validate service name (reverse domain notation)
|
||||
const serviceNameRegex = /^[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+$/;
|
||||
if (!serviceNameRegex.test(serviceName)) {
|
||||
throw new Error('Service name must be reverse domain notation (e.g., com.example.service)');
|
||||
}
|
||||
|
||||
if (serviceName.length < 3 || serviceName.length > 128) {
|
||||
throw new Error('Service name must be 3-128 characters');
|
||||
}
|
||||
|
||||
// Validate version (semantic versioning)
|
||||
const versionRegex = /^[0-9]+\.[0-9]+\.[0-9]+(-[a-z0-9.-]+)?$/;
|
||||
if (!versionRegex.test(version)) {
|
||||
throw new Error('Version must be semantic versioning (e.g., 1.0.0, 2.1.3-beta)');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a service FQN into name and version
|
||||
*/
|
||||
parseServiceFqn(fqn: string): { name: string; version: string } {
|
||||
const parts = fqn.split('@');
|
||||
if (parts.length !== 2) {
|
||||
throw new Error('Invalid FQN format');
|
||||
}
|
||||
return { name: parts[0], version: parts[1] };
|
||||
}
|
||||
}
|
||||
236
src/types.ts
236
src/types.ts
@@ -1,236 +0,0 @@
|
||||
// ============================================================================
|
||||
// Signaling Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Session side - identifies which peer in a connection
|
||||
*/
|
||||
export type Side = 'offerer' | 'answerer';
|
||||
|
||||
/**
|
||||
* Session information returned from discovery endpoints
|
||||
*/
|
||||
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 {
|
||||
/** Peer identifier/metadata (max 1024 characters) */
|
||||
peerId: string;
|
||||
/** Signaling data for peer connection */
|
||||
offer: string;
|
||||
/** Optional custom connection code (if not provided, server generates UUID) */
|
||||
code?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Response from POST /:topic/offer
|
||||
*/
|
||||
export interface CreateOfferResponse {
|
||||
/** Unique session identifier (UUID) */
|
||||
code: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request body for POST /answer
|
||||
*/
|
||||
export interface AnswerRequest {
|
||||
/** Session UUID from the offer */
|
||||
code: string;
|
||||
/** Response signaling data (required if candidate not provided) */
|
||||
answer?: string;
|
||||
/** Additional signaling data (required if answer not provided) */
|
||||
candidate?: string;
|
||||
/** Which peer is sending the data */
|
||||
side: Side;
|
||||
}
|
||||
|
||||
/**
|
||||
* Response from POST /answer
|
||||
*/
|
||||
export interface AnswerResponse {
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Request body for POST /poll
|
||||
*/
|
||||
export interface PollRequest {
|
||||
/** Session UUID */
|
||||
code: string;
|
||||
/** Which side is polling */
|
||||
side: Side;
|
||||
}
|
||||
|
||||
/**
|
||||
* Response from POST /poll when side=offerer
|
||||
*/
|
||||
export interface PollOffererResponse {
|
||||
/** Answer from answerer (null if not yet received) */
|
||||
answer: string | null;
|
||||
/** Additional signaling data from answerer */
|
||||
answerCandidates: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Response from POST /poll when side=answerer
|
||||
*/
|
||||
export interface PollAnswererResponse {
|
||||
/** Offer from offerer */
|
||||
offer: string;
|
||||
/** Additional signaling data from offerer */
|
||||
offerCandidates: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Response from POST /poll (union type)
|
||||
*/
|
||||
export type PollResponse = PollOffererResponse | PollAnswererResponse;
|
||||
|
||||
/**
|
||||
* Response from GET / - server version information
|
||||
*/
|
||||
export interface VersionResponse {
|
||||
/** Git commit hash or version identifier */
|
||||
version: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Response from GET /health
|
||||
*/
|
||||
export interface HealthResponse {
|
||||
status: 'ok';
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Error response structure
|
||||
*/
|
||||
export interface ErrorResponse {
|
||||
error: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Client configuration options
|
||||
*/
|
||||
export interface RondevuClientOptions {
|
||||
/** Base URL of the Rondevu server (e.g., 'https://example.com') */
|
||||
baseUrl: string;
|
||||
/** Optional fetch implementation (for Node.js environments) */
|
||||
fetch?: typeof fetch;
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// WebRTC Types
|
||||
// ============================================================================
|
||||
|
||||
/**
|
||||
* Configuration options for Rondevu WebRTC client
|
||||
*/
|
||||
export interface RondevuOptions {
|
||||
/** Base URL of the Rondevu server (defaults to 'https://rondevu.xtrdev.workers.dev') */
|
||||
baseUrl?: string;
|
||||
/** Peer identifier (optional, auto-generated if not provided) */
|
||||
peerId?: string;
|
||||
/** Optional fetch implementation (for Node.js environments) */
|
||||
fetch?: typeof fetch;
|
||||
/** WebRTC configuration (ICE servers, etc.) */
|
||||
rtcConfig?: RTCConfiguration;
|
||||
/** Polling interval in milliseconds (default: 1000) */
|
||||
pollingInterval?: number;
|
||||
/** Connection timeout in milliseconds (default: 30000) */
|
||||
connectionTimeout?: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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';
|
||||
}
|
||||
|
||||
/**
|
||||
* Connection role - whether this peer is creating or answering
|
||||
*/
|
||||
export type ConnectionRole = 'offerer' | 'answerer';
|
||||
|
||||
/**
|
||||
* Parameters for creating a RondevuConnection
|
||||
*/
|
||||
export interface RondevuConnectionParams {
|
||||
id: string;
|
||||
topic: string;
|
||||
role: ConnectionRole;
|
||||
pc: RTCPeerConnection;
|
||||
localPeerId: string;
|
||||
remotePeerId: string;
|
||||
pollingInterval: number;
|
||||
connectionTimeout: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Event map for RondevuConnection events
|
||||
*/
|
||||
export interface RondevuConnectionEvents {
|
||||
connect: () => void;
|
||||
disconnect: () => void;
|
||||
error: (error: Error) => void;
|
||||
datachannel: (channel: RTCDataChannel) => void;
|
||||
stream: (stream: MediaStream) => void;
|
||||
}
|
||||
200
src/usernames.ts
Normal file
200
src/usernames.ts
Normal file
@@ -0,0 +1,200 @@
|
||||
import * as ed25519 from '@noble/ed25519';
|
||||
|
||||
// Set SHA-512 hash function for ed25519 (required in @noble/ed25519 v3+)
|
||||
// Uses built-in WebCrypto API which only provides async digest
|
||||
// We use the async ed25519 functions (signAsync, verifyAsync, getPublicKeyAsync)
|
||||
ed25519.hashes.sha512Async = async (message: Uint8Array) => {
|
||||
return new Uint8Array(await crypto.subtle.digest('SHA-512', message as BufferSource));
|
||||
};
|
||||
|
||||
/**
|
||||
* Username claim result
|
||||
*/
|
||||
export interface UsernameClaimResult {
|
||||
username: string;
|
||||
publicKey: string;
|
||||
privateKey: string;
|
||||
claimedAt: number;
|
||||
expiresAt: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Username availability check result
|
||||
*/
|
||||
export interface UsernameCheckResult {
|
||||
username: string;
|
||||
available: boolean;
|
||||
claimedAt?: number;
|
||||
expiresAt?: number;
|
||||
publicKey?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Uint8Array to base64 string
|
||||
*/
|
||||
function bytesToBase64(bytes: Uint8Array): string {
|
||||
const binString = Array.from(bytes, (byte) =>
|
||||
String.fromCodePoint(byte)
|
||||
).join('');
|
||||
return btoa(binString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert base64 string to Uint8Array
|
||||
*/
|
||||
function base64ToBytes(base64: string): Uint8Array {
|
||||
const binString = atob(base64);
|
||||
return Uint8Array.from(binString, (char) => char.codePointAt(0)!);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rondevu Username API
|
||||
* Handles username claiming with Ed25519 cryptographic proof
|
||||
*/
|
||||
export class RondevuUsername {
|
||||
constructor(private baseUrl: string) {}
|
||||
|
||||
/**
|
||||
* Generates an Ed25519 keypair for username claiming
|
||||
*/
|
||||
async generateKeypair(): Promise<{ publicKey: string; privateKey: string }> {
|
||||
const privateKey = ed25519.utils.randomSecretKey();
|
||||
const publicKey = await ed25519.getPublicKeyAsync(privateKey);
|
||||
|
||||
return {
|
||||
publicKey: bytesToBase64(publicKey),
|
||||
privateKey: bytesToBase64(privateKey)
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Signs a message with an Ed25519 private key
|
||||
*/
|
||||
async signMessage(message: string, privateKeyBase64: string): Promise<string> {
|
||||
const privateKey = base64ToBytes(privateKeyBase64);
|
||||
const encoder = new TextEncoder();
|
||||
const messageBytes = encoder.encode(message);
|
||||
|
||||
const signature = await ed25519.signAsync(messageBytes, privateKey);
|
||||
return bytesToBase64(signature);
|
||||
}
|
||||
|
||||
/**
|
||||
* Claims a username
|
||||
* Generates a new keypair if one is not provided
|
||||
*/
|
||||
async claimUsername(
|
||||
username: string,
|
||||
existingKeypair?: { publicKey: string; privateKey: string }
|
||||
): Promise<UsernameClaimResult> {
|
||||
// Generate or use existing keypair
|
||||
const keypair = existingKeypair || await this.generateKeypair();
|
||||
|
||||
// Create signed message
|
||||
const timestamp = Date.now();
|
||||
const message = `claim:${username}:${timestamp}`;
|
||||
const signature = await this.signMessage(message, keypair.privateKey);
|
||||
|
||||
// Send claim request
|
||||
const response = await fetch(`${this.baseUrl}/usernames/claim`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
username,
|
||||
publicKey: keypair.publicKey,
|
||||
signature,
|
||||
message
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
throw new Error(error.error || 'Failed to claim username');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
return {
|
||||
username: data.username,
|
||||
publicKey: keypair.publicKey,
|
||||
privateKey: keypair.privateKey,
|
||||
claimedAt: data.claimedAt,
|
||||
expiresAt: data.expiresAt
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a username is available
|
||||
*/
|
||||
async checkUsername(username: string): Promise<UsernameCheckResult> {
|
||||
const response = await fetch(`${this.baseUrl}/usernames/${username}`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to check username');
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
return {
|
||||
username: data.username,
|
||||
available: data.available,
|
||||
claimedAt: data.claimedAt,
|
||||
expiresAt: data.expiresAt,
|
||||
publicKey: data.publicKey
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper: Save keypair to localStorage
|
||||
* WARNING: This stores the private key in localStorage which is not the most secure
|
||||
* For production use, consider using IndexedDB with encryption or hardware security modules
|
||||
*/
|
||||
saveKeypairToStorage(username: string, publicKey: string, privateKey: string): void {
|
||||
const data = { username, publicKey, privateKey, savedAt: Date.now() };
|
||||
localStorage.setItem(`rondevu:keypair:${username}`, JSON.stringify(data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper: Load keypair from localStorage
|
||||
*/
|
||||
loadKeypairFromStorage(username: string): { publicKey: string; privateKey: string } | null {
|
||||
const stored = localStorage.getItem(`rondevu:keypair:${username}`);
|
||||
if (!stored) return null;
|
||||
|
||||
try {
|
||||
const data = JSON.parse(stored);
|
||||
return { publicKey: data.publicKey, privateKey: data.privateKey };
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper: Delete keypair from localStorage
|
||||
*/
|
||||
deleteKeypairFromStorage(username: string): void {
|
||||
localStorage.removeItem(`rondevu:keypair:${username}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export keypair as JSON string (for backup)
|
||||
*/
|
||||
exportKeypair(publicKey: string, privateKey: string): string {
|
||||
return JSON.stringify({
|
||||
publicKey,
|
||||
privateKey,
|
||||
exportedAt: Date.now()
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Import keypair from JSON string
|
||||
*/
|
||||
importKeypair(json: string): { publicKey: string; privateKey: string } {
|
||||
const data = JSON.parse(json);
|
||||
if (!data.publicKey || !data.privateKey) {
|
||||
throw new Error('Invalid keypair format');
|
||||
}
|
||||
return { publicKey: data.publicKey, privateKey: data.privateKey };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user