@zdavison/matador 4.0.3 → 4.2.0

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 (71) hide show
  1. package/dist/core/matador.d.ts.map +1 -1
  2. package/dist/core/matador.js +4 -1
  3. package/dist/index.cjs +156 -16
  4. package/dist/index.cjs.map +1 -1
  5. package/dist/index.js.map +1 -1
  6. package/dist/pipeline/pipeline.d.ts +30 -2
  7. package/dist/pipeline/pipeline.d.ts.map +1 -1
  8. package/dist/pipeline/pipeline.js +65 -9
  9. package/dist/retry/index.d.ts +1 -1
  10. package/dist/retry/index.d.ts.map +1 -1
  11. package/dist/retry/policy.d.ts +27 -0
  12. package/dist/retry/policy.d.ts.map +1 -1
  13. package/dist/retry/standard-policy.d.ts +33 -2
  14. package/dist/retry/standard-policy.d.ts.map +1 -1
  15. package/dist/retry/standard-policy.js +73 -9
  16. package/dist/topology/types.d.ts +14 -0
  17. package/dist/topology/types.d.ts.map +1 -1
  18. package/dist/transport/rabbitmq/rabbitmq-transport.d.ts.map +1 -1
  19. package/dist/transport/rabbitmq/rabbitmq-transport.js +3 -0
  20. package/package.json +1 -1
  21. package/dist/checkpoint/context.test.d.ts +0 -2
  22. package/dist/checkpoint/context.test.d.ts.map +0 -1
  23. package/dist/checkpoint/context.test.js +0 -428
  24. package/dist/checkpoint/stores/stores.test.d.ts +0 -2
  25. package/dist/checkpoint/stores/stores.test.d.ts.map +0 -1
  26. package/dist/checkpoint/stores/stores.test.js +0 -146
  27. package/dist/codec/rabbitmq-codec.test.d.ts +0 -2
  28. package/dist/codec/rabbitmq-codec.test.d.ts.map +0 -1
  29. package/dist/codec/rabbitmq-codec.test.js +0 -428
  30. package/dist/core/fanout.test.d.ts +0 -2
  31. package/dist/core/fanout.test.d.ts.map +0 -1
  32. package/dist/core/fanout.test.js +0 -1970
  33. package/dist/core/matador.test.d.ts +0 -2
  34. package/dist/core/matador.test.d.ts.map +0 -1
  35. package/dist/core/matador.test.js +0 -813
  36. package/dist/core/shutdown.test.d.ts +0 -2
  37. package/dist/core/shutdown.test.d.ts.map +0 -1
  38. package/dist/core/shutdown.test.js +0 -599
  39. package/dist/errors/retry-errors.test.d.ts +0 -2
  40. package/dist/errors/retry-errors.test.d.ts.map +0 -1
  41. package/dist/errors/retry-errors.test.js +0 -136
  42. package/dist/pipeline/pipeline.test.d.ts +0 -2
  43. package/dist/pipeline/pipeline.test.d.ts.map +0 -1
  44. package/dist/pipeline/pipeline.test.js +0 -1124
  45. package/dist/retry/standard-policy.test.d.ts +0 -2
  46. package/dist/retry/standard-policy.test.d.ts.map +0 -1
  47. package/dist/retry/standard-policy.test.js +0 -196
  48. package/dist/schema/registry.test.d.ts +0 -2
  49. package/dist/schema/registry.test.d.ts.map +0 -1
  50. package/dist/schema/registry.test.js +0 -278
  51. package/dist/schema/types.test.d.ts +0 -2
  52. package/dist/schema/types.test.d.ts.map +0 -1
  53. package/dist/schema/types.test.js +0 -246
  54. package/dist/topology/builder.test.d.ts +0 -2
  55. package/dist/topology/builder.test.d.ts.map +0 -1
  56. package/dist/topology/builder.test.js +0 -547
  57. package/dist/transport/local/local-transport.test.d.ts +0 -2
  58. package/dist/transport/local/local-transport.test.d.ts.map +0 -1
  59. package/dist/transport/local/local-transport.test.js +0 -232
  60. package/dist/transport/multi/multi-transport.test.d.ts +0 -2
  61. package/dist/transport/multi/multi-transport.test.d.ts.map +0 -1
  62. package/dist/transport/multi/multi-transport.test.js +0 -322
  63. package/dist/transport/rabbitmq/rabbitmq-transport-reconnection.test.d.ts +0 -2
  64. package/dist/transport/rabbitmq/rabbitmq-transport-reconnection.test.d.ts.map +0 -1
  65. package/dist/transport/rabbitmq/rabbitmq-transport-reconnection.test.js +0 -149
  66. package/dist/transport/rabbitmq/rabbitmq-transport.test.d.ts +0 -2
  67. package/dist/transport/rabbitmq/rabbitmq-transport.test.d.ts.map +0 -1
  68. package/dist/transport/rabbitmq/rabbitmq-transport.test.js +0 -230
  69. package/dist/types/event.test.d.ts +0 -2
  70. package/dist/types/event.test.d.ts.map +0 -1
  71. package/dist/types/event.test.js +0 -130
