|
| 1 | +import express from "express"; |
| 2 | +import fs from "fs"; |
| 3 | +import path from "path"; |
| 4 | +import dotenv from "dotenv"; |
| 5 | +import { handleWebhook, MyMXWebhookError, MYMX_CONFIRMED_HEADER } from "mymx"; |
| 6 | + |
| 7 | +dotenv.config(); |
| 8 | + |
| 9 | +const app = express(); |
| 10 | +const port = Number(process.env.PORT || 4567); |
| 11 | +const secret = process.env.MYMX_WEBHOOK_SECRET || ""; |
| 12 | +const eventStorePath = process.env.EVENT_STORE || "./events.jsonl"; |
| 13 | + |
| 14 | +if (!secret) { |
| 15 | + throw new Error("Missing MYMX_WEBHOOK_SECRET"); |
| 16 | +} |
| 17 | + |
| 18 | +// MyMX needs raw text body to verify signatures. |
| 19 | +app.use( |
| 20 | + express.text({ |
| 21 | + type: "*/*", |
| 22 | + limit: "10mb", |
| 23 | + }) |
| 24 | +); |
| 25 | + |
| 26 | +function ensureStoreDir(filePath: string) { |
| 27 | + const dir = path.dirname(filePath); |
| 28 | + if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true }); |
| 29 | +} |
| 30 | + |
| 31 | +function appendEvent(line: string) { |
| 32 | + ensureStoreDir(eventStorePath); |
| 33 | + fs.appendFileSync(eventStorePath, line + "\n", "utf8"); |
| 34 | +} |
| 35 | + |
| 36 | +// Idempotency in MVP: keep last 10k IDs in memory. |
| 37 | +const seen = new Set<string>(); |
| 38 | +const seenQueue: string[] = []; |
| 39 | +function remember(id: string) { |
| 40 | + if (seen.has(id)) return; |
| 41 | + seen.add(id); |
| 42 | + seenQueue.push(id); |
| 43 | + if (seenQueue.length > 10_000) { |
| 44 | + const old = seenQueue.shift(); |
| 45 | + if (old) seen.delete(old); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +app.post("/webhook/mymx", (req, res) => { |
| 50 | + const rawBody = req.body; |
| 51 | + const headers = Object.fromEntries( |
| 52 | + Object.entries(req.headers).map(([k, v]) => [k, Array.isArray(v) ? v.join(",") : v]) |
| 53 | + ); |
| 54 | + |
| 55 | + try { |
| 56 | + const event = handleWebhook({ |
| 57 | + body: rawBody, |
| 58 | + headers, |
| 59 | + secret, |
| 60 | + }); |
| 61 | + |
| 62 | + if (seen.has(event.id)) { |
| 63 | + return res.status(200).set(MYMX_CONFIRMED_HEADER, "true").end(); |
| 64 | + } |
| 65 | + |
| 66 | + remember(event.id); |
| 67 | + |
| 68 | + const record = { |
| 69 | + id: event.id, |
| 70 | + received_at: event.email.received_at, |
| 71 | + from: event.email.headers.from, |
| 72 | + subject: event.email.headers.subject, |
| 73 | + to: event.email.headers.to, |
| 74 | + body_text: event.email.parsed?.body_text || null, |
| 75 | + spam_score: event.email.analysis?.spamassassin?.score ?? null, |
| 76 | + raw_event: event, |
| 77 | + }; |
| 78 | + |
| 79 | + appendEvent(JSON.stringify(record)); |
| 80 | + |
| 81 | + return res.status(200).set(MYMX_CONFIRMED_HEADER, "true").end(); |
| 82 | + } catch (err) { |
| 83 | + if (err instanceof MyMXWebhookError) { |
| 84 | + return res.status(400).json({ error: err.code }); |
| 85 | + } |
| 86 | + console.error(err); |
| 87 | + return res.status(500).json({ error: "internal_error" }); |
| 88 | + } |
| 89 | +}); |
| 90 | + |
| 91 | +app.get("/health", (_req, res) => { |
| 92 | + res.json({ ok: true }); |
| 93 | +}); |
| 94 | + |
| 95 | +app.listen(port, () => { |
| 96 | + console.log(`Mailbrain webhook receiver listening on :${port}`); |
| 97 | +}); |
0 commit comments