Open History to see the last commit
1/**
2 * Webhook delivery (PLAN.md task 6.2). New `changes` rows are fanned out to
3 * matching subscriptions as `webhook_deliveries`; delivery posts a signed
4 * JSON payload (HMAC-SHA256 over the exact body, header
5 * `X-ModelSchemas-Signature: sha256=<hex>`). Failures back off
6 * exponentially via nextAttemptAt and are drained by the 15-minute cron;
7 * after MAX_ATTEMPTS the subscription is auto-paused.
8 */
9import { and, eq, gt, lte } from 'drizzle-orm'
10
11import type { Db } from '#/db/index.ts'
12import {
13 cacheMeta,
14 changes,
15 subscriptions,
16 webhookDeliveries,
17} from '#/db/schema.ts'
18
19export const MAX_ATTEMPTS = 8
20const ENQUEUE_CHECKPOINT_KEY = 'webhooks:enqueue-checkpoint'
21const BASE_BACKOFF_SECONDS = 60
22
23export async function signPayload(
24 secret: string,
25 body: string,
26): Promise<string> {
27 const key = await crypto.subtle.importKey(
28 'raw',
29 new TextEncoder().encode(secret),
30 { name: 'HMAC', hash: 'SHA-256' },
31 false,
32 ['sign'],
33 )
34 const signature = await crypto.subtle.sign(
35 'HMAC',
36 key,
37 new TextEncoder().encode(body),
38 )
39 const hex = [...new Uint8Array(signature)]
40 .map((b) => b.toString(16).padStart(2, '0'))
41 .join('')
42 return `sha256=${hex}`
43}
44
45/**
46 * Fan out changes newer than the stored checkpoint to matching active
47 * subscriptions. Returns the number of deliveries enqueued. Idempotent via
48 * the checkpoint (a cache_meta row reusing fetchedAt as the high-water mark).
49 */
50export async function enqueueNewChanges(
51 db: Db,
52 now = Math.floor(Date.now() / 1000),
53): Promise<number> {
54 const checkpoint = await db.query.cacheMeta.findFirst({
55 where: eq(cacheMeta.key, ENQUEUE_CHECKPOINT_KEY),
56 })
57 const since = checkpoint?.fetchedAt ?? 0
58
59 const newChanges = await db
60 .select()
61 .from(changes)
62 .where(gt(changes.createdAt, since))
63 .orderBy(changes.createdAt)
64 if (newChanges.length === 0) return 0
65
66 const activeSubs = await db
67 .select()
68 .from(subscriptions)
69 .where(eq(subscriptions.active, true))
70
71 let enqueued = 0
72 for (const change of newChanges) {
73 for (const sub of activeSubs) {
74 const events = Array.isArray(sub.events)
75 ? (sub.events as Array<string>)
76 : []
77 if (!events.includes(change.type)) continue
78 if (sub.providerFilter && sub.providerFilter !== change.providerId) {
79 continue
80 }
81 await db.insert(webhookDeliveries).values({
82 id: crypto.randomUUID(),
83 subscriptionId: sub.id,
84 changeId: change.id,
85 attempt: 0,
86 nextAttemptAt: now,
87 status: 'pending',
88 })
89 enqueued++
90 }
91 }
92
93 const highWater = newChanges[newChanges.length - 1]?.createdAt ?? now
94 await db
95 .insert(cacheMeta)
96 .values({ key: ENQUEUE_CHECKPOINT_KEY, fetchedAt: highWater, staleTime: 0 })
97 .onConflictDoUpdate({
98 target: cacheMeta.key,
99 set: { fetchedAt: highWater },
100 })
101 return enqueued
102}
103
104export interface DeliveryOutcome {
105 deliveryId: string
106 subscriptionId: string
107 status: 'ok' | 'pending' | 'failed'
108 responseCode: number | null
109}
110
111/**
112 * Deliver everything due (pending, nextAttemptAt <= now). 2xx → ok;
113 * failures back off exponentially (60s · 2^attempt); attempt MAX_ATTEMPTS
114 * → delivery failed + subscription auto-paused.
115 */
116export async function deliverDue(
117 db: Db,
118 options?: {
119 fetcher?: typeof globalThis.fetch
120 limit?: number
121 now?: number
122 },
123): Promise<Array<DeliveryOutcome>> {
124 const fetcher = options?.fetcher ?? globalThis.fetch
125 const now = options?.now ?? Math.floor(Date.now() / 1000)
126 const limit = options?.limit ?? 50
127
128 const due = await db
129 .select()
130 .from(webhookDeliveries)
131 .where(
132 and(
133 eq(webhookDeliveries.status, 'pending'),
134 lte(webhookDeliveries.nextAttemptAt, now),
135 ),
136 )
137 .orderBy(webhookDeliveries.nextAttemptAt)
138 .limit(limit)
139
140 const outcomes: Array<DeliveryOutcome> = []
141 for (const delivery of due) {
142 const [sub, change] = await Promise.all([
143 db.query.subscriptions.findFirst({
144 where: eq(subscriptions.id, delivery.subscriptionId),
145 }),
146 db.query.changes.findFirst({
147 where: eq(changes.id, delivery.changeId),
148 }),
149 ])
150 if (!sub || !change || !sub.active) {
151 await db
152 .update(webhookDeliveries)
153 .set({ status: 'failed' })
154 .where(eq(webhookDeliveries.id, delivery.id))
155 outcomes.push({
156 deliveryId: delivery.id,
157 subscriptionId: delivery.subscriptionId,
158 status: 'failed',
159 responseCode: null,
160 })
161 continue
162 }
163
164 const body = JSON.stringify({
165 event: change.type,
166 change: {
167 id: change.id,
168 type: change.type,
169 provider: change.providerId,
170 subjectId: change.subjectId,
171 summary: change.summary,
172 payload: change.payload,
173 createdAt: change.createdAt,
174 },
175 _links: {
176 changes: `/v1/changes?since=${String(change.createdAt)}`,
177 provider: `/v1/providers/${change.providerId}/models`,
178 },
179 })
180
181 let responseCode: number | null = null
182 try {
183 const response = await fetcher(sub.url, {
184 method: 'POST',
185 headers: {
186 'Content-Type': 'application/json',
187 'X-ModelSchemas-Signature': await signPayload(sub.secret, body),
188 'X-ModelSchemas-Delivery': delivery.id,
189 },
190 body,
191 // SSRF guard: never follow redirects — a receiver could otherwise
192 // bounce the delivery to an internal target on the second hop. A 3xx
193 // counts as a failed attempt.
194 redirect: 'manual',
195 })
196 responseCode = response.status
197 } catch {
198 responseCode = null
199 }
200
201 if (responseCode !== null && responseCode >= 200 && responseCode < 300) {
202 await db
203 .update(webhookDeliveries)
204 .set({ status: 'ok', lastResponseCode: responseCode })
205 .where(eq(webhookDeliveries.id, delivery.id))
206 outcomes.push({
207 deliveryId: delivery.id,
208 subscriptionId: sub.id,
209 status: 'ok',
210 responseCode,
211 })
212 continue
213 }
214
215 const attempt = delivery.attempt + 1
216 if (attempt >= MAX_ATTEMPTS) {
217 // Terminal: fail the delivery and auto-pause the subscription so a
218 // dead receiver stops consuming the queue.
219 await db
220 .update(webhookDeliveries)
221 .set({ status: 'failed', attempt, lastResponseCode: responseCode })
222 .where(eq(webhookDeliveries.id, delivery.id))
223 await db
224 .update(subscriptions)
225 .set({ active: false })
226 .where(eq(subscriptions.id, sub.id))
227 console.log(
228 JSON.stringify({
229 job: 'webhooks',
230 event: 'subscription-paused',
231 subscriptionId: sub.id,
232 deliveryId: delivery.id,
233 attempts: attempt,
234 }),
235 )
236 outcomes.push({
237 deliveryId: delivery.id,
238 subscriptionId: sub.id,
239 status: 'failed',
240 responseCode,
241 })
242 } else {
243 await db
244 .update(webhookDeliveries)
245 .set({
246 attempt,
247 lastResponseCode: responseCode,
248 nextAttemptAt: now + BASE_BACKOFF_SECONDS * 2 ** attempt,
249 })
250 .where(eq(webhookDeliveries.id, delivery.id))
251 outcomes.push({
252 deliveryId: delivery.id,
253 subscriptionId: sub.id,
254 status: 'pending',
255 responseCode,
256 })
257 }
258 }
259 return outcomes
260}
261
262/** Cron entry: fan out new changes, then drain due deliveries. */
263export async function runWebhookTick(
264 db: Db,
265 options?: Parameters<typeof deliverDue>[1],
266): Promise<{ enqueued: number; outcomes: Array<DeliveryOutcome> }> {
267 const enqueued = await enqueueNewChanges(db, options?.now)
268 const outcomes = await deliverDue(db, options)
269 return { enqueued, outcomes }
270}