@zdavison/matador 2.0.9 → 2.0.11

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 (259) hide show
  1. package/dist/checkpoint/context.d.ts +59 -0
  2. package/dist/checkpoint/context.d.ts.map +1 -0
  3. package/dist/checkpoint/context.js +140 -0
  4. package/dist/checkpoint/context.test.d.ts +2 -0
  5. package/dist/checkpoint/context.test.d.ts.map +1 -0
  6. package/dist/checkpoint/context.test.js +424 -0
  7. package/dist/checkpoint/index.d.ts +7 -0
  8. package/dist/checkpoint/index.d.ts.map +1 -0
  9. package/dist/checkpoint/index.js +6 -0
  10. package/dist/checkpoint/stores/memory.d.ts +29 -0
  11. package/dist/checkpoint/stores/memory.d.ts.map +1 -0
  12. package/dist/checkpoint/stores/memory.js +39 -0
  13. package/{src/checkpoint/stores/noop.ts → dist/checkpoint/stores/noop.d.ts} +5 -13
  14. package/dist/checkpoint/stores/noop.d.ts.map +1 -0
  15. package/dist/checkpoint/stores/noop.js +18 -0
  16. package/dist/checkpoint/stores/stores.test.d.ts +2 -0
  17. package/dist/checkpoint/stores/stores.test.d.ts.map +1 -0
  18. package/dist/checkpoint/stores/stores.test.js +146 -0
  19. package/dist/checkpoint/types.d.ts +119 -0
  20. package/dist/checkpoint/types.d.ts.map +1 -0
  21. package/dist/checkpoint/types.js +1 -0
  22. package/dist/codec/codec.d.ts +29 -0
  23. package/dist/codec/codec.d.ts.map +1 -0
  24. package/dist/codec/codec.js +15 -0
  25. package/dist/codec/header-aware-codec.d.ts +36 -0
  26. package/dist/codec/header-aware-codec.d.ts.map +1 -0
  27. package/dist/codec/header-aware-codec.js +1 -0
  28. package/{src/codec/index.ts → dist/codec/index.d.ts} +2 -7
  29. package/dist/codec/index.d.ts.map +1 -0
  30. package/dist/codec/index.js +3 -0
  31. package/dist/codec/json-codec.d.ts +13 -0
  32. package/dist/codec/json-codec.d.ts.map +1 -0
  33. package/dist/codec/json-codec.js +64 -0
  34. package/dist/codec/rabbitmq-codec.d.ts +28 -0
  35. package/dist/codec/rabbitmq-codec.d.ts.map +1 -0
  36. package/dist/codec/rabbitmq-codec.js +242 -0
  37. package/dist/codec/rabbitmq-codec.test.d.ts +2 -0
  38. package/dist/codec/rabbitmq-codec.test.d.ts.map +1 -0
  39. package/dist/codec/rabbitmq-codec.test.js +433 -0
  40. package/dist/core/fanout.d.ts +59 -0
  41. package/dist/core/fanout.d.ts.map +1 -0
  42. package/dist/core/fanout.js +121 -0
  43. package/dist/core/fanout.test.d.ts +2 -0
  44. package/dist/core/fanout.test.d.ts.map +1 -0
  45. package/dist/core/fanout.test.js +1054 -0
  46. package/{src/core/index.ts → dist/core/index.d.ts} +2 -7
  47. package/dist/core/index.d.ts.map +1 -0
  48. package/dist/core/index.js +3 -0
  49. package/dist/core/matador.d.ts +133 -0
  50. package/dist/core/matador.d.ts.map +1 -0
  51. package/dist/core/matador.js +228 -0
  52. package/dist/core/matador.test.d.ts +2 -0
  53. package/dist/core/matador.test.d.ts.map +1 -0
  54. package/dist/core/matador.test.js +446 -0
  55. package/dist/core/shutdown.d.ts +78 -0
  56. package/dist/core/shutdown.d.ts.map +1 -0
  57. package/dist/core/shutdown.js +111 -0
  58. package/dist/core/shutdown.test.d.ts +2 -0
  59. package/dist/core/shutdown.test.d.ts.map +1 -0
  60. package/dist/core/shutdown.test.js +524 -0
  61. package/dist/errors/checkpoint-errors.d.ts +30 -0
  62. package/dist/errors/checkpoint-errors.d.ts.map +1 -0
  63. package/dist/errors/checkpoint-errors.js +49 -0
  64. package/dist/errors/has-description.d.ts +18 -0
  65. package/dist/errors/has-description.d.ts.map +1 -0
  66. package/dist/errors/has-description.js +9 -0
  67. package/dist/errors/index.d.ts +6 -0
  68. package/dist/errors/index.d.ts.map +1 -0
  69. package/dist/errors/index.js +22 -0
  70. package/dist/errors/matador-errors.d.ts +183 -0
  71. package/dist/errors/matador-errors.d.ts.map +1 -0
  72. package/dist/errors/matador-errors.js +376 -0
  73. package/dist/errors/retry-errors.d.ts +102 -0
  74. package/dist/errors/retry-errors.d.ts.map +1 -0
  75. package/dist/errors/retry-errors.js +155 -0
  76. package/dist/errors/retry-errors.test.d.ts +2 -0
  77. package/dist/errors/retry-errors.test.d.ts.map +1 -0
  78. package/dist/errors/retry-errors.test.js +136 -0
  79. package/dist/hooks/index.d.ts +4 -0
  80. package/dist/hooks/index.d.ts.map +1 -0
  81. package/dist/hooks/index.js +2 -0
  82. package/dist/hooks/safe-hooks.d.ts +32 -0
  83. package/dist/hooks/safe-hooks.d.ts.map +1 -0
  84. package/dist/hooks/safe-hooks.js +120 -0
  85. package/dist/hooks/types.d.ts +172 -0
  86. package/dist/hooks/types.d.ts.map +1 -0
  87. package/dist/hooks/types.js +9 -0
  88. package/dist/index.cjs +3228 -0
  89. package/dist/index.cjs.map +1 -0
  90. package/dist/index.d.cts +23 -0
  91. package/dist/index.d.ts +23 -0
  92. package/dist/index.d.ts.map +1 -0
  93. package/dist/index.js +31 -0
  94. package/dist/index.js.map +1 -0
  95. package/{src/pipeline/index.ts → dist/pipeline/index.d.ts} +1 -0
  96. package/dist/pipeline/index.d.ts.map +1 -0
  97. package/dist/pipeline/index.js +1 -0
  98. package/dist/pipeline/pipeline.d.ts +63 -0
  99. package/dist/pipeline/pipeline.d.ts.map +1 -0
  100. package/dist/pipeline/pipeline.js +273 -0
  101. package/dist/pipeline/pipeline.test.d.ts +2 -0
  102. package/dist/pipeline/pipeline.test.d.ts.map +1 -0
  103. package/dist/pipeline/pipeline.test.js +1112 -0
  104. package/{src/retry/index.ts → dist/retry/index.d.ts} +1 -1
  105. package/dist/retry/index.d.ts.map +1 -0
  106. package/dist/retry/index.js +1 -0
  107. package/dist/retry/policy.d.ts +43 -0
  108. package/dist/retry/policy.d.ts.map +1 -0
  109. package/dist/retry/policy.js +1 -0
  110. package/dist/retry/standard-policy.d.ts +44 -0
  111. package/dist/retry/standard-policy.d.ts.map +1 -0
  112. package/dist/retry/standard-policy.js +102 -0
  113. package/dist/retry/standard-policy.test.d.ts +2 -0
  114. package/dist/retry/standard-policy.test.d.ts.map +1 -0
  115. package/dist/retry/standard-policy.test.js +196 -0
  116. package/dist/schema/index.d.ts +4 -0
  117. package/dist/schema/index.d.ts.map +1 -0
  118. package/dist/schema/index.js +2 -0
  119. package/dist/schema/registry.d.ts +63 -0
  120. package/dist/schema/registry.d.ts.map +1 -0
  121. package/dist/schema/registry.js +174 -0
  122. package/dist/schema/registry.test.d.ts +2 -0
  123. package/dist/schema/registry.test.d.ts.map +1 -0
  124. package/dist/schema/registry.test.js +278 -0
  125. package/{src/schema/types.ts → dist/schema/types.d.ts} +29 -88
  126. package/dist/schema/types.d.ts.map +1 -0
  127. package/dist/schema/types.js +74 -0
  128. package/dist/schema/types.test.d.ts +2 -0
  129. package/dist/schema/types.test.d.ts.map +1 -0
  130. package/dist/schema/types.test.js +242 -0
  131. package/dist/topology/builder.d.ts +80 -0
  132. package/dist/topology/builder.d.ts.map +1 -0
  133. package/dist/topology/builder.js +165 -0
  134. package/dist/topology/builder.test.d.ts +2 -0
  135. package/dist/topology/builder.test.d.ts.map +1 -0
  136. package/dist/topology/builder.test.js +360 -0
  137. package/dist/topology/index.d.ts +5 -0
  138. package/dist/topology/index.d.ts.map +1 -0
  139. package/dist/topology/index.js +2 -0
  140. package/dist/topology/types.d.ts +128 -0
  141. package/dist/topology/types.d.ts.map +1 -0
  142. package/dist/topology/types.js +28 -0
  143. package/dist/transport/capabilities.d.ts +66 -0
  144. package/dist/transport/capabilities.d.ts.map +1 -0
  145. package/dist/transport/capabilities.js +18 -0
  146. package/dist/transport/connection-manager.d.ts +95 -0
  147. package/dist/transport/connection-manager.d.ts.map +1 -0
  148. package/dist/transport/connection-manager.js +144 -0
  149. package/dist/transport/index.d.ts +11 -0
  150. package/dist/transport/index.d.ts.map +1 -0
  151. package/dist/transport/index.js +5 -0
  152. package/dist/transport/local/local-transport.d.ts +62 -0
  153. package/dist/transport/local/local-transport.d.ts.map +1 -0
  154. package/dist/transport/local/local-transport.js +241 -0
  155. package/dist/transport/local/local-transport.test.d.ts +2 -0
  156. package/dist/transport/local/local-transport.test.d.ts.map +1 -0
  157. package/dist/transport/local/local-transport.test.js +192 -0
  158. package/dist/transport/multi/multi-transport.d.ts +94 -0
  159. package/dist/transport/multi/multi-transport.d.ts.map +1 -0
  160. package/dist/transport/multi/multi-transport.js +184 -0
  161. package/dist/transport/multi/multi-transport.test.d.ts +2 -0
  162. package/dist/transport/multi/multi-transport.test.d.ts.map +1 -0
  163. package/dist/transport/multi/multi-transport.test.js +236 -0
  164. package/dist/transport/rabbitmq/rabbitmq-transport.d.ts +83 -0
  165. package/dist/transport/rabbitmq/rabbitmq-transport.d.ts.map +1 -0
  166. package/dist/transport/rabbitmq/rabbitmq-transport.js +541 -0
  167. package/dist/transport/rabbitmq/rabbitmq-transport.test.d.ts +2 -0
  168. package/dist/transport/rabbitmq/rabbitmq-transport.test.d.ts.map +1 -0
  169. package/dist/transport/rabbitmq/rabbitmq-transport.test.js +100 -0
  170. package/dist/transport/transport.d.ts +152 -0
  171. package/dist/transport/transport.d.ts.map +1 -0
  172. package/dist/transport/transport.js +1 -0
  173. package/{src/types/common.ts → dist/types/common.d.ts} +8 -20
  174. package/dist/types/common.d.ts.map +1 -0
  175. package/dist/types/common.js +12 -0
  176. package/dist/types/dispatcher.d.ts +14 -0
  177. package/dist/types/dispatcher.d.ts.map +1 -0
  178. package/dist/types/dispatcher.js +1 -0
  179. package/dist/types/envelope.d.ts +131 -0
  180. package/dist/types/envelope.d.ts.map +1 -0
  181. package/dist/types/envelope.js +99 -0
  182. package/dist/types/event.d.ts +92 -0
  183. package/dist/types/event.d.ts.map +1 -0
  184. package/dist/types/event.js +26 -0
  185. package/dist/types/event.test.d.ts +2 -0
  186. package/dist/types/event.test.d.ts.map +1 -0
  187. package/dist/types/event.test.js +130 -0
  188. package/dist/types/index.d.ts +11 -0
  189. package/dist/types/index.d.ts.map +1 -0
  190. package/dist/types/index.js +4 -0
  191. package/dist/types/subscriber.d.ts +227 -0
  192. package/dist/types/subscriber.d.ts.map +1 -0
  193. package/dist/types/subscriber.js +97 -0
  194. package/package.json +6 -2
  195. package/examples/config.ts +0 -126
  196. package/examples/event.ts +0 -26
  197. package/examples/order-event.json +0 -19
  198. package/src/checkpoint/context.test.ts +0 -510
  199. package/src/checkpoint/context.ts +0 -213
  200. package/src/checkpoint/index.ts +0 -30
  201. package/src/checkpoint/stores/memory.ts +0 -47
  202. package/src/checkpoint/stores/stores.test.ts +0 -177
  203. package/src/checkpoint/types.ts +0 -147
  204. package/src/codec/codec.ts +0 -42
  205. package/src/codec/header-aware-codec.ts +0 -41
  206. package/src/codec/json-codec.ts +0 -69
  207. package/src/codec/rabbitmq-codec.test.ts +0 -516
  208. package/src/codec/rabbitmq-codec.ts +0 -336
  209. package/src/core/fanout.test.ts +0 -1350
  210. package/src/core/fanout.ts +0 -184
  211. package/src/core/matador.test.ts +0 -575
  212. package/src/core/matador.ts +0 -357
  213. package/src/core/shutdown.test.ts +0 -853
  214. package/src/core/shutdown.ts +0 -165
  215. package/src/errors/checkpoint-errors.ts +0 -62
  216. package/src/errors/has-description.ts +0 -25
  217. package/src/errors/index.ts +0 -58
  218. package/src/errors/matador-errors.ts +0 -477
  219. package/src/errors/retry-errors.test.ts +0 -175
  220. package/src/errors/retry-errors.ts +0 -188
  221. package/src/hooks/index.ts +0 -14
  222. package/src/hooks/safe-hooks.ts +0 -200
  223. package/src/hooks/types.ts +0 -226
  224. package/src/index.ts +0 -242
  225. package/src/pipeline/pipeline.test.ts +0 -1377
  226. package/src/pipeline/pipeline.ts +0 -393
  227. package/src/retry/policy.ts +0 -46
  228. package/src/retry/standard-policy.test.ts +0 -290
  229. package/src/retry/standard-policy.ts +0 -156
  230. package/src/schema/index.ts +0 -16
  231. package/src/schema/registry.test.ts +0 -339
  232. package/src/schema/registry.ts +0 -229
  233. package/src/schema/types.test.ts +0 -280
  234. package/src/topology/builder.test.ts +0 -451
  235. package/src/topology/builder.ts +0 -238
  236. package/src/topology/index.ts +0 -19
  237. package/src/topology/types.ts +0 -183
  238. package/src/transport/capabilities.ts +0 -88
  239. package/src/transport/connection-manager.ts +0 -218
  240. package/src/transport/index.ts +0 -42
  241. package/src/transport/local/local-transport.test.ts +0 -262
  242. package/src/transport/local/local-transport.ts +0 -330
  243. package/src/transport/multi/multi-transport.test.ts +0 -320
  244. package/src/transport/multi/multi-transport.ts +0 -294
  245. package/src/transport/rabbitmq/rabbitmq-transport.test.ts +0 -120
  246. package/src/transport/rabbitmq/rabbitmq-transport.ts +0 -782
  247. package/src/transport/transport.ts +0 -200
  248. package/src/types/dispatcher.ts +0 -18
  249. package/src/types/envelope.ts +0 -244
  250. package/src/types/event.test.ts +0 -157
  251. package/src/types/event.ts +0 -112
  252. package/src/types/index.ts +0 -62
  253. package/src/types/subscriber.ts +0 -333
  254. package/test/e2e/multi-transport.e2e.test.ts +0 -237
  255. package/test/e2e/rabbitmq-transport.e2e.test.ts +0 -618
  256. package/test/e2e/transport-compliance.e2e.test.ts +0 -506
  257. package/test/integration/matador.integration.test.ts +0 -634
  258. package/tsconfig.json +0 -29
  259. package/tsup.config.ts +0 -13
