|
1 | | -import type { Peer } from 'crossws' |
| 1 | +/** |
| 2 | + * Structural view of the crossws `Peer` backing a WebSocket RPC connection: |
| 3 | + * the transport-independent slice of its API (identity, send, pub/sub, |
| 4 | + * close, backpressure), typed locally so the `devframe/types` declaration |
| 5 | + * graph never imports `crossws`, whose own declarations require the DOM, |
| 6 | + * Bun, and Cloudflare type libs a plain Node consumer doesn't load. Every |
| 7 | + * member mirrors its crossws counterpart; for the full API (the upgrade |
| 8 | + * `request`, raw `websocket`, connected `peers`), import `Peer` from |
| 9 | + * `crossws` and cast, which opts your compilation into crossws's lib |
| 10 | + * requirements. |
| 11 | + */ |
| 12 | +export interface DevframeWsPeer { |
| 13 | + /** Unique random uuid v4 identifier for the peer. */ |
| 14 | + readonly id: string |
| 15 | + /** IP address of the peer. */ |
| 16 | + readonly remoteAddress: string | undefined |
| 17 | + /** All topics this peer has been subscribed to. */ |
| 18 | + readonly topics: Set<string> |
| 19 | + /** Bytes queued for transmission but not yet flushed to the client. */ |
| 20 | + readonly bufferedAmount: number |
| 21 | + /** Wait until the send buffer drains to `threshold` bytes (default `0`). */ |
| 22 | + waitForDrain: (opts?: { threshold?: number, pollInterval?: number }) => Promise<void> |
| 23 | + /** Send a message to the peer. */ |
| 24 | + send: (data: unknown, options?: { compress?: boolean }) => number | void | undefined |
| 25 | + /** Send a message to subscribers of a topic. */ |
| 26 | + publish: (topic: string, data: unknown, options?: { compress?: boolean }) => void |
| 27 | + /** Subscribe to a topic. */ |
| 28 | + subscribe: (topic: string) => void |
| 29 | + /** Unsubscribe from a topic. */ |
| 30 | + unsubscribe: (topic: string) => void |
| 31 | + /** Close the connection. */ |
| 32 | + close: (code?: number, reason?: string) => void |
| 33 | + /** Abruptly close the connection. */ |
| 34 | + terminate: () => void |
| 35 | +} |
2 | 36 |
|
3 | 37 | /** |
4 | 38 | * Which wire transport produced an RPC connection. Every transport speaks |
@@ -44,13 +78,13 @@ export interface DevframeRpcConnection { |
44 | 78 | * The crossws peer backing a `websocket` connection: the WS-specific |
45 | 79 | * escape hatch (pub/sub, raw socket access). Absent on other transports. |
46 | 80 | */ |
47 | | - peer?: Peer |
| 81 | + peer?: DevframeWsPeer |
48 | 82 | } |
49 | 83 |
|
50 | 84 | export interface DevframeNodeRpcSessionMeta { |
51 | 85 | id: number |
52 | 86 | /** The crossws peer backing this session's socket (WS transport only). */ |
53 | | - peer?: Peer |
| 87 | + peer?: DevframeWsPeer |
54 | 88 | clientAuthToken?: string |
55 | 89 | isTrusted?: boolean |
56 | 90 | subscribedStates: Set<string> |
|
0 commit comments