mirror of
https://github.com/bknd-io/bknd/
synced 2026-08-02 08:06:00 +00:00
3f26c45dd9
* refactored adapters to run test suites * fix bun version for tests * added missing adapter tests and refactored examples to use `bknd.config.ts` where applicable
77 lines
1.8 KiB
TypeScript
77 lines
1.8 KiB
TypeScript
/// <reference types="bun-types" />
|
|
|
|
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<Env = BunEnv> = RuntimeBkndConfig<Env> & Omit<ServeOptions, "fetch">;
|
|
|
|
export async function createApp<Env = BunEnv>(
|
|
{ distPath, ...config }: BunBkndConfig<Env> = {},
|
|
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<Env = BunEnv>(
|
|
config: BunBkndConfig<Env> = {},
|
|
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<Env = BunEnv>(
|
|
{
|
|
distPath,
|
|
connection,
|
|
initialConfig,
|
|
options,
|
|
port = config.server.default_port,
|
|
onBuilt,
|
|
buildConfig,
|
|
adminOptions,
|
|
...serveOptions
|
|
}: BunBkndConfig<Env> = {},
|
|
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}`);
|
|
}
|