@byo-oss/corev0.2.0library · shared

Core

The glue for apps that run two or more BYO packages.

Core is not a feature. It is a small library that gives your BYO packages one event bus, one cron route and one org-cleanup call, so you wire each once instead of once per package. It adds no tables and no endpoints, and nothing goes in plugins: []. With a single BYO package you don't need it.

npm i @byo-oss/core
lib/auth.ts
import { createEventBus } from "@byo-oss/core";

const onEvent = createEventBus([
  (event) => activity.record(event),
  async (event) => {
    if (event.type === "support.agent.replied" && event.userId) {
      await auth.api.notify({ body: { userId: event.userId, type: event.type, title: "New reply" } });
    }
  },
]);

export const auth = betterAuth({
  plugins: [support({ onEvent }), feedback({ onEvent })],
});
one onEvent · every BYO plugin

Relies on

peerDependencies · @byo-oss/core 0.2.0
better-authrequired
peer · ^1.7.16

Core reads the plugins you pass to betterAuth({ plugins }). It has no runtime dependency on better-auth and adds no tables.

organizationoptional
better-auth plugin

Call the cleanup fan-out from its organizationHooks when an organization is deleted or a member is removed.

[01] what you get

Wire it once, not once per package.

Each BYO package that sends events, runs background work or stores per-organization data would otherwise need its own wiring. Core reads the plugins you already pass to betterAuth({ plugins }) and calls the hooks they declare.

01

One event bus

createEventBus turns a list of consumers into the one onEvent you pass to every BYO plugin. A consumer that throws is reported, and the next one still runs.

02

One cron route

sweepAll runs every plugin's background work in plugin order, inside a time budget you set, and tells you when to call again.

03

One cleanup call

deleteOrganizationData and deleteMemberData reach every plugin that stores per-organization or per-member data.

04

Nothing is skipped

A plugin that fails never stops the others. Cleanup runs all plugins, then throws a ByoFanOutError naming the ones that failed.

05

Joins your transaction

Core opens no transaction. Call the fan-out inside yours and every plugin's cleanup runs on it, and rolls back with it.

06

A contract for plugin authors

A plugin declares sweep and cleanup hooks under a byo key, and core reaches them. better-auth ignores the key.

[02] quickstart

Three steps.

For an app that already runs two or more BYO packages.

  1. step 01

    Build one event bus

    Pass the same onEvent to every BYO plugin that emits events.

    lib/auth.ts
    import { createEventBus } from "@byo-oss/core";
    
    const onEvent = createEventBus([
      (event) => activity.record(event),
      async (event) => {
        if (event.type === "support.agent.replied" && event.userId) {
          await auth.api.notify({ body: { userId: event.userId, type: event.type, title: "New reply" } });
        }
      },
    ]);
    
    export const auth = betterAuth({
      plugins: [support({ onEvent }), feedback({ onEvent })],
    });
  2. step 02

    Add one cron route

    Run it every minute from any scheduler, and protect it the way your scheduler expects.

    app/api/cron/route.ts
    import { sweepAll } from "@byo-oss/core";
    
    export async function GET() {
      const result = await sweepAll(auth, { budgetMs: 20_000 });
      return Response.json(result);
    }
  3. step 03

    Wire the cleanup hooks

    From the organization plugin's hooks, so every package cleans up when an organization or member goes.

    lib/auth.ts
    import { deleteMemberData, deleteOrganizationData } from "@byo-oss/core";
    
    organization({
      organizationHooks: {
        afterDeleteOrganization: async ({ organization }) => {
          await deleteOrganizationData(auth, organization.id);
        },
        afterRemoveMember: async ({ member }) => {
          await deleteMemberData(auth, { userId: member.userId, organizationId: member.organizationId });
        },
      },
    });
Full quickstart in the docs →
[03] guarantees

Best-effort events, all-plugins cleanup.

events

In-process and best-effort. The bus never fails the request that emitted the event, and nothing is retried or stored.

sweeps

Core adds no guarantee of its own. Leases, retries and at-least-once delivery belong to each plugin's sweep.

cleanup

All plugins run, then it throws if any failed. Outside a transaction, the cleanups that succeeded stay done.

Server API →
[04] plugin authors

Declare hooks under byo.

A plugin declares its sweep and cleanup hooks under a byo key, and core's helpers call them. better-auth ignores the key at runtime.

  • End the plugin with satisfies ByoPlugin<BetterAuthPlugin>
  • Every hook is optional: declare only what the plugin has
  • Read the adapter with getCurrentAdapter, so the hook joins the caller's transaction
  • Keep exporting your own cleanup functions and sweep endpoint
src/index.ts
import type { BetterAuthPlugin } from "better-auth";
import type { ByoPlugin } from "@byo-oss/core";

export const feedback = (options: FeedbackOptions = {}) =>
  ({
    id: "feedback",
    endpoints: { /* ... */ },
    byo: {
      sweep: (auth) => processFeedbackDigests(auth),
      deleteOrganizationData: async (auth, organizationId) => {
        await deleteFeedbackOrgData(auth, organizationId);
      },
      deleteMemberData: async (auth, { userId, organizationId }) => {
        await deleteFeedbackMemberData(auth, { userId, organizationId });
      },
    },
  }) satisfies ByoPlugin<BetterAuthPlugin>;
[05] what this is not

Glue, not infrastructure.

Core does not replace a scheduler or a queue. There is also no exit to a third-party tool: remove it and each plugin keeps its own sweep endpoint and cleanup functions, which you then call yourself.

a scheduler

You still need one cron that calls sweepAll.

durable events

No outbox, no retries, no cross-process delivery.

user delete

User data goes through foreign keys and each plugin's own databaseHooks. There is no user-delete fan-out.

export, identity

No data export fan-out, and no shared identify(request) helper yet.

Read the introduction →