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) 3.16 kB
import E from"node:path";import{createRuntimeApp as S}from"bknd/adapter";import{config as C}from"bknd";import{serveStatic as b}from"hono/bun";async function A({distPath:i,...t}={},e={},r){let n=E.resolve(i??"./node_modules/bknd/dist","static");return l(),await S({serveStatic:b({root:n}),...t},e??process.env,r)}function x(i={},t={},e){let r;return async n=>(r||(r=await A(i,t??process.env,e)),r.fetch(n))}function I({distPath:i,connection:t,initialConfig:e,options:r,port:n=C.server.default_port,onBuilt:o,buildConfig:a,adminOptions:p,serveStatic:y,...v}={},B={},w){Bun.serve({...v,port:n,fetch:x({connection:t,initialConfig:e,options:r,onBuilt:o,buildConfig:a,adminOptions:p,distPath:i,serveStatic:y},B,w)}),console.info(`Server is running on http://localhost:${n}`)}import{registries as j}from"bknd";import{readFile as O,readdir as F,stat as c,unlink as P,writeFile as R}from"node:fs/promises";import{StorageAdapter as $,guessMimeType as u}from"bknd";import{parse as L,s as f,isFile as m}from"bknd/utils";var g=f.object({path:f.string({default:"./"})},{title:"Local",description:"Local file system storage",additionalProperties:!1}),s=class extends ${config;constructor(t={}){super(),this.config=L(g,t)}getSchema(){return g}getName(){return"local"}async listObjects(t){let e=await F(this.config.path);return await Promise.all(e.filter(n=>!t||n.startsWith(t)).map(async n=>{let o=await c(`${this.config.path}/${n}`);return{key:n,last_modified:o.mtime,size:o.size}}))}async computeEtag(t){let e=m(t)?t:new Response(t),r=await crypto.subtle.digest("SHA-256",await e.arrayBuffer());return`"${Array.from(new Uint8Array(r)).map(a=>a.toString(16).padStart(2,"0")).join("")}"`}async putObject(t,e){if(e===null)throw new Error("Body is empty");let r=`${this.config.path}/${t}`;return await R(r,m(e)?e.stream():e),await this.computeEtag(e)}async deleteObject(t){try{await P(`${this.config.path}/${t}`)}catch{}}async objectExists(t){try{return(await c(`${this.config.path}/${t}`)).isFile()}catch{return!1}}async getObject(t,e){try{let r=await O(`${this.config.path}/${t}`),n=u(t);return new Response(r,{status:200,headers:{"Content-Type":n||"application/octet-stream","Content-Length":r.length.toString()}})}catch{return new Response("",{status:404})}}getObjectUrl(t){throw new Error("Method not implemented.")}async getObjectMeta(t){let e=await c(`${this.config.path}/${t}`);return{type:u(t)||"application/octet-stream",size:e.size}}toJSON(t){return{type:this.getName(),config:this.config}}};var d=!1;function l(){return d||(j.media.register("local",s),d=!0),(i={})=>new s(i).toJSON(!0)}import{Database as h}from"bun:sqlite";import{genericSqlite as q}from"bknd";function Y(i){let t;return i?"database"in i?t=i.database:t=new h(i.url):t=new h(":memory:"),q("bun-sqlite",t,e=>{let r=n=>t.prepare(n);return{db:t,query:e.buildQueryFn({all:(n,o)=>r(n).all(...o||[]),run:(n,o)=>{let{changes:a,lastInsertRowid:p}=r(n).run(...o||[]);return{insertId:e.parseBigInt(p),numAffectedRows:e.parseBigInt(a)}}}),close:()=>t.close()}})}export{s as StorageLocalAdapter,Y as bunSqlite,A as createApp,x as createHandler,g as localAdapterConfig,l as registerLocalMediaAdapter,I as serve};