cli: automatically register local adapter

This commit is contained in:
dswbx
2024-12-10 15:50:41 +01:00
parent 5cca911e9d
commit b7ec4982dc
9 changed files with 75 additions and 36 deletions

View File

@@ -6,14 +6,25 @@ import type { Serve, ServeOptions } from "bun";
import { serveStatic } from "hono/bun";
let app: App;
export async function createApp(_config: Partial<CreateAppConfig> = {}, distPath?: string) {
export type ExtendedAppCreateConfig = Partial<CreateAppConfig> & {
distPath?: string;
onBuilt?: (app: App) => Promise<void>;
buildOptions?: Parameters<App["build"]>[0];
};
export async function createApp({
distPath,
onBuilt,
buildOptions,
...config
}: ExtendedAppCreateConfig) {
const root = path.resolve(distPath ?? "./node_modules/bknd/dist", "static");
if (!app) {
app = App.create(_config);
app = App.create(config);
app.emgr.on(
"app-built",
app.emgr.onEvent(
App.Events.AppBuiltEvent,
async () => {
app.modules.server.get(
"/*",
@@ -22,20 +33,18 @@ export async function createApp(_config: Partial<CreateAppConfig> = {}, distPath
})
);
app.registerAdminController();
await onBuilt?.(app);
},
"sync"
);
await app.build();
await app.build(buildOptions);
}
return app;
}
export type BunAdapterOptions = Omit<ServeOptions, "fetch"> &
CreateAppConfig & {
distPath?: string;
};
export type BunAdapterOptions = Omit<ServeOptions, "fetch"> & ExtendedAppCreateConfig;
export function serve({
distPath,
@@ -44,13 +53,23 @@ export function serve({
plugins,
options,
port = 1337,
onBuilt,
buildOptions,
...serveOptions
}: BunAdapterOptions = {}) {
Bun.serve({
...serveOptions,
port,
fetch: async (request: Request) => {
const app = await createApp({ connection, initialConfig, plugins, options }, distPath);
const app = await createApp({
connection,
initialConfig,
plugins,
options,
onBuilt,
buildOptions,
distPath
});
return app.fetch(request);
}
});

View File

@@ -8,6 +8,8 @@ export type NodeAdapterOptions = CreateAppConfig & {
port?: number;
hostname?: string;
listener?: Parameters<typeof honoServe>[1];
onBuilt?: (app: App) => Promise<void>;
buildOptions?: Parameters<App["build"]>[0];
};
export function serve({
@@ -15,6 +17,8 @@ export function serve({
port = 1337,
hostname,
listener,
onBuilt,
buildOptions = {},
...config
}: NodeAdapterOptions = {}) {
const root = path.relative(
@@ -31,8 +35,8 @@ export function serve({
if (!app) {
app = App.create(config);
app.emgr.on(
"app-built",
app.emgr.onEvent(
App.Events.AppBuiltEvent,
async () => {
app.modules.server.get(
"/*",
@@ -41,11 +45,12 @@ export function serve({
})
);
app.registerAdminController();
await onBuilt?.(app);
},
"sync"
);
await app.build();
await app.build(buildOptions);
}
return app.fetch(req);

View File

@@ -8,8 +8,8 @@ function createApp(config: BkndConfig, env: any) {
}
function setAppBuildListener(app: App, config: BkndConfig, html?: string) {
app.emgr.on(
"app-built",
app.emgr.onEvent(
App.Events.AppBuiltEvent,
async () => {
await config.onBuilt?.(app);
if (config.setAdminHtml) {

View File

@@ -220,15 +220,23 @@ export class Authenticator<Strategies extends Record<string, Strategy> = Record<
}
private async getAuthCookie(c: Context): Promise<string | undefined> {
const secret = this.config.jwt.secret;
try {
const secret = this.config.jwt.secret;
const token = await getSignedCookie(c, secret, "auth");
if (typeof token !== "string") {
await deleteCookie(c, "auth", this.cookieOptions);
return undefined;
}
return token;
} catch (e: any) {
if (e instanceof Error) {
console.error("[Error:getAuthCookie]", e.message);
}
const token = await getSignedCookie(c, secret, "auth");
if (typeof token !== "string") {
await deleteCookie(c, "auth", this.cookieOptions);
return undefined;
}
return token;
}
async requestCookieRefresh(c: Context) {

View File

@@ -1,8 +1,10 @@
import type { Config } from "@libsql/client/node";
import { App, type CreateAppConfig } from "App";
import type { BkndConfig } from "adapter";
import { StorageLocalAdapter } from "adapter/node";
import type { CliCommand } from "cli/types";
import { Option } from "commander";
import { registries } from "modules/registries";
import {
PLATFORMS,
type Platform,
@@ -37,6 +39,12 @@ export const run: CliCommand = (program) => {
.action(action);
};
// automatically register local adapter
const local = StorageLocalAdapter.prototype.getName();
if (!registries.media.has(local)) {
registries.media.register(local, StorageLocalAdapter);
}
type MakeAppConfig = {
connection?: CreateAppConfig["connection"];
server?: { platform?: Platform };
@@ -47,8 +55,8 @@ type MakeAppConfig = {
async function makeApp(config: MakeAppConfig) {
const app = App.create({ connection: config.connection });
app.emgr.on(
"app-built",
app.emgr.onEvent(
App.Events.AppBuiltEvent,
async () => {
await attachServeStatic(app, config.server?.platform ?? "node");
app.registerAdminController();
@@ -68,8 +76,8 @@ export async function makeConfigApp(config: BkndConfig, platform?: Platform) {
const appConfig = typeof config.app === "function" ? config.app(process.env) : config.app;
const app = App.create(appConfig);
app.emgr.on(
"app-built",
app.emgr.onEvent(
App.Events.AppBuiltEvent,
async () => {
await attachServeStatic(app, platform ?? "node");
app.registerAdminController();

View File

@@ -69,7 +69,8 @@ export class SchemaObject<Schema extends TObject> {
forceParse: true,
skipMark: this.isForceParse()
});
const updatedConfig = noEmit ? valid : await this.onBeforeUpdate(this._config, valid);
// regardless of "noEmit" this should always be triggered
const updatedConfig = await this.onBeforeUpdate(this._config, valid);
this._value = updatedConfig;
this._config = Object.freeze(updatedConfig);

View File

@@ -41,6 +41,10 @@ export class Registry<
return this.items[name];
}
has(name: keyof Items): boolean {
return name in this.items;
}
all() {
return this.items;
}

View File

@@ -1,17 +1,11 @@
import { readFile, readdir, stat, unlink, writeFile } from "node:fs/promises";
import { type Static, Type, parse } from "core/utils";
import type {
FileBody,
FileListObject,
FileMeta,
FileUploadPayload,
StorageAdapter
} from "../../Storage";
import type { FileBody, FileListObject, FileMeta, StorageAdapter } from "../../Storage";
import { guessMimeType } from "../../mime-types";
export const localAdapterConfig = Type.Object(
{
path: Type.String()
path: Type.String({ default: "./" })
},
{ title: "Local" }
);