mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 20:37:21 +00:00
reworked html serving, added new permissions for api/auth, updated adapters
This commit is contained in:
@@ -1,15 +1,37 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { App, type CreateAppConfig } from "bknd";
|
||||
import { LibsqlConnection } from "bknd/data";
|
||||
import { serveStatic } from "hono/bun";
|
||||
|
||||
let app: App;
|
||||
export function serve(config: CreateAppConfig, distPath?: string) {
|
||||
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 function serve(_config: Partial<CreateAppConfig> = {}, distPath?: string) {
|
||||
const root = path.resolve(distPath ?? "./node_modules/bknd/dist", "static");
|
||||
let app: App;
|
||||
|
||||
return async (req: Request) => {
|
||||
if (!app) {
|
||||
app = App.create(config);
|
||||
const connection = await getConnection(_config.connection);
|
||||
app = App.create({
|
||||
..._config,
|
||||
connection
|
||||
});
|
||||
|
||||
app.emgr.on(
|
||||
"app-built",
|
||||
@@ -20,7 +42,7 @@ export function serve(config: CreateAppConfig, distPath?: string) {
|
||||
root
|
||||
})
|
||||
);
|
||||
app.module?.server?.setAdminHtml(await readFile(root + "/index.html", "utf-8"));
|
||||
app.registerAdminController();
|
||||
},
|
||||
"sync"
|
||||
);
|
||||
@@ -28,6 +50,6 @@ export function serve(config: CreateAppConfig, distPath?: string) {
|
||||
await app.build();
|
||||
}
|
||||
|
||||
return app.modules.server.fetch(req);
|
||||
return app.fetch(req);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -113,11 +113,10 @@ async function getFresh(config: BkndConfig, { env, html }: Context) {
|
||||
"sync"
|
||||
);
|
||||
}
|
||||
|
||||
await app.build();
|
||||
|
||||
if (config?.setAdminHtml !== false) {
|
||||
app.module.server.setAdminHtml(html);
|
||||
if (config.setAdminHtml) {
|
||||
app.registerAdminController({ html });
|
||||
}
|
||||
|
||||
return app;
|
||||
@@ -147,6 +146,7 @@ async function getCached(
|
||||
await cache.delete(key);
|
||||
return c.json({ message: "Cache cleared" });
|
||||
});
|
||||
app.registerAdminController({ html });
|
||||
|
||||
config.onBuilt!(app);
|
||||
},
|
||||
@@ -163,13 +163,13 @@ async function getCached(
|
||||
);
|
||||
|
||||
await app.build();
|
||||
if (!cachedConfig) {
|
||||
saveConfig(app.toJSON(true));
|
||||
|
||||
if (config.setAdminHtml) {
|
||||
app.registerAdminController({ html });
|
||||
}
|
||||
|
||||
//addAssetsRoute(app, manifest);
|
||||
if (config?.setAdminHtml !== false) {
|
||||
app.module.server.setAdminHtml(html);
|
||||
if (!cachedConfig) {
|
||||
saveConfig(app.toJSON(true));
|
||||
}
|
||||
|
||||
return app;
|
||||
@@ -212,10 +212,6 @@ export class DurableBkndApp extends DurableObject {
|
||||
colo: context.colo
|
||||
});
|
||||
});
|
||||
|
||||
if (options?.setAdminHtml !== false) {
|
||||
app.module.server.setAdminHtml(options.html);
|
||||
}
|
||||
},
|
||||
"sync"
|
||||
);
|
||||
|
||||
82
app/src/adapter/node/index.ts
Normal file
82
app/src/adapter/node/index.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
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";
|
||||
import type { Manifest } from "vite";
|
||||
|
||||
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 = {
|
||||
relativeDistPath?: string;
|
||||
viteManifest?: Manifest;
|
||||
port?: number;
|
||||
hostname?: string;
|
||||
listener?: Parameters<typeof honoServe>[1];
|
||||
};
|
||||
|
||||
export function serve(_config: Partial<CreateAppConfig> = {}, options: NodeAdapterOptions = {}) {
|
||||
const root = path.relative(
|
||||
process.cwd(),
|
||||
path.resolve(options.relativeDistPath ?? "./node_modules/bknd/dist", "static")
|
||||
);
|
||||
let app: App;
|
||||
|
||||
honoServe(
|
||||
{
|
||||
port: options.port ?? 1337,
|
||||
hostname: options.hostname,
|
||||
fetch: async (req: Request) => {
|
||||
if (!app) {
|
||||
const connection = await getConnection(_config.connection);
|
||||
app = App.create({
|
||||
..._config,
|
||||
connection
|
||||
});
|
||||
|
||||
const viteManifest =
|
||||
options.viteManifest ??
|
||||
JSON.parse(await readFile(path.resolve(root, ".vite/manifest.json"), "utf-8"));
|
||||
|
||||
app.emgr.on(
|
||||
"app-built",
|
||||
async () => {
|
||||
app.modules.server.get(
|
||||
"/assets/*",
|
||||
serveStatic({
|
||||
root
|
||||
})
|
||||
);
|
||||
app.registerAdminController({
|
||||
viteManifest
|
||||
});
|
||||
},
|
||||
"sync"
|
||||
);
|
||||
|
||||
await app.build();
|
||||
}
|
||||
|
||||
return app.fetch(req);
|
||||
}
|
||||
},
|
||||
options.listener
|
||||
);
|
||||
}
|
||||
@@ -31,7 +31,7 @@ function setAppBuildListener(app: App, config: BkndConfig, html: string) {
|
||||
"app-built",
|
||||
async () => {
|
||||
await config.onBuilt?.(app);
|
||||
app.module.server.setAdminHtml(html);
|
||||
app.registerAdminController();
|
||||
app.module.server.client.get("/assets/!*", serveStatic({ root: "./" }));
|
||||
},
|
||||
"sync"
|
||||
|
||||
Reference in New Issue
Block a user