UNPKG

@beignet/core

Version:

Core framework primitives for Beignet

83 lines 3.62 kB
import "../server-only.js"; import type { StandardSchemaV1 } from "@standard-schema/spec"; import { type ChannelDefinition, type InferChannelEvent, type InferChannelParams } from "./index.js"; /** Server-authenticated scope captured at the originating HTTP boundary. JSON-safe for jobs. */ export interface BroadcastOrigin { readonly version: 1; readonly clientId: string; readonly scope: string; } /** Validate an origin already captured by a trusted server for a job payload. */ export declare const broadcastOriginSchema: StandardSchemaV1<unknown, BroadcastOrigin>; /** Never derive principal, tenant, or namespace from the optional client header. */ export declare function resolveBroadcastOrigin(options: { headers: Headers; principalId?: string | null; tenantId?: string | null; namespace: string; }): BroadcastOrigin | undefined; export type BroadcastPublication<C extends ChannelDefinition> = InferChannelEvent<C> & { params: InferChannelParams<C>; excludeOrigin?: BroadcastOrigin; }; /** Readiness proves initial subscription only. Continuity loss requires reconnect and refetch. */ export interface BroadcastSubscription { readonly ready: Promise<void>; unsubscribe(): Promise<void>; } export interface BroadcastPort { /** Resolves when accepted by the provider, including when nobody is subscribed. */ publish<C extends ChannelDefinition>(channel: C, publication: BroadcastPublication<C>): Promise<void>; subscribe<C extends ChannelDefinition>(channel: C, options: { params: InferChannelParams<C>; onEvent: (event: InferChannelEvent<C>, metadata: { excludeOrigin?: BroadcastOrigin; }) => void; onDisconnect: () => void; }): BroadcastSubscription; } /** Internal transport envelope carried by broadcast providers, never a browser payload. */ export interface BroadcastEnvelope { version: 1; channel: string; params: Record<string, string>; event: string; data: unknown; excludeOrigin?: BroadcastOrigin; } /** Low-level adapter contract. Subscription callbacks must be isolated from publishers. */ export interface BroadcastTransport { publish(key: string, envelope: BroadcastEnvelope): Promise<void>; subscribe(key: string, options: { onMessage: (envelope: unknown) => void; onDisconnect: () => void; }): BroadcastSubscription; } /** Shared validation and cleanup for provider implementations. */ export declare function createBroadcastPort(transport: BroadcastTransport): BroadcastPort; export interface ChannelBinding<Ctx> { readonly kind: "channel-binding"; readonly channel: ChannelDefinition; authorize: (args: { ctx: Ctx; params: Record<string, string>; }) => void | Promise<void>; } export interface ChannelRegistry<Ctx> { readonly kind: "channel-registry"; readonly bindings: readonly ChannelBinding<Ctx>[]; get(name: string): ChannelBinding<Ctx> | undefined; } /** Bind every channel explicitly, including public channels. */ export declare function createBroadcasting<Ctx>(): { defineChannelBinding: <C extends ChannelDefinition>(channel: C, options: { authorize: (args: { ctx: Ctx; params: InferChannelParams<C>; }) => void | Promise<void>; }) => ChannelBinding<Ctx>; defineChannelRegistry: (bindings: readonly ChannelBinding<Ctx>[]) => ChannelRegistry<Ctx>; }; /** Validate a configurable deadline without allowing an unbounded stream. */ export declare function broadcastLifetime(value?: number): number; //# sourceMappingURL=server.d.ts.map