@nimbus-cqrs/core 2.0.0-beta.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 (79) hide show
  1. package/LICENSE +85 -0
  2. package/README.md +56 -0
  3. package/esm/_dnt.shims.d.ts +2 -0
  4. package/esm/_dnt.shims.d.ts.map +1 -0
  5. package/esm/_dnt.shims.js +57 -0
  6. package/esm/deps/jsr.io/@std/fmt/1.0.10/colors.d.ts +700 -0
  7. package/esm/deps/jsr.io/@std/fmt/1.0.10/colors.d.ts.map +1 -0
  8. package/esm/deps/jsr.io/@std/fmt/1.0.10/colors.js +903 -0
  9. package/esm/deps/jsr.io/@std/ulid/1.0.0/_util.d.ts +14 -0
  10. package/esm/deps/jsr.io/@std/ulid/1.0.0/_util.d.ts.map +1 -0
  11. package/esm/deps/jsr.io/@std/ulid/1.0.0/_util.js +65 -0
  12. package/esm/deps/jsr.io/@std/ulid/1.0.0/decode_time.d.ts +20 -0
  13. package/esm/deps/jsr.io/@std/ulid/1.0.0/decode_time.d.ts.map +1 -0
  14. package/esm/deps/jsr.io/@std/ulid/1.0.0/decode_time.js +43 -0
  15. package/esm/deps/jsr.io/@std/ulid/1.0.0/mod.d.ts +44 -0
  16. package/esm/deps/jsr.io/@std/ulid/1.0.0/mod.d.ts.map +1 -0
  17. package/esm/deps/jsr.io/@std/ulid/1.0.0/mod.js +47 -0
  18. package/esm/deps/jsr.io/@std/ulid/1.0.0/monotonic_ulid.d.ts +44 -0
  19. package/esm/deps/jsr.io/@std/ulid/1.0.0/monotonic_ulid.d.ts.map +1 -0
  20. package/esm/deps/jsr.io/@std/ulid/1.0.0/monotonic_ulid.js +51 -0
  21. package/esm/deps/jsr.io/@std/ulid/1.0.0/ulid.d.ts +31 -0
  22. package/esm/deps/jsr.io/@std/ulid/1.0.0/ulid.d.ts.map +1 -0
  23. package/esm/deps/jsr.io/@std/ulid/1.0.0/ulid.js +37 -0
  24. package/esm/index.d.ts +18 -0
  25. package/esm/index.d.ts.map +1 -0
  26. package/esm/index.js +17 -0
  27. package/esm/lib/eventBus/eventBus.d.ts +329 -0
  28. package/esm/lib/eventBus/eventBus.d.ts.map +1 -0
  29. package/esm/lib/eventBus/eventBus.js +473 -0
  30. package/esm/lib/exception/exception.d.ts +10 -0
  31. package/esm/lib/exception/exception.d.ts.map +1 -0
  32. package/esm/lib/exception/exception.js +26 -0
  33. package/esm/lib/exception/forbiddenException.d.ts +8 -0
  34. package/esm/lib/exception/forbiddenException.d.ts.map +1 -0
  35. package/esm/lib/exception/forbiddenException.js +9 -0
  36. package/esm/lib/exception/genericException.d.ts +8 -0
  37. package/esm/lib/exception/genericException.d.ts.map +1 -0
  38. package/esm/lib/exception/genericException.js +9 -0
  39. package/esm/lib/exception/invalidInputException.d.ts +18 -0
  40. package/esm/lib/exception/invalidInputException.d.ts.map +1 -0
  41. package/esm/lib/exception/invalidInputException.js +28 -0
  42. package/esm/lib/exception/notFoundException.d.ts +8 -0
  43. package/esm/lib/exception/notFoundException.d.ts.map +1 -0
  44. package/esm/lib/exception/notFoundException.js +9 -0
  45. package/esm/lib/exception/unauthorizedException.d.ts +8 -0
  46. package/esm/lib/exception/unauthorizedException.d.ts.map +1 -0
  47. package/esm/lib/exception/unauthorizedException.js +9 -0
  48. package/esm/lib/log/logFormatter.d.ts +26 -0
  49. package/esm/lib/log/logFormatter.d.ts.map +1 -0
  50. package/esm/lib/log/logFormatter.js +59 -0
  51. package/esm/lib/log/logLevel.d.ts +24 -0
  52. package/esm/lib/log/logLevel.d.ts.map +1 -0
  53. package/esm/lib/log/logLevel.js +27 -0
  54. package/esm/lib/log/logger.d.ts +377 -0
  55. package/esm/lib/log/logger.d.ts.map +1 -0
  56. package/esm/lib/log/logger.js +394 -0
  57. package/esm/lib/log/options.d.ts +34 -0
  58. package/esm/lib/log/options.d.ts.map +1 -0
  59. package/esm/lib/log/options.js +9 -0
  60. package/esm/lib/message/command.d.ts +100 -0
  61. package/esm/lib/message/command.d.ts.map +1 -0
  62. package/esm/lib/message/command.js +47 -0
  63. package/esm/lib/message/event.d.ts +103 -0
  64. package/esm/lib/message/event.d.ts.map +1 -0
  65. package/esm/lib/message/event.js +47 -0
  66. package/esm/lib/message/message.d.ts +18 -0
  67. package/esm/lib/message/message.d.ts.map +1 -0
  68. package/esm/lib/message/message.js +1 -0
  69. package/esm/lib/message/query.d.ts +95 -0
  70. package/esm/lib/message/query.d.ts.map +1 -0
  71. package/esm/lib/message/query.js +45 -0
  72. package/esm/lib/message/router.d.ts +233 -0
  73. package/esm/lib/message/router.d.ts.map +1 -0
  74. package/esm/lib/message/router.js +304 -0
  75. package/esm/lib/tracing/withSpan.d.ts +67 -0
  76. package/esm/lib/tracing/withSpan.d.ts.map +1 -0
  77. package/esm/lib/tracing/withSpan.js +87 -0
  78. package/esm/package.json +3 -0
  79. package/package.json +33 -0
