Initial Commit
This commit is contained in:
@@ -0,0 +1,109 @@
|
||||
# OpenSpec Change: enterprise-server-network-trystero-server
|
||||
|
||||
- **Change ID**: enterprise-server-network-trystero-server
|
||||
- **Package**: `@suaveplan/network-trystero-server`
|
||||
- **Category**: server/network
|
||||
- **Target path**: `server/network/network-trystero-server`
|
||||
- **Status**: SUPERSEDED-BY-GENESIS-WAVE
|
||||
- **Date**: 2026-04-24
|
||||
- **Reference spec**: `/mnt/s/web/suaveplan/enterprise/network-trystero-server.md`
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
Deliver `@suaveplan/network-trystero-server` on the genesis branch from scratch at enterprise quality. Trystero enables serverless WebRTC mesh networking using public signalling infrastructure (BitTorrent DHT, Nostr, Firebase, MQTT, Supabase). This server-side package provides a Node.js host that can act as a persistent relay peer within a Trystero room — bridging browser peers, relaying data, and optionally recording room state. It wraps Trystero's `joinRoom` API into a typed `TrysteroServerAdapter` with room lifecycle, peer events, typed data channels, and full OTel observability.
|
||||
|
||||
---
|
||||
|
||||
## Current-state audit (genesis)
|
||||
|
||||
Does not exist on genesis — fresh build.
|
||||
|
||||
---
|
||||
|
||||
## Available workspace deps on genesis
|
||||
|
||||
| Package | Role |
|
||||
|---|---|
|
||||
| `@suaveplan/error` (`workspace:^`) | Typed `NETWORK_TRYSTERO.*` error codes |
|
||||
| `@suaveplan/types` (`workspace:^`) | `LoggerLike` |
|
||||
| `@suaveplan/events` (`workspace:^`) | Internal event routing |
|
||||
| `@suaveplan/logger` (`workspace:^`) | Structured logs |
|
||||
| `zod` (`catalog:forms`) | Options validation |
|
||||
| `@opentelemetry/api` (`catalog:observability`) | Gauges + counters |
|
||||
| `trystero` (`catalog:network`) | WebRTC signalling library |
|
||||
| `tslib` (`catalog:build`) | Runtime helpers |
|
||||
|
||||
---
|
||||
|
||||
## Public API surface
|
||||
|
||||
```ts
|
||||
class TrysteroServerAdapter {
|
||||
constructor(options: TrysteroServerOptions)
|
||||
join(roomId: string): Promise<void>
|
||||
leave(roomId: string): Promise<void>
|
||||
send(roomId: string, channelName: string, data: unknown, peerId?: string): void
|
||||
onData(roomId: string, channelName: string, handler: (data: unknown, peerId: string) => void): () => void
|
||||
onPeerJoin(roomId: string, handler: (peerId: string) => void): () => void
|
||||
onPeerLeave(roomId: string, handler: (peerId: string) => void): () => void
|
||||
peers(roomId: string): string[]
|
||||
leaveAll(): Promise<void>
|
||||
}
|
||||
|
||||
function createTrysteroServerAdapter(options: TrysteroServerOptions): TrysteroServerAdapter
|
||||
|
||||
interface TrysteroServerOptions {
|
||||
strategy: 'torrent' | 'nostr' | 'mqtt' | 'supabase'
|
||||
appId: string
|
||||
password?: string
|
||||
relayUrls?: string[] // for nostr / mqtt strategies
|
||||
logger?: LoggerLike
|
||||
}
|
||||
|
||||
const TrysteroServerOptionsSchema: z.ZodSchema<TrysteroServerOptions>
|
||||
|
||||
type TrysteroErrorCode =
|
||||
| 'NETWORK_TRYSTERO_JOIN_FAILED'
|
||||
| 'NETWORK_TRYSTERO_SEND_FAILED'
|
||||
| 'NETWORK_TRYSTERO_LEAVE_FAILED'
|
||||
| 'NETWORK_TRYSTERO_STRATEGY_UNSUPPORTED'
|
||||
|
||||
function registerTrysteroErrorCodes(): void
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## File layout
|
||||
|
||||
```text
|
||||
server/network/network-trystero-server/
|
||||
package.json
|
||||
tsconfig.json
|
||||
vitest.config.ts
|
||||
src/
|
||||
index.ts
|
||||
error-codes.ts
|
||||
constants/index.ts
|
||||
types/
|
||||
trystero-server.types.ts
|
||||
trystero-server.types.md
|
||||
adapter/
|
||||
trystero-server-adapter/
|
||||
trystero-server-adapter.ts
|
||||
trystero-server-adapter.md
|
||||
trystero-server-adapter.types.ts
|
||||
trystero-server-adapter.test.ts
|
||||
index.ts
|
||||
tests/
|
||||
integration.test.ts
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Impact
|
||||
|
||||
Downstream: browser-to-server WebRTC bridge, real-time multiplayer relay. Requires Node.js and network access for integration tests.
|
||||
|
||||
**Prerequisites on Verdaccio**: `@suaveplan/error`, `@suaveplan/types`, `@suaveplan/events`, `@suaveplan/logger`.
|
||||
@@ -0,0 +1,57 @@
|
||||
# Tasks: enterprise-server-network-trystero-server
|
||||
|
||||
**Change ID**: enterprise-server-network-trystero-server
|
||||
**Package**: `@suaveplan/network-trystero-server`
|
||||
**Target path**: `server/network/network-trystero-server`
|
||||
**Prerequisites on Verdaccio**: `@suaveplan/error`, `@suaveplan/types`, `@suaveplan/events`, `@suaveplan/logger`
|
||||
|
||||
---
|
||||
|
||||
## 1. Scaffold
|
||||
|
||||
- [x] Create directory tree per `proposal.md`.
|
||||
- [x] `package.json` at `0.1.0`, `"sideEffects": false`, exports `"."` + `adapter`.
|
||||
- [x] Deps: workspace + catalog deps as listed in proposal.
|
||||
- [x] DevDeps: `vitest catalog:testing`, `@vitest/coverage-istanbul catalog:testing`, `@suaveplan/testing workspace:^`.
|
||||
- [x] `tsconfig.json` extending `library-node.json`; `vitest.config.ts` with `pool: 'forks'`.
|
||||
- [x] Scripts: `lint`, `typecheck`, `test`, `test:coverage`, `build`, `docs:check`.
|
||||
|
||||
## 2. Types and errors
|
||||
|
||||
- [x] Define `TrysteroServerOptions`, `TrysteroServerOptionsSchema` in `src/types/trystero-server.types.ts`.
|
||||
- [x] Write `trystero-server.types.md` (≥200 words).
|
||||
- [x] `src/error-codes.ts`: four codes; `registerTrysteroErrorCodes()`; eager call.
|
||||
|
||||
## 3. Adapter implementation
|
||||
|
||||
- [x] Implement `TrysteroServerAdapter` in `src/adapter/trystero-server-adapter/trystero-server-adapter.ts`:
|
||||
- Maintain a `Map<roomId, { room: TrysteroRoom; channels: Map<channelName, DataChannel>; peers: Set<string> }>`.
|
||||
- `join(roomId)`: call Trystero `joinRoom(config, roomId)`; register `onPeerJoin` / `onPeerLeave` handlers; update peer set. Throw `NETWORK_TRYSTERO_JOIN_FAILED` on error.
|
||||
- `leave(roomId)`: call `room.leave()`; remove from map. Throw `NETWORK_TRYSTERO_LEAVE_FAILED` on error.
|
||||
- `send(roomId, channelName, data, peerId?)`: get or create data channel via `room.makeAction(channelName)`; call `send(data, peerId)`. Throw `NETWORK_TRYSTERO_SEND_FAILED` on missing room.
|
||||
- `onData(roomId, channelName, handler)`: register action handler; return unsubscribe fn.
|
||||
- `onPeerJoin` / `onPeerLeave`: register peer lifecycle handlers; return unsubscribe.
|
||||
- `peers(roomId)`: return `[...peerSet]` or `[]`.
|
||||
- `leaveAll()`: iterate all rooms, call `leave` on each.
|
||||
- OTel gauge `network_trystero.peer_count`; counter `network_trystero.messages_total`.
|
||||
- [x] Write `trystero-server-adapter.types.ts`.
|
||||
- [x] Write `trystero-server-adapter.test.ts` (≤500 lines): unit tests using a mocked Trystero `joinRoom` that returns a fake room object — verify all adapter state management, handler registration, unsubscribe. Use DI for Trystero factory to inject mock. Guard network integration tests with `SKIP_NETWORK_TESTS=1`.
|
||||
- [x] Write `trystero-server-adapter.md` (≥200 words, simple + advanced, API reference).
|
||||
|
||||
## 4. Integration tests
|
||||
|
||||
- [x] `tests/integration.test.ts`: skip unless `RUN_TRYSTERO_INTEGRATION=1`. Two adapters join same room (torrent strategy); one sends, other receives via `onData`. `{ timeout: 30000 }`.
|
||||
|
||||
## 5. OTel + docs
|
||||
|
||||
- [x] All OTel instrumentation per proposal.
|
||||
- [x] Root `README.md`: strategy comparison table, quick-start torrent example, API table.
|
||||
|
||||
## 6. Verify and publish
|
||||
|
||||
- [x] `bunx turbo lint --filter=@suaveplan/network-trystero-server` — zero errors.
|
||||
- [x] `bunx turbo typecheck --filter=@suaveplan/network-trystero-server` — zero errors.
|
||||
- [x] `bunx turbo test --filter=@suaveplan/network-trystero-server` — all pass.
|
||||
- [x] `bunx turbo test:coverage --filter=@suaveplan/network-trystero-server` — 100%.
|
||||
- [x] `bun scripts/publish.ts --package @suaveplan/network-trystero-server`.
|
||||
- [x] Update `CLAUDE.md`; archive to `openspec/archive/2026-04-24-enterprise-server-network-trystero-server/`.
|
||||
Reference in New Issue
Block a user