mirror of
https://github.com/xtr-dev/rondevu-client.git
synced 2025-12-10 19:03:24 +00:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a3b4dfa15f | |||
| 5c38f8f36c | |||
| 177ee2ec2d | |||
| d06b2166c1 | |||
| cbb0cc3f83 | |||
| fbd3be57d4 | |||
| 54355323d9 | |||
| 945d5a8792 | |||
| 58cd610694 | |||
| 5e673ac993 | |||
| 511bac8033 |
9
.prettierrc.json
Normal file
9
.prettierrc.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"semi": false,
|
||||||
|
"singleQuote": true,
|
||||||
|
"tabWidth": 4,
|
||||||
|
"useTabs": false,
|
||||||
|
"trailingComma": "es5",
|
||||||
|
"printWidth": 100,
|
||||||
|
"arrowParens": "avoid"
|
||||||
|
}
|
||||||
837
README.md
837
README.md
@@ -2,9 +2,9 @@
|
|||||||
|
|
||||||
[](https://www.npmjs.com/package/@xtr-dev/rondevu-client)
|
[](https://www.npmjs.com/package/@xtr-dev/rondevu-client)
|
||||||
|
|
||||||
🌐 **WebRTC with durable connections and automatic reconnection**
|
🌐 **Simple, high-level WebRTC peer-to-peer connections**
|
||||||
|
|
||||||
TypeScript/JavaScript client for Rondevu, providing durable WebRTC connections that survive network interruptions with automatic reconnection and message queuing.
|
TypeScript/JavaScript client for Rondevu, providing easy-to-use WebRTC connections with automatic signaling, username-based discovery, and built-in reconnection support.
|
||||||
|
|
||||||
**Related repositories:**
|
**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-client](https://github.com/xtr-dev/rondevu-client) - TypeScript client library ([npm](https://www.npmjs.com/package/@xtr-dev/rondevu-client))
|
||||||
@@ -15,14 +15,16 @@ TypeScript/JavaScript client for Rondevu, providing durable WebRTC connections t
|
|||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- **Durable Connections**: Automatic reconnection on network drops
|
- **High-Level Wrappers**: ServiceHost and ServiceClient eliminate WebRTC boilerplate
|
||||||
- **Message Queuing**: Messages sent during disconnections are queued and flushed on reconnect
|
- **Username-Based Discovery**: Connect to peers by username, not complex offer/answer exchange
|
||||||
- **Durable Channels**: RTCDataChannel wrappers that survive connection drops
|
- **Semver-Compatible Matching**: Requesting chat@1.0.0 matches any compatible 1.x.x version
|
||||||
- **TTL Auto-Refresh**: Services automatically republish before expiration
|
- **Privacy-First Design**: Services are hidden by default - no enumeration possible
|
||||||
- **Username Claiming**: Cryptographic ownership with Ed25519 signatures
|
- **Automatic Reconnection**: Built-in retry logic with exponential backoff
|
||||||
- **Service Publishing**: Package-style naming (com.example.chat@1.0.0)
|
- **Message Queuing**: Messages sent while disconnected are queued and flushed on reconnect
|
||||||
|
- **Cryptographic Username Claiming**: Secure ownership with Ed25519 signatures
|
||||||
|
- **Service Publishing**: Package-style naming (chat.app@1.0.0) with multiple simultaneous offers
|
||||||
- **TypeScript**: Full type safety and autocomplete
|
- **TypeScript**: Full type safety and autocomplete
|
||||||
- **Configurable**: All timeouts, retry limits, and queue sizes are configurable
|
- **Configurable Polling**: Exponential backoff with jitter to reduce server load
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
@@ -32,588 +34,400 @@ npm install @xtr-dev/rondevu-client
|
|||||||
|
|
||||||
## Quick Start
|
## Quick Start
|
||||||
|
|
||||||
### Publishing a Service (Alice)
|
### Hosting a Service (Alice)
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { Rondevu } from '@xtr-dev/rondevu-client';
|
import { RondevuService, ServiceHost } from '@xtr-dev/rondevu-client'
|
||||||
|
|
||||||
// Initialize client and register
|
// Step 1: Create and initialize service
|
||||||
const client = new Rondevu({ baseUrl: 'https://api.ronde.vu' });
|
const service = new RondevuService({
|
||||||
await client.register();
|
apiUrl: 'https://api.ronde.vu',
|
||||||
|
username: 'alice'
|
||||||
|
})
|
||||||
|
|
||||||
// Step 1: Claim username (one-time)
|
await service.initialize() // Generates keypair
|
||||||
const claim = await client.usernames.claimUsername('alice');
|
await service.claimUsername() // Claims username with signature
|
||||||
client.usernames.saveKeypairToStorage('alice', claim.publicKey, claim.privateKey);
|
|
||||||
|
|
||||||
// Step 2: Expose service with handler
|
// Step 2: Create ServiceHost
|
||||||
const keypair = client.usernames.loadKeypairFromStorage('alice');
|
const host = new ServiceHost({
|
||||||
|
service: 'chat.app@1.0.0',
|
||||||
|
rondevuService: service,
|
||||||
|
maxPeers: 5, // Accept up to 5 connections
|
||||||
|
ttl: 300000 // 5 minutes
|
||||||
|
})
|
||||||
|
|
||||||
const service = await client.exposeService({
|
// Step 3: Listen for incoming connections
|
||||||
username: 'alice',
|
host.events.on('connection', (connection) => {
|
||||||
privateKey: keypair.privateKey,
|
console.log('✅ New connection!')
|
||||||
serviceFqn: 'chat@1.0.0',
|
|
||||||
isPublic: true,
|
|
||||||
poolSize: 10, // Handle 10 concurrent connections
|
|
||||||
handler: (channel, connectionId) => {
|
|
||||||
console.log(`📡 New connection: ${connectionId}`);
|
|
||||||
|
|
||||||
channel.on('message', (data) => {
|
connection.events.on('message', (msg) => {
|
||||||
console.log('📥 Received:', data);
|
console.log('📨 Received:', msg)
|
||||||
channel.send(`Echo: ${data}`);
|
connection.sendMessage('Hello from Alice!')
|
||||||
});
|
})
|
||||||
|
|
||||||
channel.on('close', () => {
|
connection.events.on('state-change', (state) => {
|
||||||
console.log(`👋 Connection ${connectionId} closed`);
|
console.log('Connection state:', state)
|
||||||
});
|
})
|
||||||
}
|
})
|
||||||
});
|
|
||||||
|
|
||||||
// Start the service
|
host.events.on('error', (error) => {
|
||||||
const info = await service.start();
|
console.error('Host error:', error)
|
||||||
console.log(`Service published with UUID: ${info.uuid}`);
|
})
|
||||||
console.log('Waiting for connections...');
|
|
||||||
|
|
||||||
// Later: stop the service
|
// Step 4: Start hosting
|
||||||
await service.stop();
|
await host.start()
|
||||||
|
console.log('Service is now live! Others can connect to @alice')
|
||||||
|
|
||||||
|
// Later: stop hosting
|
||||||
|
host.dispose()
|
||||||
```
|
```
|
||||||
|
|
||||||
### Connecting to a Service (Bob)
|
### Connecting to a Service (Bob)
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { Rondevu } from '@xtr-dev/rondevu-client';
|
import { RondevuService, ServiceClient } from '@xtr-dev/rondevu-client'
|
||||||
|
|
||||||
// Initialize client and register
|
// Step 1: Create and initialize service
|
||||||
const client = new Rondevu({ baseUrl: 'https://api.ronde.vu' });
|
const service = new RondevuService({
|
||||||
await client.register();
|
apiUrl: 'https://api.ronde.vu',
|
||||||
|
username: 'bob'
|
||||||
|
})
|
||||||
|
|
||||||
// Connect to Alice's service
|
await service.initialize()
|
||||||
const connection = await client.connect('alice', 'chat@1.0.0', {
|
await service.claimUsername()
|
||||||
maxReconnectAttempts: 5
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create a durable channel
|
// Step 2: Create ServiceClient
|
||||||
const channel = connection.createChannel('main');
|
const client = new ServiceClient({
|
||||||
|
username: 'alice', // Connect to Alice
|
||||||
|
serviceFqn: 'chat.app@1.0.0',
|
||||||
|
rondevuService: service,
|
||||||
|
autoReconnect: true,
|
||||||
|
maxReconnectAttempts: 5
|
||||||
|
})
|
||||||
|
|
||||||
channel.on('message', (data) => {
|
// Step 3: Listen for connection events
|
||||||
console.log('📥 Received:', data);
|
client.events.on('connected', (connection) => {
|
||||||
});
|
console.log('✅ Connected to Alice!')
|
||||||
|
|
||||||
channel.on('open', () => {
|
connection.events.on('message', (msg) => {
|
||||||
console.log('✅ Channel open');
|
console.log('📨 Received:', msg)
|
||||||
channel.send('Hello Alice!');
|
})
|
||||||
});
|
|
||||||
|
|
||||||
// Listen for connection events
|
// Send a message
|
||||||
connection.on('connected', () => {
|
connection.sendMessage('Hello from Bob!')
|
||||||
console.log('🎉 Connected to Alice');
|
})
|
||||||
});
|
|
||||||
|
|
||||||
connection.on('reconnecting', (attempt, max, delay) => {
|
client.events.on('disconnected', () => {
|
||||||
console.log(`🔄 Reconnecting... (${attempt}/${max}, retry in ${delay}ms)`);
|
console.log('🔌 Disconnected')
|
||||||
});
|
})
|
||||||
|
|
||||||
connection.on('disconnected', () => {
|
client.events.on('reconnecting', ({ attempt, maxAttempts }) => {
|
||||||
console.log('🔌 Disconnected');
|
console.log(`🔄 Reconnecting (${attempt}/${maxAttempts})...`)
|
||||||
});
|
})
|
||||||
|
|
||||||
connection.on('failed', (error) => {
|
client.events.on('error', (error) => {
|
||||||
console.error('❌ Connection failed permanently:', error);
|
console.error('❌ Error:', error)
|
||||||
});
|
})
|
||||||
|
|
||||||
// Establish the connection
|
// Step 4: Connect
|
||||||
await connection.connect();
|
await client.connect()
|
||||||
|
|
||||||
// Messages sent during disconnection are automatically queued
|
// Later: disconnect
|
||||||
channel.send('This will be queued if disconnected');
|
client.dispose()
|
||||||
|
|
||||||
// Later: close the connection
|
|
||||||
await connection.close();
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Core Concepts
|
## Core Concepts
|
||||||
|
|
||||||
### DurableConnection
|
### RondevuService
|
||||||
|
|
||||||
Manages WebRTC peer lifecycle with automatic reconnection:
|
Handles authentication and username management:
|
||||||
- Automatically reconnects when connection drops
|
- Generates Ed25519 keypair for signing
|
||||||
- Exponential backoff with jitter (1s → 2s → 4s → 8s → ... max 30s)
|
- Claims usernames with cryptographic proof
|
||||||
- Configurable max retry attempts (default: 10)
|
- Provides API client for signaling server
|
||||||
- Manages multiple DurableChannel instances
|
|
||||||
|
|
||||||
### DurableChannel
|
### ServiceHost
|
||||||
|
|
||||||
Wraps RTCDataChannel with message queuing:
|
High-level wrapper for hosting a WebRTC service:
|
||||||
- Queues messages during disconnection
|
- Automatically creates and publishes offers
|
||||||
- Flushes queue on reconnection
|
- Handles incoming connections
|
||||||
- Configurable queue size and message age limits
|
- Manages ICE candidate exchange
|
||||||
- RTCDataChannel-compatible API with event emitters
|
- Supports multiple simultaneous peers
|
||||||
|
|
||||||
### DurableService
|
### ServiceClient
|
||||||
|
|
||||||
Server-side service with TTL auto-refresh:
|
High-level wrapper for connecting to services:
|
||||||
- Automatically republishes service before TTL expires
|
- Discovers services by username
|
||||||
- Creates DurableConnection for each incoming peer
|
- Handles offer/answer exchange automatically
|
||||||
- Manages connection pool for multiple simultaneous connections
|
- Built-in auto-reconnection with exponential backoff
|
||||||
|
- Event-driven API
|
||||||
|
|
||||||
|
### RTCDurableConnection
|
||||||
|
|
||||||
|
Low-level connection wrapper (used internally):
|
||||||
|
- Manages WebRTC PeerConnection lifecycle
|
||||||
|
- Handles ICE candidate polling
|
||||||
|
- Provides message queue for reliability
|
||||||
|
- State management and events
|
||||||
|
|
||||||
## API Reference
|
## API Reference
|
||||||
|
|
||||||
### Main Client
|
### RondevuService
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
const client = new Rondevu({
|
const service = new RondevuService({
|
||||||
baseUrl: 'https://api.ronde.vu', // optional, default shown
|
apiUrl: string, // Signaling server URL
|
||||||
credentials?: { peerId, secret }, // optional, skip registration
|
username: string, // Your username
|
||||||
fetch?: customFetch, // optional, for Node.js < 18
|
keypair?: Keypair // Optional: reuse existing keypair
|
||||||
RTCPeerConnection?: RTCPeerConnection, // optional, for Node.js
|
})
|
||||||
RTCSessionDescription?: RTCSessionDescription,
|
|
||||||
RTCIceCandidate?: RTCIceCandidate
|
|
||||||
});
|
|
||||||
|
|
||||||
// Register and get credentials
|
// Initialize service (generates keypair if not provided)
|
||||||
const creds = await client.register();
|
await service.initialize(): Promise<void>
|
||||||
// { peerId: '...', secret: '...' }
|
|
||||||
|
|
||||||
// Check if authenticated
|
// Claim username with cryptographic signature
|
||||||
client.isAuthenticated(); // boolean
|
await service.claimUsername(): Promise<void>
|
||||||
|
|
||||||
// Get current credentials
|
// Check if username is claimed
|
||||||
client.getCredentials(); // { peerId, secret } | undefined
|
service.isUsernameClaimed(): boolean
|
||||||
|
|
||||||
|
// Get current username
|
||||||
|
service.getUsername(): string
|
||||||
|
|
||||||
|
// Get keypair
|
||||||
|
service.getKeypair(): Keypair
|
||||||
|
|
||||||
|
// Get API client
|
||||||
|
service.getAPI(): RondevuAPI
|
||||||
```
|
```
|
||||||
|
|
||||||
### Username API
|
### ServiceHost
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// Check username availability
|
const host = new ServiceHost({
|
||||||
const check = await client.usernames.checkUsername('alice');
|
service: string, // Service FQN (e.g., 'chat.app@1.0.0')
|
||||||
// { available: true } or { available: false, expiresAt: number, publicKey: string }
|
rondevuService: RondevuService,
|
||||||
|
maxPeers?: number, // Default: 5
|
||||||
|
ttl?: number, // Default: 300000 (5 minutes)
|
||||||
|
isPublic?: boolean, // Default: true
|
||||||
|
rtcConfiguration?: RTCConfiguration
|
||||||
|
})
|
||||||
|
|
||||||
// Claim username with new keypair
|
// Start hosting
|
||||||
const claim = await client.usernames.claimUsername('alice');
|
await host.start(): Promise<void>
|
||||||
// { username, publicKey, privateKey, claimedAt, expiresAt }
|
|
||||||
|
|
||||||
// Save keypair to localStorage
|
// Stop hosting and cleanup
|
||||||
client.usernames.saveKeypairToStorage('alice', claim.publicKey, claim.privateKey);
|
host.dispose(): void
|
||||||
|
|
||||||
// Load keypair from localStorage
|
// Get all active connections
|
||||||
const keypair = client.usernames.loadKeypairFromStorage('alice');
|
host.getConnections(): RTCDurableConnection[]
|
||||||
// { publicKey, privateKey } | null
|
|
||||||
|
// Events
|
||||||
|
host.events.on('connection', (conn: RTCDurableConnection) => {})
|
||||||
|
host.events.on('error', (error: Error) => {})
|
||||||
```
|
```
|
||||||
|
|
||||||
**Username Rules:**
|
### ServiceClient
|
||||||
- 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
|
|
||||||
|
|
||||||
### Durable Service API
|
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// Expose a durable service
|
const client = new ServiceClient({
|
||||||
const service = await client.exposeService({
|
username: string, // Host username to connect to
|
||||||
username: 'alice',
|
serviceFqn: string, // Service FQN (e.g., 'chat.app@1.0.0')
|
||||||
privateKey: keypair.privateKey,
|
rondevuService: RondevuService,
|
||||||
serviceFqn: 'chat@1.0.0',
|
autoReconnect?: boolean, // Default: true
|
||||||
|
maxReconnectAttempts?: number, // Default: 5
|
||||||
|
rtcConfiguration?: RTCConfiguration
|
||||||
|
})
|
||||||
|
|
||||||
// Service options
|
// Connect to service
|
||||||
isPublic: true, // optional, default: false
|
await client.connect(): Promise<RTCDurableConnection>
|
||||||
metadata: { version: '1.0' }, // optional
|
|
||||||
ttl: 300000, // optional, default: 5 minutes
|
|
||||||
ttlRefreshMargin: 0.2, // optional, refresh at 80% of TTL
|
|
||||||
|
|
||||||
// Connection pooling
|
// Disconnect and cleanup
|
||||||
poolSize: 10, // optional, default: 1
|
client.dispose(): void
|
||||||
pollingInterval: 2000, // optional, default: 2000ms
|
|
||||||
|
|
||||||
// Connection options (applied to incoming connections)
|
// Get current connection
|
||||||
maxReconnectAttempts: 10, // optional, default: 10
|
client.getConnection(): RTCDurableConnection | null
|
||||||
reconnectBackoffBase: 1000, // optional, default: 1000ms
|
|
||||||
reconnectBackoffMax: 30000, // optional, default: 30000ms
|
|
||||||
reconnectJitter: 0.2, // optional, default: 0.2 (±20%)
|
|
||||||
connectionTimeout: 30000, // optional, default: 30000ms
|
|
||||||
|
|
||||||
// Message queuing
|
// Events
|
||||||
maxQueueSize: 1000, // optional, default: 1000
|
client.events.on('connected', (conn: RTCDurableConnection) => {})
|
||||||
maxMessageAge: 60000, // optional, default: 60000ms (1 minute)
|
client.events.on('disconnected', () => {})
|
||||||
|
client.events.on('reconnecting', (info: { attempt: number, maxAttempts: number }) => {})
|
||||||
// WebRTC configuration
|
client.events.on('error', (error: Error) => {})
|
||||||
rtcConfig: {
|
|
||||||
iceServers: [
|
|
||||||
{ urls: 'stun:stun.l.google.com:19302' }
|
|
||||||
]
|
|
||||||
},
|
|
||||||
|
|
||||||
// Connection handler
|
|
||||||
handler: (channel, connectionId) => {
|
|
||||||
// Handle incoming connection
|
|
||||||
channel.on('message', (data) => {
|
|
||||||
console.log('Received:', data);
|
|
||||||
channel.send(`Echo: ${data}`);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Start the service
|
|
||||||
const info = await service.start();
|
|
||||||
// { serviceId: '...', uuid: '...', expiresAt: 1234567890 }
|
|
||||||
|
|
||||||
// Get active connections
|
|
||||||
const connections = service.getActiveConnections();
|
|
||||||
// ['conn-123', 'conn-456']
|
|
||||||
|
|
||||||
// Get service info
|
|
||||||
const serviceInfo = service.getServiceInfo();
|
|
||||||
// { serviceId: '...', uuid: '...', expiresAt: 1234567890 } | null
|
|
||||||
|
|
||||||
// Stop the service
|
|
||||||
await service.stop();
|
|
||||||
```
|
```
|
||||||
|
|
||||||
**Service Events:**
|
### RTCDurableConnection
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
service.on('published', (serviceId, uuid) => {
|
// Connection state
|
||||||
console.log(`Service published: ${uuid}`);
|
connection.state: 'connected' | 'connecting' | 'disconnected'
|
||||||
});
|
|
||||||
|
|
||||||
service.on('connection', (connectionId) => {
|
// Send message (returns true if sent, false if queued)
|
||||||
console.log(`New connection: ${connectionId}`);
|
await connection.sendMessage(message: string): Promise<boolean>
|
||||||
});
|
|
||||||
|
|
||||||
service.on('disconnection', (connectionId) => {
|
// Queue message for sending when connected
|
||||||
console.log(`Connection closed: ${connectionId}`);
|
await connection.queueMessage(message: string, options?: QueueMessageOptions): Promise<void>
|
||||||
});
|
|
||||||
|
|
||||||
service.on('ttl-refreshed', (expiresAt) => {
|
// Disconnect
|
||||||
console.log(`TTL refreshed, expires at: ${new Date(expiresAt)}`);
|
connection.disconnect(): void
|
||||||
});
|
|
||||||
|
|
||||||
service.on('error', (error, context) => {
|
// Events
|
||||||
console.error(`Service error (${context}):`, error);
|
connection.events.on('message', (msg: string) => {})
|
||||||
});
|
connection.events.on('state-change', (state: ConnectionStates) => {})
|
||||||
|
|
||||||
service.on('closed', () => {
|
|
||||||
console.log('Service stopped');
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### Durable Connection API
|
## Configuration
|
||||||
|
|
||||||
|
### Polling Configuration
|
||||||
|
|
||||||
|
The signaling uses configurable polling with exponential backoff:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// Connect by username and service FQN
|
// Default polling config
|
||||||
const connection = await client.connect('alice', 'chat@1.0.0', {
|
{
|
||||||
// Connection options
|
initialInterval: 500, // Start at 500ms
|
||||||
maxReconnectAttempts: 10, // optional, default: 10
|
maxInterval: 5000, // Max 5 seconds
|
||||||
reconnectBackoffBase: 1000, // optional, default: 1000ms
|
backoffMultiplier: 1.5, // Increase by 1.5x each time
|
||||||
reconnectBackoffMax: 30000, // optional, default: 30000ms
|
maxRetries: 50, // Max 50 attempts
|
||||||
reconnectJitter: 0.2, // optional, default: 0.2 (±20%)
|
jitter: true // Add random 0-100ms to prevent thundering herd
|
||||||
connectionTimeout: 30000, // optional, default: 30000ms
|
|
||||||
|
|
||||||
// Message queuing
|
|
||||||
maxQueueSize: 1000, // optional, default: 1000
|
|
||||||
maxMessageAge: 60000, // optional, default: 60000ms
|
|
||||||
|
|
||||||
// WebRTC configuration
|
|
||||||
rtcConfig: {
|
|
||||||
iceServers: [
|
|
||||||
{ urls: 'stun:stun.l.google.com:19302' }
|
|
||||||
]
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Connect by UUID
|
|
||||||
const connection2 = await client.connectByUuid('service-uuid-here', {
|
|
||||||
maxReconnectAttempts: 5
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create channels before connecting
|
|
||||||
const channel = connection.createChannel('main');
|
|
||||||
const fileChannel = connection.createChannel('files', {
|
|
||||||
ordered: false,
|
|
||||||
maxRetransmits: 3
|
|
||||||
});
|
|
||||||
|
|
||||||
// Get existing channel
|
|
||||||
const existingChannel = connection.getChannel('main');
|
|
||||||
|
|
||||||
// Check connection state
|
|
||||||
const state = connection.getState();
|
|
||||||
// 'connecting' | 'connected' | 'reconnecting' | 'disconnected' | 'failed' | 'closed'
|
|
||||||
|
|
||||||
const isConnected = connection.isConnected();
|
|
||||||
|
|
||||||
// Connect
|
|
||||||
await connection.connect();
|
|
||||||
|
|
||||||
// Close connection
|
|
||||||
await connection.close();
|
|
||||||
```
|
|
||||||
|
|
||||||
**Connection Events:**
|
|
||||||
```typescript
|
|
||||||
connection.on('state', (newState, previousState) => {
|
|
||||||
console.log(`State: ${previousState} → ${newState}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
connection.on('connected', () => {
|
|
||||||
console.log('Connected');
|
|
||||||
});
|
|
||||||
|
|
||||||
connection.on('reconnecting', (attempt, maxAttempts, delay) => {
|
|
||||||
console.log(`Reconnecting (${attempt}/${maxAttempts}) in ${delay}ms`);
|
|
||||||
});
|
|
||||||
|
|
||||||
connection.on('disconnected', () => {
|
|
||||||
console.log('Disconnected');
|
|
||||||
});
|
|
||||||
|
|
||||||
connection.on('failed', (error, permanent) => {
|
|
||||||
console.error('Connection failed:', error, 'Permanent:', permanent);
|
|
||||||
});
|
|
||||||
|
|
||||||
connection.on('closed', () => {
|
|
||||||
console.log('Connection closed');
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
### Durable Channel API
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
const channel = connection.createChannel('chat', {
|
|
||||||
ordered: true, // optional, default: true
|
|
||||||
maxRetransmits: undefined // optional, for unordered channels
|
|
||||||
});
|
|
||||||
|
|
||||||
// Send data (queued if disconnected)
|
|
||||||
channel.send('Hello!');
|
|
||||||
channel.send(new ArrayBuffer(1024));
|
|
||||||
channel.send(new Blob(['data']));
|
|
||||||
|
|
||||||
// Check state
|
|
||||||
const state = channel.readyState;
|
|
||||||
// 'connecting' | 'open' | 'closing' | 'closed'
|
|
||||||
|
|
||||||
// Get buffered amount
|
|
||||||
const buffered = channel.bufferedAmount;
|
|
||||||
|
|
||||||
// Set buffered amount low threshold
|
|
||||||
channel.bufferedAmountLowThreshold = 16 * 1024; // 16KB
|
|
||||||
|
|
||||||
// Get queue size (for debugging)
|
|
||||||
const queueSize = channel.getQueueSize();
|
|
||||||
|
|
||||||
// Close channel
|
|
||||||
channel.close();
|
|
||||||
```
|
|
||||||
|
|
||||||
**Channel Events:**
|
|
||||||
```typescript
|
|
||||||
channel.on('open', () => {
|
|
||||||
console.log('Channel open');
|
|
||||||
});
|
|
||||||
|
|
||||||
channel.on('message', (data) => {
|
|
||||||
console.log('Received:', data);
|
|
||||||
});
|
|
||||||
|
|
||||||
channel.on('error', (error) => {
|
|
||||||
console.error('Channel error:', error);
|
|
||||||
});
|
|
||||||
|
|
||||||
channel.on('close', () => {
|
|
||||||
console.log('Channel closed');
|
|
||||||
});
|
|
||||||
|
|
||||||
channel.on('bufferedAmountLow', () => {
|
|
||||||
console.log('Buffer drained, safe to send more');
|
|
||||||
});
|
|
||||||
|
|
||||||
channel.on('queueOverflow', (droppedCount) => {
|
|
||||||
console.warn(`Queue overflow: ${droppedCount} messages dropped`);
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
## Configuration Options
|
|
||||||
|
|
||||||
### Connection Configuration
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
interface DurableConnectionConfig {
|
|
||||||
maxReconnectAttempts?: number; // default: 10
|
|
||||||
reconnectBackoffBase?: number; // default: 1000 (1 second)
|
|
||||||
reconnectBackoffMax?: number; // default: 30000 (30 seconds)
|
|
||||||
reconnectJitter?: number; // default: 0.2 (±20%)
|
|
||||||
connectionTimeout?: number; // default: 30000 (30 seconds)
|
|
||||||
maxQueueSize?: number; // default: 1000 messages
|
|
||||||
maxMessageAge?: number; // default: 60000 (1 minute)
|
|
||||||
rtcConfig?: RTCConfiguration;
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Service Configuration
|
This is handled automatically - no configuration needed.
|
||||||
|
|
||||||
|
### WebRTC Configuration
|
||||||
|
|
||||||
|
Provide custom STUN/TURN servers:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
interface DurableServiceConfig extends DurableConnectionConfig {
|
const host = new ServiceHost({
|
||||||
username: string;
|
service: 'chat.app@1.0.0',
|
||||||
privateKey: string;
|
rondevuService: service,
|
||||||
serviceFqn: string;
|
rtcConfiguration: {
|
||||||
isPublic?: boolean; // default: false
|
iceServers: [
|
||||||
metadata?: Record<string, any>;
|
{ urls: 'stun:stun.l.google.com:19302' },
|
||||||
ttl?: number; // default: 300000 (5 minutes)
|
{
|
||||||
ttlRefreshMargin?: number; // default: 0.2 (refresh at 80%)
|
urls: 'turn:turn.example.com:3478',
|
||||||
poolSize?: number; // default: 1
|
username: 'user',
|
||||||
pollingInterval?: number; // default: 2000 (2 seconds)
|
credential: 'pass'
|
||||||
}
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## 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 signature
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
### Chat Application
|
### Chat Application
|
||||||
|
|
||||||
```typescript
|
See [demo/demo.js](./demo/demo.js) for a complete working example.
|
||||||
// Server
|
|
||||||
const client = new Rondevu();
|
|
||||||
await client.register();
|
|
||||||
|
|
||||||
const claim = await client.usernames.claimUsername('alice');
|
### Persistent Keypair
|
||||||
client.usernames.saveKeypairToStorage('alice', claim.publicKey, claim.privateKey);
|
|
||||||
const keypair = client.usernames.loadKeypairFromStorage('alice');
|
|
||||||
|
|
||||||
const service = await client.exposeService({
|
|
||||||
username: 'alice',
|
|
||||||
privateKey: keypair.privateKey,
|
|
||||||
serviceFqn: 'chat@1.0.0',
|
|
||||||
isPublic: true,
|
|
||||||
poolSize: 50, // Handle 50 concurrent users
|
|
||||||
handler: (channel, connectionId) => {
|
|
||||||
console.log(`User ${connectionId} joined`);
|
|
||||||
|
|
||||||
channel.on('message', (data) => {
|
|
||||||
console.log(`[${connectionId}]: ${data}`);
|
|
||||||
// Broadcast to all users (implement your broadcast logic)
|
|
||||||
});
|
|
||||||
|
|
||||||
channel.on('close', () => {
|
|
||||||
console.log(`User ${connectionId} left`);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
await service.start();
|
|
||||||
|
|
||||||
// Client
|
|
||||||
const client2 = new Rondevu();
|
|
||||||
await client2.register();
|
|
||||||
|
|
||||||
const connection = await client2.connect('alice', 'chat@1.0.0');
|
|
||||||
const channel = connection.createChannel('chat');
|
|
||||||
|
|
||||||
channel.on('message', (data) => {
|
|
||||||
console.log('Message:', data);
|
|
||||||
});
|
|
||||||
|
|
||||||
await connection.connect();
|
|
||||||
channel.send('Hello everyone!');
|
|
||||||
```
|
|
||||||
|
|
||||||
### File Transfer with Progress
|
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
// Server
|
// Save keypair to localStorage
|
||||||
const service = await client.exposeService({
|
const service = new RondevuService({
|
||||||
username: 'alice',
|
apiUrl: 'https://api.ronde.vu',
|
||||||
privateKey: keypair.privateKey,
|
username: 'alice'
|
||||||
serviceFqn: 'files@1.0.0',
|
})
|
||||||
handler: (channel, connectionId) => {
|
|
||||||
channel.on('message', async (data) => {
|
|
||||||
const request = JSON.parse(data);
|
|
||||||
|
|
||||||
if (request.action === 'download') {
|
await service.initialize()
|
||||||
const file = await fs.readFile(request.path);
|
await service.claimUsername()
|
||||||
const chunkSize = 16 * 1024; // 16KB chunks
|
|
||||||
|
|
||||||
for (let i = 0; i < file.byteLength; i += chunkSize) {
|
// Save for later
|
||||||
const chunk = file.slice(i, i + chunkSize);
|
localStorage.setItem('rondevu-keypair', JSON.stringify(service.getKeypair()))
|
||||||
channel.send(chunk);
|
localStorage.setItem('rondevu-username', service.getUsername())
|
||||||
|
|
||||||
// Wait for buffer to drain if needed
|
// Load on next session
|
||||||
while (channel.bufferedAmount > 16 * 1024 * 1024) { // 16MB
|
const savedKeypair = JSON.parse(localStorage.getItem('rondevu-keypair'))
|
||||||
await new Promise(resolve => setTimeout(resolve, 100));
|
const savedUsername = localStorage.getItem('rondevu-username')
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
channel.send(JSON.stringify({ done: true }));
|
const service2 = new RondevuService({
|
||||||
}
|
apiUrl: 'https://api.ronde.vu',
|
||||||
});
|
username: savedUsername,
|
||||||
}
|
keypair: savedKeypair
|
||||||
});
|
})
|
||||||
|
|
||||||
await service.start();
|
await service2.initialize() // Reuses keypair
|
||||||
|
|
||||||
// Client
|
|
||||||
const connection = await client.connect('alice', 'files@1.0.0');
|
|
||||||
const channel = connection.createChannel('files');
|
|
||||||
|
|
||||||
const chunks = [];
|
|
||||||
channel.on('message', (data) => {
|
|
||||||
if (typeof data === 'string') {
|
|
||||||
const msg = JSON.parse(data);
|
|
||||||
if (msg.done) {
|
|
||||||
const blob = new Blob(chunks);
|
|
||||||
console.log('Download complete:', blob.size, 'bytes');
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
chunks.push(data);
|
|
||||||
console.log('Progress:', chunks.length * 16 * 1024, 'bytes');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
await connection.connect();
|
|
||||||
channel.send(JSON.stringify({ action: 'download', path: '/file.zip' }));
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Platform-Specific Setup
|
### Message Queue Example
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Messages are automatically queued if not connected yet
|
||||||
|
client.events.on('connected', (connection) => {
|
||||||
|
// Send immediately
|
||||||
|
connection.sendMessage('Hello!')
|
||||||
|
})
|
||||||
|
|
||||||
|
// Or queue for later
|
||||||
|
await client.connect()
|
||||||
|
const conn = client.getConnection()
|
||||||
|
await conn.queueMessage('This will be sent when connected', {
|
||||||
|
expiresAt: Date.now() + 60000 // Expire after 1 minute
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
## Migration from v0.9.x
|
||||||
|
|
||||||
|
v0.11.0+ introduces high-level wrappers, RESTful API changes, and semver-compatible discovery:
|
||||||
|
|
||||||
|
**API Changes:**
|
||||||
|
- Server endpoints restructured (`/usernames/*` → `/users/*`)
|
||||||
|
- Added `ServiceHost` and `ServiceClient` wrappers
|
||||||
|
- Message queue fully implemented
|
||||||
|
- Configurable polling with exponential backoff
|
||||||
|
- Removed deprecated `cleanup()` methods (use `dispose()`)
|
||||||
|
- **v0.11.0+**: Services use `offers` array instead of single `sdp`
|
||||||
|
- **v0.11.0+**: Semver-compatible service discovery (chat@1.0.0 matches 1.x.x)
|
||||||
|
- **v0.11.0+**: All services are hidden - no listing endpoint
|
||||||
|
- **v0.11.0+**: Services support multiple simultaneous offers for connection pooling
|
||||||
|
|
||||||
|
**Migration Guide:**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Before (v0.9.x) - Manual WebRTC setup
|
||||||
|
const signaler = new RondevuSignaler(service, 'chat@1.0.0')
|
||||||
|
const context = new WebRTCContext()
|
||||||
|
const pc = context.createPeerConnection()
|
||||||
|
// ... 50+ lines of boilerplate
|
||||||
|
|
||||||
|
// After (v0.11.0) - ServiceHost wrapper
|
||||||
|
const host = new ServiceHost({
|
||||||
|
service: 'chat@1.0.0',
|
||||||
|
rondevuService: service
|
||||||
|
})
|
||||||
|
await host.start()
|
||||||
|
// Done!
|
||||||
|
```
|
||||||
|
|
||||||
|
## Platform Support
|
||||||
|
|
||||||
### Modern Browsers
|
### Modern Browsers
|
||||||
Works out of the box - no additional setup needed.
|
Works out of the box - no additional setup needed.
|
||||||
|
|
||||||
### Node.js 18+
|
### Node.js 18+
|
||||||
Native fetch is available, but you need WebRTC polyfills:
|
Native fetch is available, but WebRTC requires polyfills:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install wrtc
|
npm install wrtc
|
||||||
```
|
```
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { Rondevu } from '@xtr-dev/rondevu-client';
|
import { WebRTCContext } from '@xtr-dev/rondevu-client'
|
||||||
import { RTCPeerConnection, RTCSessionDescription, RTCIceCandidate } from 'wrtc';
|
import { RTCPeerConnection, RTCSessionDescription, RTCIceCandidate } from 'wrtc'
|
||||||
|
|
||||||
const client = new Rondevu({
|
// Configure WebRTC context
|
||||||
baseUrl: 'https://api.ronde.vu',
|
const context = new WebRTCContext({
|
||||||
RTCPeerConnection,
|
RTCPeerConnection,
|
||||||
RTCSessionDescription,
|
RTCSessionDescription,
|
||||||
RTCIceCandidate
|
RTCIceCandidate
|
||||||
});
|
} as any)
|
||||||
```
|
|
||||||
|
|
||||||
### Node.js < 18
|
|
||||||
Install both fetch and WebRTC polyfills:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
npm install node-fetch wrtc
|
|
||||||
```
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { Rondevu } from '@xtr-dev/rondevu-client';
|
|
||||||
import fetch from 'node-fetch';
|
|
||||||
import { RTCPeerConnection, RTCSessionDescription, RTCIceCandidate } from 'wrtc';
|
|
||||||
|
|
||||||
const client = new Rondevu({
|
|
||||||
baseUrl: 'https://api.ronde.vu',
|
|
||||||
fetch: fetch as any,
|
|
||||||
RTCPeerConnection,
|
|
||||||
RTCSessionDescription,
|
|
||||||
RTCIceCandidate
|
|
||||||
});
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## TypeScript
|
## TypeScript
|
||||||
@@ -622,38 +436,23 @@ All types are exported:
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import type {
|
import type {
|
||||||
// Client types
|
RondevuServiceOptions,
|
||||||
Credentials,
|
ServiceHostOptions,
|
||||||
RondevuOptions,
|
ServiceHostEvents,
|
||||||
|
ServiceClientOptions,
|
||||||
// Username types
|
ServiceClientEvents,
|
||||||
UsernameCheckResult,
|
ConnectionInterface,
|
||||||
UsernameClaimResult,
|
ConnectionEvents,
|
||||||
|
ConnectionStates,
|
||||||
// Durable connection types
|
Message,
|
||||||
DurableConnectionState,
|
QueueMessageOptions,
|
||||||
DurableChannelState,
|
Signaler,
|
||||||
DurableConnectionConfig,
|
PollingConfig,
|
||||||
DurableChannelConfig,
|
Credentials,
|
||||||
DurableServiceConfig,
|
Keypair
|
||||||
QueuedMessage,
|
} from '@xtr-dev/rondevu-client'
|
||||||
DurableConnectionEvents,
|
|
||||||
DurableChannelEvents,
|
|
||||||
DurableServiceEvents,
|
|
||||||
ConnectionInfo,
|
|
||||||
ServiceInfo
|
|
||||||
} from '@xtr-dev/rondevu-client';
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Migration from v0.8.x
|
|
||||||
|
|
||||||
v0.9.0 is a **breaking change** that replaces the low-level APIs with high-level durable connections. See [MIGRATION.md](./MIGRATION.md) for detailed migration guide.
|
|
||||||
|
|
||||||
**Key Changes:**
|
|
||||||
- ❌ Removed: `client.services.*`, `client.discovery.*`, `client.createPeer()` (low-level APIs)
|
|
||||||
- ✅ Added: `client.exposeService()`, `client.connect()`, `client.connectByUuid()` (durable APIs)
|
|
||||||
- ✅ Changed: Focus on durable connections with automatic reconnection and message queuing
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
MIT
|
MIT
|
||||||
|
|||||||
281
USAGE.md
Normal file
281
USAGE.md
Normal file
@@ -0,0 +1,281 @@
|
|||||||
|
# Rondevu Client Usage Guide
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm install @xtr-dev/rondevu-client
|
||||||
|
```
|
||||||
|
|
||||||
|
## Quick Start
|
||||||
|
|
||||||
|
### 1. Register and Create Connection
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { RondevuAPI, RondevuSignaler, WebRTCRondevuConnection } from '@xtr-dev/rondevu-client';
|
||||||
|
|
||||||
|
const API_URL = 'https://api.ronde.vu';
|
||||||
|
|
||||||
|
// Register to get credentials
|
||||||
|
const api = new RondevuAPI(API_URL);
|
||||||
|
const credentials = await api.register();
|
||||||
|
|
||||||
|
// Create authenticated API client
|
||||||
|
const authenticatedApi = new RondevuAPI(API_URL, credentials);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Create an Offer (Offerer Side)
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Create a connection
|
||||||
|
const connection = new WebRTCRondevuConnection(
|
||||||
|
'connection-id',
|
||||||
|
'host-username',
|
||||||
|
'service-name'
|
||||||
|
);
|
||||||
|
|
||||||
|
// Wait for local description
|
||||||
|
await connection.ready;
|
||||||
|
|
||||||
|
// Create offer on server
|
||||||
|
const offers = await authenticatedApi.createOffers([{
|
||||||
|
sdp: connection.connection.localDescription!.sdp!,
|
||||||
|
ttl: 300000 // 5 minutes
|
||||||
|
}]);
|
||||||
|
|
||||||
|
const offerId = offers[0].id;
|
||||||
|
|
||||||
|
// Set up signaler for ICE candidate exchange
|
||||||
|
const signaler = new RondevuSignaler(authenticatedApi, offerId);
|
||||||
|
connection.setSignaler(signaler);
|
||||||
|
|
||||||
|
// Poll for answer
|
||||||
|
const checkAnswer = setInterval(async () => {
|
||||||
|
const answer = await authenticatedApi.getAnswer(offerId);
|
||||||
|
if (answer) {
|
||||||
|
clearInterval(checkAnswer);
|
||||||
|
await connection.connection.setRemoteDescription({
|
||||||
|
type: 'answer',
|
||||||
|
sdp: answer.sdp
|
||||||
|
});
|
||||||
|
console.log('Connection established!');
|
||||||
|
}
|
||||||
|
}, 1000);
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. Answer an Offer (Answerer Side)
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Get the offer
|
||||||
|
const offer = await authenticatedApi.getOffer(offerId);
|
||||||
|
|
||||||
|
// Create connection with remote offer
|
||||||
|
const connection = new WebRTCRondevuConnection(
|
||||||
|
'connection-id',
|
||||||
|
'peer-username',
|
||||||
|
'service-name',
|
||||||
|
{
|
||||||
|
type: 'offer',
|
||||||
|
sdp: offer.sdp
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
// Wait for local description (answer)
|
||||||
|
await connection.ready;
|
||||||
|
|
||||||
|
// Send answer to server
|
||||||
|
await authenticatedApi.answerOffer(
|
||||||
|
offerId,
|
||||||
|
connection.connection.localDescription!.sdp!
|
||||||
|
);
|
||||||
|
|
||||||
|
// Set up signaler for ICE candidate exchange
|
||||||
|
const signaler = new RondevuSignaler(authenticatedApi, offerId);
|
||||||
|
connection.setSignaler(signaler);
|
||||||
|
|
||||||
|
console.log('Connection established!');
|
||||||
|
```
|
||||||
|
|
||||||
|
## Using Services
|
||||||
|
|
||||||
|
### Publish a Service
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { RondevuAPI } from '@xtr-dev/rondevu-client';
|
||||||
|
|
||||||
|
const api = new RondevuAPI(API_URL, credentials);
|
||||||
|
|
||||||
|
const service = await api.publishService({
|
||||||
|
username: 'my-username',
|
||||||
|
serviceFqn: 'chat.app@1.0.0',
|
||||||
|
sdp: localDescription.sdp,
|
||||||
|
ttl: 300000,
|
||||||
|
isPublic: true,
|
||||||
|
metadata: { description: 'My chat service' },
|
||||||
|
signature: '...', // Ed25519 signature
|
||||||
|
message: '...' // Signed message
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('Service UUID:', service.uuid);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Connect to a Service
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Search for services
|
||||||
|
const services = await api.searchServices('username', 'chat.app@1.0.0');
|
||||||
|
|
||||||
|
if (services.length > 0) {
|
||||||
|
// Get service details with offer
|
||||||
|
const service = await api.getService(services[0].uuid);
|
||||||
|
|
||||||
|
// Create connection with service offer
|
||||||
|
const connection = new WebRTCRondevuConnection(
|
||||||
|
service.serviceId,
|
||||||
|
service.username,
|
||||||
|
service.serviceFqn,
|
||||||
|
{
|
||||||
|
type: 'offer',
|
||||||
|
sdp: service.sdp
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
await connection.ready;
|
||||||
|
|
||||||
|
// Answer the service offer
|
||||||
|
await api.answerOffer(
|
||||||
|
service.offerId,
|
||||||
|
connection.connection.localDescription!.sdp!
|
||||||
|
);
|
||||||
|
|
||||||
|
// Set up signaler
|
||||||
|
const signaler = new RondevuSignaler(api, service.offerId);
|
||||||
|
connection.setSignaler(signaler);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Event Handling
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { EventBus } from '@xtr-dev/rondevu-client';
|
||||||
|
|
||||||
|
// Connection events
|
||||||
|
connection.events.on('state-change', (state) => {
|
||||||
|
console.log('Connection state:', state);
|
||||||
|
});
|
||||||
|
|
||||||
|
connection.events.on('message', (message) => {
|
||||||
|
console.log('Received message:', message);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Custom events with EventBus
|
||||||
|
interface MyEvents {
|
||||||
|
'user:connected': { userId: string; timestamp: number };
|
||||||
|
'message:sent': string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const events = new EventBus<MyEvents>();
|
||||||
|
|
||||||
|
events.on('user:connected', (data) => {
|
||||||
|
console.log(`User ${data.userId} connected at ${data.timestamp}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
events.emit('user:connected', {
|
||||||
|
userId: '123',
|
||||||
|
timestamp: Date.now()
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Cleanup
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { createBin } from '@xtr-dev/rondevu-client';
|
||||||
|
|
||||||
|
const bin = createBin();
|
||||||
|
|
||||||
|
// Add cleanup functions
|
||||||
|
bin(
|
||||||
|
() => console.log('Cleanup 1'),
|
||||||
|
() => console.log('Cleanup 2')
|
||||||
|
);
|
||||||
|
|
||||||
|
// Clean all
|
||||||
|
bin.clean();
|
||||||
|
```
|
||||||
|
|
||||||
|
## API Reference
|
||||||
|
|
||||||
|
### RondevuAPI
|
||||||
|
|
||||||
|
Complete API client for Rondevu signaling server.
|
||||||
|
|
||||||
|
**Methods:**
|
||||||
|
- `register()` - Register new peer
|
||||||
|
- `createOffers(offers)` - Create offers
|
||||||
|
- `getOffer(offerId)` - Get offer by ID
|
||||||
|
- `answerOffer(offerId, sdp)` - Answer an offer
|
||||||
|
- `getAnswer(offerId)` - Poll for answer
|
||||||
|
- `searchOffers(topic)` - Search by topic
|
||||||
|
- `addIceCandidates(offerId, candidates)` - Add ICE candidates
|
||||||
|
- `getIceCandidates(offerId, since)` - Get ICE candidates (polling)
|
||||||
|
- `publishService(service)` - Publish service
|
||||||
|
- `getService(uuid)` - Get service by UUID
|
||||||
|
- `searchServices(username, serviceFqn)` - Search services
|
||||||
|
- `checkUsername(username)` - Check availability
|
||||||
|
- `claimUsername(username, publicKey, signature, message)` - Claim username
|
||||||
|
|
||||||
|
### RondevuSignaler
|
||||||
|
|
||||||
|
Handles ICE candidate exchange via polling.
|
||||||
|
|
||||||
|
**Constructor:**
|
||||||
|
```typescript
|
||||||
|
new RondevuSignaler(api: RondevuAPI, offerId: string)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Methods:**
|
||||||
|
- `addIceCandidate(candidate)` - Send local candidate
|
||||||
|
- `addListener(callback)` - Poll for remote candidates (returns cleanup function)
|
||||||
|
|
||||||
|
### WebRTCRondevuConnection
|
||||||
|
|
||||||
|
WebRTC connection wrapper with type-safe events.
|
||||||
|
|
||||||
|
**Constructor:**
|
||||||
|
```typescript
|
||||||
|
new WebRTCRondevuConnection(
|
||||||
|
id: string,
|
||||||
|
host: string,
|
||||||
|
service: string,
|
||||||
|
offer?: RTCSessionDescriptionInit
|
||||||
|
)
|
||||||
|
```
|
||||||
|
|
||||||
|
**Properties:**
|
||||||
|
- `id` - Connection ID
|
||||||
|
- `host` - Host username
|
||||||
|
- `service` - Service FQN
|
||||||
|
- `state` - Connection state
|
||||||
|
- `events` - EventBus for state changes and messages
|
||||||
|
- `ready` - Promise that resolves when local description is set
|
||||||
|
|
||||||
|
**Methods:**
|
||||||
|
- `setSignaler(signaler)` - Set signaler for ICE exchange
|
||||||
|
- `queueMessage(message, options)` - Queue message for sending
|
||||||
|
- `sendMessage(message)` - Send message immediately
|
||||||
|
|
||||||
|
### EventBus<TEvents>
|
||||||
|
|
||||||
|
Type-safe event emitter with inferred types.
|
||||||
|
|
||||||
|
**Methods:**
|
||||||
|
- `on(event, handler)` - Subscribe
|
||||||
|
- `once(event, handler)` - Subscribe once
|
||||||
|
- `off(event, handler)` - Unsubscribe
|
||||||
|
- `emit(event, data)` - Emit event
|
||||||
|
- `clear(event?)` - Clear handlers
|
||||||
|
- `listenerCount(event)` - Get listener count
|
||||||
|
- `eventNames()` - Get event names
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
See the demo application at https://github.com/xtr-dev/rondevu-demo for a complete working example.
|
||||||
52
eslint.config.js
Normal file
52
eslint.config.js
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
import js from '@eslint/js'
|
||||||
|
import tsPlugin from '@typescript-eslint/eslint-plugin'
|
||||||
|
import tsParser from '@typescript-eslint/parser'
|
||||||
|
import prettierConfig from 'eslint-config-prettier'
|
||||||
|
import prettierPlugin from 'eslint-plugin-prettier'
|
||||||
|
import unicorn from 'eslint-plugin-unicorn'
|
||||||
|
import globals from 'globals'
|
||||||
|
|
||||||
|
export default [
|
||||||
|
js.configs.recommended,
|
||||||
|
{
|
||||||
|
files: ['**/*.ts', '**/*.tsx', '**/*.js'],
|
||||||
|
languageOptions: {
|
||||||
|
parser: tsParser,
|
||||||
|
parserOptions: {
|
||||||
|
ecmaVersion: 'latest',
|
||||||
|
sourceType: 'module',
|
||||||
|
},
|
||||||
|
globals: {
|
||||||
|
...globals.browser,
|
||||||
|
...globals.node,
|
||||||
|
RTCPeerConnection: 'readonly',
|
||||||
|
RTCIceCandidate: 'readonly',
|
||||||
|
RTCSessionDescriptionInit: 'readonly',
|
||||||
|
RTCIceCandidateInit: 'readonly',
|
||||||
|
BufferSource: 'readonly',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
plugins: {
|
||||||
|
'@typescript-eslint': tsPlugin,
|
||||||
|
prettier: prettierPlugin,
|
||||||
|
unicorn: unicorn,
|
||||||
|
},
|
||||||
|
rules: {
|
||||||
|
...tsPlugin.configs.recommended.rules,
|
||||||
|
...prettierConfig.rules,
|
||||||
|
'prettier/prettier': 'error',
|
||||||
|
'@typescript-eslint/no-explicit-any': 'off',
|
||||||
|
'@typescript-eslint/no-unused-vars': ['warn', { argsIgnorePattern: '^_' }],
|
||||||
|
'unicorn/filename-case': [
|
||||||
|
'error',
|
||||||
|
{
|
||||||
|
case: 'kebabCase',
|
||||||
|
ignore: ['^README\\.md$'],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ignores: ['dist/**', 'node_modules/**', '*.config.js'],
|
||||||
|
},
|
||||||
|
]
|
||||||
2964
package-lock.json
generated
2964
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
18
package.json
18
package.json
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@xtr-dev/rondevu-client",
|
"name": "@xtr-dev/rondevu-client",
|
||||||
"version": "0.9.2",
|
"version": "0.13.0",
|
||||||
"description": "TypeScript client for Rondevu with durable WebRTC connections, automatic reconnection, and message queuing",
|
"description": "TypeScript client for Rondevu with durable WebRTC connections, automatic reconnection, and message queuing",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@@ -8,6 +8,10 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
"typecheck": "tsc --noEmit",
|
"typecheck": "tsc --noEmit",
|
||||||
|
"dev": "vite",
|
||||||
|
"lint": "eslint src demo --ext .ts,.tsx,.js",
|
||||||
|
"lint:fix": "eslint src demo --ext .ts,.tsx,.js --fix",
|
||||||
|
"format": "prettier --write \"src/**/*.{ts,tsx,js}\" \"demo/**/*.{ts,tsx,js,html}\"",
|
||||||
"prepublishOnly": "npm run build"
|
"prepublishOnly": "npm run build"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
@@ -20,7 +24,17 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"typescript": "^5.9.3"
|
"@eslint/js": "^9.39.1",
|
||||||
|
"@typescript-eslint/eslint-plugin": "^8.48.1",
|
||||||
|
"@typescript-eslint/parser": "^8.48.1",
|
||||||
|
"eslint": "^9.39.1",
|
||||||
|
"eslint-config-prettier": "^10.1.8",
|
||||||
|
"eslint-plugin-prettier": "^5.5.4",
|
||||||
|
"eslint-plugin-unicorn": "^62.0.0",
|
||||||
|
"globals": "^16.5.0",
|
||||||
|
"prettier": "^3.7.4",
|
||||||
|
"typescript": "^5.9.3",
|
||||||
|
"vite": "^7.2.6"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
"dist",
|
"dist",
|
||||||
|
|||||||
458
src/api.ts
Normal file
458
src/api.ts
Normal file
@@ -0,0 +1,458 @@
|
|||||||
|
/**
|
||||||
|
* Rondevu API Client - Single class for all API endpoints
|
||||||
|
*/
|
||||||
|
|
||||||
|
import * as ed25519 from '@noble/ed25519'
|
||||||
|
|
||||||
|
// Set SHA-512 hash function for ed25519 (required in @noble/ed25519 v3+)
|
||||||
|
ed25519.hashes.sha512Async = async (message: Uint8Array) => {
|
||||||
|
return new Uint8Array(await crypto.subtle.digest('SHA-512', message as BufferSource))
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Credentials {
|
||||||
|
peerId: string
|
||||||
|
secret: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Keypair {
|
||||||
|
publicKey: string
|
||||||
|
privateKey: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface OfferRequest {
|
||||||
|
sdp: string
|
||||||
|
topics?: string[]
|
||||||
|
ttl?: number
|
||||||
|
secret?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Offer {
|
||||||
|
id: string
|
||||||
|
peerId: string
|
||||||
|
sdp: string
|
||||||
|
topics: string[]
|
||||||
|
ttl: number
|
||||||
|
createdAt: number
|
||||||
|
expiresAt: number
|
||||||
|
answererPeerId?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface OfferRequest {
|
||||||
|
sdp: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ServiceRequest {
|
||||||
|
serviceFqn: string // Must include username: service:version@username
|
||||||
|
offers: OfferRequest[]
|
||||||
|
ttl?: number
|
||||||
|
signature: string
|
||||||
|
message: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ServiceOffer {
|
||||||
|
offerId: string
|
||||||
|
sdp: string
|
||||||
|
createdAt: number
|
||||||
|
expiresAt: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Service {
|
||||||
|
serviceId: string
|
||||||
|
offers: ServiceOffer[]
|
||||||
|
username: string
|
||||||
|
serviceFqn: string
|
||||||
|
createdAt: number
|
||||||
|
expiresAt: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IceCandidate {
|
||||||
|
candidate: RTCIceCandidateInit
|
||||||
|
createdAt: number
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper: Convert Uint8Array to base64 string
|
||||||
|
*/
|
||||||
|
function bytesToBase64(bytes: Uint8Array): string {
|
||||||
|
const binString = Array.from(bytes, byte => String.fromCodePoint(byte)).join('')
|
||||||
|
return btoa(binString)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper: Convert base64 string to Uint8Array
|
||||||
|
*/
|
||||||
|
function base64ToBytes(base64: string): Uint8Array {
|
||||||
|
const binString = atob(base64)
|
||||||
|
return Uint8Array.from(binString, char => char.codePointAt(0)!)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RondevuAPI - Complete API client for Rondevu signaling server
|
||||||
|
*/
|
||||||
|
export class RondevuAPI {
|
||||||
|
constructor(
|
||||||
|
private baseUrl: string,
|
||||||
|
private credentials?: Credentials
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set credentials for authentication
|
||||||
|
*/
|
||||||
|
setCredentials(credentials: Credentials): void {
|
||||||
|
this.credentials = credentials
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Authentication header
|
||||||
|
*/
|
||||||
|
private getAuthHeader(): Record<string, string> {
|
||||||
|
if (!this.credentials) {
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
Authorization: `Bearer ${this.credentials.peerId}:${this.credentials.secret}`,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// Ed25519 Cryptography Helpers
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate an Ed25519 keypair for username claiming and service publishing
|
||||||
|
*/
|
||||||
|
static async generateKeypair(): Promise<Keypair> {
|
||||||
|
const privateKey = ed25519.utils.randomSecretKey()
|
||||||
|
const publicKey = await ed25519.getPublicKeyAsync(privateKey)
|
||||||
|
|
||||||
|
return {
|
||||||
|
publicKey: bytesToBase64(publicKey),
|
||||||
|
privateKey: bytesToBase64(privateKey),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sign a message with an Ed25519 private key
|
||||||
|
*/
|
||||||
|
static 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify a signature
|
||||||
|
*/
|
||||||
|
static async verifySignature(
|
||||||
|
message: string,
|
||||||
|
signatureBase64: string,
|
||||||
|
publicKeyBase64: string
|
||||||
|
): Promise<boolean> {
|
||||||
|
const publicKey = base64ToBytes(publicKeyBase64)
|
||||||
|
const signature = base64ToBytes(signatureBase64)
|
||||||
|
const encoder = new TextEncoder()
|
||||||
|
const messageBytes = encoder.encode(message)
|
||||||
|
|
||||||
|
return await ed25519.verifyAsync(signature, messageBytes, publicKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// Authentication
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a new peer and get credentials
|
||||||
|
*/
|
||||||
|
async register(): Promise<Credentials> {
|
||||||
|
const response = await fetch(`${this.baseUrl}/register`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }))
|
||||||
|
throw new Error(`Registration failed: ${error.error || response.statusText}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// Offers
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create one or more offers
|
||||||
|
*/
|
||||||
|
async createOffers(offers: OfferRequest[]): Promise<Offer[]> {
|
||||||
|
const response = await fetch(`${this.baseUrl}/offers`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...this.getAuthHeader(),
|
||||||
|
},
|
||||||
|
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}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get offer by ID
|
||||||
|
*/
|
||||||
|
async getOffer(offerId: string): Promise<Offer> {
|
||||||
|
const response = await fetch(`${this.baseUrl}/offers/${offerId}`, {
|
||||||
|
headers: this.getAuthHeader(),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }))
|
||||||
|
throw new Error(`Failed to get offer: ${error.error || response.statusText}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Answer a specific offer from a service
|
||||||
|
*/
|
||||||
|
async postOfferAnswer(serviceFqn: string, offerId: string, sdp: string): Promise<{ success: boolean; offerId: string }> {
|
||||||
|
const response = await fetch(`${this.baseUrl}/services/${encodeURIComponent(serviceFqn)}/offers/${offerId}/answer`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...this.getAuthHeader(),
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ sdp }),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }))
|
||||||
|
throw new Error(`Failed to answer offer: ${error.error || response.statusText}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get answer for a specific offer (offerer polls this)
|
||||||
|
*/
|
||||||
|
async getOfferAnswer(serviceFqn: string, offerId: string): Promise<{ sdp: string; offerId: string; answererId: string; answeredAt: number } | null> {
|
||||||
|
const response = await fetch(`${this.baseUrl}/services/${encodeURIComponent(serviceFqn)}/offers/${offerId}/answer`, {
|
||||||
|
headers: this.getAuthHeader(),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
// 404 means not yet answered
|
||||||
|
if (response.status === 404) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }))
|
||||||
|
throw new Error(`Failed to get answer: ${error.error || response.statusText}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search offers by topic
|
||||||
|
*/
|
||||||
|
async searchOffers(topic: string): Promise<Offer[]> {
|
||||||
|
const response = await fetch(`${this.baseUrl}/offers?topic=${encodeURIComponent(topic)}`, {
|
||||||
|
headers: this.getAuthHeader(),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }))
|
||||||
|
throw new Error(`Failed to search offers: ${error.error || response.statusText}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// ICE Candidates
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add ICE candidates to a specific offer
|
||||||
|
*/
|
||||||
|
async addOfferIceCandidates(serviceFqn: string, offerId: string, candidates: RTCIceCandidateInit[]): Promise<{ count: number; offerId: string }> {
|
||||||
|
const response = await fetch(`${this.baseUrl}/services/${encodeURIComponent(serviceFqn)}/offers/${offerId}/ice-candidates`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...this.getAuthHeader(),
|
||||||
|
},
|
||||||
|
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}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get ICE candidates for a specific offer (with polling support)
|
||||||
|
*/
|
||||||
|
async getOfferIceCandidates(serviceFqn: string, offerId: string, since: number = 0): Promise<{ candidates: IceCandidate[]; offerId: string }> {
|
||||||
|
const url = new URL(`${this.baseUrl}/services/${encodeURIComponent(serviceFqn)}/offers/${offerId}/ice-candidates`)
|
||||||
|
url.searchParams.set('since', since.toString())
|
||||||
|
|
||||||
|
const response = await fetch(url.toString(), { headers: this.getAuthHeader() })
|
||||||
|
|
||||||
|
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 {
|
||||||
|
candidates: data.candidates || [],
|
||||||
|
offerId: data.offerId
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// Services
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Publish a service
|
||||||
|
* Service FQN must include username: service:version@username
|
||||||
|
*/
|
||||||
|
async publishService(service: ServiceRequest): Promise<Service> {
|
||||||
|
const response = await fetch(`${this.baseUrl}/services`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...this.getAuthHeader(),
|
||||||
|
},
|
||||||
|
body: JSON.stringify(service),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }))
|
||||||
|
throw new Error(`Failed to publish service: ${error.error || response.statusText}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get service by FQN (with username) - Direct lookup
|
||||||
|
* Example: chat:1.0.0@alice
|
||||||
|
*/
|
||||||
|
async getService(serviceFqn: string): Promise<{ serviceId: string; username: string; serviceFqn: string; offerId: string; sdp: string; createdAt: number; expiresAt: number }> {
|
||||||
|
const response = await fetch(`${this.baseUrl}/services/${encodeURIComponent(serviceFqn)}`, {
|
||||||
|
headers: this.getAuthHeader(),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }))
|
||||||
|
throw new Error(`Failed to get service: ${error.error || response.statusText}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Discover a random available service without knowing the username
|
||||||
|
* Example: chat:1.0.0 (without @username)
|
||||||
|
*/
|
||||||
|
async discoverService(serviceVersion: string): Promise<{ serviceId: string; username: string; serviceFqn: string; offerId: string; sdp: string; createdAt: number; expiresAt: number }> {
|
||||||
|
const response = await fetch(`${this.baseUrl}/services/${encodeURIComponent(serviceVersion)}`, {
|
||||||
|
headers: this.getAuthHeader(),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }))
|
||||||
|
throw new Error(`Failed to discover service: ${error.error || response.statusText}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Discover multiple available services with pagination
|
||||||
|
* Example: chat:1.0.0 (without @username)
|
||||||
|
*/
|
||||||
|
async discoverServices(serviceVersion: string, limit: number = 10, offset: number = 0): Promise<{ services: Array<{ serviceId: string; username: string; serviceFqn: string; offerId: string; sdp: string; createdAt: number; expiresAt: number }>; count: number; limit: number; offset: number }> {
|
||||||
|
const url = new URL(`${this.baseUrl}/services/${encodeURIComponent(serviceVersion)}`)
|
||||||
|
url.searchParams.set('limit', limit.toString())
|
||||||
|
url.searchParams.set('offset', offset.toString())
|
||||||
|
|
||||||
|
const response = await fetch(url.toString(), {
|
||||||
|
headers: this.getAuthHeader(),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }))
|
||||||
|
throw new Error(`Failed to discover services: ${error.error || response.statusText}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// Usernames
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if username is available
|
||||||
|
*/
|
||||||
|
async checkUsername(username: string): Promise<{ available: boolean; publicKey?: string; claimedAt?: number; expiresAt?: number }> {
|
||||||
|
const response = await fetch(
|
||||||
|
`${this.baseUrl}/users/${encodeURIComponent(username)}`
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }))
|
||||||
|
throw new Error(`Failed to check username: ${error.error || response.statusText}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Claim a username (requires Ed25519 signature)
|
||||||
|
*/
|
||||||
|
async claimUsername(
|
||||||
|
username: string,
|
||||||
|
publicKey: string,
|
||||||
|
signature: string,
|
||||||
|
message: string
|
||||||
|
): Promise<{ success: boolean; username: string }> {
|
||||||
|
const response = await fetch(`${this.baseUrl}/users/${encodeURIComponent(username)}`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...this.getAuthHeader(),
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
publicKey,
|
||||||
|
signature,
|
||||||
|
message,
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const error = await response.json().catch(() => ({ error: 'Unknown error' }))
|
||||||
|
throw new Error(`Failed to claim username: ${error.error || response.statusText}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json()
|
||||||
|
}
|
||||||
|
}
|
||||||
62
src/auth.ts
62
src/auth.ts
@@ -1,62 +0,0 @@
|
|||||||
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}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,361 +0,0 @@
|
|||||||
/**
|
|
||||||
* DurableChannel - Message queueing wrapper for RTCDataChannel
|
|
||||||
*
|
|
||||||
* Provides automatic message queuing during disconnections and transparent
|
|
||||||
* flushing when the connection is re-established.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { EventEmitter } from '../event-emitter.js';
|
|
||||||
import {
|
|
||||||
DurableChannelState
|
|
||||||
} from './types.js';
|
|
||||||
import type {
|
|
||||||
DurableChannelConfig,
|
|
||||||
DurableChannelEvents,
|
|
||||||
QueuedMessage
|
|
||||||
} from './types.js';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default configuration for durable channels
|
|
||||||
*/
|
|
||||||
const DEFAULT_CONFIG = {
|
|
||||||
maxQueueSize: 1000,
|
|
||||||
maxMessageAge: 60000, // 1 minute
|
|
||||||
ordered: true,
|
|
||||||
maxRetransmits: undefined
|
|
||||||
} as const;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Durable channel that survives WebRTC peer connection drops
|
|
||||||
*
|
|
||||||
* The DurableChannel wraps an RTCDataChannel and provides:
|
|
||||||
* - Automatic message queuing during disconnections
|
|
||||||
* - Queue flushing on reconnection
|
|
||||||
* - Configurable queue size and message age limits
|
|
||||||
* - RTCDataChannel-compatible API
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* ```typescript
|
|
||||||
* const channel = new DurableChannel('chat', connection, {
|
|
||||||
* maxQueueSize: 500,
|
|
||||||
* maxMessageAge: 30000
|
|
||||||
* });
|
|
||||||
*
|
|
||||||
* channel.on('message', (data) => {
|
|
||||||
* console.log('Received:', data);
|
|
||||||
* });
|
|
||||||
*
|
|
||||||
* channel.on('open', () => {
|
|
||||||
* channel.send('Hello!');
|
|
||||||
* });
|
|
||||||
*
|
|
||||||
* // Messages sent during disconnection are automatically queued
|
|
||||||
* channel.send('This will be queued if disconnected');
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
export class DurableChannel extends EventEmitter<DurableChannelEvents> {
|
|
||||||
readonly label: string;
|
|
||||||
readonly config: DurableChannelConfig;
|
|
||||||
|
|
||||||
private _state: DurableChannelState;
|
|
||||||
private underlyingChannel?: RTCDataChannel;
|
|
||||||
private messageQueue: QueuedMessage[] = [];
|
|
||||||
private queueProcessing: boolean = false;
|
|
||||||
private _bufferedAmountLowThreshold: number = 0;
|
|
||||||
|
|
||||||
// Event handlers that need cleanup
|
|
||||||
private openHandler?: () => void;
|
|
||||||
private messageHandler?: (event: MessageEvent) => void;
|
|
||||||
private errorHandler?: (event: Event) => void;
|
|
||||||
private closeHandler?: () => void;
|
|
||||||
private bufferedAmountLowHandler?: () => void;
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
label: string,
|
|
||||||
config?: DurableChannelConfig
|
|
||||||
) {
|
|
||||||
super();
|
|
||||||
this.label = label;
|
|
||||||
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
||||||
this._state = DurableChannelState.CONNECTING;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Current channel state
|
|
||||||
*/
|
|
||||||
get readyState(): DurableChannelState {
|
|
||||||
return this._state;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Buffered amount from underlying channel (0 if no channel)
|
|
||||||
*/
|
|
||||||
get bufferedAmount(): number {
|
|
||||||
return this.underlyingChannel?.bufferedAmount ?? 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Buffered amount low threshold
|
|
||||||
*/
|
|
||||||
get bufferedAmountLowThreshold(): number {
|
|
||||||
return this._bufferedAmountLowThreshold;
|
|
||||||
}
|
|
||||||
|
|
||||||
set bufferedAmountLowThreshold(value: number) {
|
|
||||||
this._bufferedAmountLowThreshold = value;
|
|
||||||
if (this.underlyingChannel) {
|
|
||||||
this.underlyingChannel.bufferedAmountLowThreshold = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Send data through the channel
|
|
||||||
*
|
|
||||||
* If the channel is open, sends immediately. Otherwise, queues the message
|
|
||||||
* for delivery when the channel reconnects.
|
|
||||||
*
|
|
||||||
* @param data - Data to send
|
|
||||||
*/
|
|
||||||
send(data: string | Blob | ArrayBuffer | ArrayBufferView): void {
|
|
||||||
if (this._state === DurableChannelState.OPEN && this.underlyingChannel) {
|
|
||||||
// Channel is open - send immediately
|
|
||||||
try {
|
|
||||||
this.underlyingChannel.send(data as any);
|
|
||||||
} catch (error) {
|
|
||||||
// Send failed - queue the message
|
|
||||||
this.enqueueMessage(data);
|
|
||||||
this.emit('error', error as Error);
|
|
||||||
}
|
|
||||||
} else if (this._state !== DurableChannelState.CLOSED) {
|
|
||||||
// Channel is not open but not closed - queue the message
|
|
||||||
this.enqueueMessage(data);
|
|
||||||
} else {
|
|
||||||
// Channel is closed - throw error
|
|
||||||
throw new Error('Cannot send on closed channel');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Close the channel
|
|
||||||
*/
|
|
||||||
close(): void {
|
|
||||||
if (this._state === DurableChannelState.CLOSED ||
|
|
||||||
this._state === DurableChannelState.CLOSING) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this._state = DurableChannelState.CLOSING;
|
|
||||||
|
|
||||||
if (this.underlyingChannel) {
|
|
||||||
this.underlyingChannel.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
this._state = DurableChannelState.CLOSED;
|
|
||||||
this.emit('close');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Attach to an underlying RTCDataChannel
|
|
||||||
*
|
|
||||||
* This is called when a WebRTC connection is established (or re-established).
|
|
||||||
* The channel will flush any queued messages and forward events.
|
|
||||||
*
|
|
||||||
* @param channel - RTCDataChannel to attach to
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
attachToChannel(channel: RTCDataChannel): void {
|
|
||||||
// Detach from any existing channel first
|
|
||||||
this.detachFromChannel();
|
|
||||||
|
|
||||||
this.underlyingChannel = channel;
|
|
||||||
|
|
||||||
// Set buffered amount low threshold
|
|
||||||
channel.bufferedAmountLowThreshold = this._bufferedAmountLowThreshold;
|
|
||||||
|
|
||||||
// Setup event handlers
|
|
||||||
this.openHandler = () => {
|
|
||||||
this._state = DurableChannelState.OPEN;
|
|
||||||
this.emit('open');
|
|
||||||
|
|
||||||
// Flush queued messages
|
|
||||||
this.flushQueue().catch(error => {
|
|
||||||
this.emit('error', error);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
this.messageHandler = (event: MessageEvent) => {
|
|
||||||
this.emit('message', event.data);
|
|
||||||
};
|
|
||||||
|
|
||||||
this.errorHandler = (event: Event) => {
|
|
||||||
this.emit('error', new Error(`Channel error: ${event.type}`));
|
|
||||||
};
|
|
||||||
|
|
||||||
this.closeHandler = () => {
|
|
||||||
if (this._state !== DurableChannelState.CLOSING &&
|
|
||||||
this._state !== DurableChannelState.CLOSED) {
|
|
||||||
// Unexpected close - transition to connecting (will reconnect)
|
|
||||||
this._state = DurableChannelState.CONNECTING;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
this.bufferedAmountLowHandler = () => {
|
|
||||||
this.emit('bufferedAmountLow');
|
|
||||||
};
|
|
||||||
|
|
||||||
// Attach handlers
|
|
||||||
channel.addEventListener('open', this.openHandler);
|
|
||||||
channel.addEventListener('message', this.messageHandler);
|
|
||||||
channel.addEventListener('error', this.errorHandler);
|
|
||||||
channel.addEventListener('close', this.closeHandler);
|
|
||||||
channel.addEventListener('bufferedamountlow', this.bufferedAmountLowHandler);
|
|
||||||
|
|
||||||
// If channel is already open, trigger open event
|
|
||||||
if (channel.readyState === 'open') {
|
|
||||||
this.openHandler();
|
|
||||||
} else if (channel.readyState === 'connecting') {
|
|
||||||
this._state = DurableChannelState.CONNECTING;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Detach from the underlying RTCDataChannel
|
|
||||||
*
|
|
||||||
* This is called when a WebRTC connection drops. The channel remains alive
|
|
||||||
* and continues queuing messages.
|
|
||||||
*
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
detachFromChannel(): void {
|
|
||||||
if (!this.underlyingChannel) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove event listeners
|
|
||||||
if (this.openHandler) {
|
|
||||||
this.underlyingChannel.removeEventListener('open', this.openHandler);
|
|
||||||
}
|
|
||||||
if (this.messageHandler) {
|
|
||||||
this.underlyingChannel.removeEventListener('message', this.messageHandler);
|
|
||||||
}
|
|
||||||
if (this.errorHandler) {
|
|
||||||
this.underlyingChannel.removeEventListener('error', this.errorHandler);
|
|
||||||
}
|
|
||||||
if (this.closeHandler) {
|
|
||||||
this.underlyingChannel.removeEventListener('close', this.closeHandler);
|
|
||||||
}
|
|
||||||
if (this.bufferedAmountLowHandler) {
|
|
||||||
this.underlyingChannel.removeEventListener('bufferedamountlow', this.bufferedAmountLowHandler);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.underlyingChannel = undefined;
|
|
||||||
this._state = DurableChannelState.CONNECTING;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Enqueue a message for later delivery
|
|
||||||
*/
|
|
||||||
private enqueueMessage(data: string | Blob | ArrayBuffer | ArrayBufferView): void {
|
|
||||||
// Prune old messages first
|
|
||||||
this.pruneOldMessages();
|
|
||||||
|
|
||||||
const message: QueuedMessage = {
|
|
||||||
data,
|
|
||||||
enqueuedAt: Date.now(),
|
|
||||||
id: `msg-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`
|
|
||||||
};
|
|
||||||
|
|
||||||
this.messageQueue.push(message);
|
|
||||||
|
|
||||||
// Handle overflow
|
|
||||||
const maxQueueSize = this.config.maxQueueSize ?? 1000;
|
|
||||||
if (this.messageQueue.length > maxQueueSize) {
|
|
||||||
const excess = this.messageQueue.length - maxQueueSize;
|
|
||||||
this.messageQueue.splice(0, excess);
|
|
||||||
this.emit('queueOverflow', excess);
|
|
||||||
console.warn(
|
|
||||||
`DurableChannel[${this.label}]: Dropped ${excess} messages due to queue overflow`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Flush all queued messages through the channel
|
|
||||||
*/
|
|
||||||
private async flushQueue(): Promise<void> {
|
|
||||||
if (this.queueProcessing || !this.underlyingChannel ||
|
|
||||||
this.underlyingChannel.readyState !== 'open') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.queueProcessing = true;
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Prune old messages before flushing
|
|
||||||
this.pruneOldMessages();
|
|
||||||
|
|
||||||
// Send all queued messages
|
|
||||||
while (this.messageQueue.length > 0) {
|
|
||||||
const message = this.messageQueue.shift();
|
|
||||||
if (!message) break;
|
|
||||||
|
|
||||||
try {
|
|
||||||
this.underlyingChannel.send(message.data as any);
|
|
||||||
} catch (error) {
|
|
||||||
// Send failed - re-queue message
|
|
||||||
this.messageQueue.unshift(message);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
|
|
||||||
// If buffer is getting full, wait for it to drain
|
|
||||||
if (this.underlyingChannel.bufferedAmount > 16 * 1024 * 1024) { // 16MB
|
|
||||||
await new Promise<void>((resolve) => {
|
|
||||||
const checkBuffer = () => {
|
|
||||||
if (!this.underlyingChannel ||
|
|
||||||
this.underlyingChannel.bufferedAmount < 8 * 1024 * 1024) {
|
|
||||||
resolve();
|
|
||||||
} else {
|
|
||||||
setTimeout(checkBuffer, 100);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
checkBuffer();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
this.queueProcessing = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove messages older than maxMessageAge from the queue
|
|
||||||
*/
|
|
||||||
private pruneOldMessages(): void {
|
|
||||||
const maxMessageAge = this.config.maxMessageAge ?? 60000;
|
|
||||||
if (maxMessageAge === Infinity || maxMessageAge <= 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const now = Date.now();
|
|
||||||
const cutoff = now - maxMessageAge;
|
|
||||||
|
|
||||||
const originalLength = this.messageQueue.length;
|
|
||||||
this.messageQueue = this.messageQueue.filter(msg => msg.enqueuedAt >= cutoff);
|
|
||||||
|
|
||||||
const pruned = originalLength - this.messageQueue.length;
|
|
||||||
if (pruned > 0) {
|
|
||||||
console.warn(
|
|
||||||
`DurableChannel[${this.label}]: Pruned ${pruned} old messages (older than ${maxMessageAge}ms)`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the current queue size
|
|
||||||
*
|
|
||||||
* @internal
|
|
||||||
*/
|
|
||||||
getQueueSize(): number {
|
|
||||||
return this.messageQueue.length;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,453 +0,0 @@
|
|||||||
/**
|
|
||||||
* DurableConnection - WebRTC connection with automatic reconnection
|
|
||||||
*
|
|
||||||
* Manages the WebRTC peer lifecycle and automatically reconnects on
|
|
||||||
* connection drops with exponential backoff.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { EventEmitter } from '../event-emitter.js';
|
|
||||||
import RondevuPeer from '../peer/index.js';
|
|
||||||
import type { RondevuOffers } from '../offers.js';
|
|
||||||
import { DurableChannel } from './channel.js';
|
|
||||||
import { createReconnectionScheduler, type ReconnectionScheduler } from './reconnection.js';
|
|
||||||
import {
|
|
||||||
DurableConnectionState
|
|
||||||
} from './types.js';
|
|
||||||
import type {
|
|
||||||
DurableConnectionConfig,
|
|
||||||
DurableConnectionEvents,
|
|
||||||
ConnectionInfo
|
|
||||||
} from './types.js';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default configuration for durable connections
|
|
||||||
*/
|
|
||||||
const DEFAULT_CONFIG: Required<DurableConnectionConfig> = {
|
|
||||||
maxReconnectAttempts: 10,
|
|
||||||
reconnectBackoffBase: 1000,
|
|
||||||
reconnectBackoffMax: 30000,
|
|
||||||
reconnectJitter: 0.2,
|
|
||||||
connectionTimeout: 30000,
|
|
||||||
maxQueueSize: 1000,
|
|
||||||
maxMessageAge: 60000,
|
|
||||||
rtcConfig: {
|
|
||||||
iceServers: [
|
|
||||||
{ urls: 'stun:stun.l.google.com:19302' },
|
|
||||||
{ urls: 'stun:stun1.l.google.com:19302' }
|
|
||||||
]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Durable WebRTC connection that automatically reconnects
|
|
||||||
*
|
|
||||||
* The DurableConnection manages the lifecycle of a WebRTC peer connection
|
|
||||||
* and provides:
|
|
||||||
* - Automatic reconnection with exponential backoff
|
|
||||||
* - Multiple durable channels that survive reconnections
|
|
||||||
* - Configurable retry limits and timeouts
|
|
||||||
* - High-level connection state events
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* ```typescript
|
|
||||||
* const connection = new DurableConnection(
|
|
||||||
* offersApi,
|
|
||||||
* { username: 'alice', serviceFqn: 'chat@1.0.0' },
|
|
||||||
* { maxReconnectAttempts: 5 }
|
|
||||||
* );
|
|
||||||
*
|
|
||||||
* connection.on('connected', () => {
|
|
||||||
* console.log('Connected!');
|
|
||||||
* });
|
|
||||||
*
|
|
||||||
* connection.on('reconnecting', (attempt, max, delay) => {
|
|
||||||
* console.log(`Reconnecting... (${attempt}/${max}, retry in ${delay}ms)`);
|
|
||||||
* });
|
|
||||||
*
|
|
||||||
* const channel = connection.createChannel('chat');
|
|
||||||
* channel.on('message', (data) => {
|
|
||||||
* console.log('Received:', data);
|
|
||||||
* });
|
|
||||||
*
|
|
||||||
* await connection.connect();
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
export class DurableConnection extends EventEmitter<DurableConnectionEvents> {
|
|
||||||
readonly connectionId: string;
|
|
||||||
readonly config: Required<DurableConnectionConfig>;
|
|
||||||
readonly connectionInfo: ConnectionInfo;
|
|
||||||
|
|
||||||
private _state: DurableConnectionState;
|
|
||||||
private currentPeer?: RondevuPeer;
|
|
||||||
private channels: Map<string, DurableChannel> = new Map();
|
|
||||||
private reconnectionScheduler?: ReconnectionScheduler;
|
|
||||||
|
|
||||||
// Track peer event handlers for cleanup
|
|
||||||
private peerConnectedHandler?: () => void;
|
|
||||||
private peerDisconnectedHandler?: () => void;
|
|
||||||
private peerFailedHandler?: (error: Error) => void;
|
|
||||||
private peerDataChannelHandler?: (channel: RTCDataChannel) => void;
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
private offersApi: RondevuOffers,
|
|
||||||
connectionInfo: ConnectionInfo,
|
|
||||||
config?: DurableConnectionConfig
|
|
||||||
) {
|
|
||||||
super();
|
|
||||||
this.connectionId = `conn-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
||||||
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
||||||
this.connectionInfo = connectionInfo;
|
|
||||||
this._state = DurableConnectionState.CONNECTING;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Current connection state
|
|
||||||
*/
|
|
||||||
getState(): DurableConnectionState {
|
|
||||||
return this._state;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Check if connection is currently connected
|
|
||||||
*/
|
|
||||||
isConnected(): boolean {
|
|
||||||
return this._state === DurableConnectionState.CONNECTED;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a durable channel on this connection
|
|
||||||
*
|
|
||||||
* The channel will be created on the current peer connection if available,
|
|
||||||
* otherwise it will be created when the connection is established.
|
|
||||||
*
|
|
||||||
* @param label - Channel label
|
|
||||||
* @param options - RTCDataChannel init options
|
|
||||||
* @returns DurableChannel instance
|
|
||||||
*/
|
|
||||||
createChannel(label: string, options?: RTCDataChannelInit): DurableChannel {
|
|
||||||
// Check if channel already exists
|
|
||||||
if (this.channels.has(label)) {
|
|
||||||
throw new Error(`Channel with label '${label}' already exists`);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create durable channel
|
|
||||||
const durableChannel = new DurableChannel(label, {
|
|
||||||
maxQueueSize: this.config.maxQueueSize,
|
|
||||||
maxMessageAge: this.config.maxMessageAge,
|
|
||||||
ordered: options?.ordered ?? true,
|
|
||||||
maxRetransmits: options?.maxRetransmits
|
|
||||||
});
|
|
||||||
|
|
||||||
this.channels.set(label, durableChannel);
|
|
||||||
|
|
||||||
// If we have a current peer, attach the channel
|
|
||||||
if (this.currentPeer && this._state === DurableConnectionState.CONNECTED) {
|
|
||||||
this.createAndAttachChannel(durableChannel, options);
|
|
||||||
}
|
|
||||||
|
|
||||||
return durableChannel;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get an existing channel by label
|
|
||||||
*/
|
|
||||||
getChannel(label: string): DurableChannel | undefined {
|
|
||||||
return this.channels.get(label);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Establish the initial connection
|
|
||||||
*
|
|
||||||
* @returns Promise that resolves when connected
|
|
||||||
*/
|
|
||||||
async connect(): Promise<void> {
|
|
||||||
if (this._state !== DurableConnectionState.CONNECTING) {
|
|
||||||
throw new Error(`Cannot connect from state: ${this._state}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
await this.establishConnection();
|
|
||||||
} catch (error) {
|
|
||||||
this._state = DurableConnectionState.DISCONNECTED;
|
|
||||||
await this.handleDisconnection();
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Close the connection gracefully
|
|
||||||
*/
|
|
||||||
async close(): Promise<void> {
|
|
||||||
if (this._state === DurableConnectionState.CLOSED) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const previousState = this._state;
|
|
||||||
this._state = DurableConnectionState.CLOSED;
|
|
||||||
|
|
||||||
// Cancel any ongoing reconnection
|
|
||||||
if (this.reconnectionScheduler) {
|
|
||||||
this.reconnectionScheduler.cancel();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close all channels
|
|
||||||
for (const channel of this.channels.values()) {
|
|
||||||
channel.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close peer connection
|
|
||||||
if (this.currentPeer) {
|
|
||||||
await this.currentPeer.close();
|
|
||||||
this.currentPeer = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.emit('state', this._state, previousState);
|
|
||||||
this.emit('closed');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Establish a WebRTC connection
|
|
||||||
*/
|
|
||||||
private async establishConnection(): Promise<void> {
|
|
||||||
// Create new peer
|
|
||||||
const peer = new RondevuPeer(this.offersApi, this.config.rtcConfig);
|
|
||||||
this.currentPeer = peer;
|
|
||||||
|
|
||||||
// Setup peer event handlers
|
|
||||||
this.setupPeerHandlers(peer);
|
|
||||||
|
|
||||||
// Determine connection method based on connection info
|
|
||||||
if (this.connectionInfo.uuid) {
|
|
||||||
// Connect by UUID
|
|
||||||
await this.connectByUuid(peer, this.connectionInfo.uuid);
|
|
||||||
} else if (this.connectionInfo.username && this.connectionInfo.serviceFqn) {
|
|
||||||
// Connect by username and service FQN
|
|
||||||
await this.connectByService(peer, this.connectionInfo.username, this.connectionInfo.serviceFqn);
|
|
||||||
} else {
|
|
||||||
throw new Error('Invalid connection info: must provide either uuid or (username + serviceFqn)');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Wait for connection with timeout
|
|
||||||
await this.waitForConnection(peer);
|
|
||||||
|
|
||||||
// Connection established
|
|
||||||
this.transitionToConnected();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Connect to a service by UUID
|
|
||||||
*/
|
|
||||||
private async connectByUuid(peer: RondevuPeer, uuid: string): Promise<void> {
|
|
||||||
// Get service details
|
|
||||||
const response = await fetch(`${this.offersApi['baseUrl']}/services/${uuid}`);
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Service not found: ${uuid}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const service = await response.json();
|
|
||||||
|
|
||||||
// Answer the offer
|
|
||||||
await peer.answer(service.offerId, service.sdp, {
|
|
||||||
secret: this.offersApi['credentials'].secret,
|
|
||||||
topics: []
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Connect to a service by username and service FQN
|
|
||||||
*/
|
|
||||||
private async connectByService(peer: RondevuPeer, username: string, serviceFqn: string): Promise<void> {
|
|
||||||
// Query service to get UUID
|
|
||||||
const response = await fetch(`${this.offersApi['baseUrl']}/index/${username}/query`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ serviceFqn })
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Service not found: ${username}/${serviceFqn}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { uuid } = await response.json();
|
|
||||||
|
|
||||||
// Connect by UUID
|
|
||||||
await this.connectByUuid(peer, uuid);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Wait for peer connection to establish
|
|
||||||
*/
|
|
||||||
private async waitForConnection(peer: RondevuPeer): Promise<void> {
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const timeout = setTimeout(() => {
|
|
||||||
reject(new Error('Connection timeout'));
|
|
||||||
}, this.config.connectionTimeout);
|
|
||||||
|
|
||||||
const onConnected = () => {
|
|
||||||
clearTimeout(timeout);
|
|
||||||
peer.off('connected', onConnected);
|
|
||||||
peer.off('failed', onFailed);
|
|
||||||
resolve();
|
|
||||||
};
|
|
||||||
|
|
||||||
const onFailed = (error: Error) => {
|
|
||||||
clearTimeout(timeout);
|
|
||||||
peer.off('connected', onConnected);
|
|
||||||
peer.off('failed', onFailed);
|
|
||||||
reject(error);
|
|
||||||
};
|
|
||||||
|
|
||||||
peer.on('connected', onConnected);
|
|
||||||
peer.on('failed', onFailed);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Setup event handlers for peer
|
|
||||||
*/
|
|
||||||
private setupPeerHandlers(peer: RondevuPeer): void {
|
|
||||||
this.peerConnectedHandler = () => {
|
|
||||||
// Connection established - will be handled by waitForConnection
|
|
||||||
};
|
|
||||||
|
|
||||||
this.peerDisconnectedHandler = () => {
|
|
||||||
if (this._state !== DurableConnectionState.CLOSED) {
|
|
||||||
this.handleDisconnection();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
this.peerFailedHandler = (error: Error) => {
|
|
||||||
if (this._state !== DurableConnectionState.CLOSED) {
|
|
||||||
console.error('Peer connection failed:', error);
|
|
||||||
this.handleDisconnection();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
this.peerDataChannelHandler = (channel: RTCDataChannel) => {
|
|
||||||
// Find or create durable channel
|
|
||||||
let durableChannel = this.channels.get(channel.label);
|
|
||||||
|
|
||||||
if (!durableChannel) {
|
|
||||||
// Auto-create channel for incoming data channels
|
|
||||||
durableChannel = new DurableChannel(channel.label, {
|
|
||||||
maxQueueSize: this.config.maxQueueSize,
|
|
||||||
maxMessageAge: this.config.maxMessageAge
|
|
||||||
});
|
|
||||||
this.channels.set(channel.label, durableChannel);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Attach the received channel
|
|
||||||
durableChannel.attachToChannel(channel);
|
|
||||||
};
|
|
||||||
|
|
||||||
peer.on('connected', this.peerConnectedHandler);
|
|
||||||
peer.on('disconnected', this.peerDisconnectedHandler);
|
|
||||||
peer.on('failed', this.peerFailedHandler);
|
|
||||||
peer.on('datachannel', this.peerDataChannelHandler);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Transition to connected state
|
|
||||||
*/
|
|
||||||
private transitionToConnected(): void {
|
|
||||||
const previousState = this._state;
|
|
||||||
this._state = DurableConnectionState.CONNECTED;
|
|
||||||
|
|
||||||
// Reset reconnection scheduler if it exists
|
|
||||||
if (this.reconnectionScheduler) {
|
|
||||||
this.reconnectionScheduler.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Attach all channels to the new peer connection
|
|
||||||
for (const [label, channel] of this.channels) {
|
|
||||||
if (this.currentPeer) {
|
|
||||||
this.createAndAttachChannel(channel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.emit('state', this._state, previousState);
|
|
||||||
this.emit('connected');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create underlying RTCDataChannel and attach to durable channel
|
|
||||||
*/
|
|
||||||
private createAndAttachChannel(
|
|
||||||
durableChannel: DurableChannel,
|
|
||||||
options?: RTCDataChannelInit
|
|
||||||
): void {
|
|
||||||
if (!this.currentPeer) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if peer already has this channel (received via datachannel event)
|
|
||||||
// If not, create it
|
|
||||||
const senders = (this.currentPeer.pc as any).getSenders?.() || [];
|
|
||||||
const existingChannel = Array.from(senders as RTCRtpSender[])
|
|
||||||
.map((sender) => (sender as any).channel as RTCDataChannel)
|
|
||||||
.find(ch => ch && ch.label === durableChannel.label);
|
|
||||||
|
|
||||||
if (existingChannel) {
|
|
||||||
durableChannel.attachToChannel(existingChannel);
|
|
||||||
} else {
|
|
||||||
// Create new channel on peer
|
|
||||||
const rtcChannel = this.currentPeer.createDataChannel(
|
|
||||||
durableChannel.label,
|
|
||||||
options
|
|
||||||
);
|
|
||||||
durableChannel.attachToChannel(rtcChannel);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle connection disconnection
|
|
||||||
*/
|
|
||||||
private async handleDisconnection(): Promise<void> {
|
|
||||||
if (this._state === DurableConnectionState.CLOSED ||
|
|
||||||
this._state === DurableConnectionState.FAILED) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const previousState = this._state;
|
|
||||||
this._state = DurableConnectionState.RECONNECTING;
|
|
||||||
|
|
||||||
this.emit('state', this._state, previousState);
|
|
||||||
this.emit('disconnected');
|
|
||||||
|
|
||||||
// Detach all channels (but keep them alive)
|
|
||||||
for (const channel of this.channels.values()) {
|
|
||||||
channel.detachFromChannel();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close old peer
|
|
||||||
if (this.currentPeer) {
|
|
||||||
await this.currentPeer.close();
|
|
||||||
this.currentPeer = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create or use existing reconnection scheduler
|
|
||||||
if (!this.reconnectionScheduler) {
|
|
||||||
this.reconnectionScheduler = createReconnectionScheduler({
|
|
||||||
maxAttempts: this.config.maxReconnectAttempts,
|
|
||||||
backoffBase: this.config.reconnectBackoffBase,
|
|
||||||
backoffMax: this.config.reconnectBackoffMax,
|
|
||||||
jitter: this.config.reconnectJitter,
|
|
||||||
onReconnect: async () => {
|
|
||||||
await this.establishConnection();
|
|
||||||
},
|
|
||||||
onMaxAttemptsExceeded: (error) => {
|
|
||||||
const prevState = this._state;
|
|
||||||
this._state = DurableConnectionState.FAILED;
|
|
||||||
this.emit('state', this._state, prevState);
|
|
||||||
this.emit('failed', error, true);
|
|
||||||
},
|
|
||||||
onBeforeAttempt: (attempt, max, delay) => {
|
|
||||||
this.emit('reconnecting', attempt, max, delay);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Schedule reconnection
|
|
||||||
this.reconnectionScheduler.schedule();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,200 +0,0 @@
|
|||||||
/**
|
|
||||||
* Reconnection utilities for durable connections
|
|
||||||
*
|
|
||||||
* This module provides utilities for managing reconnection logic with
|
|
||||||
* exponential backoff and jitter.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculate exponential backoff delay with jitter
|
|
||||||
*
|
|
||||||
* @param attempt - Current attempt number (0-indexed)
|
|
||||||
* @param base - Base delay in milliseconds
|
|
||||||
* @param max - Maximum delay in milliseconds
|
|
||||||
* @param jitter - Jitter factor (0-1), e.g., 0.2 for ±20%
|
|
||||||
* @returns Delay in milliseconds with jitter applied
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* ```typescript
|
|
||||||
* calculateBackoff(0, 1000, 30000, 0.2) // ~1000ms ± 20%
|
|
||||||
* calculateBackoff(1, 1000, 30000, 0.2) // ~2000ms ± 20%
|
|
||||||
* calculateBackoff(2, 1000, 30000, 0.2) // ~4000ms ± 20%
|
|
||||||
* calculateBackoff(5, 1000, 30000, 0.2) // ~30000ms ± 20% (capped at max)
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
export function calculateBackoff(
|
|
||||||
attempt: number,
|
|
||||||
base: number,
|
|
||||||
max: number,
|
|
||||||
jitter: number
|
|
||||||
): number {
|
|
||||||
// Calculate exponential delay: base * 2^attempt
|
|
||||||
const exponential = base * Math.pow(2, attempt);
|
|
||||||
|
|
||||||
// Cap at maximum
|
|
||||||
const capped = Math.min(exponential, max);
|
|
||||||
|
|
||||||
// Apply jitter: ± (jitter * capped)
|
|
||||||
const jitterAmount = capped * jitter;
|
|
||||||
const randomJitter = (Math.random() * 2 - 1) * jitterAmount;
|
|
||||||
|
|
||||||
// Return delay with jitter, ensuring it's not negative
|
|
||||||
return Math.max(0, capped + randomJitter);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Configuration for reconnection scheduler
|
|
||||||
*/
|
|
||||||
export interface ReconnectionSchedulerConfig {
|
|
||||||
/** Maximum number of reconnection attempts */
|
|
||||||
maxAttempts: number;
|
|
||||||
|
|
||||||
/** Base delay for exponential backoff */
|
|
||||||
backoffBase: number;
|
|
||||||
|
|
||||||
/** Maximum delay between attempts */
|
|
||||||
backoffMax: number;
|
|
||||||
|
|
||||||
/** Jitter factor for randomizing delays */
|
|
||||||
jitter: number;
|
|
||||||
|
|
||||||
/** Callback invoked for each reconnection attempt */
|
|
||||||
onReconnect: () => Promise<void>;
|
|
||||||
|
|
||||||
/** Callback invoked when max attempts exceeded */
|
|
||||||
onMaxAttemptsExceeded: (error: Error) => void;
|
|
||||||
|
|
||||||
/** Optional callback invoked before each attempt */
|
|
||||||
onBeforeAttempt?: (attempt: number, maxAttempts: number, delay: number) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Reconnection scheduler state
|
|
||||||
*/
|
|
||||||
export interface ReconnectionScheduler {
|
|
||||||
/** Current attempt number */
|
|
||||||
attempt: number;
|
|
||||||
|
|
||||||
/** Whether scheduler is active */
|
|
||||||
active: boolean;
|
|
||||||
|
|
||||||
/** Schedule next reconnection attempt */
|
|
||||||
schedule: () => void;
|
|
||||||
|
|
||||||
/** Cancel scheduled reconnection */
|
|
||||||
cancel: () => void;
|
|
||||||
|
|
||||||
/** Reset attempt counter */
|
|
||||||
reset: () => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a reconnection scheduler
|
|
||||||
*
|
|
||||||
* @param config - Scheduler configuration
|
|
||||||
* @returns Reconnection scheduler instance
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* ```typescript
|
|
||||||
* const scheduler = createReconnectionScheduler({
|
|
||||||
* maxAttempts: 10,
|
|
||||||
* backoffBase: 1000,
|
|
||||||
* backoffMax: 30000,
|
|
||||||
* jitter: 0.2,
|
|
||||||
* onReconnect: async () => {
|
|
||||||
* await connect();
|
|
||||||
* },
|
|
||||||
* onMaxAttemptsExceeded: (error) => {
|
|
||||||
* console.error('Failed to reconnect:', error);
|
|
||||||
* },
|
|
||||||
* onBeforeAttempt: (attempt, max, delay) => {
|
|
||||||
* console.log(`Reconnecting in ${delay}ms (${attempt}/${max})...`);
|
|
||||||
* }
|
|
||||||
* });
|
|
||||||
*
|
|
||||||
* // Start reconnection
|
|
||||||
* scheduler.schedule();
|
|
||||||
*
|
|
||||||
* // Cancel reconnection
|
|
||||||
* scheduler.cancel();
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
export function createReconnectionScheduler(
|
|
||||||
config: ReconnectionSchedulerConfig
|
|
||||||
): ReconnectionScheduler {
|
|
||||||
let attempt = 0;
|
|
||||||
let active = false;
|
|
||||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
||||||
|
|
||||||
const schedule = () => {
|
|
||||||
// Cancel any existing timer
|
|
||||||
if (timer) {
|
|
||||||
clearTimeout(timer);
|
|
||||||
timer = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if max attempts exceeded
|
|
||||||
if (attempt >= config.maxAttempts) {
|
|
||||||
active = false;
|
|
||||||
config.onMaxAttemptsExceeded(
|
|
||||||
new Error(`Max reconnection attempts exceeded (${config.maxAttempts})`)
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate delay
|
|
||||||
const delay = calculateBackoff(
|
|
||||||
attempt,
|
|
||||||
config.backoffBase,
|
|
||||||
config.backoffMax,
|
|
||||||
config.jitter
|
|
||||||
);
|
|
||||||
|
|
||||||
// Notify before attempt
|
|
||||||
if (config.onBeforeAttempt) {
|
|
||||||
config.onBeforeAttempt(attempt + 1, config.maxAttempts, delay);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mark as active
|
|
||||||
active = true;
|
|
||||||
|
|
||||||
// Schedule reconnection
|
|
||||||
timer = setTimeout(async () => {
|
|
||||||
attempt++;
|
|
||||||
try {
|
|
||||||
await config.onReconnect();
|
|
||||||
// Success - reset scheduler
|
|
||||||
attempt = 0;
|
|
||||||
active = false;
|
|
||||||
} catch (error) {
|
|
||||||
// Failure - schedule next attempt
|
|
||||||
schedule();
|
|
||||||
}
|
|
||||||
}, delay);
|
|
||||||
};
|
|
||||||
|
|
||||||
const cancel = () => {
|
|
||||||
if (timer) {
|
|
||||||
clearTimeout(timer);
|
|
||||||
timer = undefined;
|
|
||||||
}
|
|
||||||
active = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
const reset = () => {
|
|
||||||
cancel();
|
|
||||||
attempt = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
|
||||||
get attempt() {
|
|
||||||
return attempt;
|
|
||||||
},
|
|
||||||
get active() {
|
|
||||||
return active;
|
|
||||||
},
|
|
||||||
schedule,
|
|
||||||
cancel,
|
|
||||||
reset
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@@ -1,329 +0,0 @@
|
|||||||
/**
|
|
||||||
* DurableService - Service with automatic TTL refresh
|
|
||||||
*
|
|
||||||
* Manages service publishing with automatic reconnection for incoming
|
|
||||||
* connections and TTL auto-refresh to prevent expiration.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import { EventEmitter } from '../event-emitter.js';
|
|
||||||
import { ServicePool, type PoolStatus } from '../service-pool.js';
|
|
||||||
import type { RondevuOffers } from '../offers.js';
|
|
||||||
import { DurableChannel } from './channel.js';
|
|
||||||
import type {
|
|
||||||
DurableServiceConfig,
|
|
||||||
DurableServiceEvents,
|
|
||||||
ServiceInfo
|
|
||||||
} from './types.js';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Connection handler callback
|
|
||||||
*/
|
|
||||||
export type ConnectionHandler = (
|
|
||||||
channel: DurableChannel,
|
|
||||||
connectionId: string
|
|
||||||
) => void | Promise<void>;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Default configuration for durable services
|
|
||||||
*/
|
|
||||||
const DEFAULT_CONFIG = {
|
|
||||||
isPublic: false,
|
|
||||||
ttlRefreshMargin: 0.2,
|
|
||||||
poolSize: 1,
|
|
||||||
pollingInterval: 2000,
|
|
||||||
maxReconnectAttempts: 10,
|
|
||||||
reconnectBackoffBase: 1000,
|
|
||||||
reconnectBackoffMax: 30000,
|
|
||||||
reconnectJitter: 0.2,
|
|
||||||
connectionTimeout: 30000,
|
|
||||||
maxQueueSize: 1000,
|
|
||||||
maxMessageAge: 60000,
|
|
||||||
rtcConfig: {
|
|
||||||
iceServers: [
|
|
||||||
{ urls: 'stun:stun.l.google.com:19302' },
|
|
||||||
{ urls: 'stun:stun1.l.google.com:19302' }
|
|
||||||
]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Durable service that automatically refreshes TTL and handles reconnections
|
|
||||||
*
|
|
||||||
* The DurableService manages service publishing and provides:
|
|
||||||
* - Automatic TTL refresh before expiration
|
|
||||||
* - Durable connections for incoming peers
|
|
||||||
* - Connection pooling for multiple simultaneous connections
|
|
||||||
* - High-level connection lifecycle events
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* ```typescript
|
|
||||||
* const service = new DurableService(
|
|
||||||
* offersApi,
|
|
||||||
* (channel, connectionId) => {
|
|
||||||
* channel.on('message', (data) => {
|
|
||||||
* console.log(`Message from ${connectionId}:`, data);
|
|
||||||
* channel.send(`Echo: ${data}`);
|
|
||||||
* });
|
|
||||||
* },
|
|
||||||
* {
|
|
||||||
* username: 'alice',
|
|
||||||
* privateKey: keypair.privateKey,
|
|
||||||
* serviceFqn: 'chat@1.0.0',
|
|
||||||
* poolSize: 10
|
|
||||||
* }
|
|
||||||
* );
|
|
||||||
*
|
|
||||||
* service.on('published', (serviceId, uuid) => {
|
|
||||||
* console.log(`Service published: ${uuid}`);
|
|
||||||
* });
|
|
||||||
*
|
|
||||||
* service.on('connection', (connectionId) => {
|
|
||||||
* console.log(`New connection: ${connectionId}`);
|
|
||||||
* });
|
|
||||||
*
|
|
||||||
* await service.start();
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
export class DurableService extends EventEmitter<DurableServiceEvents> {
|
|
||||||
readonly config: Required<DurableServiceConfig>;
|
|
||||||
|
|
||||||
private serviceId?: string;
|
|
||||||
private uuid?: string;
|
|
||||||
private expiresAt?: number;
|
|
||||||
private ttlRefreshTimer?: ReturnType<typeof setTimeout>;
|
|
||||||
private servicePool?: ServicePool;
|
|
||||||
private activeChannels: Map<string, DurableChannel> = new Map();
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
private offersApi: RondevuOffers,
|
|
||||||
private baseUrl: string,
|
|
||||||
private credentials: { peerId: string; secret: string },
|
|
||||||
private handler: ConnectionHandler,
|
|
||||||
config: DurableServiceConfig
|
|
||||||
) {
|
|
||||||
super();
|
|
||||||
this.config = { ...DEFAULT_CONFIG, ...config } as Required<DurableServiceConfig>;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Start the service
|
|
||||||
*
|
|
||||||
* Publishes the service and begins accepting connections.
|
|
||||||
*
|
|
||||||
* @returns Service information
|
|
||||||
*/
|
|
||||||
async start(): Promise<ServiceInfo> {
|
|
||||||
if (this.servicePool) {
|
|
||||||
throw new Error('Service already started');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create and start service pool
|
|
||||||
this.servicePool = new ServicePool(
|
|
||||||
this.baseUrl,
|
|
||||||
this.credentials,
|
|
||||||
{
|
|
||||||
username: this.config.username,
|
|
||||||
privateKey: this.config.privateKey,
|
|
||||||
serviceFqn: this.config.serviceFqn,
|
|
||||||
rtcConfig: this.config.rtcConfig,
|
|
||||||
isPublic: this.config.isPublic,
|
|
||||||
metadata: this.config.metadata,
|
|
||||||
ttl: this.config.ttl,
|
|
||||||
poolSize: this.config.poolSize,
|
|
||||||
pollingInterval: this.config.pollingInterval,
|
|
||||||
handler: (channel, peer, connectionId) => {
|
|
||||||
this.handleNewConnection(channel, connectionId);
|
|
||||||
},
|
|
||||||
onPoolStatus: (status) => {
|
|
||||||
// Could emit pool status event if needed
|
|
||||||
},
|
|
||||||
onError: (error, context) => {
|
|
||||||
this.emit('error', error, context);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const handle = await this.servicePool.start();
|
|
||||||
|
|
||||||
// Store service info
|
|
||||||
this.serviceId = handle.serviceId;
|
|
||||||
this.uuid = handle.uuid;
|
|
||||||
this.expiresAt = Date.now() + (this.config.ttl || 300000); // Default 5 minutes
|
|
||||||
|
|
||||||
this.emit('published', this.serviceId, this.uuid);
|
|
||||||
|
|
||||||
// Schedule TTL refresh
|
|
||||||
this.scheduleRefresh();
|
|
||||||
|
|
||||||
return {
|
|
||||||
serviceId: this.serviceId,
|
|
||||||
uuid: this.uuid,
|
|
||||||
expiresAt: this.expiresAt
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop the service
|
|
||||||
*
|
|
||||||
* Unpublishes the service and closes all active connections.
|
|
||||||
*/
|
|
||||||
async stop(): Promise<void> {
|
|
||||||
// Cancel TTL refresh
|
|
||||||
if (this.ttlRefreshTimer) {
|
|
||||||
clearTimeout(this.ttlRefreshTimer);
|
|
||||||
this.ttlRefreshTimer = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close all active channels
|
|
||||||
for (const channel of this.activeChannels.values()) {
|
|
||||||
channel.close();
|
|
||||||
}
|
|
||||||
this.activeChannels.clear();
|
|
||||||
|
|
||||||
// Stop service pool
|
|
||||||
if (this.servicePool) {
|
|
||||||
await this.servicePool.stop();
|
|
||||||
this.servicePool = undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
this.emit('closed');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get list of active connection IDs
|
|
||||||
*/
|
|
||||||
getActiveConnections(): string[] {
|
|
||||||
return Array.from(this.activeChannels.keys());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get service information
|
|
||||||
*/
|
|
||||||
getServiceInfo(): ServiceInfo | null {
|
|
||||||
if (!this.serviceId || !this.uuid || !this.expiresAt) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
serviceId: this.serviceId,
|
|
||||||
uuid: this.uuid,
|
|
||||||
expiresAt: this.expiresAt
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Schedule TTL refresh
|
|
||||||
*/
|
|
||||||
private scheduleRefresh(): void {
|
|
||||||
if (!this.expiresAt || !this.config.ttl) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cancel existing timer
|
|
||||||
if (this.ttlRefreshTimer) {
|
|
||||||
clearTimeout(this.ttlRefreshTimer);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Calculate refresh time (default: refresh at 80% of TTL)
|
|
||||||
const timeUntilExpiry = this.expiresAt - Date.now();
|
|
||||||
const refreshMargin = timeUntilExpiry * this.config.ttlRefreshMargin;
|
|
||||||
const refreshTime = Math.max(0, timeUntilExpiry - refreshMargin);
|
|
||||||
|
|
||||||
// Schedule refresh
|
|
||||||
this.ttlRefreshTimer = setTimeout(() => {
|
|
||||||
this.refreshServiceTTL().catch(error => {
|
|
||||||
this.emit('error', error, 'ttl-refresh');
|
|
||||||
// Retry after short delay
|
|
||||||
setTimeout(() => this.scheduleRefresh(), 5000);
|
|
||||||
});
|
|
||||||
}, refreshTime);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Refresh service TTL
|
|
||||||
*/
|
|
||||||
private async refreshServiceTTL(): Promise<void> {
|
|
||||||
if (!this.serviceId || !this.uuid) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete old service
|
|
||||||
await this.servicePool?.stop();
|
|
||||||
|
|
||||||
// Recreate service pool (this republishes the service)
|
|
||||||
this.servicePool = new ServicePool(
|
|
||||||
this.baseUrl,
|
|
||||||
this.credentials,
|
|
||||||
{
|
|
||||||
username: this.config.username,
|
|
||||||
privateKey: this.config.privateKey,
|
|
||||||
serviceFqn: this.config.serviceFqn,
|
|
||||||
rtcConfig: this.config.rtcConfig,
|
|
||||||
isPublic: this.config.isPublic,
|
|
||||||
metadata: this.config.metadata,
|
|
||||||
ttl: this.config.ttl,
|
|
||||||
poolSize: this.config.poolSize,
|
|
||||||
pollingInterval: this.config.pollingInterval,
|
|
||||||
handler: (channel, peer, connectionId) => {
|
|
||||||
this.handleNewConnection(channel, connectionId);
|
|
||||||
},
|
|
||||||
onPoolStatus: (status) => {
|
|
||||||
// Could emit pool status event if needed
|
|
||||||
},
|
|
||||||
onError: (error, context) => {
|
|
||||||
this.emit('error', error, context);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
const handle = await this.servicePool.start();
|
|
||||||
|
|
||||||
// Update service info
|
|
||||||
this.serviceId = handle.serviceId;
|
|
||||||
this.uuid = handle.uuid;
|
|
||||||
this.expiresAt = Date.now() + (this.config.ttl || 300000);
|
|
||||||
|
|
||||||
this.emit('ttl-refreshed', this.expiresAt);
|
|
||||||
|
|
||||||
// Schedule next refresh
|
|
||||||
this.scheduleRefresh();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handle new incoming connection
|
|
||||||
*/
|
|
||||||
private handleNewConnection(channel: RTCDataChannel, connectionId: string): void {
|
|
||||||
// Create durable channel
|
|
||||||
const durableChannel = new DurableChannel(channel.label, {
|
|
||||||
maxQueueSize: this.config.maxQueueSize,
|
|
||||||
maxMessageAge: this.config.maxMessageAge
|
|
||||||
});
|
|
||||||
|
|
||||||
// Attach to underlying channel
|
|
||||||
durableChannel.attachToChannel(channel);
|
|
||||||
|
|
||||||
// Track channel
|
|
||||||
this.activeChannels.set(connectionId, durableChannel);
|
|
||||||
|
|
||||||
// Setup cleanup on close
|
|
||||||
durableChannel.on('close', () => {
|
|
||||||
this.activeChannels.delete(connectionId);
|
|
||||||
this.emit('disconnection', connectionId);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Emit connection event
|
|
||||||
this.emit('connection', connectionId);
|
|
||||||
|
|
||||||
// Invoke user handler
|
|
||||||
try {
|
|
||||||
const result = this.handler(durableChannel, connectionId);
|
|
||||||
if (result && typeof result.then === 'function') {
|
|
||||||
result.catch(error => {
|
|
||||||
this.emit('error', error, 'handler');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
this.emit('error', error as Error, 'handler');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,184 +0,0 @@
|
|||||||
/**
|
|
||||||
* Type definitions for durable WebRTC connections
|
|
||||||
*
|
|
||||||
* This module defines all interfaces, enums, and types used by the durable
|
|
||||||
* connection system for automatic reconnection and message queuing.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Connection state enum
|
|
||||||
*/
|
|
||||||
export enum DurableConnectionState {
|
|
||||||
CONNECTING = 'connecting',
|
|
||||||
CONNECTED = 'connected',
|
|
||||||
RECONNECTING = 'reconnecting',
|
|
||||||
DISCONNECTED = 'disconnected',
|
|
||||||
FAILED = 'failed',
|
|
||||||
CLOSED = 'closed'
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Channel state enum
|
|
||||||
*/
|
|
||||||
export enum DurableChannelState {
|
|
||||||
CONNECTING = 'connecting',
|
|
||||||
OPEN = 'open',
|
|
||||||
CLOSING = 'closing',
|
|
||||||
CLOSED = 'closed'
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Configuration for durable connections
|
|
||||||
*/
|
|
||||||
export interface DurableConnectionConfig {
|
|
||||||
/** Maximum number of reconnection attempts (default: 10) */
|
|
||||||
maxReconnectAttempts?: number;
|
|
||||||
|
|
||||||
/** Base delay for exponential backoff in milliseconds (default: 1000) */
|
|
||||||
reconnectBackoffBase?: number;
|
|
||||||
|
|
||||||
/** Maximum delay between reconnection attempts in milliseconds (default: 30000) */
|
|
||||||
reconnectBackoffMax?: number;
|
|
||||||
|
|
||||||
/** Jitter factor for randomizing reconnection delays (default: 0.2 = ±20%) */
|
|
||||||
reconnectJitter?: number;
|
|
||||||
|
|
||||||
/** Timeout for initial connection attempt in milliseconds (default: 30000) */
|
|
||||||
connectionTimeout?: number;
|
|
||||||
|
|
||||||
/** Maximum number of messages to queue during disconnection (default: 1000) */
|
|
||||||
maxQueueSize?: number;
|
|
||||||
|
|
||||||
/** Maximum age of queued messages in milliseconds (default: 60000) */
|
|
||||||
maxMessageAge?: number;
|
|
||||||
|
|
||||||
/** WebRTC configuration */
|
|
||||||
rtcConfig?: RTCConfiguration;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Configuration for durable channels
|
|
||||||
*/
|
|
||||||
export interface DurableChannelConfig {
|
|
||||||
/** Maximum number of messages to queue (default: 1000) */
|
|
||||||
maxQueueSize?: number;
|
|
||||||
|
|
||||||
/** Maximum age of queued messages in milliseconds (default: 60000) */
|
|
||||||
maxMessageAge?: number;
|
|
||||||
|
|
||||||
/** Whether messages should be delivered in order (default: true) */
|
|
||||||
ordered?: boolean;
|
|
||||||
|
|
||||||
/** Maximum retransmits for unordered channels (default: undefined) */
|
|
||||||
maxRetransmits?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Configuration for durable services
|
|
||||||
*/
|
|
||||||
export interface DurableServiceConfig extends DurableConnectionConfig {
|
|
||||||
/** 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;
|
|
||||||
|
|
||||||
/** Whether the service is publicly discoverable (default: false) */
|
|
||||||
isPublic?: boolean;
|
|
||||||
|
|
||||||
/** Optional metadata for the service */
|
|
||||||
metadata?: Record<string, any>;
|
|
||||||
|
|
||||||
/** Time-to-live for service in milliseconds (default: server default) */
|
|
||||||
ttl?: number;
|
|
||||||
|
|
||||||
/** Margin before TTL expiry to trigger refresh (default: 0.2 = refresh at 80%) */
|
|
||||||
ttlRefreshMargin?: number;
|
|
||||||
|
|
||||||
/** Number of simultaneous open offers to maintain (default: 1) */
|
|
||||||
poolSize?: number;
|
|
||||||
|
|
||||||
/** Polling interval for checking answers in milliseconds (default: 2000) */
|
|
||||||
pollingInterval?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Queued message structure
|
|
||||||
*/
|
|
||||||
export interface QueuedMessage {
|
|
||||||
/** Message data */
|
|
||||||
data: string | Blob | ArrayBuffer | ArrayBufferView;
|
|
||||||
|
|
||||||
/** Timestamp when message was enqueued */
|
|
||||||
enqueuedAt: number;
|
|
||||||
|
|
||||||
/** Unique message ID */
|
|
||||||
id: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Event type map for DurableConnection
|
|
||||||
*/
|
|
||||||
export interface DurableConnectionEvents extends Record<string, (...args: any[]) => void> {
|
|
||||||
'state': (state: DurableConnectionState, previousState: DurableConnectionState) => void;
|
|
||||||
'connected': () => void;
|
|
||||||
'reconnecting': (attempt: number, maxAttempts: number, nextRetryIn: number) => void;
|
|
||||||
'disconnected': () => void;
|
|
||||||
'failed': (error: Error, permanent: boolean) => void;
|
|
||||||
'closed': () => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Event type map for DurableChannel
|
|
||||||
*/
|
|
||||||
export interface DurableChannelEvents extends Record<string, (...args: any[]) => void> {
|
|
||||||
'open': () => void;
|
|
||||||
'message': (data: any) => void;
|
|
||||||
'error': (error: Error) => void;
|
|
||||||
'close': () => void;
|
|
||||||
'bufferedAmountLow': () => void;
|
|
||||||
'queueOverflow': (droppedCount: number) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Event type map for DurableService
|
|
||||||
*/
|
|
||||||
export interface DurableServiceEvents extends Record<string, (...args: any[]) => void> {
|
|
||||||
'published': (serviceId: string, uuid: string) => void;
|
|
||||||
'connection': (connectionId: string) => void;
|
|
||||||
'disconnection': (connectionId: string) => void;
|
|
||||||
'ttl-refreshed': (expiresAt: number) => void;
|
|
||||||
'error': (error: Error, context: string) => void;
|
|
||||||
'closed': () => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Information about a durable connection
|
|
||||||
*/
|
|
||||||
export interface ConnectionInfo {
|
|
||||||
/** Username (for username-based connections) */
|
|
||||||
username?: string;
|
|
||||||
|
|
||||||
/** Service FQN (for service-based connections) */
|
|
||||||
serviceFqn?: string;
|
|
||||||
|
|
||||||
/** UUID (for UUID-based connections) */
|
|
||||||
uuid?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Service information returned when service is published
|
|
||||||
*/
|
|
||||||
export interface ServiceInfo {
|
|
||||||
/** Service ID */
|
|
||||||
serviceId: string;
|
|
||||||
|
|
||||||
/** Service UUID for discovery */
|
|
||||||
uuid: string;
|
|
||||||
|
|
||||||
/** Expiration timestamp */
|
|
||||||
expiresAt: number;
|
|
||||||
}
|
|
||||||
@@ -1,109 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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<EventMap extends Record<string, (...args: any[]) => void>> {
|
|
||||||
private events: Map<keyof EventMap, Set<Function>> = new Map();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Register an event listener
|
|
||||||
*/
|
|
||||||
on<K extends keyof EventMap>(event: K, listener: EventMap[K]): this {
|
|
||||||
if (!this.events.has(event)) {
|
|
||||||
this.events.set(event, new Set());
|
|
||||||
}
|
|
||||||
this.events.get(event)!.add(listener);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Register a one-time event listener
|
|
||||||
*/
|
|
||||||
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 as EventMap[K]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove an event listener
|
|
||||||
*/
|
|
||||||
off<K extends keyof EventMap>(event: K, listener: EventMap[K]): this {
|
|
||||||
const listeners = this.events.get(event);
|
|
||||||
if (listeners) {
|
|
||||||
listeners.delete(listener);
|
|
||||||
if (listeners.size === 0) {
|
|
||||||
this.events.delete(event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Emit an event
|
|
||||||
*/
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
listeners.forEach(listener => {
|
|
||||||
try {
|
|
||||||
(listener as EventMap[K])(...args);
|
|
||||||
} catch (err) {
|
|
||||||
console.error(`Error in ${String(event)} event listener:`, err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove all listeners for an event (or all events if not specified)
|
|
||||||
*/
|
|
||||||
removeAllListeners<K extends keyof EventMap>(event?: K): this {
|
|
||||||
if (event !== undefined) {
|
|
||||||
this.events.delete(event);
|
|
||||||
} else {
|
|
||||||
this.events.clear();
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get listener count for an event
|
|
||||||
*/
|
|
||||||
listenerCount<K extends keyof EventMap>(event: K): number {
|
|
||||||
const listeners = this.events.get(event);
|
|
||||||
return listeners ? listeners.size : 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
53
src/index.ts
53
src/index.ts
@@ -1,36 +1,29 @@
|
|||||||
/**
|
/**
|
||||||
* @xtr-dev/rondevu-client
|
* @xtr-dev/rondevu-client
|
||||||
* WebRTC peer signaling and discovery client with durable connections
|
* WebRTC peer signaling client
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Export main client class
|
export { Rondevu } from './rondevu.js'
|
||||||
export { Rondevu } from './rondevu.js';
|
export { RondevuAPI } from './api.js'
|
||||||
export type { RondevuOptions } from './rondevu.js';
|
export { RondevuSignaler } from './rondevu-signaler.js'
|
||||||
|
|
||||||
// Export authentication
|
// Export types
|
||||||
export { RondevuAuth } from './auth.js';
|
|
||||||
export type { Credentials, FetchFunction } from './auth.js';
|
|
||||||
|
|
||||||
// Export username API
|
|
||||||
export { RondevuUsername } from './usernames.js';
|
|
||||||
export type { UsernameClaimResult, UsernameCheckResult } from './usernames.js';
|
|
||||||
|
|
||||||
// Export durable connection APIs
|
|
||||||
export { DurableConnection } from './durable/connection.js';
|
|
||||||
export { DurableChannel } from './durable/channel.js';
|
|
||||||
export { DurableService } from './durable/service.js';
|
|
||||||
|
|
||||||
// Export durable connection types
|
|
||||||
export type {
|
export type {
|
||||||
DurableConnectionState,
|
Signaler,
|
||||||
DurableChannelState,
|
Binnable,
|
||||||
DurableConnectionConfig,
|
} from './types.js'
|
||||||
DurableChannelConfig,
|
|
||||||
DurableServiceConfig,
|
export type {
|
||||||
QueuedMessage,
|
Credentials,
|
||||||
DurableConnectionEvents,
|
Keypair,
|
||||||
DurableChannelEvents,
|
OfferRequest,
|
||||||
DurableServiceEvents,
|
Offer,
|
||||||
ConnectionInfo,
|
ServiceRequest,
|
||||||
ServiceInfo
|
Service,
|
||||||
} from './durable/types.js';
|
IceCandidate,
|
||||||
|
} from './api.js'
|
||||||
|
|
||||||
|
export type { RondevuOptions, PublishServiceOptions } from './rondevu.js'
|
||||||
|
|
||||||
|
export type { PollingConfig } from './rondevu-signaler.js'
|
||||||
|
|
||||||
|
|||||||
@@ -1,205 +0,0 @@
|
|||||||
import { RondevuOffers, Offer } from './offers.js';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Represents an offer that has been answered
|
|
||||||
*/
|
|
||||||
export interface AnsweredOffer {
|
|
||||||
offerId: string;
|
|
||||||
answererId: string;
|
|
||||||
sdp: string; // Answer SDP
|
|
||||||
peerConnection: RTCPeerConnection; // Original peer connection
|
|
||||||
dataChannel?: RTCDataChannel; // Data channel created with offer
|
|
||||||
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<{ offers: Offer[], peerConnections: RTCPeerConnection[], dataChannels: RTCDataChannel[] }>;
|
|
||||||
|
|
||||||
/** 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 peerConnections: Map<string, RTCPeerConnection> = new Map();
|
|
||||||
private dataChannels: Map<string, RTCDataChannel> = 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 with their peer connections and data channels
|
|
||||||
*/
|
|
||||||
async addOffers(offers: Offer[], peerConnections?: RTCPeerConnection[], dataChannels?: RTCDataChannel[]): Promise<void> {
|
|
||||||
for (let i = 0; i < offers.length; i++) {
|
|
||||||
const offer = offers[i];
|
|
||||||
this.offers.set(offer.id, offer);
|
|
||||||
if (peerConnections && peerConnections[i]) {
|
|
||||||
this.peerConnections.set(offer.id, peerConnections[i]);
|
|
||||||
}
|
|
||||||
if (dataChannels && dataChannels[i]) {
|
|
||||||
this.dataChannels.set(offer.id, dataChannels[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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) {
|
|
||||||
// Get the original offer, peer connection, and data channel
|
|
||||||
const offer = this.offers.get(answer.offerId);
|
|
||||||
const pc = this.peerConnections.get(answer.offerId);
|
|
||||||
const channel = this.dataChannels.get(answer.offerId);
|
|
||||||
|
|
||||||
if (!offer || !pc) {
|
|
||||||
continue; // Offer or peer connection already consumed, skip
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove from pool BEFORE processing to prevent duplicate processing
|
|
||||||
this.offers.delete(answer.offerId);
|
|
||||||
this.peerConnections.delete(answer.offerId);
|
|
||||||
this.dataChannels.delete(answer.offerId);
|
|
||||||
|
|
||||||
// Notify ServicePool with answer, original peer connection, and data channel
|
|
||||||
await this.options.onAnswered({
|
|
||||||
offerId: answer.offerId,
|
|
||||||
answererId: answer.answererId,
|
|
||||||
sdp: answer.sdp,
|
|
||||||
peerConnection: pc,
|
|
||||||
dataChannel: channel,
|
|
||||||
answeredAt: answer.answeredAt
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// Immediate refill if below pool size
|
|
||||||
if (this.offers.size < this.options.poolSize) {
|
|
||||||
const needed = this.options.poolSize - this.offers.size;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const result = await this.options.onRefill(needed);
|
|
||||||
await this.addOffers(result.offers, result.peerConnections, result.dataChannels);
|
|
||||||
} 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 all active peer connections
|
|
||||||
*/
|
|
||||||
getActivePeerConnections(): RTCPeerConnection[] {
|
|
||||||
return Array.from(this.peerConnections.values());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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
321
src/offers.ts
@@ -1,321 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,12 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
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
|
|
||||||
);
|
|
||||||
|
|
||||||
if (candidates.length > 0) {
|
|
||||||
console.log(`📥 Received ${candidates.length} remote ICE candidate(s)`);
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const cand of candidates) {
|
|
||||||
if (cand.candidate && cand.candidate.candidate && cand.candidate.candidate !== '') {
|
|
||||||
const type = cand.candidate.candidate.includes('typ host') ? 'host' :
|
|
||||||
cand.candidate.candidate.includes('typ srflx') ? 'srflx' :
|
|
||||||
cand.candidate.candidate.includes('typ relay') ? 'relay' : 'unknown';
|
|
||||||
console.log(`🧊 Adding remote ${type} ICE candidate:`, cand.candidate.candidate);
|
|
||||||
try {
|
|
||||||
await this.peer.pc.addIceCandidate(new this.peer.RTCIceCandidate(cand.candidate));
|
|
||||||
console.log(`✅ Added remote ${type} ICE candidate`);
|
|
||||||
this.lastIceTimestamp = cand.createdAt;
|
|
||||||
} catch (err) {
|
|
||||||
console.warn(`⚠️ Failed to add remote ${type} 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,237 +0,0 @@
|
|||||||
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' }
|
|
||||||
]
|
|
||||||
},
|
|
||||||
existingPeerConnection?: RTCPeerConnection,
|
|
||||||
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);
|
|
||||||
|
|
||||||
// Use existing peer connection if provided, otherwise create new one
|
|
||||||
this.pc = existingPeerConnection || new this.RTCPeerConnection(rtcConfig);
|
|
||||||
this._state = new IdleState(this);
|
|
||||||
|
|
||||||
this.setupPeerConnection();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set up peer connection event handlers
|
|
||||||
*/
|
|
||||||
private setupPeerConnection(): void {
|
|
||||||
this.connectionStateChangeHandler = () => {
|
|
||||||
console.log(`🔌 Connection state changed: ${this.pc.connectionState}`);
|
|
||||||
switch (this.pc.connectionState) {
|
|
||||||
case 'connected':
|
|
||||||
console.log('✅ WebRTC connection established');
|
|
||||||
this.setState(new ConnectedState(this));
|
|
||||||
this.emitEvent('connected');
|
|
||||||
break;
|
|
||||||
case 'disconnected':
|
|
||||||
console.log('⚠️ WebRTC connection disconnected');
|
|
||||||
this.emitEvent('disconnected');
|
|
||||||
break;
|
|
||||||
case 'failed':
|
|
||||||
console.log('❌ WebRTC connection failed');
|
|
||||||
this.setState(new FailedState(this, new Error('Connection failed')));
|
|
||||||
break;
|
|
||||||
case 'closed':
|
|
||||||
console.log('🔒 WebRTC connection closed');
|
|
||||||
this.setState(new ClosedState(this));
|
|
||||||
this.emitEvent('disconnected');
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
this.pc.addEventListener('connectionstatechange', this.connectionStateChangeHandler);
|
|
||||||
|
|
||||||
// Add ICE connection state logging
|
|
||||||
const iceConnectionStateHandler = () => {
|
|
||||||
console.log(`🧊 ICE connection state: ${this.pc.iceConnectionState}`);
|
|
||||||
};
|
|
||||||
this.pc.addEventListener('iceconnectionstatechange', iceConnectionStateHandler);
|
|
||||||
|
|
||||||
// Add ICE gathering state logging
|
|
||||||
const iceGatheringStateHandler = () => {
|
|
||||||
console.log(`🔍 ICE gathering state: ${this.pc.iceGatheringState}`);
|
|
||||||
};
|
|
||||||
this.pc.addEventListener('icegatheringstatechange', iceGatheringStateHandler);
|
|
||||||
|
|
||||||
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) => {
|
|
||||||
const iceError = event as RTCPeerConnectionIceErrorEvent;
|
|
||||||
console.error(`❌ ICE candidate error: ${iceError.errorText || 'Unknown error'}`, {
|
|
||||||
errorCode: iceError.errorCode,
|
|
||||||
url: iceError.url,
|
|
||||||
address: iceError.address,
|
|
||||||
port: iceError.port
|
|
||||||
});
|
|
||||||
};
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,73 +0,0 @@
|
|||||||
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 !== '') {
|
|
||||||
const type = candidateData.candidate.includes('typ host') ? 'host' :
|
|
||||||
candidateData.candidate.includes('typ srflx') ? 'srflx' :
|
|
||||||
candidateData.candidate.includes('typ relay') ? 'relay' : 'unknown';
|
|
||||||
console.log(`🧊 Generated ${type} ICE candidate:`, candidateData.candidate);
|
|
||||||
try {
|
|
||||||
await this.peer.offersApi.addIceCandidates(this.peer.offerId, [candidateData]);
|
|
||||||
console.log(`✅ Sent ${type} ICE candidate to server`);
|
|
||||||
} catch (err) {
|
|
||||||
console.error(`❌ Error sending ${type} ICE candidate:`, err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (!event.candidate) {
|
|
||||||
console.log('🧊 ICE gathering complete (null candidate)');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
@@ -1,78 +0,0 @@
|
|||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
442
src/rondevu-signaler.ts
Normal file
442
src/rondevu-signaler.ts
Normal file
@@ -0,0 +1,442 @@
|
|||||||
|
import { Signaler, Binnable } from './types.js'
|
||||||
|
import { Rondevu } from './rondevu.js'
|
||||||
|
|
||||||
|
export interface PollingConfig {
|
||||||
|
initialInterval?: number // Default: 500ms
|
||||||
|
maxInterval?: number // Default: 5000ms
|
||||||
|
backoffMultiplier?: number // Default: 1.5
|
||||||
|
maxRetries?: number // Default: 50 (50 seconds max)
|
||||||
|
jitter?: boolean // Default: true
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RondevuSignaler - Handles WebRTC signaling via Rondevu service
|
||||||
|
*
|
||||||
|
* Manages offer/answer exchange and ICE candidate polling for establishing
|
||||||
|
* WebRTC connections through the Rondevu signaling server.
|
||||||
|
*
|
||||||
|
* Supports configurable polling with exponential backoff and jitter to reduce
|
||||||
|
* server load and prevent thundering herd issues.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```typescript
|
||||||
|
* const signaler = new RondevuSignaler(
|
||||||
|
* rondevuService,
|
||||||
|
* 'chat.app@1.0.0',
|
||||||
|
* 'peer-username',
|
||||||
|
* { initialInterval: 500, maxInterval: 5000, jitter: true }
|
||||||
|
* )
|
||||||
|
*
|
||||||
|
* // For offerer:
|
||||||
|
* await signaler.setOffer(offer)
|
||||||
|
* signaler.addAnswerListener(answer => {
|
||||||
|
* // Handle remote answer
|
||||||
|
* })
|
||||||
|
*
|
||||||
|
* // For answerer:
|
||||||
|
* signaler.addOfferListener(offer => {
|
||||||
|
* // Handle remote offer
|
||||||
|
* })
|
||||||
|
* await signaler.setAnswer(answer)
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
export class RondevuSignaler implements Signaler {
|
||||||
|
private offerId: string | null = null
|
||||||
|
private serviceFqn: string | null = null
|
||||||
|
private offerListeners: Array<(offer: RTCSessionDescriptionInit) => void> = []
|
||||||
|
private answerListeners: Array<(answer: RTCSessionDescriptionInit) => void> = []
|
||||||
|
private iceListeners: Array<(candidate: RTCIceCandidate) => void> = []
|
||||||
|
private answerPollingTimeout: ReturnType<typeof setTimeout> | null = null
|
||||||
|
private icePollingTimeout: ReturnType<typeof setTimeout> | null = null
|
||||||
|
private lastIceTimestamp = 0
|
||||||
|
private isPolling = false
|
||||||
|
private pollingConfig: Required<PollingConfig>
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private readonly rondevu: Rondevu,
|
||||||
|
private readonly service: string,
|
||||||
|
private readonly host?: string,
|
||||||
|
pollingConfig?: PollingConfig
|
||||||
|
) {
|
||||||
|
this.pollingConfig = {
|
||||||
|
initialInterval: pollingConfig?.initialInterval ?? 500,
|
||||||
|
maxInterval: pollingConfig?.maxInterval ?? 5000,
|
||||||
|
backoffMultiplier: pollingConfig?.backoffMultiplier ?? 1.5,
|
||||||
|
maxRetries: pollingConfig?.maxRetries ?? 50,
|
||||||
|
jitter: pollingConfig?.jitter ?? true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Publish an offer as a service
|
||||||
|
* Used by the offerer to make their offer available
|
||||||
|
*/
|
||||||
|
async setOffer(offer: RTCSessionDescriptionInit): Promise<void> {
|
||||||
|
if (!offer.sdp) {
|
||||||
|
throw new Error('Offer SDP is required')
|
||||||
|
}
|
||||||
|
|
||||||
|
// Publish service with the offer SDP
|
||||||
|
const publishedService = await this.rondevu.publishService({
|
||||||
|
serviceFqn: this.service,
|
||||||
|
offers: [{ sdp: offer.sdp }],
|
||||||
|
ttl: 300000, // 5 minutes
|
||||||
|
})
|
||||||
|
|
||||||
|
// Get the first offer from the published service
|
||||||
|
if (!publishedService.offers || publishedService.offers.length === 0) {
|
||||||
|
throw new Error('No offers returned from service publication')
|
||||||
|
}
|
||||||
|
|
||||||
|
this.offerId = publishedService.offers[0].offerId
|
||||||
|
this.serviceFqn = publishedService.serviceFqn
|
||||||
|
|
||||||
|
// Start polling for answer
|
||||||
|
this.startAnswerPolling()
|
||||||
|
|
||||||
|
// Start polling for ICE candidates
|
||||||
|
this.startIcePolling()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send an answer to the offerer
|
||||||
|
* Used by the answerer to respond to an offer
|
||||||
|
*/
|
||||||
|
async setAnswer(answer: RTCSessionDescriptionInit): Promise<void> {
|
||||||
|
if (!answer.sdp) {
|
||||||
|
throw new Error('Answer SDP is required')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.serviceFqn || !this.offerId) {
|
||||||
|
throw new Error('No service FQN or offer ID available. Must receive offer first.')
|
||||||
|
}
|
||||||
|
|
||||||
|
// Send answer to the service
|
||||||
|
const result = await this.rondevu.getAPI().postOfferAnswer(this.serviceFqn, this.offerId, answer.sdp)
|
||||||
|
this.offerId = result.offerId
|
||||||
|
|
||||||
|
// Start polling for ICE candidates
|
||||||
|
this.startIcePolling()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listen for incoming offers
|
||||||
|
* Used by the answerer to receive offers from the offerer
|
||||||
|
*/
|
||||||
|
addOfferListener(callback: (offer: RTCSessionDescriptionInit) => void): Binnable {
|
||||||
|
this.offerListeners.push(callback)
|
||||||
|
|
||||||
|
// If we have a host, start searching for their service
|
||||||
|
if (this.host && !this.isPolling) {
|
||||||
|
this.searchForOffer()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return cleanup function
|
||||||
|
return () => {
|
||||||
|
const index = this.offerListeners.indexOf(callback)
|
||||||
|
if (index > -1) {
|
||||||
|
this.offerListeners.splice(index, 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listen for incoming answers
|
||||||
|
* Used by the offerer to receive the answer from the answerer
|
||||||
|
*/
|
||||||
|
addAnswerListener(callback: (answer: RTCSessionDescriptionInit) => void): Binnable {
|
||||||
|
this.answerListeners.push(callback)
|
||||||
|
|
||||||
|
// Return cleanup function
|
||||||
|
return () => {
|
||||||
|
const index = this.answerListeners.indexOf(callback)
|
||||||
|
if (index > -1) {
|
||||||
|
this.answerListeners.splice(index, 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send an ICE candidate to the remote peer
|
||||||
|
*/
|
||||||
|
async addIceCandidate(candidate: RTCIceCandidate): Promise<void> {
|
||||||
|
if (!this.serviceFqn || !this.offerId) {
|
||||||
|
console.warn('Cannot send ICE candidate: no service FQN or offer ID')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const candidateData = candidate.toJSON()
|
||||||
|
|
||||||
|
// Skip empty candidates
|
||||||
|
if (!candidateData.candidate || candidateData.candidate === '') {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.rondevu.getAPI().addOfferIceCandidates(
|
||||||
|
this.serviceFqn,
|
||||||
|
this.offerId,
|
||||||
|
[candidateData]
|
||||||
|
)
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to send ICE candidate:', err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Listen for ICE candidates from the remote peer
|
||||||
|
*/
|
||||||
|
addListener(callback: (candidate: RTCIceCandidate) => void): Binnable {
|
||||||
|
this.iceListeners.push(callback)
|
||||||
|
|
||||||
|
// Return cleanup function
|
||||||
|
return () => {
|
||||||
|
const index = this.iceListeners.indexOf(callback)
|
||||||
|
if (index > -1) {
|
||||||
|
this.iceListeners.splice(index, 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search for an offer from the host
|
||||||
|
* Used by the answerer to find the offerer's service
|
||||||
|
*/
|
||||||
|
private async searchForOffer(): Promise<void> {
|
||||||
|
if (!this.host) {
|
||||||
|
throw new Error('No host specified for offer search')
|
||||||
|
}
|
||||||
|
|
||||||
|
this.isPolling = true
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Get service by FQN (service should include @username)
|
||||||
|
const serviceFqn = `${this.service}@${this.host}`
|
||||||
|
const serviceData = await this.rondevu.getAPI().getService(serviceFqn)
|
||||||
|
|
||||||
|
if (!serviceData) {
|
||||||
|
console.warn(`No service found for ${serviceFqn}`)
|
||||||
|
this.isPolling = false
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Store service details
|
||||||
|
this.offerId = serviceData.offerId
|
||||||
|
this.serviceFqn = serviceData.serviceFqn
|
||||||
|
|
||||||
|
// Notify offer listeners
|
||||||
|
const offer: RTCSessionDescriptionInit = {
|
||||||
|
type: 'offer',
|
||||||
|
sdp: serviceData.sdp,
|
||||||
|
}
|
||||||
|
|
||||||
|
this.offerListeners.forEach(listener => {
|
||||||
|
try {
|
||||||
|
listener(offer)
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Offer listener error:', err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to search for offer:', err)
|
||||||
|
this.isPolling = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start polling for answer (offerer side) with exponential backoff
|
||||||
|
*/
|
||||||
|
private startAnswerPolling(): void {
|
||||||
|
if (this.answerPollingTimeout || !this.serviceFqn || !this.offerId) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let interval = this.pollingConfig.initialInterval
|
||||||
|
let retries = 0
|
||||||
|
|
||||||
|
const poll = async () => {
|
||||||
|
if (!this.serviceFqn || !this.offerId) {
|
||||||
|
this.stopAnswerPolling()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const answer = await this.rondevu.getAPI().getOfferAnswer(this.serviceFqn, this.offerId)
|
||||||
|
|
||||||
|
if (answer && answer.sdp) {
|
||||||
|
// Store offerId if we didn't have it yet
|
||||||
|
if (!this.offerId) {
|
||||||
|
this.offerId = answer.offerId
|
||||||
|
}
|
||||||
|
|
||||||
|
// Got answer - notify listeners and stop polling
|
||||||
|
const answerDesc: RTCSessionDescriptionInit = {
|
||||||
|
type: 'answer',
|
||||||
|
sdp: answer.sdp,
|
||||||
|
}
|
||||||
|
|
||||||
|
this.answerListeners.forEach(listener => {
|
||||||
|
try {
|
||||||
|
listener(answerDesc)
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Answer listener error:', err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// Stop polling once we get the answer
|
||||||
|
this.stopAnswerPolling()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// No answer yet - exponential backoff
|
||||||
|
retries++
|
||||||
|
if (retries > this.pollingConfig.maxRetries) {
|
||||||
|
console.warn('Max retries reached for answer polling')
|
||||||
|
this.stopAnswerPolling()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
interval = Math.min(
|
||||||
|
interval * this.pollingConfig.backoffMultiplier,
|
||||||
|
this.pollingConfig.maxInterval
|
||||||
|
)
|
||||||
|
|
||||||
|
// Add jitter to prevent thundering herd
|
||||||
|
const finalInterval = this.pollingConfig.jitter
|
||||||
|
? interval + Math.random() * 100
|
||||||
|
: interval
|
||||||
|
|
||||||
|
this.answerPollingTimeout = setTimeout(poll, finalInterval)
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
// 404 is expected when answer isn't available yet
|
||||||
|
if (err instanceof Error && !err.message?.includes('404')) {
|
||||||
|
console.error('Error polling for answer:', err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retry with backoff
|
||||||
|
const finalInterval = this.pollingConfig.jitter
|
||||||
|
? interval + Math.random() * 100
|
||||||
|
: interval
|
||||||
|
this.answerPollingTimeout = setTimeout(poll, finalInterval)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
poll() // Start immediately
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop polling for answer
|
||||||
|
*/
|
||||||
|
private stopAnswerPolling(): void {
|
||||||
|
if (this.answerPollingTimeout) {
|
||||||
|
clearTimeout(this.answerPollingTimeout)
|
||||||
|
this.answerPollingTimeout = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start polling for ICE candidates with adaptive backoff
|
||||||
|
*/
|
||||||
|
private startIcePolling(): void {
|
||||||
|
if (this.icePollingTimeout || !this.serviceFqn || !this.offerId) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let interval = this.pollingConfig.initialInterval
|
||||||
|
|
||||||
|
const poll = async () => {
|
||||||
|
if (!this.serviceFqn || !this.offerId) {
|
||||||
|
this.stopIcePolling()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await this.rondevu
|
||||||
|
.getAPI()
|
||||||
|
.getOfferIceCandidates(this.serviceFqn, this.offerId, this.lastIceTimestamp)
|
||||||
|
|
||||||
|
let foundCandidates = false
|
||||||
|
|
||||||
|
for (const item of result.candidates) {
|
||||||
|
if (item.candidate && item.candidate.candidate && item.candidate.candidate !== '') {
|
||||||
|
foundCandidates = true
|
||||||
|
try {
|
||||||
|
const rtcCandidate = new RTCIceCandidate(item.candidate)
|
||||||
|
|
||||||
|
this.iceListeners.forEach(listener => {
|
||||||
|
try {
|
||||||
|
listener(rtcCandidate)
|
||||||
|
} catch (err) {
|
||||||
|
console.error('ICE listener error:', err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
this.lastIceTimestamp = item.createdAt
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('Failed to process ICE candidate:', err)
|
||||||
|
this.lastIceTimestamp = item.createdAt
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.lastIceTimestamp = item.createdAt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If candidates found, reset interval to initial value
|
||||||
|
// Otherwise, increase interval with backoff
|
||||||
|
if (foundCandidates) {
|
||||||
|
interval = this.pollingConfig.initialInterval
|
||||||
|
} else {
|
||||||
|
interval = Math.min(
|
||||||
|
interval * this.pollingConfig.backoffMultiplier,
|
||||||
|
this.pollingConfig.maxInterval
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add jitter
|
||||||
|
const finalInterval = this.pollingConfig.jitter
|
||||||
|
? interval + Math.random() * 100
|
||||||
|
: interval
|
||||||
|
|
||||||
|
this.icePollingTimeout = setTimeout(poll, finalInterval)
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
// 404/410 means offer expired, stop polling
|
||||||
|
if (err instanceof Error && (err.message?.includes('404') || err.message?.includes('410'))) {
|
||||||
|
console.warn('Offer not found or expired, stopping ICE polling')
|
||||||
|
this.stopIcePolling()
|
||||||
|
} else if (err instanceof Error && !err.message?.includes('404')) {
|
||||||
|
console.error('Error polling for ICE candidates:', err)
|
||||||
|
// Continue polling despite errors
|
||||||
|
const finalInterval = this.pollingConfig.jitter
|
||||||
|
? interval + Math.random() * 100
|
||||||
|
: interval
|
||||||
|
this.icePollingTimeout = setTimeout(poll, finalInterval)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
poll() // Start immediately
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop polling for ICE candidates
|
||||||
|
*/
|
||||||
|
private stopIcePolling(): void {
|
||||||
|
if (this.icePollingTimeout) {
|
||||||
|
clearTimeout(this.icePollingTimeout)
|
||||||
|
this.icePollingTimeout = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop all polling and cleanup
|
||||||
|
*/
|
||||||
|
dispose(): void {
|
||||||
|
this.stopAnswerPolling()
|
||||||
|
this.stopIcePolling()
|
||||||
|
this.offerListeners = []
|
||||||
|
this.answerListeners = []
|
||||||
|
this.iceListeners = []
|
||||||
|
}
|
||||||
|
}
|
||||||
587
src/rondevu.ts
587
src/rondevu.ts
@@ -1,296 +1,335 @@
|
|||||||
import { RondevuAuth, Credentials, FetchFunction } from './auth.js';
|
import { RondevuAPI, Credentials, Keypair, Service, ServiceRequest, IceCandidate } from './api.js'
|
||||||
import { RondevuOffers } from './offers.js';
|
|
||||||
import { RondevuUsername } from './usernames.js';
|
|
||||||
import RondevuPeer from './peer/index.js';
|
|
||||||
import { DurableService } from './durable/service.js';
|
|
||||||
import { DurableConnection } from './durable/connection.js';
|
|
||||||
import { DurableChannel } from './durable/channel.js';
|
|
||||||
import type {
|
|
||||||
DurableServiceConfig,
|
|
||||||
DurableConnectionConfig,
|
|
||||||
ConnectionInfo
|
|
||||||
} from './durable/types.js';
|
|
||||||
|
|
||||||
export interface RondevuOptions {
|
export interface RondevuOptions {
|
||||||
/**
|
apiUrl: string
|
||||||
* Base URL of the Rondevu server
|
username: string
|
||||||
* @default 'https://api.ronde.vu'
|
keypair?: Keypair
|
||||||
*/
|
credentials?: Credentials
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface PublishServiceOptions {
|
||||||
|
serviceFqn: string // Must include @username (e.g., "chat:1.0.0@alice")
|
||||||
|
offers: Array<{ sdp: string }>
|
||||||
|
ttl?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rondevu - Complete WebRTC signaling client
|
||||||
|
*
|
||||||
|
* Provides a unified API for:
|
||||||
|
* - Username claiming with Ed25519 signatures
|
||||||
|
* - Service publishing with automatic signature generation
|
||||||
|
* - Service discovery (direct, random, paginated)
|
||||||
|
* - WebRTC signaling (offer/answer exchange, ICE relay)
|
||||||
|
* - Keypair management
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```typescript
|
||||||
|
* // Initialize (generates keypair automatically)
|
||||||
|
* const rondevu = new Rondevu({
|
||||||
|
* apiUrl: 'https://signal.example.com',
|
||||||
|
* username: 'alice',
|
||||||
|
* })
|
||||||
|
*
|
||||||
|
* await rondevu.initialize()
|
||||||
|
*
|
||||||
|
* // Claim username (one time)
|
||||||
|
* await rondevu.claimUsername()
|
||||||
|
*
|
||||||
|
* // Publish a service
|
||||||
|
* const publishedService = await rondevu.publishService({
|
||||||
|
* serviceFqn: 'chat:1.0.0@alice',
|
||||||
|
* offers: [{ sdp: offerSdp }],
|
||||||
|
* ttl: 300000,
|
||||||
|
* })
|
||||||
|
*
|
||||||
|
* // Discover a service
|
||||||
|
* const service = await rondevu.getService('chat:1.0.0@bob')
|
||||||
|
*
|
||||||
|
* // Post answer
|
||||||
|
* await rondevu.postOfferAnswer(service.serviceFqn, service.offerId, answerSdp)
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
export class Rondevu {
|
export class Rondevu {
|
||||||
readonly auth: RondevuAuth;
|
private readonly api: RondevuAPI
|
||||||
readonly usernames: RondevuUsername;
|
private readonly username: string
|
||||||
|
private keypair: Keypair | null = null
|
||||||
|
private usernameClaimed = false
|
||||||
|
|
||||||
private _offers?: RondevuOffers;
|
constructor(options: RondevuOptions) {
|
||||||
private credentials?: Credentials;
|
this.username = options.username
|
||||||
private baseUrl: string;
|
this.keypair = options.keypair || null
|
||||||
private fetchFn?: FetchFunction;
|
this.api = new RondevuAPI(options.apiUrl, options.credentials)
|
||||||
private rtcPeerConnection?: typeof RTCPeerConnection;
|
|
||||||
private rtcSessionDescription?: typeof RTCSessionDescription;
|
|
||||||
private rtcIceCandidate?: typeof RTCIceCandidate;
|
|
||||||
|
|
||||||
constructor(options: RondevuOptions = {}) {
|
console.log('[Rondevu] Constructor called:', {
|
||||||
this.baseUrl = options.baseUrl || 'https://api.ronde.vu';
|
username: this.username,
|
||||||
this.fetchFn = options.fetch;
|
hasKeypair: !!this.keypair,
|
||||||
this.rtcPeerConnection = options.RTCPeerConnection;
|
publicKey: this.keypair?.publicKey
|
||||||
this.rtcSessionDescription = options.RTCSessionDescription;
|
})
|
||||||
this.rtcIceCandidate = options.RTCIceCandidate;
|
|
||||||
|
|
||||||
this.auth = new RondevuAuth(this.baseUrl, this.fetchFn);
|
|
||||||
this.usernames = new RondevuUsername(this.baseUrl);
|
|
||||||
|
|
||||||
if (options.credentials) {
|
|
||||||
this.credentials = options.credentials;
|
|
||||||
this._offers = new RondevuOffers(this.baseUrl, this.credentials, this.fetchFn);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get offers API (low-level access, requires authentication)
|
|
||||||
* For most use cases, use the durable connection APIs instead
|
|
||||||
*/
|
|
||||||
get offers(): RondevuOffers {
|
|
||||||
if (!this._offers) {
|
|
||||||
throw new Error('Not authenticated. Call register() first or provide credentials.');
|
|
||||||
}
|
|
||||||
return this._offers;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Register and initialize authenticated client
|
|
||||||
* Generates a cryptographically random peer ID (128-bit)
|
|
||||||
*/
|
|
||||||
async register(): Promise<Credentials> {
|
|
||||||
this.credentials = await this.auth.register();
|
|
||||||
|
|
||||||
// Create offers API instance
|
|
||||||
this._offers = new RondevuOffers(
|
|
||||||
this.baseUrl,
|
|
||||||
this.credentials,
|
|
||||||
this.fetchFn
|
|
||||||
);
|
|
||||||
|
|
||||||
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.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new RondevuPeer(
|
// ============================================
|
||||||
this._offers,
|
// Initialization
|
||||||
rtcConfig,
|
// ============================================
|
||||||
undefined, // No existing peer connection
|
|
||||||
this.rtcPeerConnection,
|
|
||||||
this.rtcSessionDescription,
|
|
||||||
this.rtcIceCandidate
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Expose a durable service with automatic reconnection and TTL refresh
|
* Initialize the service - generates keypair if not provided
|
||||||
*
|
* Call this before using other methods
|
||||||
* Creates a service that handles incoming connections with automatic
|
*/
|
||||||
* reconnection and message queuing during network interruptions.
|
async initialize(): Promise<void> {
|
||||||
*
|
console.log('[Rondevu] Initialize called, hasKeypair:', !!this.keypair)
|
||||||
* @param config Service configuration
|
|
||||||
* @returns DurableService instance
|
if (!this.keypair) {
|
||||||
*
|
console.log('[Rondevu] Generating new keypair...')
|
||||||
* @example
|
this.keypair = await RondevuAPI.generateKeypair()
|
||||||
* ```typescript
|
console.log('[Rondevu] Generated keypair, publicKey:', this.keypair.publicKey)
|
||||||
* const service = await client.exposeService({
|
} else {
|
||||||
* username: 'alice',
|
console.log('[Rondevu] Using existing keypair, publicKey:', this.keypair.publicKey)
|
||||||
* privateKey: keypair.privateKey,
|
}
|
||||||
* serviceFqn: 'chat@1.0.0',
|
|
||||||
* poolSize: 10,
|
// Register with API if no credentials provided
|
||||||
* handler: (channel, connectionId) => {
|
if (!this.api['credentials']) {
|
||||||
* channel.on('message', (data) => {
|
const credentials = await this.api.register()
|
||||||
* console.log('Received:', data);
|
this.api.setCredentials(credentials)
|
||||||
* channel.send(`Echo: ${data}`);
|
}
|
||||||
* });
|
|
||||||
* }
|
|
||||||
* });
|
|
||||||
*
|
|
||||||
* await service.start();
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
async exposeService(
|
|
||||||
config: DurableServiceConfig & {
|
|
||||||
handler: (channel: DurableChannel, connectionId: string) => void | Promise<void>;
|
|
||||||
}
|
|
||||||
): Promise<DurableService> {
|
|
||||||
if (!this._offers || !this.credentials) {
|
|
||||||
throw new Error('Not authenticated. Call register() first or provide credentials.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const service = new DurableService(
|
// ============================================
|
||||||
this._offers,
|
// Username Management
|
||||||
this.baseUrl,
|
// ============================================
|
||||||
this.credentials,
|
|
||||||
config.handler,
|
|
||||||
config
|
|
||||||
);
|
|
||||||
|
|
||||||
return service;
|
/**
|
||||||
}
|
* Claim the username with Ed25519 signature
|
||||||
|
* Should be called once before publishing services
|
||||||
|
*/
|
||||||
|
async claimUsername(): Promise<void> {
|
||||||
|
if (!this.keypair) {
|
||||||
|
throw new Error('Not initialized. Call initialize() first.')
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
// Check if username is already claimed
|
||||||
* Create a durable connection to a service by username and service FQN
|
const check = await this.api.checkUsername(this.username)
|
||||||
*
|
if (!check.available) {
|
||||||
* Establishes a WebRTC connection with automatic reconnection and
|
// Verify it's claimed by us
|
||||||
* message queuing during network interruptions.
|
if (check.publicKey === this.keypair.publicKey) {
|
||||||
*
|
this.usernameClaimed = true
|
||||||
* @param username Username of the service provider
|
return
|
||||||
* @param serviceFqn Fully qualified service name
|
}
|
||||||
* @param config Optional connection configuration
|
throw new Error(`Username "${this.username}" is already claimed by another user`)
|
||||||
* @returns DurableConnection instance
|
}
|
||||||
*
|
|
||||||
* @example
|
// Generate signature for username claim
|
||||||
* ```typescript
|
const message = `claim:${this.username}:${Date.now()}`
|
||||||
* const connection = await client.connect('alice', 'chat@1.0.0', {
|
const signature = await RondevuAPI.signMessage(message, this.keypair.privateKey)
|
||||||
* maxReconnectAttempts: 5
|
|
||||||
* });
|
// Claim the username
|
||||||
*
|
await this.api.claimUsername(this.username, this.keypair.publicKey, signature, message)
|
||||||
* const channel = connection.createChannel('main');
|
this.usernameClaimed = true
|
||||||
* channel.on('message', (data) => {
|
|
||||||
* console.log('Received:', data);
|
|
||||||
* });
|
|
||||||
*
|
|
||||||
* await connection.connect();
|
|
||||||
* channel.send('Hello!');
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
async connect(
|
|
||||||
username: string,
|
|
||||||
serviceFqn: string,
|
|
||||||
config?: DurableConnectionConfig
|
|
||||||
): Promise<DurableConnection> {
|
|
||||||
if (!this._offers) {
|
|
||||||
throw new Error('Not authenticated. Call register() first or provide credentials.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const connectionInfo: ConnectionInfo = {
|
/**
|
||||||
username,
|
* Check if username has been claimed (checks with server)
|
||||||
serviceFqn
|
*/
|
||||||
};
|
async isUsernameClaimed(): Promise<boolean> {
|
||||||
|
if (!this.keypair) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
return new DurableConnection(this._offers, connectionInfo, config);
|
try {
|
||||||
}
|
const check = await this.api.checkUsername(this.username)
|
||||||
|
|
||||||
/**
|
// Debug logging
|
||||||
* Create a durable connection to a service by UUID
|
console.log('[Rondevu] Username check:', {
|
||||||
*
|
username: this.username,
|
||||||
* Establishes a WebRTC connection with automatic reconnection and
|
available: check.available,
|
||||||
* message queuing during network interruptions.
|
serverPublicKey: check.publicKey,
|
||||||
*
|
localPublicKey: this.keypair.publicKey,
|
||||||
* @param uuid Service UUID
|
match: check.publicKey === this.keypair.publicKey
|
||||||
* @param config Optional connection configuration
|
})
|
||||||
* @returns DurableConnection instance
|
|
||||||
*
|
// Username is claimed if it's not available and owned by our public key
|
||||||
* @example
|
const claimed = !check.available && check.publicKey === this.keypair.publicKey
|
||||||
* ```typescript
|
|
||||||
* const connection = await client.connectByUuid('service-uuid-here', {
|
// Update internal flag to match server state
|
||||||
* maxReconnectAttempts: 5
|
this.usernameClaimed = claimed
|
||||||
* });
|
|
||||||
*
|
return claimed
|
||||||
* const channel = connection.createChannel('main');
|
} catch (err) {
|
||||||
* channel.on('message', (data) => {
|
console.error('Failed to check username claim status:', err)
|
||||||
* console.log('Received:', data);
|
return false
|
||||||
* });
|
}
|
||||||
*
|
|
||||||
* await connection.connect();
|
|
||||||
* channel.send('Hello!');
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
async connectByUuid(
|
|
||||||
uuid: string,
|
|
||||||
config?: DurableConnectionConfig
|
|
||||||
): Promise<DurableConnection> {
|
|
||||||
if (!this._offers) {
|
|
||||||
throw new Error('Not authenticated. Call register() first or provide credentials.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const connectionInfo: ConnectionInfo = {
|
// ============================================
|
||||||
uuid
|
// Service Publishing
|
||||||
};
|
// ============================================
|
||||||
|
|
||||||
return new DurableConnection(this._offers, connectionInfo, config);
|
/**
|
||||||
}
|
* Publish a service with automatic signature generation
|
||||||
|
*/
|
||||||
|
async publishService(options: PublishServiceOptions): Promise<Service> {
|
||||||
|
if (!this.keypair) {
|
||||||
|
throw new Error('Not initialized. Call initialize() first.')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.usernameClaimed) {
|
||||||
|
throw new Error(
|
||||||
|
'Username not claimed. Call claimUsername() first or the server will reject the service.'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const { serviceFqn, offers, ttl } = options
|
||||||
|
|
||||||
|
// Generate signature for service publication
|
||||||
|
const message = `publish:${this.username}:${serviceFqn}:${Date.now()}`
|
||||||
|
const signature = await RondevuAPI.signMessage(message, this.keypair.privateKey)
|
||||||
|
|
||||||
|
// Create service request
|
||||||
|
const serviceRequest: ServiceRequest = {
|
||||||
|
serviceFqn, // Must include @username
|
||||||
|
offers,
|
||||||
|
signature,
|
||||||
|
message,
|
||||||
|
ttl,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Publish to server
|
||||||
|
return await this.api.publishService(serviceRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// Service Discovery
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get service by FQN (with username) - Direct lookup
|
||||||
|
* Example: chat:1.0.0@alice
|
||||||
|
*/
|
||||||
|
async getService(serviceFqn: string): Promise<{
|
||||||
|
serviceId: string
|
||||||
|
username: string
|
||||||
|
serviceFqn: string
|
||||||
|
offerId: string
|
||||||
|
sdp: string
|
||||||
|
createdAt: number
|
||||||
|
expiresAt: number
|
||||||
|
}> {
|
||||||
|
return await this.api.getService(serviceFqn)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Discover a random available service without knowing the username
|
||||||
|
* Example: chat:1.0.0 (without @username)
|
||||||
|
*/
|
||||||
|
async discoverService(serviceVersion: string): Promise<{
|
||||||
|
serviceId: string
|
||||||
|
username: string
|
||||||
|
serviceFqn: string
|
||||||
|
offerId: string
|
||||||
|
sdp: string
|
||||||
|
createdAt: number
|
||||||
|
expiresAt: number
|
||||||
|
}> {
|
||||||
|
return await this.api.discoverService(serviceVersion)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Discover multiple available services with pagination
|
||||||
|
* Example: chat:1.0.0 (without @username)
|
||||||
|
*/
|
||||||
|
async discoverServices(serviceVersion: string, limit: number = 10, offset: number = 0): Promise<{
|
||||||
|
services: Array<{
|
||||||
|
serviceId: string
|
||||||
|
username: string
|
||||||
|
serviceFqn: string
|
||||||
|
offerId: string
|
||||||
|
sdp: string
|
||||||
|
createdAt: number
|
||||||
|
expiresAt: number
|
||||||
|
}>
|
||||||
|
count: number
|
||||||
|
limit: number
|
||||||
|
offset: number
|
||||||
|
}> {
|
||||||
|
return await this.api.discoverServices(serviceVersion, limit, offset)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// WebRTC Signaling
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Post answer SDP to specific offer
|
||||||
|
*/
|
||||||
|
async postOfferAnswer(serviceFqn: string, offerId: string, sdp: string): Promise<{
|
||||||
|
success: boolean
|
||||||
|
offerId: string
|
||||||
|
}> {
|
||||||
|
return await this.api.postOfferAnswer(serviceFqn, offerId, sdp)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get answer SDP (offerer polls this)
|
||||||
|
*/
|
||||||
|
async getOfferAnswer(serviceFqn: string, offerId: string): Promise<{
|
||||||
|
sdp: string
|
||||||
|
offerId: string
|
||||||
|
answererId: string
|
||||||
|
answeredAt: number
|
||||||
|
} | null> {
|
||||||
|
return await this.api.getOfferAnswer(serviceFqn, offerId)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add ICE candidates to specific offer
|
||||||
|
*/
|
||||||
|
async addOfferIceCandidates(serviceFqn: string, offerId: string, candidates: RTCIceCandidateInit[]): Promise<{
|
||||||
|
count: number
|
||||||
|
offerId: string
|
||||||
|
}> {
|
||||||
|
return await this.api.addOfferIceCandidates(serviceFqn, offerId, candidates)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get ICE candidates for specific offer (with polling support)
|
||||||
|
*/
|
||||||
|
async getOfferIceCandidates(serviceFqn: string, offerId: string, since: number = 0): Promise<{
|
||||||
|
candidates: IceCandidate[]
|
||||||
|
offerId: string
|
||||||
|
}> {
|
||||||
|
return await this.api.getOfferIceCandidates(serviceFqn, offerId, since)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// Utility Methods
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the current keypair (for backup/storage)
|
||||||
|
*/
|
||||||
|
getKeypair(): Keypair | null {
|
||||||
|
return this.keypair
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the username
|
||||||
|
*/
|
||||||
|
getUsername(): string {
|
||||||
|
return this.username
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the public key
|
||||||
|
*/
|
||||||
|
getPublicKey(): string | null {
|
||||||
|
return this.keypair?.publicKey || null
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Access to underlying API for advanced operations
|
||||||
|
* @deprecated Use direct methods on Rondevu instance instead
|
||||||
|
*/
|
||||||
|
getAPI(): RondevuAPI {
|
||||||
|
return this.api
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,664 +0,0 @@
|
|||||||
import { RondevuOffers, Offer } from './offers.js';
|
|
||||||
import { RondevuUsername } from './usernames.js';
|
|
||||||
import RondevuPeer from './peer/index.js';
|
|
||||||
import { OfferPool, AnsweredOffer } from './offer-pool.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;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Service handle with pool-specific methods
|
|
||||||
*/
|
|
||||||
export interface PooledServiceHandle {
|
|
||||||
/** Service ID */
|
|
||||||
serviceId: string;
|
|
||||||
|
|
||||||
/** Service UUID */
|
|
||||||
uuid: string;
|
|
||||||
|
|
||||||
/** Offer ID */
|
|
||||||
offerId: string;
|
|
||||||
|
|
||||||
/** Unpublish the service */
|
|
||||||
unpublish: () => Promise<void>;
|
|
||||||
|
|
||||||
/** 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 peerConnections: Map<string, RTCPeerConnection> = 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[] = [];
|
|
||||||
const additionalPeerConnections: RTCPeerConnection[] = [];
|
|
||||||
const additionalDataChannels: RTCDataChannel[] = [];
|
|
||||||
if (poolSize > 1) {
|
|
||||||
try {
|
|
||||||
const result = await this.createOffers(poolSize - 1);
|
|
||||||
additionalOffers.push(...result.offers);
|
|
||||||
additionalPeerConnections.push(...result.peerConnections);
|
|
||||||
additionalDataChannels.push(...result.dataChannels);
|
|
||||||
} 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 with their peer connections and data channels
|
|
||||||
const allOffers = [
|
|
||||||
{ id: service.offerId, peerId: this.credentials.peerId, sdp: service.offerSdp, topics: [], expiresAt: service.expiresAt, lastSeen: Date.now() },
|
|
||||||
...additionalOffers
|
|
||||||
];
|
|
||||||
const allPeerConnections = [
|
|
||||||
service.peerConnection,
|
|
||||||
...additionalPeerConnections
|
|
||||||
];
|
|
||||||
const allDataChannels = [
|
|
||||||
service.dataChannel,
|
|
||||||
...additionalDataChannels
|
|
||||||
];
|
|
||||||
await this.offerPool.addOffers(allOffers, allPeerConnections, allDataChannels);
|
|
||||||
|
|
||||||
// 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. Close peer connections from the pool
|
|
||||||
if (this.offerPool) {
|
|
||||||
const poolPeerConnections = this.offerPool.getActivePeerConnections();
|
|
||||||
poolPeerConnections.forEach(pc => {
|
|
||||||
try {
|
|
||||||
pc.close();
|
|
||||||
} catch {
|
|
||||||
// Ignore errors during cleanup
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. Delete remaining offers
|
|
||||||
if (this.offerPool) {
|
|
||||||
const offerIds = this.offerPool.getActiveOfferIds();
|
|
||||||
await Promise.allSettled(
|
|
||||||
offerIds.map(id => this.offersApi.delete(id).catch(() => {}))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 4. 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);
|
|
||||||
|
|
||||||
// 5. 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 {
|
|
||||||
// Use the existing peer connection from the pool
|
|
||||||
const peer = new RondevuPeer(
|
|
||||||
this.offersApi,
|
|
||||||
this.options.rtcConfig || {
|
|
||||||
iceServers: [{ urls: 'stun:stun.l.google.com:19302' }]
|
|
||||||
},
|
|
||||||
answer.peerConnection // Use the existing peer connection
|
|
||||||
);
|
|
||||||
|
|
||||||
peer.role = 'offerer';
|
|
||||||
peer.offerId = answer.offerId;
|
|
||||||
|
|
||||||
// Verify peer connection is in correct state
|
|
||||||
if (peer.pc.signalingState !== 'have-local-offer') {
|
|
||||||
console.error('Peer connection state info:', {
|
|
||||||
signalingState: peer.pc.signalingState,
|
|
||||||
connectionState: peer.pc.connectionState,
|
|
||||||
iceConnectionState: peer.pc.iceConnectionState,
|
|
||||||
iceGatheringState: peer.pc.iceGatheringState,
|
|
||||||
hasLocalDescription: !!peer.pc.localDescription,
|
|
||||||
hasRemoteDescription: !!peer.pc.remoteDescription,
|
|
||||||
localDescriptionType: peer.pc.localDescription?.type,
|
|
||||||
remoteDescriptionType: peer.pc.remoteDescription?.type,
|
|
||||||
offerId: answer.offerId
|
|
||||||
});
|
|
||||||
throw new Error(
|
|
||||||
`Invalid signaling state: ${peer.pc.signalingState}. Expected 'have-local-offer' to set remote answer.`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set remote description (the answer)
|
|
||||||
await peer.pc.setRemoteDescription({
|
|
||||||
type: 'answer',
|
|
||||||
sdp: answer.sdp
|
|
||||||
});
|
|
||||||
|
|
||||||
// Use the data channel we created when making the offer
|
|
||||||
if (!answer.dataChannel) {
|
|
||||||
throw new Error('No data channel found for answered offer');
|
|
||||||
}
|
|
||||||
|
|
||||||
const channel = answer.dataChannel;
|
|
||||||
|
|
||||||
// Wait for the channel to open (it was created when we made the offer)
|
|
||||||
if (channel.readyState !== 'open') {
|
|
||||||
await new Promise<void>((resolve, reject) => {
|
|
||||||
const timeout = setTimeout(
|
|
||||||
() => reject(new Error('Timeout waiting for data channel to open')),
|
|
||||||
30000
|
|
||||||
);
|
|
||||||
|
|
||||||
channel.onopen = () => {
|
|
||||||
clearTimeout(timeout);
|
|
||||||
resolve();
|
|
||||||
};
|
|
||||||
|
|
||||||
channel.onerror = (error) => {
|
|
||||||
clearTimeout(timeout);
|
|
||||||
reject(new Error('Data channel error'));
|
|
||||||
};
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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<{ offers: Offer[], peerConnections: RTCPeerConnection[], dataChannels: RTCDataChannel[] }> {
|
|
||||||
if (count <= 0) {
|
|
||||||
return { offers: [], peerConnections: [], dataChannels: [] };
|
|
||||||
}
|
|
||||||
|
|
||||||
// Server supports max 10 offers per request
|
|
||||||
const batchSize = Math.min(count, 10);
|
|
||||||
const offers: Offer[] = [];
|
|
||||||
const peerConnections: RTCPeerConnection[] = [];
|
|
||||||
const dataChannels: RTCDataChannel[] = [];
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Create peer connections and generate offers
|
|
||||||
const offerRequests = [];
|
|
||||||
const pendingCandidates: RTCIceCandidateInit[][] = []; // Store candidates before we have offer IDs
|
|
||||||
|
|
||||||
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) and save reference
|
|
||||||
const channel = pc.createDataChannel('rondevu-service');
|
|
||||||
dataChannels.push(channel);
|
|
||||||
|
|
||||||
// Set up temporary candidate collector BEFORE setLocalDescription
|
|
||||||
const candidatesForThisOffer: RTCIceCandidateInit[] = [];
|
|
||||||
pendingCandidates.push(candidatesForThisOffer);
|
|
||||||
|
|
||||||
pc.onicecandidate = (event) => {
|
|
||||||
if (event.candidate) {
|
|
||||||
const candidateData = event.candidate.toJSON();
|
|
||||||
if (candidateData.candidate && candidateData.candidate !== '') {
|
|
||||||
const type = candidateData.candidate.includes('typ host') ? 'host' :
|
|
||||||
candidateData.candidate.includes('typ srflx') ? 'srflx' :
|
|
||||||
candidateData.candidate.includes('typ relay') ? 'relay' : 'unknown';
|
|
||||||
console.log(`🧊 Service pool generated ${type} ICE candidate:`, candidateData.candidate);
|
|
||||||
candidatesForThisOffer.push(candidateData);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log('🧊 Service pool ICE gathering complete');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Create offer
|
|
||||||
const offer = await pc.createOffer();
|
|
||||||
await pc.setLocalDescription(offer); // ICE gathering starts here, candidates go to collector
|
|
||||||
|
|
||||||
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
|
|
||||||
});
|
|
||||||
|
|
||||||
// Keep peer connection alive - DO NOT CLOSE
|
|
||||||
peerConnections.push(pc);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Batch create offers
|
|
||||||
const createdOffers = await this.offersApi.create(offerRequests);
|
|
||||||
offers.push(...createdOffers);
|
|
||||||
|
|
||||||
// Now send all pending candidates and set up handlers for future ones
|
|
||||||
for (let i = 0; i < peerConnections.length; i++) {
|
|
||||||
const pc = peerConnections[i];
|
|
||||||
const offerId = createdOffers[i].id;
|
|
||||||
const candidates = pendingCandidates[i];
|
|
||||||
|
|
||||||
// Send any candidates that were collected while waiting for offer ID
|
|
||||||
if (candidates.length > 0) {
|
|
||||||
console.log(`📤 Sending ${candidates.length} pending ICE candidate(s) for offer ${offerId}`);
|
|
||||||
try {
|
|
||||||
await this.offersApi.addIceCandidates(offerId, candidates);
|
|
||||||
console.log(`✅ Sent ${candidates.length} pending ICE candidate(s)`);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('❌ Error sending pending ICE candidates:', err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replace temporary handler with permanent one for any future candidates
|
|
||||||
pc.onicecandidate = async (event) => {
|
|
||||||
if (event.candidate) {
|
|
||||||
const candidateData = event.candidate.toJSON();
|
|
||||||
if (candidateData.candidate && candidateData.candidate !== '') {
|
|
||||||
const type = candidateData.candidate.includes('typ host') ? 'host' :
|
|
||||||
candidateData.candidate.includes('typ srflx') ? 'srflx' :
|
|
||||||
candidateData.candidate.includes('typ relay') ? 'relay' : 'unknown';
|
|
||||||
console.log(`🧊 Service pool generated late ${type} ICE candidate:`, candidateData.candidate);
|
|
||||||
try {
|
|
||||||
await this.offersApi.addIceCandidates(offerId, [candidateData]);
|
|
||||||
console.log(`✅ Sent late ${type} ICE candidate`);
|
|
||||||
} catch (err) {
|
|
||||||
console.error(`❌ Error sending ${type} ICE candidate:`, err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
// Close any created peer connections on error
|
|
||||||
peerConnections.forEach(pc => pc.close());
|
|
||||||
this.status.failedOfferCreations++;
|
|
||||||
this.handleError(error as Error, 'offer-creation');
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
|
|
||||||
return { offers, peerConnections, dataChannels };
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Publish the initial service (creates first offer)
|
|
||||||
*/
|
|
||||||
private async publishInitialService(): Promise<{
|
|
||||||
serviceId: string;
|
|
||||||
uuid: string;
|
|
||||||
offerId: string;
|
|
||||||
offerSdp: string;
|
|
||||||
expiresAt: number;
|
|
||||||
peerConnection: RTCPeerConnection;
|
|
||||||
dataChannel: RTCDataChannel;
|
|
||||||
}> {
|
|
||||||
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' }]
|
|
||||||
});
|
|
||||||
|
|
||||||
const dataChannel = pc.createDataChannel('rondevu-service');
|
|
||||||
|
|
||||||
// Collect candidates before we have offer ID
|
|
||||||
const pendingCandidates: RTCIceCandidateInit[] = [];
|
|
||||||
|
|
||||||
// Set up temporary candidate collector BEFORE setLocalDescription
|
|
||||||
pc.onicecandidate = (event) => {
|
|
||||||
if (event.candidate) {
|
|
||||||
const candidateData = event.candidate.toJSON();
|
|
||||||
if (candidateData.candidate && candidateData.candidate !== '') {
|
|
||||||
const type = candidateData.candidate.includes('typ host') ? 'host' :
|
|
||||||
candidateData.candidate.includes('typ srflx') ? 'srflx' :
|
|
||||||
candidateData.candidate.includes('typ relay') ? 'relay' : 'unknown';
|
|
||||||
console.log(`🧊 Initial service generated ${type} ICE candidate:`, candidateData.candidate);
|
|
||||||
pendingCandidates.push(candidateData);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log('🧊 Initial service ICE gathering complete');
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Create offer
|
|
||||||
const offer = await pc.createOffer();
|
|
||||||
await pc.setLocalDescription(offer); // ICE gathering starts here
|
|
||||||
|
|
||||||
if (!offer.sdp) {
|
|
||||||
pc.close();
|
|
||||||
throw new Error('Failed to generate SDP');
|
|
||||||
}
|
|
||||||
|
|
||||||
// Store the SDP
|
|
||||||
const offerSdp = offer.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: offerSdp,
|
|
||||||
ttl,
|
|
||||||
isPublic,
|
|
||||||
metadata,
|
|
||||||
signature,
|
|
||||||
message
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
pc.close();
|
|
||||||
const error = await response.json();
|
|
||||||
throw new Error(error.error || 'Failed to publish service');
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
|
|
||||||
// Send any pending candidates
|
|
||||||
if (pendingCandidates.length > 0) {
|
|
||||||
console.log(`📤 Sending ${pendingCandidates.length} pending ICE candidate(s) for initial service`);
|
|
||||||
try {
|
|
||||||
await this.offersApi.addIceCandidates(data.offerId, pendingCandidates);
|
|
||||||
console.log(`✅ Sent ${pendingCandidates.length} pending ICE candidate(s)`);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('❌ Error sending pending ICE candidates:', err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set up handler for any future candidates
|
|
||||||
pc.onicecandidate = async (event) => {
|
|
||||||
if (event.candidate) {
|
|
||||||
const candidateData = event.candidate.toJSON();
|
|
||||||
if (candidateData.candidate && candidateData.candidate !== '') {
|
|
||||||
const type = candidateData.candidate.includes('typ host') ? 'host' :
|
|
||||||
candidateData.candidate.includes('typ srflx') ? 'srflx' :
|
|
||||||
candidateData.candidate.includes('typ relay') ? 'relay' : 'unknown';
|
|
||||||
console.log(`🧊 Initial service generated late ${type} ICE candidate:`, candidateData.candidate);
|
|
||||||
try {
|
|
||||||
await this.offersApi.addIceCandidates(data.offerId, [candidateData]);
|
|
||||||
console.log(`✅ Sent late ${type} ICE candidate`);
|
|
||||||
} catch (err) {
|
|
||||||
console.error(`❌ Error sending ${type} ICE candidate:`, err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
return {
|
|
||||||
serviceId: data.serviceId,
|
|
||||||
uuid: data.uuid,
|
|
||||||
offerId: data.offerId,
|
|
||||||
offerSdp,
|
|
||||||
expiresAt: data.expiresAt,
|
|
||||||
peerConnection: pc, // Keep peer connection alive
|
|
||||||
dataChannel // Keep data channel alive
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Manually add offers to the pool
|
|
||||||
*/
|
|
||||||
private async manualRefill(count: number): Promise<void> {
|
|
||||||
if (!this.offerPool) {
|
|
||||||
throw new Error('Pool not started');
|
|
||||||
}
|
|
||||||
|
|
||||||
const result = await this.createOffers(count);
|
|
||||||
await this.offerPool.addOffers(result.offers, result.peerConnections, result.dataChannels);
|
|
||||||
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)}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
20
src/types.ts
Normal file
20
src/types.ts
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
/**
|
||||||
|
* Core signaling types
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cleanup function returned by listener methods
|
||||||
|
*/
|
||||||
|
export type Binnable = () => void
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Signaler interface for WebRTC offer/answer/ICE exchange
|
||||||
|
*/
|
||||||
|
export interface Signaler {
|
||||||
|
addIceCandidate(candidate: RTCIceCandidate): Promise<void>
|
||||||
|
addListener(callback: (candidate: RTCIceCandidate) => void): Binnable
|
||||||
|
addOfferListener(callback: (offer: RTCSessionDescriptionInit) => void): Binnable
|
||||||
|
addAnswerListener(callback: (answer: RTCSessionDescriptionInit) => void): Binnable
|
||||||
|
setOffer(offer: RTCSessionDescriptionInit): Promise<void>
|
||||||
|
setAnswer(answer: RTCSessionDescriptionInit): Promise<void>
|
||||||
|
}
|
||||||
200
src/usernames.ts
200
src/usernames.ts
@@ -1,200 +0,0 @@
|
|||||||
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