Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(b4): relay socket + E2EE handshake + RPC/frame state machine

enesb464686532fe
diff --git a/package-lock.json b/package-lock.json
index 23f54d3e7..dbe15ca0e 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -16,6 +16,7 @@
"monaco-editor": "^0.52.2",
"node-pty": "^1.0.0",
"tweetnacl": "^1.0.3",
+ "ws": "^8.21.0",
"zustand": "^4.5.7"
},
"devDependencies": {
@@ -23,6 +24,7 @@
"@types/node": "^26.0.0",
"@types/react": "^18.3.31",
"@types/react-dom": "^18.3.7",
+ "@types/ws": "^8.18.1",
"@vitejs/plugin-react": "^4.3.1",
"@xterm/addon-fit": "^0.10.0",
"@xterm/addon-search": "^0.15.0",
@@ -2377,6 +2379,16 @@
"license": "MIT",
"optional": true
},
+ "node_modules/@types/ws": {
+ "version": "8.18.1",
+ "resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.18.1.tgz",
+ "integrity": "sha512-ThVF6DCVhA8kUGy+aazFQ4kXQ7E1Ty7A3ypFOe0IcJV8O/M511G99AW24irKrW56Wt44yG9+ij8FaqoBGkuBXg==",
+ "dev": true,
+ "license": "MIT",
+ "dependencies": {
+ "@types/node": "*"
+ }
+ },
"node_modules/@types/yauzl": {
"version": "2.10.3",
"resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.10.3.tgz",
@@ -7466,6 +7478,27 @@
"dev": true,
"license": "ISC"
},
+ "node_modules/ws": {
+ "version": "8.21.0",
+ "resolved": "https://registry.npmjs.org/ws/-/ws-8.21.0.tgz",
+ "integrity": "sha512-Vsp28b7DRcimFQvrqu2Wek3z1iYxDCWqHYB8Qsnk/S4RfaCQzPGPyBNuVjJV3cd6UiKtUtp6sNM77gWvzcCH+g==",
+ "license": "MIT",
+ "engines": {
+ "node": ">=10.0.0"
+ },
+ "peerDependencies": {
+ "bufferutil": "^4.0.1",
+ "utf-8-validate": ">=5.0.2"
+ },
+ "peerDependenciesMeta": {
+ "bufferutil": {
+ "optional": true
+ },
+ "utf-8-validate": {
+ "optional": true
+ }
+ }
+ },
"node_modules/xmlbuilder": {
"version": "15.1.1",
"resolved": "https://registry.npmjs.org/xmlbuilder/-/xmlbuilder-15.1.1.tgz",
diff --git a/package.json b/package.json
index d64c5d5ac..1fa2d2ef7 100644
--- a/package.json
+++ b/package.json
@@ -72,6 +72,7 @@
"monaco-editor": "^0.52.2",
"node-pty": "^1.0.0",
"tweetnacl": "^1.0.3",
+ "ws": "^8.21.0",
"zustand": "^4.5.7"
},
"devDependencies": {
@@ -79,6 +80,7 @@
"@types/node": "^26.0.0",
"@types/react": "^18.3.31",
"@types/react-dom": "^18.3.7",
+ "@types/ws": "^8.18.1",
"@vitejs/plugin-react": "^4.3.1",
"@xterm/addon-fit": "^0.10.0",
"@xterm/addon-search": "^0.15.0",
diff --git a/src/main/remote/relay-socket.ts b/src/main/remote/relay-socket.ts
new file mode 100644
index 000000000..6339d373c
--- /dev/null
+++ b/src/main/remote/relay-socket.ts
@@ -0,0 +1,543 @@
+// Relay socket + E2EE handshake + RPC/frame state machine (main process).
+//
+// A single connection to the dumb relay (`nodebaseserver/src/relay`). The relay
+// forwards opaque bytes between a host and a client matched by a pairing token;
+// it never decrypts. So the end-to-end-encrypted handshake and all traffic run
+// HOST<->CLIENT *through* the relay — the relay is not a handshake participant.
+//
+// Ported from REF's `web-runtime-client.ts` state machine, adapted:
+// - The token gates entry at the relay via a `?token=<token>` QUERY PARAM on
+// the wss URL (cross-task decision; matches the relay's index.ts). It is NOT
+// interleaved as a data frame — every frame on the wire is opaque peer
+// traffic.
+// - Handshake `e2ee_hello -> e2ee_ready -> e2ee_auth -> e2ee_authenticated`:
+// * client (knows the host pubkey) sends `e2ee_hello {publicKeyB64}` and
+// derives the shared key from the host pubkey;
+// * host learns the client pubkey from `e2ee_hello`, derives the same
+// shared key, replies `e2ee_ready`;
+// * client then sends an ENCRYPTED `e2ee_auth` (the relay already verified
+// the token and the offer pins the host pubkey, so for MVP a simple
+// authenticated marker is sufficient — no extra secret to prove);
+// * host replies an ENCRYPTED `e2ee_authenticated`. Both sides fire onReady
+// once their side of the auth exchange is complete.
+// - After authentication every peer message is an E2EE box (sent as binary).
+// The decrypted plaintext is tagged: 0x01 = JSON RPC envelope, 0x02 = a
+// binary terminal frame (framing.ts). Handshake control frames (`e2ee_hello`
+// / `e2ee_ready`) are sent as plaintext JSON strings so the receiver can tell
+// them apart from the encrypted binary boxes.
+//
+// Pure-ish: no IPC, no renderer. The WebSocket is INJECTED as a `RelayTransport`
+// so tests drive two RelaySockets via in-process fakes with no real network;
+// production passes a `ws` WebSocket wrapped via `wrapWebSocket`.
+
+import { decrypt, deriveSharedKey, encrypt, publicKeyToB64, type KeyPair } from './e2ee'
+import { decodeFrame, encodeFrame, type Frame, MAX_BINARY_BUFFERED_AMOUNT } from './framing'
+
+// A minimal duplex transport. `send` accepts a string (handshake control) or a
+// Uint8Array (encrypted box). `bufferedAmount` drives backpressure on frames.
+export interface RelayTransport {
+ readonly bufferedAmount: number
+ send(data: string | Uint8Array): void
+ close(): void
+ onMessage(cb: (data: unknown) => void): void
+ onClose(cb: () => void): void
+}
+
+// An RPC request envelope as seen by the peer's onRpc handler. `id` correlates a
+// later `respond`.
+export type RpcRequest = {
+ id: string
+ method: string
+ params: unknown
+}
+
+export type RelaySocket = {
+ // Send an RPC request to the peer; resolves with the peer's response body (or
+ // rejects on a non-ok response / timeout / closed connection).
+ rpc(method: string, params?: unknown): Promise<unknown>
+ // Answer a received RPC request (from onRpc) by its id.
+ respond(id: string, ok: boolean, body: unknown): void
+ // Send a terminal frame to the peer. Returns false when the transport is over
+ // its buffered-amount threshold (backpressure) or not connected.
+ sendFrame(op: number, streamId: number, seq: number, payload: Uint8Array): boolean
+ // Tear down: stops reconnects and closes the transport.
+ close(): void
+}
+
+export type ConnectRelayOptions = {
+ // The relay wss URL. `?token=` is appended automatically when opening a socket.
+ url: string
+ // Single-use pairing token; gates entry at the relay (query param).
+ token: string
+ role: 'host' | 'client'
+ // Our long-lived (host) or ephemeral (client) NaCl keypair.
+ ourKeys: KeyPair
+ // The peer's public key, base64. REQUIRED for the client (it must know the
+ // host's pubkey up front); for the host it is learned from `e2ee_hello`.
+ theirPubB64?: string
+ // Injected transport. Tests pass an in-process fake; production passes a `ws`
+ // WebSocket wrapped via `wrapWebSocket`. When omitted, a `ws` socket is opened
+ // from `url` (production default).
+ transport?: RelayTransport
+ onReady(): void
+ onRpc(msg: RpcRequest): void
+ onFrame(f: Frame): void
+ onClose(): void
+}
+
+// Plaintext-tag bytes for decrypted peer payloads.
+const TAG_RPC = 0x01
+const TAG_FRAME = 0x02
+
+const RPC_TIMEOUT_MS = 30_000
+const KEEPALIVE_INTERVAL_MS = 25_000
+const RECONNECT_DELAYS_MS = [500, 1000, 2000, 4000, 8000, 15_000]
+
+type RpcEnvelope =
+ | { kind: 'req'; id: string; method: string; params: unknown }
+ | { kind: 'res'; id: string; ok: boolean; body: unknown }
+ | { kind: 'keepalive' }
+
+type HandshakeControl =
+ | { type: 'e2ee_hello'; publicKeyB64: string }
+ | { type: 'e2ee_ready' }
+
+type State = 'connecting' | 'handshaking' | 'ready' | 'closed'
+
+const textEncoder = new TextEncoder()
+const textDecoder = new TextDecoder()
+
+export function connectRelay(opts: ConnectRelayOptions): RelaySocket {
+ if (opts.role === 'client' && !opts.theirPubB64) {
+ throw new Error('connectRelay: client role requires theirPubB64 (the host public key)')
+ }
+
+ let transport: RelayTransport | null = null
+ let sharedKey: Uint8Array | null = null
+ let state: State = 'connecting'
+ let intentionallyClosed = false
+ let reconnectAttempt = 0
+ let reconnectTimer: ReturnType<typeof setTimeout> | null = null
+ let keepaliveTimer: ReturnType<typeof setInterval> | null = null
+ let requestCounter = 0
+ let readyFired = false
+
+ const pending = new Map<
+ string,
+ { resolve: (body: unknown) => void; reject: (err: Error) => void; timer: ReturnType<typeof setTimeout> }
+ >()
+
+ function buildUrl(): string {
+ // Append `?token=` so the relay can read the pairing token from the query
+ // string (cross-task decision). Preserve any existing query.
+ const sep = opts.url.includes('?') ? '&' : '?'
+ return `${opts.url}${sep}token=${encodeURIComponent(opts.token)}`
+ }
+
+ function openConnection(): void {
+ if (intentionallyClosed) {
+ return
+ }
+ state = 'connecting'
+ sharedKey = null
+ readyFired = false
+
+ if (opts.transport) {
+ // Injected transport (tests). It is already "open"; treat that as the
+ // open event. A re-open after a drop is not supported for injected
+ // transports — production reconnect goes through the real ws path.
+ transport = opts.transport
+ } else {
+ transport = openWebSocketTransport(buildUrl())
+ }
+
+ transport.onMessage((data) => handleMessage(data))
+ transport.onClose(() => handleClose())
+
+ state = 'handshaking'
+ if (opts.role === 'client') {
+ // Client knows the host pubkey: derive now and greet.
+ sharedKey = deriveSharedKey(opts.theirPubB64!, opts.ourKeys.secretKey)
+ sendControl({ type: 'e2ee_hello', publicKeyB64: publicKeyToB64(opts.ourKeys.publicKey) })
+ }
+ // Host waits passively for `e2ee_hello` to learn the client pubkey.
+ }
+
+ function sendControl(control: HandshakeControl): void {
+ transport?.send(JSON.stringify(control))
+ }
+
+ function sendEncrypted(plaintext: Uint8Array): boolean {
+ if (!transport || !sharedKey || state !== 'ready') {
+ return false
+ }
+ transport.send(encrypt(plaintext, sharedKey))
+ return true
+ }
+
+ // During the handshake the auth control frames must be sent before `state`
+ // flips to 'ready', so this variant does not gate on state.
+ function sendEncryptedHandshake(plaintext: Uint8Array): void {
+ if (!transport || !sharedKey) {
+ return
+ }
+ transport.send(encrypt(plaintext, sharedKey))
+ }
+
+ function tagged(tag: number, body: Uint8Array): Uint8Array {
+ const out = new Uint8Array(body.length + 1)
+ out[0] = tag
+ out.set(body, 1)
+ return out
+ }
+
+ function fireReadyOnce(): void {
+ if (readyFired) {
+ return
+ }
+ readyFired = true
+ reconnectAttempt = 0
+ startKeepalive()
+ opts.onReady()
+ }
+
+ function handleMessage(data: unknown): void {
+ if (state === 'closed') {
+ return
+ }
+
+ const asString = stringFromData(data)
+ if (asString !== null) {
+ // Plaintext JSON => a handshake control frame.
+ handleControl(asString)
+ return
+ }
+
+ const bytes = bytesFromData(data)
+ if (!bytes) {
+ return
+ }
+
+ // Everything binary is an E2EE box.
+ if (!sharedKey) {
+ return
+ }
+ const plain = decrypt(bytes, sharedKey)
+ if (!plain) {
+ return
+ }
+
+ if (state === 'handshaking') {
+ handleHandshakeEncrypted(plain)
+ return
+ }
+ if (state !== 'ready') {
+ return
+ }
+ handlePeerPlaintext(plain)
+ }
+
+ function handleControl(raw: string): void {
+ let control: HandshakeControl
+ try {
+ control = JSON.parse(raw) as HandshakeControl
+ } catch {
+ return
+ }
+ if (control.type === 'e2ee_hello' && opts.role === 'host') {
+ // Host learns the client pubkey, derives the shared key, replies ready.
+ if (typeof control.publicKeyB64 !== 'string') {
+ return
+ }
+ sharedKey = deriveSharedKey(control.publicKeyB64, opts.ourKeys.secretKey)
+ sendControl({ type: 'e2ee_ready' })
+ return
+ }
+ if (control.type === 'e2ee_ready' && opts.role === 'client') {
+ // Client proves the pairing with an encrypted auth marker. The relay
+ // already verified the token and the offer pins the host pubkey, so a
+ // simple marker is sufficient for MVP.
+ sendEncryptedHandshake(tagged(TAG_RPC, textEncoder.encode(JSON.stringify({ type: 'e2ee_auth' }))))
+ }
+ }
+
+ function handleHandshakeEncrypted(plain: Uint8Array): void {
+ // Tag byte distinguishes control vs payload; during handshake we only expect
+ // control JSON (auth / authenticated).
+ if (plain.length < 1 || plain[0] !== TAG_RPC) {
+ return
+ }
+ let msg: { type?: string }
+ try {
+ msg = JSON.parse(textDecoder.decode(plain.subarray(1))) as { type?: string }
+ } catch {
+ return
+ }
+ if (msg.type === 'e2ee_auth' && opts.role === 'host') {
+ // Host accepts the client's authenticated marker, confirms, becomes ready.
+ sendEncryptedHandshake(
+ tagged(TAG_RPC, textEncoder.encode(JSON.stringify({ type: 'e2ee_authenticated' })))
+ )
+ state = 'ready'
+ fireReadyOnce()
+ return
+ }
+ if (msg.type === 'e2ee_authenticated' && opts.role === 'client') {
+ state = 'ready'
+ fireReadyOnce()
+ }
+ }
+
+ function handlePeerPlaintext(plain: Uint8Array): void {
+ if (plain.length < 1) {
+ return
+ }
+ const tag = plain[0]
+ const body = plain.subarray(1)
+ if (tag === TAG_FRAME) {
+ const frame = decodeFrame(body)
+ if (frame) {
+ opts.onFrame(frame)
+ }
+ return
+ }
+ if (tag !== TAG_RPC) {
+ return
+ }
+ let env: RpcEnvelope
+ try {
+ env = JSON.parse(textDecoder.decode(body)) as RpcEnvelope
+ } catch {
+ return
+ }
+ if (env.kind === 'keepalive') {
+ return
+ }
+ if (env.kind === 'req') {
+ opts.onRpc({ id: env.id, method: env.method, params: env.params })
+ return
+ }
+ if (env.kind === 'res') {
+ const waiter = pending.get(env.id)
+ if (!waiter) {
+ return
+ }
+ pending.delete(env.id)
+ clearTimeout(waiter.timer)
+ if (env.ok) {
+ waiter.resolve(env.body)
+ } else {
+ waiter.reject(new Error(rpcErrorMessage(env.body)))
+ }
+ }
+ }
+
+ function startKeepalive(): void {
+ stopKeepalive()
+ keepaliveTimer = setInterval(() => {
+ sendEncrypted(tagged(TAG_RPC, textEncoder.encode(JSON.stringify({ kind: 'keepalive' }))))
+ }, KEEPALIVE_INTERVAL_MS)
+ // Don't keep the event loop alive purely for keepalives.
+ keepaliveTimer.unref?.()
+ }
+
+ function stopKeepalive(): void {
+ if (keepaliveTimer) {
+ clearInterval(keepaliveTimer)
+ keepaliveTimer = null
+ }
+ }
+
+ function handleClose(): void {
+ stopKeepalive()
+ transport = null
+ sharedKey = null
+ const wasReady = state === 'ready'
+ rejectAllPending('Relay connection closed.')
+ if (intentionallyClosed) {
+ state = 'closed'
+ return
+ }
+ state = 'closed'
+ opts.onClose()
+ // Reconnect only applies to the production ws path; injected transports are
+ // single-shot (the test fake cannot re-open).
+ if (!opts.transport && wasReady) {
+ scheduleReconnect()
+ }
+ }
+
+ function scheduleReconnect(): void {
+ if (reconnectTimer || intentionallyClosed) {
+ return
+ }
+ const delay = RECONNECT_DELAYS_MS[Math.min(reconnectAttempt, RECONNECT_DELAYS_MS.length - 1)]
+ reconnectAttempt += 1
+ reconnectTimer = setTimeout(() => {
+ reconnectTimer = null
+ openConnection()
+ }, delay)
+ reconnectTimer.unref?.()
+ }
+
+ function rejectAllPending(reason: string): void {
+ const err = new Error(reason)
+ for (const [id, waiter] of pending) {
+ pending.delete(id)
+ clearTimeout(waiter.timer)
+ waiter.reject(err)
+ }
+ }
+
+ function nextId(): string {
+ requestCounter += 1
+ return `${opts.role}-rpc-${requestCounter}-${Date.now()}`
+ }
+
+ openConnection()
+
+ return {
+ rpc(method, params) {
+ return new Promise((resolve, reject) => {
+ const id = nextId()
+ const timer = setTimeout(() => {
+ pending.delete(id)
+ reject(new Error(`RPC timed out: ${method}`))
+ }, RPC_TIMEOUT_MS)
+ timer.unref?.()
+ pending.set(id, { resolve, reject, timer })
+ const sent = sendEncrypted(
+ tagged(TAG_RPC, textEncoder.encode(JSON.stringify({ kind: 'req', id, method, params })))
+ )
+ if (!sent) {
+ pending.delete(id)
+ clearTimeout(timer)
+ reject(new Error('Relay socket is not connected.'))
+ }
+ })
+ },
+ respond(id, ok, body) {
+ sendEncrypted(tagged(TAG_RPC, textEncoder.encode(JSON.stringify({ kind: 'res', id, ok, body }))))
+ },
+ sendFrame(op, streamId, seq, payload) {
+ if (!transport || state !== 'ready') {
+ return false
+ }
+ if (transport.bufferedAmount > MAX_BINARY_BUFFERED_AMOUNT) {
+ return false
+ }
+ return sendEncrypted(tagged(TAG_FRAME, encodeFrame(op, streamId, seq, payload)))
+ },
+ close() {
+ intentionallyClosed = true
+ if (reconnectTimer) {
+ clearTimeout(reconnectTimer)
+ reconnectTimer = null
+ }
+ stopKeepalive()
+ rejectAllPending('Relay socket closed.')
+ state = 'closed'
+ transport?.close()
+ transport = null
+ sharedKey = null
+ }
+ }
+}
+
+function rpcErrorMessage(body: unknown): string {
+ if (body && typeof body === 'object') {
+ const maybe = body as { message?: unknown; error?: unknown }
+ if (typeof maybe.message === 'string') {
+ return maybe.message
+ }
+ if (typeof maybe.error === 'string') {
+ return maybe.error
+ }
+ }
+ return 'RPC failed.'
+}
+
+// --- transport payload coercion ---------------------------------------------
+
+function stringFromData(data: unknown): string | null {
+ if (typeof data === 'string') {
+ return data
+ }
+ return null
+}
+
+function bytesFromData(data: unknown): Uint8Array | null {
+ if (data instanceof Uint8Array) {
+ return data
+ }
+ if (data instanceof ArrayBuffer) {
+ return new Uint8Array(data)
+ }
+ // `ws` may deliver a Node Buffer (which is a Uint8Array) or an array of
+ // Buffers when fragmented; handle the array case defensively.
+ if (Array.isArray(data) && data.every((d) => d instanceof Uint8Array)) {
+ const total = data.reduce((n, d) => n + (d as Uint8Array).length, 0)
+ const out = new Uint8Array(total)
+ let offset = 0
+ for (const d of data as Uint8Array[]) {
+ out.set(d, offset)
+ offset += d.length
+ }
+ return out
+ }
+ return null
+}
+
+// --- production ws wrapper ----------------------------------------------------
+
+// Wrap a connected `ws` WebSocket as a RelayTransport. Kept here so the host
+// (Task 6) and client (Task 7) share one ws adapter. Imported lazily so the
+// pure module + its tests never load `ws`.
+export function wrapWebSocket(ws: import('ws').WebSocket): RelayTransport {
+ ws.binaryType = 'nodebuffer'
+ return {
+ get bufferedAmount() {
+ return ws.bufferedAmount
+ },
+ send: (data) => ws.send(data),
+ close: () => ws.close(),
+ onMessage: (cb) => ws.on('message', (data: unknown) => cb(data)),
+ onClose: (cb) => ws.on('close', () => cb())
+ }
+}
+
+// Open a `ws` WebSocket to `url` and return it wrapped. Sends start only once
+// the socket is open; messages before open are dropped (the handshake retries on
+// reconnect). Used as the production default when no transport is injected.
+function openWebSocketTransport(url: string): RelayTransport {
+ // Lazy require so test/pure consumers don't pull in the native-ish dep.
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
+ const WS = require('ws') as typeof import('ws')
+ const ws = new WS.WebSocket(url)
+ ws.binaryType = 'nodebuffer'
+ const wrapped = wrapWebSocket(ws)
+ // Buffer sends issued before 'open'.
+ const queue: (string | Uint8Array)[] = []
+ let open = false
+ ws.on('open', () => {
+ open = true
+ for (const item of queue.splice(0)) {
+ ws.send(item)
+ }
+ })
+ return {
+ get bufferedAmount() {
+ return ws.bufferedAmount
+ },
+ send: (data) => {
+ if (open) {
+ ws.send(data)
+ } else {
+ queue.push(data)
+ }
+ },
+ close: () => ws.close(),
+ onMessage: wrapped.onMessage,
+ onClose: wrapped.onClose
+ }
+}
diff --git a/test/remote/relay-socket.test.ts b/test/remote/relay-socket.test.ts
new file mode 100644
index 000000000..1f3fa7e9e
--- /dev/null
+++ b/test/remote/relay-socket.test.ts
@@ -0,0 +1,128 @@
+import { describe, it, expect } from 'vitest'
+import { connectRelay, type RelayTransport } from '../../src/main/remote/relay-socket'
+import { genKeyPair, publicKeyToB64 } from '../../src/main/remote/e2ee'
+import { OP, decodeFrame, type Frame } from '../../src/main/remote/framing'
+
+// A pair of in-process fake duplex transports: whatever one `send`s is delivered
+// asynchronously to the other's onMessage. No real network, no `ws`.
+function makeTransportPair(): { a: RelayTransport; b: RelayTransport } {
+ let aOnMessage: ((data: unknown) => void) | null = null
+ let bOnMessage: ((data: unknown) => void) | null = null
+ let aOnClose: (() => void) | null = null
+ let bOnClose: (() => void) | null = null
+ let closed = false
+
+ const deliver = (cb: ((data: unknown) => void) | null, data: unknown): void => {
+ // Async delivery models a real socket and keeps the handshake event-driven.
+ queueMicrotask(() => {
+ if (!closed) cb?.(data)
+ })
+ }
+ const teardown = (): void => {
+ if (closed) return
+ closed = true
+ queueMicrotask(() => {
+ aOnClose?.()
+ bOnClose?.()
+ })
+ }
+
+ const a: RelayTransport = {
+ bufferedAmount: 0,
+ send: (data) => deliver(bOnMessage, data),
+ close: teardown,
+ onMessage: (cb) => {
+ aOnMessage = cb
+ },
+ onClose: (cb) => {
+ aOnClose = cb
+ }
+ }
+ const b: RelayTransport = {
+ bufferedAmount: 0,
+ send: (data) => deliver(aOnMessage, data),
+ close: teardown,
+ onMessage: (cb) => {
+ bOnMessage = cb
+ },
+ onClose: (cb) => {
+ bOnClose = cb
+ }
+ }
+ return { a, b }
+}
+
+function deferred(): { promise: Promise<void>; resolve: () => void } {
+ let resolve!: () => void
+ const promise = new Promise<void>((r) => {
+ resolve = r
+ })
+ return { promise, resolve }
+}
+
+describe('relay-socket', () => {
+ it('completes the host<->client handshake and round-trips an RPC + a frame', async () => {
+ const hostKeys = genKeyPair()
+ const clientKeys = genKeyPair()
+ const { a: hostTransport, b: clientTransport } = makeTransportPair()
+
+ const hostReady = deferred()
+ const clientReady = deferred()
+ const receivedFrames: Frame[] = []
+
+ const host = connectRelay({
+ url: 'wss://relay.example/ws',
+ token: 'pairing-token',
+ role: 'host',
+ ourKeys: hostKeys,
+ transport: hostTransport,
+ onReady: () => hostReady.resolve(),
+ onRpc: (msg) => {
+ // The host answers a ping request.
+ if (msg.method === 'ping') {
+ host.respond(msg.id, true, { pong: true })
+ }
+ },
+ onFrame: (f) => {
+ receivedFrames.push(f)
+ },
+ onClose: () => {}
+ })
+
+ const client = connectRelay({
+ url: 'wss://relay.example/ws',
+ token: 'pairing-token',
+ role: 'client',
+ ourKeys: clientKeys,
+ theirPubB64: publicKeyToB64(hostKeys.publicKey),
+ transport: clientTransport,
+ onReady: () => clientReady.resolve(),
+ onRpc: () => {},
+ onFrame: () => {},
+ onClose: () => {}
+ })
+
+ await Promise.all([hostReady.promise, clientReady.promise])
+
+ // RPC round-trip: client -> host -> resolved result.
+ const result = await client.rpc('ping', { hello: 'world' })
+ expect(result).toEqual({ pong: true })
+
+ // Binary frame: client -> host, decoded on the host's onFrame.
+ const payload = new TextEncoder().encode('terminal output')
+ const ok = client.sendFrame(OP.Output, 7, 42, payload)
+ expect(ok).toBe(true)
+
+ // Wait for async delivery.
+ await new Promise((r) => setTimeout(r, 20))
+ expect(receivedFrames.length).toBe(1)
+ const frame = receivedFrames[0]!
+ expect(frame.op).toBe(OP.Output)
+ expect(frame.streamId).toBe(7)
+ expect(frame.seq).toBe(42)
+ expect(new TextDecoder().decode(frame.payload)).toBe('terminal output')
+
+ host.close()
+ client.close()
+ })
+})