optimize adapter imports to deduplicate

This commit is contained in:
dswbx
2025-01-25 14:13:34 +01:00
parent 89b1fd3909
commit 5b40d5eaf4
19 changed files with 64 additions and 64 deletions

View File

@@ -173,7 +173,6 @@ function baseConfig(adapter: string): tsup.Options {
], ],
metafile: true, metafile: true,
splitting: false, splitting: false,
treeshake: true,
onSuccess: async () => { onSuccess: async () => {
delayTypes(); delayTypes();
} }

View File

@@ -1,5 +1,5 @@
import { type FrameworkBkndConfig, createFrameworkApp } from "adapter";
import { Api, type ApiOptions, type App } from "bknd"; import { Api, type ApiOptions, type App } from "bknd";
import { type FrameworkBkndConfig, createFrameworkApp } from "bknd/adapter";
export type AstroBkndConfig<Args = TAstro> = FrameworkBkndConfig<Args>; export type AstroBkndConfig<Args = TAstro> = FrameworkBkndConfig<Args>;

View File

@@ -1 +1,2 @@
export * from "./astro.adapter"; export * from "./astro.adapter";
export { registerLocalMediaAdapter } from "bknd/adapter/node";

View File

@@ -2,10 +2,11 @@
import path from "node:path"; import path from "node:path";
import type { App } from "bknd"; import type { App } from "bknd";
import { type RuntimeBkndConfig, createRuntimeApp } from "bknd/adapter";
import { registerLocalMediaAdapter } from "bknd/adapter/node";
import { config } from "bknd/core";
import type { ServeOptions } from "bun"; import type { ServeOptions } from "bun";
import { config } from "core";
import { serveStatic } from "hono/bun"; import { serveStatic } from "hono/bun";
import { type RuntimeBkndConfig, createRuntimeApp } from "../index";
let app: App; let app: App;
@@ -15,9 +16,9 @@ export async function createApp({ distPath, ...config }: RuntimeBkndConfig = {})
const root = path.resolve(distPath ?? "./node_modules/bknd/dist", "static"); const root = path.resolve(distPath ?? "./node_modules/bknd/dist", "static");
if (!app) { if (!app) {
registerLocalMediaAdapter();
app = await createRuntimeApp({ app = await createRuntimeApp({
...config, ...config,
registerLocalMedia: true,
serveStatic: serveStatic({ root }) serveStatic: serveStatic({ root })
}); });
} }

View File

@@ -1,6 +1,6 @@
import type { FrameworkBkndConfig } from "bknd/adapter";
import { Hono } from "hono"; import { Hono } from "hono";
import { serveStatic } from "hono/cloudflare-workers"; import { serveStatic } from "hono/cloudflare-workers";
import type { FrameworkBkndConfig } from "../index";
import { getCached } from "./modes/cached"; import { getCached } from "./modes/cached";
import { getDurable } from "./modes/durable"; import { getDurable } from "./modes/durable";
import { getFresh, getWarm } from "./modes/fresh"; import { getFresh, getWarm } from "./modes/fresh";

View File

