mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 21:06:04 +00:00
added aws lambda adapter + improvements to handle concurrency
This commit is contained in:
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));
|
||||
|
||||
Reference in New Issue
Block a user