-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebhook-server.mjs
More file actions
60 lines (54 loc) · 1.55 KB
/
Copy pathwebhook-server.mjs
File metadata and controls
60 lines (54 loc) · 1.55 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
import { createServer } from "node:http";
import {
describeBeatAPIWebhookEvent,
verifyBeatAPIWebhook,
} from "./lib/webhook.mjs";
const secret = process.env.BEATAPI_WEBHOOK_SECRET;
const maxBodyBytes = 1024 * 1024;
if (!secret) {
throw new Error("BEATAPI_WEBHOOK_SECRET is required.");
}
const server = createServer((request, response) => {
if (request.method !== "POST" || request.url !== "/webhooks/beatapi") {
response.writeHead(404).end("Not found");
return;
}
const chunks = [];
let bodyBytes = 0;
let bodyTooLarge = false;
request.on("data", (chunk) => {
if (bodyTooLarge) return;
bodyBytes += chunk.length;
if (bodyBytes > maxBodyBytes) {
bodyTooLarge = true;
response.writeHead(413).end("Payload too large");
request.destroy();
return;
}
chunks.push(chunk);
});
request.on("end", () => {
if (bodyTooLarge) return;
const rawBody = Buffer.concat(chunks).toString("utf8");
const valid = verifyBeatAPIWebhook({
rawBody,
secret,
signature: request.headers["x-beatapi-signature"],
timestamp: request.headers["x-beatapi-timestamp"],
});
if (!valid) {
response.writeHead(401).end("Invalid signature");
return;
}
try {
const event = JSON.parse(rawBody);
console.log(`Received ${describeBeatAPIWebhookEvent(event)}`);
response.writeHead(204).end();
} catch {
response.writeHead(400).end("Invalid JSON");
}
});
});
server.listen(3000, () => {
console.log("Webhook receiver listening on http://localhost:3000");
});