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:
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);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user