feat: client shell, sign-in and protocol client (#67)
New `client/` package (Vite + TypeScript + PixiJS v8, plain DOM overlay): the pure reducer seam (no Pixi import), an append-only slice registry build-time glob, the `proto` 2 WS client with envelope/ack/closing mapping, the sign-in door (`POST /join`, one "Key not recognised." for wrong and unknown), the DOM shell/HUD and a minimal Pixi stage created only after a live `hello`. `hello` resets every slice so a reconnect cannot show stale state; `proto-mismatch` shows reload; `revoked` returns to the sign-in page and stops. The browser e2e moves into the client package (`client/e2e/`, owning playwright-core) and runs the real Vite build through the TLS/HTTP2 test nginx: join -> canvas -> hello/roster for GM and player, no data before join, identical wrong/unknown key refusal, proto mismatch, revoked. `devbox run client-test` adds the Vitest seam; `devbox run e2e` builds the client first. The #66 stub dist stays local to `tools/vhost.test.mjs`, so `devbox run test` still needs no npm install. Refs #67
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
// The protocol client (DESIGN 6.2, 6.3, 6.5; spec #63 decision 14). It owns the socket, the `reqId`
|
||||
// counter and the pending-request map; it emits reducer events and never touches the DOM. The
|
||||
// socket factory and clock-free design make it unit-testable without a browser.
|
||||
//
|
||||
// Close mapping (6.5, FIXED #18): a server-initiated close is `closing {reason, code}` and then a
|
||||
// close frame the browser reports as 1005, so the reason always comes from the last `closing` frame.
|
||||
// `revoked` and `proto-mismatch` are terminal: this client does not reconnect to them. Reconnect
|
||||
// with backoff is T5 (#68); T4 only proves the outcome mapping.
|
||||
|
||||
import type { AckFrame, CloseReason, ServerFrame } from './protocol.ts'
|
||||
import { encodeIntent, isAck, isClosing, parseServerFrame, wsUrl } from './protocol.ts'
|
||||
|
||||
export interface SocketLike {
|
||||
send(data: string): void
|
||||
close(code?: number, reason?: string): void
|
||||
onopen: (() => void) | null
|
||||
onmessage: ((event: { data: unknown }) => void) | null
|
||||
onclose: ((event: { code: number; reason: string }) => void) | null
|
||||
onerror: ((event: unknown) => void) | null
|
||||
}
|
||||
|
||||
export type ClientEvent =
|
||||
| { type: 'open' }
|
||||
| { type: 'frame'; frame: ServerFrame }
|
||||
| { type: 'close'; reason: CloseReason | null; code: number | null }
|
||||
|
||||
export interface ProtocolClientOptions {
|
||||
url?: string
|
||||
createSocket?: (url: string) => SocketLike
|
||||
onEvent: (event: ClientEvent) => void
|
||||
}
|
||||
|
||||
type Pending = {
|
||||
resolve: (ack: AckFrame) => void
|
||||
reject: (error: Error) => void
|
||||
}
|
||||
|
||||
function defaultSocket(url: string): SocketLike {
|
||||
return new WebSocket(url) as unknown as SocketLike
|
||||
}
|
||||
|
||||
export class ProtocolClient {
|
||||
#url: string
|
||||
#createSocket: (url: string) => SocketLike
|
||||
#onEvent: (event: ClientEvent) => void
|
||||
#socket: SocketLike | null = null
|
||||
#nextReqId = 1
|
||||
#pending = new Map<number, Pending>()
|
||||
#lastClosing: { reason: CloseReason; code: number } | null = null
|
||||
#state: 'idle' | 'connecting' | 'open' | 'closed' = 'idle'
|
||||
|
||||
constructor(options: ProtocolClientOptions) {
|
||||
this.#url = options.url ?? wsUrl()
|
||||
this.#createSocket = options.createSocket ?? defaultSocket
|
||||
this.#onEvent = options.onEvent
|
||||
}
|
||||
|
||||
get state(): 'idle' | 'connecting' | 'open' | 'closed' {
|
||||
return this.#state
|
||||
}
|
||||
|
||||
get pendingCount(): number {
|
||||
return this.#pending.size
|
||||
}
|
||||
|
||||
connect(): void {
|
||||
if (this.#state !== 'idle') return
|
||||
this.#state = 'connecting'
|
||||
const socket = this.#createSocket(this.#url)
|
||||
this.#socket = socket
|
||||
|
||||
socket.onopen = () => {
|
||||
if (this.#state !== 'connecting') return
|
||||
this.#state = 'open'
|
||||
this.#onEvent({ type: 'open' })
|
||||
}
|
||||
socket.onmessage = event => {
|
||||
if (this.#state === 'closed') return // a frame in flight after the client closed
|
||||
const frame = parseServerFrame(event.data)
|
||||
if (!frame) return // binary or unreadable: 6.2 says ignore what cannot be read
|
||||
if (isClosing(frame)) this.#lastClosing = { reason: frame.reason, code: frame.code }
|
||||
if (isAck(frame)) {
|
||||
const pending = this.#pending.get(frame.reqId)
|
||||
if (pending) {
|
||||
this.#pending.delete(frame.reqId)
|
||||
pending.resolve(frame)
|
||||
}
|
||||
}
|
||||
this.#onEvent({ type: 'frame', frame })
|
||||
}
|
||||
socket.onerror = () => {
|
||||
// The browser follows an error with a close; mapping the outcome there keeps it single.
|
||||
}
|
||||
socket.onclose = event => {
|
||||
if (this.#state === 'closed') return
|
||||
this.#state = 'closed'
|
||||
const reason = this.#lastClosing?.reason ?? null
|
||||
const code = this.#lastClosing?.code ?? event.code ?? null
|
||||
this.#failPending(new Error(`connection closed (${reason ?? code ?? 'no code'})`))
|
||||
this.#onEvent({ type: 'close', reason, code })
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send one intent and resolve with its single `ack` (DESIGN 6 Conventions: exactly one ack per
|
||||
* readable intent, whatever the outcome). Pending reqIds die with the connection (6.3), and a
|
||||
* `hello` on a live connection does not drop them.
|
||||
*/
|
||||
send(t: string, fields: Record<string, unknown> = {}): Promise<AckFrame> {
|
||||
const socket = this.#socket
|
||||
if (this.#state !== 'open' || !socket) {
|
||||
return Promise.reject(new Error('not connected'))
|
||||
}
|
||||
const reqId = this.#nextReqId++
|
||||
const frame = encodeIntent(t, reqId, fields)
|
||||
return new Promise<AckFrame>((resolve, reject) => {
|
||||
this.#pending.set(reqId, { resolve, reject })
|
||||
try {
|
||||
socket.send(frame)
|
||||
} catch (error) {
|
||||
this.#pending.delete(reqId)
|
||||
reject(error instanceof Error ? error : new Error(String(error)))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
close(): void {
|
||||
if (this.#state === 'closed') return
|
||||
this.#state = 'closed'
|
||||
try {
|
||||
this.#socket?.close()
|
||||
} catch {
|
||||
// A socket that is already gone is the outcome we wanted.
|
||||
}
|
||||
this.#failPending(new Error('connection closed by the client'))
|
||||
}
|
||||
|
||||
#failPending(error: Error): void {
|
||||
const pending = [...this.#pending.values()]
|
||||
this.#pending.clear()
|
||||
for (const entry of pending) entry.reject(error)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user