@crossdelta/cloudevents 0.5.6 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/README.md +48 -28
  2. package/dist/index.cjs +1602 -0
  3. package/dist/index.d.mts +812 -0
  4. package/dist/index.d.ts +812 -9
  5. package/dist/index.js +1574 -6
  6. package/package.json +20 -18
  7. package/dist/adapters/cloudevents/cloudevents.d.ts +0 -14
  8. package/dist/adapters/cloudevents/cloudevents.js +0 -58
  9. package/dist/adapters/cloudevents/index.d.ts +0 -8
  10. package/dist/adapters/cloudevents/index.js +0 -7
  11. package/dist/adapters/cloudevents/parsers/binary-mode.d.ts +0 -5
  12. package/dist/adapters/cloudevents/parsers/binary-mode.js +0 -32
  13. package/dist/adapters/cloudevents/parsers/pubsub.d.ts +0 -5
  14. package/dist/adapters/cloudevents/parsers/pubsub.js +0 -54
  15. package/dist/adapters/cloudevents/parsers/raw-event.d.ts +0 -5
  16. package/dist/adapters/cloudevents/parsers/raw-event.js +0 -17
  17. package/dist/adapters/cloudevents/parsers/structured-mode.d.ts +0 -5
  18. package/dist/adapters/cloudevents/parsers/structured-mode.js +0 -18
  19. package/dist/adapters/cloudevents/types.d.ts +0 -29
  20. package/dist/adapters/cloudevents/types.js +0 -1
  21. package/dist/domain/contract-helper.d.ts +0 -63
  22. package/dist/domain/contract-helper.js +0 -61
  23. package/dist/domain/discovery.d.ts +0 -24
  24. package/dist/domain/discovery.js +0 -201
  25. package/dist/domain/handler-factory.d.ts +0 -49
  26. package/dist/domain/handler-factory.js +0 -169
  27. package/dist/domain/index.d.ts +0 -6
  28. package/dist/domain/index.js +0 -4
  29. package/dist/domain/types.d.ts +0 -108
  30. package/dist/domain/types.js +0 -6
  31. package/dist/domain/validation.d.ts +0 -37
  32. package/dist/domain/validation.js +0 -53
  33. package/dist/infrastructure/errors.d.ts +0 -53
  34. package/dist/infrastructure/errors.js +0 -54
  35. package/dist/infrastructure/index.d.ts +0 -4
  36. package/dist/infrastructure/index.js +0 -2
  37. package/dist/infrastructure/logging.d.ts +0 -18
  38. package/dist/infrastructure/logging.js +0 -27
  39. package/dist/middlewares/cloudevents-middleware.d.ts +0 -171
  40. package/dist/middlewares/cloudevents-middleware.js +0 -276
  41. package/dist/middlewares/index.d.ts +0 -1
  42. package/dist/middlewares/index.js +0 -1
  43. package/dist/processing/dlq-safe.d.ts +0 -82
  44. package/dist/processing/dlq-safe.js +0 -108
  45. package/dist/processing/handler-cache.d.ts +0 -36
  46. package/dist/processing/handler-cache.js +0 -94
  47. package/dist/processing/idempotency.d.ts +0 -51
  48. package/dist/processing/idempotency.js +0 -112
  49. package/dist/processing/index.d.ts +0 -4
  50. package/dist/processing/index.js +0 -4
  51. package/dist/processing/validation.d.ts +0 -41
  52. package/dist/processing/validation.js +0 -48
  53. package/dist/publishing/index.d.ts +0 -2
  54. package/dist/publishing/index.js +0 -2
  55. package/dist/publishing/nats.publisher.d.ts +0 -19
  56. package/dist/publishing/nats.publisher.js +0 -115
  57. package/dist/publishing/pubsub.publisher.d.ts +0 -39
  58. package/dist/publishing/pubsub.publisher.js +0 -84
  59. package/dist/transports/nats/base-message-processor.d.ts +0 -44
  60. package/dist/transports/nats/base-message-processor.js +0 -118
  61. package/dist/transports/nats/index.d.ts +0 -5
  62. package/dist/transports/nats/index.js +0 -5
  63. package/dist/transports/nats/jetstream-consumer.d.ts +0 -217
  64. package/dist/transports/nats/jetstream-consumer.js +0 -367
  65. package/dist/transports/nats/jetstream-message-processor.d.ts +0 -9
  66. package/dist/transports/nats/jetstream-message-processor.js +0 -32
  67. package/dist/transports/nats/nats-consumer.d.ts +0 -36
  68. package/dist/transports/nats/nats-consumer.js +0 -84
  69. package/dist/transports/nats/nats-message-processor.d.ts +0 -11
  70. package/dist/transports/nats/nats-message-processor.js +0 -32
