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