import{b as a,d as R,e as h}from"./chunk-TF5C7XJX.js";import"./chunk-DWY47FQV.js";import{HTTPException as bn}from"hono/http-exception";import{Hono as En}from"hono/tiny";import{validator as u}from"hono/validator";import{array as Y,literal as Z,minLength as K,object as J,pipe as nn,safeParse as en,string as tn,union as rn}from"valibot";var on=J({tags:rn([nn(Y(tn()),K(1)),Z("all")])});async function L(n){let{output:r,success:e}=en(on,await n.req.json(),{abortEarly:!0});return e?r:n.text("Invalid input",400)}import{parseDuration as T,parseSize as sn,Server as an}from"@prisma/query-plan-executor";import{version as w}from"@prisma/query-plan-executor";var I;async function q(n){return I===void 0&&(I=await an.create({databaseUrl:n.get("db").connectionString,maxResponseSize:sn("128 MiB"),queryTimeout:T("PT5M"),maxTransactionTimeout:T("PT5M"),maxTransactionWaitTime:T("PT5M"),perRequestLogContext:{logFormat:"text",logLevel:n.get("debug")?"debug":"off"}})),I}import{Buffer as B}from"buffer";var S=new Map;async function b(n){let e=new TextEncoder().encode(n),t=await crypto.subtle.digest("SHA-256",e);return Array.from(new Uint8Array(t)).map(s=>s.toString(16).padStart(2,"0")).join("")}function M(n){let r=n.req.param("schemaHash"),e=S.get(r);return e==null?n.json({EngineNotStarted:{reason:"SchemaMissing"}},404):{schemaHash:r,schemas:e}}async function j(n,r,e){let t=B.from(n,"base64").toString("utf8"),o=un(t,r.toString());e&&console.log("[Accelerate] schema with override:",o);let i=await b(o);return{base64Override:B.from(o,"utf8").toString("base64"),overrideHash:i}}var cn="  ";function un(n,r){let e=fn(n);if(!e)throw new Error("No datasource block found in schema.");let t=n.slice(e.openBraceIndex+1,e.closeBraceIndex),o=pn(t,r,hn(n,e.openBraceIndex));return`${n.slice(0,e.openBraceIndex+1)}${o}${n.slice(e.closeBraceIndex)}`}function fn(n){let r="datasource";for(let e=0;e<n.length;e+=1){let t=n[e];if(t==="/"&&n[e+1]==="/"){e=E(n,e+2);continue}if(t==="/"&&n[e+1]==="*"){e=C(n,e+2);continue}if(t==='"'){e=H(n,e+1);continue}if(n.startsWith(r,e)&&!_(n[e-1])&&!_(n[e+r.length])){let o=ln(n,e+r.length,"{");if(o===-1)return null;let i=mn(n,o);return i===-1?null:{closeBraceIndex:i,openBraceIndex:o}}}return null}function ln(n,r,e){for(let t=r;t<n.length;t+=1){let o=n[t];if(o==="/"&&n[t+1]==="/"){t=E(n,t+2);continue}if(o==="/"&&n[t+1]==="*"){t=C(n,t+2);continue}if(o==='"'){t=H(n,t+1);continue}if(o===e)return t}return-1}function mn(n,r){let e=0;for(let t=r;t<n.length;t+=1){let o=n[t];if(o==="/"&&n[t+1]==="/"){t=E(n,t+2);continue}if(o==="/"&&n[t+1]==="*"){t=C(n,t+2);continue}if(o==='"'){t=H(n,t+1);continue}if(o==="{"){e+=1;continue}if(o==="}"&&(e-=1,e===0))return t}return-1}function E(n,r){let e=r;for(;e<n.length&&n[e]!==`
`;)e+=1;return e}function C(n,r){let e=n.indexOf("*/",r);return e===-1?n.length:e+1}function H(n,r){let e=r;for(;e<n.length;){if(n[e]==="\\"){e+=2;continue}if(n[e]==='"')return e;e+=1}return n.length}function _(n){return n!=null&&/[A-Za-z0-9_]/.test(n)}function pn(n,r,e){let t=`"${yn(r)}"`,o=dn(n);for(let[d,A]of o.entries()){let{comment:X,code:G}=V(A.line),g=G.match(/^(\s*)url(\s*)=(\s*).*/);if(g)return o[d]={line:`${g[1]}url${g[2]}=${g[3]}${t}${X}`,terminator:A.terminator},P(o)}let i=Sn(n),s=gn(o,e),c=`${s}url = ${t}`;if(!n.includes(`
`)&&!n.includes("\r")){let d=n.trim();return d.length===0?`${i}${c}${i}${e}`:`${i}${s}${d}${i}${c}${i}${e}`}let f=o.at(-1);if(f&&f.terminator===""&&/^\s*$/.test(f.line))return`${P(o.slice(0,-1))}${c}${i}${e}`;let $=P(o),Q=$.endsWith(`
`)||$.endsWith("\r")?"":i;return`${$}${Q}${c}${i}${e}`}function dn(n){let r=[],e=0;for(let t=0;t<n.length;t+=1){if(n[t]==="\r"&&n[t+1]===`
`){r.push({line:n.slice(e,t),terminator:`\r
`}),t+=1,e=t+1;continue}(n[t]===`
`||n[t]==="\r")&&(r.push({line:n.slice(e,t),terminator:n[t]}),e=t+1)}return(e<n.length||n.length===0)&&r.push({line:n.slice(e),terminator:""}),r}function P(n){return n.map(r=>`${r.line}${r.terminator}`).join("")}function V(n){let r=!1,e=!1;for(let t=0;t<n.length;t+=1){let o=n[t];if(e){if(r){r=!1;continue}if(o==="\\"){r=!0;continue}o==='"'&&(e=!1);continue}if(o==='"'){e=!0;continue}if(o==="/"&&n[t+1]==="/")return{code:n.slice(0,t),comment:n.slice(t)}}return{code:n,comment:""}}function gn(n,r){for(let e of n){let{code:t}=V(e.line),o=t.match(/^([ \t]+)[A-Za-z_][A-Za-z0-9_]*\s*=/);if(o?.[1])return o[1]}return`${r}${cn}`}function hn(n,r){let e=n.lastIndexOf(`
`,r)+1,t=e;for(;n[t]===" "||n[t]==="	";)t+=1;return n.slice(e,t)}function Sn(n){return n.includes(`\r
`)?`\r
`:`
`}function yn(n){return n.replaceAll("\\","\\\\").replaceAll('"','\\"')}function y(n){let{req:r}=n;return{traceparent:r.header("traceparent"),"X-capture-telemetry":r.header("X-capture-telemetry")}}import{integer as D,looseObject as xn,minValue as N,number as O,object as $n,optional as Tn,pipe as U,safeParse as k,string as z,union as In}from"valibot";var wn=$n({isolation_level:Tn(z()),max_wait:U(O(),D(),N(0)),timeout:U(O(),D(),N(0))});async function F(n){let{issues:r,output:e,success:t}=k(wn,await n.req.json(),{abortEarly:!0});return t?e:n.json({EngineNotStarted:{reason:"InvalidRequest",issues:r}},400)}var Pn=xn({id:In([z(),O()])});function W(n,r){let{output:e,success:t}=k(Pn,n);return t?e:r.json({EngineMalfunction:{}},500)}var x=new En;x.post("/invalidate",u("header",a),async n=>{let r=await L(n);return r instanceof Response?r:n.body(null)});var Cn="/:clientVersion/:schemaHash",p=x.basePath(Cn);x.route("/",p);var Hn=["/graphql","/itx/:transactionId/graphql"];p.on("POST",[...Hn],u("header",a),async n=>{let{req:r}=n;try{let e=await v(n);if(e instanceof Response)return e;let t=await r.text(),o=r.param("transactionId"),i=await e.request(t,{...y(n),"X-transaction-id":o});return n.text(i)}catch(e){return h(e,n)}});p.basePath("/itx/:transactionId").on("POST",["/commit","/rollback"],u("header",a),async n=>{let{req:r}=n;try{let e=await v(n);if(e instanceof Response)return e;let o=`${r.routePath.split("/").filter(Boolean).at(-1)}Transaction`,i=r.param("transactionId"),s=await e[o](i,y(n));return n.json(s)}catch(e){return h(e,n)}});p.put("/schema",u("header",a),async n=>{let{req:r}=n,e=await r.text();if(!e)return n.text("Missing schema",400);let t=r.param("schemaHash"),o=S.get(t);if(o==null){if(t!==await b(e))return n.text("Schema hash mismatch",400);let i=await j(e,n.get("db").prismaORMConnectionString,n.get("debug"));return S.set(t,{base64Original:e,...i}),n.text(t)}return e!==o.base64Original?n.text("Schema mismatch",400):n.text(t)});p.post("/transaction/start",u("header",a),async n=>{let{req:r}=n,e=await F(n);if(e instanceof Response)return e;try{let t=await v(n);if(t instanceof Response)return t;let o=await t.startTransaction(e,y(n)),i=W(o,n);if(i instanceof Response)return i;let{id:s}=i,c=r.param("clientVersion"),f=r.param("schemaHash");return n.json({...o,"data-proxy":{endpoint:`${new URL(r.url).origin}/${c}/${f}/itx/${s}`}})}catch(t){return h(t,n)}});async function v(n){let{req:r}=n,e=M(n);if(e instanceof Response)return e;let{base64Override:t,overrideHash:o}=e.schemas;return await R.get({base64Schema:t,clientVersion:process.env.PRISMA_DEV_FORCE_CLIENT_VERSION||r.param("clientVersion"),debug:n.get("debug"),platform:n.get("platform"),schemaHash:o})}var On=[["/connection-info","GET"],["/query","POST"],["/transaction/start","POST"],["/transaction/:transactionId/commit","POST"],["/transaction/:transactionId/query","POST"],["/transaction/:transactionId/rollback","POST"]];for(let[n,r]of On)x.on(r,n,u("header",a),async e=>{let t=e.req.header("prisma-engine-hash");if(t!=="0.0.0"&&t!==w)throw new bn(400,{message:`Using an HTTP connection string is not supported with Prisma Client version ${t} by this version of \`prisma dev\`. Please either use a direct TCP connection string or upgrade your client to version ${w}.`});return await(await q(e)).fetch(e.req.raw)});export{x as accelerateRoute};
