UNPKG

bun-ws-router

Version:

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

26 lines 1.62 kB
import type { ServerWebSocket } from "bun"; import type { InferOutput } from "valibot"; import * as v from "valibot"; import type { MessageSchemaType } from "./types"; /** * Validates a message against its schema and publishes it to a WebSocket topic. * * PURPOSE: Ensures all published messages conform to their schemas, preventing * runtime errors for subscribers expecting specific message formats. * * FLOW: Extract type → Construct message → Validate → Publish (or log error) * * @param ws - The ServerWebSocket instance to publish from * @param topic - The topic to publish to (subscribers will receive the message) * @param schema - The Valibot schema to validate the message against * @param payload - The payload to include in the message (type inferred from schema) * @param metaOrOpts - Optional metadata or options object with origin tracking * @returns True if message was validated and published successfully */ export declare function publish<Schema extends MessageSchemaType>(ws: ServerWebSocket<{ clientId: string; } & Record<string, unknown>>, topic: string, schema: Schema, payload: Schema extends v.ObjectSchema<infer TEntries, any> ? TEntries extends Record<string, any> ? "payload" extends keyof TEntries ? InferOutput<TEntries["payload"]> : unknown : unknown : unknown, metaOrOpts?: Partial<Schema extends v.ObjectSchema<infer TEntries, any> ? TEntries extends Record<string, any> ? "meta" extends keyof TEntries ? InferOutput<TEntries["meta"]> : unknown : unknown : unknown> | { origin?: string; key?: string; }): boolean; //# sourceMappingURL=publish.d.ts.map