npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@crossdelta/cloudevents

v0.3.2

Published

CloudEvents toolkit for TypeScript - Zod validation, handler discovery, NATS JetStream

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)   │
  └──────────────┘      │              │      └──────────────┘
                        └──────────────┘
        │                                            │
        │  publishNatsRawEvent(...)                  │  handleEvent(...)
        ▼                                            ▼
  ┌──────────────┐                            ┌──────────────┐
  │ { orderId,   │                            │ Zod schema   │
  │   total }    │                            │ validates    │
  └──────────────┘                            └──────────────┘
bun add @crossdelta/cloudevents zod@4

Prerequisites: A running NATS server with JetStream enabled.

Note: Requires Zod v4 for full TypeScript support.

Quick Start

1. Create an event handler (src/handlers/order-created.event.ts):

import { handleEvent } from '@crossdelta/cloudevents'
import { z } from 'zod'

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: 'orders.created',
  },
  async (data) => {
    console.log(`New order: ${data.orderId}, total: ${data.total}`)
  },
)

2. Start consuming:

import { consumeJetStreamEvents } from '@crossdelta/cloudevents'

await consumeJetStreamEvents({
  stream: 'ORDERS',                         // Auto-created if not exists
  subjects: ['orders.*'],
  consumer: 'my-service',
  discover: './src/handlers/**/*.event.ts',
})

3. Publish from another service:

import { publish } from '@crossdelta/cloudevents'

await publish('orders.created', { orderId: 'ord_123', total: 99.99 })

That's it. Handlers are auto-discovered, validated with Zod, and messages persist in JetStream.


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 (orders.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: 'orders.created',
  },
  async (data) => { ... }
)

Handlers

Drop a *.event.ts file anywhere — it's auto-registered:

// src/handlers/user-signup.event.ts
import { z } from 'zod'

const UserSignupSchema = z.object({ 
  email: z.string().email(),
  name: z.string(),
})

// Export type for use in use-cases
export type UserSignupEvent = z.infer<typeof UserSignupSchema>

export default handleEvent(
  {
    schema: UserSignupSchema,
    type: 'users.signup',
  },
  async (data) => {
    await sendWelcomeEmail(data.email)
  },
)

Publishing

await publish('orders.created', orderData)

Consuming

// JetStream (recommended) — persistent, retries, exactly-once
await consumeJetStreamEvents({
  stream: 'ORDERS',
  subjects: ['orders.*'],
  consumer: 'billing',
  discover: './src/handlers/**/*.event.ts',
})

// Core NATS — fire-and-forget, simpler
await consumeNatsEvents({
  subjects: ['notifications.*'],
  discover: './src/handlers/**/*.event.ts',
})

Configuration

Environment Variables

NATS_URL=nats://localhost:4222
NATS_USER=myuser        # optional
NATS_PASSWORD=mypass    # optional

Consumer Options

await consumeJetStreamEvents({
  // Required
  stream: 'ORDERS',
  subjects: ['orders.*'],
  consumer: 'my-service',
  discover: './src/handlers/**/*.event.ts',

  // Optional
  servers: 'nats://localhost:4222',
  maxDeliver: 5,           // Retry attempts
  ackWait: 30_000,         // Timeout per attempt (ms)
  quarantineTopic: 'dlq',  // For poison messages
})

Advanced Features

Filter events by tenant:

export default handleEvent({
  type: 'orders.created',
  schema: OrderSchema,
  tenantId: 'tenant-a',  // Only process tenant-a events
}, async (data) => { ... })

Add custom filter logic:

export default handleEvent({
  type: 'orders.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 consumeJetStreamEvents({
  // ...
  idempotencyStore: redisStore,
})

Invalid messages are quarantined, not lost:

await consumeJetStreamEvents({
  // ...
  quarantineTopic: 'events.quarantine',
  errorTopic: 'events.errors',
})
import { Hono } from 'hono'
import { cloudEvents } from '@crossdelta/cloudevents'

const app = new Hono()
app.use('/events', cloudEvents({ discover: 'src/handlers/**/*.event.ts' }))

API

| Function | Purpose | |----------|---------| | handleEvent(options, handler) | Create a handler | | consumeJetStreamEvents(options) | Consume with persistence | | consumeNatsEvents(options) | Consume fire-and-forget | | publish(type, data) | Publish event |


License

MIT