-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathwebhooks.ts
More file actions
178 lines (158 loc) · 4.88 KB
/
Copy pathwebhooks.ts
File metadata and controls
178 lines (158 loc) · 4.88 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
import { createHmac } from "crypto";
export type WebhookSignatureScheme = "github" | "stripe";
export interface WebhookSubscription {
id: number;
url: string;
events: string[];
active: boolean;
secret?: string;
signatureScheme?: WebhookSignatureScheme;
owner: string;
repo?: string;
}
export interface WebhookDelivery {
id: number;
hook_id: number;
event: string;
action?: string;
payload: unknown;
status_code: number | null;
delivered_at: string;
duration: number | null;
success: boolean;
}
const MAX_DELIVERIES = 1000;
export class WebhookDispatcher {
private subscriptions: WebhookSubscription[] = [];
private deliveries: WebhookDelivery[] = [];
private subscriptionIdCounter = 1;
private deliveryIdCounter = 1;
register(sub: Omit<WebhookSubscription, "id"> & { id?: number }): WebhookSubscription {
const { id: explicitId, ...rest } = sub;
const id = explicitId !== undefined ? explicitId : this.subscriptionIdCounter++;
if (id >= this.subscriptionIdCounter) {
this.subscriptionIdCounter = id + 1;
}
const subscription: WebhookSubscription = { ...rest, id };
this.subscriptions.push(subscription);
return subscription;
}
unregister(id: number): boolean {
const idx = this.subscriptions.findIndex((s) => s.id === id);
if (idx === -1) return false;
this.subscriptions.splice(idx, 1);
return true;
}
getSubscription(id: number): WebhookSubscription | undefined {
return this.subscriptions.find((s) => s.id === id);
}
getSubscriptions(owner?: string, repo?: string): WebhookSubscription[] {
return this.subscriptions.filter((s) => {
if (owner && s.owner !== owner) return false;
if (repo !== undefined && s.repo !== repo) return false;
return true;
});
}
updateSubscription(
id: number,
data: Partial<Pick<WebhookSubscription, "url" | "events" | "active" | "secret">>,
): WebhookSubscription | undefined {
const sub = this.subscriptions.find((s) => s.id === id);
if (!sub) return undefined;
Object.assign(sub, data);
return sub;
}
async dispatch(
event: string,
action: string | undefined,
payload: unknown,
owner: string,
repo?: string,
): Promise<void> {
const matchingSubs = this.subscriptions.filter((s) => {
if (!s.active) return false;
if (s.owner !== owner) return false;
if (repo !== undefined) {
if (s.repo !== repo) return false;
} else if (s.repo !== undefined) {
return false;
}
return event === "ping" || s.events.includes("*") || s.events.includes(event);
});
for (const sub of matchingSubs) {
const delivery: WebhookDelivery = {
id: this.deliveryIdCounter++,
hook_id: sub.id,
event,
action,
payload,
status_code: null,
delivered_at: new Date().toISOString(),
duration: null,
success: false,
};
const body = JSON.stringify(payload);
const headers = buildWebhookHeaders(sub, event, delivery.id, body);
try {
const start = Date.now();
const response = await fetch(sub.url, {
method: "POST",
headers,
body,
signal: AbortSignal.timeout(10000),
});
delivery.duration = Date.now() - start;
delivery.status_code = response.status;
delivery.success = response.ok;
} catch {
delivery.duration = 0;
delivery.success = false;
}
this.deliveries.push(delivery);
if (this.deliveries.length > MAX_DELIVERIES) {
this.deliveries.splice(0, this.deliveries.length - MAX_DELIVERIES);
}
}
}
getDeliveries(hookId?: number): WebhookDelivery[] {
if (hookId !== undefined) {
return this.deliveries.filter((d) => d.hook_id === hookId);
}
return [...this.deliveries];
}
clear(): void {
this.subscriptions.length = 0;
this.deliveries.length = 0;
this.subscriptionIdCounter = 1;
this.deliveryIdCounter = 1;
}
}
function buildWebhookHeaders(
sub: WebhookSubscription,
event: string,
deliveryId: number,
body: string,
): Record<string, string> {
if (sub.signatureScheme === "stripe") {
const headers: Record<string, string> = {
"Content-Type": "application/json",
};
if (sub.secret) {
const timestamp = Math.floor(Date.now() / 1000);
const payload = `${timestamp}.${body}`;
const signature = createHmac("sha256", sub.secret).update(payload).digest("hex");
headers["Stripe-Signature"] = `t=${timestamp},v1=${signature}`;
}
return headers;
}
const headers: Record<string, string> = {
"Content-Type": "application/json",
"X-GitHub-Event": event,
"X-GitHub-Delivery": String(deliveryId),
};
if (sub.secret) {
const hmac = createHmac("sha256", sub.secret).update(body).digest("hex");
headers["X-Hub-Signature-256"] = `sha256=${hmac}`;
}
return headers;
}