@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,243 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { MatadorEvent, createSubscriber, createSubscriberStub, } from '../types/index.js';
|
|
3
|
+
import { bind, installPlugins, isSchemaEntryTuple } from './types.js';
|
|
4
|
+
class UserCreatedEvent extends MatadorEvent {
|
|
5
|
+
data;
|
|
6
|
+
static key = 'user.created';
|
|
7
|
+
static description = 'User created event';
|
|
8
|
+
constructor(data) {
|
|
9
|
+
super();
|
|
10
|
+
this.data = data;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
class OrderPlacedEvent extends MatadorEvent {
|
|
14
|
+
data;
|
|
15
|
+
static key = 'order.placed';
|
|
16
|
+
static description = 'Order placed event';
|
|
17
|
+
constructor(data) {
|
|
18
|
+
super();
|
|
19
|
+
this.data = data;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
class ChatMessageSentEvent extends MatadorEvent {
|
|
23
|
+
data;
|
|
24
|
+
static key = 'chat.message.sent';
|
|
25
|
+
static description = 'Chat message sent event';
|
|
26
|
+
constructor(data) {
|
|
27
|
+
super();
|
|
28
|
+
this.data = data;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
describe('bind', () => {
|
|
32
|
+
it('should create a schema entry tuple', () => {
|
|
33
|
+
const subscriber = createSubscriber({
|
|
34
|
+
name: 'test-sub',
|
|
35
|
+
description: 'Test subscriber',
|
|
36
|
+
callback: async () => { },
|
|
37
|
+
});
|
|
38
|
+
const entry = bind(UserCreatedEvent, [subscriber]);
|
|
39
|
+
expect(isSchemaEntryTuple(entry)).toBe(true);
|
|
40
|
+
expect(entry[0]).toBe(UserCreatedEvent);
|
|
41
|
+
expect(entry[1]).toHaveLength(1);
|
|
42
|
+
expect(entry[1][0]).toBe(subscriber);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
describe('isSchemaEntryTuple', () => {
|
|
46
|
+
it('should return true for tuple format', () => {
|
|
47
|
+
const entry = [UserCreatedEvent, []];
|
|
48
|
+
expect(isSchemaEntryTuple(entry)).toBe(true);
|
|
49
|
+
});
|
|
50
|
+
it('should return false for object format', () => {
|
|
51
|
+
const entry = { eventClass: UserCreatedEvent, subscribers: [] };
|
|
52
|
+
expect(isSchemaEntryTuple(entry)).toBe(false);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
describe('installPlugins', () => {
|
|
56
|
+
it('should add plugin subscriber to all events', () => {
|
|
57
|
+
const sub1 = createSubscriber({
|
|
58
|
+
name: 'user-handler',
|
|
59
|
+
description: 'Handles user events',
|
|
60
|
+
callback: async () => { },
|
|
61
|
+
});
|
|
62
|
+
const sub2 = createSubscriber({
|
|
63
|
+
name: 'order-handler',
|
|
64
|
+
description: 'Handles order events',
|
|
65
|
+
callback: async () => { },
|
|
66
|
+
});
|
|
67
|
+
const globalSub = createSubscriber({
|
|
68
|
+
name: 'analytics',
|
|
69
|
+
description: 'Analytics subscriber',
|
|
70
|
+
callback: async () => { },
|
|
71
|
+
});
|
|
72
|
+
const baseSchema = {
|
|
73
|
+
[UserCreatedEvent.key]: [UserCreatedEvent, [sub1]],
|
|
74
|
+
[OrderPlacedEvent.key]: [OrderPlacedEvent, [sub2]],
|
|
75
|
+
};
|
|
76
|
+
const schema = installPlugins(baseSchema, [{ subscriber: globalSub }]);
|
|
77
|
+
// Check user.created has both subscribers
|
|
78
|
+
const userEntry = schema[UserCreatedEvent.key];
|
|
79
|
+
expect(isSchemaEntryTuple(userEntry)).toBe(true);
|
|
80
|
+
const [, userSubs] = userEntry;
|
|
81
|
+
expect(userSubs).toHaveLength(2);
|
|
82
|
+
expect(userSubs[0]).toBe(sub1);
|
|
83
|
+
expect(userSubs[1]).toBe(globalSub);
|
|
84
|
+
// Check order.placed has both subscribers
|
|
85
|
+
const orderEntry = schema[OrderPlacedEvent.key];
|
|
86
|
+
const [, orderSubs] = orderEntry;
|
|
87
|
+
expect(orderSubs).toHaveLength(2);
|
|
88
|
+
expect(orderSubs[0]).toBe(sub2);
|
|
89
|
+
expect(orderSubs[1]).toBe(globalSub);
|
|
90
|
+
});
|
|
91
|
+
it('should respect exclusions', () => {
|
|
92
|
+
const sub1 = createSubscriber({
|
|
93
|
+
name: 'user-handler',
|
|
94
|
+
description: 'Handles user events',
|
|
95
|
+
callback: async () => { },
|
|
96
|
+
});
|
|
97
|
+
const sub2 = createSubscriber({
|
|
98
|
+
name: 'order-handler',
|
|
99
|
+
description: 'Handles order events',
|
|
100
|
+
callback: async () => { },
|
|
101
|
+
});
|
|
102
|
+
const sub3 = createSubscriber({
|
|
103
|
+
name: 'chat-handler',
|
|
104
|
+
description: 'Handles chat events',
|
|
105
|
+
callback: async () => { },
|
|
106
|
+
});
|
|
107
|
+
const globalSub = createSubscriber({
|
|
108
|
+
name: 'analytics',
|
|
109
|
+
description: 'Analytics subscriber',
|
|
110
|
+
callback: async () => { },
|
|
111
|
+
});
|
|
112
|
+
const baseSchema = {
|
|
113
|
+
[UserCreatedEvent.key]: [UserCreatedEvent, [sub1]],
|
|
114
|
+
[OrderPlacedEvent.key]: [OrderPlacedEvent, [sub2]],
|
|
115
|
+
[ChatMessageSentEvent.key]: [ChatMessageSentEvent, [sub3]],
|
|
116
|
+
};
|
|
117
|
+
const schema = installPlugins(baseSchema, [
|
|
118
|
+
{
|
|
119
|
+
subscriber: globalSub,
|
|
120
|
+
exclusions: [ChatMessageSentEvent.key],
|
|
121
|
+
},
|
|
122
|
+
]);
|
|
123
|
+
// user.created should have analytics
|
|
124
|
+
const userEntry = schema[UserCreatedEvent.key];
|
|
125
|
+
const [, userSubs] = userEntry;
|
|
126
|
+
expect(userSubs).toHaveLength(2);
|
|
127
|
+
// order.placed should have analytics
|
|
128
|
+
const orderEntry = schema[OrderPlacedEvent.key];
|
|
129
|
+
const [, orderSubs] = orderEntry;
|
|
130
|
+
expect(orderSubs).toHaveLength(2);
|
|
131
|
+
// chat.message.sent should NOT have analytics (excluded)
|
|
132
|
+
const chatEntry = schema[ChatMessageSentEvent.key];
|
|
133
|
+
const [, chatSubs] = chatEntry;
|
|
134
|
+
expect(chatSubs).toHaveLength(1);
|
|
135
|
+
expect(chatSubs[0]).toBe(sub3);
|
|
136
|
+
});
|
|
137
|
+
it('should handle multiple plugins', () => {
|
|
138
|
+
const sub1 = createSubscriber({
|
|
139
|
+
name: 'user-handler',
|
|
140
|
+
description: 'Handles user events',
|
|
141
|
+
callback: async () => { },
|
|
142
|
+
});
|
|
143
|
+
const analyticsSub = createSubscriber({
|
|
144
|
+
name: 'analytics',
|
|
145
|
+
description: 'Analytics subscriber',
|
|
146
|
+
callback: async () => { },
|
|
147
|
+
});
|
|
148
|
+
const loggingSub = createSubscriber({
|
|
149
|
+
name: 'logging',
|
|
150
|
+
description: 'Logging subscriber',
|
|
151
|
+
callback: async () => { },
|
|
152
|
+
});
|
|
153
|
+
const baseSchema = {
|
|
154
|
+
[UserCreatedEvent.key]: [UserCreatedEvent, [sub1]],
|
|
155
|
+
};
|
|
156
|
+
const schema = installPlugins(baseSchema, [
|
|
157
|
+
{ subscriber: analyticsSub },
|
|
158
|
+
{ subscriber: loggingSub },
|
|
159
|
+
]);
|
|
160
|
+
const entry = schema[UserCreatedEvent.key];
|
|
161
|
+
const [, subs] = entry;
|
|
162
|
+
expect(subs).toHaveLength(3);
|
|
163
|
+
expect(subs[0]).toBe(sub1);
|
|
164
|
+
expect(subs[1]).toBe(analyticsSub);
|
|
165
|
+
expect(subs[2]).toBe(loggingSub);
|
|
166
|
+
});
|
|
167
|
+
it('should handle object format schema entries', () => {
|
|
168
|
+
const sub1 = createSubscriber({
|
|
169
|
+
name: 'user-handler',
|
|
170
|
+
description: 'Handles user events',
|
|
171
|
+
callback: async () => { },
|
|
172
|
+
});
|
|
173
|
+
const globalSub = createSubscriber({
|
|
174
|
+
name: 'analytics',
|
|
175
|
+
description: 'Analytics subscriber',
|
|
176
|
+
callback: async () => { },
|
|
177
|
+
});
|
|
178
|
+
const baseSchema = {
|
|
179
|
+
[UserCreatedEvent.key]: {
|
|
180
|
+
eventClass: UserCreatedEvent,
|
|
181
|
+
subscribers: [sub1],
|
|
182
|
+
},
|
|
183
|
+
};
|
|
184
|
+
const schema = installPlugins(baseSchema, [{ subscriber: globalSub }]);
|
|
185
|
+
const entry = schema[UserCreatedEvent.key];
|
|
186
|
+
expect(isSchemaEntryTuple(entry)).toBe(true);
|
|
187
|
+
const [eventClass, subs] = entry;
|
|
188
|
+
expect(eventClass).toBe(UserCreatedEvent);
|
|
189
|
+
expect(subs).toHaveLength(2);
|
|
190
|
+
});
|
|
191
|
+
it('should work with subscriber stubs', () => {
|
|
192
|
+
const sub1 = createSubscriber({
|
|
193
|
+
name: 'user-handler',
|
|
194
|
+
description: 'Handles user events',
|
|
195
|
+
callback: async () => { },
|
|
196
|
+
});
|
|
197
|
+
const stubSub = createSubscriberStub({
|
|
198
|
+
name: 'remote-analytics',
|
|
199
|
+
description: 'Remote analytics stub',
|
|
200
|
+
targetQueue: 'analytics-worker',
|
|
201
|
+
});
|
|
202
|
+
const baseSchema = {
|
|
203
|
+
[UserCreatedEvent.key]: [UserCreatedEvent, [sub1]],
|
|
204
|
+
};
|
|
205
|
+
const schema = installPlugins(baseSchema, [{ subscriber: stubSub }]);
|
|
206
|
+
const entry = schema[UserCreatedEvent.key];
|
|
207
|
+
const [, subs] = entry;
|
|
208
|
+
expect(subs).toHaveLength(2);
|
|
209
|
+
expect(subs[1]).toBe(stubSub);
|
|
210
|
+
});
|
|
211
|
+
it('should return empty schema for empty input', () => {
|
|
212
|
+
const schema = installPlugins({}, [
|
|
213
|
+
{
|
|
214
|
+
subscriber: createSubscriber({
|
|
215
|
+
name: 'analytics',
|
|
216
|
+
description: 'Analytics subscriber',
|
|
217
|
+
callback: async () => { },
|
|
218
|
+
}),
|
|
219
|
+
},
|
|
220
|
+
]);
|
|
221
|
+
expect(Object.keys(schema)).toHaveLength(0);
|
|
222
|
+
});
|
|
223
|
+
it('should not modify original schema', () => {
|
|
224
|
+
const sub1 = createSubscriber({
|
|
225
|
+
name: 'user-handler',
|
|
226
|
+
description: 'Handles user events',
|
|
227
|
+
callback: async () => { },
|
|
228
|
+
});
|
|
229
|
+
const globalSub = createSubscriber({
|
|
230
|
+
name: 'analytics',
|
|
231
|
+
description: 'Analytics subscriber',
|
|
232
|
+
callback: async () => { },
|
|
233
|
+
});
|
|
234
|
+
const baseSchema = {
|
|
235
|
+
[UserCreatedEvent.key]: [UserCreatedEvent, [sub1]],
|
|
236
|
+
};
|
|
237
|
+
installPlugins(baseSchema, [{ subscriber: globalSub }]);
|
|
238
|
+
// Original schema should be unchanged
|
|
239
|
+
const entry = baseSchema[UserCreatedEvent.key];
|
|
240
|
+
const [, subs] = entry;
|
|
241
|
+
expect(subs).toHaveLength(1);
|
|
242
|
+
});
|
|
243
|
+
});
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { HasDescription } from '../errors/index.js';
|
|
2
|
+
import type { DeadLetterConfig, QueueDefinition, RetryConfig, Topology } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Options for adding a queue.
|
|
5
|
+
* Excludes 'name' as that is provided as the first argument to addQueue.
|
|
6
|
+
*/
|
|
7
|
+
export type QueueOptions = Omit<QueueDefinition, 'name'>;
|
|
8
|
+
/**
|
|
9
|
+
* Error thrown when topology validation fails.
|
|
10
|
+
*/
|
|
11
|
+
export declare class TopologyValidationError extends Error implements HasDescription {
|
|
12
|
+
readonly issues: readonly string[];
|
|
13
|
+
readonly description: string;
|
|
14
|
+
constructor(message: string, issues: readonly string[]);
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Fluent builder for creating Topology configurations.
|
|
18
|
+
*/
|
|
19
|
+
export declare class TopologyBuilder {
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new TopologyBuilder instance.
|
|
22
|
+
*/
|
|
23
|
+
static create(): TopologyBuilder;
|
|
24
|
+
private namespace;
|
|
25
|
+
private queues;
|
|
26
|
+
private deadLetter;
|
|
27
|
+
private retry;
|
|
28
|
+
/**
|
|
29
|
+
* Sets the namespace prefix for all queues.
|
|
30
|
+
*/
|
|
31
|
+
withNamespace(namespace: string): this;
|
|
32
|
+
/**
|
|
33
|
+
* Adds a queue to the topology.
|
|
34
|
+
*/
|
|
35
|
+
addQueue(name: string, options?: QueueOptions): this;
|
|
36
|
+
/**
|
|
37
|
+
* Alias for addQueue().
|
|
38
|
+
*/
|
|
39
|
+
queue(name: string, options?: QueueOptions): this;
|
|
40
|
+
/**
|
|
41
|
+
* Configures dead-letter queue settings.
|
|
42
|
+
*/
|
|
43
|
+
withDeadLetter(config: Partial<DeadLetterConfig>): this;
|
|
44
|
+
/**
|
|
45
|
+
* Configures retry settings.
|
|
46
|
+
*/
|
|
47
|
+
withRetry(config: Partial<RetryConfig>): this;
|
|
48
|
+
/**
|
|
49
|
+
* Disables retry functionality.
|
|
50
|
+
*/
|
|
51
|
+
withoutRetry(): this;
|
|
52
|
+
/**
|
|
53
|
+
* Disables dead-letter queues.
|
|
54
|
+
*/
|
|
55
|
+
withoutDeadLetter(): this;
|
|
56
|
+
/**
|
|
57
|
+
* Validates the topology configuration.
|
|
58
|
+
*/
|
|
59
|
+
validate(): readonly string[];
|
|
60
|
+
/**
|
|
61
|
+
* Builds the topology configuration.
|
|
62
|
+
* @throws TopologyValidationError if validation fails
|
|
63
|
+
*/
|
|
64
|
+
build(): Topology;
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../../src/topology/builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,KAAK,EACV,gBAAgB,EAChB,eAAe,EACf,WAAW,EACX,QAAQ,EACT,MAAM,YAAY,CAAC;AAEpB;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG,IAAI,CAAC,eAAe,EAAE,MAAM,CAAC,CAAC;AAEzD;;GAEG;AACH,qBAAa,uBAAwB,SAAQ,KAAM,YAAW,cAAc;aASxD,MAAM,EAAE,SAAS,MAAM,EAAE;IAR3C,QAAQ,CAAC,WAAW,SAIuC;gBAGzD,OAAO,EAAE,MAAM,EACC,MAAM,EAAE,SAAS,MAAM,EAAE;CAK5C;AAED;;GAEG;AACH,qBAAa,eAAe;IAC1B;;OAEG;IACH,MAAM,CAAC,MAAM,IAAI,eAAe;IAIhC,OAAO,CAAC,SAAS,CAAM;IACvB,OAAO,CAAC,MAAM,CAAyB;IACvC,OAAO,CAAC,UAAU,CAGhB;IACF,OAAO,CAAC,KAAK,CAIX;IAEF;;OAEG;IACH,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI;IAKtC;;OAEG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,YAAiB,GAAG,IAAI;IAKxD;;OAEG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,YAAiB,GAAG,IAAI;IAIrD;;OAEG;IACH,cAAc,CAAC,MAAM,EAAE,OAAO,CAAC,gBAAgB,CAAC,GAAG,IAAI;IAQvD;;OAEG;IACH,SAAS,CAAC,MAAM,EAAE,OAAO,CAAC,WAAW,CAAC,GAAG,IAAI;IAS7C;;OAEG;IACH,YAAY,IAAI,IAAI;IAKpB;;OAEG;IACH,iBAAiB,IAAI,IAAI;IAQzB;;OAEG;IACH,QAAQ,IAAI,SAAS,MAAM,EAAE;IAsD7B;;;OAGG;IACH,KAAK,IAAI,QAAQ;CAgBlB"}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error thrown when topology validation fails.
|
|
3
|
+
*/
|
|
4
|
+
export class TopologyValidationError extends Error {
|
|
5
|
+
issues;
|
|
6
|
+
description = 'The topology configuration is invalid. Check the issues array for ' +
|
|
7
|
+
'specific validation failures such as missing namespace, invalid queue ' +
|
|
8
|
+
'names, or conflicting settings. This error occurs during Matador ' +
|
|
9
|
+
'initialization and must be fixed in the configuration.';
|
|
10
|
+
constructor(message, issues) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.issues = issues;
|
|
13
|
+
this.name = 'TopologyValidationError';
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Fluent builder for creating Topology configurations.
|
|
18
|
+
*/
|
|
19
|
+
export class TopologyBuilder {
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new TopologyBuilder instance.
|
|
22
|
+
*/
|
|
23
|
+
static create() {
|
|
24
|
+
return new TopologyBuilder();
|
|
25
|
+
}
|
|
26
|
+
namespace = '';
|
|
27
|
+
queues = [];
|
|
28
|
+
deadLetter = {
|
|
29
|
+
unhandled: { enabled: true },
|
|
30
|
+
undeliverable: { enabled: true },
|
|
31
|
+
};
|
|
32
|
+
retry = {
|
|
33
|
+
enabled: true,
|
|
34
|
+
defaultDelayMs: 1000,
|
|
35
|
+
maxDelayMs: 300000, // 5 minutes
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Sets the namespace prefix for all queues.
|
|
39
|
+
*/
|
|
40
|
+
withNamespace(namespace) {
|
|
41
|
+
this.namespace = namespace;
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Adds a queue to the topology.
|
|
46
|
+
*/
|
|
47
|
+
addQueue(name, options = {}) {
|
|
48
|
+
this.queues.push({ name, ...options });
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Alias for addQueue().
|
|
53
|
+
*/
|
|
54
|
+
queue(name, options = {}) {
|
|
55
|
+
return this.addQueue(name, options);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Configures dead-letter queue settings.
|
|
59
|
+
*/
|
|
60
|
+
withDeadLetter(config) {
|
|
61
|
+
this.deadLetter = {
|
|
62
|
+
unhandled: config.unhandled ?? this.deadLetter.unhandled,
|
|
63
|
+
undeliverable: config.undeliverable ?? this.deadLetter.undeliverable,
|
|
64
|
+
};
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Configures retry settings.
|
|
69
|
+
*/
|
|
70
|
+
withRetry(config) {
|
|
71
|
+
this.retry = {
|
|
72
|
+
enabled: config.enabled ?? this.retry.enabled,
|
|
73
|
+
defaultDelayMs: config.defaultDelayMs ?? this.retry.defaultDelayMs,
|
|
74
|
+
maxDelayMs: config.maxDelayMs ?? this.retry.maxDelayMs,
|
|
75
|
+
};
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Disables retry functionality.
|
|
80
|
+
*/
|
|
81
|
+
withoutRetry() {
|
|
82
|
+
this.retry = { ...this.retry, enabled: false };
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Disables dead-letter queues.
|
|
87
|
+
*/
|
|
88
|
+
withoutDeadLetter() {
|
|
89
|
+
this.deadLetter = {
|
|
90
|
+
unhandled: { enabled: false },
|
|
91
|
+
undeliverable: { enabled: false },
|
|
92
|
+
};
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Validates the topology configuration.
|
|
97
|
+
*/
|
|
98
|
+
validate() {
|
|
99
|
+
const issues = [];
|
|
100
|
+
if (!this.namespace || this.namespace.trim() === '') {
|
|
101
|
+
issues.push('Namespace is required');
|
|
102
|
+
}
|
|
103
|
+
else if (!/^[a-zA-Z][a-zA-Z0-9_-]*$/.test(this.namespace)) {
|
|
104
|
+
issues.push('Namespace must start with a letter and contain only alphanumeric characters, underscores, and hyphens');
|
|
105
|
+
}
|
|
106
|
+
if (this.queues.length === 0) {
|
|
107
|
+
issues.push('At least one queue is required');
|
|
108
|
+
}
|
|
109
|
+
const queueNames = new Set();
|
|
110
|
+
for (const queue of this.queues) {
|
|
111
|
+
if (!queue.name || queue.name.trim() === '') {
|
|
112
|
+
issues.push('Queue name cannot be empty');
|
|
113
|
+
}
|
|
114
|
+
else if (!/^[a-zA-Z][a-zA-Z0-9_-]*$/.test(queue.name)) {
|
|
115
|
+
issues.push(`Queue name "${queue.name}" must start with a letter and contain only alphanumeric characters, underscores, and hyphens`);
|
|
116
|
+
}
|
|
117
|
+
else if (queueNames.has(queue.name)) {
|
|
118
|
+
issues.push(`Duplicate queue name: "${queue.name}"`);
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
queueNames.add(queue.name);
|
|
122
|
+
}
|
|
123
|
+
if (queue.concurrency !== undefined && queue.concurrency < 1) {
|
|
124
|
+
issues.push(`Queue "${queue.name}" concurrency must be at least 1`);
|
|
125
|
+
}
|
|
126
|
+
if (queue.consumerTimeout !== undefined && queue.consumerTimeout < 0) {
|
|
127
|
+
issues.push(`Queue "${queue.name}" consumer timeout must be non-negative`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (this.retry.enabled) {
|
|
131
|
+
if (this.retry.defaultDelayMs < 0) {
|
|
132
|
+
issues.push('Default retry delay must be non-negative');
|
|
133
|
+
}
|
|
134
|
+
if (this.retry.maxDelayMs < this.retry.defaultDelayMs) {
|
|
135
|
+
issues.push('Max retry delay must be greater than or equal to default delay');
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return issues;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Builds the topology configuration.
|
|
142
|
+
* @throws TopologyValidationError if validation fails
|
|
143
|
+
*/
|
|
144
|
+
build() {
|
|
145
|
+
const issues = this.validate();
|
|
146
|
+
if (issues.length > 0) {
|
|
147
|
+
throw new TopologyValidationError(`Invalid topology: ${issues.join('; ')}`, issues);
|
|
148
|
+
}
|
|
149
|
+
return {
|
|
150
|
+
namespace: this.namespace,
|
|
151
|
+
queues: [...this.queues],
|
|
152
|
+
deadLetter: this.deadLetter,
|
|
153
|
+
retry: this.retry,
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder.test.d.ts","sourceRoot":"","sources":["../../src/topology/builder.test.ts"],"names":[],"mappings":""}
|