import Fastify, { type FastifyInstance, type FastifyReply, type FastifyRequest } from "fastify"; import { readFileSync } from "node:fs"; import { join, dirname } from "node:path"; import { fileURLToPath } from "node:url"; import { MailboxStore, rowToMessage } from "./db.js"; import type { DaemonConfig } from "./config.js"; import { registerMcp } from "./mcp.js"; export const HEADER_NAME = "x-mailbox"; declare module "fastify" { interface FastifyRequest { mailboxName?: string; } } function readVersion(): string { try { const here = dirname(fileURLToPath(import.meta.url)); const pkg = JSON.parse(readFileSync(join(here, "..", "package.json"), "utf8")) as { version?: string; }; return pkg.version ?? "unknown"; } catch { return "unknown"; } } const ANONYMOUS_PATHS = new Set(["/v1/list", "/v1/peek"]); export async function buildServer(cfg: DaemonConfig, store: MailboxStore): Promise { const app = Fastify({ logger: true }); const version = readVersion(); app.addHook("onRequest", async (req: FastifyRequest, reply: FastifyReply) => { const url = req.url.split("?")[0] ?? "/"; if (url === "/health" || url === "/mcp" || url.startsWith("/mcp/")) return; const headerValue = req.headers[HEADER_NAME]; const name = (Array.isArray(headerValue) ? headerValue[0] : headerValue ?? "").trim(); if (!name) { if (ANONYMOUS_PATHS.has(url)) return; reply.code(400).send({ error: `Missing ${HEADER_NAME} header.` }); return reply; } req.mailboxName = name; store.upsertMailbox(name); }); app.get("/health", async () => ({ status: "ok", version, dbPath: cfg.dbPath, })); app.post<{ Body: { to?: string; body?: string } }>("/v1/send", async (req, reply) => { const { to, body } = req.body ?? {}; if (!to || !body) { reply.code(400); return { error: "to and body are required" }; } const from = req.mailboxName!; const result = store.send(from, to, body); return { id: result.id, queuedAt: result.queuedAt.toISOString() }; }); app.get<{ Querystring: { name?: string } }>("/v1/peek", async (req, reply) => { const name = (req.query.name ?? "").trim(); if (!name) { reply.code(400); return { error: "name is required" }; } const status = store.peek(name); return { pending: status.pending, oldestAt: status.oldestAt?.toISOString() ?? null, }; }); app.post<{ Querystring: { name?: string } }>("/v1/check-inbox", async (req, reply) => { const name = (req.query.name ?? "").trim(); if (name !== req.mailboxName) { reply.code(403); return { error: "X-Mailbox header must match name." }; } return store.checkInbox(name).map((m) => { const msg = rowToMessage(m); return { ...msg, sentAt: msg.sentAt.toISOString() }; }); }); app.get("/v1/list", async (req) => { const name = req.mailboxName; return store.listMailboxes(name).map((m) => ({ name: m.name, lastSeenAt: m.lastSeenAt.toISOString(), pendingForYou: m.pendingForYou, })); }); await registerMcp(app, store); return app; } export async function startServer(cfg: DaemonConfig): Promise<{ app: FastifyInstance; store: MailboxStore }> { const store = new MailboxStore(cfg.dbPath); const app = await buildServer(cfg, store); await app.listen({ host: cfg.bind, port: cfg.port }); return { app, store }; }