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