@nathapp/nestjs-outbox 4.0.0 → 4.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.
Files changed (42) hide show
  1. package/package.json +6 -6
  2. package/.eslintrc.js +0 -8
  3. package/.nax/features/nestjs-outbox/.nax-acceptance.test.ts +0 -878
  4. package/.turbo/turbo-build.log +0 -4
  5. package/CHANGELOG.md +0 -43
  6. package/lib/constant/index.ts +0 -1
  7. package/lib/constant/outbox.constants.ts +0 -3
  8. package/lib/exception/index.ts +0 -3
  9. package/lib/exception/outbox-exception-code.enum.ts +0 -4
  10. package/lib/exception/outbox-invalid-event.exception.ts +0 -9
  11. package/lib/exception/outbox-misconfigured.exception.ts +0 -9
  12. package/lib/index.ts +0 -6
  13. package/lib/interface/index.ts +0 -18
  14. package/lib/interface/outbox-module-options.interface.ts +0 -18
  15. package/lib/interface/outbox-publisher.interface.ts +0 -5
  16. package/lib/interface/outbox-record.interface.ts +0 -23
  17. package/lib/interface/outbox-status.enum.ts +0 -6
  18. package/lib/interface/outbox-store.interface.ts +0 -23
  19. package/lib/module/index.ts +0 -1
  20. package/lib/module/outbox.module.ts +0 -64
  21. package/lib/provider/in-memory-outbox-store.ts +0 -114
  22. package/lib/provider/index.ts +0 -4
  23. package/lib/provider/outbox-relay.ts +0 -147
  24. package/lib/provider/outbox.service.ts +0 -70
  25. package/lib/provider/resolve-outbox-options.ts +0 -51
  26. package/lib/util/compute-backoff.ts +0 -4
  27. package/lib/util/index.ts +0 -1
  28. package/test/barrel/index.spec.ts +0 -45
  29. package/test/constant/outbox.constants.spec.ts +0 -20
  30. package/test/e2e/outbox.module.spec.ts +0 -371
  31. package/test/exception/outbox.exceptions.spec.ts +0 -38
  32. package/test/interface/outbox-status.enum.spec.ts +0 -19
  33. package/test/jest-nax-setup.js +0 -31
  34. package/test/jest-nax.json +0 -30
  35. package/test/jest-test.json +0 -28
  36. package/test/module/outbox.module.spec.ts +0 -139
  37. package/test/provider/in-memory-outbox-store.spec.ts +0 -216
  38. package/test/provider/outbox-relay.spec.ts +0 -565
  39. package/test/provider/outbox-service.spec.ts +0 -229
  40. package/test/provider/resolve-outbox-options.spec.ts +0 -50
  41. package/test/util/compute-backoff.spec.ts +0 -25
  42. package/tsconfig.json +0 -9
