mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
small refactorings and cleanups, improved bun/node adapter, updated docs
This commit is contained in:
@@ -2,51 +2,34 @@ import path from "node:path";
|
||||
import { serve as honoServe } from "@hono/node-server";
|
||||
import { serveStatic } from "@hono/node-server/serve-static";
|
||||
import { App, type CreateAppConfig } from "bknd";
|
||||
import { LibsqlConnection } from "bknd/data";
|
||||
|
||||
async function getConnection(conn?: CreateAppConfig["connection"]) {
|
||||
if (conn) {
|
||||
if (LibsqlConnection.isConnection(conn)) {
|
||||
return conn;
|
||||
}
|
||||
|
||||
return new LibsqlConnection(conn.config);
|
||||
}
|
||||
|
||||
const createClient = await import("@libsql/client/node").then((m) => m.createClient);
|
||||
if (!createClient) {
|
||||
throw new Error('libsql client not found, you need to install "@libsql/client/node"');
|
||||
}
|
||||
|
||||
console.log("Using in-memory database");
|
||||
return new LibsqlConnection(createClient({ url: ":memory:" }));
|
||||
}
|
||||
|
||||
export type NodeAdapterOptions = {
|
||||
export type NodeAdapterOptions = CreateAppConfig & {
|
||||
relativeDistPath?: string;
|
||||
port?: number;
|
||||
hostname?: string;
|
||||
listener?: Parameters<typeof honoServe>[1];
|
||||
};
|
||||
|
||||
export function serve(_config: Partial<CreateAppConfig> = {}, options: NodeAdapterOptions = {}) {
|
||||
export function serve({
|
||||
relativeDistPath,
|
||||
port = 1337,
|
||||
hostname,
|
||||
listener,
|
||||
...config
|
||||
}: NodeAdapterOptions = {}) {
|
||||
const root = path.relative(
|
||||
process.cwd(),
|
||||
path.resolve(options.relativeDistPath ?? "./node_modules/bknd/dist", "static")
|
||||
path.resolve(relativeDistPath ?? "./node_modules/bknd/dist", "static")
|
||||
);
|
||||
let app: App;
|
||||
|
||||
honoServe(
|
||||
{
|
||||
port: options.port ?? 1337,
|
||||
hostname: options.hostname,
|
||||
port,
|
||||
hostname,
|
||||
fetch: async (req: Request) => {
|
||||
if (!app) {
|
||||
const connection = await getConnection(_config.connection);
|
||||
app = App.create({
|
||||
..._config,
|
||||
connection
|
||||
});
|
||||
app = App.create(config);
|
||||
|
||||
app.emgr.on(
|
||||
"app-built",
|
||||
@@ -68,6 +51,9 @@ export function serve(_config: Partial<CreateAppConfig> = {}, options: NodeAdapt
|
||||
return app.fetch(req);
|
||||
}
|
||||
},
|
||||
options.listener
|
||||
(connInfo) => {
|
||||
console.log(`Server is running on http://localhost:${connInfo.port}`);
|
||||
listener?.(connInfo);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user