@@ -0,0 +1,812 @@
1
+ import { Context } from 'hono';
2
+ import { CloudEventV1 } from 'cloudevents';
3
+ import { ZodTypeAny, z } from 'zod';
4
+ import { ConsumerMessages, Subscription } from 'nats';
5
+
6
+ /**
7
+ * Adapter types for external protocols
8
+ */
9
+
10
+ /**
11
+ * Simplified event context for handler consumption
12
+ *
13
+ * Provides essential CloudEvent metadata without the complexity of the full CloudEventV1 interface.
14
+ * This is what handler functions receive as context parameter.
15
+ */
16
+ interface EventContext {
17
+ /** Event type identifier (from CloudEvent.type) */
18
+ eventType: string;
19
+ /** Event source (from CloudEvent.source) */
20
+ source: string;
21
+ /** Optional subject (from CloudEvent.subject) */
22
+ subject?: string;
23
+ /** Event timestamp (from CloudEvent.time) */
24
+ time: string;
25
+ /** Message ID for deduplication (from CloudEvent.id) */
26
+ messageId?: string;
27
+ }
28
+ /**
29
+ * Result of CloudEvent parsing - either CloudEvents SDK object or normalized data
30
+ */
31
+ interface CloudEventParseResult {
32
+ event: CloudEventV1<unknown> | null;
33
+ cloudEventData: CloudEventV1<unknown> | null;
34
+ }
35
+
36
+ /**
37
+ * Parses CloudEvent from Hono context with automatic format detection.
38
+ *
39
+ * Supports:
40
+ * 1. Structured CloudEvent (body with specversion)
41
+ * 2. Pub/Sub push message format (body with message field)
42
+ * 3. Binary CloudEvent (CloudEvent headers)
43
+ * 4. Raw event data (fallback)
44
+ *
45
+ * Uses dynamic imports to load only necessary parsers for optimal performance.
46
+ */
47
+ declare const parseEventFromContext: (context: Context) => Promise<CloudEventParseResult>;
48
+
49
+ /**
50
+ * Domain Types
51
+ *
52
+ * Core business logic types for handlers, events, and domain operations
53
+ */
54
+
55
+ /**
56
+ * Event handler interface
57
+ */
58
+ interface EventHandler<T = unknown> {
59
+ handle(payload: T, context?: unknown): Promise<void> | void;
60
+ }
61
+ /**
62
+ * Constructor type for event handler classes that implement the EventHandler interface
63
+ */
64
+ type HandlerConstructor<T = unknown> = (new (...args: unknown[]) => EventHandler<T>) & {
65
+ __eventarcMetadata?: {
66
+ schema: ZodTypeAny;
67
+ declaredType?: string;
68
+ match?: (event: EnrichedEvent<T>) => boolean;
69
+ safeParse?: boolean;
70
+ };
71
+ };
72
+ /**
73
+ * Enriched event with both data and context
74
+ */
75
+ interface EnrichedEvent<T> extends EventContext {
76
+ data: T;
77
+ /** Tenant identifier for multi-tenant event routing */
78
+ tenantId?: string;
79
+ }
80
+ /**
81
+ * Match function type for custom event matching
82
+ */
83
+ type MatchFn<T> = (event: EnrichedEvent<T>) => boolean;
84
+ /**
85
+ * Channel metadata for NATS JetStream routing
86
+ */
87
+ interface ChannelMetadata {
88
+ /** JetStream stream name (e.g., 'ORDERS') */
89
+ stream: string;
90
+ /** NATS subject (defaults to event type if not specified) */
91
+ subject: string;
92
+ }
93
+ /**
94
+ * Channel configuration input (subject is optional, defaults to type)
95
+ */
96
+ interface ChannelConfig {
97
+ /** JetStream stream name (e.g., 'ORDERS') */
98
+ stream: string;
99
+ /** NATS subject (optional, defaults to event type) */
100
+ subject?: string;
101
+ }
102
+ /**
103
+ * Options for creating event handlers
104
+ *
105
+ * Note: In Zod v4, we use a more relaxed schema constraint to allow
106
+ * contracts defined in external packages to work correctly.
107
+ */
108
+ interface HandleEventOptions<S = ZodTypeAny> {
109
+ schema: S;
110
+ type?: string;
111
+ match?: MatchFn<unknown>;
112
+ safeParse?: boolean;
113
+ /** Filter events by tenant ID(s). If set, only events matching these tenant(s) are processed. */
114
+ tenantId?: string | string[];
115
+ /** Channel metadata for NATS JetStream routing (optional) */
116
+ channel?: ChannelConfig;
117
+ }
118
+ /**
119
+ * Routing configuration for type → subject → stream mapping
120
+ */
121
+ interface RoutingConfig {
122
+ /** Map event type prefix to NATS subject prefix, e.g., { 'orderboss.orders': 'orders' } */
123
+ typeToSubjectMap?: Record<string, string>;
124
+ /** Map event type prefix to stream name, e.g., { 'orderboss.orders': 'ORDERS' } */
125
+ typeToStreamMap?: Record<string, string>;
126
+ /** Default subject prefix when no mapping found */
127
+ defaultSubjectPrefix?: string;
128
+ }
129
+ /**
130
+ * Idempotency store interface for deduplication
131
+ */
132
+ interface IdempotencyStore {
133
+ /** Check if a message has already been processed */
134
+ has(messageId: string): Promise<boolean> | boolean;
135
+ /** Mark a message as processed */
136
+ add(messageId: string, ttlMs?: number): Promise<void> | void;
137
+ /** Clear the store (useful for testing) */
138
+ clear?(): Promise<void> | void;
139
+ }
140
+ /**
141
+ * Type helper to extract data type from a Zod schema
142
+ * Handles both data-only schemas and full CloudEvent schemas with a 'data' field
143
+ */
144
+ type InferEventData<S extends ZodTypeAny> = S['_output'] extends {
145
+ data: infer D;
146
+ } ? D : S['_output'];
147
+
148
+ /**
149
+ * Creates a type-safe event contract with optional channel metadata
150
+ *
151
+ * This helper ensures proper type inference when using contracts
152
+ * with handleEvent across package boundaries.
153
+ *
154
+ * @example
155
+ * Basic contract without channel:
156
+ * ```typescript
157
+ * export const OrderCreatedContract = createContract({
158
+ * type: 'orders.created',
159
+ * schema: z.object({
160
+ * orderId: z.string(),
161
+ * total: z.number(),
162
+ * }),
163
+ * })
164
+ * ```
165
+ *
166
+ * @example
167
+ * Contract with channel metadata:
168
+ * ```typescript
169
+ * export const OrderCreatedContract = createContract({
170
+ * type: 'orders.created',
171
+ * channel: {
172
+ * stream: 'ORDERS',
173
+ * // subject defaults to 'orders.created' if omitted
174
+ * },
175
+ * schema: z.object({
176
+ * orderId: z.string(),
177
+ * total: z.number(),
178
+ * }),
179
+ * })
180
+ * ```
181
+ *
182
+ * @example
183
+ * Contract with explicit subject:
184
+ * ```typescript
185
+ * export const OrderCreatedContract = createContract({
186
+ * type: 'orders.created',
187
+ * channel: {
188
+ * stream: 'ORDERS',
189
+ * subject: 'orders.v1.created', // Override default
190
+ * },
191
+ * schema: OrderSchema,
192
+ * })
193
+ * ```
194
+ */
195
+ declare function createContract<TSchema extends ZodTypeAny>(options: {
196
+ type: string;
197
+ schema: TSchema;
198
+ match?: HandleEventOptions['match'];
199
+ safeParse?: boolean;
200
+ tenantId?: string | string[];
201
+ channel?: {
202
+ stream: string;
203
+ subject?: string;
204
+ };
205
+ }): HandleEventOptions<TSchema> & {
206
+ _schema: TSchema;
207
+ channel?: ChannelMetadata;
208
+ };
209
+
210
+ /**
211
+ * Creates an event handler using the handleEvent pattern
212
+ * Compatible with the new middleware system
213
+ *
214
+ * Supports both inline schemas (Basic Mode) and shared contracts (Advanced Mode).
215
+ *
216
+ * @example Basic Mode - Data-only schema inline
217
+ * ```typescript
218
+ * export default handleEvent({
219
+ * type: 'orderboss.orders.created',
220
+ * schema: z.object({ orderId: z.string(), total: z.number() }),
221
+ * }, async (data) => {
222
+ * console.log('Order created:', data.orderId)
223
+ * })
224
+ * ```
225
+ *
226
+ * @example Advanced Mode - With shared contract
227
+ * ```typescript
228
+ * import { OrderCreatedContract } from '@orderboss/contracts'
229
+ *
230
+ * export default handleEvent(OrderCreatedContract, async (data) => {
231
+ * console.log('Order created:', data.orderId)
232
+ * })
233
+ * ```
234
+ *
235
+ * @example With tenant filtering
236
+ * ```typescript
237
+ * export default handleEvent({
238
+ * type: 'orderboss.orders.created',
239
+ * schema: OrderSchema,
240
+ * tenantId: ['tenant-a', 'tenant-b'],
241
+ * }, async (data, context) => {
242
+ * console.log(`Order for tenant ${context?.tenantId}:`, data)
243
+ * })
244
+ * ```
245
+ */
246
+ declare function handleEvent<TSchema extends ZodTypeAny>(schemaOrOptions: TSchema | HandleEventOptions<TSchema> | HandleEventOptions, handler: (payload: TSchema['_output'], context?: EventContext) => Promise<void> | void, eventType?: string): HandlerConstructor;
247
+ /**
248
+ * Creates an event schema with type inference
249
+ * Automatically enforces the presence of a 'type' field
250
+ */
251
+ declare function eventSchema<T extends Record<string, ZodTypeAny>>(schema: T & {
252
+ type: ZodTypeAny;
253
+ }): z.ZodObject<T & {
254
+ type: ZodTypeAny;
255
+ } extends infer T_1 ? { -readonly [P in keyof T_1]: T_1[P]; } : never, z.core.$strip>;
256
+
257
+ /**
258
+ * Domain Validation
259
+ *
260
+ * Core business logic for validating events, handlers, and schemas
261
+ * Contains no infrastructure concerns - pure domain logic
262
+ */
263
+
264
+ /**
265
+ * Extracts the event type from a Zod schema by looking for a literal 'type' field
266
+ *
267
+ * @param schema - Zod schema to extract type from
268
+ * @returns Event type string or undefined if not found
269
+ */
270
+ declare const extractTypeFromSchema: (schema: ZodTypeAny) => string | undefined;
271
+
272
+ /**
273
+ * Handler Cache Management
274
+ * Immutable cache operations for CloudEvents handlers
275
+ */
276
+
277
+ /**
278
+ * Clears the handler cache. Useful for testing.
279
+ * @internal
280
+ */
281
+ declare const clearHandlerCache: () => void;
282
+
283
+ /**
284
+ * Idempotency utilities for CloudEvents processing
285
+ *
286
+ * Provides deduplication support to ensure handlers process each message exactly once.
287
+ * Uses CloudEvent ID as the deduplication key.
288
+ */
289
+
290
+ /**
291
+ * Options for the in-memory idempotency store
292
+ */
293
+ interface InMemoryIdempotencyStoreOptions {
294
+ /** Maximum number of message IDs to store (LRU eviction). @default 10000 */
295
+ maxSize?: number;
296
+ /** Default TTL for entries in milliseconds. @default 86400000 (24 hours) */
297
+ defaultTtlMs?: number;
298
+ }
299
+ /**
300
+ * Creates an in-memory idempotency store with LRU eviction and TTL support.
301
+ *
302
+ * Suitable for single-instance deployments or development. For production
303
+ * multi-instance deployments, use a Redis-based store.
304
+ *
305
+ * @example
306
+ * ```typescript
307
+ * const store = createInMemoryIdempotencyStore({ maxSize: 5000, defaultTtlMs: 3600000 })
308
+ *
309
+ * await consumeJetStreamEvents({
310
+ * stream: 'ORDERS',
311
+ * subjects: ['orders.>'],
312
+ * consumer: 'notifications',
313
+ * discover: `./src/events/**\/*.handler.ts`,
314
+ * idempotencyStore: store,
315
+ * })
316
+ * ```
317
+ */
318
+ declare function createInMemoryIdempotencyStore(options?: InMemoryIdempotencyStoreOptions): IdempotencyStore;
319
+ /**
320
+ * Gets or creates the default idempotency store
321
+ */
322
+ declare function getDefaultIdempotencyStore(): IdempotencyStore;
323
+ /**
324
+ * Resets the default idempotency store. Useful for testing.
325
+ */
326
+ declare function resetDefaultIdempotencyStore(): void;
327
+ /**
328
+ * Checks if a message should be processed (not a duplicate)
329
+ * and marks it as processed if so.
330
+ *
331
+ * @returns true if the message should be processed, false if it's a duplicate
332
+ */
333
+ declare function checkAndMarkProcessed(store: IdempotencyStore, messageId: string, ttlMs?: number): Promise<boolean>;
334
+
335
+ /**
336
+ * CloudEvents Middleware for Hono - Simplified Core
337
+ *
338
+ * Supports automatic handler discovery and manual registration.
339
+ * Handles event validation, routing, and safeParse mode for graceful degradation.
340
+ * Includes optional DLQ-Safe mode for Pub/Sub Push endpoints.
341
+ */
342
+
343
+ /**
344
+ * Configuration options for CloudEvents middleware
345
+ *
346
+ * @interface CloudEventsOptions
347
+ */
348
+ interface CloudEventsOptions {
349
+ /**
350
+ * Directory path for automatic handler discovery
351
+ *
352
+ * @example './src/events' or './handlers'
353
+ * @default undefined
354
+ */
355
+ discover?: string;
356
+ /**
357
+ * Array of handler constructors for manual registration
358
+ *
359
+ * @example [CustomerCreatedHandler, OrderProcessedHandler]
360
+ * @default []
361
+ */
362
+ handlers?: HandlerConstructor[];
363
+ /**
364
+ * Logging configuration
365
+ *
366
+ * - `false`: No logging (production default)
367
+ * - `true`: Basic structured logging
368
+ * - `'pretty'`: Human-readable format (development)
369
+ * - `'structured'`: JSON format (production)
370
+ *
371
+ * @default false
372
+ */
373
+ log?: boolean | 'pretty' | 'structured';
374
+ /**
375
+ * Google Cloud Pub/Sub topic for quarantining invalid messages
376
+ *
377
+ * When specified, enables DLQ-Safe mode:
378
+ * - Always returns HTTP 204 to prevent redelivery
379
+ * - Quarantines validation errors and malformed events
380
+ * - Non-blocking background processing
381
+ *
382
+ * @example 'projects/my-project/topics/quarantine-topic'
383
+ * @default undefined
384
+ */
385
+ quarantineTopic?: string;
386
+ /**
387
+ * Google Cloud Pub/Sub topic for publishing recoverable handler errors
388
+ *
389
+ * Used in DLQ-Safe mode to publish errors that can be retried later:
390
+ * - Handler execution failures
391
+ * - Temporary processing errors
392
+ * - Network timeouts
393
+ *
394
+ * @example 'projects/my-project/topics/error-topic'
395
+ * @default undefined
396
+ */
397
+ errorTopic?: string;
398
+ /**
399
+ * Google Cloud Project ID
400
+ *
401
+ * @example 'my-production-project'
402
+ * @default Detected from environment (GOOGLE_CLOUD_PROJECT, etc.)
403
+ */
404
+ projectId?: string;
405
+ /**
406
+ * CloudEvent source identifier
407
+ *
408
+ * @example 'my-service' or 'https://my-domain.com/service'
409
+ * @default Auto-detected from environment
410
+ */
411
+ source?: string;
412
+ /**
413
+ * Set for message deduplication based on CloudEvent ID
414
+ *
415
+ * Automatically managed by the middleware to prevent duplicate processing.
416
+ * Can be provided for custom deduplication logic.
417
+ *
418
+ * @default new Set<string>()
419
+ */
420
+ processedMessageIds?: Set<string>;
421
+ }
422
+
423
+ /**
424
+ * CloudEvents middleware for Hono applications
425
+ *
426
+ * Provides automatic CloudEvent processing with handler discovery, validation,
427
+ * and optional DLQ-Safe mode for Google Cloud Pub/Sub Push endpoints.
428
+ *
429
+ * @example
430
+ * ```typescript
431
+ * import { Hono } from 'hono'
432
+ * import { cloudEvents } from '@orderboss/cloudevents'
433
+ *
434
+ * const app = new Hono()
435
+ *
436
+ * // Basic usage with handler discovery
437
+ * app.use('*', cloudEvents({
438
+ * discover: './src/events/*.event.{ts,js}',
439
+ * log: true
440
+ * }))
441
+ *
442
+ * // Manual handler registration
443
+ * app.use('*', cloudEvents({
444
+ * handlers: [CustomerCreatedHandler, OrderProcessedHandler],
445
+ * log: 'pretty'
446
+ * }))
447
+ *
448
+ * // DLQ-Safe mode for Pub/Sub Push
449
+ * app.use('*', cloudEvents({
450
+ * discover: './src/events/*.event.{ts,js}',
451
+ * quarantineTopic: 'quarantine-topic',
452
+ * errorTopic: 'error-topic',
453
+ * projectId: 'my-project',
454
+ * log: 'structured'
455
+ * }))
456
+ * ```
457
+ *
458
+ * @param options - Configuration options for the middleware
459
+ * @param options.discover - Directory path for automatic handler discovery (e.g., './src/events')
460
+ * @param options.handlers - Array of handler constructors for manual registration
461
+ * @param options.log - Enable logging: false (default), true, 'pretty' (dev), or 'structured' (prod)
462
+ * @param options.quarantineTopic - Pub/Sub topic for quarantining invalid messages (enables DLQ-Safe mode)
463
+ * @param options.errorTopic - Pub/Sub topic for publishing recoverable handler errors
464
+ * @param options.projectId - Google Cloud Project ID (defaults to environment detection)
465
+ * @param options.source - CloudEvent source identifier (defaults to auto-detection)
466
+ * @param options.processedMessageIds - Set for message deduplication (automatically managed)
467
+ *
468
+ * @returns Hono middleware function that processes CloudEvents
469
+ *
470
+ * @remarks
471
+ * **Handler Discovery:**
472
+ * - Automatically discovers handlers in the specified directory
473
+ * - Handlers must export a class that extends the base handler pattern
474
+ * - Supports nested directories and TypeScript files
475
+ *
476
+ * **Event Format Support:**
477
+ * - CloudEvents Structured mode (JSON with specversion)
478
+ * - CloudEvents Binary mode (headers + data)
479
+ * - Google Pub/Sub Push format
480
+ * - Raw event data (wrapped in CloudEvent)
481
+ *
482
+ * **DLQ-Safe Mode:**
483
+ * - Activated when `quarantineTopic` is specified
484
+ * - Always returns HTTP 204 to prevent Pub/Sub redelivery
485
+ * - Quarantines invalid messages to specified topic
486
+ * - Publishes handler errors to error topic
487
+ * - All DLQ operations are non-blocking for optimal performance
488
+ *
489
+ * **Standard Mode:**
490
+ * - Returns HTTP 200 for successful processing
491
+ * - Returns HTTP 422 for validation errors
492
+ * - Returns HTTP 500 for handler errors
493
+ * - Allows Pub/Sub DLQ behavior for unhandled errors
494
+ *
495
+ * **Performance Features:**
496
+ * - Handler caching for improved startup time
497
+ * - Dynamic parser loading for optimal bundle size
498
+ * - Message deduplication based on CloudEvent ID
499
+ * - Background processing for DLQ operations
500
+ *
501
+ * @since 1.0.0
502
+ */
503
+ declare function cloudEvents(options?: CloudEventsOptions): (ctx: Context, next: () => Promise<void>) => Promise<void | Response>;
504
+
505
+ interface PublishNatsEventOptions {
506
+ servers?: string;
507
+ source?: string;
508
+ subject?: string;
509
+ tenantId?: string;
510
+ }
511
+ declare const deriveSubjectFromType: (eventType: string, config?: RoutingConfig) => string;
512
+ declare const deriveStreamFromType: (eventType: string, config?: RoutingConfig) => string | undefined;
513
+ declare const publishNatsRawEvent: (subjectName: string, eventType: string, eventData: unknown, options?: PublishNatsEventOptions) => Promise<string>;
514
+ declare const publishNatsEvent: <T extends ZodTypeAny>(subjectName: string, schema: T, eventData: unknown, options?: PublishNatsEventOptions) => Promise<string>;
515
+ declare const publish: (eventTypeOrContract: string | {
516
+ type: string;
517
+ channel?: {
518
+ subject?: string;
519
+ };
520
+ }, eventData: unknown, options?: PublishNatsEventOptions) => Promise<string>;
521
+ declare const __resetNatsPublisher: () => void;
522
+
523
+ interface PublishEventOptions {
524
+ projectId?: string;
525
+ keyFilename?: string;
526
+ source?: string;
527
+ subject?: string;
528
+ attributes?: Record<string, string>;
529
+ }
530
+ /**
531
+ * Publishes an event using a Zod schema for validation and type extraction.
532
+ * Automatically extracts the event type from the schema and creates a CloudEvent.
533
+ *
534
+ * @param topicName - PubSub topic name
535
+ * @param schema - Zod schema that defines the event structure and type
536
+ * @param eventData - Event data that must match the schema
537
+ * @param options - Optional PubSub configuration
538
+ * @returns Promise resolving to the published message ID
539
+ *
540
+ * @example
541
+ * ```typescript
542
+ * await publishEvent(
543
+ * 'customer-events',
544
+ * CustomerCreatedSchema,
545
+ * { customer: { id: '123', email: 'test@example.com' } }
546
+ * )
547
+ * ```
548
+ */
549
+ declare function publishEvent<T extends ZodTypeAny>(topicName: string, schema: T, eventData: unknown, options?: PublishEventOptions): Promise<string>;
550
+ /**
551
+ * Raw event publisher - bypasses schema validation.
552
+ * Use this when you need direct control over the event type and data.
553
+ *
554
+ * @param topicName - PubSub topic name
555
+ * @param eventType - Manual event type identifier
556
+ * @param eventData - Raw event data
557
+ * @param options - Optional PubSub configuration
558
+ * @returns Promise resolving to the published message ID
559
+ */
560
+ declare function publishRawEvent(topicName: string, eventType: string, eventData: unknown, options?: PublishEventOptions): Promise<string>;
561
+
562
+ /**
563
+ * Stream configuration options
564
+ */
565
+ interface StreamConfig {
566
+ /** Maximum age of messages in the stream (ms). @default 7 days */
567
+ maxAge?: number;
568
+ /** Maximum size of the stream in bytes. @default 1GB */
569
+ maxBytes?: number;
570
+ /** Number of replicas. @default 1 */
571
+ replicas?: number;
572
+ }
573
+ /**
574
+ * Options for creating/ensuring a JetStream stream exists
575
+ */
576
+ interface JetStreamStreamOptions {
577
+ /** NATS server URL. Defaults to NATS_URL env or nats://localhost:4222 */
578
+ servers?: string;
579
+ /** NATS username for authentication (defaults to NATS_USER env var) */
580
+ user?: string;
581
+ /** NATS password for authentication (defaults to NATS_PASSWORD env var) */
582
+ pass?: string;
583
+ /** JetStream stream name */
584
+ stream: string;
585
+ /** Subjects to bind to the stream (e.g., ['orders.>', 'payments.>']) */
586
+ subjects: string[];
587
+ /** Stream configuration */
588
+ config?: StreamConfig;
589
+ }
590
+ /**
591
+ * Stream definition for batch operations
592
+ */
593
+ interface StreamDefinition {
594
+ /** JetStream stream name (e.g., 'ORDERS') */
595
+ stream: string;
596
+ /** Subjects to bind (e.g., ['orders.*']) */
597
+ subjects: string[];
598
+ /** Optional stream-specific config */
599
+ config?: StreamConfig;
600
+ }
601
+ /**
602
+ * Options for ensuring multiple JetStream streams exist
603
+ */
604
+ interface JetStreamStreamsOptions {
605
+ /** NATS server URL. Defaults to NATS_URL env or nats://localhost:4222 */
606
+ servers?: string;
607
+ /** NATS username for authentication (defaults to NATS_USER env var) */
608
+ user?: string;
609
+ /** NATS password for authentication (defaults to NATS_PASSWORD env var) */
610
+ pass?: string;
611
+ /** Array of stream definitions */
612
+ streams: StreamDefinition[];
613
+ }
614
+ /**
615
+ * JetStream consumer configuration
616
+ */
617
+ interface JetStreamConsumerOptions extends Pick<CloudEventsOptions, 'quarantineTopic' | 'errorTopic' | 'projectId' | 'source'> {
618
+ /** NATS server URL. Defaults to NATS_URL env or nats://localhost:4222 */
619
+ servers?: string;
620
+ /** NATS username for authentication (defaults to NATS_USER env var) */
621
+ user?: string;
622
+ /** NATS password for authentication (defaults to NATS_PASSWORD env var) */
623
+ pass?: string;
624
+ /** JetStream stream name (must already exist or use ensureJetStreamStream first) */
625
+ stream: string;
626
+ /** Durable consumer name. Required for persistence across restarts */
627
+ consumer: string;
628
+ /**
629
+ * Optional filter subjects for this consumer.
630
+ * If specified, consumer only receives messages matching these subjects.
631
+ * If omitted, consumer receives all messages from the stream.
632
+ * @example ['orders.created', 'orders.updated']
633
+ */
634
+ filterSubjects?: string[];
635
+ /** Glob pattern to discover event handlers */
636
+ discover: string;
637
+ /**
638
+ * Where to start consuming from on first subscription.
639
+ * @default 'new' - Only new messages
640
+ * Options: 'all' | 'new' | 'last' | Date
641
+ */
642
+ startFrom?: 'all' | 'new' | 'last' | Date;
643
+ /**
644
+ * Max number of messages to buffer for processing
645
+ * @default 100
646
+ */
647
+ maxMessages?: number;
648
+ /**
649
+ * Ack wait timeout in milliseconds before message is redelivered
650
+ * @default 30000 (30 seconds)
651
+ */
652
+ ackWait?: number;
653
+ /**
654
+ * Max redelivery attempts before message goes to DLQ
655
+ * @default 3
656
+ */
657
+ maxDeliver?: number;
658
+ /**
659
+ * Idempotency store for deduplication. Defaults to in-memory store.
660
+ * Pass `false` to disable idempotency checks entirely.
661
+ */
662
+ idempotencyStore?: IdempotencyStore | false;
663
+ /**
664
+ * TTL for idempotency records in milliseconds.
665
+ * @default 86400000 (24 hours)
666
+ */
667
+ idempotencyTtl?: number;
668
+ }
669
+ /**
670
+ * Ensures a JetStream stream exists with the given configuration.
671
+ * This is typically called once during application startup or in infrastructure setup.
672
+ *
673
+ * @example
674
+ * ```typescript
675
+ * // In infrastructure/setup code:
676
+ * await ensureJetStreamStream({
677
+ * stream: 'ORDERS',
678
+ * subjects: ['orders.>'],
679
+ * config: {
680
+ * maxAge: 7 * 24 * 60 * 60 * 1000, // 7 days
681
+ * replicas: 3
682
+ * }
683
+ * })
684
+ * ```
685
+ */
686
+ declare function ensureJetStreamStream(options: JetStreamStreamOptions): Promise<void>;
687
+ /**
688
+ * Ensures multiple JetStream streams exist with a single NATS connection.
689
+ * More efficient than calling ensureJetStreamStream multiple times.
690
+ *
691
+ * @example
692
+ * ```typescript
693
+ * await ensureJetStreamStreams({
694
+ * streams: [
695
+ * { stream: 'ORDERS', subjects: ['orders.*'] },
696
+ * { stream: 'CUSTOMERS', subjects: ['customers.*'] },
697
+ * ]
698
+ * })
699
+ * ```
700
+ */
701
+ declare function ensureJetStreamStreams(options: JetStreamStreamsOptions): Promise<void>;
702
+ /**
703
+ * Consume CloudEvents from NATS JetStream with persistence and guaranteed delivery.
704
+ *
705
+ * Features:
706
+ * - Automatic stream and consumer creation
707
+ * - Durable subscriptions (survive restarts)
708
+ * - Automatic acknowledgments on successful processing
709
+ * - Configurable retry with max redelivery
710
+ * - Dead letter queue support
711
+ *
712
+ * @example
713
+ * ```typescript
714
+ * await consumeJetStreamEvents({
715
+ * stream: 'ORDERS',
716
+ * subjects: ['orders.>'],
717
+ * consumer: 'notifications',
718
+ * discover: `./src/events/**\/*.handler.ts`,
719
+ * })
720
+ * ```
721
+ */
722
+ declare function consumeJetStreamEvents(options: JetStreamConsumerOptions): Promise<ConsumerMessages>;
723
+ /**
724
+ * Options for consuming from multiple JetStream streams
725
+ */
726
+ interface JetStreamStreamsConsumerOptions extends Pick<CloudEventsOptions, 'quarantineTopic' | 'errorTopic' | 'projectId' | 'source'> {
727
+ /** NATS server URL. Defaults to NATS_URL env or nats://localhost:4222 */
728
+ servers?: string;
729
+ /** NATS username for authentication (defaults to NATS_USER env var) */
730
+ user?: string;
731
+ /** NATS password for authentication (defaults to NATS_PASSWORD env var) */
732
+ pass?: string;
733
+ /** Durable consumer name. Required for persistence across restarts */
734
+ consumer: string;
735
+ /** Array of stream names to consume from */
736
+ streams: string[];
737
+ /** Glob pattern to discover event handlers */
738
+ discover: string;
739
+ /** Where to start consuming from on first subscription. @default 'new' */
740
+ startFrom?: 'all' | 'new' | 'last' | Date;
741
+ /** Max number of messages to buffer for processing @default 100 */
742
+ maxMessages?: number;
743
+ /** Ack wait timeout in milliseconds @default 30000 */
744
+ ackWait?: number;
745
+ /** Max redelivery attempts @default 3 */
746
+ maxDeliver?: number;
747
+ /** Idempotency store for deduplication */
748
+ idempotencyStore?: IdempotencyStore | false;
749
+ /** TTL for idempotency records in milliseconds @default 86400000 */
750
+ idempotencyTtl?: number;
751
+ }
752
+ /**
753
+ * Consume CloudEvents from multiple JetStream streams with a single connection.
754
+ * More efficient than calling consumeJetStreamEvents multiple times.
755
+ *
756
+ * @example
757
+ * ```typescript
758
+ * await consumeJetStreamStreams({
759
+ * streams: ['ORDERS', 'CUSTOMERS'],
760
+ * consumer: 'notifications',
761
+ * discover: `./src/events/**\/*.handler.ts`,
762
+ * })
763
+ * ```
764
+ */
765
+ declare function consumeJetStreamStreams(options: JetStreamStreamsConsumerOptions): Promise<ConsumerMessages[]>;
766
+ /**
767
+ * Alias for ensureJetStreamStreams - shorter name
768
+ * @see ensureJetStreamStreams
769
+ */
770
+ declare const ensureJetStreams: typeof ensureJetStreamStreams;
771
+ /**
772
+ * Alias for consumeJetStreamStreams - shorter name
773
+ * @see consumeJetStreamStreams
774
+ */
775
+ declare const consumeJetStreams: typeof consumeJetStreamStreams;
776
+
777
+ /**
778
+ * Describes the configuration required to bootstrap the NATS event consumer.
779
+ *
780
+ * @property servers - Optional NATS connection string; defaults to `NATS_URL` or the local instance.
781
+ * @property subject - NATS subject to subscribe to for incoming events.
782
+ * @property discover - Glob pattern or directory used to discover event handler classes.
783
+ * @property consumerName - Optional identifier appended to log output and the consumer name.
784
+ * @property user - Optional NATS username for authentication (can also use `NATS_USER` env var).
785
+ * @property pass - Optional NATS password for authentication (can also use `NATS_PASSWORD` env var).
786
+ * @property quarantineTopic - Optional Pub/Sub topic for quarantining malformed messages when DLQ mode is enabled.
787
+ * @property errorTopic - Optional Pub/Sub topic for recovering handler errors when DLQ mode is enabled.
788
+ * @property projectId - Optional Google Cloud project identifier used for DLQ publishing.
789
+ * @property source - Optional CloudEvent source identifier applied to DLQ messages.
790
+ */
791
+ interface NatsConsumerOptions extends Pick<CloudEventsOptions, 'quarantineTopic' | 'errorTopic' | 'projectId' | 'source'> {
792
+ servers?: string;
793
+ subject: string;
794
+ discover: string;
795
+ consumerName?: string;
796
+ /** NATS username for authentication (defaults to NATS_USER env var) */
797
+ user?: string;
798
+ /** NATS password for authentication (defaults to NATS_PASSWORD env var) */
799
+ pass?: string;
800
+ }
801
+ /**
802
+ * Connects to NATS, discovers matching event handlers, and processes incoming CloudEvents.
803
+ *
804
+ * @param options - Consumer configuration describing connection, discovery, and subscription details.
805
+ * @returns The active NATS subscription for the configured subject.
806
+ *
807
+ * When `quarantineTopic` or `errorTopic` is provided, the consumer forwards malformed messages
808
+ * and handler failures to the configured DLQ topics instead of throwing errors.
809
+ */
810
+ declare function consumeNatsEvents(options: NatsConsumerOptions): Promise<Subscription>;
811
+
812
+ export { type ChannelConfig, type ChannelMetadata, type EnrichedEvent, type EventContext, type HandleEventOptions, type IdempotencyStore, type InferEventData, type JetStreamConsumerOptions, type JetStreamStreamOptions, type JetStreamStreamsConsumerOptions, type JetStreamStreamsOptions, type PublishEventOptions, type PublishNatsEventOptions, type RoutingConfig, type StreamConfig, type StreamDefinition, __resetNatsPublisher, checkAndMarkProcessed, clearHandlerCache, cloudEvents, consumeJetStreamEvents, consumeJetStreamStreams, consumeJetStreams, consumeNatsEvents, createContract, createInMemoryIdempotencyStore, deriveStreamFromType, deriveSubjectFromType, ensureJetStreamStream, ensureJetStreamStreams, ensureJetStreams, eventSchema, extractTypeFromSchema, getDefaultIdempotencyStore, handleEvent, parseEventFromContext, publish, publishEvent, publishNatsEvent, publishNatsRawEvent, publishRawEvent, resetDefaultIdempotencyStore };