/** * Finding: A-03 * * app/api/internal/outbox/dispatch/route.ts * * Safety net that drains anything the request-time `after()` kick didn't send (provider * outage, crashed invocation). Scheduled every minute via vercel.json: * * { "crons": [{ "path": "/api/internal/outbox/dispatch", "schedule": "* * * * *" }] } * * Vercel Cron sends `Authorization: Bearer $CRON_SECRET`; anything else is rejected, so the * endpoint can't be used to trigger sends from outside. */ import { timingSafeEqual } from "node:crypto"; import { dispatchOutbox } from "./outbox"; export const dynamic = "force-dynamic"; export const maxDuration = 60; function authorised(req: Request): boolean { const expected = `Bearer ${process.env.CRON_SECRET ?? ""}`; const got = req.headers.get("authorization") ?? ""; const a = Buffer.from(got); const b = Buffer.from(expected); return process.env.CRON_SECRET !== undefined && a.length === b.length && timingSafeEqual(a, b); } export async function GET(req: Request) { if (!authorised(req)) return new Response(null, { status: 404 }); // don't advertise the route const result = await dispatchOutbox(); console.info(JSON.stringify({ level: "info", event: "outbox.dispatch", ...result })); return Response.json(result, { headers: { "Cache-Control": "no-store" } }); }