@smitejs/messaging
Messaging endpoints for Smite: channels with consumers, producers, and an in-memory broker executor.
yarn add @smitejs/messaging13 documented symbols · version 2.0.0-SNAPSHOT.0 · source
Builders
functionchannel
function channel<Payload = unknown>(app: AppDescriptor, name: string): MessagingChannelBuilder<Payload>;Creates a channel builder for an app. The shared (app, name) piece yields a builder that refines kind/schema and attaches any number of messaging .consumer children, related under the app's messaging.channel node.
const orders = channel(app, "orders")
.kind("topic")
.schema(z.object({ orderId: z.string() }))
.on(() => undefined);"messaging.channel"
"orders"
"topic"
1
1packages/messaging/src/channel.ts:84
interfaceMessagingChannelBuilder
interface MessagingChannelBuilder<Payload> {
readonly kind: <NextKind extends ChannelKind>(kind: NextKind) => MessagingChannelBuilder<Payload>;
readonly schema: <Next extends Payload>(schema: z.ZodType<Next>) => MessagingChannelBuilder<Next>;
readonly on: (fn: MessageConsumer<Payload>) => MessagingChannelBuilder<Payload>;
readonly node: MessagingChannelDescriptor<Payload>;
}A channel builder: pick a kind and optional zod schema from one shared channel(app, name) piece, then attach consumers with on(fn). Always a builder; each on() relates a messaging.consumer child under the channel.
packages/messaging/src/channel.ts:60
Collector
functionchannelsOf
function channelsOf(app: AppDescriptor): readonly CollectedChannel[];Walks an app's messaging.channel children and returns the collected channels with their consumers. Shared by artifact generators and the broker executor.
channel(app, "a").on(() => undefined);
channel(app, "b")
.kind("queue")
.on(() => undefined);
const collected = channelsOf(app);["a", "b"]packages/messaging/src/collector.ts:24
interfaceCollectedChannel
interface CollectedChannel<Payload = unknown> {
readonly name: string;
readonly kind: ChannelKind;
readonly schema?: z.ZodType<Payload>;
readonly consumers: readonly MessageConsumer<Payload>[];
}A channel as seen by artifact generators: its name, kind, and the payload schema it validates against.
packages/messaging/src/collector.ts:6
Executor
functionbroker
function broker(app: AppDescriptor): MessageBroker;A runtime in-memory broker that walks an app's messaging.channel IR tree (via child refs, never the registry) and delivers published payloads to the registered consumers. queue channels route to the most recent consumer; topic channels fan out to every consumer. Works standalone and behind a transport — same stateless per-trigger contract as every executor.
const bus = broker(app);
const delivered = await bus.publish("events", { kind: "order.placed" });2
["first:order.placed", "second:order.placed"]packages/messaging/src/broker.ts:20
constdispatchTo
const dispatchTo: <Payload = unknown>(descriptor: MessagingChannelDescriptor<Payload>, consumers: readonly MessagingConsumerDescriptor<Payload>[], payload: unknown, at?: number | Date) => Promise<number>;Fires a channel's consumers with a fresh envelope at the given (or current) instant. queue routes to the most recent consumer; topic fans out to every consumer. Returns the number of consumers invoked.
packages/messaging/src/channel.ts:128
interfaceMessageBroker
interface MessageBroker {
readonly channels: readonly CollectedChannel[];
readonly publish: (channel: string, payload: unknown) => Promise<number>;
readonly lookup: (channel: string) => CollectedChannel | undefined;
}A runtime handle returned by broker: the collected channels, plus publish to deliver a payload and lookup to find a channel by name.
packages/messaging/src/broker.ts:6
Internals
interfaceMessagingChannelDescriptor
interface MessagingChannelDescriptor<Payload> extends Descriptor<"messaging.channel", {
readonly name: string;
readonly kind: ChannelKind;
readonly schema?: z.ZodType<Payload>;
}> {
}A messaging.channel IR node.
packages/messaging/src/channel.ts:34
interfaceMessagingConsumerDescriptor
interface MessagingConsumerDescriptor<Payload> extends Descriptor<"messaging.consumer", {
readonly fn: MessageConsumer<Payload>;
}> {
}A node wrapping a channel's consumer function.
packages/messaging/src/channel.ts:49
Surface
constmessaging
const messaging: {
broker: typeof broker;
channel: typeof channel;
channelsOf: typeof channelsOf;
};The messaging namespace: one import for the whole messaging app extensor — channel builders, the broker executor, and the collector.
const bundle = messaging;"function"
"function"
"function"packages/messaging/src/index.ts:21
Types
typeChannelKind
type ChannelKind = "queue" | "topic";A named inbound/outbound messaging endpoint. queue delivers each message to one consumer; topic broadcasts to every consumer on the channel.
packages/messaging/src/channel.ts:5
typeMessageConsumer
type MessageConsumer<Payload> = (envelope: MessageEnvelope<Payload>) => void | Promise<void>;A consumer for a channel: receives each delivered MessageEnvelope.
packages/messaging/src/channel.ts:25
interfaceMessageEnvelope
interface MessageEnvelope<Payload> {
readonly id: string;
readonly channel: string;
readonly publishedAt: number;
readonly payload: Payload;
}The envelope a consumer receives.