@prisma/dev
Version:
A local Prisma Postgres server for development and testing
2 lines (1 loc) • 1.64 kB
JavaScript
import{a as r}from"./chunk-ANHBRJRZ.js";import{minLength as D,object as S,optional as B,parseJson as x,pipe as n,regex as h,safeParse as O,string as s,url as f}from"valibot";var l=/^(postgres|postgresql):\/\//,T=n(s(),x(),S({databaseUrl:n(s(),f(),h(l)),name:B(n(s(),D(1))),shadowDatabaseUrl:n(s(),f(),h(l))}));function $(o){return Buffer.from(JSON.stringify(Object.fromEntries(Object.entries(o).sort(([[e],[t]])=>e.localeCompare(t)))),"utf8").toString("base64url")}function E(o){let e=Buffer.from(o,"base64url").toString("utf8"),{issues:t,output:a,success:i}=O(T,e,{abortEarly:!0});return i?[null,a]:[t]}function j(o,e){let t=o.headers.authorization;if(!t)throw new r(401,"Missing API Key");let[a,i="",y]=t.split(" ");if(a!=="Bearer"||y)throw new r(401,"Invalid API Key");let[c,p]=E(i);if(c)throw new r(401,"Invalid API Key",{cause:c.join(", ")});let{databaseUrl:P,name:d,shadowDatabaseUrl:A}=p,u=e.name,m=e.port;if(!d)throw new r(401,`Wrong API Key; The Prisma Dev server running at port ${m} requires an API Key from a newer version of \`prisma dev\`. Check the "${u}" server's output for the updated \`DATABASE_URL\` value.`);if(d!==u)throw new r(401,`Wrong API Key; The Prisma Dev server running at port ${m} is named "${u}", but the API Key is for "${d}"`);let{hostname:I,port:w}=new URL(P),{port:g}=e.db,{hostname:b,port:K}=new URL(A),v=e.shadowDBPort;if(I!=="localhost"||Number(w)!==g||b!=="localhost"||Number(K)!==v)throw new r(401,"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 p}export{$ as a,j as b};