@crossdelta/cloudevents
v0.13.0
Published
CloudEvents toolkit for TypeScript - Zod validation, handler discovery, NATS JetStream & Core
Maintainers
Readme
@crossdelta/cloudevents
Type-safe event-driven microservices with NATS and Zod validation, using the CloudEvents specification.
NATS JetStream
┌──────────────┐
┌──────────────┐ │ │ ┌──────────────┐
│ Service │ │ Stream: │ │ Service │
│ (publish) │─────▶│ ORDERS │─────▶│ (consume) │
└──────────────┘ │ │ └──────────────┘
└──────────────┘
│ │
│ publish(...) │ handleEvent(...)
▼ ▼
┌──────────────┐ ┌──────────────┐
│ { orderId, │ │ Zod schema │
│ total } │ │ validates │
└──────────────┘ └──────────────┘bun add @crossdelta/cloudevents zod@4Prerequisites: A running NATS server with JetStream enabled.
Note: Requires Zod v4 for full TypeScript support.
Quick Start
Note: Services never create streams. Streams must exist before services consume from them:
- Development: Use NATS CLI or Platform SDK (
pf devauto-creates from contracts)- Production: Use infrastructure-as-code (Pulumi, Terraform) or the Stream Setup function below
1. Create an event handler (src/events/order-created.handler.ts):
import { handleEvent } from '@crossdelta/cloudevents'
import { z } from 'zod'
// Export schema for mock generation
export const OrderCreatedSchema = z.object({
orderId: z.string(),
total: z.number(),
})
// Export type for use in use-cases
export type OrderCreatedEvent = z.infer<typeof OrderCreatedSchema>
export default handleEvent(
{
schema: OrderCreatedSchema,
type: 'order.created',
},
async (data) => {
console.log(`New order: ${data.orderId}, total: ${data.total}`)
},
)2. Start consuming:
import { consumeJetStreams } from '@crossdelta/cloudevents'
consumeJetStreams({
streams: ['ORDERS'],
consumer: 'my-service',
discover: './src/events/**/*.handler.ts',
})3. Publish from another service:
import { publish } from '@crossdelta/cloudevents'
await publish('order.created', { orderId: 'ord_123', total: 99.99 })
// String API auto-pluralizes the domain token: subject → 'orders.created'
// Use createContract({ type, channel, schema }) for explicit subject control (no pluralization)That's it. Handlers are auto-discovered, validated with Zod, and messages persist in JetStream.
Stream creation is handled by your development environment or infrastructure — not by services:
- Development: NATS CLI, @crossdelta/platform-sdk, or manual setup
- Production: See Stream Setup below for
ensureJetStreams()usage in infrastructure code
Why use this?
| Problem | Solution | |---------|----------| | Messages lost on restart | JetStream persists messages | | Scattered handler registration | Auto-discovery via glob patterns | | Runtime type errors | Zod validation with TypeScript inference | | Poison messages crash services | DLQ quarantines invalid messages |
Core Concepts
Event Type vs. Event Data
Important distinction:
- Event Type (
order.created): Lives in the CloudEvent envelope (ce.type). Used for routing and handler matching. - Event Data (
{ orderId, total }): The actual payload. Does not include the type.
const Schema = z.object({
orderId: z.string(),
})
export default handleEvent(
{
schema: Schema,
type: 'order.created',
},
async (data) => { ... }
)Handlers
Drop a *.handler.ts file anywhere — it's auto-registered:
// src/events/user-signup.handler.ts
import { z } from 'zod'
const UserSignupSchema = z.object({
email: z.email(),
name: z.string(),
})
// Export type for use in use-cases
export type UserSignupEvent = z.infer<typeof UserSignupSchema>
export default handleEvent(
{
schema: UserSignupSchema,
type: 'user.signup',
},
async (data) => {
await sendWelcomeEmail(data.email)
},
)Publishing
await publish('order.created', orderData)publish() sends over NATS Core and returns without waiting for a stream. Where an answer to a sender depends on the message being stored (a webhook acknowledged with 2xx), use publishConfirmed(): it resolves only with the stream's acknowledgement and rejects with a PublishNotConfirmedError (reason: no-stream, timeout, disconnected, wrong-stream, rejected) otherwise. Its timeout bounds the whole call, opening the connection included.
try {
const { stream, seq, duplicate } = await publishConfirmed(OrderReceivedContract, data, {
messageId: webhookId, // a redelivery inside the stream's duplicate window is not stored twice
timeout: 2000,
})
return reply.status(202)
} catch (error) {
if (error instanceof PublishNotConfirmedError) return reply.status(503) // the sender retries
throw error
}Stream Setup
For infrastructure use only - Services should never call this function.
Create streams during infrastructure setup (Pulumi, deployment scripts):
import { ensureJetStreams } from '@crossdelta/cloudevents'
await ensureJetStreams({
streams: [
{
stream: 'ORDERS',
subjects: ['order.>'], // singular — matches contract subjects (order.created, order.completed, …)
config: {
maxAge: 7 * 24 * 60 * 60 * 1000, // 7 days retention
replicas: 3, // For HA
},
},
],
})Consuming
import { consumeJetStreams } from '@crossdelta/cloudevents'
// JetStream (recommended) — persistent, retries, exactly-once
consumeJetStreams({
streams: ['ORDERS'],
consumer: 'billing',
discover: './src/events/**/*.handler.ts',
})
// Core NATS — fire-and-forget, simpler
await consumeNatsEvents({
subject: 'notifications.*',
consumerName: 'my-service',
discover: './src/events/**/*.handler.ts',
})
// NATS Core Request-Reply — synchronous validation feedback
// Consumer returns result, publisher awaits it
import { request } from '@crossdelta/cloudevents'
const result = await request('call.received', payload)
// result: { accepted: true, data: { ... } } | { accepted: false, error: '...' }Shutting down
The package registers no signal handler; the service calls these from its own SIGTERM handler, in this order, under its own deadline:
import { closeConnection, drainNatsConsumers } from '@crossdelta/cloudevents'
await drainNatsConsumers() // 1. no new messages; running handlers finish
await server.stop() // 2. HTTP requests in flight may still publish
await closeConnection() // 3. the publisher connectiondrainNatsConsumers()stops the intake of every NATS Core and JetStream consumer, so the queue group hands new messages to the other replicas, waits until each message loop has handled what it holds (a JetStream message is acknowledged then), and drains the consumer connections. A message the loop never reached stays unacknowledged and is redelivered afterack_wait.closeConnection()drains and closes the publisher. It does not wait for a first connect that is still pending (withwaitOnFirstConnectthat connect never settles while NATS is down); a connection that arrives after the close is closed at once.
Configuration
Environment Variables
NATS_URL=nats://localhost:4222
NATS_USER=myuser # optional
NATS_PASSWORD=mypass # optionalOptional: Google Cloud Pub/Sub
To use Google Cloud Pub/Sub instead of NATS, install the optional dependency:
bun add @google-cloud/pubsub
# or
npm install @google-cloud/pubsubThen use the publishToPubSub function:
import { publishToPubSub } from '@crossdelta/cloudevents'
await publishToPubSub('order.created', { orderId: 'ord_123', total: 99.99 })Note: @google-cloud/pubsub is not bundled with this library to keep the package size small. Install it only if you need Google Cloud Pub/Sub support.
Stream Options
await ensureJetStreams({
streams: [
{
// Required
stream: 'ORDERS',
subjects: ['order.>'], // singular — matches contract subjects
// Optional
config: {
maxAge: 7 * 24 * 60 * 60 * 1000, // Message retention (ms)
maxBytes: 1024 * 1024 * 1024, // Reserved at creation, not on write
replicas: 1, // Replication factor
storage: 'file', // 'file' or 'memory'
retention: 'limits', // 'limits', 'interest', or 'workqueue'
},
},
],
// Optional
servers: 'nats://localhost:4222',
})Consumer Options
consumeJetStreams({
// Required
streams: ['ORDERS'],
consumer: 'my-service',
discover: './src/events/**/*.handler.ts',
// Optional
servers: 'nats://localhost:4222',
filterSubjects: ['order.created'], // Filter specific subjects
maxDeliver: 5, // Retry attempts
ackWait: 30_000, // Timeout per attempt (ms)
quarantineTopic: 'dlq', // For poison messages
})Advanced Features
Contracts can include channel metadata to define which NATS JetStream stream they belong to. This enables infrastructure-as-code workflows where streams are materialized from contracts.
Basic Contract with Channel
import { createContract } from '@crossdelta/cloudevents'
import { z } from 'zod'
export const OrdersCreatedContract = createContract({
type: 'order.created',
channel: {
stream: 'ORDERS',
// subject defaults to 'order.created' if omitted
},
schema: z.object({
orderId: z.string(),
total: z.number(),
}),
})Channel Configuration
interface ChannelConfig {
stream: string // JetStream stream name (e.g., 'ORDERS')
subject?: string // NATS subject (optional override)
}Subject Routing:
- Without
subject: defaults totype—order.createdstaysorder.created - With
subject: uses that exact string
Note:
publish('order.x', data)(string API) auto-pluralizes →orders.x. Contracts do not. Configure JetStream streams with singular subjects (order.>) to match contract events.
// subject = 'order.created' (from type)
createContract({ type: 'order.created', channel: { stream: 'ORDERS' } })
// subject = 'orders.v1.created' (explicit override)
createContract({ type: 'order.created', channel: { stream: 'ORDERS', subject: 'orders.v1.created' } })Multiple Events, Same Stream
// order.created → ORDERS stream
export const OrdersCreatedContract = createContract({
type: 'order.created',
channel: { stream: 'ORDERS' },
schema: OrderCreatedSchema,
})
// order.updated → ORDERS stream (same stream!)
export const OrdersUpdatedContract = createContract({
type: 'order.updated',
channel: { stream: 'ORDERS' },
schema: OrderUpdatedSchema,
})Result: Both events share the ORDERS stream with subjects order.created and order.updated.
Infrastructure Materialization
Channel metadata is conceptual - it defines routing, not infrastructure policy.
Contracts define:
- Event type (
order.created) - Stream routing (
ORDERS) - Subject mapping (
order.created)
Infrastructure defines:
- Retention (7 days, 14 days, etc.)
- Storage (disk, memory)
- Replicas (1, 3, etc.)
- Limits (max message size, etc.)
Example Infrastructure (Pulumi):
import { collectStreamDefinitions } from './helpers/streams'
import { ensureJetStreams } from '@crossdelta/cloudevents'
// Collect from contracts
const streams = collectStreamDefinitions()
// [{ stream: 'ORDERS', subjects: ['order.created', 'order.updated'] }]
// Materialize with policies
await ensureJetStreams({
streams: streams.map(({ stream, subjects }) => ({
stream,
subjects,
config: {
maxAge: 14 * 24 * 60 * 60 * 1000, // 14 days
replicas: 3, // High availability
storage: 'file', // Persistent
},
})),
})Development vs. Production
| Environment | Streams | How | Retention | |-------------|---------|-----|-----------| | Development | Ephemeral | Auto-created by services | None (memory) | | Production | Persistent | Materialized via infrastructure | Explicit (7d, 14d, etc.) |
Key Principle: Contracts define what and where, infrastructure defines how and how long.
Backward Compatibility
Channel metadata is optional - contracts without channel work as before:
// Legacy contract (still works)
export const LegacyContract = createContract({
type: 'legacy.event',
schema: LegacySchema,
// No channel - handler still works
})When multiple services consume the same event, use createContract to create shared event contracts:
// packages/contracts/src/events/orders/created.ts
import { createContract } from '@crossdelta/cloudevents'
import { z } from 'zod'
export const OrderCreatedSchema = z.object({
orderId: z.string(),
total: z.number(),
})
export type OrderCreatedData = z.infer<typeof OrderCreatedSchema>
export const OrdersCreatedContract = createContract({
type: 'order.created',
schema: OrderCreatedSchema,
})Then import and use in handlers:
import { handleEvent } from '@crossdelta/cloudevents'
import { OrdersCreatedContract } from '@my-org/contracts'
export default handleEvent(
OrdersCreatedContract,
async (data) => {
// data is typed as OrderCreatedData
console.log(data.orderId)
},
)Benefits:
- Single source of truth for event schemas
- No schema duplication
- Type safety across services
Filter events by tenant:
export default handleEvent({
type: 'order.created',
schema: OrderSchema,
tenantId: 'tenant-a', // Only process tenant-a events
}, async (data) => { ... })Use type: '*' to create a handler that processes all events, regardless of type. Useful for audit logging, metrics collection, or event replay.
import { handleEvent } from '@crossdelta/cloudevents'
import { z } from 'zod'
export default handleEvent(
{
type: '*',
schema: z.record(z.string(), z.unknown()),
},
async (data, context) => {
await persistAuditEvent({
eventType: context.eventType,
source: context.source,
data,
})
},
)Routing behavior:
- Specific handlers (
type: 'order.created') always take priority - The wildcard handler runs only when no specific handler matches
- Only one wildcard handler per consumer is recommended
- Works with both JetStream and NATS Core consumers
Add custom filter logic:
export default handleEvent({
type: 'order.created',
schema: OrderSchema,
match: (event) => event.data.total > 100, // Only high-value orders
}, async (data) => { ... })Deduplication is built-in. For distributed systems, provide a Redis store:
const redisStore = {
has: (id) => redis.exists(`idem:${id}`),
add: (id, ttl) => redis.set(`idem:${id}`, '1', 'PX', ttl),
}
await consumeJetStreams({
streams: ['ORDERS'],
consumer: 'my-service',
discover: './src/events/**/*.handler.ts',
idempotencyStore: redisStore,
})Invalid messages are quarantined, not lost:
await consumeJetStreams({
streams: ['ORDERS'],
consumer: 'my-service',
discover: './src/events/**/*.handler.ts',
quarantineTopic: 'events.quarantine', // For malformed messages
errorTopic: 'events.errors', // For handler errors
})Messages that fail validation go to quarantineTopic, messages that crash handlers go to errorTopic.
import { Hono } from 'hono'
import { cloudEvents } from '@crossdelta/cloudevents'
const app = new Hono()
app.use('/events', cloudEvents({ discover: 'src/events/**/*.handler.ts' }))API
| Function | Purpose |
|----------|---------|
| handleEvent(options, handler) | Create a handler (supports type: '*' wildcard) |
| createContract(options) | Create shared event contract |
| ensureJetStreams(options) | Create/update JetStream streams |
| consumeJetStreams(options) | Consume from multiple streams |
| consumeNatsEvents(options) | Consume fire-and-forget |
| publish(type, data) | Publish event to JetStream (fire-and-forget) |
| publishConfirmed(contract, data, opts?) | Publish to JetStream and resolve only after the stream acknowledged it |
| request(type, data, opts?) | Send command via NATS Core and await synchronous reply |
| isNatsConnected() | Check if NATS publisher connection is live (for health checks) |
| isConsumerConnected() | Check if any NATS consumer connection is live (for readiness probes) |
| configureNatsPublisher(config) | Set connection options for the publisher singleton |
| drainNatsConsumers() | Stop every consumer's intake, let running handlers finish, drain the consumer connections (shutdown) |
| closeConnection() | Drain and close the publisher connection (shutdown, CLI tools); never waits for a pending first connect |
Distributed Tracing
When @crossdelta/telemetry is installed (optional peer dependency), trace context is propagated automatically across NATS:
- Publish: The current W3C
traceparentis injected as a CloudEvent extension field. - Consume: The
traceparentextension is extracted and restored in AsyncLocalStorage before the handler runs.
This enables end-to-end distributed traces in Grafana:
Storefront → API Gateway → NATS → Orders Service → NATS → Audit Service
└── all spans share the same traceId ──┘No configuration required. If @crossdelta/telemetry is not installed, CloudEvents work normally without trace propagation.
License
MIT © crossdelta
