import{minLength as D,object as B,optional as S,parseJson as O,pipe as s,regex as m,safeParse as T,string as n,url as l}from"valibot";var y=/^(postgres|postgresql):\/\//,U=s(n(),O(),B({databaseUrl:s(n(),l(),m(y)),name:S(s(n(),D(1))),shadowDatabaseUrl:s(n(),l(),m(y))}));function H(r){return Buffer.from(JSON.stringify(Object.fromEntries(Object.entries(r).sort(([[e],[o]])=>e.localeCompare(o)))),"utf8").toString("base64url")}function E(r){let e=Buffer.from(r,"base64url").toString("utf8"),{issues:o,output:t,success:a}=T(U,e,{abortEarly:!0});return a?[null,t]:[o]}var $=async(r,e)=>{let{authorization:o}=r,{HTTPException:t}=await import("hono/http-exception");if(!o)throw new t(401,{message:"Missing API Key"});let[a,f="",h]=o.split(" ");if(a!=="Bearer"||h)throw new t(401,{message:"Invalid API Key"});let[u,c]=E(f);if(u)throw new t(401,{message:"Invalid API Key",cause:u.join(", ")});let{databaseUrl:P,name:i,shadowDatabaseUrl:g}=c,d=e.get("name"),p=new URL(e.req.url).port;if(!i)throw new t(401,{message:`Wrong API Key; The Prisma Dev server running at port ${p} requires an API Key from a newer version of \`prisma dev\`. Check the "${d}" server's output for the updated \`DATABASE_URL\` value.`});if(i!==d)throw new t(401,{message:`Wrong API Key; The Prisma Dev server running at port ${p} is named "${d}", but the API Key is for "${i}"`});let{hostname:w,port:A}=new URL(P),{port:I}=e.get("db"),{hostname:K,port:b}=new URL(g),v=e.get("shadowDBPort");if(w!=="localhost"||Number(A)!==I||K!=="localhost"||Number(b)!==v)throw new t(401,{message:"Wrong API Key; Check your Prisma schema's `provider.url` value (probably defined in `.env`'s `DATABASE_URL` environment variable) is aligned with `prisma dev`'s output"});return{decodedAPIKey:c}};export{H as a,$ as b};
