forked from Prompt-Hash-Stellar/prompt-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhookDispatcher.ts
More file actions
182 lines (156 loc) · 4.86 KB
/
Copy pathwebhookDispatcher.ts
File metadata and controls
182 lines (156 loc) · 4.86 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
import { createHmac, randomUUID } from "crypto";
import WebhookSubscription from "../models/WebhookSubscription";
import WebhookDeliveryLog from "../models/WebhookDeliveryLog";
const MAX_RETRIES = 3;
const BASE_DELAY_MS = 2_000;
const MAX_FAILURES_BEFORE_DISABLE = 10;
const DELIVERY_TIMEOUT_MS = 10_000;
export const ALLOWED_EVENTS = [
"PromptPurchased",
"PromptCreated",
"LicenseTransferred",
"ReviewSubmitted",
] as const;
export type WebhookEvent = (typeof ALLOWED_EVENTS)[number];
export interface WebhookPayload {
event: string;
deliveryId: string;
timestamp: string;
data: Record<string, unknown>;
}
export function signPayload(secret: string, body: string): string {
return `sha256=${createHmac("sha256", secret).update(body).digest("hex")}`;
}
export function verifySignature(
secret: string,
body: string,
signature: string,
): boolean {
const expected = signPayload(secret, body);
if (expected.length !== signature.length) return false;
let result = 0;
for (let i = 0; i < expected.length; i++) {
result |= expected.charCodeAt(i) ^ signature.charCodeAt(i);
}
return result === 0;
}
function computeRetryDelay(attempt: number): number {
return BASE_DELAY_MS * Math.pow(2, attempt);
}
async function deliverOnce(
url: string,
secret: string,
payload: WebhookPayload,
): Promise<number> {
const body = JSON.stringify(payload);
const signature = signPayload(secret, body);
const res = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-PromptHash-Signature": signature,
"X-PromptHash-Delivery": payload.deliveryId,
"X-PromptHash-Event": payload.event,
"X-PromptHash-Timestamp": payload.timestamp,
},
body,
signal: AbortSignal.timeout(DELIVERY_TIMEOUT_MS),
});
return res.status;
}
async function deliverWithRetry(
subscriptionId: string,
url: string,
secret: string,
payload: WebhookPayload,
): Promise<void> {
let logEntry = await WebhookDeliveryLog.findOne({ deliveryId: payload.deliveryId });
if (!logEntry) {
logEntry = await WebhookDeliveryLog.create({
deliveryId: payload.deliveryId,
subscriptionId,
event: payload.event,
url,
status: "retrying",
});
}
for (let attempt = 0; attempt <= MAX_RETRIES; attempt++) {
logEntry.attempts = attempt + 1;
try {
const status = await deliverOnce(url, secret, payload);
logEntry.lastStatus = status;
if (status >= 200 && status < 300) {
logEntry.status = "success";
logEntry.completedAt = new Date();
await logEntry.save();
await WebhookSubscription.findByIdAndUpdate(subscriptionId, {
lastDeliveredAt: new Date(),
$set: { failureCount: 0 },
});
return;
}
if (status >= 400 && status < 500 && status !== 429) {
logEntry.status = "failed";
logEntry.lastError = `HTTP ${status}`;
logEntry.completedAt = new Date();
await logEntry.save();
const updated = await WebhookSubscription.findByIdAndUpdate(
subscriptionId,
{ $inc: { failureCount: 1 } },
{ new: true },
);
if (updated && updated.failureCount >= MAX_FAILURES_BEFORE_DISABLE) {
await WebhookSubscription.findByIdAndUpdate(subscriptionId, { active: false });
}
return;
}
logEntry.lastError = `HTTP ${status}`;
} catch (err) {
logEntry.lastError = err instanceof Error ? err.message : String(err);
}
const isLastAttempt = attempt === MAX_RETRIES;
if (!isLastAttempt) {
const delay = computeRetryDelay(attempt);
logEntry.nextRetryAt = new Date(Date.now() + delay);
logEntry.status = "retrying";
await logEntry.save();
await new Promise((r) => setTimeout(r, delay));
continue;
}
logEntry.status = "failed";
logEntry.completedAt = new Date();
await logEntry.save();
const updated = await WebhookSubscription.findByIdAndUpdate(
subscriptionId,
{ $inc: { failureCount: 1 } },
{ new: true },
);
if (updated && updated.failureCount >= MAX_FAILURES_BEFORE_DISABLE) {
await WebhookSubscription.findByIdAndUpdate(subscriptionId, { active: false });
}
}
}
export async function dispatchEvent(
creatorWallet: string,
event: string,
data: Record<string, unknown>,
): Promise<void> {
if (!ALLOWED_EVENTS.includes(event as WebhookEvent)) return;
const subscriptions = await WebhookSubscription.find({
walletAddress: creatorWallet.toLowerCase(),
active: true,
events: event,
});
if (subscriptions.length === 0) return;
const payload: WebhookPayload = {
event,
deliveryId: randomUUID(),
timestamp: new Date().toISOString(),
data,
};
await Promise.allSettled(
subscriptions.map((sub) =>
deliverWithRetry(String(sub._id), sub.url, sub.secret, payload),
),
);
}