@@ -1,5 +1,5 @@
import { createRuntimeApp } from "adapter";
import { App } from "bknd"; import { App } from "bknd";
import { createRuntimeApp } from "bknd/adapter";
import type { CloudflareBkndConfig, Context } from "../index"; import type { CloudflareBkndConfig, Context } from "../index";
export async function getCached(config: CloudflareBkndConfig, { env, ctx, ...args }: Context) { export async function getCached(config: CloudflareBkndConfig, { env, ctx, ...args }: Context) {

View File

@@ -1,7 +1,7 @@
import { DurableObject } from "cloudflare:workers"; import { DurableObject } from "cloudflare:workers";
import { createRuntimeApp, makeConfig } from "adapter";
import type { CloudflareBkndConfig, Context } from "adapter/cloudflare";
import type { App, CreateAppConfig } from "bknd"; import type { App, CreateAppConfig } from "bknd";
import { createRuntimeApp, makeConfig } from "bknd/adapter";
import type { CloudflareBkndConfig, Context } from "../index";
export async function getDurable(config: CloudflareBkndConfig, ctx: Context) { export async function getDurable(config: CloudflareBkndConfig, ctx: Context) {
const { dobj } = config.bindings?.(ctx.env)!; const { dobj } = config.bindings?.(ctx.env)!;

View File

@@ -1,5 +1,5 @@
import { createRuntimeApp } from "adapter";
import type { App } from "bknd"; import type { App } from "bknd";
import { createRuntimeApp } from "bknd/adapter";
import type { CloudflareBkndConfig, Context } from "../index"; import type { CloudflareBkndConfig, Context } from "../index";
export async function makeApp(config: CloudflareBkndConfig, ctx: Context) { export async function makeApp(config: CloudflareBkndConfig, ctx: Context) {

View File

@@ -1,8 +1,6 @@
import type { IncomingMessage } from "node:http"; import { App, type CreateAppConfig } from "bknd";
import { App, type CreateAppConfig, registries } from "bknd"; import { config as $config } from "bknd/core";
import { config as $config } from "core";
import type { MiddlewareHandler } from "hono"; import type { MiddlewareHandler } from "hono";
import { StorageLocalAdapter } from "media/storage/adapters/StorageLocalAdapter";
import type { AdminControllerOptions } from "modules/server/AdminController"; import type { AdminControllerOptions } from "modules/server/AdminController";
export type BkndConfig<Args = any> = CreateAppConfig & { export type BkndConfig<Args = any> = CreateAppConfig & {
@@ -18,34 +16,6 @@ export type RuntimeBkndConfig<Args = any> = BkndConfig<Args> & {
distPath?: string; distPath?: string;
}; };
export function nodeRequestToRequest(req: IncomingMessage): Request {
let protocol = "http";
try {
protocol = req.headers["x-forwarded-proto"] as string;
} catch (e) {}
const host = req.headers.host;
const url = `${protocol}://${host}${req.url}`;
const headers = new Headers();
for (const [key, value] of Object.entries(req.headers)) {
if (Array.isArray(value)) {
headers.append(key, value.join(", "));
} else if (value) {
headers.append(key, value);
}
}
const method = req.method || "GET";
return new Request(url, {
method,
headers
});
}
export function registerLocalMediaAdapter() {
registries.media.register("local", StorageLocalAdapter);
}
export function makeConfig<Args = any>(config: BkndConfig<Args>, args?: Args): CreateAppConfig { export function makeConfig<Args = any>(config: BkndConfig<Args>, args?: Args): CreateAppConfig {
let additionalConfig: CreateAppConfig = {}; let additionalConfig: CreateAppConfig = {};
if ("app" in config && config.app) { if ("app" in config && config.app) {
@@ -87,20 +57,14 @@ export async function createFrameworkApp<Args = any>(
export async function createRuntimeApp<Env = any>( export async function createRuntimeApp<Env = any>(
{ {
serveStatic, serveStatic,
registerLocalMedia,
adminOptions, adminOptions,
...config ...config
}: RuntimeBkndConfig & { }: RuntimeBkndConfig & {
serveStatic?: MiddlewareHandler | [string, MiddlewareHandler]; serveStatic?: MiddlewareHandler | [string, MiddlewareHandler];
registerLocalMedia?: boolean;
adminOptions?: AdminControllerOptions | false; adminOptions?: AdminControllerOptions | false;
}, },
env?: Env env?: Env
): Promise<App> { ): Promise<App> {
if (registerLocalMedia) {
registerLocalMediaAdapter();
}
const app = App.create(makeConfig(config, env)); const app = App.create(makeConfig(config, env));
app.emgr.onEvent( app.emgr.onEvent(

View File

@@ -1 +1,2 @@
export * from "./nextjs.adapter"; export * from "./nextjs.adapter";
export { registerLocalMediaAdapter } from "bknd/adapter/node";

View File

@@ -1,6 +1,7 @@
import type { IncomingMessage, ServerResponse } from "node:http"; import type { IncomingMessage, ServerResponse } from "node:http";
import { Api, type App } from "bknd"; import { Api, type App } from "bknd";
import { type FrameworkBkndConfig, createFrameworkApp, nodeRequestToRequest } from "../index"; import { type FrameworkBkndConfig, createFrameworkApp } from "bknd/adapter";
import { nodeRequestToRequest } from "bknd/adapter/node";
export type NextjsBkndConfig = FrameworkBkndConfig & { export type NextjsBkndConfig = FrameworkBkndConfig & {
cleanSearch?: string[]; cleanSearch?: string[];

View File

@@ -1,6 +1,37 @@
export * from "./node.adapter"; import type { IncomingMessage } from "node:http";
export { import { registries } from "bknd";
StorageLocalAdapter, import {
type LocalAdapterConfig type LocalAdapterConfig,
StorageLocalAdapter
} from "../../media/storage/adapters/StorageLocalAdapter"; } from "../../media/storage/adapters/StorageLocalAdapter";
export { registerLocalMediaAdapter } from "../index";
export * from "./node.adapter";
export { StorageLocalAdapter, type LocalAdapterConfig };
export function nodeRequestToRequest(req: IncomingMessage): Request {
let protocol = "http";
try {
protocol = req.headers["x-forwarded-proto"] as string;
} catch (e) {}
const host = req.headers.host;
const url = `${protocol}://${host}${req.url}`;
const headers = new Headers();
for (const [key, value] of Object.entries(req.headers)) {
if (Array.isArray(value)) {
headers.append(key, value.join(", "));
} else if (value) {
headers.append(key, value);
}
}
const method = req.method || "GET";
return new Request(url, {
method,
headers
});
}
export function registerLocalMediaAdapter() {
registries.media.register("local", StorageLocalAdapter);
}

View File

@@ -1,9 +1,10 @@
import path from "node:path"; import path from "node:path";
import { serve as honoServe } from "@hono/node-server"; import { serve as honoServe } from "@hono/node-server";
import { serveStatic } from "@hono/node-server/serve-static"; import { serveStatic } from "@hono/node-server/serve-static";
import { registerLocalMediaAdapter } from "adapter/node/index";
import type { App } from "bknd"; import type { App } from "bknd";
import { config as $config } from "core"; import { type RuntimeBkndConfig, createRuntimeApp } from "bknd/adapter";
import { type RuntimeBkndConfig, createRuntimeApp } from "../index"; import { config as $config } from "bknd/core";
export type NodeBkndConfig = RuntimeBkndConfig & { export type NodeBkndConfig = RuntimeBkndConfig & {
port?: number; port?: number;
@@ -37,9 +38,9 @@ export function serve({
hostname, hostname,
fetch: async (req: Request) => { fetch: async (req: Request) => {
if (!app) { if (!app) {
registerLocalMediaAdapter();
app = await createRuntimeApp({ app = await createRuntimeApp({
...config, ...config,
registerLocalMedia: true,
serveStatic: serveStatic({ root }) serveStatic: serveStatic({ root })
}); });
} }

View File

@@ -1,2 +1,3 @@
export * from "./remix.adapter"; export * from "./remix.adapter";
export * from "./AdminPage"; export * from "./AdminPage";
export { registerLocalMediaAdapter } from "bknd/adapter/node";

View File

@@ -1,5 +1,5 @@
import { type FrameworkBkndConfig, createFrameworkApp } from "adapter";
import type { App } from "bknd"; import type { App } from "bknd";
import { type FrameworkBkndConfig, createFrameworkApp } from "bknd/adapter";
import { Api } from "bknd/client"; import { Api } from "bknd/client";
export type RemixBkndConfig<Args = RemixContext> = FrameworkBkndConfig<Args>; export type RemixBkndConfig<Args = RemixContext> = FrameworkBkndConfig<Args>;

View File

@@ -1 +1,2 @@
export * from "./vite.adapter"; export * from "./vite.adapter";
export { registerLocalMediaAdapter } from "bknd/adapter/node";

View File

@@ -1,7 +1,8 @@
import { serveStatic } from "@hono/node-server/serve-static"; import { serveStatic } from "@hono/node-server/serve-static";
import { type DevServerOptions, default as honoViteDevServer } from "@hono/vite-dev-server"; import { type DevServerOptions, default as honoViteDevServer } from "@hono/vite-dev-server";
import { type RuntimeBkndConfig, createRuntimeApp } from "adapter";
import type { App } from "bknd"; import type { App } from "bknd";
import { type RuntimeBkndConfig, createRuntimeApp } from "bknd/adapter";
import { registerLocalMediaAdapter } from "bknd/adapter/node";
import { devServerConfig } from "./dev-server-config"; import { devServerConfig } from "./dev-server-config";
export type ViteBkndConfig<Env = any> = RuntimeBkndConfig<Env> & { export type ViteBkndConfig<Env = any> = RuntimeBkndConfig<Env> & {
@@ -28,10 +29,10 @@ ${addBkndContext ? "<!-- BKND_CONTEXT -->" : ""}
} }
async function createApp(config: ViteBkndConfig = {}, env?: any) { async function createApp(config: ViteBkndConfig = {}, env?: any) {
registerLocalMediaAdapter();
return await createRuntimeApp( return await createRuntimeApp(
{ {
...config, ...config,
registerLocalMedia: true,
adminOptions: adminOptions:
config.setAdminHtml === false config.setAdminHtml === false
? undefined ? undefined

View File

@@ -1,7 +1,6 @@
import type { APIContext } from "astro"; import type { APIContext } from "astro";
import { App } from "bknd"; import { App } from "bknd";
import { serve } from "bknd/adapter/astro"; import { registerLocalMediaAdapter, serve } from "bknd/adapter/astro";
import { registerLocalMediaAdapter } from "bknd/adapter/node";
import { boolean, em, entity, text } from "bknd/data"; import { boolean, em, entity, text } from "bknd/data";
import { secureRandomString } from "bknd/utils"; import { secureRandomString } from "bknd/utils";

View File

@@ -1,6 +1,5 @@
import { App } from "bknd"; import { App } from "bknd";
import { registerLocalMediaAdapter } from "bknd/adapter/node"; import { registerLocalMediaAdapter, serve } from "bknd/adapter/remix";
import { serve } from "bknd/adapter/remix";
import { boolean, em, entity, text } from "bknd/data"; import { boolean, em, entity, text } from "bknd/data";
import { secureRandomString } from "bknd/utils"; import { secureRandomString } from "bknd/utils";