@zdavison/matador 2.0.8 → 2.0.10

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 (77) hide show
  1. package/dist/index.d.cts +1 -1
  2. package/dist/index.d.ts +1 -1
  3. package/dist/index.d.ts.map +1 -1
  4. package/package.json +6 -2
  5. package/examples/config.ts +0 -126
  6. package/examples/event.ts +0 -26
  7. package/examples/order-event.json +0 -19
  8. package/src/checkpoint/context.test.ts +0 -510
  9. package/src/checkpoint/context.ts +0 -213
  10. package/src/checkpoint/index.ts +0 -30
  11. package/src/checkpoint/stores/memory.ts +0 -47
  12. package/src/checkpoint/stores/noop.ts +0 -22
  13. package/src/checkpoint/stores/stores.test.ts +0 -177
  14. package/src/checkpoint/types.ts +0 -147
  15. package/src/codec/codec.ts +0 -42
  16. package/src/codec/header-aware-codec.ts +0 -41
  17. package/src/codec/index.ts +0 -11
  18. package/src/codec/json-codec.ts +0 -69
  19. package/src/codec/rabbitmq-codec.test.ts +0 -516
  20. package/src/codec/rabbitmq-codec.ts +0 -336
  21. package/src/core/fanout.test.ts +0 -1350
  22. package/src/core/fanout.ts +0 -184
  23. package/src/core/index.ts +0 -12
  24. package/src/core/matador.test.ts +0 -575
  25. package/src/core/matador.ts +0 -357
  26. package/src/core/shutdown.test.ts +0 -853
  27. package/src/core/shutdown.ts +0 -165
  28. package/src/errors/checkpoint-errors.ts +0 -62
  29. package/src/errors/has-description.ts +0 -25
  30. package/src/errors/index.ts +0 -58
  31. package/src/errors/matador-errors.ts +0 -477
  32. package/src/errors/retry-errors.test.ts +0 -175
  33. package/src/errors/retry-errors.ts +0 -188
  34. package/src/hooks/index.ts +0 -14
  35. package/src/hooks/safe-hooks.ts +0 -200
  36. package/src/hooks/types.ts +0 -226
  37. package/src/index.ts +0 -241
  38. package/src/pipeline/index.ts +0 -2
  39. package/src/pipeline/pipeline.test.ts +0 -1377
  40. package/src/pipeline/pipeline.ts +0 -393
  41. package/src/retry/index.ts +0 -4
  42. package/src/retry/policy.ts +0 -46
  43. package/src/retry/standard-policy.test.ts +0 -290
  44. package/src/retry/standard-policy.ts +0 -156
  45. package/src/schema/index.ts +0 -16
  46. package/src/schema/registry.test.ts +0 -339
  47. package/src/schema/registry.ts +0 -229
  48. package/src/schema/types.test.ts +0 -280
  49. package/src/schema/types.ts +0 -217
  50. package/src/topology/builder.test.ts +0 -451
  51. package/src/topology/builder.ts +0 -238
  52. package/src/topology/index.ts +0 -19
  53. package/src/topology/types.ts +0 -183
  54. package/src/transport/capabilities.ts +0 -88
  55. package/src/transport/connection-manager.ts +0 -218
  56. package/src/transport/index.ts +0 -42
  57. package/src/transport/local/local-transport.test.ts +0 -262
  58. package/src/transport/local/local-transport.ts +0 -330
  59. package/src/transport/multi/multi-transport.test.ts +0 -320
  60. package/src/transport/multi/multi-transport.ts +0 -294
  61. package/src/transport/rabbitmq/rabbitmq-transport.test.ts +0 -120
  62. package/src/transport/rabbitmq/rabbitmq-transport.ts +0 -782
  63. package/src/transport/transport.ts +0 -200
  64. package/src/types/common.ts +0 -53
  65. package/src/types/dispatcher.ts +0 -18
  66. package/src/types/envelope.ts +0 -244
  67. package/src/types/event.test.ts +0 -157
  68. package/src/types/event.ts +0 -112
  69. package/src/types/index.ts +0 -62
  70. package/src/types/subscriber.ts +0 -333
  71. package/test/e2e/multi-transport.e2e.test.ts +0 -237
  72. package/test/e2e/rabbitmq-transport.e2e.test.ts +0 -618
  73. package/test/e2e/transport-compliance.e2e.test.ts +0 -506
  74. package/test/integration/matador.integration.test.ts +0 -634
  75. package/tsconfig.json +0 -29
  76. package/tsconfig.tsbuildinfo +0 -1
  77. package/tsup.config.ts +0 -13