@@ -0,0 +1,242 @@
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
+ targetQueue: 'analytics-worker',
200
+ });
201
+ const baseSchema = {
202
+ [UserCreatedEvent.key]: [UserCreatedEvent, [sub1]],
203
+ };
204
+ const schema = installPlugins(baseSchema, [{ subscriber: stubSub }]);
205
+ const entry = schema[UserCreatedEvent.key];
206
+ const [, subs] = entry;
207
+ expect(subs).toHaveLength(2);
208
+ expect(subs[1]).toBe(stubSub);
209
+ });
210
+ it('should return empty schema for empty input', () => {
211
+ const schema = installPlugins({}, [
212
+ {
213
+ subscriber: createSubscriber({
214
+ name: 'analytics',
215
+ description: 'Analytics subscriber',
216
+ callback: async () => { },
217
+ }),
218
+ },
219
+ ]);
220
+ expect(Object.keys(schema)).toHaveLength(0);
221
+ });
222
+ it('should not modify original schema', () => {
223
+ const sub1 = createSubscriber({
224
+ name: 'user-handler',
225
+ description: 'Handles user events',
226
+ callback: async () => { },
227
+ });
228
+ const globalSub = createSubscriber({
229
+ name: 'analytics',
230
+ description: 'Analytics subscriber',
231
+ callback: async () => { },
232
+ });
233
+ const baseSchema = {
234
+ [UserCreatedEvent.key]: [UserCreatedEvent, [sub1]],
235
+ };
236
+ installPlugins(baseSchema, [{ subscriber: globalSub }]);
237
+ // Original schema should be unchanged
238
+ const entry = baseSchema[UserCreatedEvent.key];
239
+ const [, subs] = entry;
240
+ expect(subs).toHaveLength(1);
241
+ });
242
+ });
@@ -0,0 +1,80 @@
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
+ * @param definition - A complete QueueDefinition object
35
+ */
36
+ addQueue(definition: QueueDefinition): this;
37
+ /**
38
+ * Adds a queue to the topology.
39
+ * @param name - Queue name
40
+ * @param options - Queue options
41
+ */
42
+ addQueue(name: string, options?: QueueOptions): this;
43
+ /**
44
+ * Alias for addQueue().
45
+ * @param definition - A complete QueueDefinition object
46
+ */
47
+ queue(definition: QueueDefinition): this;
48
+ /**
49
+ * Alias for addQueue().
50
+ * @param name - Queue name
51
+ * @param options - Queue options
52
+ */
53
+ queue(name: string, options?: QueueOptions): this;
54
+ /**
55
+ * Configures dead-letter queue settings.
56
+ */
57
+ withDeadLetter(config: Partial<DeadLetterConfig>): this;
58
+ /**
59
+ * Configures retry settings.
60
+ */
61
+ withRetry(config: Partial<RetryConfig>): this;
62
+ /**
63
+ * Disables retry functionality.
64
+ */
65
+ withoutRetry(): this;
66
+ /**
67
+ * Disables dead-letter queues.
68
+ */
69
+ withoutDeadLetter(): this;
70
+ /**
71
+ * Validates the topology configuration.
72
+ */
73
+ validate(): readonly string[];
74
+ /**
75
+ * Builds the topology configuration.
76
+ * @throws TopologyValidationError if validation fails
77
+ */
78
+ build(): Topology;
79
+ }
80
+ //# 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;AAWzD;;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;;;OAGG;IACH,QAAQ,CAAC,UAAU,EAAE,eAAe,GAAG,IAAI;IAC3C;;;;OAIG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI;IAapD;;;OAGG;IACH,KAAK,CAAC,UAAU,EAAE,eAAe,GAAG,IAAI;IACxC;;;;OAIG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,GAAG,IAAI;IAWjD;;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;IAuD7B;;;OAGG;IACH,KAAK,IAAI,QAAQ;CAgBlB"}
@@ -0,0 +1,165 @@
1
+ /**
2
+ * Type guard to check if the argument is a QueueDefinition object.
3
+ */
4
+ function isQueueDefinition(arg) {
5
+ return typeof arg === 'object' && arg !== null && 'name' in arg;
6
+ }
7
+ /**
8
+ * Error thrown when topology validation fails.
9
+ */
10
+ export class TopologyValidationError extends Error {
11
+ issues;
12
+ description = 'The topology configuration is invalid. Check the issues array for ' +
13
+ 'specific validation failures such as missing namespace, invalid queue ' +
14
+ 'names, or conflicting settings. This error occurs during Matador ' +
15
+ 'initialization and must be fixed in the configuration.';
16
+ constructor(message, issues) {
17
+ super(message);
18
+ this.issues = issues;
19
+ this.name = 'TopologyValidationError';
20
+ }
21
+ }
22
+ /**
23
+ * Fluent builder for creating Topology configurations.
24
+ */
25
+ export class TopologyBuilder {
26
+ /**
27
+ * Creates a new TopologyBuilder instance.
28
+ */
29
+ static create() {
30
+ return new TopologyBuilder();
31
+ }
32
+ namespace = '';
33
+ queues = [];
34
+ deadLetter = {
35
+ unhandled: { enabled: true },
36
+ undeliverable: { enabled: true },
37
+ };
38
+ retry = {
39
+ enabled: true,
40
+ defaultDelayMs: 1000,
41
+ maxDelayMs: 300000, // 5 minutes
42
+ };
43
+ /**
44
+ * Sets the namespace prefix for all queues.
45
+ */
46
+ withNamespace(namespace) {
47
+ this.namespace = namespace;
48
+ return this;
49
+ }
50
+ addQueue(nameOrDefinition, options = {}) {
51
+ if (isQueueDefinition(nameOrDefinition)) {
52
+ this.queues.push(nameOrDefinition);
53
+ }
54
+ else {
55
+ this.queues.push({ name: nameOrDefinition, ...options });
56
+ }
57
+ return this;
58
+ }
59
+ queue(nameOrDefinition, options = {}) {
60
+ if (isQueueDefinition(nameOrDefinition)) {
61
+ return this.addQueue(nameOrDefinition);
62
+ }
63
+ return this.addQueue(nameOrDefinition, options);
64
+ }
65
+ /**
66
+ * Configures dead-letter queue settings.
67
+ */
68
+ withDeadLetter(config) {
69
+ this.deadLetter = {
70
+ unhandled: config.unhandled ?? this.deadLetter.unhandled,
71
+ undeliverable: config.undeliverable ?? this.deadLetter.undeliverable,
72
+ };
73
+ return this;
74
+ }
75
+ /**
76
+ * Configures retry settings.
77
+ */
78
+ withRetry(config) {
79
+ this.retry = {
80
+ enabled: config.enabled ?? this.retry.enabled,
81
+ defaultDelayMs: config.defaultDelayMs ?? this.retry.defaultDelayMs,
82
+ maxDelayMs: config.maxDelayMs ?? this.retry.maxDelayMs,
83
+ };
84
+ return this;
85
+ }
86
+ /**
87
+ * Disables retry functionality.
88
+ */
89
+ withoutRetry() {
90
+ this.retry = { ...this.retry, enabled: false };
91
+ return this;
92
+ }
93
+ /**
94
+ * Disables dead-letter queues.
95
+ */
96
+ withoutDeadLetter() {
97
+ this.deadLetter = {
98
+ unhandled: { enabled: false },
99
+ undeliverable: { enabled: false },
100
+ };
101
+ return this;
102
+ }
103
+ /**
104
+ * Validates the topology configuration.
105
+ */
106
+ validate() {
107
+ const issues = [];
108
+ if (!this.namespace || this.namespace.trim() === '') {
109
+ issues.push('Namespace is required');
110
+ }
111
+ else if (!/^[a-zA-Z][a-zA-Z0-9_-]*$/.test(this.namespace)) {
112
+ issues.push('Namespace must start with a letter and contain only alphanumeric characters, underscores, and hyphens');
113
+ }
114
+ if (this.queues.length === 0) {
115
+ issues.push('At least one queue is required');
116
+ }
117
+ const queueNames = new Set();
118
+ for (const queue of this.queues) {
119
+ if (!queue.name || queue.name.trim() === '') {
120
+ issues.push('Queue name cannot be empty');
121
+ }
122
+ else if (!queue.exact && !/^[a-zA-Z][a-zA-Z0-9_-]*$/.test(queue.name)) {
123
+ // Skip pattern validation for exact queues (allows names like 'matador.shared.id-platform')
124
+ issues.push(`Queue name "${queue.name}" must start with a letter and contain only alphanumeric characters, underscores, and hyphens`);
125
+ }
126
+ else if (queueNames.has(queue.name)) {
127
+ issues.push(`Duplicate queue name: "${queue.name}"`);
128
+ }
129
+ else {
130
+ queueNames.add(queue.name);
131
+ }
132
+ if (queue.concurrency !== undefined && queue.concurrency < 1) {
133
+ issues.push(`Queue "${queue.name}" concurrency must be at least 1`);
134
+ }
135
+ if (queue.consumerTimeout !== undefined && queue.consumerTimeout < 0) {
136
+ issues.push(`Queue "${queue.name}" consumer timeout must be non-negative`);
137
+ }
138
+ }
139
+ if (this.retry.enabled) {
140
+ if (this.retry.defaultDelayMs < 0) {
141
+ issues.push('Default retry delay must be non-negative');
142
+ }
143
+ if (this.retry.maxDelayMs < this.retry.defaultDelayMs) {
144
+ issues.push('Max retry delay must be greater than or equal to default delay');
145
+ }
146
+ }
147
+ return issues;
148
+ }
149
+ /**
150
+ * Builds the topology configuration.
151
+ * @throws TopologyValidationError if validation fails
152
+ */
153
+ build() {
154
+ const issues = this.validate();
155
+ if (issues.length > 0) {
156
+ throw new TopologyValidationError(`Invalid topology: ${issues.join('; ')}`, issues);
157
+ }
158
+ return {
159
+ namespace: this.namespace,
160
+ queues: [...this.queues],
161
+ deadLetter: this.deadLetter,
162
+ retry: this.retry,
163
+ };
164
+ }
165
+ }
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=builder.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"builder.test.d.ts","sourceRoot":"","sources":["../../src/topology/builder.test.ts"],"names":[],"mappings":""}