UNPKG

bknd

Version:

Lightweight Firebase/Supabase alternative built to run anywhere — incl. Next.js, React Router, Astro, Cloudflare, Bun, Node, AWS Lambda & more.

2 lines (1 loc) 1.23 kB
import{d1Sqlite as m,getBinding as P,registerMedia as x}from"bknd/adapter/cloudflare";import l from"node:process";import{$console as e}from"bknd/utils";async function g(n){try{let{version:r}=await import("wrangler/package.json",{with:{type:"json"}}).then(t=>t.default);e.log("Using wrangler version",r);let{getPlatformProxy:o}=await import("wrangler");return o(n)}catch(r){e.error("Failed to import wrangler",String(r));let o=import.meta.resolve("wrangler");e.log("Wrangler resolved to",o),o?.split("/").pop()?.endsWith(".json")&&e.error("You have a `wrangler.json` in your current directory. Please change to .jsonc or .toml")}l.exit(1)}function b(n={},r){let o=typeof r?.useProxy=="boolean"?r.useProxy:l.env.PROXY==="1",t;e.log("Using cloudflare platform proxy");async function a(i){return o?(t||(t=await g(r?.proxyOptions),l.on("exit",()=>{t?.dispose()})),t.env):i||{}}return{app:async i=>{let p=await a(i),f=o?P(p,"D1Database"):void 0,y=typeof n.app=="function"?await n.app(p):n,d=o&&f?m({binding:f.value}):y.connection;return{...y,beforeBuild:async(s,u)=>{if(!o)return;let c=await a();x(c,u),await n?.beforeBuild?.(s,u)},bindings:async s=>await n?.bindings?.(await a(s))||{},connection:d}}}}export{b as withPlatformProxy};