UNPKG

bun-ws-router

Version:

Lightweight client/server WebSocket router for Bun with type-safe Zod/Valibot validation.

39 lines 1.9 kB
// SPDX-FileCopyrightText: 2025-present Kriasoft // SPDX-License-Identifier: MIT import { WebSocketRouter as BaseWebSocketRouter } from "../shared/router"; import { ZodValidatorAdapter } from "./adapter"; /** * WebSocket router for Bun that provides type-safe message routing with Zod validation. * Routes incoming messages to handlers based on message type. * * ARCHITECTURE: This is a thin wrapper that binds the Zod validator adapter to the * shared router implementation. The adapter pattern allows swapping between Zod and * Valibot without duplicating routing logic. * * @template T - Application-specific data to store with each WebSocket connection. * Always includes a clientId property generated automatically. * Example: { userId: string, roles: string[] } */ export class WebSocketRouter extends BaseWebSocketRouter { constructor() { // NOTE: ZodValidatorAdapter handles schema validation and error formatting // specific to Zod. The base router handles all routing and connection logic. super(new ZodValidatorAdapter()); } /** * Registers a message handler with Zod-specific type inference. * * This override provides validator-specific types for better IDE experience, * but creates a Liskov Substitution Principle variance issue. The more specific * handler signature means this class can't be used everywhere the base class is expected. * This is an intentional trade-off for better developer experience. * * @see specs/adrs.md#ADR-001 - Type override solution for IDE inference */ // @ts-expect-error - Intentional override with more specific types for better DX onMessage(schema, handler) { // eslint-disable-next-line @typescript-eslint/no-explicit-any return super.onMessage(schema, handler); } } //# sourceMappingURL=router.js.map