@@ -1,428 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from 'bun:test';
2
- import { DuplicateIoKeyError } from '../errors/index.js';
3
- import { ResumableContext } from './context.js';
4
- import { MemoryCheckpointStore } from './stores/memory.js';
5
- function createTestEnvelope(id = 'test-envelope-id', attempts = 1) {
6
- return {
7
- id,
8
- data: { userId: '123' },
9
- docket: {
10
- eventKey: 'test.event',
11
- targetSubscriber: 'test-subscriber',
12
- importance: 'should-investigate',
13
- attempts,
14
- createdAt: new Date().toISOString(),
15
- },
16
- };
17
- }
18
- function createTestSubscriber(name = 'test-subscriber') {
19
- return {
20
- name,
21
- description: 'Test subscriber',
22
- idempotent: 'resumable',
23
- importance: 'should-investigate',
24
- };
25
- }
26
- describe('ResumableContext', () => {
27
- let store;
28
- let envelope;
29
- let subscriber;
30
- beforeEach(() => {
31
- store = new MemoryCheckpointStore();
32
- envelope = createTestEnvelope();
33
- subscriber = createTestSubscriber();
34
- });
35
- describe('io()', () => {
36
- it('should execute function and cache result on first call', async () => {
37
- const context = new ResumableContext({
38
- store,
39
- envelope,
40
- subscriber,
41
- });
42
- let executeCount = 0;
43
- const result = await context.io('step-1', () => {
44
- executeCount++;
45
- return 'result-1';
46
- });
47
- expect(result).toBe('result-1');
48
- expect(executeCount).toBe(1);
49
- expect(store.size).toBe(1);
50
- });
51
- it('should return cached result on retry without re-executing', async () => {
52
- // First execution - cache the result
53
- const firstContext = new ResumableContext({
54
- store,
55
- envelope,
56
- subscriber,
57
- });
58
- let executeCount = 0;
59
- await firstContext.io('step-1', () => {
60
- executeCount++;
61
- return 'result-1';
62
- });
63
- expect(executeCount).toBe(1);
64
- // Simulate retry - load existing checkpoint
65
- const checkpoint = await store.get(envelope.id);
66
- const retryContext = new ResumableContext({
67
- store,
68
- envelope: createTestEnvelope(envelope.id, 2),
69
- subscriber,
70
- existingCheckpoint: checkpoint,
71
- });
72
- const result = await retryContext.io('step-1', () => {
73
- executeCount++;
74
- return 'should-not-be-used';
75
- });
76
- expect(result).toBe('result-1');
77
- expect(executeCount).toBe(1); // Still 1, not re-executed
78
- });
79
- it('should throw DuplicateIoKeyError on duplicate key within same execution', async () => {
80
- const context = new ResumableContext({
81
- store,
82
- envelope,
83
- subscriber,
84
- });
85
- await context.io('same-key', () => 'first');
86
- await expect(context.io('same-key', () => 'second')).rejects.toThrow(DuplicateIoKeyError);
87
- });
88
- it('should handle async functions', async () => {
89
- const context = new ResumableContext({
90
- store,
91
- envelope,
92
- subscriber,
93
- });
94
- const result = await context.io('async-step', async () => {
95
- await new Promise((resolve) => setTimeout(resolve, 10));
96
- return { value: 42 };
97
- });
98
- expect(result).toEqual({ value: 42 });
99
- });
100
- it('should not cache failed operations', async () => {
101
- const context = new ResumableContext({
102
- store,
103
- envelope,
104
- subscriber,
105
- });
106
- // First attempt - fails
107
- await expect(context.io('failing-step', () => {
108
- throw new Error('Operation failed');
109
- })).rejects.toThrow('Operation failed');
110
- // Checkpoint should not contain the failed step
111
- const checkpoint = await store.get(envelope.id);
112
- expect(checkpoint?.completedSteps['failing-step']).toBeUndefined();
113
- });
114
- it('should persist checkpoint after each io() call', async () => {
115
- const context = new ResumableContext({
116
- store,
117
- envelope,
118
- subscriber,
119
- });
120
- await context.io('step-1', () => 'result-1');
121
- let checkpoint = await store.get(envelope.id);
122
- if (!checkpoint)
123
- throw new Error('expected checkpoint after step-1');
124
- expect(Object.keys(checkpoint.completedSteps)).toHaveLength(1);
125
- await context.io('step-2', () => 'result-2');
126
- checkpoint = await store.get(envelope.id);
127
- if (!checkpoint)
128
- throw new Error('expected checkpoint after step-2');
129
- expect(Object.keys(checkpoint.completedSteps)).toHaveLength(2);
130
- });
131
- it('should support various JSON-serializable return types', async () => {
132
- const context = new ResumableContext({
133
- store,
134
- envelope,
135
- subscriber,
136
- });
137
- expect(await context.io('string', () => 'hello')).toBe('hello');
138
- expect(await context.io('number', () => 42)).toBe(42);
139
- expect(await context.io('boolean', () => true)).toBe(true);
140
- expect(await context.io('null', () => null)).toBe(null);
141
- expect(await context.io('array', () => [1, 2, 3])).toEqual([1, 2, 3]);
142
- expect(await context.io('object', () => ({ foo: 'bar' }))).toEqual({
143
- foo: 'bar',
144
- });
145
- });
146
- });
147
- describe('all()', () => {
148
- it('should execute multiple operations in parallel', async () => {
149
- const context = new ResumableContext({
150
- store,
151
- envelope,
152
- subscriber,
153
- });
154
- const executionOrder = [];
155
- const [a, b, c] = await context.all([
156
- [
157
- 'fetch-a',
158
- () => {
159
- executionOrder.push('a');
160
- return 'result-a';
161
- },
162
- ],
163
- [
164
- 'fetch-b',
165
- () => {
166
- executionOrder.push('b');
167
- return 'result-b';
168
- },
169
- ],
170
- [
171
- 'fetch-c',
172
- () => {
173
- executionOrder.push('c');
174
- return 'result-c';
175
- },
176
- ],
177
- ]);
178
- expect(a).toBe('result-a');
179
- expect(b).toBe('result-b');
180
- expect(c).toBe('result-c');
181
- expect(executionOrder).toHaveLength(3);
182
- });
183
- it('should cache all results on retry', async () => {
184
- // First execution
185
- const firstContext = new ResumableContext({
186
- store,
187
- envelope,
188
- subscriber,
189
- });
190
- let executeCount = 0;
191
- await firstContext.all([
192
- [
193
- 'fetch-a',
194
- () => {
195
- executeCount++;
196
- return 'result-a';
197
- },
198
- ],
199
- [
200
- 'fetch-b',
201
- () => {
202
- executeCount++;
203
- return 'result-b';
204
- },
205
- ],
206
- ]);
207
- expect(executeCount).toBe(2);
208
- // Retry with cached checkpoint
209
- const checkpoint = await store.get(envelope.id);
210
- const retryContext = new ResumableContext({
211
- store,
212
- envelope: createTestEnvelope(envelope.id, 2),
213
- subscriber,
214
- existingCheckpoint: checkpoint,
215
- });
216
- const [a, b] = await retryContext.all([
217
- [
218
- 'fetch-a',
219
- () => {
220
- executeCount++;
221
- return 'new-a';
222
- },
223
- ],
224
- [
225
- 'fetch-b',
226
- () => {
227
- executeCount++;
228
- return 'new-b';
229
- },
230
- ],
231
- ]);
232
- expect(a).toBe('result-a'); // Cached
233
- expect(b).toBe('result-b'); // Cached
234
- expect(executeCount).toBe(2); // Still 2, not re-executed
235
- });
236
- it('should throw on duplicate key in all()', async () => {
237
- const context = new ResumableContext({
238
- store,
239
- envelope,
240
- subscriber,
241
- });
242
- await expect(context.all([
243
- ['same-key', () => 'first'],
244
- ['same-key', () => 'second'],
245
- ])).rejects.toThrow(DuplicateIoKeyError);
246
- });
247
- it('should throw if all() key conflicts with previous io() key', async () => {
248
- const context = new ResumableContext({
249
- store,
250
- envelope,
251
- subscriber,
252
- });
253
- await context.io('used-key', () => 'first');
254
- await expect(context.all([
255
- ['used-key', () => 'second'],
256
- ['new-key', () => 'third'],
257
- ])).rejects.toThrow(DuplicateIoKeyError);
258
- });
259
- });
260
- describe('attempt and isRetry', () => {
261
- it('should report attempt number from envelope', async () => {
262
- const context = new ResumableContext({
263
- store,
264
- envelope: createTestEnvelope('test', 3),
265
- subscriber,
266
- });
267
- expect(context.attempt).toBe(3);
268
- });
269
- it('should report isRetry as false for first attempt', async () => {
270
- const context = new ResumableContext({
271
- store,
272
- envelope: createTestEnvelope('test', 1),
273
- subscriber,
274
- });
275
- expect(context.isRetry).toBe(false);
276
- });
277
- it('should report isRetry as true for subsequent attempts', async () => {
278
- const context = new ResumableContext({
279
- store,
280
- envelope: createTestEnvelope('test', 2),
281
- subscriber,
282
- });
283
- expect(context.isRetry).toBe(true);
284
- });
285
- });
286
- describe('clear()', () => {
287
- it('should delete checkpoint from store', async () => {
288
- const context = new ResumableContext({
289
- store,
290
- envelope,
291
- subscriber,
292
- });
293
- await context.io('step-1', () => 'result');
294
- expect(store.size).toBe(1);
295
- await context.clear();
296
- expect(store.size).toBe(0);
297
- });
298
- });
299
- describe('hooks', () => {
300
- it('should call onCheckpointHit when using cached value', async () => {
301
- const onCheckpointHit = vi.fn();
302
- // First execution - populate cache
303
- const firstContext = new ResumableContext({
304
- store,
305
- envelope,
306
- subscriber,
307
- });
308
- await firstContext.io('cached-step', () => 'cached-result');
309
- // Retry with hooks
310
- const checkpoint = await store.get(envelope.id);
311
- const retryContext = new ResumableContext({
312
- store,
313
- envelope: createTestEnvelope(envelope.id, 2),
314
- subscriber,
315
- existingCheckpoint: checkpoint,
316
- hooks: { onCheckpointHit },
317
- });
318
- await retryContext.io('cached-step', () => 'new-result');
319
- expect(onCheckpointHit).toHaveBeenCalledWith({
320
- envelope: expect.anything(),
321
- subscriber,
322
- stepKey: 'cached-step',
323
- });
324
- });
325
- it('should call onCheckpointMiss when executing fresh', async () => {
326
- const onCheckpointMiss = vi.fn();
327
- const context = new ResumableContext({
328
- store,
329
- envelope,
330
- subscriber,
331
- hooks: { onCheckpointMiss },
332
- });
333
- await context.io('fresh-step', () => 'result');
334
- expect(onCheckpointMiss).toHaveBeenCalledWith({
335
- envelope,
336
- subscriber,
337
- stepKey: 'fresh-step',
338
- });
339
- });
340
- });
341
- describe('real-world scenarios', () => {
342
- it('should replay cached steps and execute new ones on retry', async () => {
343
- const executionLog = [];
344
- let shouldFail = true;
345
- // First attempt - fails after step-1
346
- const firstContext = new ResumableContext({
347
- store,
348
- envelope,
349
- subscriber,
350
- });
351
- try {
352
- await firstContext.io('step-0', () => {
353
- executionLog.push('step-0');
354
- return 'result-0';
355
- });
356
- await firstContext.io('step-1', () => {
357
- executionLog.push('step-1');
358
- return 'result-1';
359
- });
360
- if (shouldFail) {
361
- shouldFail = false;
362
- throw new Error('Simulated failure');
363
- }
364
- await firstContext.io('step-2', () => {
365
- executionLog.push('step-2');
366
- return 'result-2';
367
- });
368
- }
369
- catch {
370
- // Expected failure
371
- }
372
- expect(executionLog).toEqual(['step-0', 'step-1']);
373
- // Retry - step-0 and step-1 use cache, step-2 executes
374
- executionLog.length = 0;
375
- const checkpoint = await store.get(envelope.id);
376
- const retryContext = new ResumableContext({
377
- store,
378
- envelope: createTestEnvelope(envelope.id, 2),
379
- subscriber,
380
- existingCheckpoint: checkpoint,
381
- });
382
- await retryContext.io('step-0', () => {
383
- executionLog.push('step-0');
384
- return 'should-not-use';
385
- });
386
- await retryContext.io('step-1', () => {
387
- executionLog.push('step-1');
388
- return 'should-not-use';
389
- });
390
- await retryContext.io('step-2', () => {
391
- executionLog.push('step-2');
392
- return 'result-2';
393
- });
394
- expect(executionLog).toEqual(['step-2']); // Only step-2 executed!
395
- });
396
- it('should handle conditional io() calls correctly', async () => {
397
- const context = new ResumableContext({
398
- store,
399
- envelope: { ...envelope, data: { sendEmail: true } },
400
- subscriber,
401
- });
402
- const result = await context.io('send-email', () => ({
403
- messageId: 'msg-123',
404
- }));
405
- expect(result).toEqual({ messageId: 'msg-123' });
406
- });
407
- it('should handle dynamic loop with unique keys', async () => {
408
- const context = new ResumableContext({
409
- store,
410
- envelope,
411
- subscriber,
412
- });
413
- const items = [
414
- { id: 'item-1', value: 10 },
415
- { id: 'item-2', value: 20 },
416
- { id: 'item-3', value: 30 },
417
- ];
418
- const results = [];
419
- for (const item of items) {
420
- const processed = await context.io(`process-${item.id}`, () => {
421
- return item.value * 2;
422
- });
423
- results.push(processed);
424
- }
425
- expect(results).toEqual([20, 40, 60]);
426
- });
427
- });
428
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=stores.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"stores.test.d.ts","sourceRoot":"","sources":["../../../src/checkpoint/stores/stores.test.ts"],"names":[],"mappings":""}
@@ -1,146 +0,0 @@
1
- import { beforeEach, describe, expect, it } from 'bun:test';
2
- import { MemoryCheckpointStore } from './memory.js';
3
- import { NoOpCheckpointStore } from './noop.js';
4
- describe('MemoryCheckpointStore', () => {
5
- let store;
6
- beforeEach(() => {
7
- store = new MemoryCheckpointStore();
8
- });
9
- describe('get()', () => {
10
- it('should return undefined for non-existent checkpoint', async () => {
11
- const result = await store.get('non-existent');
12
- expect(result).toBeUndefined();
13
- });
14
- it('should return stored checkpoint', async () => {
15
- const checkpoint = {
16
- envelopeId: 'test-id',
17
- subscriberName: 'test-subscriber',
18
- completedSteps: { 'step-1': 'result-1' },
19
- };
20
- await store.set('test-id', checkpoint);
21
- const result = await store.get('test-id');
22
- expect(result).toEqual(checkpoint);
23
- });
24
- });
25
- describe('set()', () => {
26
- it('should store checkpoint', async () => {
27
- const checkpoint = {
28
- envelopeId: 'test-id',
29
- subscriberName: 'test-subscriber',
30
- completedSteps: {},
31
- };
32
- await store.set('test-id', checkpoint);
33
- expect(store.size).toBe(1);
34
- });
35
- it('should overwrite existing checkpoint', async () => {
36
- const checkpoint1 = {
37
- envelopeId: 'test-id',
38
- subscriberName: 'test-subscriber',
39
- completedSteps: { 'step-1': 'old' },
40
- };
41
- const checkpoint2 = {
42
- envelopeId: 'test-id',
43
- subscriberName: 'test-subscriber',
44
- completedSteps: { 'step-1': 'new', 'step-2': 'added' },
45
- };
46
- await store.set('test-id', checkpoint1);
47
- await store.set('test-id', checkpoint2);
48
- const result = await store.get('test-id');
49
- expect(result).toEqual(checkpoint2);
50
- expect(store.size).toBe(1);
51
- });
52
- });
53
- describe('delete()', () => {
54
- it('should delete checkpoint', async () => {
55
- const checkpoint = {
56
- envelopeId: 'test-id',
57
- subscriberName: 'test-subscriber',
58
- completedSteps: {},
59
- };
60
- await store.set('test-id', checkpoint);
61
- expect(store.size).toBe(1);
62
- await store.delete('test-id');
63
- expect(store.size).toBe(0);
64
- });
65
- it('should not throw when deleting non-existent checkpoint', async () => {
66
- await expect(store.delete('non-existent')).resolves.toBeUndefined();
67
- });
68
- });
69
- describe('utility methods', () => {
70
- it('size should return number of checkpoints', async () => {
71
- expect(store.size).toBe(0);
72
- await store.set('id-1', {
73
- envelopeId: 'id-1',
74
- subscriberName: 'sub',
75
- completedSteps: {},
76
- });
77
- expect(store.size).toBe(1);
78
- await store.set('id-2', {
79
- envelopeId: 'id-2',
80
- subscriberName: 'sub',
81
- completedSteps: {},
82
- });
83
- expect(store.size).toBe(2);
84
- });
85
- it('clear() should remove all checkpoints', async () => {
86
- await store.set('id-1', {
87
- envelopeId: 'id-1',
88
- subscriberName: 'sub',
89
- completedSteps: {},
90
- });
91
- await store.set('id-2', {
92
- envelopeId: 'id-2',
93
- subscriberName: 'sub',
94
- completedSteps: {},
95
- });
96
- expect(store.size).toBe(2);
97
- store.clear();
98
- expect(store.size).toBe(0);
99
- });
100
- it('keys() should return all checkpoint IDs', async () => {
101
- await store.set('id-1', {
102
- envelopeId: 'id-1',
103
- subscriberName: 'sub',
104
- completedSteps: {},
105
- });
106
- await store.set('id-2', {
107
- envelopeId: 'id-2',
108
- subscriberName: 'sub',
109
- completedSteps: {},
110
- });
111
- const keys = store.keys();
112
- expect(keys).toContain('id-1');
113
- expect(keys).toContain('id-2');
114
- expect(keys).toHaveLength(2);
115
- });
116
- });
117
- });
118
- describe('NoOpCheckpointStore', () => {
119
- let store;
120
- beforeEach(() => {
121
- store = new NoOpCheckpointStore();
122
- });
123
- describe('get()', () => {
124
- it('should always return undefined', async () => {
125
- const result = await store.get('any-id');
126
- expect(result).toBeUndefined();
127
- });
128
- });
129
- describe('set()', () => {
130
- it('should not store anything (no-op)', async () => {
131
- const checkpoint = {
132
- envelopeId: 'test-id',
133
- subscriberName: 'test-subscriber',
134
- completedSteps: { 'step-1': 'result-1' },
135
- };
136
- await store.set('test-id', checkpoint);
137
- const result = await store.get('test-id');
138
- expect(result).toBeUndefined();
139
- });
140
- });
141
- describe('delete()', () => {
142
- it('should not throw', async () => {
143
- await expect(store.delete('any-id')).resolves.toBeUndefined();
144
- });
145
- });
146
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=rabbitmq-codec.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"rabbitmq-codec.test.d.ts","sourceRoot":"","sources":["../../src/codec/rabbitmq-codec.test.ts"],"names":[],"mappings":""}