@@ -1,357 +0,0 @@
1
- import type { CheckpointStore } from '../checkpoint/index.js';
2
- import type { Codec } from '../codec/index.js';
3
- import { JsonCodec } from '../codec/index.js';
4
- import {
5
- InvalidSchemaError,
6
- NotStartedError,
7
- ShutdownInProgressError,
8
- } from '../errors/index.js';
9
- import type { MatadorHooks } from '../hooks/index.js';
10
- import { SafeHooks } from '../hooks/index.js';
11
- import { ProcessingPipeline } from '../pipeline/index.js';
12
- import type { RetryPolicy } from '../retry/index.js';
13
- import { StandardRetryPolicy } from '../retry/index.js';
14
- import type { MatadorSchema } from '../schema/index.js';
15
- import { SchemaRegistry, isSchemaEntryTuple } from '../schema/index.js';
16
- import type { Topology } from '../topology/index.js';
17
- import { getQualifiedQueueName } from '../topology/index.js';
18
- import type { Subscription, Transport } from '../transport/index.js';
19
- import type {
20
- AnySubscriber,
21
- Dispatcher,
22
- Event,
23
- EventClass,
24
- EventOptions,
25
- } from '../types/index.js';
26
- import type { SendResult } from './fanout.js';
27
- import { FanoutEngine } from './fanout.js';
28
- import type { HandlersState, ShutdownConfig } from './shutdown.js';
29
- import { ShutdownManager } from './shutdown.js';
30
-
31
- /**
32
- * Configuration for Matador.
33
- */
34
- export interface MatadorConfig {
35
- /** Transport for message delivery */
36
- readonly transport: Transport;
37
-
38
- /** Topology configuration */
39
- readonly topology: Topology;
40
-
41
- /** Event schema mapping event keys to event classes and subscribers */
42
- readonly schema: MatadorSchema;
43
-
44
- /** Queues to consume from (empty = no consumption) */
45
- readonly consumeFrom?: readonly string[] | undefined;
46
-
47
- /** Custom codec (defaults to JSON) */
48
- readonly codec?: Codec | undefined;
49
-
50
- /** Custom retry policy */
51
- readonly retryPolicy?: RetryPolicy | undefined;
52
-
53
- /** Shutdown configuration */
54
- readonly shutdownConfig?: Partial<ShutdownConfig> | undefined;
55
-
56
- /**
57
- * Checkpoint store for resumable subscribers.
58
- * Required for persisting io() results across retries.
59
- * If not provided, resumable subscribers will work but checkpoints
60
- * won't persist across retries.
61
- */
62
- readonly checkpointStore?: CheckpointStore | undefined;
63
- }
64
-
65
- /**
66
- * Matador - Transport-agnostic event processing library.
67
- *
68
- * Main orchestrator that wires together:
69
- * - Transport: Message delivery
70
- * - Schema: Event-subscriber registry
71
- * - Pipeline: Message processing
72
- * - Fanout: Event sending
73
- * - Shutdown: Graceful termination
74
- */
75
- export class Matador implements Dispatcher {
76
- private readonly transport: Transport;
77
- private readonly topology: Topology;
78
- private readonly schema;
79
- private readonly codec: Codec;
80
- private readonly retryPolicy: RetryPolicy;
81
- private readonly hooks;
82
- private readonly pipeline;
83
- private readonly fanout;
84
- private readonly shutdownManager;
85
- private readonly consumeFrom: readonly string[];
86
- private readonly subscriptions: Subscription[] = [];
87
- private started = false;
88
-
89
- /**
90
- * Creates a new Matador instance.
91
- *
92
- * @param config - Static configuration (transport, topology, schema, etc.)
93
- * @param hooks - Lifecycle hooks for logging, monitoring, and dynamic configuration.
94
- * Passed separately to support NestJS dependency injection.
95
- */
96
- constructor(config: MatadorConfig, hooks?: MatadorHooks) {
97
- this.transport = config.transport;
98
- this.topology = config.topology;
99
- this.consumeFrom = config.consumeFrom ?? [];
100
-
101
- // Initialize components
102
- this.schema = new SchemaRegistry();
103
- this.codec = config.codec ?? new JsonCodec();
104
- this.retryPolicy = config.retryPolicy ?? new StandardRetryPolicy();
105
- this.hooks = new SafeHooks(hooks);
106
-
107
- // Register schema from config
108
- this.registerSchema(config.schema);
109
-
110
- // Create pipeline
111
- this.pipeline = new ProcessingPipeline({
112
- transport: this.transport,
113
- schema: this.schema,
114
- codec: this.codec,
115
- retryPolicy: this.retryPolicy,
116
- hooks: this.hooks,
117
- checkpointStore: config.checkpointStore,
118
- dispatcher: this,
119
- });
120
-
121
- // Create fanout engine
122
- const defaultQueue = this.topology.queues[0]?.name ?? 'default';
123
- this.fanout = new FanoutEngine({
124
- transport: this.transport,
125
- schema: this.schema,
126
- hooks: this.hooks,
127
- namespace: this.topology.namespace,
128
- defaultQueue,
129
- });
130
-
131
- // Create shutdown manager
132
- this.shutdownManager = new ShutdownManager(
133
- () => this.fanout.eventsBeingEnqueuedCount,
134
- () => this.stopReceiving(),
135
- () => this.transport.disconnect(),
136
- config.shutdownConfig,
137
- );
138
- }
139
-
140
- /**
141
- * Registers an event class with its subscribers.
142
- */
143
- register<T>(
144
- eventClass: EventClass<T>,
145
- subscribers: readonly AnySubscriber[],
146
- ): this {
147
- this.schema.register(eventClass, subscribers);
148
- return this;
149
- }
150
-
151
- /**
152
- * Registers events from a schema object.
153
- * Supports both object format and tuple format entries.
154
- *
155
- * @example
156
- * ```typescript
157
- * // Tuple format
158
- * matador.registerSchema({
159
- * [UserCreatedEvent.key]: [UserCreatedEvent, [emailSubscriber]],
160
- * [OrderPlacedEvent.key]: [OrderPlacedEvent, [invoiceSubscriber]],
161
- * });
162
- *
163
- * // Object format
164
- * matador.registerSchema({
165
- * [UserCreatedEvent.key]: { eventClass: UserCreatedEvent, subscribers: [emailSubscriber] },
166
- * });
167
- * ```
168
- */
169
- registerSchema(schema: MatadorSchema): this {
170
- for (const entry of Object.values(schema)) {
171
- if (isSchemaEntryTuple(entry)) {
172
- // Tuple format: [EventClass, Subscriber[]]
173
- const [eventClass, subscribers] = entry;
174
- this.schema.register(eventClass, subscribers);
175
- } else {
176
- // Object format: { eventClass, subscribers }
177
- this.schema.register(entry.eventClass, entry.subscribers);
178
- }
179
- }
180
- return this;
181
- }
182
-
183
- /**
184
- * Starts Matador - connects transport and begins consuming.
185
- * This method is idempotent - calling it multiple times is safe.
186
- */
187
- async start(): Promise<void> {
188
- // Idempotent: if already started, just return
189
- if (this.started) {
190
- return;
191
- }
192
-
193
- // Validate schema
194
- const validation = this.schema.validate();
195
- if (!validation.valid) {
196
- const errors = validation.issues.filter((i) => i.severity === 'error');
197
- throw new InvalidSchemaError(
198
- 'Schema validation failed',
199
- errors.map((e) => e.message).join(', '),
200
- );
201
- }
202
-
203
- // Connect transport
204
- this.hooks.logger.info(
205
- `[Matador] ⏳ Starting backend '${this.transport.name}'...`,
206
- );
207
- await this.transport.connect();
208
- this.hooks.logger.info(
209
- `[Matador] 🟢 Backend '${this.transport.name}' started.`,
210
- );
211
-
212
- // Apply topology
213
- await this.transport.applyTopology(this.topology);
214
-
215
- // Subscribe to queues
216
- if (this.consumeFrom.length > 0) {
217
- this.hooks.logger.info(
218
- `[Matador] 🟢 Worker subscribing to '${this.consumeFrom.join(',')}'.`,
219
- );
220
- }
221
- for (const queueName of this.consumeFrom) {
222
- const qualifiedName = getQualifiedQueueName(
223
- this.topology.namespace,
224
- queueName,
225
- );
226
- const queueDef = this.topology.queues.find((q) => q.name === queueName);
227
-
228
- const subscription = await this.transport.subscribe(
229
- qualifiedName,
230
- async (envelope, receipt) => {
231
- this.shutdownManager.incrementProcessing();
232
- try {
233
- const rawMessage = this.codec.encode(envelope);
234
- await this.pipeline.process(rawMessage, receipt);
235
- } finally {
236
- this.shutdownManager.decrementProcessing();
237
- }
238
- },
239
- queueDef?.concurrency !== undefined
240
- ? { concurrency: queueDef.concurrency }
241
- : undefined,
242
- );
243
-
244
- this.subscriptions.push(subscription);
245
- }
246
-
247
- this.started = true;
248
- }
249
-
250
- /**
251
- * Sends an event to all registered subscribers.
252
- *
253
- * @example
254
- * ```typescript
255
- * // Pass the event class and data directly
256
- * await matador.send(UserCreatedEvent, { userId: '123' });
257
- *
258
- * // Or pass an event instance
259
- * const event = new UserCreatedEvent({ userId: '123' });
260
- * await matador.send(event);
261
- * ```
262
- */
263
- async send<T>(
264
- eventClass: EventClass<T>,
265
- data: T,
266
- options?: EventOptions,
267
- ): Promise<SendResult>;
268
- async send<T>(event: Event<T>, options?: EventOptions): Promise<SendResult>;
269
- async send<T>(
270
- eventOrClass: Event<T> | EventClass<T>,
271
- dataOrOptions?: T | EventOptions,
272
- maybeOptions?: EventOptions,
273
- ): Promise<SendResult> {
274
- if (!this.started) {
275
- throw new NotStartedError('send');
276
- }
277
-
278
- if (!this.shutdownManager.isEnqueueAllowed) {
279
- throw new ShutdownInProgressError();
280
- }
281
-
282
- // Determine if first arg is an event instance or event class
283
- const isEventClass =
284
- typeof eventOrClass === 'function' && 'key' in eventOrClass;
285
-
286
- if (isEventClass) {
287
- // Called as: send(EventClass, data, options?)
288
- const eventClass = eventOrClass as EventClass<T>;
289
- const data = dataOrOptions as T;
290
- const options = maybeOptions;
291
- const event = new eventClass(data);
292
- return this.fanout.send(eventClass, event, options);
293
- } else {
294
- // Called as: send(event, options?)
295
- const event = eventOrClass as Event<T>;
296
- const options = dataOrOptions as EventOptions | undefined;
297
- const eventClass = event.constructor as EventClass<T>;
298
- return this.fanout.send(eventClass, event, options);
299
- }
300
- }
301
-
302
- /**
303
- * Gets current handler state.
304
- */
305
- getHandlersState(): HandlersState {
306
- return this.shutdownManager.getHandlersState();
307
- }
308
-
309
- /**
310
- * Checks if Matador is idle (no processing or enqueuing).
311
- */
312
- isIdle(): boolean {
313
- return this.shutdownManager.getHandlersState().isIdle;
314
- }
315
-
316
- /**
317
- * Waits for all handlers to become idle.
318
- */
319
- async waitForIdle(timeoutMs = 30000): Promise<boolean> {
320
- const deadline = Date.now() + timeoutMs;
321
-
322
- while (!this.isIdle()) {
323
- if (Date.now() > deadline) {
324
- return false;
325
- }
326
- await new Promise((resolve) => setTimeout(resolve, 100));
327
- }
328
-
329
- return true;
330
- }
331
-
332
- /**
333
- * Gracefully shuts down Matador.
334
- */
335
- async shutdown(): Promise<void> {
336
- if (!this.started) {
337
- return;
338
- }
339
-
340
- await this.shutdownManager.shutdown();
341
- this.started = false;
342
- }
343
-
344
- /**
345
- * Checks if transport is connected.
346
- */
347
- isConnected(): boolean {
348
- return this.transport.isConnected();
349
- }
350
-
351
- private async stopReceiving(): Promise<void> {
352
- for (const subscription of this.subscriptions) {
353
- await subscription.unsubscribe();
354
- }
355
- this.subscriptions.length = 0;
356
- }
357
- }