-
Notifications
You must be signed in to change notification settings - Fork 272
Expand file tree
/
Copy pathstore.ts
More file actions
655 lines (573 loc) · 21.3 KB
/
Copy pathstore.ts
File metadata and controls
655 lines (573 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/**
* @module webhooks/store
*
* ## Two webhook storage subsystems — relationship and rationale
*
* This codebase contains **two distinct webhook-related storage paths**:
*
* ### 1. `WebhookDeliveryStore` (this file)
*
* Backs the *management/inspection HTTP routes* in `src/routes/webhooks.ts`:
* - `GET /internal/webhooks/deliveries` — delivery tracking records
* - `GET /internal/webhooks/outbox` — outbound items queued via `/queue`
* - `GET /internal/webhooks/dlq` — dead-letter queue for failed deliveries
* - `POST /internal/webhooks/queue` — enqueue a new delivery for HTTP push
*
* The **default implementation** (`WebhookDeliveryStore`) is fully in-memory
* and is intentionally kept for **development / test environments** where
* zero infrastructure dependencies is more valuable than durability.
*
* In production, set `WEBHOOK_DELIVERY_STORE=postgres` to activate
* `PgWebhookDeliveryStore` (see `src/webhooks/pgStore.ts`), which persists
* outbox items and DLQ entries in Postgres so they survive restarts and are
* visible across all replicas. A startup warning is emitted when the
* process boots with `NODE_ENV=production` and this flag is absent or set to
* `"memory"`.
*
* ### 2. `WebhookDispatcher` / `webhook_outbox` Postgres table
*
* Lives in `src/webhooks/service.ts` (`WebhookDispatcher` class) and is a
* completely separate subsystem. It implements a transactional outbox pattern
* for **stream-event fanout**: when a stream event is written to Postgres, a
* corresponding row is inserted into `webhook_outbox` in the *same
* transaction*. The dispatcher polls that table and pushes the events to
* registered consumer endpoints via HTTPS.
*
* This path is durable by design — it was the DB-backed outbox all along.
* `WebhookDeliveryStore` is *not* a replacement for it; it handles a
* different, higher-level concern (delivery status tracking and the operator
* DLQ/inspection surface).
*
* ### Summary
*
* | Path | Backed by | Durable? | Purpose |
* |------------------------|-------------------------|----------|----------------------------------|
* | `WebhookDeliveryStore` | In-memory Maps (default)| ❌ / ✅* | Mgmt routes: track, DLQ, queue |
* | `PgWebhookDeliveryStore`| Postgres tables | ✅ | Same, but durable across restarts|
* | `WebhookDispatcher` | `webhook_outbox` table | ✅ | Stream-event fanout via Postgres |
*
* *Set `WEBHOOK_DELIVERY_STORE=postgres` to switch to the durable path.
*/
import type { WebhookDelivery, WebhookDeliveryStatus, DLQReasonCode } from './types.js';
import { logger } from '../lib/logger.js';
export interface DeadLetterQueueItem {
id: string;
deliveryId: string;
eventId: string;
eventType: string;
endpointUrl: string;
payload: string;
originalDelivery: WebhookDelivery;
failureReason: string;
reasonCode: DLQReasonCode;
createdAt: number;
processedAt?: number;
}
export type OutboxItemStatus = 'pending' | 'in_flight' | 'delivered' | 'failed';
export interface ClaimOptions {
workerId?: string;
lockTimeoutMs?: number;
now?: number;
}
export interface OutboxItem {
id: string;
deliveryId: string;
eventId: string;
eventType: string;
endpointUrl: string;
payload: string;
secret: string;
priority: 'high' | 'normal' | 'low';
createdAt: number;
scheduledFor: number;
attempts: number;
maxAttempts: number;
/** Defaults to `'pending'` when not provided to `addToOutbox`. */
status?: OutboxItemStatus;
lockedAt?: number;
lockedBy?: string;
}
export const DEFAULT_CLAIM_LOCK_TIMEOUT_MS = 30_000;
// ─────────────────────────────────────────────────────────────────────────────
// IWebhookDeliveryStore — shared interface implemented by both the in-memory
// store (development) and the Postgres-backed store (production).
// ─────────────────────────────────────────────────────────────────────────────
/**
* Contract for webhook delivery storage. Both `WebhookDeliveryStore`
* (in-memory) and `PgWebhookDeliveryStore` (Postgres) implement this
* interface, allowing callers (service.ts, routes/webhooks.ts) to be
* storage-agnostic.
*/
export interface IWebhookDeliveryStore {
store(delivery: WebhookDelivery): void;
get(id: string): WebhookDelivery | undefined;
getByDeliveryId(deliveryId: string): WebhookDelivery | undefined;
updateStatus(id: string, status: WebhookDeliveryStatus): void;
addToOutbox(item: Omit<OutboxItem, 'id' | 'status'>): string;
getReadyOutboxItems(now?: number): OutboxItem[];
removeFromOutbox(id: string): boolean;
updateOutboxItemAttempt(id: string, attempts: number): void;
claimReadyOutboxItems(opts?: ClaimOptions): OutboxItem[];
reclaimStuckItems(opts?: ClaimOptions): OutboxItem[];
releaseOutboxItem(id: string, workerId: string): boolean;
markOutboxItemDelivered(id: string, workerId: string): boolean;
addToDeadLetterQueue(delivery: WebhookDelivery, failureReason: string, reasonCode?: DLQReasonCode): string;
getDeadLetterQueueItems(limit?: number): DeadLetterQueueItem[];
processDeadLetterQueueItem(id: string, processedAt?: number): boolean;
getPendingRetries(now?: number): WebhookDelivery[];
getByEventId(eventId: string): WebhookDelivery[];
registerDeliveryId(deliveryId: string): void;
isDuplicateDelivery(deliveryId: string): boolean;
getMetrics(): { totalDeliveries: number; successfulDeliveries: number; failedDeliveries: number; dlqItems: number; outboxItems: number };
cleanup(olderThanMs?: number): { cleaned: number; errors: string[] };
clear(): void;
getAll(): WebhookDelivery[];
getAllOutboxItems(): OutboxItem[];
}
// ─────────────────────────────────────────────────────────────────────────────
// In-memory implementation (development / test default)
// ─────────────────────────────────────────────────────────────────────────────
/**
* In-memory implementation of `IWebhookDeliveryStore`.
*
* Suitable for **development and testing** where infrastructure dependencies
* should be minimal. All state is lost on process restart.
*
* **Do not use in production** — set `WEBHOOK_DELIVERY_STORE=postgres` to
* activate the durable Postgres-backed implementation instead. A startup
* warning is logged automatically when this implementation is active in a
* production environment.
*/
export class WebhookDeliveryStore implements IWebhookDeliveryStore {
// Main delivery storage
private deliveries: Map<string, WebhookDelivery> = new Map();
private deliveryIdIndex: Map<string, string> = new Map();
// Outbox pattern for reliable delivery
private outbox: Map<string, OutboxItem> = new Map();
private outboxPriorityQueue: Map<string, OutboxItem[]> = new Map();
// Dead-letter queue for failed deliveries
private deadLetterQueue: Map<string, DeadLetterQueueItem> = new Map();
// Metrics
private metrics = {
totalDeliveries: 0,
successfulDeliveries: 0,
failedDeliveries: 0,
dlqItems: 0,
outboxItems: 0,
};
/**
* Store a webhook delivery record
*/
store(delivery: WebhookDelivery): void {
this.deliveries.set(delivery.id, delivery);
this.deliveryIdIndex.set(delivery.deliveryId, delivery.id);
this.metrics.totalDeliveries++;
logger.debug('Webhook delivery stored', undefined, {
deliveryId: delivery.deliveryId,
status: delivery.status,
});
}
/**
* Get a delivery by its ID
*/
get(id: string): WebhookDelivery | undefined {
return this.deliveries.get(id);
}
/**
* Get a delivery by its deliveryId (for deduplication)
*/
getByDeliveryId(deliveryId: string): WebhookDelivery | undefined {
const id = this.deliveryIdIndex.get(deliveryId);
return id ? this.deliveries.get(id) : undefined;
}
/**
* Update delivery status and track metrics
*/
updateStatus(id: string, status: WebhookDeliveryStatus): void {
const delivery = this.deliveries.get(id);
if (delivery) {
const oldStatus = delivery.status;
delivery.status = status;
delivery.updatedAt = Date.now();
// Update metrics
if (oldStatus !== 'delivered' && status === 'delivered') {
this.metrics.successfulDeliveries++;
} else if (oldStatus !== 'permanent_failure' && status === 'permanent_failure') {
this.metrics.failedDeliveries++;
}
logger.debug('Webhook delivery status updated', undefined, {
deliveryId: delivery.deliveryId,
oldStatus,
newStatus: status,
});
}
}
/**
* Add item to outbox for reliable delivery
*/
addToOutbox(item: Omit<OutboxItem, 'id' | 'status'>): string {
const id = `outbox_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
const outboxItem: OutboxItem = { ...item, id, status: 'pending' };
this.outbox.set(id, outboxItem);
// Add to priority queue
const priority = outboxItem.priority;
if (!this.outboxPriorityQueue.has(priority)) {
this.outboxPriorityQueue.set(priority, []);
}
this.outboxPriorityQueue.get(priority)!.push(outboxItem);
this.metrics.outboxItems++;
logger.info('Item added to webhook outbox', undefined, {
outboxId: id,
deliveryId: item.deliveryId,
priority,
scheduledFor: new Date(item.scheduledFor).toISOString(),
});
return id;
}
/**
* Hydrate a full outbox item (with pre-existing id and status) into the
* in-memory mirror. Used by `PgWebhookDeliveryStore.hydrate()` to
* restore persisted rows on startup.
*
* This method intentionally accepts the complete `OutboxItem` including
* `id` and `status` — it is **not** a general-purpose enqueue and
* should only be called during hydration.
*/
hydrateOutboxItem(item: OutboxItem): string {
this.outbox.set(item.id, item);
const priority = item.priority;
if (!this.outboxPriorityQueue.has(priority)) {
this.outboxPriorityQueue.set(priority, []);
}
this.outboxPriorityQueue.get(priority)!.push(item);
this.metrics.outboxItems++;
return item.id;
}
/**
* Get items from outbox that are ready for processing (pending status only).
* Items claimed by a worker (in_flight) are excluded.
*/
getReadyOutboxItems(now: number = Date.now()): OutboxItem[] {
const readyItems: OutboxItem[] = [];
// Process by priority: high -> normal -> low
const priorities = ['high', 'normal', 'low'];
for (const priority of priorities) {
const items = this.outboxPriorityQueue.get(priority) || [];
const ready = items
.filter((item) =>
item.status === 'pending' &&
item.scheduledFor <= now &&
item.attempts < item.maxAttempts
)
.sort((a, b) => a.scheduledFor - b.scheduledFor);
readyItems.push(...ready);
}
return readyItems;
}
/**
* Remove item from outbox
*/
removeFromOutbox(id: string): boolean {
const item = this.outbox.get(id);
if (!item) return false;
this.outbox.delete(id);
// Remove from priority queue
const priorityItems = this.outboxPriorityQueue.get(item.priority);
if (priorityItems) {
const index = priorityItems.findIndex((i) => i.id === id);
if (index !== -1) {
priorityItems.splice(index, 1);
}
}
this.metrics.outboxItems--;
return true;
}
/**
* Update outbox item attempt count
*/
updateOutboxItemAttempt(id: string, attempts: number): void {
const item = this.outbox.get(id);
if (item) {
item.attempts = attempts;
}
}
/**
* Atomically claim ready outbox items for a specific worker.
*
* Finds items with `status = 'pending'` that are due (scheduledFor ≤ now
* and attempts < maxAttempts), then marks them `in_flight` with the
* given `workerId` and a `lockedAt` timestamp. Multiple workers calling
* this method on the same store will never both claim the same row
* because the `pending` → `in_flight` transition is synchronous — once
* the first caller mutates the status, subsequent callers skip the row.
*
* @returns The list of items successfully claimed by this worker.
*/
claimReadyOutboxItems(opts: ClaimOptions = { workerId: 'default-worker' }): OutboxItem[] {
const workerId = opts.workerId ?? 'default-worker';
const lockTimeoutMs = opts.lockTimeoutMs ?? DEFAULT_CLAIM_LOCK_TIMEOUT_MS;
const now = opts.now ?? Date.now();
const reclaimWindow = now - lockTimeoutMs;
const claimed: OutboxItem[] = [];
const priorities: ('high' | 'normal' | 'low')[] = ['high', 'normal', 'low'];
for (const priority of priorities) {
const items = this.outboxPriorityQueue.get(priority) || [];
for (const item of items) {
if (item.attempts >= item.maxAttempts) continue;
// Claim eligible items: pending or stuck in_flight with expired lock
const isPending = item.status === 'pending' && item.scheduledFor <= now;
const isStuck = item.status === 'in_flight' &&
item.lockedAt != null &&
item.lockedAt < reclaimWindow;
if (!isPending && !isStuck) continue;
item.status = 'in_flight';
item.lockedAt = now;
item.lockedBy = workerId;
claimed.push(item);
}
}
return claimed;
}
/**
* Reclaim stuck in-flight items whose lock has expired.
*
* Finds items with `status = 'in_flight'` where
* `lockedAt + lockTimeoutMs < now` and reassigns the lock to the
* requesting worker. This is a subset of what {@link claimReadyOutboxItems}
* does (which also handles `pending` items), but is exposed as a
* separate method for observability and targeted use cases.
*/
reclaimStuckItems(opts: ClaimOptions = { workerId: 'default-worker' }): OutboxItem[] {
const workerId = opts.workerId ?? 'default-worker';
const lockTimeoutMs = opts.lockTimeoutMs ?? DEFAULT_CLAIM_LOCK_TIMEOUT_MS;
const now = opts.now ?? Date.now();
const reclaimWindow = now - lockTimeoutMs;
const reclaimed: OutboxItem[] = [];
for (const item of this.outbox.values()) {
if (
item.status === 'in_flight' &&
item.lockedAt != null &&
item.lockedAt < reclaimWindow
) {
item.status = 'in_flight';
item.lockedAt = now;
item.lockedBy = workerId;
reclaimed.push(item);
}
}
return reclaimed;
}
/**
* Release a claimed outbox item so it can be claimed by another worker.
*
* Only the worker that currently holds the lock can release it (verified
* via `lockedBy`). Resets status to `pending` and clears lock fields.
*
* @returns `true` if the item was released, `false` if not found or
* the caller does not hold the lock.
*/
releaseOutboxItem(id: string, workerId: string): boolean {
const item = this.outbox.get(id);
if (!item) return false;
if (item.lockedBy !== workerId) return false;
item.status = 'pending';
item.lockedAt = undefined;
item.lockedBy = undefined;
return true;
}
/**
* Mark a claimed outbox item as delivered and remove it from the queue.
*
* Only the locking worker may mark the item as delivered.
*
* @returns `true` if the item was delivered, `false` if not found or
* the caller does not hold the lock.
*/
markOutboxItemDelivered(id: string, workerId: string): boolean {
const item = this.outbox.get(id);
if (!item) return false;
if (item.lockedBy !== workerId) return false;
item.status = 'delivered';
item.lockedAt = undefined;
item.lockedBy = undefined;
// Remove from outbox Map
this.outbox.delete(id);
// Remove from priority queue so it won't appear in future queries
const priorityItems = this.outboxPriorityQueue.get(item.priority);
if (priorityItems) {
const index = priorityItems.findIndex((i) => i.id === id);
if (index !== -1) {
priorityItems.splice(index, 1);
}
}
this.metrics.outboxItems--;
return true;
}
/**
* Add failed delivery to dead-letter queue
*/
addToDeadLetterQueue(
delivery: WebhookDelivery,
failureReason: string,
reasonCode: DLQReasonCode = 'other'
): string {
const id = `dlq_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
const dlqItem: DeadLetterQueueItem = {
id,
deliveryId: delivery.deliveryId,
eventId: delivery.eventId,
eventType: delivery.eventType,
endpointUrl: delivery.endpointUrl,
payload: delivery.payload,
originalDelivery: delivery,
failureReason,
reasonCode,
createdAt: Date.now(),
};
this.deadLetterQueue.set(id, dlqItem);
this.metrics.dlqItems++;
logger.error('Webhook delivery moved to dead-letter queue', undefined, {
dlqId: id,
deliveryId: delivery.deliveryId,
failureReason,
reasonCode,
attemptCount: delivery.attempts.length,
});
return id;
}
/**
* Get items from dead-letter queue
*/
getDeadLetterQueueItems(limit?: number): DeadLetterQueueItem[] {
const items = Array.from(this.deadLetterQueue.values());
return limit ? items.slice(0, limit) : items;
}
/**
* Process dead-letter queue item (retry or manual handling)
*/
processDeadLetterQueueItem(id: string, processedAt: number = Date.now()): boolean {
const item = this.deadLetterQueue.get(id);
if (!item) return false;
item.processedAt = processedAt;
this.deadLetterQueue.delete(id);
this.metrics.dlqItems--;
logger.info('Dead-letter queue item processed', undefined, {
dlqId: id,
deliveryId: item.deliveryId,
processedAt: new Date(processedAt).toISOString(),
});
return true;
}
/**
* Get all pending deliveries that are ready for retry.
* Circuit-breaker gating is applied by {@link WebhookService.processPendingRetries}.
*/
getPendingRetries(now: number = Date.now()): WebhookDelivery[] {
const retries: WebhookDelivery[] = [];
for (const delivery of this.deliveries.values()) {
if (delivery.status === 'pending') {
const lastAttempt = delivery.attempts[delivery.attempts.length - 1];
if (lastAttempt?.nextRetryAt && lastAttempt.nextRetryAt <= now) {
retries.push(delivery);
}
}
}
return retries;
}
/**
* Get all deliveries for an event
*/
getByEventId(eventId: string): WebhookDelivery[] {
const results: WebhookDelivery[] = [];
for (const delivery of this.deliveries.values()) {
if (delivery.eventId === eventId) {
results.push(delivery);
}
}
return results;
}
/**
* Register a delivery ID for deduplication without a full delivery record.
* Used by the /receive endpoint for inbound webhook verification.
*/
registerDeliveryId(deliveryId: string): void {
this.deliveryIdIndex.set(deliveryId, deliveryId);
}
/**
* Check if a delivery ID has been seen before (for deduplication)
*/
isDuplicateDelivery(deliveryId: string): boolean {
return this.deliveryIdIndex.has(deliveryId);
}
/**
* Get store metrics
*/
getMetrics(): {
totalDeliveries: number;
successfulDeliveries: number;
failedDeliveries: number;
dlqItems: number;
outboxItems: number;
} {
return { ...this.metrics };
}
/**
* Clean up old data (for maintenance)
*/
cleanup(olderThanMs: number = 7 * 24 * 60 * 60 * 1000): { cleaned: number; errors: string[] } {
const now = Date.now();
const cutoff = now - olderThanMs;
let cleaned = 0;
const errors: string[] = [];
try {
// Clean up old successful deliveries
for (const [id, delivery] of this.deliveries.entries()) {
if (delivery.status === 'delivered' && delivery.updatedAt < cutoff) {
this.deliveries.delete(id);
this.deliveryIdIndex.delete(delivery.deliveryId);
cleaned++;
}
}
// Clean up old DLQ items
for (const [id, item] of this.deadLetterQueue.entries()) {
if (item.createdAt < cutoff) {
this.deadLetterQueue.delete(id);
cleaned++;
}
}
logger.info('Webhook store cleanup completed', undefined, { cleaned, olderThanMs });
} catch (error) {
errors.push(error instanceof Error ? error.message : String(error));
}
return { cleaned, errors };
}
/**
* Clear all data (for testing)
*/
clear(): void {
this.deliveries.clear();
this.deliveryIdIndex.clear();
this.outbox.clear();
this.outboxPriorityQueue.clear();
this.deadLetterQueue.clear();
this.metrics = {
totalDeliveries: 0,
successfulDeliveries: 0,
failedDeliveries: 0,
dlqItems: 0,
outboxItems: 0,
};
}
/**
* Get all deliveries (for testing/monitoring)
*/
getAll(): WebhookDelivery[] {
return Array.from(this.deliveries.values());
}
/**
* Get all outbox items (for testing/monitoring)
*/
getAllOutboxItems(): OutboxItem[] {
return Array.from(this.outbox.values());
}
}