@@ -0,0 +1,329 @@
1
+ import type { Event } from '../message/event.js';
2
+ /**
3
+ * The type for the NimbusEventBus options.
4
+ */
5
+ export type NimbusEventBusOptions = {
6
+ /**
7
+ * The name of the event bus instance for metrics and traces.
8
+ * Defaults to 'default'.
9
+ */
10
+ name?: string;
11
+ /**
12
+ * The maximum number of retries for handling the event in case of an error.
13
+ * Defaults to 2.
14
+ */
15
+ maxRetries?: number;
16
+ /**
17
+ * The base delay for exponential backoff in milliseconds.
18
+ * Defaults to 1000ms.
19
+ */
20
+ baseDelay?: number;
21
+ /**
22
+ * The maximum delay cap for exponential backoff in milliseconds.
23
+ * Defaults to 30000ms (30 seconds).
24
+ */
25
+ maxDelay?: number;
26
+ /**
27
+ * Whether to add jitter to the retry delay to prevent thundering herd issues.
28
+ * Defaults to true.
29
+ */
30
+ useJitter?: boolean;
31
+ /**
32
+ * Optional callback invoked when an event is published.
33
+ * Useful for custom logging or debugging.
34
+ */
35
+ logPublish?: (event: Event) => void;
36
+ };
37
+ /**
38
+ * The input type for subscribing to an event.
39
+ */
40
+ export type SubscribeEventInput<TEvent extends Event> = {
41
+ /**
42
+ * The CloudEvents event type to subscribe to (e.g., 'at.overlap.nimbus.order-created').
43
+ */
44
+ type: string;
45
+ /**
46
+ * The async handler function that processes received events.
47
+ */
48
+ handler: (event: TEvent) => Promise<void>;
49
+ /**
50
+ * Optional error callback invoked when event handling fails after all retries.
51
+ * If not provided, errors are logged using the default logger.
52
+ */
53
+ onError?: (error: Error, event: TEvent) => void;
54
+ /**
55
+ * Optional retry options that override the EventBus defaults for this subscription.
56
+ */
57
+ options?: Omit<NimbusEventBusOptions, 'name'>;
58
+ };
59
+ /**
60
+ * The NimbusEventBus is used to publish and subscribe to events within the application.
61
+ *
62
+ * Events are delivered asynchronously to all registered handlers. If a handler fails,
63
+ * it will be retried using exponential backoff until it succeeds or the maximum retry
64
+ * count is reached.
65
+ *
66
+ * All operations are instrumented with OpenTelemetry tracing and metrics for observability.
67
+ *
68
+ * @example
69
+ * ```ts
70
+ * import { createEvent, NimbusEventBus } from '@nimbus/core';
71
+ *
72
+ * const eventBus = new NimbusEventBus({
73
+ * name: 'orders',
74
+ * maxRetries: 3,
75
+ * baseDelay: 1000,
76
+ * maxDelay: 30000,
77
+ * useJitter: true,
78
+ * logPublish: (event) => {
79
+ * console.log('Event published:', event.type, event.correlationid);
80
+ * },
81
+ * });
82
+ *
83
+ * // Subscribe to events
84
+ * eventBus.subscribeEvent({
85
+ * type: 'at.overlap.nimbus.order-created',
86
+ * handler: async (event) => {
87
+ * console.log('Order created:', event.data);
88
+ * },
89
+ * onError: (error, event) => {
90
+ * console.error('Failed to handle event:', event.id, error.message);
91
+ * },
92
+ * options: {
93
+ * maxRetries: 5,
94
+ * baseDelay: 500,
95
+ * maxDelay: 15000,
96
+ * useJitter: true,
97
+ * },
98
+ * });
99
+ *
100
+ * // Publish an event
101
+ * const event = createEvent({
102
+ * type: 'at.overlap.nimbus.order-created',
103
+ * source: 'https://api.example.com',
104
+ * correlationid: '550e8400-e29b-41d4-a716-446655440000',
105
+ * subject: '/orders/12345',
106
+ * data: { orderId: '12345', customerId: '67890' },
107
+ * datacontenttype: 'application/json',
108
+ * });
109
+ *
110
+ * eventBus.putEvent(event);
111
+ * ```
112
+ */
113
+ export declare class NimbusEventBus {
114
+ private readonly _eventEmitter;
115
+ private readonly _name;
116
+ private readonly _maxRetries;
117
+ private readonly _baseDelay;
118
+ private readonly _maxDelay;
119
+ private readonly _useJitter;
120
+ private readonly _logPublish?;
121
+ /**
122
+ * Create a new NimbusEventBus instance.
123
+ *
124
+ * @param options - The options for the event bus.
125
+ * @param options.name - The name of the event bus instance for metrics and traces. Defaults to 'default'.
126
+ * @param options.maxRetries - The maximum number of retries for handling the event in case of an error. Defaults to 2.
127
+ * @param options.baseDelay - The base delay for exponential backoff in milliseconds. Defaults to 1000ms.
128
+ * @param options.maxDelay - The maximum delay cap for exponential backoff in milliseconds. Defaults to 30000ms.
129
+ * @param options.useJitter - Whether to add jitter to the retry delay. Defaults to true.
130
+ * @param options.logPublish - Optional callback invoked when an event is published.
131
+ *
132
+ * @example
133
+ * ```ts
134
+ * import { getLogger, NimbusEventBus } from '@nimbus/core';
135
+ *
136
+ * const eventBus = new NimbusEventBus({
137
+ * name: 'orders',
138
+ * maxRetries: 3,
139
+ * baseDelay: 1000,
140
+ * maxDelay: 30000,
141
+ * useJitter: true,
142
+ * logPublish: (event) => {
143
+ * getLogger().debug({
144
+ * category: 'EventBus',
145
+ * message: 'Published event',
146
+ * data: { type: event.type, id: event.id },
147
+ * correlationId: event.correlationid,
148
+ * });
149
+ * },
150
+ * });
151
+ * ```
152
+ */
153
+ constructor(options?: NimbusEventBusOptions);
154
+ /**
155
+ * Publish an event to the event bus.
156
+ *
157
+ * The event is validated against the CloudEvents 64KB size limit before publishing.
158
+ * All subscribers registered for this event type will receive the event asynchronously.
159
+ *
160
+ * @param event - The CloudEvents-compliant event to publish.
161
+ * @throws {GenericException} If the event size exceeds 64KB.
162
+ *
163
+ * @example
164
+ * ```ts
165
+ * import { createEvent, getEventBus } from '@nimbus/core';
166
+ *
167
+ * const eventBus = getEventBus('default');
168
+ *
169
+ * // Create and publish an event with all CloudEvents properties
170
+ * const event = createEvent({
171
+ * type: 'at.overlap.nimbus.order-created',
172
+ * source: 'https://api.example.com',
173
+ * correlationid: '550e8400-e29b-41d4-a716-446655440000',
174
+ * subject: '/orders/12345',
175
+ * data: {
176
+ * orderId: '12345',
177
+ * customerId: '67890',
178
+ * items: ['item-1', 'item-2'],
179
+ * total: 99.99,
180
+ * },
181
+ * datacontenttype: 'application/json',
182
+ * dataschema: 'https://schemas.example.com/order-created.json',
183
+ * });
184
+ *
185
+ * eventBus.putEvent(event);
186
+ * ```
187
+ */
188
+ putEvent<TEvent extends Event>(event: TEvent): void;
189
+ /**
190
+ * Subscribe to an event type with a handler function.
191
+ *
192
+ * When an event matching the specified type is published, the handler is invoked.
193
+ * If the handler throws an error, it will be retried using exponential backoff
194
+ * (delay doubles with each attempt) until either it succeeds or the maximum retry
195
+ * count is reached.
196
+ *
197
+ * @param input - The subscription configuration.
198
+ * @param input.type - The CloudEvents event type to subscribe to.
199
+ * @param input.handler - The async handler function to process events.
200
+ * @param input.onError - Optional callback invoked when all retries are exhausted.
201
+ * @param input.options - Optional retry options to override EventBus defaults.
202
+ * @param input.options.maxRetries - Override maximum retry attempts for this subscription.
203
+ * @param input.options.baseDelay - Override base delay in milliseconds for this subscription.
204
+ * @param input.options.maxDelay - Override maximum delay cap in milliseconds for this subscription.
205
+ * @param input.options.useJitter - Override jitter setting for this subscription.
206
+ *
207
+ * @example
208
+ * ```ts
209
+ * import { getEventBus, getLogger } from '@nimbus/core';
210
+ *
211
+ * const eventBus = getEventBus('default');
212
+ *
213
+ * // Subscribe with all available options
214
+ * eventBus.subscribeEvent({
215
+ * type: 'at.overlap.nimbus.order-created',
216
+ * handler: async (event) => {
217
+ * // Process the event
218
+ * console.log('Order created:', event.data.orderId);
219
+ * console.log('Correlation ID:', event.correlationid);
220
+ * },
221
+ * onError: (error, event) => {
222
+ * getLogger().error({
223
+ * category: 'OrderHandler',
224
+ * message: 'Failed to process order after all retries',
225
+ * data: { eventId: event.id, orderId: event.data.orderId },
226
+ * error,
227
+ * correlationId: event.correlationid,
228
+ * });
229
+ * },
230
+ * options: {
231
+ * maxRetries: 5,
232
+ * baseDelay: 500,
233
+ * maxDelay: 15000,
234
+ * useJitter: true,
235
+ * },
236
+ * });
237
+ * ```
238
+ */
239
+ subscribeEvent<TEvent extends Event>({ type, handler, onError, options, }: SubscribeEventInput<TEvent>): void;
240
+ private _processEvent;
241
+ private _recordDeliveryMetrics;
242
+ private _handleFinalFailure;
243
+ private _calculateRetryDelay;
244
+ /**
245
+ * Validate the size of the event and return the size in bytes.
246
+ *
247
+ * To comply with the CloudEvent spec a transmitted event
248
+ * can only have a maximum size of 64KB.
249
+ *
250
+ * @param event - The event to validate.
251
+ * @returns The size of the event in bytes.
252
+ */
253
+ private _validateEventSize;
254
+ }
255
+ /**
256
+ * Setup a named EventBus instance and register it for later retrieval.
257
+ *
258
+ * Use this function to configure an EventBus with specific options at application
259
+ * startup, then retrieve it later using {@link getEventBus}.
260
+ *
261
+ * @param name - The unique name for this EventBus instance.
262
+ * @param options - Optional configuration options for the EventBus.
263
+ * @param options.maxRetries - The maximum number of retries for handling events. Defaults to 2.
264
+ * @param options.baseDelay - The base delay for exponential backoff in milliseconds. Defaults to 1000ms.
265
+ * @param options.maxDelay - The maximum delay cap for exponential backoff in milliseconds. Defaults to 30000ms.
266
+ * @param options.useJitter - Whether to add jitter to the retry delay. Defaults to true.
267
+ * @param options.logPublish - Optional callback invoked when an event is published.
268
+ *
269
+ * @example
270
+ * ```ts
271
+ * import { getLogger, setupEventBus } from '@nimbus/core';
272
+ *
273
+ * // At application startup, configure the event bus with all options
274
+ * setupEventBus('default', {
275
+ * maxRetries: 3,
276
+ * baseDelay: 1000,
277
+ * maxDelay: 30000,
278
+ * useJitter: true,
279
+ * logPublish: (event) => {
280
+ * getLogger().debug({
281
+ * category: 'EventBus',
282
+ * message: 'Published event',
283
+ * data: { type: event.type, id: event.id },
284
+ * correlationId: event.correlationid,
285
+ * });
286
+ * },
287
+ * });
288
+ * ```
289
+ */
290
+ export declare const setupEventBus: (name: string, options?: Omit<NimbusEventBusOptions, "name">) => void;
291
+ /**
292
+ * Get a named EventBus instance.
293
+ *
294
+ * If an EventBus with the given name has been configured via {@link setupEventBus},
295
+ * that instance is returned. Otherwise, a new EventBus with default options is created
296
+ * and registered.
297
+ *
298
+ * @param name - The name of the EventBus instance to retrieve. Defaults to 'default'.
299
+ * @returns The NimbusEventBus instance.
300
+ *
301
+ * @example
302
+ * ```ts
303
+ * import { createEvent, getEventBus } from '@nimbus/core';
304
+ *
305
+ * // Get the event bus configured earlier with setupEventBus
306
+ * const eventBus = getEventBus('default');
307
+ *
308
+ * // Subscribe to events
309
+ * eventBus.subscribeEvent({
310
+ * type: 'at.overlap.nimbus.order-created',
311
+ * handler: async (event) => {
312
+ * console.log('Order created:', event.data.orderId);
313
+ * },
314
+ * });
315
+ *
316
+ * // Publish an event
317
+ * const event = createEvent({
318
+ * type: 'at.overlap.nimbus.order-created',
319
+ * source: 'https://api.example.com',
320
+ * correlationid: '550e8400-e29b-41d4-a716-446655440000',
321
+ * data: { orderId: '12345', customerId: '67890' },
322
+ * datacontenttype: 'application/json',
323
+ * });
324
+ *
325
+ * eventBus.putEvent(event);
326
+ * ```
327
+ */
328
+ export declare const getEventBus: (name?: string) => NimbusEventBus;
329
+ //# sourceMappingURL=eventBus.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"eventBus.d.ts","sourceRoot":"","sources":["../../../src/lib/eventBus/eventBus.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AA0CjD;;GAEG;AACH,MAAM,MAAM,qBAAqB,GAAG;IAChC;;;OAGG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC;IACd;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,SAAS,CA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