mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 20:37:21 +00:00
small refactorings and cleanups, improved bun/node adapter, updated docs
This commit is contained in:
@@ -1,10 +1,8 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import type { ServeStaticOptions } from "@hono/node-server/serve-static";
|
||||
import { type Config, createClient } from "@libsql/client/node";
|
||||
import { Connection, LibsqlConnection, SqliteLocalConnection } from "data";
|
||||
import type { Config } from "@libsql/client/node";
|
||||
import type { MiddlewareHandler } from "hono";
|
||||
import { fileExists, getDistPath, getRelativeDistPath } from "../../utils/sys";
|
||||
import open from "open";
|
||||
import { fileExists, getRelativeDistPath } from "../../utils/sys";
|
||||
|
||||
export const PLATFORMS = ["node", "bun"] as const;
|
||||
export type Platform = (typeof PLATFORMS)[number];
|
||||
@@ -33,7 +31,8 @@ export async function attachServeStatic(app: any, platform: Platform) {
|
||||
|
||||
export async function startServer(server: Platform, app: any, options: { port: number }) {
|
||||
const port = options.port;
|
||||
console.log("running on", server, port);
|
||||
console.log(`(using ${server} serve)`);
|
||||
|
||||
switch (server) {
|
||||
case "node": {
|
||||
// https://github.com/honojs/node-server/blob/main/src/response.ts#L88
|
||||
@@ -53,27 +52,9 @@ export async function startServer(server: Platform, app: any, options: { port: n
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Server listening on", "http://localhost:" + port);
|
||||
}
|
||||
|
||||
export async function getHtml() {
|
||||
return await readFile(path.resolve(getDistPath(), "static/index.html"), "utf-8");
|
||||
}
|
||||
|
||||
export function getConnection(connectionOrConfig?: Connection | Config): Connection {
|
||||
if (connectionOrConfig) {
|
||||
if (connectionOrConfig instanceof Connection) {
|
||||
return connectionOrConfig;
|
||||
}
|
||||
|
||||
if ("url" in connectionOrConfig) {
|
||||
return new LibsqlConnection(createClient(connectionOrConfig));
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Using in-memory database");
|
||||
return new LibsqlConnection(createClient({ url: ":memory:" }));
|
||||
//return new SqliteLocalConnection(new Database(":memory:"));
|
||||
const url = `http://localhost:${port}`;
|
||||
console.log(`Server listening on ${url}`);
|
||||
await open(url);
|
||||
}
|
||||
|
||||
export async function getConfigPath(filePath?: string) {
|
||||
|
||||
@@ -1,16 +1,13 @@
|
||||
import type { Config } from "@libsql/client/node";
|
||||
import { App } from "App";
|
||||
import { App, type CreateAppConfig } from "App";
|
||||
import type { BkndConfig } from "adapter";
|
||||
import type { CliCommand } from "cli/types";
|
||||
import { Option } from "commander";
|
||||
import type { Connection } from "data";
|
||||
import {
|
||||
PLATFORMS,
|
||||
type Platform,
|
||||
attachServeStatic,
|
||||
getConfigPath,
|
||||
getConnection,
|
||||
getHtml,
|
||||
startServer
|
||||
} from "./platform";
|
||||
|
||||
@@ -41,14 +38,14 @@ export const run: CliCommand = (program) => {
|
||||
};
|
||||
|
||||
type MakeAppConfig = {
|
||||
connection: Connection;
|
||||
connection?: CreateAppConfig["connection"];
|
||||
server?: { platform?: Platform };
|
||||
setAdminHtml?: boolean;
|
||||
onBuilt?: (app: App) => Promise<void>;
|
||||
};
|
||||
|
||||
async function makeApp(config: MakeAppConfig) {
|
||||
const app = new App(config.connection);
|
||||
const app = App.create({ connection: config.connection });
|
||||
|
||||
app.emgr.on(
|
||||
"app-built",
|
||||
@@ -99,9 +96,9 @@ async function action(options: {
|
||||
|
||||
let app: App;
|
||||
if (options.dbUrl || !configFilePath) {
|
||||
const connection = getConnection(
|
||||
options.dbUrl ? { url: options.dbUrl, authToken: options.dbToken } : undefined
|
||||
);
|
||||
const connection = options.dbUrl
|
||||
? { type: "libsql" as const, config: { url: options.dbUrl, authToken: options.dbToken } }
|
||||
: undefined;
|
||||
app = await makeApp({ connection, server: { platform: options.server } });
|
||||
} else {
|
||||
console.log("Using config from:", configFilePath);
|
||||
|
||||
Reference in New Issue
Block a user