···11+import {z} from 'zod/v4'
22+13export function assert<V>(value: V | undefined | null): V {
24 if (value == null) throw new Error('unexpected nullish!')
35 return value
46}
77+88+export function assertParsed<T>(value: z.ZodSafeParseResult<T>): T {
99+ if (!value.success) throw value.error
1010+ return value.data
1111+}
···11import {normalizeProtocolError} from '#lib/errors'
22import {makeExceptionMessage} from '#lib/schema'
33import {putSocket} from '#lib/socket'
44+import {WebSocket} from 'isomorphic-ws'
4556import {drivePreauth} from './driver-preauth'
67import {driveRealm} from './driver-realm'
···1213 * @param ws - the socket to drive through the machine
1314 * @returns a promise which resolves when the socket session completes
1415 */
1515-export async function machine(ws: WebSocket, signal?: AbortSignal) {
1616+export async function driveSocket(ws: WebSocket, signal?: AbortSignal) {
1617 try {
1717- const {realm, identid, pubkey} = await drivePreauth(ws, signal)
1818+ const auth = await drivePreauth(ws, 3_000, signal)
1919+ if (auth == null) return
2020+2121+ const {realm} = auth
1822 try {
1919- realm.attachSocket(identid, ws)
2020- await driveRealm(ws, realm, identid, pubkey, signal)
2323+ realm.attachSocket(auth.identid, ws)
2424+ console.log('attached socket:', auth.identid)
2525+ await driveRealm(ws, auth, signal)
2126 } finally {
2222- realm.detachSocket(identid, ws)
2727+ console.log('detached socket:', auth.identid)
2828+ realm.detachSocket(auth.identid, ws)
2329 }
2430 } catch (exc: unknown) {
2531 const error = normalizeProtocolError(exc)
+3-1
src/realm/server/realm.spec.ts
···11+import {beforeEach, describe, expect, it} from '@jest/globals'
22+import {WebSocket} from 'isomorphic-ws'
33+14import {RealmAction} from '#realm/schema'
25import {IdentBrand, RealmBrand} from '#realm/schema/brands'
36import {generate as generateTimestamp} from '#realm/schema/timestamp'
44-import {beforeEach, describe, expect, it} from '@jest/globals'
5768import {Realm} from './realm'
79
+1
src/realm/server/realm.ts
···11+import {WebSocket} from 'isomorphic-ws'
12import {DatabaseSync} from 'node:sqlite'
2334import {JWK, jwkExport, jwkImport, jwkSchema} from '#lib/crypto/jwks'