mirror of
https://github.com/xtr-dev/rondevu-client.git
synced 2025-12-13 20:33:25 +00:00
Compare commits
91 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0aa9921941 | |||
| 5fc20f1be9 | |||
| 54c371f451 | |||
| 5f4743e086 | |||
| 2ce3e98df0 | |||
| 800f6eaa94 | |||
| d6a9876440 | |||
| 9262043e97 | |||
| bd16798a2f | |||
| c662161cd9 | |||
| a9a0127ccf | |||
| 4345709e9c | |||
| 43dfd72c3d | |||
| ec19ce50db | |||
| e5c82b75b1 | |||
| a2c01d530f | |||
| 7223e45b98 | |||
| d55abf2b63 | |||
| 4ce5217135 | |||
| 238cc08bf5 | |||
| f4ae5dee30 | |||
| a499062e52 | |||
| b5f36d8f77 | |||
| 214f611dc2 | |||
| 1112eeefd4 | |||
| 0fe8e82858 | |||
| c9a5e0eae6 | |||
| 239563ac5c | |||
| 3327c5b219 | |||
| b4be5e9060 | |||
| b60799a712 | |||
| 8fbb76a336 | |||
| a3b4dfa15f | |||
| 5c38f8f36c | |||
| 177ee2ec2d | |||
| d06b2166c1 | |||
| cbb0cc3f83 | |||
| fbd3be57d4 | |||
| 54355323d9 | |||
| 945d5a8792 | |||
| 58cd610694 | |||
| 5e673ac993 | |||
| 511bac8033 | |||
| eb2c61bdb8 | |||
| 3139897b25 | |||
| a550641993 | |||
| 04603cfe2d | |||
| 6c2fd7952e | |||
| d6f2a10e3e | |||
| a6dc342f3b | |||
| 9486376442 | |||
| cffb092d3f | |||
| 122f211e7c | |||
| 4a6d0ee091 | |||
| b2d42fa776 | |||
| 63e14ddc5b | |||
| c9f6119148 | |||
| 15f821f08a | |||
| 895e7765f9 | |||
| 49d3984640 | |||
| 6057c3c582 | |||
| 255fe42a43 | |||
| 83fd0f84a4 | |||
| aa53d5bc3d | |||
| f5aa6e2189 | |||
| afdca83640 | |||
| c7ea1b9b8f | |||
| 660663945e | |||
| f119a42fcd | |||
| cd55072acb | |||
| 26f71e7a2b | |||
| 0ac1f94502 | |||
| 3530213870 | |||
| e052464482 | |||
| 53206d306b | |||
| c860419e66 | |||
| e22e74fb74 | |||
| 135eda73cf | |||
| 8d7075ccc4 | |||
| db8f0f4ced | |||
| 3a227a21ac | |||
| de1f3eac9c | |||
| 557cc0a838 | |||
| 6e661f69bc | |||
| 00f4da7250 | |||
| 6c344ec8e1 | |||
| 5a5da124a6 | |||
| c8b7a2913f | |||
| 6ddf7cb7f0 | |||
| 35ce051a26 | |||
| 280c8c284f |
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"
|
||||||
|
}
|
||||||
486
ADVANCED.md
Normal file
486
ADVANCED.md
Normal file
@@ -0,0 +1,486 @@
|
|||||||
|
# Rondevu Client - Advanced Usage
|
||||||
|
|
||||||
|
Comprehensive guide for advanced features, platform support, and detailed API reference.
|
||||||
|
|
||||||
|
## Table of Contents
|
||||||
|
|
||||||
|
- [API Reference](#api-reference)
|
||||||
|
- [Types](#types)
|
||||||
|
- [Advanced Usage](#advanced-usage)
|
||||||
|
- [Platform Support](#platform-support)
|
||||||
|
- [Username Rules](#username-rules)
|
||||||
|
- [Service FQN Format](#service-fqn-format)
|
||||||
|
- [Examples](#examples)
|
||||||
|
- [Migration Guide](#migration-guide)
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## API Reference
|
||||||
|
|
||||||
|
### Rondevu Class
|
||||||
|
|
||||||
|
Main class for all Rondevu operations.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Rondevu } from '@xtr-dev/rondevu-client'
|
||||||
|
|
||||||
|
// Create and connect to Rondevu
|
||||||
|
const rondevu = await Rondevu.connect({
|
||||||
|
apiUrl: string, // Signaling server URL
|
||||||
|
username?: string, // Optional: your username (auto-generates anonymous if omitted)
|
||||||
|
keypair?: Keypair, // Optional: reuse existing keypair
|
||||||
|
cryptoAdapter?: CryptoAdapter // Optional: platform-specific crypto (defaults to WebCryptoAdapter)
|
||||||
|
batching?: BatcherOptions | false // Optional: RPC batching configuration
|
||||||
|
iceServers?: IceServerPreset | RTCIceServer[] // Optional: preset name or custom STUN/TURN servers
|
||||||
|
debug?: boolean // Optional: enable debug logging (default: false)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Platform Support (Browser & Node.js)
|
||||||
|
|
||||||
|
The client supports both browser and Node.js environments using crypto adapters:
|
||||||
|
|
||||||
|
**Browser (default):**
|
||||||
|
```typescript
|
||||||
|
import { Rondevu } from '@xtr-dev/rondevu-client'
|
||||||
|
|
||||||
|
// WebCryptoAdapter is used by default - no configuration needed
|
||||||
|
const rondevu = await Rondevu.connect({
|
||||||
|
apiUrl: 'https://api.ronde.vu',
|
||||||
|
username: 'alice'
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
**Node.js (19+ or 18 with --experimental-global-webcrypto):**
|
||||||
|
```typescript
|
||||||
|
import { Rondevu, NodeCryptoAdapter } from '@xtr-dev/rondevu-client'
|
||||||
|
|
||||||
|
const rondevu = await Rondevu.connect({
|
||||||
|
apiUrl: 'https://api.ronde.vu',
|
||||||
|
username: 'alice',
|
||||||
|
cryptoAdapter: new NodeCryptoAdapter()
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
**Note:** Node.js support requires:
|
||||||
|
- Node.js 19+ (crypto.subtle available globally), OR
|
||||||
|
- Node.js 18 with `--experimental-global-webcrypto` flag
|
||||||
|
- WebRTC implementation like `wrtc` or `node-webrtc` for RTCPeerConnection
|
||||||
|
|
||||||
|
**Custom Crypto Adapter:**
|
||||||
|
```typescript
|
||||||
|
import { CryptoAdapter, Keypair } from '@xtr-dev/rondevu-client'
|
||||||
|
|
||||||
|
class CustomCryptoAdapter implements CryptoAdapter {
|
||||||
|
async generateKeypair(): Promise<Keypair> { /* ... */ }
|
||||||
|
async signMessage(message: string, privateKey: string): Promise<string> { /* ... */ }
|
||||||
|
async verifySignature(message: string, signature: string, publicKey: string): Promise<boolean> { /* ... */ }
|
||||||
|
bytesToBase64(bytes: Uint8Array): string { /* ... */ }
|
||||||
|
base64ToBytes(base64: string): Uint8Array { /* ... */ }
|
||||||
|
randomBytes(length: number): Uint8Array { /* ... */ }
|
||||||
|
}
|
||||||
|
|
||||||
|
const rondevu = await Rondevu.connect({
|
||||||
|
apiUrl: 'https://api.ronde.vu',
|
||||||
|
cryptoAdapter: new CustomCryptoAdapter()
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Username Management
|
||||||
|
|
||||||
|
Usernames are **automatically claimed** on the first authenticated request (like `publishService()`).
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Check if username is claimed (checks server)
|
||||||
|
await rondevu.isUsernameClaimed(): Promise<boolean>
|
||||||
|
|
||||||
|
// Get username
|
||||||
|
rondevu.getUsername(): string
|
||||||
|
|
||||||
|
// Get public key
|
||||||
|
rondevu.getPublicKey(): string
|
||||||
|
|
||||||
|
// Get keypair (for backup/storage)
|
||||||
|
rondevu.getKeypair(): Keypair
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Service Publishing
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Publish service with offers
|
||||||
|
await rondevu.publishService({
|
||||||
|
service: string, // e.g., 'chat:1.0.0' (username auto-appended)
|
||||||
|
maxOffers: number, // Maximum number of concurrent offers to maintain
|
||||||
|
offerFactory?: OfferFactory, // Optional: custom offer creation (defaults to simple data channel)
|
||||||
|
ttl?: number // Optional: milliseconds (default: 300000)
|
||||||
|
}): Promise<void>
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Service Discovery
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Direct lookup by FQN (with username)
|
||||||
|
await rondevu.getService('chat:1.0.0@alice'): Promise<ServiceOffer>
|
||||||
|
|
||||||
|
// Random discovery (without username)
|
||||||
|
await rondevu.discoverService('chat:1.0.0'): Promise<ServiceOffer>
|
||||||
|
|
||||||
|
// Paginated discovery (returns multiple offers)
|
||||||
|
await rondevu.discoverServices(
|
||||||
|
'chat:1.0.0', // serviceVersion
|
||||||
|
10, // limit
|
||||||
|
0 // offset
|
||||||
|
): Promise<{ services: ServiceOffer[], count: number, limit: number, offset: number }>
|
||||||
|
```
|
||||||
|
|
||||||
|
#### WebRTC Signaling
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Post answer SDP
|
||||||
|
await rondevu.postOfferAnswer(
|
||||||
|
serviceFqn: string,
|
||||||
|
offerId: string,
|
||||||
|
sdp: string
|
||||||
|
): Promise<{ success: boolean, offerId: string }>
|
||||||
|
|
||||||
|
// Get answer SDP (offerer polls this - deprecated, use pollOffers instead)
|
||||||
|
await rondevu.getOfferAnswer(
|
||||||
|
serviceFqn: string,
|
||||||
|
offerId: string
|
||||||
|
): Promise<{ sdp: string, offerId: string, answererId: string, answeredAt: number } | null>
|
||||||
|
|
||||||
|
// Combined polling for answers and ICE candidates (RECOMMENDED for offerers)
|
||||||
|
await rondevu.pollOffers(since?: number): Promise<{
|
||||||
|
answers: Array<{
|
||||||
|
offerId: string
|
||||||
|
serviceId?: string
|
||||||
|
answererId: string
|
||||||
|
sdp: string
|
||||||
|
answeredAt: number
|
||||||
|
}>
|
||||||
|
iceCandidates: Record<string, Array<{
|
||||||
|
candidate: RTCIceCandidateInit | null
|
||||||
|
role: 'offerer' | 'answerer'
|
||||||
|
peerId: string
|
||||||
|
createdAt: number
|
||||||
|
}>>
|
||||||
|
}>
|
||||||
|
|
||||||
|
// Add ICE candidates
|
||||||
|
await rondevu.addOfferIceCandidates(
|
||||||
|
serviceFqn: string,
|
||||||
|
offerId: string,
|
||||||
|
candidates: RTCIceCandidateInit[]
|
||||||
|
): Promise<{ count: number, offerId: string }>
|
||||||
|
|
||||||
|
// Get ICE candidates (with polling support)
|
||||||
|
await rondevu.getOfferIceCandidates(
|
||||||
|
serviceFqn: string,
|
||||||
|
offerId: string,
|
||||||
|
since: number = 0
|
||||||
|
): Promise<{ candidates: IceCandidate[], offerId: string }>
|
||||||
|
```
|
||||||
|
|
||||||
|
### RondevuAPI Class
|
||||||
|
|
||||||
|
Low-level HTTP API client (used internally by Rondevu class).
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { RondevuAPI } from '@xtr-dev/rondevu-client'
|
||||||
|
|
||||||
|
const api = new RondevuAPI(
|
||||||
|
baseUrl: string,
|
||||||
|
username: string,
|
||||||
|
keypair: Keypair
|
||||||
|
)
|
||||||
|
|
||||||
|
// Check username
|
||||||
|
await api.checkUsername(username: string): Promise<{
|
||||||
|
available: boolean
|
||||||
|
publicKey?: string
|
||||||
|
claimedAt?: number
|
||||||
|
expiresAt?: number
|
||||||
|
}>
|
||||||
|
|
||||||
|
// Note: Username claiming is now implicit - usernames are auto-claimed
|
||||||
|
// on first authenticated request to the server
|
||||||
|
|
||||||
|
// ... (all other HTTP endpoints)
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Cryptographic Helpers
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Generate Ed25519 keypair
|
||||||
|
const keypair = await RondevuAPI.generateKeypair(): Promise<Keypair>
|
||||||
|
|
||||||
|
// Sign message
|
||||||
|
const signature = await RondevuAPI.signMessage(
|
||||||
|
message: string,
|
||||||
|
privateKey: string
|
||||||
|
): Promise<string>
|
||||||
|
|
||||||
|
// Verify signature
|
||||||
|
const valid = await RondevuAPI.verifySignature(
|
||||||
|
message: string,
|
||||||
|
signature: string,
|
||||||
|
publicKey: string
|
||||||
|
): Promise<boolean>
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Types
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
interface Keypair {
|
||||||
|
publicKey: string // Base64-encoded Ed25519 public key
|
||||||
|
privateKey: string // Base64-encoded Ed25519 private key
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Service {
|
||||||
|
serviceId: string
|
||||||
|
offers: ServiceOffer[]
|
||||||
|
username: string
|
||||||
|
serviceFqn: string
|
||||||
|
createdAt: number
|
||||||
|
expiresAt: number
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ServiceOffer {
|
||||||
|
offerId: string
|
||||||
|
sdp: string
|
||||||
|
createdAt: number
|
||||||
|
expiresAt: number
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IceCandidate {
|
||||||
|
candidate: RTCIceCandidateInit | null
|
||||||
|
createdAt: number
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Advanced Usage
|
||||||
|
|
||||||
|
### Anonymous Username
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Auto-generate anonymous username (format: anon-{timestamp}-{random})
|
||||||
|
const rondevu = await Rondevu.connect({
|
||||||
|
apiUrl: 'https://api.ronde.vu'
|
||||||
|
// No username provided - will generate anonymous username
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log(rondevu.getUsername()) // e.g., "anon-lx2w34-a3f501"
|
||||||
|
|
||||||
|
// Anonymous users behave exactly like regular users
|
||||||
|
await rondevu.publishService({
|
||||||
|
service: 'chat:1.0.0',
|
||||||
|
maxOffers: 5
|
||||||
|
})
|
||||||
|
|
||||||
|
await rondevu.startFilling()
|
||||||
|
```
|
||||||
|
|
||||||
|
### Persistent Keypair
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Save keypair and username to localStorage
|
||||||
|
const rondevu = await Rondevu.connect({
|
||||||
|
apiUrl: 'https://api.ronde.vu',
|
||||||
|
username: 'alice'
|
||||||
|
})
|
||||||
|
|
||||||
|
// Save for later (username will be auto-claimed on first authenticated request)
|
||||||
|
localStorage.setItem('rondevu-username', rondevu.getUsername())
|
||||||
|
localStorage.setItem('rondevu-keypair', JSON.stringify(rondevu.getKeypair()))
|
||||||
|
|
||||||
|
// Load on next session
|
||||||
|
const savedUsername = localStorage.getItem('rondevu-username')
|
||||||
|
const savedKeypair = JSON.parse(localStorage.getItem('rondevu-keypair'))
|
||||||
|
|
||||||
|
const rondevu2 = await Rondevu.connect({
|
||||||
|
apiUrl: 'https://api.ronde.vu',
|
||||||
|
username: savedUsername,
|
||||||
|
keypair: savedKeypair
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### Service Discovery
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Get a random available service
|
||||||
|
const service = await rondevu.discoverService('chat:1.0.0')
|
||||||
|
console.log('Discovered:', service.username)
|
||||||
|
|
||||||
|
// Get multiple services (paginated)
|
||||||
|
const result = await rondevu.discoverServices('chat:1.0.0', 10, 0)
|
||||||
|
console.log(`Found ${result.count} services:`)
|
||||||
|
result.services.forEach(s => console.log(` - ${s.username}`))
|
||||||
|
```
|
||||||
|
|
||||||
|
### Multiple Concurrent Offers
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Publish service with multiple offers for connection pooling
|
||||||
|
const offers = []
|
||||||
|
const connections = []
|
||||||
|
|
||||||
|
for (let i = 0; i < 5; i++) {
|
||||||
|
const pc = new RTCPeerConnection(rtcConfig)
|
||||||
|
const dc = pc.createDataChannel('chat')
|
||||||
|
const offer = await pc.createOffer()
|
||||||
|
await pc.setLocalDescription(offer)
|
||||||
|
|
||||||
|
offers.push({ sdp: offer.sdp })
|
||||||
|
connections.push({ pc, dc })
|
||||||
|
}
|
||||||
|
|
||||||
|
const service = await rondevu.publishService({
|
||||||
|
service: 'chat:1.0.0',
|
||||||
|
offers,
|
||||||
|
ttl: 300000
|
||||||
|
})
|
||||||
|
|
||||||
|
// Each offer can be answered independently
|
||||||
|
console.log(`Published ${service.offers.length} offers`)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Debug Logging
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Enable debug logging to see internal operations
|
||||||
|
const rondevu = await Rondevu.connect({
|
||||||
|
apiUrl: 'https://api.ronde.vu',
|
||||||
|
username: 'alice',
|
||||||
|
debug: true // All internal logs will be displayed with [Rondevu] prefix
|
||||||
|
})
|
||||||
|
|
||||||
|
// Debug logs include:
|
||||||
|
// - Connection establishment
|
||||||
|
// - Keypair generation
|
||||||
|
// - Service publishing
|
||||||
|
// - Offer creation
|
||||||
|
// - ICE candidate exchange
|
||||||
|
// - Connection state changes
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Platform Support
|
||||||
|
|
||||||
|
### Modern Browsers
|
||||||
|
Works out of the box - no additional setup needed.
|
||||||
|
|
||||||
|
### Node.js 18+
|
||||||
|
Native fetch is available, but WebRTC requires polyfills:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm install wrtc
|
||||||
|
```
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { RTCPeerConnection, RTCSessionDescription, RTCIceCandidate } from 'wrtc'
|
||||||
|
|
||||||
|
// Use wrtc implementations
|
||||||
|
const pc = new RTCPeerConnection()
|
||||||
|
```
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 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
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Service FQN Format
|
||||||
|
|
||||||
|
- **Format**: `service:version@username`
|
||||||
|
- **Service**: Lowercase alphanumeric + dash (e.g., `chat`, `video-call`)
|
||||||
|
- **Version**: Semantic versioning (e.g., `1.0.0`, `2.1.3`)
|
||||||
|
- **Username**: Claimed username
|
||||||
|
- **Example**: `chat:1.0.0@alice`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
### Node.js Service Host Example
|
||||||
|
|
||||||
|
You can host WebRTC services in Node.js that browser clients can connect to:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import { Rondevu, NodeCryptoAdapter } from '@xtr-dev/rondevu-client'
|
||||||
|
import wrtc from 'wrtc'
|
||||||
|
|
||||||
|
const { RTCPeerConnection } = wrtc
|
||||||
|
|
||||||
|
// Initialize with Node crypto adapter
|
||||||
|
const rondevu = await Rondevu.connect({
|
||||||
|
apiUrl: 'https://api.ronde.vu',
|
||||||
|
username: 'mybot',
|
||||||
|
cryptoAdapter: new NodeCryptoAdapter()
|
||||||
|
})
|
||||||
|
|
||||||
|
// Create peer connection (offerer creates data channel)
|
||||||
|
const pc = new RTCPeerConnection(rtcConfig)
|
||||||
|
const dc = pc.createDataChannel('chat')
|
||||||
|
|
||||||
|
// Publish service (username auto-claimed on first publish)
|
||||||
|
await rondevu.publishService({
|
||||||
|
service: 'chat:1.0.0',
|
||||||
|
maxOffers: 5
|
||||||
|
})
|
||||||
|
|
||||||
|
await rondevu.startFilling()
|
||||||
|
|
||||||
|
// Browser clients can now discover and connect to chat:1.0.0@mybot
|
||||||
|
```
|
||||||
|
|
||||||
|
**See also:**
|
||||||
|
- [React Demo](https://github.com/xtr-dev/rondevu-demo) - Complete browser UI ([live](https://ronde.vu))
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Migration Guide
|
||||||
|
|
||||||
|
### Migration from v0.3.x
|
||||||
|
|
||||||
|
v0.4.0 removes high-level abstractions and uses manual WebRTC setup:
|
||||||
|
|
||||||
|
**Removed:**
|
||||||
|
- `ServiceHost` class (use manual WebRTC + `publishService()`)
|
||||||
|
- `ServiceClient` class (use manual WebRTC + `getService()`)
|
||||||
|
- `RTCDurableConnection` class (use native WebRTC APIs)
|
||||||
|
- `RondevuService` class (merged into `Rondevu`)
|
||||||
|
|
||||||
|
**Added:**
|
||||||
|
- `pollOffers()` - Combined polling for answers and ICE candidates
|
||||||
|
- `publishService()` - Automatic offer pool management
|
||||||
|
- `connectToService()` - Automatic answering side setup
|
||||||
|
|
||||||
|
**Migration Example:**
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// Before (v0.3.x) - ServiceHost
|
||||||
|
const host = new ServiceHost({
|
||||||
|
service: 'chat@1.0.0',
|
||||||
|
rondevuService: service
|
||||||
|
})
|
||||||
|
await host.start()
|
||||||
|
|
||||||
|
// After (v0.4.0+) - Automatic setup
|
||||||
|
await rondevu.publishService({
|
||||||
|
service: 'chat:1.0.0',
|
||||||
|
maxOffers: 5
|
||||||
|
})
|
||||||
|
|
||||||
|
await rondevu.startFilling()
|
||||||
|
```
|
||||||
547
MIGRATION.md
Normal file
547
MIGRATION.md
Normal file
@@ -0,0 +1,547 @@
|
|||||||
|
# Migration Guide: v0.8.x → v0.9.0
|
||||||
|
|
||||||
|
This guide helps you migrate from Rondevu Client v0.8.x to v0.9.0.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
v0.9.0 is a **breaking change** that completely replaces low-level APIs with high-level durable connections featuring automatic reconnection and message queuing.
|
||||||
|
|
||||||
|
### What's New
|
||||||
|
|
||||||
|
✅ **Durable Connections**: Automatic reconnection on network drops
|
||||||
|
✅ **Message Queuing**: Messages sent during disconnections are queued and flushed on reconnect
|
||||||
|
✅ **Durable Channels**: RTCDataChannel wrappers that survive connection drops
|
||||||
|
✅ **TTL Auto-Refresh**: Services automatically republish before expiration
|
||||||
|
✅ **Simplified API**: Direct methods on main client instead of nested APIs
|
||||||
|
|
||||||
|
### What's Removed
|
||||||
|
|
||||||
|
❌ **Low-level APIs**: `client.services.*`, `client.discovery.*`, `client.createPeer()` no longer exported
|
||||||
|
❌ **Manual Connection Management**: No need to handle WebRTC peer lifecycle manually
|
||||||
|
❌ **Service Handles**: Replaced with DurableService instances
|
||||||
|
|
||||||
|
## Breaking Changes
|
||||||
|
|
||||||
|
### 1. Service Exposure
|
||||||
|
|
||||||
|
#### v0.8.x (Old)
|
||||||
|
```typescript
|
||||||
|
import { Rondevu } from '@xtr-dev/rondevu-client';
|
||||||
|
|
||||||
|
const client = new Rondevu();
|
||||||
|
await client.register();
|
||||||
|
|
||||||
|
const handle = await client.services.exposeService({
|
||||||
|
username: 'alice',
|
||||||
|
privateKey: keypair.privateKey,
|
||||||
|
serviceFqn: 'chat@1.0.0',
|
||||||
|
isPublic: true,
|
||||||
|
handler: (channel, peer) => {
|
||||||
|
channel.onmessage = (e) => {
|
||||||
|
console.log('Received:', e.data);
|
||||||
|
channel.send(`Echo: ${e.data}`);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Unpublish
|
||||||
|
await handle.unpublish();
|
||||||
|
```
|
||||||
|
|
||||||
|
#### v0.9.0 (New)
|
||||||
|
```typescript
|
||||||
|
import { Rondevu } from '@xtr-dev/rondevu-client';
|
||||||
|
|
||||||
|
const client = new Rondevu();
|
||||||
|
await client.register();
|
||||||
|
|
||||||
|
const service = await client.exposeService({
|
||||||
|
username: 'alice',
|
||||||
|
privateKey: keypair.privateKey,
|
||||||
|
serviceFqn: 'chat@1.0.0',
|
||||||
|
isPublic: true,
|
||||||
|
poolSize: 10, // NEW: Handle multiple concurrent connections
|
||||||
|
handler: (channel, connectionId) => {
|
||||||
|
// NEW: DurableChannel with event emitters
|
||||||
|
channel.on('message', (data) => {
|
||||||
|
console.log('Received:', data);
|
||||||
|
channel.send(`Echo: ${data}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// NEW: Start the service
|
||||||
|
await service.start();
|
||||||
|
|
||||||
|
// NEW: Stop the service
|
||||||
|
await service.stop();
|
||||||
|
```
|
||||||
|
|
||||||
|
**Key Differences:**
|
||||||
|
- `client.services.exposeService()` → `client.exposeService()`
|
||||||
|
- Returns `DurableService` instead of `ServiceHandle`
|
||||||
|
- Handler receives `DurableChannel` instead of `RTCDataChannel`
|
||||||
|
- Handler receives `connectionId` string instead of `RondevuPeer`
|
||||||
|
- DurableChannel uses `.on('message', ...)` instead of `.onmessage = ...`
|
||||||
|
- Must call `service.start()` to begin accepting connections
|
||||||
|
- Use `service.stop()` instead of `handle.unpublish()`
|
||||||
|
|
||||||
|
### 2. Connecting to Services
|
||||||
|
|
||||||
|
#### v0.8.x (Old)
|
||||||
|
```typescript
|
||||||
|
// Connect by username + FQN
|
||||||
|
const { peer, channel } = await client.discovery.connect(
|
||||||
|
'alice',
|
||||||
|
'chat@1.0.0'
|
||||||
|
);
|
||||||
|
|
||||||
|
channel.onmessage = (e) => {
|
||||||
|
console.log('Received:', e.data);
|
||||||
|
};
|
||||||
|
|
||||||
|
channel.onopen = () => {
|
||||||
|
channel.send('Hello!');
|
||||||
|
};
|
||||||
|
|
||||||
|
peer.on('connected', () => {
|
||||||
|
console.log('Connected');
|
||||||
|
});
|
||||||
|
|
||||||
|
peer.on('failed', (error) => {
|
||||||
|
console.error('Failed:', error);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
#### v0.9.0 (New)
|
||||||
|
```typescript
|
||||||
|
// Connect by username + FQN
|
||||||
|
const connection = await client.connect('alice', 'chat@1.0.0', {
|
||||||
|
maxReconnectAttempts: 10 // NEW: Configurable reconnection
|
||||||
|
});
|
||||||
|
|
||||||
|
// NEW: Create durable channel
|
||||||
|
const channel = connection.createChannel('main');
|
||||||
|
|
||||||
|
channel.on('message', (data) => {
|
||||||
|
console.log('Received:', data);
|
||||||
|
});
|
||||||
|
|
||||||
|
channel.on('open', () => {
|
||||||
|
channel.send('Hello!');
|
||||||
|
});
|
||||||
|
|
||||||
|
// NEW: Connection lifecycle events
|
||||||
|
connection.on('connected', () => {
|
||||||
|
console.log('Connected');
|
||||||
|
});
|
||||||
|
|
||||||
|
connection.on('reconnecting', (attempt, max, delay) => {
|
||||||
|
console.log(`Reconnecting (${attempt}/${max})...`);
|
||||||
|
});
|
||||||
|
|
||||||
|
connection.on('failed', (error) => {
|
||||||
|
console.error('Failed permanently:', error);
|
||||||
|
});
|
||||||
|
|
||||||
|
// NEW: Must explicitly connect
|
||||||
|
await connection.connect();
|
||||||
|
```
|
||||||
|
|
||||||
|
**Key Differences:**
|
||||||
|
- `client.discovery.connect()` → `client.connect()`
|
||||||
|
- Returns `DurableConnection` instead of `{ peer, channel }`
|
||||||
|
- Must create channels with `connection.createChannel()`
|
||||||
|
- Must call `connection.connect()` to establish connection
|
||||||
|
- Automatic reconnection with configurable retry limits
|
||||||
|
- Messages sent during disconnection are automatically queued
|
||||||
|
|
||||||
|
### 3. Connecting by UUID
|
||||||
|
|
||||||
|
#### v0.8.x (Old)
|
||||||
|
```typescript
|
||||||
|
const { peer, channel } = await client.discovery.connectByUuid('service-uuid');
|
||||||
|
|
||||||
|
channel.onmessage = (e) => {
|
||||||
|
console.log('Received:', e.data);
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
#### v0.9.0 (New)
|
||||||
|
```typescript
|
||||||
|
const connection = await client.connectByUuid('service-uuid', {
|
||||||
|
maxReconnectAttempts: 5
|
||||||
|
});
|
||||||
|
|
||||||
|
const channel = connection.createChannel('main');
|
||||||
|
|
||||||
|
channel.on('message', (data) => {
|
||||||
|
console.log('Received:', data);
|
||||||
|
});
|
||||||
|
|
||||||
|
await connection.connect();
|
||||||
|
```
|
||||||
|
|
||||||
|
**Key Differences:**
|
||||||
|
- `client.discovery.connectByUuid()` → `client.connectByUuid()`
|
||||||
|
- Returns `DurableConnection` instead of `{ peer, channel }`
|
||||||
|
- Must create channels and connect explicitly
|
||||||
|
|
||||||
|
### 4. Multi-Connection Services (Offer Pooling)
|
||||||
|
|
||||||
|
#### v0.8.x (Old)
|
||||||
|
```typescript
|
||||||
|
const handle = await client.services.exposeService({
|
||||||
|
username: 'alice',
|
||||||
|
privateKey: keypair.privateKey,
|
||||||
|
serviceFqn: 'chat@1.0.0',
|
||||||
|
poolSize: 5,
|
||||||
|
pollingInterval: 2000,
|
||||||
|
handler: (channel, peer, connectionId) => {
|
||||||
|
console.log(`Connection: ${connectionId}`);
|
||||||
|
},
|
||||||
|
onPoolStatus: (status) => {
|
||||||
|
console.log('Pool status:', status);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const status = handle.getStatus();
|
||||||
|
await handle.addOffers(3);
|
||||||
|
```
|
||||||
|
|
||||||
|
#### v0.9.0 (New)
|
||||||
|
```typescript
|
||||||
|
const service = await client.exposeService({
|
||||||
|
username: 'alice',
|
||||||
|
privateKey: keypair.privateKey,
|
||||||
|
serviceFqn: 'chat@1.0.0',
|
||||||
|
poolSize: 5, // SAME: Pool size
|
||||||
|
pollingInterval: 2000, // SAME: Polling interval
|
||||||
|
handler: (channel, connectionId) => {
|
||||||
|
console.log(`Connection: ${connectionId}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await service.start();
|
||||||
|
|
||||||
|
// Get active connections
|
||||||
|
const connections = service.getActiveConnections();
|
||||||
|
|
||||||
|
// Listen for connection events
|
||||||
|
service.on('connection', (connectionId) => {
|
||||||
|
console.log('New connection:', connectionId);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
**Key Differences:**
|
||||||
|
- `onPoolStatus` callback removed (use `service.on('connection')` instead)
|
||||||
|
- `handle.getStatus()` replaced with `service.getActiveConnections()`
|
||||||
|
- `handle.addOffers()` removed (pool auto-manages offers)
|
||||||
|
- Handler receives `DurableChannel` instead of `RTCDataChannel`
|
||||||
|
|
||||||
|
## Feature Comparison
|
||||||
|
|
||||||
|
| Feature | v0.8.x | v0.9.0 |
|
||||||
|
|---------|--------|--------|
|
||||||
|
| Service exposure | `client.services.exposeService()` | `client.exposeService()` |
|
||||||
|
| Connection | `client.discovery.connect()` | `client.connect()` |
|
||||||
|
| Connection by UUID | `client.discovery.connectByUuid()` | `client.connectByUuid()` |
|
||||||
|
| Channel type | `RTCDataChannel` | `DurableChannel` |
|
||||||
|
| Event handling | `.onmessage`, `.onopen`, etc. | `.on('message')`, `.on('open')`, etc. |
|
||||||
|
| Automatic reconnection | ❌ No | ✅ Yes (configurable) |
|
||||||
|
| Message queuing | ❌ No | ✅ Yes (during disconnections) |
|
||||||
|
| TTL auto-refresh | ❌ No | ✅ Yes (configurable) |
|
||||||
|
| Peer lifecycle | Manual | Automatic |
|
||||||
|
| Connection pooling | ✅ Yes | ✅ Yes (same API) |
|
||||||
|
|
||||||
|
## API Mapping
|
||||||
|
|
||||||
|
### Removed Exports
|
||||||
|
|
||||||
|
These are no longer exported in v0.9.0:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ❌ Removed
|
||||||
|
import {
|
||||||
|
RondevuServices,
|
||||||
|
RondevuDiscovery,
|
||||||
|
RondevuPeer,
|
||||||
|
ServiceHandle,
|
||||||
|
PooledServiceHandle,
|
||||||
|
ConnectResult
|
||||||
|
} from '@xtr-dev/rondevu-client';
|
||||||
|
```
|
||||||
|
|
||||||
|
### New Exports
|
||||||
|
|
||||||
|
These are new in v0.9.0:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ New
|
||||||
|
import {
|
||||||
|
DurableConnection,
|
||||||
|
DurableChannel,
|
||||||
|
DurableService,
|
||||||
|
DurableConnectionState,
|
||||||
|
DurableChannelState,
|
||||||
|
DurableConnectionConfig,
|
||||||
|
DurableChannelConfig,
|
||||||
|
DurableServiceConfig,
|
||||||
|
DurableConnectionEvents,
|
||||||
|
DurableChannelEvents,
|
||||||
|
DurableServiceEvents,
|
||||||
|
ConnectionInfo,
|
||||||
|
ServiceInfo,
|
||||||
|
QueuedMessage
|
||||||
|
} from '@xtr-dev/rondevu-client';
|
||||||
|
```
|
||||||
|
|
||||||
|
### Unchanged Exports
|
||||||
|
|
||||||
|
These work the same in both versions:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
// ✅ Unchanged
|
||||||
|
import {
|
||||||
|
Rondevu,
|
||||||
|
RondevuAuth,
|
||||||
|
RondevuUsername,
|
||||||
|
Credentials,
|
||||||
|
UsernameClaimResult,
|
||||||
|
UsernameCheckResult
|
||||||
|
} from '@xtr-dev/rondevu-client';
|
||||||
|
```
|
||||||
|
|
||||||
|
## Configuration Options
|
||||||
|
|
||||||
|
### New Connection Options
|
||||||
|
|
||||||
|
v0.9.0 adds extensive configuration for automatic reconnection and message queuing:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const connection = await client.connect('alice', 'chat@1.0.0', {
|
||||||
|
// Reconnection
|
||||||
|
maxReconnectAttempts: 10, // default: 10
|
||||||
|
reconnectBackoffBase: 1000, // default: 1000ms
|
||||||
|
reconnectBackoffMax: 30000, // default: 30000ms (30 seconds)
|
||||||
|
reconnectJitter: 0.2, // default: 0.2 (±20%)
|
||||||
|
connectionTimeout: 30000, // default: 30000ms
|
||||||
|
|
||||||
|
// Message queuing
|
||||||
|
maxQueueSize: 1000, // default: 1000 messages
|
||||||
|
maxMessageAge: 60000, // default: 60000ms (1 minute)
|
||||||
|
|
||||||
|
// WebRTC
|
||||||
|
rtcConfig: {
|
||||||
|
iceServers: [...]
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
### New Service Options
|
||||||
|
|
||||||
|
Services can now auto-refresh TTL:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
const service = await client.exposeService({
|
||||||
|
username: 'alice',
|
||||||
|
privateKey: keypair.privateKey,
|
||||||
|
serviceFqn: 'chat@1.0.0',
|
||||||
|
|
||||||
|
// TTL auto-refresh (NEW)
|
||||||
|
ttl: 300000, // default: 300000ms (5 minutes)
|
||||||
|
ttlRefreshMargin: 0.2, // default: 0.2 (refresh at 80% of TTL)
|
||||||
|
|
||||||
|
// All connection options also apply to incoming connections
|
||||||
|
maxReconnectAttempts: 10,
|
||||||
|
maxQueueSize: 1000,
|
||||||
|
// ...
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Migration Checklist
|
||||||
|
|
||||||
|
- [ ] Replace `client.services.exposeService()` with `client.exposeService()`
|
||||||
|
- [ ] Add `await service.start()` after creating service
|
||||||
|
- [ ] Replace `handle.unpublish()` with `service.stop()`
|
||||||
|
- [ ] Replace `client.discovery.connect()` with `client.connect()`
|
||||||
|
- [ ] Replace `client.discovery.connectByUuid()` with `client.connectByUuid()`
|
||||||
|
- [ ] Create channels with `connection.createChannel()` instead of receiving them directly
|
||||||
|
- [ ] Add `await connection.connect()` to establish connection
|
||||||
|
- [ ] Update handlers from `(channel, peer, connectionId?)` to `(channel, connectionId)`
|
||||||
|
- [ ] Replace `.onmessage` with `.on('message', ...)`
|
||||||
|
- [ ] Replace `.onopen` with `.on('open', ...)`
|
||||||
|
- [ ] Replace `.onclose` with `.on('close', ...)`
|
||||||
|
- [ ] Replace `.onerror` with `.on('error', ...)`
|
||||||
|
- [ ] Add reconnection event handlers (`connection.on('reconnecting', ...)`)
|
||||||
|
- [ ] Review and configure reconnection options if needed
|
||||||
|
- [ ] Review and configure message queue limits if needed
|
||||||
|
- [ ] Update TypeScript imports to use new types
|
||||||
|
- [ ] Test automatic reconnection behavior
|
||||||
|
- [ ] Test message queuing during disconnections
|
||||||
|
|
||||||
|
## Common Migration Patterns
|
||||||
|
|
||||||
|
### Pattern 1: Simple Echo Service
|
||||||
|
|
||||||
|
#### Before (v0.8.x)
|
||||||
|
```typescript
|
||||||
|
await client.services.exposeService({
|
||||||
|
username: 'alice',
|
||||||
|
privateKey: keypair.privateKey,
|
||||||
|
serviceFqn: 'echo@1.0.0',
|
||||||
|
handler: (channel) => {
|
||||||
|
channel.onmessage = (e) => {
|
||||||
|
channel.send(`Echo: ${e.data}`);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
#### After (v0.9.0)
|
||||||
|
```typescript
|
||||||
|
const service = await client.exposeService({
|
||||||
|
username: 'alice',
|
||||||
|
privateKey: keypair.privateKey,
|
||||||
|
serviceFqn: 'echo@1.0.0',
|
||||||
|
handler: (channel) => {
|
||||||
|
channel.on('message', (data) => {
|
||||||
|
channel.send(`Echo: ${data}`);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await service.start();
|
||||||
|
```
|
||||||
|
|
||||||
|
### Pattern 2: Connection with Error Handling
|
||||||
|
|
||||||
|
#### Before (v0.8.x)
|
||||||
|
```typescript
|
||||||
|
try {
|
||||||
|
const { peer, channel } = await client.discovery.connect('alice', 'chat@1.0.0');
|
||||||
|
|
||||||
|
channel.onopen = () => {
|
||||||
|
channel.send('Hello!');
|
||||||
|
};
|
||||||
|
|
||||||
|
peer.on('failed', (error) => {
|
||||||
|
console.error('Connection failed:', error);
|
||||||
|
// Manual reconnection logic here
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to connect:', error);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### After (v0.9.0)
|
||||||
|
```typescript
|
||||||
|
const connection = await client.connect('alice', 'chat@1.0.0', {
|
||||||
|
maxReconnectAttempts: 5
|
||||||
|
});
|
||||||
|
|
||||||
|
const channel = connection.createChannel('main');
|
||||||
|
|
||||||
|
channel.on('open', () => {
|
||||||
|
channel.send('Hello!');
|
||||||
|
});
|
||||||
|
|
||||||
|
connection.on('reconnecting', (attempt, max, delay) => {
|
||||||
|
console.log(`Reconnecting (${attempt}/${max}) in ${delay}ms`);
|
||||||
|
});
|
||||||
|
|
||||||
|
connection.on('failed', (error) => {
|
||||||
|
console.error('Connection failed permanently:', error);
|
||||||
|
});
|
||||||
|
|
||||||
|
try {
|
||||||
|
await connection.connect();
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Initial connection failed:', error);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Pattern 3: Multi-User Chat Server
|
||||||
|
|
||||||
|
#### Before (v0.8.x)
|
||||||
|
```typescript
|
||||||
|
const connections = new Map();
|
||||||
|
|
||||||
|
await client.services.exposeService({
|
||||||
|
username: 'alice',
|
||||||
|
privateKey: keypair.privateKey,
|
||||||
|
serviceFqn: 'chat@1.0.0',
|
||||||
|
poolSize: 10,
|
||||||
|
handler: (channel, peer, connectionId) => {
|
||||||
|
connections.set(connectionId, channel);
|
||||||
|
|
||||||
|
channel.onmessage = (e) => {
|
||||||
|
// Broadcast to all
|
||||||
|
for (const [id, ch] of connections) {
|
||||||
|
if (id !== connectionId) {
|
||||||
|
ch.send(e.data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
channel.onclose = () => {
|
||||||
|
connections.delete(connectionId);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
#### After (v0.9.0)
|
||||||
|
```typescript
|
||||||
|
const channels = new Map();
|
||||||
|
|
||||||
|
const service = await client.exposeService({
|
||||||
|
username: 'alice',
|
||||||
|
privateKey: keypair.privateKey,
|
||||||
|
serviceFqn: 'chat@1.0.0',
|
||||||
|
poolSize: 10,
|
||||||
|
handler: (channel, connectionId) => {
|
||||||
|
channels.set(connectionId, channel);
|
||||||
|
|
||||||
|
channel.on('message', (data) => {
|
||||||
|
// Broadcast to all
|
||||||
|
for (const [id, ch] of channels) {
|
||||||
|
if (id !== connectionId) {
|
||||||
|
ch.send(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
channel.on('close', () => {
|
||||||
|
channels.delete(connectionId);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
await service.start();
|
||||||
|
|
||||||
|
// Optional: Track connections
|
||||||
|
service.on('connection', (connectionId) => {
|
||||||
|
console.log(`User ${connectionId} joined`);
|
||||||
|
});
|
||||||
|
|
||||||
|
service.on('disconnection', (connectionId) => {
|
||||||
|
console.log(`User ${connectionId} left`);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
## Benefits of Migration
|
||||||
|
|
||||||
|
1. **Reliability**: Automatic reconnection handles network hiccups transparently
|
||||||
|
2. **Simplicity**: No need to manage WebRTC peer lifecycle manually
|
||||||
|
3. **Durability**: Messages sent during disconnections are queued and delivered when connection restores
|
||||||
|
4. **Uptime**: Services automatically refresh TTL before expiration
|
||||||
|
5. **Type Safety**: Better TypeScript types with DurableChannel event emitters
|
||||||
|
6. **Debugging**: Queue size monitoring, connection state tracking, and detailed events
|
||||||
|
|
||||||
|
## Getting Help
|
||||||
|
|
||||||
|
If you encounter issues during migration:
|
||||||
|
1. Check the [README](./README.md) for complete API documentation
|
||||||
|
2. Review the examples for common patterns
|
||||||
|
3. Open an issue on [GitHub](https://github.com/xtr-dev/rondevu-client/issues)
|
||||||
506
README.md
506
README.md
@@ -1,27 +1,31 @@
|
|||||||
# @xtr-dev/rondevu-client
|
# Rondevu Client
|
||||||
|
|
||||||
[](https://www.npmjs.com/package/@xtr-dev/rondevu-client)
|
[](https://www.npmjs.com/package/@xtr-dev/rondevu-client)
|
||||||
|
|
||||||
🌐 **Topic-based peer discovery and WebRTC signaling client**
|
🌐 **Simple WebRTC signaling client with username-based discovery**
|
||||||
|
|
||||||
TypeScript/JavaScript client for Rondevu, providing topic-based peer discovery, stateless authentication, and complete WebRTC signaling.
|
TypeScript/JavaScript client for Rondevu, providing WebRTC signaling with username claiming, service publishing/discovery, and efficient batch polling.
|
||||||
|
|
||||||
**Related repositories:**
|
**Related repositories:**
|
||||||
- [rondevu-server](https://github.com/xtr-dev/rondevu) - HTTP signaling server
|
- [@xtr-dev/rondevu-client](https://github.com/xtr-dev/rondevu-client) - TypeScript client library ([npm](https://www.npmjs.com/package/@xtr-dev/rondevu-client))
|
||||||
- [rondevu-demo](https://rondevu-demo.pages.dev) - Interactive demo
|
- [@xtr-dev/rondevu-server](https://github.com/xtr-dev/rondevu-server) - HTTP signaling server ([npm](https://www.npmjs.com/package/@xtr-dev/rondevu-server), [live](https://api.ronde.vu))
|
||||||
|
- [@xtr-dev/rondevu-demo](https://github.com/xtr-dev/rondevu-demo) - Interactive demo ([live](https://ronde.vu))
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
- **Topic-Based Discovery**: Find peers by topics (e.g., torrent infohashes)
|
- **Username Claiming**: Secure ownership with Ed25519 signatures
|
||||||
- **Stateless Authentication**: No server-side sessions, portable credentials
|
- **Anonymous Users**: Auto-generated anonymous usernames for quick testing
|
||||||
- **Bloom Filters**: Efficient peer exclusion for repeated discoveries
|
- **Service Publishing**: Publish services with multiple offers for connection pooling
|
||||||
- **Multi-Offer Management**: Create and manage multiple offers per peer
|
- **Service Discovery**: Direct lookup, random discovery, or paginated search
|
||||||
- **Complete WebRTC Signaling**: Full offer/answer and ICE candidate exchange
|
- **Efficient Batch Polling**: Single endpoint for answers and ICE candidates (50% fewer requests)
|
||||||
|
- **Semantic Version Matching**: Compatible version resolution (chat:1.0.0 matches any 1.x.x)
|
||||||
- **TypeScript**: Full type safety and autocomplete
|
- **TypeScript**: Full type safety and autocomplete
|
||||||
|
- **Keypair Management**: Generate or reuse Ed25519 keypairs
|
||||||
|
- **Automatic Signatures**: All authenticated requests signed automatically
|
||||||
|
|
||||||
## Install
|
## Installation
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
npm install @xtr-dev/rondevu-client
|
npm install @xtr-dev/rondevu-client
|
||||||
@@ -29,424 +33,144 @@ npm install @xtr-dev/rondevu-client
|
|||||||
|
|
||||||
## Quick Start
|
## Quick Start
|
||||||
|
|
||||||
The easiest way to use Rondevu is with the high-level `RondevuConnection` class, which handles all WebRTC connection complexity including offer/answer exchange, ICE candidates, and connection lifecycle.
|
### Publishing a Service (Offerer)
|
||||||
|
|
||||||
### Creating an Offer (Peer A)
|
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { Rondevu } from '@xtr-dev/rondevu-client';
|
import { Rondevu } from '@xtr-dev/rondevu-client'
|
||||||
|
|
||||||
const client = new Rondevu({ baseUrl: 'https://api.ronde.vu' });
|
// 1. Connect to Rondevu
|
||||||
await client.register();
|
const rondevu = await Rondevu.connect({
|
||||||
|
apiUrl: 'https://api.ronde.vu',
|
||||||
|
username: 'alice', // Or omit for anonymous username
|
||||||
|
iceServers: 'ipv4-turn' // Preset: 'ipv4-turn', 'hostname-turns', 'google-stun', 'relay-only'
|
||||||
|
})
|
||||||
|
|
||||||
// Create a connection
|
// 2. Publish service with automatic offer management
|
||||||
const conn = client.createConnection();
|
await rondevu.publishService({
|
||||||
|
service: 'chat:1.0.0',
|
||||||
|
maxOffers: 5, // Maintain up to 5 concurrent offers
|
||||||
|
offerFactory: async (rtcConfig) => {
|
||||||
|
const pc = new RTCPeerConnection(rtcConfig)
|
||||||
|
const dc = pc.createDataChannel('chat')
|
||||||
|
|
||||||
// Set up event listeners
|
dc.addEventListener('open', () => {
|
||||||
conn.on('connected', () => {
|
console.log('Connection opened!')
|
||||||
console.log('Connected to peer!');
|
dc.send('Hello from Alice!')
|
||||||
});
|
})
|
||||||
|
|
||||||
conn.on('datachannel', (channel) => {
|
dc.addEventListener('message', (e) => {
|
||||||
console.log('Data channel ready');
|
console.log('Received:', e.data)
|
||||||
|
})
|
||||||
|
|
||||||
channel.onmessage = (event) => {
|
const offer = await pc.createOffer()
|
||||||
console.log('Received:', event.data);
|
await pc.setLocalDescription(offer)
|
||||||
};
|
return { pc, dc, offer }
|
||||||
|
|
||||||
channel.send('Hello from peer A!');
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create offer and advertise on topics
|
|
||||||
const offerId = await conn.createOffer({
|
|
||||||
topics: ['my-app', 'room-123'],
|
|
||||||
ttl: 300000 // 5 minutes
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log('Offer created:', offerId);
|
|
||||||
console.log('Share these topics with peers:', ['my-app', 'room-123']);
|
|
||||||
```
|
|
||||||
|
|
||||||
### Answering an Offer (Peer B)
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { Rondevu } from '@xtr-dev/rondevu-client';
|
|
||||||
|
|
||||||
const client = new Rondevu({ baseUrl: 'https://api.ronde.vu' });
|
|
||||||
await client.register();
|
|
||||||
|
|
||||||
// Discover offers by topic
|
|
||||||
const offers = await client.offers.findByTopic('my-app', { limit: 10 });
|
|
||||||
|
|
||||||
if (offers.length > 0) {
|
|
||||||
const offer = offers[0];
|
|
||||||
|
|
||||||
// Create connection
|
|
||||||
const conn = client.createConnection();
|
|
||||||
|
|
||||||
// Set up event listeners
|
|
||||||
conn.on('connecting', () => {
|
|
||||||
console.log('Connecting...');
|
|
||||||
});
|
|
||||||
|
|
||||||
conn.on('connected', () => {
|
|
||||||
console.log('Connected!');
|
|
||||||
});
|
|
||||||
|
|
||||||
conn.on('datachannel', (channel) => {
|
|
||||||
console.log('Data channel ready');
|
|
||||||
|
|
||||||
channel.onmessage = (event) => {
|
|
||||||
console.log('Received:', event.data);
|
|
||||||
};
|
|
||||||
|
|
||||||
channel.send('Hello from peer B!');
|
|
||||||
});
|
|
||||||
|
|
||||||
// Answer the offer
|
|
||||||
await conn.answer(offer.id, offer.sdp);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
### Connection Events
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
conn.on('connecting', () => {
|
|
||||||
// Connection is being established
|
|
||||||
});
|
|
||||||
|
|
||||||
conn.on('connected', () => {
|
|
||||||
// Connection established successfully
|
|
||||||
});
|
|
||||||
|
|
||||||
conn.on('disconnected', () => {
|
|
||||||
// Connection lost or closed
|
|
||||||
});
|
|
||||||
|
|
||||||
conn.on('error', (error) => {
|
|
||||||
// An error occurred
|
|
||||||
console.error('Connection error:', error);
|
|
||||||
});
|
|
||||||
|
|
||||||
conn.on('datachannel', (channel) => {
|
|
||||||
// Data channel is ready to use
|
|
||||||
});
|
|
||||||
|
|
||||||
conn.on('track', (event) => {
|
|
||||||
// Media track received (for audio/video streaming)
|
|
||||||
const stream = event.streams[0];
|
|
||||||
videoElement.srcObject = stream;
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
### Adding Media Tracks
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
// Get user's camera/microphone
|
|
||||||
const stream = await navigator.mediaDevices.getUserMedia({
|
|
||||||
video: true,
|
|
||||||
audio: true
|
|
||||||
});
|
|
||||||
|
|
||||||
// Add tracks to connection
|
|
||||||
stream.getTracks().forEach(track => {
|
|
||||||
conn.addTrack(track, stream);
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
### Connection Properties
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
// Get connection state
|
|
||||||
console.log(conn.connectionState); // 'connecting', 'connected', 'disconnected', etc.
|
|
||||||
|
|
||||||
// Get offer ID
|
|
||||||
console.log(conn.id);
|
|
||||||
|
|
||||||
// Get data channel
|
|
||||||
console.log(conn.channel);
|
|
||||||
```
|
|
||||||
|
|
||||||
### Closing a Connection
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
conn.close();
|
|
||||||
```
|
|
||||||
|
|
||||||
## Platform-Specific Setup
|
|
||||||
|
|
||||||
### Node.js 18+ (with native fetch)
|
|
||||||
|
|
||||||
Works out of the box - no additional setup needed.
|
|
||||||
|
|
||||||
### Node.js < 18 (without native fetch)
|
|
||||||
|
|
||||||
Install node-fetch and provide it to the client:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
npm install node-fetch
|
|
||||||
```
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { Rondevu } from '@xtr-dev/rondevu-client';
|
|
||||||
import fetch from 'node-fetch';
|
|
||||||
|
|
||||||
const client = new Rondevu({
|
|
||||||
baseUrl: 'https://api.ronde.vu',
|
|
||||||
fetch: fetch as any
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
### Deno
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { Rondevu } from 'npm:@xtr-dev/rondevu-client';
|
|
||||||
|
|
||||||
const client = new Rondevu({
|
|
||||||
baseUrl: 'https://api.ronde.vu'
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
### Bun
|
|
||||||
|
|
||||||
Works out of the box - no additional setup needed.
|
|
||||||
|
|
||||||
### Cloudflare Workers
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { Rondevu } from '@xtr-dev/rondevu-client';
|
|
||||||
|
|
||||||
export default {
|
|
||||||
async fetch(request: Request, env: Env) {
|
|
||||||
const client = new Rondevu({
|
|
||||||
baseUrl: 'https://api.ronde.vu'
|
|
||||||
});
|
|
||||||
|
|
||||||
const creds = await client.register();
|
|
||||||
return new Response(JSON.stringify(creds));
|
|
||||||
}
|
}
|
||||||
};
|
})
|
||||||
|
|
||||||
|
// 3. Start accepting connections
|
||||||
|
await rondevu.startFilling()
|
||||||
```
|
```
|
||||||
|
|
||||||
## Low-Level API Usage
|
### Connecting to a Service (Answerer)
|
||||||
|
|
||||||
For advanced use cases where you need direct control over the signaling process, you can use the low-level API:
|
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
import { Rondevu, BloomFilter } from '@xtr-dev/rondevu-client';
|
import { Rondevu } from '@xtr-dev/rondevu-client'
|
||||||
|
|
||||||
const client = new Rondevu({ baseUrl: 'https://api.ronde.vu' });
|
// 1. Connect to Rondevu
|
||||||
|
const rondevu = await Rondevu.connect({
|
||||||
|
apiUrl: 'https://api.ronde.vu',
|
||||||
|
username: 'bob',
|
||||||
|
iceServers: 'ipv4-turn'
|
||||||
|
})
|
||||||
|
|
||||||
// Register and get credentials
|
// 2. Connect to service (automatic WebRTC setup)
|
||||||
const creds = await client.register();
|
const connection = await rondevu.connectToService({
|
||||||
console.log('Peer ID:', creds.peerId);
|
serviceFqn: 'chat:1.0.0@alice',
|
||||||
|
onConnection: ({ dc, peerUsername }) => {
|
||||||
|
console.log('Connected to', peerUsername)
|
||||||
|
|
||||||
// Save credentials for later use
|
dc.addEventListener('message', (e) => {
|
||||||
localStorage.setItem('rondevu-creds', JSON.stringify(creds));
|
console.log('Received:', e.data)
|
||||||
|
})
|
||||||
|
|
||||||
// Create offer with topics
|
dc.addEventListener('open', () => {
|
||||||
const offers = await client.offers.create([{
|
dc.send('Hello from Bob!')
|
||||||
sdp: 'v=0...', // Your WebRTC offer SDP
|
})
|
||||||
topics: ['movie-xyz', 'hd-content'],
|
|
||||||
ttl: 300000 // 5 minutes
|
|
||||||
}]);
|
|
||||||
|
|
||||||
// Discover peers by topic
|
|
||||||
const discovered = await client.offers.findByTopic('movie-xyz', {
|
|
||||||
limit: 50
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log(`Found ${discovered.length} peers`);
|
|
||||||
|
|
||||||
// Use bloom filter to exclude known peers
|
|
||||||
const knownPeers = new Set(['peer-id-1', 'peer-id-2']);
|
|
||||||
const bloom = new BloomFilter(1024, 3);
|
|
||||||
knownPeers.forEach(id => bloom.add(id));
|
|
||||||
|
|
||||||
const newPeers = await client.offers.findByTopic('movie-xyz', {
|
|
||||||
bloomFilter: bloom.toBytes(),
|
|
||||||
limit: 50
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
## API Reference
|
|
||||||
|
|
||||||
### Authentication
|
|
||||||
|
|
||||||
#### `client.register()`
|
|
||||||
Register a new peer and receive credentials.
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
const creds = await client.register();
|
|
||||||
// { peerId: '...', secret: '...' }
|
|
||||||
```
|
|
||||||
|
|
||||||
### Topics
|
|
||||||
|
|
||||||
#### `client.offers.getTopics(options?)`
|
|
||||||
List all topics with active peer counts (paginated).
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
const result = await client.offers.getTopics({
|
|
||||||
limit: 50,
|
|
||||||
offset: 0
|
|
||||||
});
|
|
||||||
|
|
||||||
// {
|
|
||||||
// topics: [
|
|
||||||
// { topic: 'movie-xyz', activePeers: 42 },
|
|
||||||
// { topic: 'torrent-abc', activePeers: 15 }
|
|
||||||
// ],
|
|
||||||
// total: 123,
|
|
||||||
// limit: 50,
|
|
||||||
// offset: 0
|
|
||||||
// }
|
|
||||||
```
|
|
||||||
|
|
||||||
### Offers
|
|
||||||
|
|
||||||
#### `client.offers.create(offers)`
|
|
||||||
Create one or more offers with topics.
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
const offers = await client.offers.create([
|
|
||||||
{
|
|
||||||
sdp: 'v=0...',
|
|
||||||
topics: ['topic-1', 'topic-2'],
|
|
||||||
ttl: 300000 // optional, default 5 minutes
|
|
||||||
}
|
}
|
||||||
]);
|
})
|
||||||
|
|
||||||
|
// Access connection
|
||||||
|
connection.dc.send('Another message')
|
||||||
|
connection.pc.close() // Close when done
|
||||||
```
|
```
|
||||||
|
|
||||||
#### `client.offers.findByTopic(topic, options?)`
|
## Core API
|
||||||
Find offers by topic with optional bloom filter.
|
|
||||||
|
### Rondevu.connect()
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
const offers = await client.offers.findByTopic('movie-xyz', {
|
const rondevu = await Rondevu.connect({
|
||||||
limit: 50,
|
apiUrl: string, // Required: Signaling server URL
|
||||||
bloomFilter: bloomBytes // optional
|
username?: string, // Optional: your username (auto-generates anonymous if omitted)
|
||||||
});
|
keypair?: Keypair, // Optional: reuse existing keypair
|
||||||
|
iceServers?: IceServerPreset | RTCIceServer[], // Optional: preset or custom config
|
||||||
|
debug?: boolean // Optional: enable debug logging (default: false)
|
||||||
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
#### `client.offers.getMine()`
|
### Service Publishing
|
||||||
Get all offers owned by the authenticated peer.
|
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
const myOffers = await client.offers.getMine();
|
await rondevu.publishService({
|
||||||
|
service: string, // e.g., 'chat:1.0.0' (username auto-appended)
|
||||||
|
maxOffers: number, // Maximum concurrent offers to maintain
|
||||||
|
offerFactory?: OfferFactory, // Optional: custom offer creation
|
||||||
|
ttl?: number // Optional: offer lifetime in ms (default: 300000)
|
||||||
|
})
|
||||||
|
|
||||||
|
await rondevu.startFilling() // Start accepting connections
|
||||||
|
rondevu.stopFilling() // Stop and close all connections
|
||||||
```
|
```
|
||||||
|
|
||||||
#### `client.offers.heartbeat(offerId)`
|
### Service Discovery
|
||||||
Update last_seen timestamp for an offer.
|
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
await client.offers.heartbeat(offerId);
|
// Direct lookup (with username)
|
||||||
|
await rondevu.getService('chat:1.0.0@alice')
|
||||||
|
|
||||||
|
// Random discovery (without username)
|
||||||
|
await rondevu.discoverService('chat:1.0.0')
|
||||||
|
|
||||||
|
// Paginated discovery
|
||||||
|
await rondevu.discoverServices('chat:1.0.0', limit, offset)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### `client.offers.delete(offerId)`
|
### Connecting to Services
|
||||||
Delete a specific offer.
|
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
await client.offers.delete(offerId);
|
const connection = await rondevu.connectToService({
|
||||||
|
serviceFqn?: string, // Full FQN like 'chat:1.0.0@alice'
|
||||||
|
service?: string, // Service without username (for discovery)
|
||||||
|
username?: string, // Target username (combined with service)
|
||||||
|
onConnection?: (context) => void, // Called when data channel opens
|
||||||
|
rtcConfig?: RTCConfiguration // Optional: override ICE servers
|
||||||
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
#### `client.offers.answer(offerId, sdp)`
|
## Documentation
|
||||||
Answer an offer (locks it to answerer).
|
|
||||||
|
|
||||||
```typescript
|
📚 **[ADVANCED.md](./ADVANCED.md)** - Comprehensive guide including:
|
||||||
await client.offers.answer(offerId, answerSdp);
|
- Detailed API reference for all methods
|
||||||
```
|
- Type definitions and interfaces
|
||||||
|
- Platform support (Browser & Node.js)
|
||||||
|
- Advanced usage patterns
|
||||||
|
- Username rules and service FQN format
|
||||||
|
- Examples and migration guides
|
||||||
|
|
||||||
#### `client.offers.getAnswers()`
|
## Examples
|
||||||
Poll for answers to your offers.
|
|
||||||
|
|
||||||
```typescript
|
- [React Demo](https://github.com/xtr-dev/rondevu-demo) - Full browser UI ([live](https://ronde.vu))
|
||||||
const answers = await client.offers.getAnswers();
|
|
||||||
```
|
|
||||||
|
|
||||||
### ICE Candidates
|
|
||||||
|
|
||||||
#### `client.offers.addIceCandidates(offerId, candidates)`
|
|
||||||
Post ICE candidates for an offer.
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
await client.offers.addIceCandidates(offerId, [
|
|
||||||
'candidate:1 1 UDP...'
|
|
||||||
]);
|
|
||||||
```
|
|
||||||
|
|
||||||
#### `client.offers.getIceCandidates(offerId, since?)`
|
|
||||||
Get ICE candidates from the other peer.
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
const candidates = await client.offers.getIceCandidates(offerId);
|
|
||||||
```
|
|
||||||
|
|
||||||
### Bloom Filter
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { BloomFilter } from '@xtr-dev/rondevu-client';
|
|
||||||
|
|
||||||
// Create filter: size=1024 bits, hash=3 functions
|
|
||||||
const bloom = new BloomFilter(1024, 3);
|
|
||||||
|
|
||||||
// Add items
|
|
||||||
bloom.add('peer-id-1');
|
|
||||||
bloom.add('peer-id-2');
|
|
||||||
|
|
||||||
// Test membership
|
|
||||||
bloom.test('peer-id-1'); // true (probably)
|
|
||||||
bloom.test('unknown'); // false (definitely)
|
|
||||||
|
|
||||||
// Export for API
|
|
||||||
const bytes = bloom.toBytes();
|
|
||||||
```
|
|
||||||
|
|
||||||
## TypeScript
|
|
||||||
|
|
||||||
All types are exported:
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import type {
|
|
||||||
Credentials,
|
|
||||||
Offer,
|
|
||||||
CreateOfferRequest,
|
|
||||||
TopicInfo,
|
|
||||||
IceCandidate,
|
|
||||||
FetchFunction,
|
|
||||||
RondevuOptions,
|
|
||||||
ConnectionOptions,
|
|
||||||
RondevuConnectionEvents
|
|
||||||
} from '@xtr-dev/rondevu-client';
|
|
||||||
```
|
|
||||||
|
|
||||||
## Environment Compatibility
|
|
||||||
|
|
||||||
The client library is designed to work across different JavaScript runtimes:
|
|
||||||
|
|
||||||
| Environment | Native Fetch | Custom Fetch Needed |
|
|
||||||
|-------------|--------------|---------------------|
|
|
||||||
| Modern Browsers | ✅ Yes | ❌ No |
|
|
||||||
| Node.js 18+ | ✅ Yes | ❌ No |
|
|
||||||
| Node.js < 18 | ❌ No | ✅ Yes (node-fetch) |
|
|
||||||
| Deno | ✅ Yes | ❌ No |
|
|
||||||
| Bun | ✅ Yes | ❌ No |
|
|
||||||
| Cloudflare Workers | ✅ Yes | ❌ No |
|
|
||||||
|
|
||||||
**If your environment doesn't have native fetch:**
|
|
||||||
|
|
||||||
```bash
|
|
||||||
npm install node-fetch
|
|
||||||
```
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { Rondevu } from '@xtr-dev/rondevu-client';
|
|
||||||
import fetch from 'node-fetch';
|
|
||||||
|
|
||||||
const client = new Rondevu({
|
|
||||||
baseUrl: 'https://rondevu.xtrdev.workers.dev',
|
|
||||||
fetch: fetch as any
|
|
||||||
});
|
|
||||||
```
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
|
|||||||
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'],
|
||||||
|
},
|
||||||
|
]
|
||||||
2997
package-lock.json
generated
Normal file
2997
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
25
package.json
25
package.json
@@ -1,13 +1,17 @@
|
|||||||
{
|
{
|
||||||
"name": "@xtr-dev/rondevu-client",
|
"name": "@xtr-dev/rondevu-client",
|
||||||
"version": "0.4.1",
|
"version": "0.17.0",
|
||||||
"description": "TypeScript client for Rondevu topic-based peer discovery and signaling server",
|
"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",
|
||||||
"types": "dist/index.d.ts",
|
"types": "dist/index.d.ts",
|
||||||
"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,10 +24,23 @@
|
|||||||
"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",
|
||||||
"README.md"
|
"README.md"
|
||||||
]
|
],
|
||||||
|
"dependencies": {
|
||||||
|
"@noble/ed25519": "^3.0.0"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
423
src/api.ts
Normal file
423
src/api.ts
Normal file
@@ -0,0 +1,423 @@
|
|||||||
|
/**
|
||||||
|
* Rondevu API Client - RPC interface
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { CryptoAdapter, Keypair } from './crypto-adapter.js'
|
||||||
|
import { WebCryptoAdapter } from './web-crypto-adapter.js'
|
||||||
|
import { RpcBatcher, BatcherOptions } from './rpc-batcher.js'
|
||||||
|
|
||||||
|
export type { Keypair } from './crypto-adapter.js'
|
||||||
|
export type { BatcherOptions } from './rpc-batcher.js'
|
||||||
|
|
||||||
|
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 | null
|
||||||
|
createdAt: number
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RPC request format
|
||||||
|
*/
|
||||||
|
interface RpcRequest {
|
||||||
|
method: string
|
||||||
|
message: string
|
||||||
|
signature: string
|
||||||
|
publicKey?: string
|
||||||
|
params?: any
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RPC response format
|
||||||
|
*/
|
||||||
|
interface RpcResponse {
|
||||||
|
success: boolean
|
||||||
|
result?: any
|
||||||
|
error?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RondevuAPI - RPC-based API client for Rondevu signaling server
|
||||||
|
*/
|
||||||
|
export class RondevuAPI {
|
||||||
|
private crypto: CryptoAdapter
|
||||||
|
private batcher: RpcBatcher | null = null
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
private baseUrl: string,
|
||||||
|
private username: string,
|
||||||
|
private keypair: Keypair,
|
||||||
|
cryptoAdapter?: CryptoAdapter,
|
||||||
|
batcherOptions?: BatcherOptions | false
|
||||||
|
) {
|
||||||
|
// Use WebCryptoAdapter by default (browser environment)
|
||||||
|
this.crypto = cryptoAdapter || new WebCryptoAdapter()
|
||||||
|
|
||||||
|
// Create batcher if not explicitly disabled
|
||||||
|
if (batcherOptions !== false) {
|
||||||
|
this.batcher = new RpcBatcher(
|
||||||
|
(requests) => this.rpcBatchDirect(requests),
|
||||||
|
batcherOptions
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate authentication parameters for RPC calls
|
||||||
|
*/
|
||||||
|
private async generateAuth(method: string, params: string = ''): Promise<{
|
||||||
|
message: string
|
||||||
|
signature: string
|
||||||
|
}> {
|
||||||
|
const timestamp = Date.now()
|
||||||
|
const message = params
|
||||||
|
? `${method}:${this.username}:${params}:${timestamp}`
|
||||||
|
: `${method}:${this.username}:${timestamp}`
|
||||||
|
|
||||||
|
const signature = await this.crypto.signMessage(message, this.keypair.privateKey)
|
||||||
|
|
||||||
|
return { message, signature }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute RPC call with optional batching
|
||||||
|
*/
|
||||||
|
private async rpc(request: RpcRequest): Promise<any> {
|
||||||
|
// Use batcher if enabled
|
||||||
|
if (this.batcher) {
|
||||||
|
return await this.batcher.add(request)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Direct call without batching
|
||||||
|
return await this.rpcDirect(request)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute single RPC call directly (bypasses batcher)
|
||||||
|
*/
|
||||||
|
private async rpcDirect(request: RpcRequest): Promise<any> {
|
||||||
|
const response = await fetch(`${this.baseUrl}/rpc`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(request),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP ${response.status}: ${response.statusText}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const result: RpcResponse = await response.json()
|
||||||
|
|
||||||
|
if (!result.success) {
|
||||||
|
throw new Error(result.error || 'RPC call failed')
|
||||||
|
}
|
||||||
|
|
||||||
|
return result.result
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute batch RPC calls directly (bypasses batcher)
|
||||||
|
*/
|
||||||
|
private async rpcBatchDirect(requests: RpcRequest[]): Promise<any[]> {
|
||||||
|
const response = await fetch(`${this.baseUrl}/rpc`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify(requests),
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP ${response.status}: ${response.statusText}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
const results: RpcResponse[] = await response.json()
|
||||||
|
|
||||||
|
// Validate response is an array
|
||||||
|
if (!Array.isArray(results)) {
|
||||||
|
console.error('Invalid RPC batch response:', results)
|
||||||
|
throw new Error('Server returned invalid batch response (not an array)')
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check response length matches request length
|
||||||
|
if (results.length !== requests.length) {
|
||||||
|
console.error(`Response length mismatch: expected ${requests.length}, got ${results.length}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return results.map((result, i) => {
|
||||||
|
if (!result || typeof result !== 'object') {
|
||||||
|
throw new Error(`Invalid response at index ${i}`)
|
||||||
|
}
|
||||||
|
if (!result.success) {
|
||||||
|
throw new Error(result.error || `RPC call ${i} failed`)
|
||||||
|
}
|
||||||
|
return result.result
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// Ed25519 Cryptography Helpers
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate an Ed25519 keypair for username claiming and service publishing
|
||||||
|
* @param cryptoAdapter - Optional crypto adapter (defaults to WebCryptoAdapter)
|
||||||
|
*/
|
||||||
|
static async generateKeypair(cryptoAdapter?: CryptoAdapter): Promise<Keypair> {
|
||||||
|
const adapter = cryptoAdapter || new WebCryptoAdapter()
|
||||||
|
return await adapter.generateKeypair()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sign a message with an Ed25519 private key
|
||||||
|
* @param cryptoAdapter - Optional crypto adapter (defaults to WebCryptoAdapter)
|
||||||
|
*/
|
||||||
|
static async signMessage(
|
||||||
|
message: string,
|
||||||
|
privateKeyBase64: string,
|
||||||
|
cryptoAdapter?: CryptoAdapter
|
||||||
|
): Promise<string> {
|
||||||
|
const adapter = cryptoAdapter || new WebCryptoAdapter()
|
||||||
|
return await adapter.signMessage(message, privateKeyBase64)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify an Ed25519 signature
|
||||||
|
* @param cryptoAdapter - Optional crypto adapter (defaults to WebCryptoAdapter)
|
||||||
|
*/
|
||||||
|
static async verifySignature(
|
||||||
|
message: string,
|
||||||
|
signatureBase64: string,
|
||||||
|
publicKeyBase64: string,
|
||||||
|
cryptoAdapter?: CryptoAdapter
|
||||||
|
): Promise<boolean> {
|
||||||
|
const adapter = cryptoAdapter || new WebCryptoAdapter()
|
||||||
|
return await adapter.verifySignature(message, signatureBase64, publicKeyBase64)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// Username Management
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a username is available
|
||||||
|
*/
|
||||||
|
async isUsernameAvailable(username: string): Promise<boolean> {
|
||||||
|
const auth = await this.generateAuth('getUser', username)
|
||||||
|
const result = await this.rpc({
|
||||||
|
method: 'getUser',
|
||||||
|
message: auth.message,
|
||||||
|
signature: auth.signature,
|
||||||
|
params: { username },
|
||||||
|
})
|
||||||
|
return result.available
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if current username is claimed
|
||||||
|
*/
|
||||||
|
async isUsernameClaimed(): Promise<boolean> {
|
||||||
|
const auth = await this.generateAuth('getUser', this.username)
|
||||||
|
const result = await this.rpc({
|
||||||
|
method: 'getUser',
|
||||||
|
message: auth.message,
|
||||||
|
signature: auth.signature,
|
||||||
|
params: { username: this.username },
|
||||||
|
})
|
||||||
|
return !result.available
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// Service Management
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Publish a service
|
||||||
|
*/
|
||||||
|
async publishService(service: ServiceRequest): Promise<Service> {
|
||||||
|
const auth = await this.generateAuth('publishService', service.serviceFqn)
|
||||||
|
return await this.rpc({
|
||||||
|
method: 'publishService',
|
||||||
|
message: auth.message,
|
||||||
|
signature: auth.signature,
|
||||||
|
publicKey: this.keypair.publicKey,
|
||||||
|
params: {
|
||||||
|
serviceFqn: service.serviceFqn,
|
||||||
|
offers: service.offers,
|
||||||
|
ttl: service.ttl,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get service by FQN (direct lookup, random, or paginated)
|
||||||
|
*/
|
||||||
|
async getService(
|
||||||
|
serviceFqn: string,
|
||||||
|
options?: { limit?: number; offset?: number }
|
||||||
|
): Promise<any> {
|
||||||
|
const auth = await this.generateAuth('getService', serviceFqn)
|
||||||
|
return await this.rpc({
|
||||||
|
method: 'getService',
|
||||||
|
message: auth.message,
|
||||||
|
signature: auth.signature,
|
||||||
|
publicKey: this.keypair.publicKey,
|
||||||
|
params: {
|
||||||
|
serviceFqn,
|
||||||
|
...options,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a service
|
||||||
|
*/
|
||||||
|
async deleteService(serviceFqn: string): Promise<void> {
|
||||||
|
const auth = await this.generateAuth('deleteService', serviceFqn)
|
||||||
|
await this.rpc({
|
||||||
|
method: 'deleteService',
|
||||||
|
message: auth.message,
|
||||||
|
signature: auth.signature,
|
||||||
|
publicKey: this.keypair.publicKey,
|
||||||
|
params: { serviceFqn },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// WebRTC Signaling
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Answer an offer
|
||||||
|
*/
|
||||||
|
async answerOffer(serviceFqn: string, offerId: string, sdp: string): Promise<void> {
|
||||||
|
const auth = await this.generateAuth('answerOffer', offerId)
|
||||||
|
await this.rpc({
|
||||||
|
method: 'answerOffer',
|
||||||
|
message: auth.message,
|
||||||
|
signature: auth.signature,
|
||||||
|
publicKey: this.keypair.publicKey,
|
||||||
|
params: { serviceFqn, offerId, sdp },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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> {
|
||||||
|
try {
|
||||||
|
const auth = await this.generateAuth('getOfferAnswer', offerId)
|
||||||
|
return await this.rpc({
|
||||||
|
method: 'getOfferAnswer',
|
||||||
|
message: auth.message,
|
||||||
|
signature: auth.signature,
|
||||||
|
publicKey: this.keypair.publicKey,
|
||||||
|
params: { serviceFqn, offerId },
|
||||||
|
})
|
||||||
|
} catch (err) {
|
||||||
|
if ((err as Error).message.includes('not yet answered')) {
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Combined polling for answers and ICE candidates
|
||||||
|
*/
|
||||||
|
async poll(since?: number): Promise<{
|
||||||
|
answers: Array<{
|
||||||
|
offerId: string
|
||||||
|
serviceId?: string
|
||||||
|
answererId: string
|
||||||
|
sdp: string
|
||||||
|
answeredAt: number
|
||||||
|
}>
|
||||||
|
iceCandidates: Record<
|
||||||
|
string,
|
||||||
|
Array<{
|
||||||
|
candidate: RTCIceCandidateInit | null
|
||||||
|
role: 'offerer' | 'answerer'
|
||||||
|
peerId: string
|
||||||
|
createdAt: number
|
||||||
|
}>
|
||||||
|
>
|
||||||
|
}> {
|
||||||
|
const auth = await this.generateAuth('poll')
|
||||||
|
return await this.rpc({
|
||||||
|
method: 'poll',
|
||||||
|
message: auth.message,
|
||||||
|
signature: auth.signature,
|
||||||
|
publicKey: this.keypair.publicKey,
|
||||||
|
params: { since },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add ICE candidates to a specific offer
|
||||||
|
*/
|
||||||
|
async addOfferIceCandidates(
|
||||||
|
serviceFqn: string,
|
||||||
|
offerId: string,
|
||||||
|
candidates: RTCIceCandidateInit[]
|
||||||
|
): Promise<{ count: number; offerId: string }> {
|
||||||
|
const auth = await this.generateAuth('addIceCandidates', offerId)
|
||||||
|
return await this.rpc({
|
||||||
|
method: 'addIceCandidates',
|
||||||
|
message: auth.message,
|
||||||
|
signature: auth.signature,
|
||||||
|
publicKey: this.keypair.publicKey,
|
||||||
|
params: { serviceFqn, offerId, candidates },
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get ICE candidates for a specific offer
|
||||||
|
*/
|
||||||
|
async getOfferIceCandidates(
|
||||||
|
serviceFqn: string,
|
||||||
|
offerId: string,
|
||||||
|
since: number = 0
|
||||||
|
): Promise<{ candidates: IceCandidate[]; offerId: string }> {
|
||||||
|
const auth = await this.generateAuth('getIceCandidates', `${offerId}:${since}`)
|
||||||
|
const result = await this.rpc({
|
||||||
|
method: 'getIceCandidates',
|
||||||
|
message: auth.message,
|
||||||
|
signature: auth.signature,
|
||||||
|
publicKey: this.keypair.publicKey,
|
||||||
|
params: { serviceFqn, offerId, since },
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
candidates: result.candidates || [],
|
||||||
|
offerId: result.offerId,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
60
src/auth.ts
60
src/auth.ts
@@ -1,60 +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
|
|
||||||
*/
|
|
||||||
async register(): Promise<Credentials> {
|
|
||||||
const response = await this.fetchFn(`${this.baseUrl}/register`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify({}),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
|
||||||
throw new Error(`Registration failed: ${error.error || response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
return {
|
|
||||||
peerId: data.peerId,
|
|
||||||
secret: data.secret,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create Authorization header value
|
|
||||||
*/
|
|
||||||
static createAuthHeader(credentials: Credentials): string {
|
|
||||||
return `Bearer ${credentials.peerId}:${credentials.secret}`;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
83
src/bloom.ts
83
src/bloom.ts
@@ -1,83 +0,0 @@
|
|||||||
// Declare Buffer for Node.js compatibility
|
|
||||||
declare const Buffer: any;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Simple bloom filter implementation for peer ID exclusion
|
|
||||||
* Uses multiple hash functions for better distribution
|
|
||||||
*/
|
|
||||||
export class BloomFilter {
|
|
||||||
private bits: Uint8Array;
|
|
||||||
private size: number;
|
|
||||||
private numHashes: number;
|
|
||||||
|
|
||||||
constructor(size: number = 1024, numHashes: number = 3) {
|
|
||||||
this.size = size;
|
|
||||||
this.numHashes = numHashes;
|
|
||||||
this.bits = new Uint8Array(Math.ceil(size / 8));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add a peer ID to the filter
|
|
||||||
*/
|
|
||||||
add(peerId: string): void {
|
|
||||||
for (let i = 0; i < this.numHashes; i++) {
|
|
||||||
const hash = this.hash(peerId, i);
|
|
||||||
const index = hash % this.size;
|
|
||||||
const byteIndex = Math.floor(index / 8);
|
|
||||||
const bitIndex = index % 8;
|
|
||||||
this.bits[byteIndex] |= 1 << bitIndex;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test if peer ID might be in the filter
|
|
||||||
*/
|
|
||||||
test(peerId: string): boolean {
|
|
||||||
for (let i = 0; i < this.numHashes; i++) {
|
|
||||||
const hash = this.hash(peerId, i);
|
|
||||||
const index = hash % this.size;
|
|
||||||
const byteIndex = Math.floor(index / 8);
|
|
||||||
const bitIndex = index % 8;
|
|
||||||
if (!(this.bits[byteIndex] & (1 << bitIndex))) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get raw bits for transmission
|
|
||||||
*/
|
|
||||||
toBytes(): Uint8Array {
|
|
||||||
return this.bits;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert to base64 for URL parameters
|
|
||||||
*/
|
|
||||||
toBase64(): string {
|
|
||||||
// Convert Uint8Array to regular array then to string
|
|
||||||
const binaryString = String.fromCharCode(...Array.from(this.bits));
|
|
||||||
// Use btoa for browser, or Buffer for Node.js
|
|
||||||
if (typeof btoa !== 'undefined') {
|
|
||||||
return btoa(binaryString);
|
|
||||||
} else if (typeof Buffer !== 'undefined') {
|
|
||||||
return Buffer.from(this.bits).toString('base64');
|
|
||||||
} else {
|
|
||||||
// Fallback: manual base64 encoding
|
|
||||||
throw new Error('No base64 encoding available');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Simple hash function (FNV-1a variant)
|
|
||||||
*/
|
|
||||||
private hash(str: string, seed: number): number {
|
|
||||||
let hash = 2166136261 ^ seed;
|
|
||||||
for (let i = 0; i < str.length; i++) {
|
|
||||||
hash ^= str.charCodeAt(i);
|
|
||||||
hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
|
|
||||||
}
|
|
||||||
return hash >>> 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
208
src/client.ts
208
src/client.ts
@@ -1,208 +0,0 @@
|
|||||||
import {
|
|
||||||
RondevuClientOptions,
|
|
||||||
CreateOfferRequest,
|
|
||||||
CreateOfferResponse,
|
|
||||||
AnswerRequest,
|
|
||||||
AnswerResponse,
|
|
||||||
PollRequest,
|
|
||||||
PollOffererResponse,
|
|
||||||
PollAnswererResponse,
|
|
||||||
VersionResponse,
|
|
||||||
HealthResponse,
|
|
||||||
ErrorResponse,
|
|
||||||
Side,
|
|
||||||
} from './types.js';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* HTTP API client for Rondevu peer signaling server
|
|
||||||
*/
|
|
||||||
export class RondevuAPI {
|
|
||||||
private readonly baseUrl: string;
|
|
||||||
private readonly fetchImpl: typeof fetch;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new Rondevu API client instance
|
|
||||||
* @param options - Client configuration options
|
|
||||||
*/
|
|
||||||
constructor(options: RondevuClientOptions) {
|
|
||||||
this.baseUrl = options.baseUrl.replace(/\/$/, ''); // Remove trailing slash
|
|
||||||
this.fetchImpl = options.fetch || globalThis.fetch.bind(globalThis);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Makes an HTTP request to the Rondevu server
|
|
||||||
*/
|
|
||||||
private async request<T>(
|
|
||||||
endpoint: string,
|
|
||||||
options: RequestInit = {}
|
|
||||||
): Promise<T> {
|
|
||||||
const url = `${this.baseUrl}${endpoint}`;
|
|
||||||
|
|
||||||
const headers: Record<string, string> = {
|
|
||||||
...(options.headers as Record<string, string>),
|
|
||||||
};
|
|
||||||
|
|
||||||
if (options.body) {
|
|
||||||
headers['Content-Type'] = 'application/json';
|
|
||||||
}
|
|
||||||
|
|
||||||
const response = await this.fetchImpl(url, {
|
|
||||||
...options,
|
|
||||||
headers,
|
|
||||||
});
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const error = data as ErrorResponse;
|
|
||||||
throw new Error(error.error || `HTTP ${response.status}: ${response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return data as T;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets server version information
|
|
||||||
*
|
|
||||||
* @returns Server version
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* ```typescript
|
|
||||||
* const api = new RondevuAPI({ baseUrl: 'https://example.com' });
|
|
||||||
* const { version } = await api.getVersion();
|
|
||||||
* console.log('Server version:', version);
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
async getVersion(): Promise<VersionResponse> {
|
|
||||||
return this.request<VersionResponse>('/', {
|
|
||||||
method: 'GET',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new offer
|
|
||||||
*
|
|
||||||
* @param request - Offer details including peer ID, signaling data, and optional custom code
|
|
||||||
* @returns Unique offer code (UUID or custom code)
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* ```typescript
|
|
||||||
* const api = new RondevuAPI({ baseUrl: 'https://example.com' });
|
|
||||||
* const { code } = await api.createOffer({
|
|
||||||
* peerId: 'peer-123',
|
|
||||||
* offer: signalingData,
|
|
||||||
* code: 'my-custom-code' // optional
|
|
||||||
* });
|
|
||||||
* console.log('Offer code:', code);
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
async createOffer(request: CreateOfferRequest): Promise<CreateOfferResponse> {
|
|
||||||
return this.request<CreateOfferResponse>('/offer', {
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends an answer or candidate to an existing offer
|
|
||||||
*
|
|
||||||
* @param request - Answer details including offer code and signaling data
|
|
||||||
* @returns Success confirmation
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* ```typescript
|
|
||||||
* const api = new RondevuAPI({ baseUrl: 'https://example.com' });
|
|
||||||
*
|
|
||||||
* // Send answer
|
|
||||||
* await api.sendAnswer({
|
|
||||||
* code: offerCode,
|
|
||||||
* answer: answerData,
|
|
||||||
* side: 'answerer'
|
|
||||||
* });
|
|
||||||
*
|
|
||||||
* // Send candidate
|
|
||||||
* await api.sendAnswer({
|
|
||||||
* code: offerCode,
|
|
||||||
* candidate: candidateData,
|
|
||||||
* side: 'offerer'
|
|
||||||
* });
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
async sendAnswer(request: AnswerRequest): Promise<AnswerResponse> {
|
|
||||||
return this.request<AnswerResponse>('/answer', {
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Polls for offer data from the other peer
|
|
||||||
*
|
|
||||||
* @param code - Offer code
|
|
||||||
* @param side - Which side is polling ('offerer' or 'answerer')
|
|
||||||
* @returns Offer data including offers, answers, and candidates
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* ```typescript
|
|
||||||
* const api = new RondevuAPI({ baseUrl: 'https://example.com' });
|
|
||||||
*
|
|
||||||
* // Offerer polls for answer
|
|
||||||
* const offererData = await api.poll(offerCode, 'offerer');
|
|
||||||
* if (offererData.answer) {
|
|
||||||
* console.log('Received answer:', offererData.answer);
|
|
||||||
* }
|
|
||||||
*
|
|
||||||
* // Answerer polls for offer
|
|
||||||
* const answererData = await api.poll(offerCode, 'answerer');
|
|
||||||
* console.log('Received offer:', answererData.offer);
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
async poll(
|
|
||||||
code: string,
|
|
||||||
side: Side
|
|
||||||
): Promise<PollOffererResponse | PollAnswererResponse> {
|
|
||||||
const request: PollRequest = { code, side };
|
|
||||||
return this.request<PollOffererResponse | PollAnswererResponse>('/poll', {
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify(request),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks server health and version
|
|
||||||
*
|
|
||||||
* @returns Health status, timestamp, and version
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* ```typescript
|
|
||||||
* const api = new RondevuAPI({ baseUrl: 'https://example.com' });
|
|
||||||
* const health = await api.health();
|
|
||||||
* console.log('Server status:', health.status);
|
|
||||||
* console.log('Server version:', health.version);
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
async health(): Promise<HealthResponse> {
|
|
||||||
return this.request<HealthResponse>('/health', {
|
|
||||||
method: 'GET',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Ends a session by deleting the offer from the server
|
|
||||||
*
|
|
||||||
* @param code - The offer code
|
|
||||||
* @returns Success confirmation
|
|
||||||
*
|
|
||||||
* @example
|
|
||||||
* ```typescript
|
|
||||||
* const api = new RondevuAPI({ baseUrl: 'https://example.com' });
|
|
||||||
* await api.leave('my-offer-code');
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
async leave(code: string): Promise<{ success: boolean }> {
|
|
||||||
return this.request<{ success: boolean }>('/leave', {
|
|
||||||
method: 'POST',
|
|
||||||
body: JSON.stringify({ code }),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,388 +0,0 @@
|
|||||||
import { RondevuOffers, RTCIceCandidateInit } from './offers.js';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Events emitted by RondevuConnection
|
|
||||||
*/
|
|
||||||
export interface RondevuConnectionEvents {
|
|
||||||
'connecting': () => void;
|
|
||||||
'connected': () => void;
|
|
||||||
'disconnected': () => void;
|
|
||||||
'error': (error: Error) => void;
|
|
||||||
'datachannel': (channel: RTCDataChannel) => void;
|
|
||||||
'track': (event: RTCTrackEvent) => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Options for creating a WebRTC connection
|
|
||||||
*/
|
|
||||||
export interface ConnectionOptions {
|
|
||||||
/**
|
|
||||||
* RTCConfiguration for the peer connection
|
|
||||||
* @default { iceServers: [{ urls: 'stun:stun.l.google.com:19302' }] }
|
|
||||||
*/
|
|
||||||
rtcConfig?: RTCConfiguration;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Topics to advertise this connection under
|
|
||||||
*/
|
|
||||||
topics: string[];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* How long the offer should live (milliseconds)
|
|
||||||
* @default 300000 (5 minutes)
|
|
||||||
*/
|
|
||||||
ttl?: number;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Whether to create a data channel automatically (for offerer)
|
|
||||||
* @default true
|
|
||||||
*/
|
|
||||||
createDataChannel?: boolean;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Label for the automatically created data channel
|
|
||||||
* @default 'data'
|
|
||||||
*/
|
|
||||||
dataChannelLabel?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* High-level WebRTC connection manager for Rondevu
|
|
||||||
* Handles offer/answer exchange, ICE candidates, and connection lifecycle
|
|
||||||
*/
|
|
||||||
export class RondevuConnection {
|
|
||||||
private pc: RTCPeerConnection;
|
|
||||||
private offersApi: RondevuOffers;
|
|
||||||
private offerId?: string;
|
|
||||||
private role?: 'offerer' | 'answerer';
|
|
||||||
private icePollingInterval?: ReturnType<typeof setInterval>;
|
|
||||||
private answerPollingInterval?: ReturnType<typeof setInterval>;
|
|
||||||
private lastIceTimestamp: number = 0; // Start at 0 to get all candidates on first poll
|
|
||||||
private eventListeners: Map<keyof RondevuConnectionEvents, Set<Function>> = new Map();
|
|
||||||
private dataChannel?: RTCDataChannel;
|
|
||||||
private pendingIceCandidates: RTCIceCandidateInit[] = [];
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Current connection state
|
|
||||||
*/
|
|
||||||
get connectionState(): RTCPeerConnectionState {
|
|
||||||
return this.pc.connectionState;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The offer ID for this connection
|
|
||||||
*/
|
|
||||||
get id(): string | undefined {
|
|
||||||
return this.offerId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get the primary data channel (if created)
|
|
||||||
*/
|
|
||||||
get channel(): RTCDataChannel | undefined {
|
|
||||||
return this.dataChannel;
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
offersApi: RondevuOffers,
|
|
||||||
private rtcConfig: RTCConfiguration = {
|
|
||||||
iceServers: [
|
|
||||||
{ urls: 'stun:stun.l.google.com:19302' },
|
|
||||||
{ urls: 'stun:stun1.l.google.com:19302' }
|
|
||||||
]
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
this.offersApi = offersApi;
|
|
||||||
this.pc = new RTCPeerConnection(rtcConfig);
|
|
||||||
this.setupPeerConnection();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set up peer connection event handlers
|
|
||||||
*/
|
|
||||||
private setupPeerConnection(): void {
|
|
||||||
this.pc.onicecandidate = async (event) => {
|
|
||||||
if (event.candidate) {
|
|
||||||
// Convert RTCIceCandidate to RTCIceCandidateInit (plain object)
|
|
||||||
const candidateData: RTCIceCandidateInit = {
|
|
||||||
candidate: event.candidate.candidate,
|
|
||||||
sdpMid: event.candidate.sdpMid,
|
|
||||||
sdpMLineIndex: event.candidate.sdpMLineIndex,
|
|
||||||
usernameFragment: event.candidate.usernameFragment,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (this.offerId) {
|
|
||||||
// offerId is set, send immediately (trickle ICE)
|
|
||||||
try {
|
|
||||||
await this.offersApi.addIceCandidates(this.offerId, [candidateData]);
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Error sending ICE candidate:', err);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// offerId not set yet, buffer the candidate
|
|
||||||
this.pendingIceCandidates.push(candidateData);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
this.pc.onconnectionstatechange = () => {
|
|
||||||
switch (this.pc.connectionState) {
|
|
||||||
case 'connecting':
|
|
||||||
this.emit('connecting');
|
|
||||||
break;
|
|
||||||
case 'connected':
|
|
||||||
this.emit('connected');
|
|
||||||
// Stop polling once connected - we have all the ICE candidates we need
|
|
||||||
this.stopPolling();
|
|
||||||
break;
|
|
||||||
case 'disconnected':
|
|
||||||
case 'failed':
|
|
||||||
case 'closed':
|
|
||||||
this.emit('disconnected');
|
|
||||||
this.stopPolling();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
this.pc.ondatachannel = (event) => {
|
|
||||||
this.dataChannel = event.channel;
|
|
||||||
this.emit('datachannel', event.channel);
|
|
||||||
};
|
|
||||||
|
|
||||||
this.pc.ontrack = (event) => {
|
|
||||||
this.emit('track', event);
|
|
||||||
};
|
|
||||||
|
|
||||||
this.pc.onicecandidateerror = (event) => {
|
|
||||||
console.error('ICE candidate error:', event);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Flush buffered ICE candidates (trickle ICE support)
|
|
||||||
*/
|
|
||||||
private async flushPendingIceCandidates(): Promise<void> {
|
|
||||||
if (this.pendingIceCandidates.length > 0 && this.offerId) {
|
|
||||||
try {
|
|
||||||
await this.offersApi.addIceCandidates(this.offerId, this.pendingIceCandidates);
|
|
||||||
this.pendingIceCandidates = [];
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Error flushing pending ICE candidates:', err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create an offer and advertise on topics
|
|
||||||
*/
|
|
||||||
async createOffer(options: ConnectionOptions): Promise<string> {
|
|
||||||
this.role = 'offerer';
|
|
||||||
|
|
||||||
// Create data channel if requested
|
|
||||||
if (options.createDataChannel !== false) {
|
|
||||||
this.dataChannel = this.pc.createDataChannel(
|
|
||||||
options.dataChannelLabel || 'data'
|
|
||||||
);
|
|
||||||
this.emit('datachannel', this.dataChannel);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create WebRTC offer
|
|
||||||
const offer = await this.pc.createOffer();
|
|
||||||
await this.pc.setLocalDescription(offer);
|
|
||||||
|
|
||||||
// Create offer on Rondevu server
|
|
||||||
const offers = await this.offersApi.create([{
|
|
||||||
sdp: offer.sdp!,
|
|
||||||
topics: options.topics,
|
|
||||||
ttl: options.ttl || 300000
|
|
||||||
}]);
|
|
||||||
|
|
||||||
this.offerId = offers[0].id;
|
|
||||||
|
|
||||||
// Flush any ICE candidates that were generated during offer creation
|
|
||||||
await this.flushPendingIceCandidates();
|
|
||||||
|
|
||||||
// Start polling for answers
|
|
||||||
this.startAnswerPolling();
|
|
||||||
|
|
||||||
return this.offerId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Answer an existing offer
|
|
||||||
*/
|
|
||||||
async answer(offerId: string, offerSdp: string): Promise<void> {
|
|
||||||
this.role = 'answerer';
|
|
||||||
|
|
||||||
// Set remote description
|
|
||||||
await this.pc.setRemoteDescription({
|
|
||||||
type: 'offer',
|
|
||||||
sdp: offerSdp
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create answer
|
|
||||||
const answer = await this.pc.createAnswer();
|
|
||||||
await this.pc.setLocalDescription(answer);
|
|
||||||
|
|
||||||
// Send answer to server FIRST
|
|
||||||
// This registers us as the answerer before ICE candidates arrive
|
|
||||||
await this.offersApi.answer(offerId, answer.sdp!);
|
|
||||||
|
|
||||||
// Now set offerId to enable ICE candidate sending
|
|
||||||
// This prevents a race condition where ICE candidates arrive before answer is registered
|
|
||||||
this.offerId = offerId;
|
|
||||||
|
|
||||||
// Flush any ICE candidates that were generated during answer creation
|
|
||||||
await this.flushPendingIceCandidates();
|
|
||||||
|
|
||||||
// Start polling for ICE candidates
|
|
||||||
this.startIcePolling();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Start polling for answers (offerer only)
|
|
||||||
*/
|
|
||||||
private startAnswerPolling(): void {
|
|
||||||
if (this.role !== 'offerer' || !this.offerId) return;
|
|
||||||
|
|
||||||
this.answerPollingInterval = setInterval(async () => {
|
|
||||||
try {
|
|
||||||
const answers = await this.offersApi.getAnswers();
|
|
||||||
const myAnswer = answers.find(a => a.offerId === this.offerId);
|
|
||||||
|
|
||||||
if (myAnswer) {
|
|
||||||
// Set remote description
|
|
||||||
await this.pc.setRemoteDescription({
|
|
||||||
type: 'answer',
|
|
||||||
sdp: myAnswer.sdp
|
|
||||||
});
|
|
||||||
|
|
||||||
// Stop answer polling, start ICE polling
|
|
||||||
this.stopAnswerPolling();
|
|
||||||
this.startIcePolling();
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Error polling for answers:', err);
|
|
||||||
// Stop polling if offer expired/not found
|
|
||||||
if (err instanceof Error && err.message.includes('not found')) {
|
|
||||||
this.stopPolling();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, 2000);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Start polling for ICE candidates
|
|
||||||
*/
|
|
||||||
private startIcePolling(): void {
|
|
||||||
if (!this.offerId) return;
|
|
||||||
|
|
||||||
this.icePollingInterval = setInterval(async () => {
|
|
||||||
if (!this.offerId) return;
|
|
||||||
|
|
||||||
try {
|
|
||||||
const candidates = await this.offersApi.getIceCandidates(
|
|
||||||
this.offerId,
|
|
||||||
this.lastIceTimestamp
|
|
||||||
);
|
|
||||||
|
|
||||||
for (const cand of candidates) {
|
|
||||||
// Use the candidate object directly - it's already RTCIceCandidateInit
|
|
||||||
await this.pc.addIceCandidate(new RTCIceCandidate(cand.candidate));
|
|
||||||
this.lastIceTimestamp = cand.createdAt;
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error('Error polling for ICE candidates:', err);
|
|
||||||
// Stop polling if offer expired/not found
|
|
||||||
if (err instanceof Error && err.message.includes('not found')) {
|
|
||||||
this.stopPolling();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, 1000);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop answer polling
|
|
||||||
*/
|
|
||||||
private stopAnswerPolling(): void {
|
|
||||||
if (this.answerPollingInterval) {
|
|
||||||
clearInterval(this.answerPollingInterval);
|
|
||||||
this.answerPollingInterval = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop ICE polling
|
|
||||||
*/
|
|
||||||
private stopIcePolling(): void {
|
|
||||||
if (this.icePollingInterval) {
|
|
||||||
clearInterval(this.icePollingInterval);
|
|
||||||
this.icePollingInterval = undefined;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stop all polling
|
|
||||||
*/
|
|
||||||
private stopPolling(): void {
|
|
||||||
this.stopAnswerPolling();
|
|
||||||
this.stopIcePolling();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add event listener
|
|
||||||
*/
|
|
||||||
on<K extends keyof RondevuConnectionEvents>(
|
|
||||||
event: K,
|
|
||||||
listener: RondevuConnectionEvents[K]
|
|
||||||
): void {
|
|
||||||
if (!this.eventListeners.has(event)) {
|
|
||||||
this.eventListeners.set(event, new Set());
|
|
||||||
}
|
|
||||||
this.eventListeners.get(event)!.add(listener);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove event listener
|
|
||||||
*/
|
|
||||||
off<K extends keyof RondevuConnectionEvents>(
|
|
||||||
event: K,
|
|
||||||
listener: RondevuConnectionEvents[K]
|
|
||||||
): void {
|
|
||||||
const listeners = this.eventListeners.get(event);
|
|
||||||
if (listeners) {
|
|
||||||
listeners.delete(listener);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Emit event
|
|
||||||
*/
|
|
||||||
private emit<K extends keyof RondevuConnectionEvents>(
|
|
||||||
event: K,
|
|
||||||
...args: Parameters<RondevuConnectionEvents[K]>
|
|
||||||
): void {
|
|
||||||
const listeners = this.eventListeners.get(event);
|
|
||||||
if (listeners) {
|
|
||||||
listeners.forEach(listener => {
|
|
||||||
(listener as any)(...args);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add a media track to the connection
|
|
||||||
*/
|
|
||||||
addTrack(track: MediaStreamTrack, ...streams: MediaStream[]): RTCRtpSender {
|
|
||||||
return this.pc.addTrack(track, ...streams);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Close the connection and clean up
|
|
||||||
*/
|
|
||||||
close(): void {
|
|
||||||
this.stopPolling();
|
|
||||||
this.pc.close();
|
|
||||||
this.eventListeners.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
48
src/crypto-adapter.ts
Normal file
48
src/crypto-adapter.ts
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
/**
|
||||||
|
* Crypto adapter interface for platform-independent cryptographic operations
|
||||||
|
*/
|
||||||
|
|
||||||
|
export interface Keypair {
|
||||||
|
publicKey: string
|
||||||
|
privateKey: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Platform-independent crypto adapter interface
|
||||||
|
* Implementations provide platform-specific crypto operations
|
||||||
|
*/
|
||||||
|
export interface CryptoAdapter {
|
||||||
|
/**
|
||||||
|
* Generate an Ed25519 keypair
|
||||||
|
*/
|
||||||
|
generateKeypair(): Promise<Keypair>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sign a message with an Ed25519 private key
|
||||||
|
*/
|
||||||
|
signMessage(message: string, privateKeyBase64: string): Promise<string>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Verify an Ed25519 signature
|
||||||
|
*/
|
||||||
|
verifySignature(
|
||||||
|
message: string,
|
||||||
|
signatureBase64: string,
|
||||||
|
publicKeyBase64: string
|
||||||
|
): Promise<boolean>
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert Uint8Array to base64 string
|
||||||
|
*/
|
||||||
|
bytesToBase64(bytes: Uint8Array): string
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert base64 string to Uint8Array
|
||||||
|
*/
|
||||||
|
base64ToBytes(base64: string): Uint8Array
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate random bytes
|
||||||
|
*/
|
||||||
|
randomBytes(length: number): Uint8Array
|
||||||
|
}
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
/**
|
|
||||||
* Simple EventEmitter implementation for browser and Node.js compatibility
|
|
||||||
*/
|
|
||||||
export class EventEmitter {
|
|
||||||
private events: Map<string, Set<Function>>;
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
this.events = new Map();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Register an event listener
|
|
||||||
*/
|
|
||||||
on(event: string, listener: Function): 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(event: string, listener: Function): this {
|
|
||||||
const onceWrapper = (...args: any[]) => {
|
|
||||||
this.off(event, onceWrapper);
|
|
||||||
listener.apply(this, args);
|
|
||||||
};
|
|
||||||
return this.on(event, onceWrapper);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove an event listener
|
|
||||||
*/
|
|
||||||
off(event: string, listener: Function): 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
|
|
||||||
*/
|
|
||||||
emit(event: string, ...args: any[]): boolean {
|
|
||||||
const listeners = this.events.get(event);
|
|
||||||
if (!listeners || listeners.size === 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
listeners.forEach(listener => {
|
|
||||||
try {
|
|
||||||
listener.apply(this, args);
|
|
||||||
} catch (err) {
|
|
||||||
console.error(`Error in ${event} event listener:`, err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove all listeners for an event (or all events if not specified)
|
|
||||||
*/
|
|
||||||
removeAllListeners(event?: string): this {
|
|
||||||
if (event) {
|
|
||||||
this.events.delete(event);
|
|
||||||
} else {
|
|
||||||
this.events.clear();
|
|
||||||
}
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get listener count for an event
|
|
||||||
*/
|
|
||||||
listenerCount(event: string): number {
|
|
||||||
const listeners = this.events.get(event);
|
|
||||||
return listeners ? listeners.size : 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
52
src/index.ts
52
src/index.ts
@@ -1,31 +1,39 @@
|
|||||||
/**
|
/**
|
||||||
* @xtr-dev/rondevu-client
|
* @xtr-dev/rondevu-client
|
||||||
* WebRTC peer signaling and discovery client with topic-based discovery
|
* 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 { RpcBatcher } from './rpc-batcher.js'
|
||||||
|
|
||||||
// Export authentication
|
// Export crypto adapters
|
||||||
export { RondevuAuth } from './auth.js';
|
export { WebCryptoAdapter } from './web-crypto-adapter.js'
|
||||||
export type { Credentials, FetchFunction } from './auth.js';
|
export { NodeCryptoAdapter } from './node-crypto-adapter.js'
|
||||||
|
|
||||||
// Export offers API
|
// Export types
|
||||||
export { RondevuOffers } from './offers.js';
|
|
||||||
export type {
|
export type {
|
||||||
CreateOfferRequest,
|
Signaler,
|
||||||
Offer,
|
Binnable,
|
||||||
IceCandidate,
|
} from './types.js'
|
||||||
TopicInfo
|
|
||||||
} from './offers.js';
|
|
||||||
|
|
||||||
// Export bloom filter
|
|
||||||
export { BloomFilter } from './bloom.js';
|
|
||||||
|
|
||||||
// Export connection manager
|
|
||||||
export { RondevuConnection } from './connection.js';
|
|
||||||
export type {
|
export type {
|
||||||
ConnectionOptions,
|
Keypair,
|
||||||
RondevuConnectionEvents
|
OfferRequest,
|
||||||
} from './connection.js';
|
ServiceRequest,
|
||||||
|
Service,
|
||||||
|
ServiceOffer,
|
||||||
|
IceCandidate,
|
||||||
|
} from './api.js'
|
||||||
|
|
||||||
|
export type {
|
||||||
|
RondevuOptions,
|
||||||
|
PublishServiceOptions,
|
||||||
|
ConnectToServiceOptions,
|
||||||
|
ConnectionContext,
|
||||||
|
OfferContext,
|
||||||
|
OfferFactory
|
||||||
|
} from './rondevu.js'
|
||||||
|
|
||||||
|
export type { CryptoAdapter } from './crypto-adapter.js'
|
||||||
|
|
||||||
|
|||||||
98
src/node-crypto-adapter.ts
Normal file
98
src/node-crypto-adapter.ts
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
/**
|
||||||
|
* Node.js Crypto adapter for Node.js environments
|
||||||
|
* Requires Node.js 19+ or Node.js 18 with --experimental-global-webcrypto flag
|
||||||
|
*/
|
||||||
|
|
||||||
|
import * as ed25519 from '@noble/ed25519'
|
||||||
|
import { CryptoAdapter, Keypair } from './crypto-adapter.js'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Node.js Crypto implementation using Node.js built-in APIs
|
||||||
|
* Uses Buffer for base64 encoding and crypto.randomBytes for random generation
|
||||||
|
*
|
||||||
|
* Requirements:
|
||||||
|
* - Node.js 19+ (crypto.subtle available globally)
|
||||||
|
* - OR Node.js 18 with --experimental-global-webcrypto flag
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```typescript
|
||||||
|
* import { RondevuAPI } from '@xtr-dev/rondevu-client'
|
||||||
|
* import { NodeCryptoAdapter } from '@xtr-dev/rondevu-client/node'
|
||||||
|
*
|
||||||
|
* const api = new RondevuAPI(
|
||||||
|
* 'https://signal.example.com',
|
||||||
|
* 'alice',
|
||||||
|
* keypair,
|
||||||
|
* new NodeCryptoAdapter()
|
||||||
|
* )
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
export class NodeCryptoAdapter implements CryptoAdapter {
|
||||||
|
constructor() {
|
||||||
|
// Set SHA-512 hash function for ed25519 using Node's crypto.subtle
|
||||||
|
if (typeof crypto === 'undefined' || !crypto.subtle) {
|
||||||
|
throw new Error(
|
||||||
|
'crypto.subtle is not available. ' +
|
||||||
|
'Node.js 19+ is required, or Node.js 18 with --experimental-global-webcrypto flag'
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
ed25519.hashes.sha512Async = async (message: Uint8Array) => {
|
||||||
|
const hash = await crypto.subtle.digest('SHA-512', message as BufferSource)
|
||||||
|
return new Uint8Array(hash)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async generateKeypair(): Promise<Keypair> {
|
||||||
|
const privateKey = ed25519.utils.randomSecretKey()
|
||||||
|
const publicKey = await ed25519.getPublicKeyAsync(privateKey)
|
||||||
|
|
||||||
|
return {
|
||||||
|
publicKey: this.bytesToBase64(publicKey),
|
||||||
|
privateKey: this.bytesToBase64(privateKey),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async signMessage(message: string, privateKeyBase64: string): Promise<string> {
|
||||||
|
const privateKey = this.base64ToBytes(privateKeyBase64)
|
||||||
|
const encoder = new TextEncoder()
|
||||||
|
const messageBytes = encoder.encode(message)
|
||||||
|
const signature = await ed25519.signAsync(messageBytes, privateKey)
|
||||||
|
|
||||||
|
return this.bytesToBase64(signature)
|
||||||
|
}
|
||||||
|
|
||||||
|
async verifySignature(
|
||||||
|
message: string,
|
||||||
|
signatureBase64: string,
|
||||||
|
publicKeyBase64: string
|
||||||
|
): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
const signature = this.base64ToBytes(signatureBase64)
|
||||||
|
const publicKey = this.base64ToBytes(publicKeyBase64)
|
||||||
|
const encoder = new TextEncoder()
|
||||||
|
const messageBytes = encoder.encode(message)
|
||||||
|
|
||||||
|
return await ed25519.verifyAsync(signature, messageBytes, publicKey)
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bytesToBase64(bytes: Uint8Array): string {
|
||||||
|
// Node.js Buffer provides native base64 encoding
|
||||||
|
// @ts-expect-error - Buffer is available in Node.js but not in browser TypeScript definitions
|
||||||
|
return Buffer.from(bytes).toString('base64')
|
||||||
|
}
|
||||||
|
|
||||||
|
base64ToBytes(base64: string): Uint8Array {
|
||||||
|
// Node.js Buffer provides native base64 decoding
|
||||||
|
// @ts-expect-error - Buffer is available in Node.js but not in browser TypeScript definitions
|
||||||
|
return new Uint8Array(Buffer.from(base64, 'base64'))
|
||||||
|
}
|
||||||
|
|
||||||
|
randomBytes(length: number): Uint8Array {
|
||||||
|
// Use Web Crypto API's getRandomValues (available in Node 19+)
|
||||||
|
return crypto.getRandomValues(new Uint8Array(length))
|
||||||
|
}
|
||||||
|
}
|
||||||
338
src/offers.ts
338
src/offers.ts
@@ -1,338 +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 {
|
|
||||||
id?: string;
|
|
||||||
sdp: string;
|
|
||||||
topics: string[];
|
|
||||||
ttl?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Offer {
|
|
||||||
id: string;
|
|
||||||
peerId: string;
|
|
||||||
sdp: string;
|
|
||||||
topics: string[];
|
|
||||||
createdAt?: number;
|
|
||||||
expiresAt: number;
|
|
||||||
lastSeen: number;
|
|
||||||
answererPeerId?: string;
|
|
||||||
answerSdp?: string;
|
|
||||||
answeredAt?: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* RTCIceCandidateInit interface for environments without native WebRTC types
|
|
||||||
*/
|
|
||||||
export interface RTCIceCandidateInit {
|
|
||||||
candidate?: string;
|
|
||||||
sdpMid?: string | null;
|
|
||||||
sdpMLineIndex?: number | null;
|
|
||||||
usernameFragment?: string | null;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface IceCandidate {
|
|
||||||
candidate: RTCIceCandidateInit; // Full candidate object
|
|
||||||
peerId: string;
|
|
||||||
role: 'offerer' | 'answerer';
|
|
||||||
createdAt: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface TopicInfo {
|
|
||||||
topic: string;
|
|
||||||
activePeers: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class RondevuOffers {
|
|
||||||
private fetchFn: FetchFunction;
|
|
||||||
|
|
||||||
constructor(
|
|
||||||
private baseUrl: string,
|
|
||||||
private credentials: Credentials,
|
|
||||||
fetchFn?: FetchFunction
|
|
||||||
) {
|
|
||||||
// Use provided fetch or fall back to global fetch
|
|
||||||
this.fetchFn = fetchFn || ((...args) => {
|
|
||||||
if (typeof globalThis.fetch === 'function') {
|
|
||||||
return globalThis.fetch(...args);
|
|
||||||
}
|
|
||||||
throw new Error(
|
|
||||||
'fetch is not available. Please provide a fetch implementation in the constructor options.'
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create one or more offers
|
|
||||||
*/
|
|
||||||
async create(offers: CreateOfferRequest[]): Promise<Offer[]> {
|
|
||||||
const response = await this.fetchFn(`${this.baseUrl}/offers`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
Authorization: RondevuAuth.createAuthHeader(this.credentials),
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ offers }),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
|
||||||
throw new Error(`Failed to create offers: ${error.error || response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
return data.offers;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Find offers by topic with optional bloom filter
|
|
||||||
*/
|
|
||||||
async findByTopic(
|
|
||||||
topic: string,
|
|
||||||
options?: {
|
|
||||||
bloomFilter?: Uint8Array;
|
|
||||||
limit?: number;
|
|
||||||
}
|
|
||||||
): Promise<Offer[]> {
|
|
||||||
const params = new URLSearchParams();
|
|
||||||
|
|
||||||
if (options?.bloomFilter) {
|
|
||||||
// Convert to base64
|
|
||||||
const binaryString = String.fromCharCode(...Array.from(options.bloomFilter));
|
|
||||||
const base64 = typeof btoa !== 'undefined'
|
|
||||||
? btoa(binaryString)
|
|
||||||
: (typeof Buffer !== 'undefined' ? Buffer.from(options.bloomFilter).toString('base64') : '');
|
|
||||||
params.set('bloom', base64);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options?.limit) {
|
|
||||||
params.set('limit', options.limit.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
const url = `${this.baseUrl}/offers/by-topic/${encodeURIComponent(topic)}${
|
|
||||||
params.toString() ? '?' + params.toString() : ''
|
|
||||||
}`;
|
|
||||||
|
|
||||||
const response = await this.fetchFn(url, {
|
|
||||||
method: 'GET',
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
|
||||||
throw new Error(`Failed to find offers: ${error.error || response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
return data.offers;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get all offers from a specific peer
|
|
||||||
*/
|
|
||||||
async getByPeerId(peerId: string): Promise<{
|
|
||||||
offers: Offer[];
|
|
||||||
topics: string[];
|
|
||||||
}> {
|
|
||||||
const response = await this.fetchFn(`${this.baseUrl}/peers/${encodeURIComponent(peerId)}/offers`, {
|
|
||||||
method: 'GET',
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
|
||||||
throw new Error(`Failed to get peer offers: ${error.error || response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return await response.json();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get topics with active peer counts (paginated)
|
|
||||||
*/
|
|
||||||
async getTopics(options?: {
|
|
||||||
limit?: number;
|
|
||||||
offset?: number;
|
|
||||||
}): Promise<{
|
|
||||||
topics: TopicInfo[];
|
|
||||||
total: number;
|
|
||||||
limit: number;
|
|
||||||
offset: number;
|
|
||||||
}> {
|
|
||||||
const params = new URLSearchParams();
|
|
||||||
|
|
||||||
if (options?.limit) {
|
|
||||||
params.set('limit', options.limit.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (options?.offset) {
|
|
||||||
params.set('offset', options.offset.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
const url = `${this.baseUrl}/topics${
|
|
||||||
params.toString() ? '?' + params.toString() : ''
|
|
||||||
}`;
|
|
||||||
|
|
||||||
const response = await this.fetchFn(url, {
|
|
||||||
method: 'GET',
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
|
||||||
throw new Error(`Failed to get topics: ${error.error || response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return await response.json();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get own offers
|
|
||||||
*/
|
|
||||||
async getMine(): Promise<Offer[]> {
|
|
||||||
const response = await this.fetchFn(`${this.baseUrl}/offers/mine`, {
|
|
||||||
method: 'GET',
|
|
||||||
headers: {
|
|
||||||
Authorization: RondevuAuth.createAuthHeader(this.credentials),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
|
||||||
throw new Error(`Failed to get own offers: ${error.error || response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
return data.offers;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Update offer heartbeat
|
|
||||||
*/
|
|
||||||
async heartbeat(offerId: string): Promise<void> {
|
|
||||||
const response = await this.fetchFn(`${this.baseUrl}/offers/${encodeURIComponent(offerId)}/heartbeat`, {
|
|
||||||
method: 'PUT',
|
|
||||||
headers: {
|
|
||||||
Authorization: RondevuAuth.createAuthHeader(this.credentials),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
|
||||||
throw new Error(`Failed to update heartbeat: ${error.error || response.statusText}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Delete an offer
|
|
||||||
*/
|
|
||||||
async delete(offerId: string): Promise<void> {
|
|
||||||
const response = await this.fetchFn(`${this.baseUrl}/offers/${encodeURIComponent(offerId)}`, {
|
|
||||||
method: 'DELETE',
|
|
||||||
headers: {
|
|
||||||
Authorization: RondevuAuth.createAuthHeader(this.credentials),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
|
||||||
throw new Error(`Failed to delete offer: ${error.error || response.statusText}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Answer an offer
|
|
||||||
*/
|
|
||||||
async answer(offerId: string, sdp: string): Promise<void> {
|
|
||||||
const response = await this.fetchFn(`${this.baseUrl}/offers/${encodeURIComponent(offerId)}/answer`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
Authorization: RondevuAuth.createAuthHeader(this.credentials),
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ sdp }),
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
|
||||||
throw new Error(`Failed to answer offer: ${error.error || response.statusText}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Get answers to your offers
|
|
||||||
*/
|
|
||||||
async getAnswers(): Promise<Array<{
|
|
||||||
offerId: string;
|
|
||||||
answererId: string;
|
|
||||||
sdp: string;
|
|
||||||
answeredAt: number;
|
|
||||||
topics: string[];
|
|
||||||
}>> {
|
|
||||||
const response = await this.fetchFn(`${this.baseUrl}/offers/answers`, {
|
|
||||||
method: 'GET',
|
|
||||||
headers: {
|
|
||||||
Authorization: RondevuAuth.createAuthHeader(this.credentials),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const error = await response.json().catch(() => ({ error: 'Unknown error' }));
|
|
||||||
throw new Error(`Failed to get answers: ${error.error || response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const data = await response.json();
|
|
||||||
return data.answers;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Post ICE candidates for an offer
|
|
||||||
*/
|
|
||||||
async addIceCandidates(
|
|
||||||
offerId: string,
|
|
||||||
candidates: RTCIceCandidateInit[]
|
|
||||||
): 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
965
src/rondevu.ts
965
src/rondevu.ts
@@ -1,103 +1,888 @@
|
|||||||
import { RondevuAuth, Credentials, FetchFunction } from './auth.js';
|
import { RondevuAPI, Keypair, IceCandidate, BatcherOptions } from './api.js'
|
||||||
import { RondevuOffers } from './offers.js';
|
import { CryptoAdapter } from './crypto-adapter.js'
|
||||||
import { RondevuConnection, ConnectionOptions } from './connection.js';
|
|
||||||
|
// ICE server preset names
|
||||||
|
export type IceServerPreset = 'ipv4-turn' | 'hostname-turns' | 'google-stun' | 'relay-only'
|
||||||
|
|
||||||
|
// ICE server presets
|
||||||
|
export const ICE_SERVER_PRESETS: Record<IceServerPreset, RTCIceServer[]> = {
|
||||||
|
'ipv4-turn': [
|
||||||
|
{ urls: 'stun:57.129.61.67:3478' },
|
||||||
|
{
|
||||||
|
urls: [
|
||||||
|
'turn:57.129.61.67:3478?transport=tcp',
|
||||||
|
'turn:57.129.61.67:3478?transport=udp',
|
||||||
|
],
|
||||||
|
username: 'webrtcuser',
|
||||||
|
credential: 'supersecretpassword'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'hostname-turns': [
|
||||||
|
{ urls: 'stun:turn.share.fish:3478' },
|
||||||
|
{
|
||||||
|
urls: [
|
||||||
|
'turns:turn.share.fish:5349?transport=tcp',
|
||||||
|
'turns:turn.share.fish:5349?transport=udp',
|
||||||
|
'turn:turn.share.fish:3478?transport=tcp',
|
||||||
|
'turn:turn.share.fish:3478?transport=udp',
|
||||||
|
],
|
||||||
|
username: 'webrtcuser',
|
||||||
|
credential: 'supersecretpassword'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'google-stun': [
|
||||||
|
{ urls: 'stun:stun.l.google.com:19302' },
|
||||||
|
{ urls: 'stun:stun1.l.google.com:19302' }
|
||||||
|
],
|
||||||
|
'relay-only': [
|
||||||
|
{ urls: 'stun:57.129.61.67:3478' },
|
||||||
|
{
|
||||||
|
urls: [
|
||||||
|
'turn:57.129.61.67:3478?transport=tcp',
|
||||||
|
'turn:57.129.61.67:3478?transport=udp',
|
||||||
|
],
|
||||||
|
username: 'webrtcuser',
|
||||||
|
credential: 'supersecretpassword',
|
||||||
|
// @ts-expect-error - iceTransportPolicy is valid but not in RTCIceServer type
|
||||||
|
iceTransportPolicy: 'relay'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
export interface RondevuOptions {
|
export interface RondevuOptions {
|
||||||
/**
|
apiUrl: string
|
||||||
* Base URL of the Rondevu server
|
username?: string // Optional, will generate anonymous if not provided
|
||||||
* @default 'https://api.ronde.vu'
|
keypair?: Keypair // Optional, will generate if not provided
|
||||||
*/
|
cryptoAdapter?: CryptoAdapter // Optional, defaults to WebCryptoAdapter
|
||||||
baseUrl?: string;
|
batching?: BatcherOptions | false // Optional, defaults to enabled with default options
|
||||||
|
iceServers?: IceServerPreset | RTCIceServer[] // Optional: preset name or custom STUN/TURN servers
|
||||||
/**
|
debug?: boolean // Optional: enable debug logging (default: false)
|
||||||
* Existing credentials (peerId + secret) to skip registration
|
|
||||||
*/
|
|
||||||
credentials?: Credentials;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Custom fetch implementation for environments without native fetch
|
|
||||||
* (Node.js < 18, some Workers environments, etc.)
|
|
||||||
*
|
|
||||||
* @example Node.js
|
|
||||||
* ```typescript
|
|
||||||
* import fetch from 'node-fetch';
|
|
||||||
* const client = new Rondevu({ fetch });
|
|
||||||
* ```
|
|
||||||
*/
|
|
||||||
fetch?: FetchFunction;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface OfferContext {
|
||||||
|
pc: RTCPeerConnection
|
||||||
|
dc?: RTCDataChannel
|
||||||
|
offer: RTCSessionDescriptionInit
|
||||||
|
}
|
||||||
|
|
||||||
|
export type OfferFactory = (rtcConfig: RTCConfiguration) => Promise<OfferContext>
|
||||||
|
|
||||||
|
export interface PublishServiceOptions {
|
||||||
|
service: string // Service name and version (e.g., "chat:2.0.0") - username will be auto-appended
|
||||||
|
maxOffers: number // Maximum number of concurrent offers to maintain
|
||||||
|
offerFactory?: OfferFactory // Optional: custom offer creation (defaults to simple data channel)
|
||||||
|
ttl?: number // Time-to-live for offers in milliseconds (default: 300000)
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ConnectionContext {
|
||||||
|
pc: RTCPeerConnection
|
||||||
|
dc: RTCDataChannel
|
||||||
|
serviceFqn: string
|
||||||
|
offerId: string
|
||||||
|
peerUsername: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface ConnectToServiceOptions {
|
||||||
|
serviceFqn?: string // Full FQN like 'chat:2.0.0@alice'
|
||||||
|
service?: string // Service without username (for discovery)
|
||||||
|
username?: string // Target username (combined with service)
|
||||||
|
onConnection?: (context: ConnectionContext) => void | Promise<void> // Called when data channel opens
|
||||||
|
rtcConfig?: RTCConfiguration // Optional: override default ICE servers
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ActiveOffer {
|
||||||
|
offerId: string
|
||||||
|
serviceFqn: string
|
||||||
|
pc: RTCPeerConnection
|
||||||
|
dc?: RTCDataChannel
|
||||||
|
answered: boolean
|
||||||
|
createdAt: number
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rondevu - Complete WebRTC signaling client
|
||||||
|
*
|
||||||
|
* Provides a unified API for:
|
||||||
|
* - Implicit username claiming (auto-claimed on first authenticated request)
|
||||||
|
* - Service publishing with automatic signature generation
|
||||||
|
* - Service discovery (direct, random, paginated)
|
||||||
|
* - WebRTC signaling (offer/answer exchange, ICE relay)
|
||||||
|
* - Keypair management
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```typescript
|
||||||
|
* // Create and initialize Rondevu instance with preset ICE servers
|
||||||
|
* const rondevu = await Rondevu.connect({
|
||||||
|
* apiUrl: 'https://signal.example.com',
|
||||||
|
* username: 'alice',
|
||||||
|
* iceServers: 'ipv4-turn' // Use preset: 'ipv4-turn', 'hostname-turns', 'google-stun', or 'relay-only'
|
||||||
|
* })
|
||||||
|
*
|
||||||
|
* // Or use custom ICE servers
|
||||||
|
* const rondevu2 = await Rondevu.connect({
|
||||||
|
* apiUrl: 'https://signal.example.com',
|
||||||
|
* username: 'bob',
|
||||||
|
* iceServers: [
|
||||||
|
* { urls: 'stun:stun.l.google.com:19302' },
|
||||||
|
* { urls: 'turn:turn.example.com:3478', username: 'user', credential: 'pass' }
|
||||||
|
* ]
|
||||||
|
* })
|
||||||
|
*
|
||||||
|
* // Publish a service with automatic offer management
|
||||||
|
* await rondevu.publishService({
|
||||||
|
* service: 'chat:2.0.0',
|
||||||
|
* maxOffers: 5, // Maintain up to 5 concurrent offers
|
||||||
|
* offerFactory: async (rtcConfig) => {
|
||||||
|
* const pc = new RTCPeerConnection(rtcConfig)
|
||||||
|
* const dc = pc.createDataChannel('chat')
|
||||||
|
* const offer = await pc.createOffer()
|
||||||
|
* await pc.setLocalDescription(offer)
|
||||||
|
* return { pc, dc, offer }
|
||||||
|
* }
|
||||||
|
* })
|
||||||
|
*
|
||||||
|
* // Start accepting connections (auto-fills offers and polls)
|
||||||
|
* await rondevu.startFilling()
|
||||||
|
*
|
||||||
|
* // Access active connections
|
||||||
|
* for (const offer of rondevu.getActiveOffers()) {
|
||||||
|
* offer.dc?.addEventListener('message', (e) => console.log(e.data))
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* // Stop when done
|
||||||
|
* rondevu.stopFilling()
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
export class Rondevu {
|
export class Rondevu {
|
||||||
readonly auth: RondevuAuth;
|
// Constants
|
||||||
private _offers?: RondevuOffers;
|
private static readonly DEFAULT_TTL_MS = 300000 // 5 minutes
|
||||||
private credentials?: Credentials;
|
private static readonly POLLING_INTERVAL_MS = 1000 // 1 second
|
||||||
private baseUrl: string;
|
|
||||||
private fetchFn?: FetchFunction;
|
|
||||||
|
|
||||||
constructor(options: RondevuOptions = {}) {
|
private api: RondevuAPI
|
||||||
this.baseUrl = options.baseUrl || 'https://api.ronde.vu';
|
private readonly apiUrl: string
|
||||||
this.fetchFn = options.fetch;
|
private username: string
|
||||||
|
private keypair: Keypair
|
||||||
|
private usernameClaimed = false
|
||||||
|
private cryptoAdapter?: CryptoAdapter
|
||||||
|
private batchingOptions?: BatcherOptions | false
|
||||||
|
private iceServers: RTCIceServer[]
|
||||||
|
private debugEnabled: boolean
|
||||||
|
|
||||||
this.auth = new RondevuAuth(this.baseUrl, this.fetchFn);
|
// Service management
|
||||||
|
private currentService: string | null = null
|
||||||
|
private maxOffers = 0
|
||||||
|
private offerFactory: OfferFactory | null = null
|
||||||
|
private ttl = Rondevu.DEFAULT_TTL_MS
|
||||||
|
private activeOffers = new Map<string, ActiveOffer>()
|
||||||
|
|
||||||
if (options.credentials) {
|
// Polling
|
||||||
this.credentials = options.credentials;
|
private filling = false
|
||||||
this._offers = new RondevuOffers(this.baseUrl, this.credentials, this.fetchFn);
|
private pollingInterval: ReturnType<typeof setInterval> | null = null
|
||||||
}
|
private lastPollTimestamp = 0
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
private constructor(
|
||||||
* Get offers API (requires authentication)
|
apiUrl: string,
|
||||||
*/
|
username: string,
|
||||||
get offers(): RondevuOffers {
|
keypair: Keypair,
|
||||||
if (!this._offers) {
|
api: RondevuAPI,
|
||||||
throw new Error('Not authenticated. Call register() first or provide credentials.');
|
iceServers: RTCIceServer[],
|
||||||
}
|
cryptoAdapter?: CryptoAdapter,
|
||||||
return this._offers;
|
batchingOptions?: BatcherOptions | false,
|
||||||
}
|
debugEnabled = false
|
||||||
|
) {
|
||||||
|
this.apiUrl = apiUrl
|
||||||
|
this.username = username
|
||||||
|
this.keypair = keypair
|
||||||
|
this.api = api
|
||||||
|
this.iceServers = iceServers
|
||||||
|
this.cryptoAdapter = cryptoAdapter
|
||||||
|
this.batchingOptions = batchingOptions
|
||||||
|
this.debugEnabled = debugEnabled
|
||||||
|
|
||||||
/**
|
this.debug('Instance created:', {
|
||||||
* Register and initialize authenticated client
|
username: this.username,
|
||||||
*/
|
publicKey: this.keypair.publicKey,
|
||||||
async register(): Promise<Credentials> {
|
hasIceServers: iceServers.length > 0,
|
||||||
this.credentials = await this.auth.register();
|
batchingEnabled: batchingOptions !== false
|
||||||
|
})
|
||||||
// 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 connection (requires authentication)
|
|
||||||
* This is a high-level helper that creates and manages WebRTC connections
|
|
||||||
*
|
|
||||||
* @param rtcConfig Optional RTCConfiguration for the peer connection
|
|
||||||
* @returns RondevuConnection instance
|
|
||||||
*/
|
|
||||||
createConnection(rtcConfig?: RTCConfiguration): RondevuConnection {
|
|
||||||
if (!this._offers) {
|
|
||||||
throw new Error('Not authenticated. Call register() first or provide credentials.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new RondevuConnection(this._offers, rtcConfig);
|
/**
|
||||||
}
|
* Internal debug logging - only logs if debug mode is enabled
|
||||||
|
*/
|
||||||
|
private debug(message: string, ...args: any[]): void {
|
||||||
|
if (this.debugEnabled) {
|
||||||
|
console.log(`[Rondevu] ${message}`, ...args)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create and initialize a Rondevu client
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```typescript
|
||||||
|
* const rondevu = await Rondevu.connect({
|
||||||
|
* apiUrl: 'https://api.ronde.vu',
|
||||||
|
* username: 'alice'
|
||||||
|
* })
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
static async connect(options: RondevuOptions): Promise<Rondevu> {
|
||||||
|
const username = options.username || Rondevu.generateAnonymousUsername()
|
||||||
|
|
||||||
|
// Handle preset string or custom array
|
||||||
|
let iceServers: RTCIceServer[]
|
||||||
|
if (typeof options.iceServers === 'string') {
|
||||||
|
iceServers = ICE_SERVER_PRESETS[options.iceServers]
|
||||||
|
} else {
|
||||||
|
iceServers = options.iceServers || [
|
||||||
|
{ urls: 'stun:stun.l.google.com:19302' }
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.debug) {
|
||||||
|
console.log('[Rondevu] Connecting:', {
|
||||||
|
username,
|
||||||
|
hasKeypair: !!options.keypair,
|
||||||
|
iceServers: iceServers.length,
|
||||||
|
batchingEnabled: options.batching !== false
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate keypair if not provided
|
||||||
|
let keypair = options.keypair
|
||||||
|
if (!keypair) {
|
||||||
|
if (options.debug) console.log('[Rondevu] Generating new keypair...')
|
||||||
|
keypair = await RondevuAPI.generateKeypair(options.cryptoAdapter)
|
||||||
|
if (options.debug) console.log('[Rondevu] Generated keypair, publicKey:', keypair.publicKey)
|
||||||
|
} else {
|
||||||
|
if (options.debug) console.log('[Rondevu] Using existing keypair, publicKey:', keypair.publicKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create API instance
|
||||||
|
const api = new RondevuAPI(
|
||||||
|
options.apiUrl,
|
||||||
|
username,
|
||||||
|
keypair,
|
||||||
|
options.cryptoAdapter,
|
||||||
|
options.batching
|
||||||
|
)
|
||||||
|
if (options.debug) console.log('[Rondevu] Created API instance')
|
||||||
|
|
||||||
|
return new Rondevu(
|
||||||
|
options.apiUrl,
|
||||||
|
username,
|
||||||
|
keypair,
|
||||||
|
api,
|
||||||
|
iceServers,
|
||||||
|
options.cryptoAdapter,
|
||||||
|
options.batching,
|
||||||
|
options.debug || false
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate an anonymous username with timestamp and random component
|
||||||
|
*/
|
||||||
|
private static generateAnonymousUsername(): string {
|
||||||
|
const timestamp = Date.now().toString(36)
|
||||||
|
const random = Array.from(crypto.getRandomValues(new Uint8Array(3)))
|
||||||
|
.map(b => b.toString(16).padStart(2, '0')).join('')
|
||||||
|
return `anon-${timestamp}-${random}`
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// Username Management
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if username has been claimed (checks with server)
|
||||||
|
*/
|
||||||
|
async isUsernameClaimed(): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
const claimed = await this.api.isUsernameClaimed()
|
||||||
|
|
||||||
|
// Update internal flag to match server state
|
||||||
|
this.usernameClaimed = claimed
|
||||||
|
|
||||||
|
return claimed
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Failed to check username claim status:', err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// Service Publishing
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Default offer factory - creates a simple data channel connection
|
||||||
|
*/
|
||||||
|
private async defaultOfferFactory(rtcConfig: RTCConfiguration): Promise<OfferContext> {
|
||||||
|
const pc = new RTCPeerConnection(rtcConfig)
|
||||||
|
const dc = pc.createDataChannel('default')
|
||||||
|
|
||||||
|
const offer = await pc.createOffer()
|
||||||
|
await pc.setLocalDescription(offer)
|
||||||
|
|
||||||
|
return { pc, dc, offer }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Publish a service with automatic offer management
|
||||||
|
* Call startFilling() to begin accepting connections
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```typescript
|
||||||
|
* await rondevu.publishService({
|
||||||
|
* service: 'chat:2.0.0',
|
||||||
|
* maxOffers: 5
|
||||||
|
* })
|
||||||
|
* await rondevu.startFilling()
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
async publishService(options: PublishServiceOptions): Promise<void> {
|
||||||
|
const { service, maxOffers, offerFactory, ttl } = options
|
||||||
|
|
||||||
|
this.currentService = service
|
||||||
|
this.maxOffers = maxOffers
|
||||||
|
this.offerFactory = offerFactory || this.defaultOfferFactory.bind(this)
|
||||||
|
this.ttl = ttl || Rondevu.DEFAULT_TTL_MS
|
||||||
|
|
||||||
|
this.debug(`Publishing service: ${service} with maxOffers: ${maxOffers}`)
|
||||||
|
this.usernameClaimed = true
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set up ICE candidate handler to send candidates to the server
|
||||||
|
*/
|
||||||
|
private setupIceCandidateHandler(
|
||||||
|
pc: RTCPeerConnection,
|
||||||
|
serviceFqn: string,
|
||||||
|
offerId: string
|
||||||
|
): void {
|
||||||
|
pc.onicecandidate = async (event) => {
|
||||||
|
if (event.candidate) {
|
||||||
|
try {
|
||||||
|
await this.api.addOfferIceCandidates(
|
||||||
|
serviceFqn,
|
||||||
|
offerId,
|
||||||
|
[event.candidate.toJSON()]
|
||||||
|
)
|
||||||
|
} catch (err) {
|
||||||
|
console.error('[Rondevu] Failed to send ICE candidate:', err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a single offer and publish it to the server
|
||||||
|
*/
|
||||||
|
private async createOffer(): Promise<void> {
|
||||||
|
if (!this.currentService || !this.offerFactory) {
|
||||||
|
throw new Error('Service not published. Call publishService() first.')
|
||||||
|
}
|
||||||
|
|
||||||
|
const rtcConfig: RTCConfiguration = {
|
||||||
|
iceServers: this.iceServers
|
||||||
|
}
|
||||||
|
|
||||||
|
this.debug('Creating new offer...')
|
||||||
|
|
||||||
|
// Create the offer using the factory
|
||||||
|
const { pc, dc, offer } = await this.offerFactory(rtcConfig)
|
||||||
|
|
||||||
|
// Auto-append username to service
|
||||||
|
const serviceFqn = `${this.currentService}@${this.username}`
|
||||||
|
|
||||||
|
// Publish to server
|
||||||
|
const result = await this.api.publishService({
|
||||||
|
serviceFqn,
|
||||||
|
offers: [{ sdp: offer.sdp! }],
|
||||||
|
ttl: this.ttl,
|
||||||
|
signature: '',
|
||||||
|
message: '',
|
||||||
|
})
|
||||||
|
|
||||||
|
const offerId = result.offers[0].offerId
|
||||||
|
|
||||||
|
// Store active offer
|
||||||
|
this.activeOffers.set(offerId, {
|
||||||
|
offerId,
|
||||||
|
serviceFqn,
|
||||||
|
pc,
|
||||||
|
dc,
|
||||||
|
answered: false,
|
||||||
|
createdAt: Date.now()
|
||||||
|
})
|
||||||
|
|
||||||
|
this.debug(`Offer created: ${offerId}`)
|
||||||
|
|
||||||
|
// Set up ICE candidate handler
|
||||||
|
this.setupIceCandidateHandler(pc, serviceFqn, offerId)
|
||||||
|
|
||||||
|
// Monitor connection state
|
||||||
|
pc.onconnectionstatechange = () => {
|
||||||
|
this.debug(`Offer ${offerId} connection state: ${pc.connectionState}`)
|
||||||
|
|
||||||
|
if (pc.connectionState === 'failed' || pc.connectionState === 'closed') {
|
||||||
|
this.activeOffers.delete(offerId)
|
||||||
|
this.fillOffers() // Try to replace failed offer
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fill offers to reach maxOffers count
|
||||||
|
*/
|
||||||
|
private async fillOffers(): Promise<void> {
|
||||||
|
if (!this.filling || !this.currentService) return
|
||||||
|
|
||||||
|
const currentCount = this.activeOffers.size
|
||||||
|
const needed = this.maxOffers - currentCount
|
||||||
|
|
||||||
|
this.debug(`Filling offers: current=${currentCount}, needed=${needed}`)
|
||||||
|
|
||||||
|
for (let i = 0; i < needed; i++) {
|
||||||
|
try {
|
||||||
|
await this.createOffer()
|
||||||
|
} catch (err) {
|
||||||
|
console.error('[Rondevu] Failed to create offer:', err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Poll for answers and ICE candidates (internal use for automatic offer management)
|
||||||
|
*/
|
||||||
|
private async pollInternal(): Promise<void> {
|
||||||
|
if (!this.filling) return
|
||||||
|
|
||||||
|
try {
|
||||||
|
const result = await this.api.poll(this.lastPollTimestamp)
|
||||||
|
|
||||||
|
// Process answers
|
||||||
|
for (const answer of result.answers) {
|
||||||
|
const activeOffer = this.activeOffers.get(answer.offerId)
|
||||||
|
if (activeOffer && !activeOffer.answered) {
|
||||||
|
this.debug(`Received answer for offer ${answer.offerId}`)
|
||||||
|
|
||||||
|
await activeOffer.pc.setRemoteDescription({
|
||||||
|
type: 'answer',
|
||||||
|
sdp: answer.sdp
|
||||||
|
})
|
||||||
|
|
||||||
|
activeOffer.answered = true
|
||||||
|
this.lastPollTimestamp = answer.answeredAt
|
||||||
|
|
||||||
|
// Create replacement offer
|
||||||
|
this.fillOffers()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Process ICE candidates
|
||||||
|
for (const [offerId, candidates] of Object.entries(result.iceCandidates)) {
|
||||||
|
const activeOffer = this.activeOffers.get(offerId)
|
||||||
|
if (activeOffer) {
|
||||||
|
const answererCandidates = candidates.filter(c => c.role === 'answerer')
|
||||||
|
|
||||||
|
for (const item of answererCandidates) {
|
||||||
|
if (item.candidate) {
|
||||||
|
await activeOffer.pc.addIceCandidate(new RTCIceCandidate(item.candidate))
|
||||||
|
this.lastPollTimestamp = Math.max(this.lastPollTimestamp, item.createdAt)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('[Rondevu] Polling error:', err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start filling offers and polling for answers/ICE
|
||||||
|
* Call this after publishService() to begin accepting connections
|
||||||
|
*/
|
||||||
|
async startFilling(): Promise<void> {
|
||||||
|
if (this.filling) {
|
||||||
|
this.debug('Already filling')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.currentService) {
|
||||||
|
throw new Error('No service published. Call publishService() first.')
|
||||||
|
}
|
||||||
|
|
||||||
|
this.debug('Starting offer filling and polling')
|
||||||
|
this.filling = true
|
||||||
|
|
||||||
|
// Fill initial offers
|
||||||
|
await this.fillOffers()
|
||||||
|
|
||||||
|
// Start polling
|
||||||
|
this.pollingInterval = setInterval(() => {
|
||||||
|
this.pollInternal()
|
||||||
|
}, Rondevu.POLLING_INTERVAL_MS)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop filling offers and polling
|
||||||
|
* Closes all active peer connections
|
||||||
|
*/
|
||||||
|
stopFilling(): void {
|
||||||
|
this.debug('Stopping offer filling and polling')
|
||||||
|
this.filling = false
|
||||||
|
|
||||||
|
// Stop polling
|
||||||
|
if (this.pollingInterval) {
|
||||||
|
clearInterval(this.pollingInterval)
|
||||||
|
this.pollingInterval = null
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close all active connections
|
||||||
|
for (const [offerId, offer] of this.activeOffers.entries()) {
|
||||||
|
this.debug(`Closing offer ${offerId}`)
|
||||||
|
offer.dc?.close()
|
||||||
|
offer.pc.close()
|
||||||
|
}
|
||||||
|
|
||||||
|
this.activeOffers.clear()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolve the full service FQN from various input options
|
||||||
|
* Supports direct FQN, service+username, or service discovery
|
||||||
|
*/
|
||||||
|
private async resolveServiceFqn(options: ConnectToServiceOptions): Promise<string> {
|
||||||
|
const { serviceFqn, service, username } = options
|
||||||
|
|
||||||
|
if (serviceFqn) {
|
||||||
|
return serviceFqn
|
||||||
|
} else if (service && username) {
|
||||||
|
return `${service}@${username}`
|
||||||
|
} else if (service) {
|
||||||
|
// Discovery mode - get random service
|
||||||
|
this.debug(`Discovering service: ${service}`)
|
||||||
|
const discovered = await this.discoverService(service)
|
||||||
|
return discovered.serviceFqn
|
||||||
|
} else {
|
||||||
|
throw new Error('Either serviceFqn or service must be provided')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Start polling for remote ICE candidates
|
||||||
|
* Returns the polling interval ID
|
||||||
|
*/
|
||||||
|
private startIcePolling(
|
||||||
|
pc: RTCPeerConnection,
|
||||||
|
serviceFqn: string,
|
||||||
|
offerId: string
|
||||||
|
): ReturnType<typeof setInterval> {
|
||||||
|
let lastIceTimestamp = 0
|
||||||
|
|
||||||
|
return setInterval(async () => {
|
||||||
|
try {
|
||||||
|
const result = await this.api.getOfferIceCandidates(
|
||||||
|
serviceFqn,
|
||||||
|
offerId,
|
||||||
|
lastIceTimestamp
|
||||||
|
)
|
||||||
|
for (const item of result.candidates) {
|
||||||
|
if (item.candidate) {
|
||||||
|
await pc.addIceCandidate(new RTCIceCandidate(item.candidate))
|
||||||
|
lastIceTimestamp = item.createdAt
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.error('[Rondevu] Failed to poll ICE candidates:', err)
|
||||||
|
}
|
||||||
|
}, Rondevu.POLLING_INTERVAL_MS)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Automatically connect to a service (answerer side)
|
||||||
|
* Handles the entire connection flow: discovery, WebRTC setup, answer exchange, ICE candidates
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```typescript
|
||||||
|
* // Connect to specific user
|
||||||
|
* const connection = await rondevu.connectToService({
|
||||||
|
* serviceFqn: 'chat:2.0.0@alice',
|
||||||
|
* onConnection: ({ dc, peerUsername }) => {
|
||||||
|
* console.log('Connected to', peerUsername)
|
||||||
|
* dc.addEventListener('message', (e) => console.log(e.data))
|
||||||
|
* dc.addEventListener('open', () => dc.send('Hello!'))
|
||||||
|
* }
|
||||||
|
* })
|
||||||
|
*
|
||||||
|
* // Discover random service
|
||||||
|
* const connection = await rondevu.connectToService({
|
||||||
|
* service: 'chat:2.0.0',
|
||||||
|
* onConnection: ({ dc, peerUsername }) => {
|
||||||
|
* console.log('Connected to', peerUsername)
|
||||||
|
* }
|
||||||
|
* })
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
async connectToService(options: ConnectToServiceOptions): Promise<ConnectionContext> {
|
||||||
|
const { onConnection, rtcConfig } = options
|
||||||
|
|
||||||
|
// Validate inputs
|
||||||
|
if (options.serviceFqn !== undefined && typeof options.serviceFqn === 'string' && !options.serviceFqn.trim()) {
|
||||||
|
throw new Error('serviceFqn cannot be empty')
|
||||||
|
}
|
||||||
|
if (options.service !== undefined && typeof options.service === 'string' && !options.service.trim()) {
|
||||||
|
throw new Error('service cannot be empty')
|
||||||
|
}
|
||||||
|
if (options.username !== undefined && typeof options.username === 'string' && !options.username.trim()) {
|
||||||
|
throw new Error('username cannot be empty')
|
||||||
|
}
|
||||||
|
|
||||||
|
// Determine the full service FQN
|
||||||
|
const fqn = await this.resolveServiceFqn(options)
|
||||||
|
this.debug(`Connecting to service: ${fqn}`)
|
||||||
|
|
||||||
|
// 1. Get service offer
|
||||||
|
const serviceData = await this.api.getService(fqn)
|
||||||
|
this.debug(`Found service from @${serviceData.username}`)
|
||||||
|
|
||||||
|
// 2. Create RTCPeerConnection
|
||||||
|
const rtcConfiguration = rtcConfig || {
|
||||||
|
iceServers: this.iceServers
|
||||||
|
}
|
||||||
|
const pc = new RTCPeerConnection(rtcConfiguration)
|
||||||
|
|
||||||
|
// 3. Set up data channel handler (answerer receives it from offerer)
|
||||||
|
let dc: RTCDataChannel | null = null
|
||||||
|
const dataChannelPromise = new Promise<RTCDataChannel>((resolve) => {
|
||||||
|
pc.ondatachannel = (event) => {
|
||||||
|
this.debug('Data channel received from offerer')
|
||||||
|
dc = event.channel
|
||||||
|
resolve(dc)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// 4. Set up ICE candidate exchange
|
||||||
|
this.setupIceCandidateHandler(pc, serviceData.serviceFqn, serviceData.offerId)
|
||||||
|
|
||||||
|
// 5. Poll for remote ICE candidates
|
||||||
|
const icePollInterval = this.startIcePolling(pc, serviceData.serviceFqn, serviceData.offerId)
|
||||||
|
|
||||||
|
// 6. Set remote description
|
||||||
|
await pc.setRemoteDescription({
|
||||||
|
type: 'offer',
|
||||||
|
sdp: serviceData.sdp
|
||||||
|
})
|
||||||
|
|
||||||
|
// 7. Create and send answer
|
||||||
|
const answer = await pc.createAnswer()
|
||||||
|
await pc.setLocalDescription(answer)
|
||||||
|
await this.api.answerOffer(
|
||||||
|
serviceData.serviceFqn,
|
||||||
|
serviceData.offerId,
|
||||||
|
answer.sdp!
|
||||||
|
)
|
||||||
|
|
||||||
|
// 8. Wait for data channel to be established
|
||||||
|
dc = await dataChannelPromise
|
||||||
|
|
||||||
|
// Create connection context
|
||||||
|
const context: ConnectionContext = {
|
||||||
|
pc,
|
||||||
|
dc,
|
||||||
|
serviceFqn: serviceData.serviceFqn,
|
||||||
|
offerId: serviceData.offerId,
|
||||||
|
peerUsername: serviceData.username
|
||||||
|
}
|
||||||
|
|
||||||
|
// 9. Set up connection state monitoring
|
||||||
|
pc.onconnectionstatechange = () => {
|
||||||
|
this.debug(`Connection state: ${pc.connectionState}`)
|
||||||
|
if (pc.connectionState === 'failed' || pc.connectionState === 'closed') {
|
||||||
|
clearInterval(icePollInterval)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 10. Wait for data channel to open and call onConnection
|
||||||
|
if (dc.readyState === 'open') {
|
||||||
|
this.debug('Data channel already open')
|
||||||
|
if (onConnection) {
|
||||||
|
await onConnection(context)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
await new Promise<void>((resolve) => {
|
||||||
|
dc!.addEventListener('open', async () => {
|
||||||
|
this.debug('Data channel opened')
|
||||||
|
if (onConnection) {
|
||||||
|
await onConnection(context)
|
||||||
|
}
|
||||||
|
resolve()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return context
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// 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.getService(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.getService(serviceVersion, { limit, offset })
|
||||||
|
}
|
||||||
|
|
||||||
|
// ============================================
|
||||||
|
// WebRTC Signaling
|
||||||
|
// ============================================
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Post answer SDP to specific offer
|
||||||
|
*/
|
||||||
|
async postOfferAnswer(serviceFqn: string, offerId: string, sdp: string): Promise<{
|
||||||
|
success: boolean
|
||||||
|
offerId: string
|
||||||
|
}> {
|
||||||
|
await this.api.answerOffer(serviceFqn, offerId, sdp)
|
||||||
|
return { success: true, offerId }
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Combined polling for answers and ICE candidates
|
||||||
|
* Returns all answered offers and ICE candidates for all peer's offers since timestamp
|
||||||
|
*/
|
||||||
|
async poll(since?: number): Promise<{
|
||||||
|
answers: Array<{
|
||||||
|
offerId: string
|
||||||
|
serviceId?: string
|
||||||
|
answererId: string
|
||||||
|
sdp: string
|
||||||
|
answeredAt: number
|
||||||
|
}>
|
||||||
|
iceCandidates: Record<string, Array<{
|
||||||
|
candidate: RTCIceCandidateInit | null
|
||||||
|
role: 'offerer' | 'answerer'
|
||||||
|
peerId: string
|
||||||
|
createdAt: number
|
||||||
|
}>>
|
||||||
|
}> {
|
||||||
|
return await this.api.poll(since)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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 {
|
||||||
|
return this.keypair
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the username
|
||||||
|
*/
|
||||||
|
getUsername(): string {
|
||||||
|
return this.username
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the public key
|
||||||
|
*/
|
||||||
|
getPublicKey(): string {
|
||||||
|
return this.keypair.publicKey
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Access to underlying API for advanced operations
|
||||||
|
* @deprecated Use direct methods on Rondevu instance instead
|
||||||
|
*/
|
||||||
|
getAPIPublic(): RondevuAPI {
|
||||||
|
return this.api
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
157
src/rpc-batcher.ts
Normal file
157
src/rpc-batcher.ts
Normal file
@@ -0,0 +1,157 @@
|
|||||||
|
/**
|
||||||
|
* RPC Batcher - Throttles and batches RPC requests to reduce HTTP overhead
|
||||||
|
*/
|
||||||
|
|
||||||
|
export interface BatcherOptions {
|
||||||
|
/**
|
||||||
|
* Maximum number of requests to batch together
|
||||||
|
* Default: 10
|
||||||
|
*/
|
||||||
|
maxBatchSize?: number
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maximum time to wait before sending a batch (ms)
|
||||||
|
* Default: 50ms
|
||||||
|
*/
|
||||||
|
maxWaitTime?: number
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Minimum time between batches (ms)
|
||||||
|
* Default: 10ms
|
||||||
|
*/
|
||||||
|
throttleInterval?: number
|
||||||
|
}
|
||||||
|
|
||||||
|
interface QueuedRequest {
|
||||||
|
request: any
|
||||||
|
resolve: (value: any) => void
|
||||||
|
reject: (error: Error) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Batches and throttles RPC requests to optimize network usage
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```typescript
|
||||||
|
* const batcher = new RpcBatcher(
|
||||||
|
* (requests) => api.rpcBatch(requests),
|
||||||
|
* { maxBatchSize: 10, maxWaitTime: 50 }
|
||||||
|
* )
|
||||||
|
*
|
||||||
|
* // These will be batched together if called within maxWaitTime
|
||||||
|
* const result1 = await batcher.add(request1)
|
||||||
|
* const result2 = await batcher.add(request2)
|
||||||
|
* const result3 = await batcher.add(request3)
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
export class RpcBatcher {
|
||||||
|
private queue: QueuedRequest[] = []
|
||||||
|
private batchTimeout: ReturnType<typeof setTimeout> | null = null
|
||||||
|
private lastBatchTime: number = 0
|
||||||
|
private options: Required<BatcherOptions>
|
||||||
|
private sendBatch: (requests: any[]) => Promise<any[]>
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
sendBatch: (requests: any[]) => Promise<any[]>,
|
||||||
|
options?: BatcherOptions
|
||||||
|
) {
|
||||||
|
this.sendBatch = sendBatch
|
||||||
|
this.options = {
|
||||||
|
maxBatchSize: options?.maxBatchSize ?? 10,
|
||||||
|
maxWaitTime: options?.maxWaitTime ?? 50,
|
||||||
|
throttleInterval: options?.throttleInterval ?? 10,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add an RPC request to the batch queue
|
||||||
|
* Returns a promise that resolves when the request completes
|
||||||
|
*/
|
||||||
|
async add(request: any): Promise<any> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
this.queue.push({ request, resolve, reject })
|
||||||
|
|
||||||
|
// Send immediately if batch is full
|
||||||
|
if (this.queue.length >= this.options.maxBatchSize) {
|
||||||
|
this.flush()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Schedule batch if not already scheduled
|
||||||
|
if (!this.batchTimeout) {
|
||||||
|
this.batchTimeout = setTimeout(() => {
|
||||||
|
this.flush()
|
||||||
|
}, this.options.maxWaitTime)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Flush the queue immediately
|
||||||
|
*/
|
||||||
|
async flush(): Promise<void> {
|
||||||
|
// Clear timeout if set
|
||||||
|
if (this.batchTimeout) {
|
||||||
|
clearTimeout(this.batchTimeout)
|
||||||
|
this.batchTimeout = null
|
||||||
|
}
|
||||||
|
|
||||||
|
// Nothing to flush
|
||||||
|
if (this.queue.length === 0) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Throttle: wait if we sent a batch too recently
|
||||||
|
const now = Date.now()
|
||||||
|
const timeSinceLastBatch = now - this.lastBatchTime
|
||||||
|
if (timeSinceLastBatch < this.options.throttleInterval) {
|
||||||
|
const waitTime = this.options.throttleInterval - timeSinceLastBatch
|
||||||
|
await new Promise(resolve => setTimeout(resolve, waitTime))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract requests from queue
|
||||||
|
const batch = this.queue.splice(0, this.options.maxBatchSize)
|
||||||
|
const requests = batch.map(item => item.request)
|
||||||
|
|
||||||
|
this.lastBatchTime = Date.now()
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Send batch request
|
||||||
|
const results = await this.sendBatch(requests)
|
||||||
|
|
||||||
|
// Resolve individual promises
|
||||||
|
for (let i = 0; i < batch.length; i++) {
|
||||||
|
batch[i].resolve(results[i])
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
// Reject all promises in batch
|
||||||
|
for (const item of batch) {
|
||||||
|
item.reject(error as Error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get current queue size
|
||||||
|
*/
|
||||||
|
getQueueSize(): number {
|
||||||
|
return this.queue.length
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clear the queue without sending
|
||||||
|
*/
|
||||||
|
clear(): void {
|
||||||
|
if (this.batchTimeout) {
|
||||||
|
clearTimeout(this.batchTimeout)
|
||||||
|
this.batchTimeout = null
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reject all pending requests
|
||||||
|
for (const item of this.queue) {
|
||||||
|
item.reject(new Error('Batch queue cleared'))
|
||||||
|
}
|
||||||
|
|
||||||
|
this.queue = []
|
||||||
|
}
|
||||||
|
}
|
||||||
188
src/types.ts
188
src/types.ts
@@ -1,182 +1,20 @@
|
|||||||
// ============================================================================
|
/**
|
||||||
// Signaling Types
|
* Core signaling types
|
||||||
// ============================================================================
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Session side - identifies which peer in a connection
|
* Cleanup function returned by listener methods
|
||||||
*/
|
*/
|
||||||
export type Side = 'offerer' | 'answerer';
|
export type Binnable = () => void
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Request body for POST /offer
|
* Signaler interface for WebRTC offer/answer/ICE exchange
|
||||||
*/
|
*/
|
||||||
export interface CreateOfferRequest {
|
export interface Signaler {
|
||||||
/** Peer identifier/metadata (max 1024 characters) */
|
addIceCandidate(candidate: RTCIceCandidate): Promise<void>
|
||||||
peerId: string;
|
addListener(callback: (candidate: RTCIceCandidate) => void): Binnable
|
||||||
/** Signaling data for peer connection */
|
addOfferListener(callback: (offer: RTCSessionDescriptionInit) => void): Binnable
|
||||||
offer: string;
|
addAnswerListener(callback: (answer: RTCSessionDescriptionInit) => void): Binnable
|
||||||
/** Optional custom connection code (if not provided, server generates UUID) */
|
setOffer(offer: RTCSessionDescriptionInit): Promise<void>
|
||||||
code?: string;
|
setAnswer(answer: RTCSessionDescriptionInit): Promise<void>
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from POST /offer
|
|
||||||
*/
|
|
||||||
export interface CreateOfferResponse {
|
|
||||||
/** Unique session identifier (UUID) */
|
|
||||||
code: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Request body for POST /answer
|
|
||||||
*/
|
|
||||||
export interface AnswerRequest {
|
|
||||||
/** Session UUID from the offer */
|
|
||||||
code: string;
|
|
||||||
/** Response signaling data (required if candidate not provided) */
|
|
||||||
answer?: string;
|
|
||||||
/** Additional signaling data (required if answer not provided) */
|
|
||||||
candidate?: string;
|
|
||||||
/** Which peer is sending the data */
|
|
||||||
side: Side;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from POST /answer
|
|
||||||
*/
|
|
||||||
export interface AnswerResponse {
|
|
||||||
success: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Request body for POST /poll
|
|
||||||
*/
|
|
||||||
export interface PollRequest {
|
|
||||||
/** Session UUID */
|
|
||||||
code: string;
|
|
||||||
/** Which side is polling */
|
|
||||||
side: Side;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from POST /poll when side=offerer
|
|
||||||
*/
|
|
||||||
export interface PollOffererResponse {
|
|
||||||
/** Answer from answerer (null if not yet received) */
|
|
||||||
answer: string | null;
|
|
||||||
/** Additional signaling data from answerer */
|
|
||||||
answerCandidates: string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from POST /poll when side=answerer
|
|
||||||
*/
|
|
||||||
export interface PollAnswererResponse {
|
|
||||||
/** Offer from offerer */
|
|
||||||
offer: string;
|
|
||||||
/** Additional signaling data from offerer */
|
|
||||||
offerCandidates: string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from POST /poll (union type)
|
|
||||||
*/
|
|
||||||
export type PollResponse = PollOffererResponse | PollAnswererResponse;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from GET / - server version information
|
|
||||||
*/
|
|
||||||
export interface VersionResponse {
|
|
||||||
/** Git commit hash or version identifier */
|
|
||||||
version: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Response from GET /health
|
|
||||||
*/
|
|
||||||
export interface HealthResponse {
|
|
||||||
status: 'ok';
|
|
||||||
timestamp: number;
|
|
||||||
version: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Error response structure
|
|
||||||
*/
|
|
||||||
export interface ErrorResponse {
|
|
||||||
error: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Client configuration options
|
|
||||||
*/
|
|
||||||
export interface RondevuClientOptions {
|
|
||||||
/** Base URL of the Rondevu server (e.g., 'https://example.com') */
|
|
||||||
baseUrl: string;
|
|
||||||
/** Optional fetch implementation (for Node.js environments) */
|
|
||||||
fetch?: typeof fetch;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ============================================================================
|
|
||||||
// WebRTC Types
|
|
||||||
// ============================================================================
|
|
||||||
|
|
||||||
/**
|
|
||||||
* WebRTC polyfill for Node.js and other non-browser platforms
|
|
||||||
*/
|
|
||||||
export interface WebRTCPolyfill {
|
|
||||||
RTCPeerConnection: typeof RTCPeerConnection;
|
|
||||||
RTCSessionDescription: typeof RTCSessionDescription;
|
|
||||||
RTCIceCandidate: typeof RTCIceCandidate;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Configuration options for Rondevu WebRTC client
|
|
||||||
*/
|
|
||||||
export interface RondevuOptions {
|
|
||||||
/** Base URL of the Rondevu server (defaults to 'https://api.ronde.vu') */
|
|
||||||
baseUrl?: string;
|
|
||||||
/** Peer identifier (optional, auto-generated if not provided) */
|
|
||||||
peerId?: string;
|
|
||||||
/** Optional fetch implementation (for Node.js environments) */
|
|
||||||
fetch?: typeof fetch;
|
|
||||||
/** WebRTC configuration (ICE servers, etc.) */
|
|
||||||
rtcConfig?: RTCConfiguration;
|
|
||||||
/** Polling interval in milliseconds (default: 1000) */
|
|
||||||
pollingInterval?: number;
|
|
||||||
/** Connection timeout in milliseconds (default: 30000) */
|
|
||||||
connectionTimeout?: number;
|
|
||||||
/** WebRTC polyfill for Node.js (e.g., wrtc or @roamhq/wrtc) */
|
|
||||||
wrtc?: WebRTCPolyfill;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Connection role - whether this peer is creating or answering
|
|
||||||
*/
|
|
||||||
export type ConnectionRole = 'offerer' | 'answerer';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Parameters for creating a RondevuConnection
|
|
||||||
*/
|
|
||||||
export interface RondevuConnectionParams {
|
|
||||||
id: string;
|
|
||||||
topic?: string;
|
|
||||||
role: ConnectionRole;
|
|
||||||
pc: RTCPeerConnection;
|
|
||||||
localPeerId: string;
|
|
||||||
remotePeerId: string;
|
|
||||||
pollingInterval: number;
|
|
||||||
connectionTimeout: number;
|
|
||||||
wrtc?: WebRTCPolyfill;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Event map for RondevuConnection events
|
|
||||||
*/
|
|
||||||
export interface RondevuConnectionEvents {
|
|
||||||
connect: () => void;
|
|
||||||
disconnect: () => void;
|
|
||||||
error: (error: Error) => void;
|
|
||||||
datachannel: (channel: RTCDataChannel) => void;
|
|
||||||
stream: (stream: MediaStream) => void;
|
|
||||||
}
|
}
|
||||||
|
|||||||
67
src/web-crypto-adapter.ts
Normal file
67
src/web-crypto-adapter.ts
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
/**
|
||||||
|
* Web Crypto adapter for browser environments
|
||||||
|
*/
|
||||||
|
|
||||||
|
import * as ed25519 from '@noble/ed25519'
|
||||||
|
import { CryptoAdapter, Keypair } from './crypto-adapter.js'
|
||||||
|
|
||||||
|
// 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))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Web Crypto implementation using browser APIs
|
||||||
|
* Uses btoa/atob for base64 encoding and crypto.getRandomValues for random bytes
|
||||||
|
*/
|
||||||
|
export class WebCryptoAdapter implements CryptoAdapter {
|
||||||
|
async generateKeypair(): Promise<Keypair> {
|
||||||
|
const privateKey = ed25519.utils.randomSecretKey()
|
||||||
|
const publicKey = await ed25519.getPublicKeyAsync(privateKey)
|
||||||
|
|
||||||
|
return {
|
||||||
|
publicKey: this.bytesToBase64(publicKey),
|
||||||
|
privateKey: this.bytesToBase64(privateKey),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async signMessage(message: string, privateKeyBase64: string): Promise<string> {
|
||||||
|
const privateKey = this.base64ToBytes(privateKeyBase64)
|
||||||
|
const encoder = new TextEncoder()
|
||||||
|
const messageBytes = encoder.encode(message)
|
||||||
|
const signature = await ed25519.signAsync(messageBytes, privateKey)
|
||||||
|
|
||||||
|
return this.bytesToBase64(signature)
|
||||||
|
}
|
||||||
|
|
||||||
|
async verifySignature(
|
||||||
|
message: string,
|
||||||
|
signatureBase64: string,
|
||||||
|
publicKeyBase64: string
|
||||||
|
): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
const signature = this.base64ToBytes(signatureBase64)
|
||||||
|
const publicKey = this.base64ToBytes(publicKeyBase64)
|
||||||
|
const encoder = new TextEncoder()
|
||||||
|
const messageBytes = encoder.encode(message)
|
||||||
|
|
||||||
|
return await ed25519.verifyAsync(signature, messageBytes, publicKey)
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bytesToBase64(bytes: Uint8Array): string {
|
||||||
|
const binString = Array.from(bytes, byte => String.fromCodePoint(byte)).join('')
|
||||||
|
return btoa(binString)
|
||||||
|
}
|
||||||
|
|
||||||
|
base64ToBytes(base64: string): Uint8Array {
|
||||||
|
const binString = atob(base64)
|
||||||
|
return Uint8Array.from(binString, char => char.codePointAt(0)!)
|
||||||
|
}
|
||||||
|
|
||||||
|
randomBytes(length: number): Uint8Array {
|
||||||
|
return crypto.getRandomValues(new Uint8Array(length))
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user