mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
added aws lambda adapter + improvements to handle concurrency
This commit is contained in:
@@ -216,6 +216,7 @@ async function buildAdapters() {
|
||||
await tsup.build(baseConfig("remix"));
|
||||
await tsup.build(baseConfig("bun"));
|
||||
await tsup.build(baseConfig("astro"));
|
||||
await tsup.build(baseConfig("aws"));
|
||||
await tsup.build(
|
||||
baseConfig("cloudflare", {
|
||||
external: [/^kysely/],
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"type": "module",
|
||||
"sideEffects": false,
|
||||
"bin": "./dist/cli/index.js",
|
||||
"version": "0.9.0-rc.1-7",
|
||||
"version": "0.9.0-rc.1-11",
|
||||
"description": "Lightweight Firebase/Supabase alternative built to run anywhere — incl. Next.js, Remix, Astro, Cloudflare, Bun, Node, AWS Lambda & more.",
|
||||
"homepage": "https://bknd.io",
|
||||
"repository": {
|
||||
@@ -191,6 +191,11 @@
|
||||
"import": "./dist/adapter/astro/index.js",
|
||||
"require": "./dist/adapter/astro/index.cjs"
|
||||
},
|
||||
"./adapter/aws": {
|
||||
"types": "./dist/types/adapter/aws/index.d.ts",
|
||||
"import": "./dist/adapter/aws/index.js",
|
||||
"require": "./dist/adapter/aws/index.cjs"
|
||||
},
|
||||
"./dist/main.css": "./dist/ui/main.css",
|
||||
"./dist/styles.css": "./dist/ui/styles.css",
|
||||
"./dist/manifest.json": "./dist/static/.vite/manifest.json"
|
||||
|
||||
@@ -58,6 +58,8 @@ export class App {
|
||||
adminController?: AdminController;
|
||||
private trigger_first_boot = false;
|
||||
private plugins: AppPlugin[];
|
||||
private _id: string = crypto.randomUUID();
|
||||
private _building: boolean = false;
|
||||
|
||||
constructor(
|
||||
private connection: Connection,
|
||||
@@ -90,6 +92,11 @@ export class App {
|
||||
server.use(async (c, next) => {
|
||||
c.set("app", this);
|
||||
await next();
|
||||
|
||||
try {
|
||||
// gracefully add the app id
|
||||
c.res.headers.set("X-bknd-id", this._id);
|
||||
} catch (e) {}
|
||||
});
|
||||
},
|
||||
});
|
||||
@@ -100,9 +107,18 @@ export class App {
|
||||
return this.modules.ctx().emgr;
|
||||
}
|
||||
|
||||
async build(options?: { sync?: boolean }) {
|
||||
async build(options?: { sync?: boolean; fetch?: boolean; forceBuild?: boolean }) {
|
||||
// prevent multiple concurrent builds
|
||||
if (this._building) {
|
||||
while (this._building) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
}
|
||||
if (!options?.forceBuild) return;
|
||||
}
|
||||
this._building = true;
|
||||
|
||||
if (options?.sync) this.modules.ctx().flags.sync_required = true;
|
||||
await this.modules.build();
|
||||
await this.modules.build({ fetch: options?.fetch });
|
||||
|
||||
const { guard, server } = this.modules.ctx();
|
||||
|
||||
@@ -127,6 +143,8 @@ export class App {
|
||||
app: this,
|
||||
});
|
||||
}
|
||||
|
||||
this._building = false;
|
||||
}
|
||||
|
||||
mutateConfig<Module extends keyof Modules>(module: Module) {
|
||||
|
||||
68
app/src/adapter/aws/aws-lambda.adapter.ts
Normal file
68
app/src/adapter/aws/aws-lambda.adapter.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import type { App } from "bknd";
|
||||
import { handle } from "hono/aws-lambda";
|
||||
import { type RuntimeBkndConfig, createRuntimeApp } from "bknd/adapter";
|
||||
|
||||
export type AwsLambdaBkndConfig = RuntimeBkndConfig & {
|
||||
assets?:
|
||||
| {
|
||||
mode: "local";
|
||||
root: string;
|
||||
}
|
||||
| {
|
||||
mode: "url";
|
||||
url: string;
|
||||
};
|
||||
};
|
||||
|
||||
let app: App;
|
||||
export async function createApp({
|
||||
adminOptions = false,
|
||||
assets,
|
||||
...config
|
||||
}: AwsLambdaBkndConfig = {}) {
|
||||
if (!app) {
|
||||
let additional: Partial<RuntimeBkndConfig> = {
|
||||
adminOptions,
|
||||
};
|
||||
|
||||
if (assets?.mode) {
|
||||
switch (assets.mode) {
|
||||
case "local":
|
||||
// @todo: serve static outside app context
|
||||
additional = {
|
||||
adminOptions: adminOptions === false ? undefined : adminOptions,
|
||||
serveStatic: (await import("@hono/node-server/serve-static")).serveStatic({
|
||||
root: assets.root,
|
||||
onFound: (path, c) => {
|
||||
c.res.headers.set("Cache-Control", "public, max-age=31536000");
|
||||
},
|
||||
}),
|
||||
};
|
||||
break;
|
||||
case "url":
|
||||
additional.adminOptions = {
|
||||
...(typeof adminOptions === "object" ? adminOptions : {}),
|
||||
assets_path: assets.url,
|
||||
};
|
||||
break;
|
||||
default:
|
||||
throw new Error("Invalid assets mode");
|
||||
}
|
||||
}
|
||||
|
||||
app = await createRuntimeApp({
|
||||
...config,
|
||||
...additional,
|
||||
});
|
||||
}
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
export function serveLambda(config: AwsLambdaBkndConfig = {}) {
|
||||
console.log("serving lambda");
|
||||
return async (event) => {
|
||||
const app = await createApp(config);
|
||||
return await handle(app.server)(event);
|
||||
};
|
||||
}
|
||||
1
app/src/adapter/aws/index.ts
Normal file
1
app/src/adapter/aws/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from "./aws-lambda.adapter";
|
||||
@@ -34,6 +34,7 @@ export function serve({
|
||||
port = config.server.default_port,
|
||||
onBuilt,
|
||||
buildConfig,
|
||||
adminOptions,
|
||||
...serveOptions
|
||||
}: BunBkndConfig = {}) {
|
||||
Bun.serve({
|
||||
@@ -46,6 +47,7 @@ export function serve({
|
||||
options,
|
||||
onBuilt,
|
||||
buildConfig,
|
||||
adminOptions,
|
||||
distPath,
|
||||
});
|
||||
return app.fetch(request);
|
||||
|
||||
@@ -14,6 +14,8 @@ export type FrameworkBkndConfig<Args = any> = BkndConfig<Args>;
|
||||
|
||||
export type RuntimeBkndConfig<Args = any> = BkndConfig<Args> & {
|
||||
distPath?: string;
|
||||
serveStatic?: MiddlewareHandler | [string, MiddlewareHandler];
|
||||
adminOptions?: AdminControllerOptions | false;
|
||||
};
|
||||
|
||||
export function makeConfig<Args = any>(config: BkndConfig<Args>, args?: Args): CreateAppConfig {
|
||||
@@ -55,14 +57,7 @@ export async function createFrameworkApp<Args = any>(
|
||||
}
|
||||
|
||||
export async function createRuntimeApp<Env = any>(
|
||||
{
|
||||
serveStatic,
|
||||
adminOptions,
|
||||
...config
|
||||
}: RuntimeBkndConfig & {
|
||||
serveStatic?: MiddlewareHandler | [string, MiddlewareHandler];
|
||||
adminOptions?: AdminControllerOptions | false;
|
||||
},
|
||||
{ serveStatic, adminOptions, ...config }: RuntimeBkndConfig,
|
||||
env?: Env,
|
||||
): Promise<App> {
|
||||
const app = App.create(makeConfig(config, env));
|
||||
|
||||
36
app/src/cli/commands/copy-assets.ts
Normal file
36
app/src/cli/commands/copy-assets.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { getRelativeDistPath } from "cli/utils/sys";
|
||||
import type { CliCommand } from "../types";
|
||||
import { Option } from "commander";
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import c from "picocolors";
|
||||
|
||||
export const copyAssets: CliCommand = (program) => {
|
||||
program
|
||||
.command("copy-assets")
|
||||
.description("copy static assets")
|
||||
.addOption(new Option("-o --out <directory>", "directory to copy to"))
|
||||
.addOption(new Option("-c --clean", "clean the output directory"))
|
||||
.action(action);
|
||||
};
|
||||
|
||||
async function action(options: { out?: string; clean?: boolean }) {
|
||||
const out = options.out ?? "static";
|
||||
|
||||
// clean "out" directory
|
||||
if (options.clean) {
|
||||
await fs.rm(out, { recursive: true, force: true });
|
||||
}
|
||||
|
||||
// recursively copy from src/assets to out using node fs
|
||||
const from = path.resolve(getRelativeDistPath(), "static");
|
||||
await fs.cp(from, out, { recursive: true });
|
||||
|
||||
// in out, move ".vite/manifest.json" to "manifest.json"
|
||||
await fs.rename(path.resolve(out, ".vite/manifest.json"), path.resolve(out, "manifest.json"));
|
||||
|
||||
// delete ".vite" directory in out
|
||||
await fs.rm(path.resolve(out, ".vite"), { recursive: true });
|
||||
|
||||
console.log(c.green(`Assets copied to: ${c.bold(out)}`));
|
||||
}
|
||||
@@ -4,3 +4,4 @@ export { run } from "./run";
|
||||
export { debug } from "./debug";
|
||||
export { user } from "./user";
|
||||
export { create } from "./create";
|
||||
export { copyAssets } from "./copy-assets";
|
||||
|
||||
@@ -468,13 +468,18 @@ export class ModuleManager {
|
||||
});
|
||||
}
|
||||
|
||||
async build() {
|
||||
async build(opts?: { fetch?: boolean }) {
|
||||
this.logger.context("build").log("version", this.version());
|
||||
this.logger.log("booted with", this._booted_with);
|
||||
|
||||
// if no config provided, try fetch from db
|
||||
if (this.version() === 0) {
|
||||
this.logger.context("no version").log("version is 0");
|
||||
if (this.version() === 0 || opts?.fetch === true) {
|
||||
if (this.version() === 0) {
|
||||
this.logger.context("no version").log("version is 0");
|
||||
} else {
|
||||
this.logger.context("force fetch").log("force fetch");
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await this.fetch();
|
||||
|
||||
|
||||
@@ -20,10 +20,11 @@ export class SystemApi extends ModuleApi<any> {
|
||||
return this.get<{ version: number } & ModuleConfigs>("config");
|
||||
}
|
||||
|
||||
readSchema(options?: { config?: boolean; secrets?: boolean }) {
|
||||
readSchema(options?: { config?: boolean; secrets?: boolean; fresh?: boolean }) {
|
||||
return this.get<ApiSchemaResponse>("schema", {
|
||||
config: options?.config ? 1 : 0,
|
||||
secrets: options?.secrets ? 1 : 0,
|
||||
fresh: options?.fresh ? 1 : 0,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -164,13 +164,23 @@ export class AdminController extends Controller {
|
||||
};
|
||||
|
||||
if (isProd) {
|
||||
// @ts-ignore
|
||||
const manifest = await import("bknd/dist/manifest.json", {
|
||||
assert: { type: "json" },
|
||||
});
|
||||
let manifest: any;
|
||||
if (this.options.assets_path.startsWith("http")) {
|
||||
manifest = await fetch(this.options.assets_path + "manifest.json", {
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
},
|
||||
}).then((res) => res.json());
|
||||
} else {
|
||||
// @ts-ignore
|
||||
manifest = await import("bknd/dist/manifest.json", {
|
||||
assert: { type: "json" },
|
||||
}).then((res) => res.default);
|
||||
}
|
||||
|
||||
// @todo: load all marked as entry (incl. css)
|
||||
assets.js = manifest.default["src/ui/main.tsx"].file;
|
||||
assets.css = manifest.default["src/ui/main.tsx"].css[0] as any;
|
||||
assets.js = manifest["src/ui/main.tsx"].file;
|
||||
assets.css = manifest["src/ui/main.tsx"].css[0] as any;
|
||||
}
|
||||
|
||||
const theme = configs.server.admin.color_scheme ?? "light";
|
||||
@@ -197,16 +207,8 @@ export class AdminController extends Controller {
|
||||
)}
|
||||
{isProd ? (
|
||||
<Fragment>
|
||||
<script
|
||||
type="module"
|
||||
CrossOrigin
|
||||
src={this.options.assets_path + assets?.js}
|
||||
/>
|
||||
<link
|
||||
rel="stylesheet"
|
||||
crossOrigin
|
||||
href={this.options.assets_path + assets?.css}
|
||||
/>
|
||||
<script type="module" src={this.options.assets_path + assets?.js} />
|
||||
<link rel="stylesheet" href={this.options.assets_path + assets?.css} />
|
||||
</Fragment>
|
||||
) : (
|
||||
<Fragment>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/// <reference types="@cloudflare/workers-types" />
|
||||
|
||||
import type { App } from "App";
|
||||
import { tbValidator as tb } from "core";
|
||||
import { $console, tbValidator as tb } from "core";
|
||||
import {
|
||||
StringEnum,
|
||||
Type,
|
||||
@@ -229,17 +229,23 @@ export class SystemController extends Controller {
|
||||
Type.Object({
|
||||
config: Type.Optional(booleanLike),
|
||||
secrets: Type.Optional(booleanLike),
|
||||
fresh: Type.Optional(booleanLike),
|
||||
}),
|
||||
),
|
||||
async (c) => {
|
||||
const module = c.req.param("module") as ModuleKey | undefined;
|
||||
const { config, secrets } = c.req.valid("query");
|
||||
const { config, secrets, fresh } = c.req.valid("query");
|
||||
|
||||
config && this.ctx.guard.throwUnlessGranted(SystemPermissions.configRead, c);
|
||||
secrets && this.ctx.guard.throwUnlessGranted(SystemPermissions.configReadSecrets, c);
|
||||
|
||||
const { version, ...schema } = this.app.getSchema();
|
||||
|
||||
if (fresh) {
|
||||
// in cases of concurrency, refetching schema/config must be always fresh
|
||||
await this.app.build({ fetch: true });
|
||||
}
|
||||
|
||||
if (module) {
|
||||
return c.json({
|
||||
module,
|
||||
@@ -265,14 +271,18 @@ export class SystemController extends Controller {
|
||||
"query",
|
||||
Type.Object({
|
||||
sync: Type.Optional(booleanLike),
|
||||
fetch: Type.Optional(booleanLike),
|
||||
}),
|
||||
),
|
||||
async (c) => {
|
||||
const { sync } = c.req.valid("query") as Record<string, boolean>;
|
||||
const options = c.req.valid("query") as Record<string, boolean>;
|
||||
this.ctx.guard.throwUnlessGranted(SystemPermissions.build, c);
|
||||
|
||||
await this.app.build({ sync });
|
||||
return c.json({ success: true, options: { sync } });
|
||||
await this.app.build(options);
|
||||
return c.json({
|
||||
success: true,
|
||||
options,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -47,19 +47,29 @@ export function BkndProvider({
|
||||
const api = useApi();
|
||||
|
||||
async function reloadSchema() {
|
||||
await fetchSchema(includeSecrets, true);
|
||||
await fetchSchema(includeSecrets, {
|
||||
force: true,
|
||||
fresh: true,
|
||||
});
|
||||
}
|
||||
|
||||
async function fetchSchema(_includeSecrets: boolean = false, force?: boolean) {
|
||||
async function fetchSchema(
|
||||
_includeSecrets: boolean = false,
|
||||
opts?: {
|
||||
force?: boolean;
|
||||
fresh?: boolean;
|
||||
},
|
||||
) {
|
||||
const requesting = withSecrets ? Fetching.Secrets : Fetching.Schema;
|
||||
if (fetching.current === requesting) return;
|
||||
|
||||
if (withSecrets && !force) return;
|
||||
if (withSecrets && opts?.force !== true) return;
|
||||
fetching.current = requesting;
|
||||
|
||||
const res = await api.system.readSchema({
|
||||
config: true,
|
||||
secrets: _includeSecrets,
|
||||
fresh: opts?.fresh,
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { IconSettings } from "@tabler/icons-react";
|
||||
import { IconRefresh, IconSettings } from "@tabler/icons-react";
|
||||
import { ucFirst } from "core/utils";
|
||||
import { useBknd } from "ui/client/bknd";
|
||||
import { Empty } from "ui/components/display/Empty";
|
||||
@@ -12,11 +12,16 @@ import { AuthSettings } from "./routes/auth.settings";
|
||||
import { DataSettings } from "./routes/data.settings";
|
||||
import { FlowsSettings } from "./routes/flows.settings";
|
||||
import { ServerSettings } from "./routes/server.settings";
|
||||
import { IconButton } from "ui/components/buttons/IconButton";
|
||||
|
||||
function SettingsSidebar() {
|
||||
const { version, schema } = useBknd();
|
||||
const { version, schema, actions } = useBknd();
|
||||
useBrowserTitle(["Settings"]);
|
||||
|
||||
async function handleRefresh() {
|
||||
await actions.reload();
|
||||
}
|
||||
|
||||
const modules = Object.keys(schema).map((key) => {
|
||||
return {
|
||||
title: schema[key].title ?? ucFirst(key),
|
||||
@@ -26,7 +31,14 @@ function SettingsSidebar() {
|
||||
|
||||
return (
|
||||
<AppShell.Sidebar>
|
||||
<AppShell.SectionHeader right={<span className="font-mono">v{version}</span>}>
|
||||
<AppShell.SectionHeader
|
||||
right={
|
||||
<div className="flex items-center gap-2">
|
||||
<span className="font-mono leading-none">v{version}</span>
|
||||
<IconButton Icon={IconRefresh} onClick={handleRefresh} />
|
||||
</div>
|
||||
}
|
||||
>
|
||||
Settings
|
||||
</AppShell.SectionHeader>
|
||||
<AppShell.Scrollable initialOffset={96}>
|
||||
|
||||
Reference in New Issue
Block a user