/** * The JSON `Envelope` wire protocol shared by every P2P transport. * * Authoritative state is encoded with the REAL `@colyseus/schema` `Encoder` * (see `runtime.ts`) and decoded with the real `Decoder` (`client.ts`). The * encoder's `Uint8Array` output rides as a base64 STRING on the envelope * (`join-ok.state`, `state-snapshot.state`, `state-patch.patch`), so the whole * envelope survives `JSON.stringify` on any transport unchanged. See * `wire-bytes.ts`. */ export type Envelope = | { readonly kind: 'join'; readonly requestId: string; readonly room: string; readonly options?: unknown; } | { readonly kind: 'join-ok'; readonly requestId: string; readonly sessionId: string; /** base64-encoded full-state bytes from `Encoder.encodeAll`/`encodeAllView`. */ readonly state: string; readonly clock: number; } | { readonly kind: 'join-error'; readonly requestId: string; readonly message: string } | { readonly kind: 'message'; readonly type: string; readonly payload?: unknown } | { readonly kind: 'state-patch'; /** base64-encoded delta bytes from `Encoder.encode`/`encodeView`. */ readonly patch: string; readonly clock: number; } | { readonly kind: 'state-snapshot'; /** base64-encoded full-state bytes (resync). */ readonly state: string; readonly clock: number; } | { readonly kind: 'state-resync'; readonly clock: number } | { readonly kind: 'leave'; readonly code?: number; readonly reason?: string } | { readonly kind: 'ping'; readonly t: number } | { readonly kind: 'pong'; readonly t: number }; export const P2P_CLOSE_CODES = { badEnvelope: 4400, unauthorized: 4401, roomNotFound: 4404, joinTimeout: 4408, envelopeTooLarge: 4413, rateLimited: 4429, relayQuotaExceeded: 4430, roomFull: 4431, hostMissing: 4432, relayNotAllowed: 4433, internalError: 4500, } as const; export type P2PCloseCode = (typeof P2P_CLOSE_CODES)[keyof typeof P2P_CLOSE_CODES]; export function isEnvelope(value: unknown): value is Envelope { return typeof value === 'object' && value !== null && 'kind' in value; }