mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 20:37:21 +00:00
public commit
This commit is contained in:
33
app/src/adapter/bun/bun.adapter.ts
Normal file
33
app/src/adapter/bun/bun.adapter.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { App, type CreateAppConfig } from "bknd";
|
||||
import { serveStatic } from "hono/bun";
|
||||
|
||||
let app: App;
|
||||
export function serve(config: CreateAppConfig, distPath?: string) {
|
||||
const root = path.resolve(distPath ?? "./node_modules/bknd/dist", "static");
|
||||
|
||||
return async (req: Request) => {
|
||||
if (!app) {
|
||||
app = App.create(config);
|
||||
|
||||
app.emgr.on(
|
||||
"app-built",
|
||||
async () => {
|
||||
app.modules.server.get(
|
||||
"/assets/*",
|
||||
serveStatic({
|
||||
root
|
||||
})
|
||||
);
|
||||
app.module?.server?.setAdminHtml(await readFile(root + "/index.html", "utf-8"));
|
||||
},
|
||||
"sync"
|
||||
);
|
||||
|
||||
await app.build();
|
||||
}
|
||||
|
||||
return app.modules.server.fetch(req);
|
||||
};
|
||||
}
|
||||
1
app/src/adapter/bun/index.ts
Normal file
1
app/src/adapter/bun/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./bun.adapter";
|
||||
267
app/src/adapter/cloudflare/cloudflare-workers.adapter.ts
Normal file
267
app/src/adapter/cloudflare/cloudflare-workers.adapter.ts
Normal file
@@ -0,0 +1,267 @@
|
||||
import { DurableObject } from "cloudflare:workers";
|
||||
import { App, type CreateAppConfig } from "bknd";
|
||||
import { Hono } from "hono";
|
||||
import { serveStatic } from "hono/cloudflare-workers";
|
||||
import type { BkndConfig, CfBkndModeCache } from "../index";
|
||||
|
||||
// @ts-ignore
|
||||
//import manifest from "__STATIC_CONTENT_MANIFEST";
|
||||
|
||||
import _html from "../../static/index.html";
|
||||
|
||||
type Context = {
|
||||
request: Request;
|
||||
env: any;
|
||||
ctx: ExecutionContext;
|
||||
manifest: any;
|
||||
html: string;
|
||||
};
|
||||
|
||||
export function serve(_config: BkndConfig, manifest?: string, overrideHtml?: string) {
|
||||
const html = overrideHtml ?? _html;
|
||||
return {
|
||||
async fetch(request: Request, env: any, ctx: ExecutionContext) {
|
||||
const url = new URL(request.url);
|
||||
|
||||
if (manifest) {
|
||||
const pathname = url.pathname.slice(1);
|
||||
const assetManifest = JSON.parse(manifest);
|
||||
if (pathname && pathname in assetManifest) {
|
||||
const hono = new Hono();
|
||||
|
||||
hono.all("*", async (c, next) => {
|
||||
const res = await serveStatic({
|
||||
path: `./${pathname}`,
|
||||
manifest,
|
||||
onNotFound: (path) => console.log("not found", path)
|
||||
})(c as any, next);
|
||||
if (res instanceof Response) {
|
||||
const ttl = pathname.startsWith("assets/")
|
||||
? 60 * 60 * 24 * 365 // 1 year
|
||||
: 60 * 5; // 5 minutes
|
||||
res.headers.set("Cache-Control", `public, max-age=${ttl}`);
|
||||
return res;
|
||||
}
|
||||
|
||||
return c.notFound();
|
||||
});
|
||||
|
||||
return hono.fetch(request, env);
|
||||
}
|
||||
}
|
||||
|
||||
const config = {
|
||||
..._config,
|
||||
setAdminHtml: _config.setAdminHtml ?? !!manifest
|
||||
};
|
||||
const context = { request, env, ctx, manifest, html };
|
||||
const mode = config.cloudflare?.mode?.(env);
|
||||
|
||||
if (!mode) {
|
||||
console.log("serving fresh...");
|
||||
const app = await getFresh(config, context);
|
||||
return app.fetch(request, env);
|
||||
} else if ("cache" in mode) {
|
||||
console.log("serving cached...");
|
||||
const app = await getCached(config as any, context);
|
||||
return app.fetch(request, env);
|
||||
} else if ("durableObject" in mode) {
|
||||
console.log("serving durable...");
|
||||
|
||||
if (config.onBuilt) {
|
||||
console.log("onBuilt() is not supported with DurableObject mode");
|
||||
}
|
||||
|
||||
const start = performance.now();
|
||||
|
||||
const durable = mode.durableObject;
|
||||
const id = durable.idFromName(mode.key);
|
||||
const stub = durable.get(id) as unknown as DurableBkndApp;
|
||||
|
||||
const create_config = typeof config.app === "function" ? config.app(env) : config.app;
|
||||
|
||||
const res = await stub.fire(request, {
|
||||
config: create_config,
|
||||
html,
|
||||
keepAliveSeconds: mode.keepAliveSeconds,
|
||||
setAdminHtml: config.setAdminHtml
|
||||
});
|
||||
|
||||
const headers = new Headers(res.headers);
|
||||
headers.set("X-TTDO", String(performance.now() - start));
|
||||
|
||||
return new Response(res.body, {
|
||||
status: res.status,
|
||||
statusText: res.statusText,
|
||||
headers
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
async function getFresh(config: BkndConfig, { env, html }: Context) {
|
||||
const create_config = typeof config.app === "function" ? config.app(env) : config.app;
|
||||
const app = App.create(create_config);
|
||||
|
||||
if (config.onBuilt) {
|
||||
app.emgr.onEvent(
|
||||
App.Events.AppBuiltEvent,
|
||||
async ({ params: { app } }) => {
|
||||
config.onBuilt!(app);
|
||||
},
|
||||
"sync"
|
||||
);
|
||||
}
|
||||
|
||||
await app.build();
|
||||
|
||||
if (config?.setAdminHtml !== false) {
|
||||
app.module.server.setAdminHtml(html);
|
||||
}
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
async function getCached(
|
||||
config: BkndConfig & { cloudflare: { mode: CfBkndModeCache } },
|
||||
{ env, html, ctx }: Context
|
||||
) {
|
||||
const { cache, key } = config.cloudflare.mode(env) as ReturnType<CfBkndModeCache>;
|
||||
const create_config = typeof config.app === "function" ? config.app(env) : config.app;
|
||||
|
||||
const cachedConfig = await cache.get(key);
|
||||
const initialConfig = cachedConfig ? JSON.parse(cachedConfig) : undefined;
|
||||
|
||||
const app = App.create({ ...create_config, initialConfig });
|
||||
|
||||
async function saveConfig(__config: any) {
|
||||
ctx.waitUntil(cache.put(key, JSON.stringify(__config)));
|
||||
}
|
||||
|
||||
if (config.onBuilt) {
|
||||
app.emgr.onEvent(
|
||||
App.Events.AppBuiltEvent,
|
||||
async ({ params: { app } }) => {
|
||||
app.module.server.client.get("/__bknd/cache", async (c) => {
|
||||
await cache.delete(key);
|
||||
return c.json({ message: "Cache cleared" });
|
||||
});
|
||||
|
||||
config.onBuilt!(app);
|
||||
},
|
||||
"sync"
|
||||
);
|
||||
}
|
||||
|
||||
app.emgr.onEvent(
|
||||
App.Events.AppConfigUpdatedEvent,
|
||||
async ({ params: { app } }) => {
|
||||
saveConfig(app.toJSON(true));
|
||||
},
|
||||
"sync"
|
||||
);
|
||||
|
||||
await app.build();
|
||||
if (!cachedConfig) {
|
||||
saveConfig(app.toJSON(true));
|
||||
}
|
||||
|
||||
//addAssetsRoute(app, manifest);
|
||||
if (config?.setAdminHtml !== false) {
|
||||
app.module.server.setAdminHtml(html);
|
||||
}
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
export class DurableBkndApp extends DurableObject {
|
||||
protected id = Math.random().toString(36).slice(2);
|
||||
protected app?: App;
|
||||
protected interval?: any;
|
||||
|
||||
async fire(
|
||||
request: Request,
|
||||
options: {
|
||||
config: CreateAppConfig;
|
||||
html: string;
|
||||
keepAliveSeconds?: number;
|
||||
setAdminHtml?: boolean;
|
||||
}
|
||||
) {
|
||||
let buildtime = 0;
|
||||
if (!this.app) {
|
||||
const start = performance.now();
|
||||
const config = options.config;
|
||||
|
||||
// change protocol to websocket if libsql
|
||||
if ("type" in config.connection && config.connection.type === "libsql") {
|
||||
config.connection.config.protocol = "wss";
|
||||
}
|
||||
|
||||
this.app = App.create(config);
|
||||
this.app.emgr.onEvent(
|
||||
App.Events.AppBuiltEvent,
|
||||
async ({ params: { app } }) => {
|
||||
app.modules.server.get("/__do", async (c) => {
|
||||
// @ts-ignore
|
||||
const context: any = c.req.raw.cf ? c.req.raw.cf : c.env.cf;
|
||||
return c.json({
|
||||
id: this.id,
|
||||
keepAlive: options?.keepAliveSeconds,
|
||||
colo: context.colo
|
||||
});
|
||||
});
|
||||
|
||||
if (options?.setAdminHtml !== false) {
|
||||
app.module.server.setAdminHtml(options.html);
|
||||
}
|
||||
},
|
||||
"sync"
|
||||
);
|
||||
|
||||
await this.app.build();
|
||||
|
||||
buildtime = performance.now() - start;
|
||||
}
|
||||
|
||||
if (options?.keepAliveSeconds) {
|
||||
this.keepAlive(options.keepAliveSeconds);
|
||||
}
|
||||
|
||||
console.log("id", this.id);
|
||||
const res = await this.app!.fetch(request);
|
||||
const headers = new Headers(res.headers);
|
||||
headers.set("X-BuildTime", buildtime.toString());
|
||||
headers.set("X-DO-ID", this.id);
|
||||
|
||||
return new Response(res.body, {
|
||||
status: res.status,
|
||||
statusText: res.statusText,
|
||||
headers
|
||||
});
|
||||
}
|
||||
|
||||
protected keepAlive(seconds: number) {
|
||||
console.log("keep alive for", seconds);
|
||||
if (this.interval) {
|
||||
console.log("clearing, there is a new");
|
||||
clearInterval(this.interval);
|
||||
}
|
||||
|
||||
let i = 0;
|
||||
this.interval = setInterval(() => {
|
||||
i += 1;
|
||||
//console.log("keep-alive", i);
|
||||
if (i === seconds) {
|
||||
console.log("cleared");
|
||||
clearInterval(this.interval);
|
||||
|
||||
// ping every 30 seconds
|
||||
} else if (i % 30 === 0) {
|
||||
console.log("ping");
|
||||
this.app?.modules.ctx().connection.ping();
|
||||
}
|
||||
}, 1000);
|
||||
}
|
||||
}
|
||||
1
app/src/adapter/cloudflare/index.ts
Normal file
1
app/src/adapter/cloudflare/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./cloudflare-workers.adapter";
|
||||
36
app/src/adapter/index.ts
Normal file
36
app/src/adapter/index.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import type { App, CreateAppConfig } from "bknd";
|
||||
|
||||
export type CfBkndModeCache<Env = any> = (env: Env) => {
|
||||
cache: KVNamespace;
|
||||
key: string;
|
||||
};
|
||||
|
||||
export type CfBkndModeDurableObject<Env = any> = (env: Env) => {
|
||||
durableObject: DurableObjectNamespace;
|
||||
key: string;
|
||||
keepAliveSeconds?: number;
|
||||
};
|
||||
|
||||
export type CloudflareBkndConfig<Env = any> = {
|
||||
mode?: CfBkndModeCache | CfBkndModeDurableObject;
|
||||
forceHttps?: boolean;
|
||||
};
|
||||
|
||||
export type BkndConfig<Env = any> = {
|
||||
app: CreateAppConfig | ((env: Env) => CreateAppConfig);
|
||||
setAdminHtml?: boolean;
|
||||
server?: {
|
||||
port?: number;
|
||||
platform?: "node" | "bun";
|
||||
};
|
||||
cloudflare?: CloudflareBkndConfig<Env>;
|
||||
onBuilt?: (app: App) => Promise<void>;
|
||||
};
|
||||
|
||||
export type BkndConfigJson = {
|
||||
app: CreateAppConfig;
|
||||
setAdminHtml?: boolean;
|
||||
server?: {
|
||||
port?: number;
|
||||
};
|
||||
};
|
||||
1
app/src/adapter/nextjs/index.ts
Normal file
1
app/src/adapter/nextjs/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./nextjs.adapter";
|
||||
25
app/src/adapter/nextjs/nextjs.adapter.ts
Normal file
25
app/src/adapter/nextjs/nextjs.adapter.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { App, type CreateAppConfig } from "bknd";
|
||||
import { isDebug } from "bknd/core";
|
||||
|
||||
function getCleanRequest(req: Request) {
|
||||
// clean search params from "route" attribute
|
||||
const url = new URL(req.url);
|
||||
url.searchParams.delete("route");
|
||||
return new Request(url.toString(), {
|
||||
method: req.method,
|
||||
headers: req.headers,
|
||||
body: req.body
|
||||
});
|
||||
}
|
||||
|
||||
let app: App;
|
||||
export function serve(config: CreateAppConfig) {
|
||||
return async (req: Request) => {
|
||||
if (!app || isDebug()) {
|
||||
app = App.create(config);
|
||||
await app.build();
|
||||
}
|
||||
const request = getCleanRequest(req);
|
||||
return app.fetch(request, process.env);
|
||||
};
|
||||
}
|
||||
1
app/src/adapter/remix/index.ts
Normal file
1
app/src/adapter/remix/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./remix.adapter";
|
||||
12
app/src/adapter/remix/remix.adapter.ts
Normal file
12
app/src/adapter/remix/remix.adapter.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { App, type CreateAppConfig } from "../../App";
|
||||
|
||||
let app: App;
|
||||
export function serve(config: CreateAppConfig) {
|
||||
return async (args: { request: Request }) => {
|
||||
if (!app) {
|
||||
app = App.create(config);
|
||||
await app.build();
|
||||
}
|
||||
return app.fetch(args.request);
|
||||
};
|
||||
}
|
||||
1
app/src/adapter/vite/index.ts
Normal file
1
app/src/adapter/vite/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./vite.adapter";
|
||||
82
app/src/adapter/vite/vite.adapter.ts
Normal file
82
app/src/adapter/vite/vite.adapter.ts
Normal file
@@ -0,0 +1,82 @@
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { serveStatic } from "@hono/node-server/serve-static";
|
||||
import { App } from "../../App";
|
||||
import type { BkndConfig } from "../index";
|
||||
|
||||
async function getHtml() {
|
||||
return readFile("index.html", "utf8");
|
||||
}
|
||||
function addViteScripts(html: string) {
|
||||
return html.replace(
|
||||
"<head>",
|
||||
`<script type="module">
|
||||
import RefreshRuntime from "/@react-refresh"
|
||||
RefreshRuntime.injectIntoGlobalHook(window)
|
||||
window.$RefreshReg$ = () => {}
|
||||
window.$RefreshSig$ = () => (type) => type
|
||||
window.__vite_plugin_react_preamble_installed__ = true
|
||||
</script>
|
||||
<script type="module" src="/@vite/client"></script>
|
||||
`
|
||||
);
|
||||
}
|
||||
|
||||
function createApp(config: BkndConfig, env: any) {
|
||||
const create_config = typeof config.app === "function" ? config.app(env) : config.app;
|
||||
return App.create(create_config);
|
||||
}
|
||||
|
||||
function setAppBuildListener(app: App, config: BkndConfig, html: string) {
|
||||
app.emgr.on(
|
||||
"app-built",
|
||||
async () => {
|
||||
await config.onBuilt?.(app);
|
||||
app.module.server.setAdminHtml(html);
|
||||
app.module.server.client.get("/assets/!*", serveStatic({ root: "./" }));
|
||||
},
|
||||
"sync"
|
||||
);
|
||||
}
|
||||
|
||||
export async function serveFresh(config: BkndConfig, _html?: string) {
|
||||
let html = _html;
|
||||
if (!html) {
|
||||
html = await getHtml();
|
||||
}
|
||||
|
||||
html = addViteScripts(html);
|
||||
|
||||
return {
|
||||
async fetch(request: Request, env: any, ctx: ExecutionContext) {
|
||||
const app = createApp(config, env);
|
||||
|
||||
setAppBuildListener(app, config, html);
|
||||
await app.build();
|
||||
|
||||
//console.log("routes", app.module.server.client.routes);
|
||||
return app.fetch(request, env, ctx);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let app: App;
|
||||
export async function serveCached(config: BkndConfig, _html?: string) {
|
||||
let html = _html;
|
||||
if (!html) {
|
||||
html = await getHtml();
|
||||
}
|
||||
|
||||
html = addViteScripts(html);
|
||||
|
||||
return {
|
||||
async fetch(request: Request, env: any, ctx: ExecutionContext) {
|
||||
if (!app) {
|
||||
app = createApp(config, env);
|
||||
setAppBuildListener(app, config, html);
|
||||
await app.build();
|
||||
}
|
||||
|
||||
return app.fetch(request, env, ctx);
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user