import { Elysia } from "elysia"; import { resolveProfile } from "../lib/profile.ts"; // /@(/rest)? → 301 to the canonical /@ URL; renders in place if the handle won't resolve. const DID_HANDLE_RE = /^\/@(did:[^/]+)(\/.*)?$/; export const canonicalHandlePlugin = new Elysia({ name: "canonical-handle", }).onRequest(async ({ request }) => { const url = new URL(request.url); const match = url.pathname.match(DID_HANDLE_RE); if (!match) return; const did = match[1] as string; const rest = match[2] ?? ""; const profile = await resolveProfile(did); // Skip if the handle is missing or still a DID — would 301 to the same URL. if (!profile.handle || profile.handle.startsWith("did:")) return; const canonical = `/@${profile.handle}${rest}${url.search}`; return new Response(null, { status: 301, headers: { Location: canonical }, }); });