fix: add modes export, fix event firing with modes and cloudflare

This commit is contained in:
dswbx
2025-10-28 09:18:16 +01:00
parent 2847e64b77
commit ef41b71921
10 changed files with 33 additions and 10 deletions

View File

@@ -85,7 +85,12 @@ async function buildApi() {
sourcemap, sourcemap,
watch, watch,
define, define,
entry: ["src/index.ts", "src/core/utils/index.ts", "src/plugins/index.ts"], entry: [
"src/index.ts",
"src/core/utils/index.ts",
"src/plugins/index.ts",
"src/modes/index.ts",
],
outDir: "dist", outDir: "dist",
external: [...external], external: [...external],
metafile: true, metafile: true,

View File

@@ -180,6 +180,11 @@
"import": "./dist/plugins/index.js", "import": "./dist/plugins/index.js",
"require": "./dist/plugins/index.js" "require": "./dist/plugins/index.js"
}, },
"./modes": {
"types": "./dist/types/modes/index.d.ts",
"import": "./dist/modes/index.js",
"require": "./dist/modes/index.js"
},
"./adapter/sqlite": { "./adapter/sqlite": {
"types": "./dist/types/adapter/sqlite/edge.d.ts", "types": "./dist/types/adapter/sqlite/edge.d.ts",
"import": { "import": {

View File

@@ -295,6 +295,7 @@ export class App<
return this.module.auth.createUser(p); return this.module.auth.createUser(p);
} }
// @todo: potentially add option to clone the app, so that when used in listeners, it won't trigger listeners
getApi(options?: LocalApiOptions) { getApi(options?: LocalApiOptions) {
const fetcher = this.server.request as typeof fetch; const fetcher = this.server.request as typeof fetch;
if (options && options instanceof Request) { if (options && options instanceof Request) {

View File

@@ -37,19 +37,19 @@ export async function createApp<Env extends CloudflareEnv = CloudflareEnv>(
config: CloudflareBkndConfig<Env> = {}, config: CloudflareBkndConfig<Env> = {},
ctx: Partial<CloudflareContext<Env>> = {}, ctx: Partial<CloudflareContext<Env>> = {},
) { ) {
const appConfig = await makeConfig( const appConfig = await makeConfig(config, ctx);
return await createRuntimeApp<Env>(
{ {
...config, ...appConfig,
onBuilt: async (app) => { onBuilt: async (app) => {
if (ctx.ctx) { if (ctx.ctx) {
registerAsyncsExecutionContext(app, ctx?.ctx); registerAsyncsExecutionContext(app, ctx?.ctx);
} }
await config.onBuilt?.(app); await appConfig.onBuilt?.(app);
}, },
}, },
ctx, ctx?.env,
); );
return await createRuntimeApp<Env>(appConfig, ctx?.env);
} }
// compatiblity // compatiblity

View File

@@ -158,6 +158,7 @@ export async function makeConfig<Env extends CloudflareEnv = CloudflareEnv>(
sessionHelper.set(c, session); sessionHelper.set(c, session);
await next(); await next();
}); });
appConfig.options?.manager?.onServerInit?.(server);
}, },
}, },
}; };

View File

@@ -205,7 +205,17 @@ export class EventManager<
if (listener.mode === "sync") { if (listener.mode === "sync") {
syncs.push(listener); syncs.push(listener);
} else { } else {
asyncs.push(async () => await listener.handler(event, listener.event.slug)); asyncs.push(async () => {
try {
await listener.handler(event, listener.event.slug);
} catch (e) {
if (this.options?.onError) {
this.options.onError(event, e);
} else {
$console.error("Error executing async listener", listener, e);
}
}
});
} }
// Remove if `once` is true, otherwise keep // Remove if `once` is true, otherwise keep
return !listener.once; return !listener.once;

View File

@@ -9,7 +9,6 @@ import {
pickKeys, pickKeys,
mcpTool, mcpTool,
convertNumberedObjectToArray, convertNumberedObjectToArray,
mergeObject,
} from "bknd/utils"; } from "bknd/utils";
import * as SystemPermissions from "modules/permissions"; import * as SystemPermissions from "modules/permissions";
import type { AppDataConfig } from "../data-schema"; import type { AppDataConfig } from "../data-schema";

View File

@@ -50,6 +50,7 @@ export { getFlashMessage } from "core/server/flash";
export * from "core/drivers"; export * from "core/drivers";
export { Event, InvalidEventReturn } from "core/events/Event"; export { Event, InvalidEventReturn } from "core/events/Event";
export type { export type {
EventListener,
ListenerMode, ListenerMode,
ListenerHandler, ListenerHandler,
} from "core/events/EventListener"; } from "core/events/EventListener";

View File

@@ -68,6 +68,7 @@ export function hybrid<Args>({
const mm = app.modules as DbModuleManager; const mm = app.modules as DbModuleManager;
mm.buildSyncConfig = syncSchemaOptions; mm.buildSyncConfig = syncSchemaOptions;
} }
await appConfig.beforeBuild?.(app);
}, },
config: fileConfig, config: fileConfig,
options: { options: {

View File

@@ -59,8 +59,8 @@ export type BkndModeConfig<Args = any, Additional = {}> = BkndConfig<
export async function makeModeConfig< export async function makeModeConfig<
Args = any, Args = any,
Config extends BkndModeConfig<Args> = BkndModeConfig<Args>, Config extends BkndModeConfig<Args> = BkndModeConfig<Args>,
>(_config: Config, args: Args) { >({ app, ..._config }: Config, args: Args) {
const appConfig = typeof _config.app === "function" ? await _config.app(args) : _config.app; const appConfig = typeof app === "function" ? await app(args) : app;
const config = { const config = {
..._config, ..._config,