@@ -1,139 +0,0 @@
1
- import { Test } from '@nestjs/testing';
2
- import { OutboxModule } from '../../lib/module/outbox.module';
3
- import { OutboxService } from '../../lib/provider/outbox.service';
4
- import { OutboxRelay } from '../../lib/provider/outbox-relay';
5
- import {
6
- OUTBOX_MODULE_OPTIONS,
7
- OUTBOX_STORE,
8
- } from '../../lib/constant/outbox.constants';
9
- import { InMemoryOutboxStore } from '../../lib/provider/in-memory-outbox-store';
10
- import { IOutboxStore } from '../../lib/interface/outbox-store.interface';
11
- import { IOutboxPublisher } from '../../lib/interface/outbox-publisher.interface';
12
-
13
- const stubPublisher = (): IOutboxPublisher => ({
14
- publish: jest.fn().mockResolvedValue(undefined),
15
- });
16
-
17
- const safeGet = <T>(moduleRef: { get: (token: unknown) => T }, token: unknown): T | undefined => {
18
- try {
19
- return moduleRef.get(token);
20
- } catch {
21
- return undefined;
22
- }
23
- };
24
-
25
- describe('OutboxModule — register / registerAsync', () => {
26
- describe('AC1: register() returns a global dynamic module keyed to OutboxModule', () => {
27
- it('AC1: register({ publisher }) returns a DynamicModule with global === true and module === OutboxModule', () => {
28
- const publisher = stubPublisher();
29
- const dynamicModule = OutboxModule.register({ publisher }) as {
30
- global?: boolean;
31
- module?: unknown;
32
- };
33
-
34
- expect(dynamicModule).toBeDefined();
35
- expect(dynamicModule.global).toBe(true);
36
- expect(dynamicModule.module).toBe(OutboxModule);
37
- });
38
-
39
- it('AC1 (boundary): register() with no arguments also returns global === true and module === OutboxModule', () => {
40
- const dynamicModule = OutboxModule.register() as {
41
- global?: boolean;
42
- module?: unknown;
43
- };
44
-
45
- expect(dynamicModule.global).toBe(true);
46
- expect(dynamicModule.module).toBe(OutboxModule);
47
- });
48
- });
49
-
50
- describe('AC2: register({ publisher }) resolves OUTBOX_STORE to an InMemoryOutboxStore', () => {
51
- it('AC2: a testing module built from register({ publisher }) resolves OUTBOX_STORE to InMemoryOutboxStore', async () => {
52
- const publisher = stubPublisher();
53
-
54
- const moduleRef = await Test.createTestingModule({
55
- imports: [OutboxModule.register({ publisher })],
56
- }).compile();
57
-
58
- const store = safeGet<IOutboxStore>(moduleRef, OUTBOX_STORE);
59
- expect(store).toBeDefined();
60
- expect(store).toBeInstanceOf(InMemoryOutboxStore);
61
-
62
- await moduleRef.close();
63
- });
64
- });
65
-
66
- describe('AC3: register({ store, publisher }) resolves OUTBOX_STORE to the custom instance (reference-equal)', () => {
67
- it('AC3: a custom store passed via register({ store, publisher }) is exposed under OUTBOX_STORE reference-equal', async () => {
68
- const customStore: IOutboxStore = {
69
- save: jest.fn().mockResolvedValue(undefined),
70
- claimBatch: jest.fn().mockResolvedValue([]),
71
- markPublished: jest.fn().mockResolvedValue(undefined),
72
- markRetry: jest.fn().mockResolvedValue(undefined),
73
- markDead: jest.fn().mockResolvedValue(undefined),
74
- };
75
- const publisher = stubPublisher();
76
-
77
- const moduleRef = await Test.createTestingModule({
78
- imports: [OutboxModule.register({ store: customStore, publisher })],
79
- }).compile();
80
-
81
- const resolved = safeGet<IOutboxStore>(moduleRef, OUTBOX_STORE);
82
- expect(resolved).toBe(customStore);
83
-
84
- await moduleRef.close();
85
- });
86
- });
87
-
88
- describe('Module exports: register() and registerAsync() expose OutboxService and OutboxRelay so importing modules can inject them', () => {
89
- it('register({ publisher }) returns a DynamicModule whose exports include OutboxService and OutboxRelay', () => {
90
- const publisher = stubPublisher();
91
- const dynamicModule = OutboxModule.register({ publisher }) as {
92
- exports?: unknown[];
93
- };
94
-
95
- const exports = dynamicModule.exports ?? [];
96
- expect(exports).toContain(OutboxService);
97
- expect(exports).toContain(OutboxRelay);
98
- });
99
-
100
- it('registerAsync({ useFactory }) returns a DynamicModule whose exports include OutboxService and OutboxRelay', () => {
101
- const publisher = stubPublisher();
102
- const dynamicModule = OutboxModule.registerAsync({
103
- inject: [],
104
- useFactory: () => ({ publisher }),
105
- }) as { exports?: unknown[] };
106
-
107
- const exports = dynamicModule.exports ?? [];
108
- expect(exports).toContain(OutboxService);
109
- expect(exports).toContain(OutboxRelay);
110
- });
111
- });
112
-
113
- describe('AC4: registerAsync({ useFactory }) builds and the resolved module options expose factory-supplied values', () => {
114
- it('AC4: registerAsync with useFactory returning { publisher, relay: { pollIntervalMs: 111 } } builds and OUTBOX_MODULE_OPTIONS.relay.pollIntervalMs === 111', async () => {
115
- const publisher = stubPublisher();
116
-
117
- const moduleRef = await Test.createTestingModule({
118
- imports: [
119
- OutboxModule.registerAsync({
120
- inject: [],
121
- useFactory: () => ({
122
- publisher,
123
- relay: { pollIntervalMs: 111 },
124
- }),
125
- }),
126
- ],
127
- }).compile();
128
-
129
- const resolved = safeGet<{ relay: { pollIntervalMs?: number } }>(
130
- moduleRef,
131
- OUTBOX_MODULE_OPTIONS,
132
- );
133
- expect(resolved).toBeDefined();
134
- expect(resolved!.relay.pollIntervalMs).toBe(111);
135
-
136
- await moduleRef.close();
137
- });
138
- });
139
- });
@@ -1,216 +0,0 @@
1
- import { InMemoryOutboxStore } from '../../lib/provider/in-memory-outbox-store';
2
- import { OutboxRecord } from '../../lib/interface/outbox-record.interface';
3
- import { OutboxStatus } from '../../lib/interface/outbox-status.enum';
4
-
5
- const record = (overrides: Partial<OutboxRecord> = {}): OutboxRecord => ({
6
- id: 'rec-1',
7
- type: 'order.created',
8
- payload: { orderId: 'o-1' },
9
- status: OutboxStatus.PENDING,
10
- attempts: 0,
11
- createdAt: new Date('2026-01-01T00:00:00.000Z'),
12
- nextAttemptAt: new Date('2026-01-01T00:00:00.000Z'),
13
- ...overrides,
14
- });
15
-
16
- describe('InMemoryOutboxStore', () => {
17
- describe('AC1: claimable PENDING record is returned by claimBatch', () => {
18
- it('returns a PENDING record whose nextAttemptAt equals now', async () => {
19
- const store = new InMemoryOutboxStore();
20
- const now = new Date('2026-01-01T00:00:00.000Z');
21
- const rec = record({ id: 'r1', nextAttemptAt: now });
22
- await store.save(rec, {});
23
-
24
- const claimed = await store.claimBatch(10, 30000, now, 'owner-1');
25
-
26
- expect(claimed).toHaveLength(1);
27
- expect(claimed[0].id).toBe('r1');
28
- });
29
- });
30
-
31
- describe('AC2: claimed records carry PROCESSING status, owner, and leaseUntil = now + leaseMs', () => {
32
- it('marks the returned record as PROCESSING with owner and leaseUntil at now+30000', async () => {
33
- const store = new InMemoryOutboxStore();
34
- const now = new Date('2026-01-01T00:00:00.000Z');
35
- const rec = record({ id: 'r1', nextAttemptAt: now });
36
- await store.save(rec, {});
37
-
38
- const claimed = await store.claimBatch(10, 30000, now, 'owner-1');
39
-
40
- expect(claimed).toHaveLength(1);
41
- expect(claimed[0].status).toBe(OutboxStatus.PROCESSING);
42
- expect(claimed[0].owner).toBe('owner-1');
43
- expect(claimed[0].leaseUntil).toBeInstanceOf(Date);
44
- expect(claimed[0].leaseUntil!.getTime()).toBe(now.getTime() + 30000);
45
- });
46
- });
47
-
48
- describe('AC3: claimBatch returns at most `limit` records', () => {
49
- it('returns exactly 2 records when 5 are saved and limit=2', async () => {
50
- const store = new InMemoryOutboxStore();
51
- const now = new Date('2026-01-01T00:00:00.000Z');
52
- for (let i = 0; i < 5; i++) {
53
- await store.save(record({ id: `r${i}`, nextAttemptAt: now }), {});
54
- }
55
-
56
- const claimed = await store.claimBatch(2, 30000, now, 'o');
57
-
58
- expect(claimed).toHaveLength(2);
59
- });
60
- });
61
-
62
- describe('AC4: PENDING record with nextAttemptAt strictly after now is not returned', () => {
63
- it('does not return a PENDING record scheduled in the future', async () => {
64
- const store = new InMemoryOutboxStore();
65
- const now = new Date('2026-01-01T00:00:00.000Z');
66
- const future = new Date('2026-01-01T00:00:05.000Z');
67
- await store.save(record({ id: 'future', nextAttemptAt: future }), {});
68
-
69
- const claimed = await store.claimBatch(10, 30000, now, 'o');
70
-
71
- expect(claimed).toHaveLength(0);
72
- });
73
- });
74
-
75
- describe('AC5: claimed record is exclusive — second claimBatch by another owner does not return it', () => {
76
- it('does not re-claim a record currently leased by owner o1', async () => {
77
- const store = new InMemoryOutboxStore();
78
- const now = new Date('2026-01-01T00:00:00.000Z');
79
- await store.save(record({ id: 'r1', nextAttemptAt: now }), {});
80
-
81
- const first = await store.claimBatch(10, 30000, now, 'o1');
82
- expect(first.map((r) => r.id)).toEqual(['r1']);
83
-
84
- const second = await store.claimBatch(10, 30000, now, 'o2');
85
- expect(second.map((r) => r.id)).toEqual([]);
86
- });
87
- });
88
-
89
- describe('AC6: PROCESSING record with expired lease is reclaimable', () => {
90
- it('returns a PROCESSING record whose leaseUntil <= now2', async () => {
91
- const store = new InMemoryOutboxStore();
92
- const now1 = new Date('2026-01-01T00:00:00.000Z');
93
- const now2 = new Date('2026-01-01T00:01:00.000Z');
94
- await store.save(record({ id: 'r1', nextAttemptAt: now1 }), {});
95
-
96
- const first = await store.claimBatch(10, 30000, now1, 'o1');
97
- expect(first.map((r) => r.id)).toEqual(['r1']);
98
-
99
- const reclaimed = await store.claimBatch(10, 30000, now2, 'o3');
100
- expect(reclaimed.map((r) => r.id)).toEqual(['r1']);
101
- expect(reclaimed[0].owner).toBe('o3');
102
- });
103
- });
104
-
105
- describe('AC7: markPublished transitions to PUBLISHED and removes from future claim windows', () => {
106
- it('sets status=PUBLISHED, sets publishedAt, and is never re-claimed', async () => {
107
- const store = new InMemoryOutboxStore();
108
- const now = new Date('2026-01-01T00:00:00.000Z');
109
- const later = new Date('2026-01-01T00:05:00.000Z');
110
- const publishedAt = new Date('2026-01-01T00:00:01.000Z');
111
- await store.save(record({ id: 'r1', nextAttemptAt: now }), {});
112
-
113
- const claimed = await store.claimBatch(10, 30000, now, 'o');
114
- expect(claimed.map((r) => r.id)).toEqual(['r1']);
115
-
116
- await store.markPublished('r1', publishedAt, 'o');
117
-
118
- const internal = (store as unknown as { map: Map<string, OutboxRecord> }).map;
119
- expect(internal.get('r1')!.status).toBe(OutboxStatus.PUBLISHED);
120
- expect(internal.get('r1')!.publishedAt).toBe(publishedAt);
121
-
122
- const after = await store.claimBatch(10, 30000, later, 'o');
123
- expect(after.map((r) => r.id)).toEqual([]);
124
- });
125
- });
126
-
127
- describe('AC8: markRetry transitions PROCESSING -> PENDING with attempts and nextAttemptAt', () => {
128
- it('schedules the record for future and only claims after the scheduled time', async () => {
129
- const store = new InMemoryOutboxStore();
130
- const now = new Date('2026-01-01T00:00:00.000Z');
131
- const future = new Date('2026-01-01T00:00:30.000Z');
132
- const afterFuture = new Date('2026-01-01T00:00:31.000Z');
133
- await store.save(record({ id: 'r1', nextAttemptAt: now }), {});
134
-
135
- const claimed = await store.claimBatch(10, 30000, now, 'o');
136
- expect(claimed.map((r) => r.id)).toEqual(['r1']);
137
-
138
- await store.markRetry('r1', 3, future, 'o');
139
-
140
- const internal = (store as unknown as { map: Map<string, OutboxRecord> }).map;
141
- expect(internal.get('r1')!.status).toBe(OutboxStatus.PENDING);
142
- expect(internal.get('r1')!.attempts).toBe(3);
143
- expect(internal.get('r1')!.nextAttemptAt).toBe(future);
144
-
145
- const beforeFuture = await store.claimBatch(10, 30000, now, 'o');
146
- expect(beforeFuture.map((r) => r.id)).toEqual([]);
147
-
148
- const afterFutureClaim = await store.claimBatch(10, 30000, afterFuture, 'o');
149
- expect(afterFutureClaim.map((r) => r.id)).toEqual(['r1']);
150
- });
151
- });
152
-
153
- describe('AC9: markDead transitions to DEAD and removes from future claim windows', () => {
154
- it('sets status=DEAD and attempts=10, and is never re-claimed', async () => {
155
- const store = new InMemoryOutboxStore();
156
- const now = new Date('2026-01-01T00:00:00.000Z');
157
- const later = new Date('2026-01-01T00:05:00.000Z');
158
- await store.save(record({ id: 'r1', nextAttemptAt: now }), {});
159
-
160
- const claimed = await store.claimBatch(10, 30000, now, 'o');
161
- expect(claimed.map((r) => r.id)).toEqual(['r1']);
162
-
163
- await store.markDead('r1', 10, 'o');
164
-
165
- const internal = (store as unknown as { map: Map<string, OutboxRecord> }).map;
166
- expect(internal.get('r1')!.status).toBe(OutboxStatus.DEAD);
167
- expect(internal.get('r1')!.attempts).toBe(10);
168
-
169
- const after = await store.claimBatch(10, 30000, later, 'o');
170
- expect(after.map((r) => r.id)).toEqual([]);
171
- });
172
- });
173
-
174
- describe('AC11: lease fencing — terminal transitions reject a stale lease owner', () => {
175
- it('rejects markPublished/markRetry/markDead when the owner no longer holds the lease', async () => {
176
- const store = new InMemoryOutboxStore();
177
- const now = new Date('2026-01-01T00:00:00.000Z');
178
- const later = new Date('2026-01-01T00:05:00.000Z');
179
- await store.save(record({ id: 'r1', nextAttemptAt: now }), {});
180
-
181
- await store.claimBatch(10, 30000, now, 'o1');
182
- // lease expires and a second owner reclaims the record
183
- await store.claimBatch(10, 30000, later, 'o2');
184
-
185
- await expect(store.markPublished('r1', later, 'o1')).rejects.toThrow(
186
- /no longer held by lease owner/,
187
- );
188
- await expect(store.markRetry('r1', 1, later, 'o1')).rejects.toThrow(
189
- /no longer held by lease owner/,
190
- );
191
- await expect(store.markDead('r1', 1, 'o1')).rejects.toThrow(
192
- /no longer held by lease owner/,
193
- );
194
-
195
- const internal = (store as unknown as { map: Map<string, OutboxRecord> }).map;
196
- expect(internal.get('r1')!.owner).toBe('o2');
197
- });
198
- });
199
-
200
- describe('AC10: non-JSON payload (Date) survives save + claim without round-tripping', () => {
201
- it('exposes the same Date instance after claim, demonstrating the in-memory limitation', async () => {
202
- const store = new InMemoryOutboxStore();
203
- const now = new Date('2026-01-01T00:00:00.000Z');
204
- const payloadDate = new Date('2026-06-15T12:34:56.000Z');
205
- await store.save(record({ id: 'r1', nextAttemptAt: now, payload: { when: payloadDate } }), {});
206
-
207
- const claimed = await store.claimBatch(10, 30000, now, 'o');
208
-
209
- expect(claimed).toHaveLength(1);
210
- const claimedPayload = claimed[0].payload as { when: Date };
211
- expect(claimedPayload.when).toBeInstanceOf(Date);
212
- expect(claimedPayload.when).toBe(payloadDate);
213
- expect(claimedPayload.when.getTime()).toBe(payloadDate.getTime());
214
- });
215
- });
216
- });