/// import path from "node:path"; import { type RuntimeBkndConfig, createRuntimeApp, type RuntimeOptions } from "bknd/adapter"; import { registerLocalMediaAdapter } from "bknd/adapter/node"; import { config } from "bknd/core"; import type { ServeOptions } from "bun"; import { serveStatic } from "hono/bun"; type BunEnv = Bun.Env; export type BunBkndConfig = RuntimeBkndConfig & Omit; export async function createApp( { distPath, ...config }: BunBkndConfig = {}, args: Env = {} as Env, opts?: RuntimeOptions, ) { const root = path.resolve(distPath ?? "./node_modules/bknd/dist", "static"); registerLocalMediaAdapter(); return await createRuntimeApp( { ...config, serveStatic: serveStatic({ root }), }, args ?? (process.env as Env), opts, ); } export function createHandler( config: BunBkndConfig = {}, args: Env = {} as Env, opts?: RuntimeOptions, ) { return async (req: Request) => { const app = await createApp(config, args ?? (process.env as Env), opts); return app.fetch(req); }; } export function serve( { distPath, connection, initialConfig, options, port = config.server.default_port, onBuilt, buildConfig, adminOptions, ...serveOptions }: BunBkndConfig = {}, args: Env = {} as Env, opts?: RuntimeOptions, ) { Bun.serve({ ...serveOptions, port, fetch: createHandler( { connection, initialConfig, options, onBuilt, buildConfig, adminOptions, distPath, }, args, opts, ), }); console.log(`Server is running on http://localhost:${port}`); }