@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,184 @@
|
|
|
1
|
+
import { AllTransportsFailedError } from '../../errors/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Symbol key used to store the source transport on wrapped receipts.
|
|
4
|
+
*/
|
|
5
|
+
const SOURCE_TRANSPORT = Symbol('sourceTransport');
|
|
6
|
+
/**
|
|
7
|
+
* Extracts the source transport from a tagged receipt.
|
|
8
|
+
*/
|
|
9
|
+
function getSourceTransport(receipt) {
|
|
10
|
+
return receipt[SOURCE_TRANSPORT];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Computes merged capabilities from multiple transports.
|
|
14
|
+
* Uses the most restrictive/conservative interpretation.
|
|
15
|
+
*/
|
|
16
|
+
function mergeCapabilities(transports) {
|
|
17
|
+
const primary = transports[0];
|
|
18
|
+
if (!primary) {
|
|
19
|
+
throw new Error('At least one transport is required');
|
|
20
|
+
}
|
|
21
|
+
// For multi-transport scenarios, we use primary's capabilities since that's
|
|
22
|
+
// what we'll use for subscriptions and most operations
|
|
23
|
+
return primary.capabilities;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* A transport wrapper that manages multiple transports.
|
|
27
|
+
*
|
|
28
|
+
* Features:
|
|
29
|
+
* - All transports are connected upfront
|
|
30
|
+
* - send() tries transports in order until one succeeds (fallback behavior)
|
|
31
|
+
* - Specific transport can be selected via getDesiredBackend hook
|
|
32
|
+
* - Subscriptions are created on ALL transports
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* const transport = new MultiTransport(
|
|
37
|
+
* { transports: [rabbitMQTransport, localTransport] },
|
|
38
|
+
* { onEnqueueFallback: (ctx) => console.warn(`Fallback to ${ctx.nextTransport}`) },
|
|
39
|
+
* );
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export class MultiTransport {
|
|
43
|
+
name;
|
|
44
|
+
capabilities;
|
|
45
|
+
primary;
|
|
46
|
+
/** All available transports, in order of preference (primary first). */
|
|
47
|
+
transports;
|
|
48
|
+
/** Whether fallback to secondary transports is enabled. */
|
|
49
|
+
fallbackEnabled;
|
|
50
|
+
hooks;
|
|
51
|
+
connected = false;
|
|
52
|
+
constructor(config, hooks = {}) {
|
|
53
|
+
const primary = config.transports[0];
|
|
54
|
+
if (!primary) {
|
|
55
|
+
throw new Error('At least one transport is required');
|
|
56
|
+
}
|
|
57
|
+
this.primary = primary;
|
|
58
|
+
this.transports = config.transports;
|
|
59
|
+
this.fallbackEnabled = config.fallbackEnabled ?? true;
|
|
60
|
+
this.hooks = hooks;
|
|
61
|
+
this.name = `multi(${this.transports.map((t) => t.name).join(',')})`;
|
|
62
|
+
this.capabilities = mergeCapabilities(this.transports);
|
|
63
|
+
}
|
|
64
|
+
async connect() {
|
|
65
|
+
// Connect all transports in parallel
|
|
66
|
+
await Promise.all(this.transports.map((t) => t.connect()));
|
|
67
|
+
this.connected = true;
|
|
68
|
+
}
|
|
69
|
+
async disconnect() {
|
|
70
|
+
// Disconnect all transports in parallel
|
|
71
|
+
await Promise.all(this.transports.map((t) => t.disconnect()));
|
|
72
|
+
this.connected = false;
|
|
73
|
+
}
|
|
74
|
+
isConnected() {
|
|
75
|
+
// Consider connected if primary is connected
|
|
76
|
+
return this.connected && this.primary.isConnected();
|
|
77
|
+
}
|
|
78
|
+
async applyTopology(topology) {
|
|
79
|
+
// Apply topology to all transports so they're all ready
|
|
80
|
+
await Promise.all(this.transports.map((t) => t.applyTopology(topology)));
|
|
81
|
+
}
|
|
82
|
+
async send(queue, envelope, options) {
|
|
83
|
+
// Determine which transport to use
|
|
84
|
+
const selectedTransport = await this.selectTransport();
|
|
85
|
+
// If fallback is disabled, only use selected transport
|
|
86
|
+
if (!this.fallbackEnabled) {
|
|
87
|
+
return selectedTransport.send(queue, envelope, options);
|
|
88
|
+
}
|
|
89
|
+
// Build transport order: selected first, then others
|
|
90
|
+
const transportOrder = this.getTransportOrder(selectedTransport);
|
|
91
|
+
const errors = [];
|
|
92
|
+
for (let i = 0; i < transportOrder.length; i++) {
|
|
93
|
+
const transport = transportOrder[i];
|
|
94
|
+
try {
|
|
95
|
+
return await transport.send(queue, envelope, options);
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
99
|
+
errors.push(err);
|
|
100
|
+
// Notify about fallback before trying next transport
|
|
101
|
+
const nextTransport = transportOrder[i + 1];
|
|
102
|
+
if (nextTransport && this.hooks.onEnqueueFallback) {
|
|
103
|
+
this.hooks.onEnqueueFallback({
|
|
104
|
+
envelope,
|
|
105
|
+
queue,
|
|
106
|
+
failedTransport: transport.name,
|
|
107
|
+
nextTransport: nextTransport.name,
|
|
108
|
+
error: err,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
// Continue to next transport
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
// All transports failed
|
|
115
|
+
throw new AllTransportsFailedError(queue, errors);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Selects the transport to use based on the getDesiredBackend hook.
|
|
119
|
+
*/
|
|
120
|
+
async selectTransport() {
|
|
121
|
+
if (!this.hooks.getDesiredBackend) {
|
|
122
|
+
return this.primary;
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
const desiredBackend = await this.hooks.getDesiredBackend();
|
|
126
|
+
if (!desiredBackend) {
|
|
127
|
+
return this.primary;
|
|
128
|
+
}
|
|
129
|
+
const selected = this.transports.find((t) => t.name === desiredBackend);
|
|
130
|
+
if (selected) {
|
|
131
|
+
return selected;
|
|
132
|
+
}
|
|
133
|
+
// Desired backend not found, use primary
|
|
134
|
+
return this.primary;
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
// Hook threw, use primary
|
|
138
|
+
return this.primary;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Returns transports in order, with the selected transport first.
|
|
143
|
+
*/
|
|
144
|
+
getTransportOrder(selected) {
|
|
145
|
+
if (selected === this.primary) {
|
|
146
|
+
return this.transports;
|
|
147
|
+
}
|
|
148
|
+
// Put selected first, then the rest (excluding selected)
|
|
149
|
+
return [selected, ...this.transports.filter((t) => t !== selected)];
|
|
150
|
+
}
|
|
151
|
+
async subscribe(queue, handler, options) {
|
|
152
|
+
// Subscribe on ALL transports so messages are processed regardless of
|
|
153
|
+
// which transport they were enqueued to
|
|
154
|
+
const subscriptions = await Promise.all(this.transports.map((transport) => {
|
|
155
|
+
// Wrap handler to tag receipts with source transport
|
|
156
|
+
const wrappedHandler = (envelope, receipt) => {
|
|
157
|
+
const taggedReceipt = Object.assign(receipt, {
|
|
158
|
+
[SOURCE_TRANSPORT]: transport,
|
|
159
|
+
});
|
|
160
|
+
return handler(envelope, taggedReceipt);
|
|
161
|
+
};
|
|
162
|
+
return transport.subscribe(queue, wrappedHandler, options);
|
|
163
|
+
}));
|
|
164
|
+
return {
|
|
165
|
+
unsubscribe: async () => {
|
|
166
|
+
await Promise.all(subscriptions.map((s) => s.unsubscribe()));
|
|
167
|
+
},
|
|
168
|
+
get isActive() {
|
|
169
|
+
return subscriptions.some((s) => s.isActive);
|
|
170
|
+
},
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
async complete(receipt) {
|
|
174
|
+
const transport = getSourceTransport(receipt) ?? this.primary;
|
|
175
|
+
return transport.complete(receipt);
|
|
176
|
+
}
|
|
177
|
+
async sendToDeadLetter(receipt, dlqName, envelope, reason) {
|
|
178
|
+
const transport = getSourceTransport(receipt) ?? this.primary;
|
|
179
|
+
if (transport.sendToDeadLetter) {
|
|
180
|
+
return transport.sendToDeadLetter(receipt, dlqName, envelope, reason);
|
|
181
|
+
}
|
|
182
|
+
throw new Error('Transport does not support dead letter routing');
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multi-transport.test.d.ts","sourceRoot":"","sources":["../../../src/transport/multi/multi-transport.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, mock } from 'bun:test';
|
|
2
|
+
import { createEnvelope } from '../../types/index.js';
|
|
3
|
+
import { LocalTransport } from '../local/local-transport.js';
|
|
4
|
+
import { MultiTransport } from './multi-transport.js';
|
|
5
|
+
describe('MultiTransport', () => {
|
|
6
|
+
let primary;
|
|
7
|
+
let secondary;
|
|
8
|
+
let transport;
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
primary = new LocalTransport();
|
|
11
|
+
secondary = new LocalTransport();
|
|
12
|
+
transport = new MultiTransport({
|
|
13
|
+
transports: [primary, secondary],
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
describe('constructor', () => {
|
|
17
|
+
it('should throw if no transports provided', () => {
|
|
18
|
+
expect(() => new MultiTransport({ transports: [] })).toThrow('At least one transport is required');
|
|
19
|
+
});
|
|
20
|
+
it('should set name based on transport names', () => {
|
|
21
|
+
expect(transport.name).toBe('multi(local,local)');
|
|
22
|
+
});
|
|
23
|
+
it('should use primary transport capabilities', () => {
|
|
24
|
+
expect(transport.capabilities).toBe(primary.capabilities);
|
|
25
|
+
});
|
|
26
|
+
it('should expose primary transport', () => {
|
|
27
|
+
expect(transport.primary).toBe(primary);
|
|
28
|
+
});
|
|
29
|
+
it('should expose all transports', () => {
|
|
30
|
+
expect(transport.transports).toEqual([primary, secondary]);
|
|
31
|
+
});
|
|
32
|
+
it('should default fallbackEnabled to true', () => {
|
|
33
|
+
expect(transport.fallbackEnabled).toBe(true);
|
|
34
|
+
});
|
|
35
|
+
it('should allow disabling fallback', () => {
|
|
36
|
+
const noFallback = new MultiTransport({
|
|
37
|
+
transports: [primary, secondary],
|
|
38
|
+
fallbackEnabled: false,
|
|
39
|
+
});
|
|
40
|
+
expect(noFallback.fallbackEnabled).toBe(false);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
describe('connection', () => {
|
|
44
|
+
it('should start disconnected', () => {
|
|
45
|
+
expect(transport.isConnected()).toBe(false);
|
|
46
|
+
});
|
|
47
|
+
it('should connect all transports', async () => {
|
|
48
|
+
await transport.connect();
|
|
49
|
+
expect(transport.isConnected()).toBe(true);
|
|
50
|
+
expect(primary.isConnected()).toBe(true);
|
|
51
|
+
expect(secondary.isConnected()).toBe(true);
|
|
52
|
+
});
|
|
53
|
+
it('should disconnect all transports', async () => {
|
|
54
|
+
await transport.connect();
|
|
55
|
+
await transport.disconnect();
|
|
56
|
+
expect(transport.isConnected()).toBe(false);
|
|
57
|
+
expect(primary.isConnected()).toBe(false);
|
|
58
|
+
expect(secondary.isConnected()).toBe(false);
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
describe('topology', () => {
|
|
62
|
+
it('should apply topology to all transports', async () => {
|
|
63
|
+
const topology = {
|
|
64
|
+
namespace: 'test',
|
|
65
|
+
queues: [{ name: 'events' }],
|
|
66
|
+
deadLetter: {
|
|
67
|
+
unhandled: { enabled: true },
|
|
68
|
+
undeliverable: { enabled: true },
|
|
69
|
+
},
|
|
70
|
+
retry: {
|
|
71
|
+
enabled: true,
|
|
72
|
+
defaultDelayMs: 1000,
|
|
73
|
+
maxDelayMs: 30000,
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
await transport.connect();
|
|
77
|
+
await transport.applyTopology(topology);
|
|
78
|
+
// Both transports should have the queue
|
|
79
|
+
expect(primary.getQueueSize('test.events')).toBe(0);
|
|
80
|
+
expect(secondary.getQueueSize('test.events')).toBe(0);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
describe('send - primary success', () => {
|
|
84
|
+
beforeEach(async () => {
|
|
85
|
+
await transport.connect();
|
|
86
|
+
});
|
|
87
|
+
it('should send to primary transport when it succeeds', async () => {
|
|
88
|
+
const envelope = createTestEnvelope();
|
|
89
|
+
await transport.send('test-queue', envelope);
|
|
90
|
+
expect(primary.getQueueSize('test-queue')).toBe(1);
|
|
91
|
+
expect(secondary.getQueueSize('test-queue')).toBe(0);
|
|
92
|
+
});
|
|
93
|
+
it('should not call onEnqueueFallback when primary succeeds', async () => {
|
|
94
|
+
const onEnqueueFallback = mock(() => { });
|
|
95
|
+
const transportWithCallback = new MultiTransport({ transports: [primary, secondary] }, { onEnqueueFallback });
|
|
96
|
+
await transportWithCallback.connect();
|
|
97
|
+
const envelope = createTestEnvelope();
|
|
98
|
+
await transportWithCallback.send('test-queue', envelope);
|
|
99
|
+
expect(onEnqueueFallback).not.toHaveBeenCalled();
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
describe('send - fallback', () => {
|
|
103
|
+
beforeEach(async () => {
|
|
104
|
+
await transport.connect();
|
|
105
|
+
});
|
|
106
|
+
it('should fallback when primary fails', async () => {
|
|
107
|
+
// Make primary fail
|
|
108
|
+
await primary.disconnect();
|
|
109
|
+
const envelope = createTestEnvelope();
|
|
110
|
+
await transport.send('test-queue', envelope);
|
|
111
|
+
expect(secondary.getQueueSize('test-queue')).toBe(1);
|
|
112
|
+
});
|
|
113
|
+
it('should call onEnqueueFallback when fallback is used', async () => {
|
|
114
|
+
const fallbackContexts = [];
|
|
115
|
+
const transportWithCallback = new MultiTransport({ transports: [primary, secondary] }, { onEnqueueFallback: (ctx) => fallbackContexts.push(ctx) });
|
|
116
|
+
await transportWithCallback.connect();
|
|
117
|
+
// Make primary fail
|
|
118
|
+
await primary.disconnect();
|
|
119
|
+
const envelope = createTestEnvelope();
|
|
120
|
+
await transportWithCallback.send('test-queue', envelope);
|
|
121
|
+
expect(fallbackContexts).toHaveLength(1);
|
|
122
|
+
const ctx = fallbackContexts[0];
|
|
123
|
+
expect(ctx.failedTransport).toBe('local');
|
|
124
|
+
expect(ctx.nextTransport).toBe('local');
|
|
125
|
+
expect(ctx.queue).toBe('test-queue');
|
|
126
|
+
expect(ctx.envelope).toBe(envelope);
|
|
127
|
+
expect(ctx.error.message).toContain('is not connected');
|
|
128
|
+
});
|
|
129
|
+
it('should throw when all transports fail', async () => {
|
|
130
|
+
await primary.disconnect();
|
|
131
|
+
await secondary.disconnect();
|
|
132
|
+
const envelope = createTestEnvelope();
|
|
133
|
+
await expect(transport.send('test-queue', envelope)).rejects.toThrow('All transports failed');
|
|
134
|
+
});
|
|
135
|
+
it('should try transports in order', async () => {
|
|
136
|
+
const third = new LocalTransport();
|
|
137
|
+
const multiTransport = new MultiTransport({
|
|
138
|
+
transports: [primary, secondary, third],
|
|
139
|
+
});
|
|
140
|
+
await multiTransport.connect();
|
|
141
|
+
// Make primary and secondary fail
|
|
142
|
+
await primary.disconnect();
|
|
143
|
+
await secondary.disconnect();
|
|
144
|
+
const envelope = createTestEnvelope();
|
|
145
|
+
await multiTransport.send('test-queue', envelope);
|
|
146
|
+
expect(third.getQueueSize('test-queue')).toBe(1);
|
|
147
|
+
});
|
|
148
|
+
it('should not fallback when fallbackEnabled is false', async () => {
|
|
149
|
+
const noFallback = new MultiTransport({
|
|
150
|
+
transports: [primary, secondary],
|
|
151
|
+
fallbackEnabled: false,
|
|
152
|
+
});
|
|
153
|
+
await noFallback.connect();
|
|
154
|
+
// Make primary fail
|
|
155
|
+
await primary.disconnect();
|
|
156
|
+
const envelope = createTestEnvelope();
|
|
157
|
+
await expect(noFallback.send('test-queue', envelope)).rejects.toThrow('is not connected');
|
|
158
|
+
// Secondary should not have received the message
|
|
159
|
+
expect(secondary.getQueueSize('test-queue')).toBe(0);
|
|
160
|
+
});
|
|
161
|
+
it('should send to primary when fallbackEnabled is false and primary succeeds', async () => {
|
|
162
|
+
const noFallback = new MultiTransport({
|
|
163
|
+
transports: [primary, secondary],
|
|
164
|
+
fallbackEnabled: false,
|
|
165
|
+
});
|
|
166
|
+
await noFallback.connect();
|
|
167
|
+
const envelope = createTestEnvelope();
|
|
168
|
+
await noFallback.send('test-queue', envelope);
|
|
169
|
+
expect(primary.getQueueSize('test-queue')).toBe(1);
|
|
170
|
+
expect(secondary.getQueueSize('test-queue')).toBe(0);
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
describe('subscribe', () => {
|
|
174
|
+
beforeEach(async () => {
|
|
175
|
+
await transport.connect();
|
|
176
|
+
});
|
|
177
|
+
it('should receive messages from primary transport', async () => {
|
|
178
|
+
const receivedMessages = [];
|
|
179
|
+
await transport.subscribe('test-queue', async (envelope) => {
|
|
180
|
+
receivedMessages.push(envelope);
|
|
181
|
+
});
|
|
182
|
+
// Send directly to primary (simulating normal operation)
|
|
183
|
+
await primary.send('test-queue', createTestEnvelope());
|
|
184
|
+
expect(receivedMessages).toHaveLength(1);
|
|
185
|
+
});
|
|
186
|
+
it('should receive messages that fell back to secondary transport', async () => {
|
|
187
|
+
const receivedMessages = [];
|
|
188
|
+
// Subscribe through MultiTransport
|
|
189
|
+
await transport.subscribe('test-queue', async (envelope) => {
|
|
190
|
+
receivedMessages.push(envelope);
|
|
191
|
+
});
|
|
192
|
+
// Make primary fail for sends
|
|
193
|
+
await primary.disconnect();
|
|
194
|
+
// Send through MultiTransport - should fall back to secondary
|
|
195
|
+
const envelope = createTestEnvelope();
|
|
196
|
+
await transport.send('test-queue', envelope);
|
|
197
|
+
// Message went to secondary transport, subscriber should still receive it
|
|
198
|
+
expect(receivedMessages).toHaveLength(1);
|
|
199
|
+
expect(receivedMessages[0].id).toBe(envelope.id);
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
describe('complete', () => {
|
|
203
|
+
beforeEach(async () => {
|
|
204
|
+
await transport.connect();
|
|
205
|
+
});
|
|
206
|
+
it('should complete using primary transport', async () => {
|
|
207
|
+
await primary.send('test-queue', createTestEnvelope());
|
|
208
|
+
const received = await primary.receiveOne('test-queue');
|
|
209
|
+
expect(received).not.toBeNull();
|
|
210
|
+
await transport.complete(received.receipt);
|
|
211
|
+
expect(primary.getCompleted()).toHaveLength(1);
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
describe('sendToDeadLetter', () => {
|
|
215
|
+
beforeEach(async () => {
|
|
216
|
+
await transport.connect();
|
|
217
|
+
});
|
|
218
|
+
it('should send to dead letter using primary transport', async () => {
|
|
219
|
+
const envelope = createTestEnvelope();
|
|
220
|
+
await primary.send('test-queue', envelope);
|
|
221
|
+
const received = await primary.receiveOne('test-queue');
|
|
222
|
+
expect(received).not.toBeNull();
|
|
223
|
+
await transport.sendToDeadLetter(received.receipt, 'dlq', envelope, 'test error');
|
|
224
|
+
expect(primary.getQueueSize('test-queue')).toBe(0);
|
|
225
|
+
expect(primary.getQueueSize('test-queue.dlq')).toBe(1);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
function createTestEnvelope() {
|
|
230
|
+
return createEnvelope({
|
|
231
|
+
eventKey: 'test.event',
|
|
232
|
+
targetSubscriber: 'test-subscriber',
|
|
233
|
+
data: { test: 'data' },
|
|
234
|
+
importance: 'should-investigate',
|
|
235
|
+
});
|
|
236
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { type Logger } from '../../hooks/index.js';
|
|
2
|
+
import type { Topology } from '../../topology/types.js';
|
|
3
|
+
import type { Envelope } from '../../types/index.js';
|
|
4
|
+
import type { TransportCapabilities } from '../capabilities.js';
|
|
5
|
+
import { type ConnectionManagerConfig } from '../connection-manager.js';
|
|
6
|
+
import type { MessageHandler, MessageReceipt, SendOptions, SubscribeOptions, Subscription, Transport } from '../transport.js';
|
|
7
|
+
/**
|
|
8
|
+
* Configuration options for the RabbitMQ transport.
|
|
9
|
+
*/
|
|
10
|
+
export interface RabbitMQTransportConfig {
|
|
11
|
+
/** RabbitMQ connection URL */
|
|
12
|
+
readonly url: string;
|
|
13
|
+
/** Connection manager configuration */
|
|
14
|
+
readonly connection?: Partial<ConnectionManagerConfig> | undefined;
|
|
15
|
+
/** Use quorum queues for durability (default: true) */
|
|
16
|
+
readonly quorumQueues?: boolean | undefined;
|
|
17
|
+
/** Default prefetch count per consumer (default: 10) */
|
|
18
|
+
readonly defaultPrefetch?: number | undefined;
|
|
19
|
+
/** Enable the delayed message exchange plugin if available (default: true) */
|
|
20
|
+
readonly enableDelayedMessages?: boolean | undefined;
|
|
21
|
+
/** Logger for transport events (defaults to console) */
|
|
22
|
+
readonly logger?: Logger | undefined;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* RabbitMQ transport implementation using amqplib.
|
|
26
|
+
*/
|
|
27
|
+
export declare class RabbitMQTransport implements Transport {
|
|
28
|
+
readonly name = "rabbitmq";
|
|
29
|
+
private _capabilities;
|
|
30
|
+
get capabilities(): TransportCapabilities;
|
|
31
|
+
private connection;
|
|
32
|
+
private publishChannel;
|
|
33
|
+
private readonly connectionManager;
|
|
34
|
+
private readonly queueChannels;
|
|
35
|
+
private topology;
|
|
36
|
+
private readonly codec;
|
|
37
|
+
private readonly config;
|
|
38
|
+
private readonly logger;
|
|
39
|
+
private delayedExchangeAvailable;
|
|
40
|
+
constructor(config: RabbitMQTransportConfig);
|
|
41
|
+
connect(): Promise<void>;
|
|
42
|
+
disconnect(): Promise<void>;
|
|
43
|
+
isConnected(): boolean;
|
|
44
|
+
applyTopology(topology: Topology): Promise<void>;
|
|
45
|
+
send(queue: string, envelope: Envelope, options?: SendOptions): Promise<Transport['name']>;
|
|
46
|
+
subscribe(queue: string, handler: MessageHandler, options?: SubscribeOptions): Promise<Subscription>;
|
|
47
|
+
complete(receipt: MessageReceipt): Promise<void>;
|
|
48
|
+
sendToDeadLetter(receipt: MessageReceipt, dlqName: string, envelope: Envelope, reason: string): Promise<void>;
|
|
49
|
+
/**
|
|
50
|
+
* Gets or creates a dedicated channel for a queue subscription.
|
|
51
|
+
*
|
|
52
|
+
* We create separate channels per subscribed queue to enable independent
|
|
53
|
+
* prefetch/concurrency control.
|
|
54
|
+
*/
|
|
55
|
+
private getOrCreateQueueChannel;
|
|
56
|
+
private doConnect;
|
|
57
|
+
private doDisconnect;
|
|
58
|
+
private setupDelayedExchange;
|
|
59
|
+
private assertWorkQueue;
|
|
60
|
+
private assertRetryQueue;
|
|
61
|
+
private assertDeadLetterQueues;
|
|
62
|
+
private getMainExchangeName;
|
|
63
|
+
private getDLXExchangeName;
|
|
64
|
+
private getDelayedExchangeName;
|
|
65
|
+
private getAttemptNumber;
|
|
66
|
+
/**
|
|
67
|
+
* Gets the native delivery count for poison message detection.
|
|
68
|
+
* This tracks how many times the message was delivered without acknowledgment,
|
|
69
|
+
* which helps detect crash loops.
|
|
70
|
+
*/
|
|
71
|
+
private getDeliveryCount;
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=rabbitmq-transport.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rabbitmq-transport.d.ts","sourceRoot":"","sources":["../../../src/transport/rabbitmq/rabbitmq-transport.ts"],"names":[],"mappings":"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"}
|