@brickkit/core 0.8.2 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +25 -0
- package/dist/chunks/client.d.ts +959 -3
- package/dist/chunks/client.d.ts.map +1 -1
- package/dist/chunks/client.js +2 -1
- package/dist/chunks/client.js.map +1 -1
- package/dist/index.d.ts +145 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +914 -6
- package/dist/index.js.map +1 -1
- package/dist/pg.d.ts +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -5,12 +5,14 @@ import { Type, Type as t } from "@sinclair/typebox";
|
|
|
5
5
|
import { TypeCompiler } from "@sinclair/typebox/compiler";
|
|
6
6
|
import { formatWithOptions } from "node:util";
|
|
7
7
|
import { Elysia } from "elysia";
|
|
8
|
-
import {
|
|
8
|
+
import { Table, and, and as and$1, asc, asc as asc$1, desc, eq, eq as eq$1, getTableColumns, getTableColumns as getTableColumns$1, getTableName, getTableName as getTableName$1, gt, gte, ilike, inArray, inArray as inArray$1, is, isNotNull, isNull, like, lt, lt as lt$1, lte, lte as lte$1, ne, not, notInArray, or, or as or$1, relations, sql, sql as sql$1 } from "drizzle-orm";
|
|
9
|
+
import { bigserial, index as index$1, integer as integer$1, jsonb, pgTable, text as text$1, timestamp } from "drizzle-orm/pg-core";
|
|
10
|
+
import { blob, foreignKey, index, index as index$2, integer, integer as integer$2, primaryKey, real, sqliteTable, sqliteTable as sqliteTable$1, text, text as text$2, uniqueIndex } from "drizzle-orm/sqlite-core";
|
|
11
|
+
import { existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
12
|
+
import { dirname, join, resolve } from "node:path";
|
|
13
|
+
import { pathToFileURL } from "node:url";
|
|
9
14
|
import postgres from "postgres";
|
|
10
15
|
import { drizzle } from "drizzle-orm/postgres-js";
|
|
11
|
-
import { Table, and, asc, desc, eq, getTableColumns, getTableColumns as getTableColumns$1, getTableName, getTableName as getTableName$1, gt, gte, ilike, inArray, is, isNotNull, isNull, like, lt, lte, ne, not, notInArray, or, relations, sql, sql as sql$1 } from "drizzle-orm";
|
|
12
|
-
import { mkdirSync } from "node:fs";
|
|
13
|
-
import { dirname } from "node:path";
|
|
14
16
|
//#region \0rolldown/runtime.js
|
|
15
17
|
var __require = /* #__PURE__ */ (() => createRequire(import.meta.url))();
|
|
16
18
|
//#endregion
|
|
@@ -164,6 +166,7 @@ var ServiceRegistry = class {
|
|
|
164
166
|
exportArchitecture(application) {
|
|
165
167
|
return {
|
|
166
168
|
version: "1.0.0",
|
|
169
|
+
...application?.events ? { events: application.events } : {},
|
|
167
170
|
services: (application?.services ?? this.list()).map((service) => {
|
|
168
171
|
const routes = application ? application.routes.filter((route) => route.service === service.name) : this.routes.get(service) ?? analyzeDeclaredApplication([service]).ir.routes;
|
|
169
172
|
const byAction = new Map(routes.map((route) => [route.action, route]));
|
|
@@ -2218,6 +2221,798 @@ function endpointPaths(defs) {
|
|
|
2218
2221
|
return paths;
|
|
2219
2222
|
}
|
|
2220
2223
|
//#endregion
|
|
2224
|
+
//#region src/events/outbox.ts
|
|
2225
|
+
const OUTBOX = Symbol.for("brick.outbox");
|
|
2226
|
+
function sqliteOutbox(name) {
|
|
2227
|
+
const timestampMs = (column) => integer$2(column, { mode: "timestamp_ms" });
|
|
2228
|
+
return sqliteTable$1(name, {
|
|
2229
|
+
seq: integer$2("seq").primaryKey({ autoIncrement: true }),
|
|
2230
|
+
id: text$2("id").notNull().unique(),
|
|
2231
|
+
eventId: text$2("event_id").notNull(),
|
|
2232
|
+
listener: text$2("listener").notNull(),
|
|
2233
|
+
name: text$2("name").notNull(),
|
|
2234
|
+
payload: text$2("payload", { mode: "json" }).notNull(),
|
|
2235
|
+
traceId: text$2("trace_id"),
|
|
2236
|
+
source: text$2("source"),
|
|
2237
|
+
occurredAt: timestampMs("occurred_at").notNull(),
|
|
2238
|
+
status: text$2("status").$type().notNull(),
|
|
2239
|
+
attempts: integer$2("attempts").notNull().default(0),
|
|
2240
|
+
lastError: text$2("last_error"),
|
|
2241
|
+
availableAt: timestampMs("available_at").notNull(),
|
|
2242
|
+
claimedAt: timestampMs("claimed_at"),
|
|
2243
|
+
deliveredAt: timestampMs("delivered_at")
|
|
2244
|
+
}, (table) => [index$2(`${name}_due`).on(table.status, table.availableAt), index$2(`${name}_event`).on(table.eventId)]);
|
|
2245
|
+
}
|
|
2246
|
+
function postgresOutbox(name) {
|
|
2247
|
+
const at = (column) => timestamp(column, { withTimezone: true });
|
|
2248
|
+
return pgTable(name, {
|
|
2249
|
+
seq: bigserial("seq", { mode: "number" }).primaryKey(),
|
|
2250
|
+
id: text$1("id").notNull().unique(),
|
|
2251
|
+
eventId: text$1("event_id").notNull(),
|
|
2252
|
+
listener: text$1("listener").notNull(),
|
|
2253
|
+
name: text$1("name").notNull(),
|
|
2254
|
+
payload: jsonb("payload").notNull(),
|
|
2255
|
+
traceId: text$1("trace_id"),
|
|
2256
|
+
source: text$1("source"),
|
|
2257
|
+
occurredAt: at("occurred_at").notNull(),
|
|
2258
|
+
status: text$1("status").$type().notNull(),
|
|
2259
|
+
attempts: integer$1("attempts").notNull().default(0),
|
|
2260
|
+
lastError: text$1("last_error"),
|
|
2261
|
+
availableAt: at("available_at").notNull(),
|
|
2262
|
+
claimedAt: at("claimed_at"),
|
|
2263
|
+
deliveredAt: at("delivered_at")
|
|
2264
|
+
}, (table) => [index$1(`${name}_due`).on(table.status, table.availableAt), index$1(`${name}_event`).on(table.eventId)]);
|
|
2265
|
+
}
|
|
2266
|
+
function outboxTable(engine, options = {}) {
|
|
2267
|
+
const name = options.name ?? "brick_outbox";
|
|
2268
|
+
const table = engine === "sqlite" ? sqliteOutbox(name) : postgresOutbox(name);
|
|
2269
|
+
Object.defineProperty(table, OUTBOX, { value: { engine } });
|
|
2270
|
+
return table;
|
|
2271
|
+
}
|
|
2272
|
+
/**
|
|
2273
|
+
* Whether a value is an outbox table made by `outboxTable`.
|
|
2274
|
+
*
|
|
2275
|
+
* @param value - Anything.
|
|
2276
|
+
* @returns `true` for an outbox table, narrowing its type.
|
|
2277
|
+
*/
|
|
2278
|
+
function isOutboxTable(value) {
|
|
2279
|
+
return typeof value === "object" && value !== null && OUTBOX in value;
|
|
2280
|
+
}
|
|
2281
|
+
function outboxEngine(table) {
|
|
2282
|
+
return table[OUTBOX].engine;
|
|
2283
|
+
}
|
|
2284
|
+
//#endregion
|
|
2285
|
+
//#region src/events/transaction.ts
|
|
2286
|
+
const sqliteQueues = /* @__PURE__ */ new WeakMap();
|
|
2287
|
+
function isSqliteDatabase(db) {
|
|
2288
|
+
return typeof db?.$client?.prepare === "function" && typeof db?.$client?.run === "function";
|
|
2289
|
+
}
|
|
2290
|
+
function rollbackQuietly(db) {
|
|
2291
|
+
try {
|
|
2292
|
+
db.$client.run("ROLLBACK");
|
|
2293
|
+
} catch {
|
|
2294
|
+
return;
|
|
2295
|
+
}
|
|
2296
|
+
}
|
|
2297
|
+
/**
|
|
2298
|
+
* Run `fn` in a database transaction: everything it writes through the `tx` it is
|
|
2299
|
+
* given is committed together when it returns, or undone if it throws.
|
|
2300
|
+
*
|
|
2301
|
+
* Postgres uses a real transaction on one connection. SQLite has one connection
|
|
2302
|
+
* and a synchronous driver, which Drizzle's own `transaction` cannot use with an
|
|
2303
|
+
* async callback (it would commit before the callback finished). So transactions
|
|
2304
|
+
* on one SQLite database take turns, each between `BEGIN IMMEDIATE` and `COMMIT`.
|
|
2305
|
+
* A write made elsewhere in the process during that time, outside this helper,
|
|
2306
|
+
* would join the transaction; use Postgres where that matters.
|
|
2307
|
+
*
|
|
2308
|
+
* @param db - A Drizzle database, from `service.getDb()`.
|
|
2309
|
+
* @param fn - Receives the transaction handle to run queries on.
|
|
2310
|
+
* @returns What `fn` returned.
|
|
2311
|
+
* @throws What `fn` threw, after rolling back.
|
|
2312
|
+
*/
|
|
2313
|
+
async function runInTransaction(db, fn) {
|
|
2314
|
+
if (!isSqliteDatabase(db)) return db.transaction((tx) => fn(tx));
|
|
2315
|
+
const previous = sqliteQueues.get(db) ?? Promise.resolve();
|
|
2316
|
+
let release;
|
|
2317
|
+
const turn = new Promise((resolve) => {
|
|
2318
|
+
release = resolve;
|
|
2319
|
+
});
|
|
2320
|
+
sqliteQueues.set(db, previous.then(() => turn));
|
|
2321
|
+
await previous;
|
|
2322
|
+
try {
|
|
2323
|
+
db.$client.run("BEGIN IMMEDIATE");
|
|
2324
|
+
try {
|
|
2325
|
+
const result = await fn(db);
|
|
2326
|
+
db.$client.run("COMMIT");
|
|
2327
|
+
return result;
|
|
2328
|
+
} catch (error) {
|
|
2329
|
+
rollbackQuietly(db);
|
|
2330
|
+
throw error;
|
|
2331
|
+
}
|
|
2332
|
+
} finally {
|
|
2333
|
+
release();
|
|
2334
|
+
}
|
|
2335
|
+
}
|
|
2336
|
+
//#endregion
|
|
2337
|
+
//#region src/events/dispatcher.ts
|
|
2338
|
+
function resolveDispatchOptions(options = {}) {
|
|
2339
|
+
return {
|
|
2340
|
+
timer: options.timer ?? true,
|
|
2341
|
+
intervalMs: options.intervalMs ?? 1e3,
|
|
2342
|
+
batchSize: options.batchSize ?? 50,
|
|
2343
|
+
maxAttempts: options.maxAttempts ?? 8,
|
|
2344
|
+
claimTimeoutMs: options.claimTimeoutMs ?? 6e4,
|
|
2345
|
+
maxBackoffMs: options.maxBackoffMs ?? 3e5,
|
|
2346
|
+
retainDeliveredMs: options.retainDeliveredMs ?? 864e5
|
|
2347
|
+
};
|
|
2348
|
+
}
|
|
2349
|
+
const PURGE_INTERVAL_MS = 6e4;
|
|
2350
|
+
function backoffMs(attempt, maxBackoffMs) {
|
|
2351
|
+
return Math.min(1e3 * 2 ** Math.max(0, attempt - 1), maxBackoffMs);
|
|
2352
|
+
}
|
|
2353
|
+
/**
|
|
2354
|
+
* Delivers the events recorded in one outbox table to the app's listeners.
|
|
2355
|
+
*
|
|
2356
|
+
* It claims a batch of due rows, runs each listener, and marks the row delivered.
|
|
2357
|
+
* A listener that throws is retried later with a growing delay, and parked as
|
|
2358
|
+
* dead after its attempts run out. Several dispatchers can share a table: on
|
|
2359
|
+
* Postgres they claim with `FOR UPDATE SKIP LOCKED`, and a claim that is not
|
|
2360
|
+
* finished within `claimTimeoutMs` (the process died) is taken by another.
|
|
2361
|
+
* Delivery is at least once, so a listener should tolerate running twice for the
|
|
2362
|
+
* same `deliveryId`.
|
|
2363
|
+
*/
|
|
2364
|
+
var Dispatcher = class {
|
|
2365
|
+
db;
|
|
2366
|
+
table;
|
|
2367
|
+
listeners;
|
|
2368
|
+
onError;
|
|
2369
|
+
engine;
|
|
2370
|
+
options;
|
|
2371
|
+
inFlight = /* @__PURE__ */ new Set();
|
|
2372
|
+
timer;
|
|
2373
|
+
tickQueue;
|
|
2374
|
+
lastPurge = 0;
|
|
2375
|
+
constructor(db, table, listeners, options = {}, onError = (error) => console.error("[Brick-TS events]", error)) {
|
|
2376
|
+
this.db = db;
|
|
2377
|
+
this.table = table;
|
|
2378
|
+
this.listeners = listeners;
|
|
2379
|
+
this.onError = onError;
|
|
2380
|
+
this.engine = outboxEngine(table);
|
|
2381
|
+
this.options = resolveDispatchOptions(options);
|
|
2382
|
+
}
|
|
2383
|
+
/** Whether the dispatcher is looking for work on a timer. */
|
|
2384
|
+
get running() {
|
|
2385
|
+
return this.timer !== void 0;
|
|
2386
|
+
}
|
|
2387
|
+
/** Start looking for due deliveries on a timer. Does nothing if it already is. */
|
|
2388
|
+
start() {
|
|
2389
|
+
if (this.timer || !this.options.timer) return;
|
|
2390
|
+
this.timer = setInterval(() => void this.tick(), this.options.intervalMs);
|
|
2391
|
+
this.timer.unref?.();
|
|
2392
|
+
this.tick();
|
|
2393
|
+
}
|
|
2394
|
+
/**
|
|
2395
|
+
* Stop the timer and wait for the deliveries that are running to finish.
|
|
2396
|
+
*/
|
|
2397
|
+
async stop() {
|
|
2398
|
+
if (this.timer) clearInterval(this.timer);
|
|
2399
|
+
this.timer = void 0;
|
|
2400
|
+
await this.tickQueue;
|
|
2401
|
+
await Promise.allSettled(this.inFlight);
|
|
2402
|
+
}
|
|
2403
|
+
/** Look for work soon, without waiting for the timer. Safe to call often. */
|
|
2404
|
+
notify() {
|
|
2405
|
+
if (!this.timer) return;
|
|
2406
|
+
setTimeout(() => void this.tick(), 0).unref?.();
|
|
2407
|
+
}
|
|
2408
|
+
/**
|
|
2409
|
+
* Claim what is due and run it, once. Used by the timer, and by tests and
|
|
2410
|
+
* workers that drive delivery themselves.
|
|
2411
|
+
*
|
|
2412
|
+
* @returns How many deliveries it started.
|
|
2413
|
+
*/
|
|
2414
|
+
tick() {
|
|
2415
|
+
this.tickQueue = (this.tickQueue ?? Promise.resolve(0)).then(() => this.claimAndRun(), () => this.claimAndRun());
|
|
2416
|
+
return this.tickQueue;
|
|
2417
|
+
}
|
|
2418
|
+
/**
|
|
2419
|
+
* Deliver everything that is due now and wait until those deliveries are done.
|
|
2420
|
+
* Deliveries that fail and are rescheduled are not waited for again.
|
|
2421
|
+
*
|
|
2422
|
+
* @returns How many deliveries it ran.
|
|
2423
|
+
*/
|
|
2424
|
+
async drain() {
|
|
2425
|
+
let total = 0;
|
|
2426
|
+
for (;;) {
|
|
2427
|
+
const started = await this.tick();
|
|
2428
|
+
await Promise.allSettled(this.inFlight);
|
|
2429
|
+
if (started === 0) return total;
|
|
2430
|
+
total += started;
|
|
2431
|
+
}
|
|
2432
|
+
}
|
|
2433
|
+
/**
|
|
2434
|
+
* How many rows are in each state.
|
|
2435
|
+
*/
|
|
2436
|
+
async counts() {
|
|
2437
|
+
const t = this.table;
|
|
2438
|
+
const rows = await this.db.select({
|
|
2439
|
+
status: t.status,
|
|
2440
|
+
count: sql$1`count(*)`
|
|
2441
|
+
}).from(t).groupBy(t.status);
|
|
2442
|
+
const counts = {
|
|
2443
|
+
pending: 0,
|
|
2444
|
+
claimed: 0,
|
|
2445
|
+
delivered: 0,
|
|
2446
|
+
dead: 0
|
|
2447
|
+
};
|
|
2448
|
+
for (const row of rows) counts[row.status] = Number(row.count);
|
|
2449
|
+
return counts;
|
|
2450
|
+
}
|
|
2451
|
+
/**
|
|
2452
|
+
* Put parked deliveries back in line, with their attempts reset.
|
|
2453
|
+
*
|
|
2454
|
+
* @param deliveryId - One delivery to retry. Omit it to retry every dead one.
|
|
2455
|
+
* @returns How many deliveries were put back.
|
|
2456
|
+
*/
|
|
2457
|
+
async retryDead(deliveryId) {
|
|
2458
|
+
const t = this.table;
|
|
2459
|
+
const rows = await this.db.update(t).set({
|
|
2460
|
+
status: "pending",
|
|
2461
|
+
attempts: 0,
|
|
2462
|
+
availableAt: /* @__PURE__ */ new Date()
|
|
2463
|
+
}).where(deliveryId ? and$1(eq$1(t.status, "dead"), eq$1(t.id, deliveryId)) : eq$1(t.status, "dead")).returning({ id: t.id });
|
|
2464
|
+
if (rows.length) this.notify();
|
|
2465
|
+
return rows.length;
|
|
2466
|
+
}
|
|
2467
|
+
async claimAndRun() {
|
|
2468
|
+
try {
|
|
2469
|
+
const room = this.options.batchSize - this.inFlight.size;
|
|
2470
|
+
if (room <= 0) return 0;
|
|
2471
|
+
const rows = await this.claim(room);
|
|
2472
|
+
for (const row of rows) {
|
|
2473
|
+
const run = this.deliver(row).finally(() => {
|
|
2474
|
+
this.inFlight.delete(run);
|
|
2475
|
+
});
|
|
2476
|
+
this.inFlight.add(run);
|
|
2477
|
+
}
|
|
2478
|
+
await this.purgeDelivered();
|
|
2479
|
+
return rows.length;
|
|
2480
|
+
} catch (error) {
|
|
2481
|
+
this.onError(error);
|
|
2482
|
+
return 0;
|
|
2483
|
+
}
|
|
2484
|
+
}
|
|
2485
|
+
async claim(limit) {
|
|
2486
|
+
const t = this.table;
|
|
2487
|
+
const now = /* @__PURE__ */ new Date();
|
|
2488
|
+
const stale = new Date(now.getTime() - this.options.claimTimeoutMs);
|
|
2489
|
+
const due = or$1(and$1(eq$1(t.status, "pending"), lte$1(t.availableAt, now)), and$1(eq$1(t.status, "claimed"), lt$1(t.claimedAt, stale)));
|
|
2490
|
+
const claimIn = async (tx) => {
|
|
2491
|
+
let select = tx.select().from(t).where(due).orderBy(asc$1(t.seq)).limit(limit);
|
|
2492
|
+
if (this.engine === "postgres") select = select.for("update", { skipLocked: true });
|
|
2493
|
+
const picked = await select;
|
|
2494
|
+
if (!picked.length) return [];
|
|
2495
|
+
await tx.update(t).set({
|
|
2496
|
+
status: "claimed",
|
|
2497
|
+
claimedAt: now,
|
|
2498
|
+
attempts: sql$1`${t.attempts} + 1`
|
|
2499
|
+
}).where(inArray$1(t.seq, picked.map((row) => row.seq)));
|
|
2500
|
+
return picked.map((row) => ({
|
|
2501
|
+
...row,
|
|
2502
|
+
attempts: row.attempts + 1
|
|
2503
|
+
}));
|
|
2504
|
+
};
|
|
2505
|
+
return this.engine === "postgres" ? this.db.transaction(claimIn) : runInTransaction(this.db, claimIn);
|
|
2506
|
+
}
|
|
2507
|
+
async deliver(row) {
|
|
2508
|
+
const listener = this.listeners(row.listener);
|
|
2509
|
+
if (!listener) {
|
|
2510
|
+
await this.park(row, `No listener named '${row.listener}' is registered`);
|
|
2511
|
+
return;
|
|
2512
|
+
}
|
|
2513
|
+
const delivery = {
|
|
2514
|
+
id: row.eventId,
|
|
2515
|
+
name: row.name,
|
|
2516
|
+
payload: row.payload,
|
|
2517
|
+
occurredAt: row.occurredAt.toISOString(),
|
|
2518
|
+
...row.traceId ? { traceId: row.traceId } : {},
|
|
2519
|
+
...row.source ? { source: row.source } : {},
|
|
2520
|
+
listener: row.listener,
|
|
2521
|
+
deliveryId: row.id,
|
|
2522
|
+
attempt: row.attempts
|
|
2523
|
+
};
|
|
2524
|
+
try {
|
|
2525
|
+
await listener.run(delivery);
|
|
2526
|
+
} catch (error) {
|
|
2527
|
+
await this.fail(row, error, listener.maxAttempts ?? this.options.maxAttempts);
|
|
2528
|
+
return;
|
|
2529
|
+
}
|
|
2530
|
+
await this.markDelivered(row);
|
|
2531
|
+
}
|
|
2532
|
+
async markDelivered(row) {
|
|
2533
|
+
const t = this.table;
|
|
2534
|
+
try {
|
|
2535
|
+
if (this.options.retainDeliveredMs === 0) await this.db.delete(t).where(eq$1(t.seq, row.seq));
|
|
2536
|
+
else await this.db.update(t).set({
|
|
2537
|
+
status: "delivered",
|
|
2538
|
+
deliveredAt: /* @__PURE__ */ new Date(),
|
|
2539
|
+
lastError: null
|
|
2540
|
+
}).where(eq$1(t.seq, row.seq));
|
|
2541
|
+
} catch (error) {
|
|
2542
|
+
this.onError(error);
|
|
2543
|
+
}
|
|
2544
|
+
}
|
|
2545
|
+
async fail(row, error, maxAttempts) {
|
|
2546
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2547
|
+
if (row.attempts >= maxAttempts) {
|
|
2548
|
+
await this.park(row, message);
|
|
2549
|
+
return;
|
|
2550
|
+
}
|
|
2551
|
+
const t = this.table;
|
|
2552
|
+
try {
|
|
2553
|
+
await this.db.update(t).set({
|
|
2554
|
+
status: "pending",
|
|
2555
|
+
lastError: message,
|
|
2556
|
+
claimedAt: null,
|
|
2557
|
+
availableAt: new Date(Date.now() + backoffMs(row.attempts, this.options.maxBackoffMs))
|
|
2558
|
+
}).where(eq$1(t.seq, row.seq));
|
|
2559
|
+
} catch (writeError) {
|
|
2560
|
+
this.onError(writeError);
|
|
2561
|
+
}
|
|
2562
|
+
}
|
|
2563
|
+
async park(row, message) {
|
|
2564
|
+
const t = this.table;
|
|
2565
|
+
try {
|
|
2566
|
+
await this.db.update(t).set({
|
|
2567
|
+
status: "dead",
|
|
2568
|
+
lastError: message,
|
|
2569
|
+
claimedAt: null
|
|
2570
|
+
}).where(eq$1(t.seq, row.seq));
|
|
2571
|
+
} catch (error) {
|
|
2572
|
+
this.onError(error);
|
|
2573
|
+
}
|
|
2574
|
+
}
|
|
2575
|
+
async purgeDelivered() {
|
|
2576
|
+
const keep = this.options.retainDeliveredMs;
|
|
2577
|
+
const now = Date.now();
|
|
2578
|
+
if (keep === 0 || now - this.lastPurge < PURGE_INTERVAL_MS) return;
|
|
2579
|
+
this.lastPurge = now;
|
|
2580
|
+
const t = this.table;
|
|
2581
|
+
await this.db.delete(t).where(and$1(eq$1(t.status, "delivered"), lt$1(t.deliveredAt, new Date(now - keep))));
|
|
2582
|
+
}
|
|
2583
|
+
};
|
|
2584
|
+
//#endregion
|
|
2585
|
+
//#region src/events/id.ts
|
|
2586
|
+
const ALPHABET = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
|
|
2587
|
+
function newEventId(now = Date.now()) {
|
|
2588
|
+
let time = "";
|
|
2589
|
+
let remaining = now;
|
|
2590
|
+
for (let index = 0; index < 10; index++) {
|
|
2591
|
+
time = ALPHABET[remaining % 32] + time;
|
|
2592
|
+
remaining = Math.floor(remaining / 32);
|
|
2593
|
+
}
|
|
2594
|
+
let random = "";
|
|
2595
|
+
for (const byte of crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(16))) random += ALPHABET[byte % 32];
|
|
2596
|
+
return `evt_${time}${random}`;
|
|
2597
|
+
}
|
|
2598
|
+
//#endregion
|
|
2599
|
+
//#region src/events/workflows.ts
|
|
2600
|
+
/** `getQueueTopicPrefix("workflow")` from `@workflow/world`: the queue the runtime reads. */
|
|
2601
|
+
const WORKFLOW_QUEUE_PREFIX = "__wkf_workflow_";
|
|
2602
|
+
const BUNDLE_DIR = ".well-known/workflow/v1";
|
|
2603
|
+
const STOPPED = "The events were stopped, so workflows can no longer run";
|
|
2604
|
+
/** Where the SDK builds the URL of a webhook: the route `handleWebhook` serves. */
|
|
2605
|
+
const WEBHOOK_ROUTE = "/.well-known/workflow/v1/webhook/*";
|
|
2606
|
+
/** Where the transform and the bundle register steps by id. The SDK's own generated code uses it. */
|
|
2607
|
+
const STEP_REGISTRY = Symbol.for("@workflow/core//registeredSteps");
|
|
2608
|
+
/**
|
|
2609
|
+
* Where the workflow bundle is read from, and the project it is built from.
|
|
2610
|
+
*
|
|
2611
|
+
* @param options - The `events.workflows` option.
|
|
2612
|
+
* @returns Absolute `root` and `dir`. The bundle cannot live elsewhere: the
|
|
2613
|
+
* builder finds the SDK from where it writes, and skips only this directory when it
|
|
2614
|
+
* looks for workflows.
|
|
2615
|
+
*/
|
|
2616
|
+
function resolveWorkflowPaths(options = {}) {
|
|
2617
|
+
const root = resolve(options.root ?? process.cwd());
|
|
2618
|
+
return {
|
|
2619
|
+
root,
|
|
2620
|
+
dir: join(root, BUNDLE_DIR)
|
|
2621
|
+
};
|
|
2622
|
+
}
|
|
2623
|
+
/**
|
|
2624
|
+
* Runs workflow listeners: wires the world into the Workflow SDK, serves the
|
|
2625
|
+
* built bundle from this process, and starts one run for each delivery.
|
|
2626
|
+
*
|
|
2627
|
+
* Nothing is loaded until it is needed, so an app without workflow listeners
|
|
2628
|
+
* never imports the SDK.
|
|
2629
|
+
*/
|
|
2630
|
+
var WorkflowRuntime = class {
|
|
2631
|
+
world;
|
|
2632
|
+
listeners;
|
|
2633
|
+
root;
|
|
2634
|
+
dir;
|
|
2635
|
+
ready;
|
|
2636
|
+
connected;
|
|
2637
|
+
closed = false;
|
|
2638
|
+
constructor(world, options, listeners) {
|
|
2639
|
+
this.world = world;
|
|
2640
|
+
this.listeners = listeners;
|
|
2641
|
+
({root: this.root, dir: this.dir} = resolveWorkflowPaths(options));
|
|
2642
|
+
}
|
|
2643
|
+
/** Begin loading the runtime now rather than at the first delivery. */
|
|
2644
|
+
warmUp() {
|
|
2645
|
+
this.ensure().catch((error) => console.error(`[Brick-TS] The workflow runtime did not start: ${error instanceof Error ? error.message : String(error)}`));
|
|
2646
|
+
}
|
|
2647
|
+
/**
|
|
2648
|
+
* Start the run for a delivery.
|
|
2649
|
+
*
|
|
2650
|
+
* The run is named after the delivery id, so a delivery that is tried again
|
|
2651
|
+
* finds the run it already started instead of starting another.
|
|
2652
|
+
*
|
|
2653
|
+
* @param listener - The workflow listener the delivery is for.
|
|
2654
|
+
* @param delivery - The delivery.
|
|
2655
|
+
* @returns The run id.
|
|
2656
|
+
*/
|
|
2657
|
+
async startRun(listener, delivery) {
|
|
2658
|
+
const sdk = await this.ensure();
|
|
2659
|
+
const { attempt: _attempt, ...event } = delivery;
|
|
2660
|
+
return (await sdk.start(listener.workflow, [event], {
|
|
2661
|
+
world: this.world,
|
|
2662
|
+
runKey: delivery.deliveryId,
|
|
2663
|
+
runKeyTime: Date.parse(delivery.occurredAt)
|
|
2664
|
+
})).runId;
|
|
2665
|
+
}
|
|
2666
|
+
/**
|
|
2667
|
+
* Answer a request to a webhook a workflow created with `createWebhook`, by
|
|
2668
|
+
* resuming the workflow that waits on it. Needs the world but not the queue
|
|
2669
|
+
* consumer, so a process that only records events (`dispatch: false`) serves
|
|
2670
|
+
* webhooks too.
|
|
2671
|
+
*
|
|
2672
|
+
* @param request - The request to `/.well-known/workflow/v1/webhook/<token>`.
|
|
2673
|
+
* @returns The response the bundle's webhook handler gives.
|
|
2674
|
+
*/
|
|
2675
|
+
async handleWebhook(request) {
|
|
2676
|
+
await this.connect();
|
|
2677
|
+
const file = join(this.dir, "webhook.mjs");
|
|
2678
|
+
if (!existsSync(file)) throw new Error(`No webhook bundle at ${file}. brick dev and brick start build it; in a test or script, call buildWorkflows() from @brickkit/cli/workflows first.`);
|
|
2679
|
+
const handler = (await import(pathToFileURL(file).href))[request.method];
|
|
2680
|
+
if (typeof handler !== "function") return new Response(null, { status: 405 });
|
|
2681
|
+
return handler(request);
|
|
2682
|
+
}
|
|
2683
|
+
/** Stop the world. The runtime cannot be started again afterwards. */
|
|
2684
|
+
async close() {
|
|
2685
|
+
this.closed = true;
|
|
2686
|
+
await this.ready?.catch(() => {});
|
|
2687
|
+
await this.connected?.catch(() => {});
|
|
2688
|
+
await this.world.close?.();
|
|
2689
|
+
}
|
|
2690
|
+
ensure() {
|
|
2691
|
+
if (this.closed) return Promise.reject(/* @__PURE__ */ new Error(STOPPED));
|
|
2692
|
+
this.ready ??= this.initialize().catch((error) => {
|
|
2693
|
+
this.ready = void 0;
|
|
2694
|
+
throw error;
|
|
2695
|
+
});
|
|
2696
|
+
return this.ready;
|
|
2697
|
+
}
|
|
2698
|
+
/** Load the SDK and give it the world: all a webhook needs. */
|
|
2699
|
+
connect() {
|
|
2700
|
+
if (this.closed) return Promise.reject(/* @__PURE__ */ new Error(STOPPED));
|
|
2701
|
+
this.connected ??= this.loadSdk().then((sdk) => {
|
|
2702
|
+
sdk.setWorld(this.world);
|
|
2703
|
+
return sdk;
|
|
2704
|
+
}).catch((error) => {
|
|
2705
|
+
this.connected = void 0;
|
|
2706
|
+
throw error;
|
|
2707
|
+
});
|
|
2708
|
+
return this.connected;
|
|
2709
|
+
}
|
|
2710
|
+
async initialize() {
|
|
2711
|
+
if (typeof this.world.createRunId !== "function") throw new Error("This world cannot name a run after its key, so an event delivered twice would start two runs. Use the world from @brickkit/redis-world or @brickkit/bullmq-world.");
|
|
2712
|
+
const sdk = await this.connect();
|
|
2713
|
+
const flow = join(this.dir, "flow.mjs");
|
|
2714
|
+
if (!existsSync(flow)) throw new Error(`No workflow bundle at ${flow}. brick dev and brick start build it; in a test or script, call buildWorkflows() from @brickkit/cli/workflows first.`);
|
|
2715
|
+
const manifest = this.readManifest();
|
|
2716
|
+
this.checkListeners(manifest);
|
|
2717
|
+
const bundle = await this.loadBundle(flow);
|
|
2718
|
+
if (typeof bundle.POST !== "function") throw new Error(`${flow} does not export a POST handler`);
|
|
2719
|
+
this.world.registerHandler?.(WORKFLOW_QUEUE_PREFIX, bundle.POST);
|
|
2720
|
+
await this.world.start?.();
|
|
2721
|
+
return sdk;
|
|
2722
|
+
}
|
|
2723
|
+
async loadSdk() {
|
|
2724
|
+
const from = createRequire(join(this.root, "package.json"));
|
|
2725
|
+
const load = async (specifier) => {
|
|
2726
|
+
let path;
|
|
2727
|
+
try {
|
|
2728
|
+
path = from.resolve(specifier);
|
|
2729
|
+
} catch {
|
|
2730
|
+
throw new Error(`The Workflow SDK is not installed in ${this.root} (could not resolve '${specifier}'). Add it with: bun add workflow`);
|
|
2731
|
+
}
|
|
2732
|
+
return import(pathToFileURL(path).href);
|
|
2733
|
+
};
|
|
2734
|
+
const [api, runtime] = await Promise.all([load("workflow/api"), load("workflow/runtime")]);
|
|
2735
|
+
return {
|
|
2736
|
+
start: api.start,
|
|
2737
|
+
setWorld: runtime.setWorld
|
|
2738
|
+
};
|
|
2739
|
+
}
|
|
2740
|
+
/**
|
|
2741
|
+
* The bundle holds its own copy of every step and registers them all when it
|
|
2742
|
+
* loads, replacing the ones the app registered. A copy has its own module state:
|
|
2743
|
+
* a step would find no database connection, no config, none of what the app set
|
|
2744
|
+
* up. So the app's steps are put back, and the bundle's copies serve only the
|
|
2745
|
+
* steps the app never loaded.
|
|
2746
|
+
*/
|
|
2747
|
+
async loadBundle(flow) {
|
|
2748
|
+
const registry = () => globalThis[STEP_REGISTRY];
|
|
2749
|
+
const app = new Map(registry());
|
|
2750
|
+
const bundle = await import(pathToFileURL(flow).href);
|
|
2751
|
+
const registered = registry();
|
|
2752
|
+
if (registered) for (const [id, step] of app) registered.set(id, step);
|
|
2753
|
+
return bundle;
|
|
2754
|
+
}
|
|
2755
|
+
readManifest() {
|
|
2756
|
+
const path = join(this.dir, "manifest.json");
|
|
2757
|
+
if (!existsSync(path)) return void 0;
|
|
2758
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
2759
|
+
}
|
|
2760
|
+
/**
|
|
2761
|
+
* A listener's workflow must carry the id the transform gives it, and the bundle
|
|
2762
|
+
* must know that id: a function that was never transformed, or a bundle built
|
|
2763
|
+
* before the workflow existed, would fail on its first delivery with a less
|
|
2764
|
+
* helpful message.
|
|
2765
|
+
*/
|
|
2766
|
+
checkListeners(manifest) {
|
|
2767
|
+
const known = /* @__PURE__ */ new Set();
|
|
2768
|
+
for (const file of Object.values(manifest?.workflows ?? {})) for (const entry of Object.values(file)) if (entry.workflowId) known.add(entry.workflowId);
|
|
2769
|
+
for (const listener of this.listeners) {
|
|
2770
|
+
const id = listener.workflow?.workflowId;
|
|
2771
|
+
if (typeof id !== "string") throw new Error(`The workflow of listener '${listener.name}' was not transformed. Run the app with brick dev or brick start, or call registerWorkflowTransform() from @brickkit/cli/workflows before importing it.`);
|
|
2772
|
+
if (manifest && !known.has(id)) throw new Error(`The workflow bundle does not contain '${id}' (listener '${listener.name}'). Rebuild it with brick dev or brick start.`);
|
|
2773
|
+
}
|
|
2774
|
+
}
|
|
2775
|
+
};
|
|
2776
|
+
//#endregion
|
|
2777
|
+
//#region src/events/hub.ts
|
|
2778
|
+
function matchesEvent(pattern, name) {
|
|
2779
|
+
if (pattern === "*") return true;
|
|
2780
|
+
if (pattern.endsWith("*")) return name.startsWith(pattern.slice(0, -1));
|
|
2781
|
+
return pattern === name;
|
|
2782
|
+
}
|
|
2783
|
+
/** The recorder of each service database, so `emitEvent(ctx, ...)` can find it from `ctx.db`. */
|
|
2784
|
+
const recorders = /* @__PURE__ */ new WeakMap();
|
|
2785
|
+
const startedHubs = globalThis[Symbol.for("brickkit.events.started")] ??= /* @__PURE__ */ new Set();
|
|
2786
|
+
/**
|
|
2787
|
+
* Stop every events hub started in this process, by any copy of `@brickkit/core`,
|
|
2788
|
+
* and wait for their deliveries. A hub that fails to stop does not cut the wait
|
|
2789
|
+
* for the others: the first failure is thrown once every hub is done.
|
|
2790
|
+
*/
|
|
2791
|
+
async function stopAllEvents() {
|
|
2792
|
+
const failed = (await Promise.allSettled([...startedHubs].map((hub) => hub.stop()))).find((result) => result.status === "rejected");
|
|
2793
|
+
if (failed) throw failed.reason;
|
|
2794
|
+
}
|
|
2795
|
+
/**
|
|
2796
|
+
* The events of one app: who listens, where events are recorded, and the
|
|
2797
|
+
* dispatchers that deliver them. `brick({ events })` makes one and puts it on
|
|
2798
|
+
* `app.events`.
|
|
2799
|
+
*/
|
|
2800
|
+
var EventsHub = class {
|
|
2801
|
+
byName = /* @__PURE__ */ new Map();
|
|
2802
|
+
dispatchers = /* @__PURE__ */ new Map();
|
|
2803
|
+
deliverables = /* @__PURE__ */ new Map();
|
|
2804
|
+
dispatchOptions;
|
|
2805
|
+
runtime;
|
|
2806
|
+
recorderNames = [];
|
|
2807
|
+
started = false;
|
|
2808
|
+
stopping;
|
|
2809
|
+
constructor(options) {
|
|
2810
|
+
for (const listener of options.listeners ?? []) {
|
|
2811
|
+
if (typeof listener.name !== "string" || !listener.name) throw new Error("An event listener needs a name");
|
|
2812
|
+
if (listener.name.includes(":")) throw new Error(`Event listener name '${listener.name}' must not contain ':'`);
|
|
2813
|
+
if (!listener.on?.length) throw new Error(`Event listener '${listener.name}' listens for nothing`);
|
|
2814
|
+
if (this.byName.has(listener.name)) throw new Error(`Duplicate event listener '${listener.name}'`);
|
|
2815
|
+
if (!listener.run === !listener.workflow) throw new Error(`Event listener '${listener.name}' needs exactly one of run and workflow`);
|
|
2816
|
+
this.byName.set(listener.name, listener);
|
|
2817
|
+
}
|
|
2818
|
+
const workflowListeners = [...this.byName.values()].filter((listener) => listener.workflow);
|
|
2819
|
+
if (workflowListeners.length) {
|
|
2820
|
+
if (!options.world) throw new Error(`Event listener '${workflowListeners[0].name}' runs a workflow, so events needs a world: brick({ events: { world: createWorld(...) } }) with createWorld from @brickkit/redis-world.`);
|
|
2821
|
+
this.runtime = new WorkflowRuntime(options.world, options.workflows, workflowListeners);
|
|
2822
|
+
}
|
|
2823
|
+
this.dispatchOptions = options.dispatch === false ? false : options.dispatch === true || options.dispatch === void 0 ? {} : options.dispatch;
|
|
2824
|
+
}
|
|
2825
|
+
/**
|
|
2826
|
+
* Give each service whose database holds an outbox table a recorder, and a
|
|
2827
|
+
* dispatcher for that table. Call it before the first request: a service has no
|
|
2828
|
+
* recorder until then.
|
|
2829
|
+
*
|
|
2830
|
+
* @param services - The app's services.
|
|
2831
|
+
* @returns The hub, for chaining.
|
|
2832
|
+
*/
|
|
2833
|
+
attach(services) {
|
|
2834
|
+
let attached = 0;
|
|
2835
|
+
for (const service of services) {
|
|
2836
|
+
const table = service.getOutbox?.();
|
|
2837
|
+
if (!table) continue;
|
|
2838
|
+
attached++;
|
|
2839
|
+
this.recorderNames.push(service.name);
|
|
2840
|
+
const bound = this.bind(table);
|
|
2841
|
+
service.setEvents?.(bound);
|
|
2842
|
+
const db = service.getDb();
|
|
2843
|
+
if (db && typeof db === "object") recorders.set(db, bound);
|
|
2844
|
+
}
|
|
2845
|
+
if (attached === 0 && this.byName.size > 0) console.warn("[Brick-TS] events are configured, but no service database has an outbox table. Add outboxTable() to a database's tables so its writes can record events.");
|
|
2846
|
+
for (const service of services) {
|
|
2847
|
+
const table = service.getOutbox?.();
|
|
2848
|
+
if (table && !this.dispatchers.has(table)) this.dispatchers.set(table, new Dispatcher(service.getDb(), table, (name) => this.deliverable(name), this.dispatchOptions || {}));
|
|
2849
|
+
}
|
|
2850
|
+
return this;
|
|
2851
|
+
}
|
|
2852
|
+
/** The listeners that want an event. */
|
|
2853
|
+
matching(name) {
|
|
2854
|
+
const found = [];
|
|
2855
|
+
for (const listener of this.byName.values()) if (listener.on.some((pattern) => matchesEvent(pattern, name))) found.push(listener);
|
|
2856
|
+
return found;
|
|
2857
|
+
}
|
|
2858
|
+
/** The listeners and settings, as plain data. */
|
|
2859
|
+
describe() {
|
|
2860
|
+
return {
|
|
2861
|
+
listeners: [...this.byName.values()].map((listener) => ({
|
|
2862
|
+
name: listener.name,
|
|
2863
|
+
on: [...listener.on],
|
|
2864
|
+
kind: listener.workflow ? "workflow" : "handler"
|
|
2865
|
+
})),
|
|
2866
|
+
dispatch: this.dispatchOptions !== false,
|
|
2867
|
+
recorders: [...this.recorderNames]
|
|
2868
|
+
};
|
|
2869
|
+
}
|
|
2870
|
+
/**
|
|
2871
|
+
* Where the workflow bundle is read from, the project it is built from, and
|
|
2872
|
+
* the world the workflows run on. Only set when a listener runs a workflow.
|
|
2873
|
+
* The CLI builds the bundle there before events start, and shows the world's
|
|
2874
|
+
* runs in the dev dashboard.
|
|
2875
|
+
*/
|
|
2876
|
+
get workflows() {
|
|
2877
|
+
return this.runtime && {
|
|
2878
|
+
root: this.runtime.root,
|
|
2879
|
+
dir: this.runtime.dir,
|
|
2880
|
+
world: this.runtime.world
|
|
2881
|
+
};
|
|
2882
|
+
}
|
|
2883
|
+
/**
|
|
2884
|
+
* Resume the workflow that waits on a webhook. The server mounts this on the
|
|
2885
|
+
* route the Workflow SDK puts in webhook URLs.
|
|
2886
|
+
*/
|
|
2887
|
+
webhook(request) {
|
|
2888
|
+
if (!this.runtime) throw new Error("No listener of this app runs a workflow");
|
|
2889
|
+
return this.runtime.handleWebhook(request);
|
|
2890
|
+
}
|
|
2891
|
+
/**
|
|
2892
|
+
* Start delivering recorded events from this process, and start the world that
|
|
2893
|
+
* runs workflow listeners. Does nothing if this process only records events
|
|
2894
|
+
* (`dispatch: false`), already started, or a stop is still waiting.
|
|
2895
|
+
*/
|
|
2896
|
+
start() {
|
|
2897
|
+
if (this.dispatchOptions === false || this.started || this.stopping) return;
|
|
2898
|
+
this.started = true;
|
|
2899
|
+
startedHubs.add(this);
|
|
2900
|
+
this.runtime?.warmUp();
|
|
2901
|
+
for (const dispatcher of this.dispatchers.values()) dispatcher.start();
|
|
2902
|
+
}
|
|
2903
|
+
/**
|
|
2904
|
+
* Stop delivering, wait for the deliveries that are running, and close the
|
|
2905
|
+
* world. A world cannot be started again, so workflow listeners stop for good.
|
|
2906
|
+
* A stop that arrives while another runs waits for that one.
|
|
2907
|
+
*/
|
|
2908
|
+
stop() {
|
|
2909
|
+
return this.stopping ??= this.shutDown().finally(() => {
|
|
2910
|
+
this.stopping = void 0;
|
|
2911
|
+
startedHubs.delete(this);
|
|
2912
|
+
});
|
|
2913
|
+
}
|
|
2914
|
+
async shutDown() {
|
|
2915
|
+
this.started = false;
|
|
2916
|
+
await Promise.all([...this.dispatchers.values()].map((dispatcher) => dispatcher.stop()));
|
|
2917
|
+
await this.runtime?.close();
|
|
2918
|
+
}
|
|
2919
|
+
/**
|
|
2920
|
+
* Deliver everything that is due now and wait for it. For tests and for
|
|
2921
|
+
* workers that drive delivery themselves.
|
|
2922
|
+
*
|
|
2923
|
+
* @returns How many deliveries ran.
|
|
2924
|
+
*/
|
|
2925
|
+
async drain() {
|
|
2926
|
+
let total = 0;
|
|
2927
|
+
for (const dispatcher of this.dispatchers.values()) total += await dispatcher.drain();
|
|
2928
|
+
return total;
|
|
2929
|
+
}
|
|
2930
|
+
/** How many rows are in each state, across the app's outbox tables. */
|
|
2931
|
+
async counts() {
|
|
2932
|
+
const total = {
|
|
2933
|
+
pending: 0,
|
|
2934
|
+
claimed: 0,
|
|
2935
|
+
delivered: 0,
|
|
2936
|
+
dead: 0
|
|
2937
|
+
};
|
|
2938
|
+
for (const dispatcher of this.dispatchers.values()) {
|
|
2939
|
+
const counts = await dispatcher.counts();
|
|
2940
|
+
for (const status of Object.keys(total)) total[status] += counts[status];
|
|
2941
|
+
}
|
|
2942
|
+
return total;
|
|
2943
|
+
}
|
|
2944
|
+
/**
|
|
2945
|
+
* Put parked deliveries back in line.
|
|
2946
|
+
*
|
|
2947
|
+
* @param deliveryId - One delivery. Omit it to retry every dead one.
|
|
2948
|
+
* @returns How many deliveries were put back.
|
|
2949
|
+
*/
|
|
2950
|
+
async retryDead(deliveryId) {
|
|
2951
|
+
let total = 0;
|
|
2952
|
+
for (const dispatcher of this.dispatchers.values()) total += await dispatcher.retryDead(deliveryId);
|
|
2953
|
+
return total;
|
|
2954
|
+
}
|
|
2955
|
+
deliverable(name) {
|
|
2956
|
+
const listener = this.byName.get(name);
|
|
2957
|
+
if (!listener) return void 0;
|
|
2958
|
+
let found = this.deliverables.get(name);
|
|
2959
|
+
if (!found) {
|
|
2960
|
+
const runtime = this.runtime;
|
|
2961
|
+
found = listener.workflow ? {
|
|
2962
|
+
...listener.maxAttempts !== void 0 ? { maxAttempts: listener.maxAttempts } : {},
|
|
2963
|
+
run: async (delivery) => {
|
|
2964
|
+
await runtime.startRun(listener, delivery);
|
|
2965
|
+
}
|
|
2966
|
+
} : listener;
|
|
2967
|
+
this.deliverables.set(name, found);
|
|
2968
|
+
}
|
|
2969
|
+
return found;
|
|
2970
|
+
}
|
|
2971
|
+
bind(table) {
|
|
2972
|
+
const startAndNotify = () => {
|
|
2973
|
+
this.start();
|
|
2974
|
+
this.dispatchers.get(table)?.notify();
|
|
2975
|
+
};
|
|
2976
|
+
const insert = async (tx, context, name, payload) => {
|
|
2977
|
+
const listeners = this.matching(name);
|
|
2978
|
+
if (!listeners.length) return false;
|
|
2979
|
+
const eventId = newEventId();
|
|
2980
|
+
const occurredAt = /* @__PURE__ */ new Date();
|
|
2981
|
+
await tx.insert(table).values(listeners.map((listener) => ({
|
|
2982
|
+
id: `${eventId}:${listener.name}`,
|
|
2983
|
+
eventId,
|
|
2984
|
+
listener: listener.name,
|
|
2985
|
+
name,
|
|
2986
|
+
payload,
|
|
2987
|
+
traceId: context.traceId ?? null,
|
|
2988
|
+
source: context.source ?? null,
|
|
2989
|
+
occurredAt,
|
|
2990
|
+
status: "pending",
|
|
2991
|
+
attempts: 0,
|
|
2992
|
+
availableAt: occurredAt
|
|
2993
|
+
})));
|
|
2994
|
+
return true;
|
|
2995
|
+
};
|
|
2996
|
+
return {
|
|
2997
|
+
wants: (name) => this.matching(name).length > 0,
|
|
2998
|
+
transaction: async (db, context, fn) => {
|
|
2999
|
+
let recorded = false;
|
|
3000
|
+
const result = await runInTransaction(db, async (tx) => {
|
|
3001
|
+
const record = async (name, payload) => {
|
|
3002
|
+
if (await insert(tx, context, name, payload)) recorded = true;
|
|
3003
|
+
};
|
|
3004
|
+
return fn(tx, record);
|
|
3005
|
+
});
|
|
3006
|
+
if (recorded) startAndNotify();
|
|
3007
|
+
return result;
|
|
3008
|
+
},
|
|
3009
|
+
record: async (tx, context, name, payload) => {
|
|
3010
|
+
await insert(tx, context, name, payload);
|
|
3011
|
+
}
|
|
3012
|
+
};
|
|
3013
|
+
}
|
|
3014
|
+
};
|
|
3015
|
+
//#endregion
|
|
2221
3016
|
//#region src/server.ts
|
|
2222
3017
|
/**
|
|
2223
3018
|
* Create the HTTP application: an Elysia app that serves every service's
|
|
@@ -2284,6 +3079,8 @@ function brick(options = {}) {
|
|
|
2284
3079
|
else registerReferenceRoute(app, settings);
|
|
2285
3080
|
}
|
|
2286
3081
|
const services = options.services ?? getGlobalRegistry().list();
|
|
3082
|
+
const events = options.events ? new EventsHub(options.events).attach(services) : void 0;
|
|
3083
|
+
if (events?.workflows) app.all(WEBHOOK_ROUTE, ({ request }) => events.webhook(request));
|
|
2287
3084
|
if (telemetry) for (const service of services) instrumentDatabase(service.getDb());
|
|
2288
3085
|
for (const service of services) if (typeof service.build === "function") service.build();
|
|
2289
3086
|
const endpointDefs = [];
|
|
@@ -2409,9 +3206,11 @@ function brick(options = {}) {
|
|
|
2409
3206
|
routes: Object.freeze(mountedRoutes),
|
|
2410
3207
|
telemetry,
|
|
2411
3208
|
documentation: normalizeDocumentation(options.documentation),
|
|
2412
|
-
dashboard
|
|
3209
|
+
dashboard,
|
|
3210
|
+
...events ? { events: events.describe() } : {}
|
|
2413
3211
|
});
|
|
2414
3212
|
Object.defineProperty(brickApp, "definition", { value: definition });
|
|
3213
|
+
Object.defineProperty(brickApp, "events", { value: events });
|
|
2415
3214
|
getGlobalRegistry().setApplicationRoutes(definition);
|
|
2416
3215
|
app.get("/_brick/services", compiled ? cachedJsonRoute(() => getGlobalRegistry().exportArchitecture(definition), () => {
|
|
2417
3216
|
const registry = getGlobalRegistry();
|
|
@@ -3040,6 +3839,14 @@ var ServiceImpl = class {
|
|
|
3040
3839
|
getDb() {
|
|
3041
3840
|
return this.database?.getDb();
|
|
3042
3841
|
}
|
|
3842
|
+
/** What a write needs to record events. Set by `brick({ events })`. */
|
|
3843
|
+
events;
|
|
3844
|
+
getOutbox() {
|
|
3845
|
+
return Object.values(this.database?.tables ?? {}).find(isOutboxTable);
|
|
3846
|
+
}
|
|
3847
|
+
setEvents(events) {
|
|
3848
|
+
this.events = events;
|
|
3849
|
+
}
|
|
3043
3850
|
setDb(database) {
|
|
3044
3851
|
if (!isDatabaseHandle(database)) throw new Error("Use defineDatabase() to set a service database");
|
|
3045
3852
|
this.database = database;
|
|
@@ -3214,6 +4021,107 @@ function createRpcEnvelope(from, defaultContext, callContext) {
|
|
|
3214
4021
|
return envelope;
|
|
3215
4022
|
}
|
|
3216
4023
|
//#endregion
|
|
4024
|
+
//#region src/events/listener.ts
|
|
4025
|
+
/**
|
|
4026
|
+
* Define a listener: code that runs for each event it listens for, after the write
|
|
4027
|
+
* that caused the event has been committed. Pass it to `brick({ events: {
|
|
4028
|
+
* listeners } })`.
|
|
4029
|
+
*
|
|
4030
|
+
* It either runs a handler (`run`) in the process that delivers events, or starts
|
|
4031
|
+
* a durable workflow (`workflow`) on the app's `events.world`. Delivery is at
|
|
4032
|
+
* least once. A handler that throws is retried later with a growing delay, then
|
|
4033
|
+
* parked as dead, so it can run again for the same event: use
|
|
4034
|
+
* `delivery.deliveryId` to make its work idempotent. A workflow needs no such
|
|
4035
|
+
* care: a delivery starts one run however often it is tried, and the workflow
|
|
4036
|
+
* retries its own steps.
|
|
4037
|
+
*
|
|
4038
|
+
* @param config - `name` (unique; it is part of each delivery's id), `on` (an
|
|
4039
|
+
* event name or a list of them; a trailing `*` matches a prefix, as in
|
|
4040
|
+
* `user.*`), `run` or `workflow`, and optionally `maxAttempts`.
|
|
4041
|
+
* @returns The listener.
|
|
4042
|
+
* @throws When the name is empty or contains `:`, `on` is empty, or the listener
|
|
4043
|
+
* has both or neither of `run` and `workflow`.
|
|
4044
|
+
* @example
|
|
4045
|
+
* import { brick, defineListener } from "@brickkit/core";
|
|
4046
|
+
*
|
|
4047
|
+
* const welcome = defineListener({
|
|
4048
|
+
* name: "welcome-email",
|
|
4049
|
+
* on: "user.create",
|
|
4050
|
+
* run: async ({ payload, deliveryId }) => {
|
|
4051
|
+
* await mailer.sendWelcome(payload.row.email, { idempotencyKey: deliveryId });
|
|
4052
|
+
* },
|
|
4053
|
+
* });
|
|
4054
|
+
*
|
|
4055
|
+
* export default brick({ services, events: { listeners: [welcome] } });
|
|
4056
|
+
* @example
|
|
4057
|
+
* // src/workflows/onboard.ts
|
|
4058
|
+
* export async function onboard(event: WorkflowEvent<CrudEventPayload>) {
|
|
4059
|
+
* "use workflow";
|
|
4060
|
+
* await sendWelcome(event.payload.row.email);
|
|
4061
|
+
* }
|
|
4062
|
+
*
|
|
4063
|
+
* defineListener({ name: "onboard", on: "user.create", workflow: onboard });
|
|
4064
|
+
*/
|
|
4065
|
+
function defineListener(config) {
|
|
4066
|
+
if (typeof config.name !== "string" || !config.name) throw new Error("An event listener needs a name");
|
|
4067
|
+
if (config.name.includes(":")) throw new Error(`Event listener name '${config.name}' must not contain ':'`);
|
|
4068
|
+
const on = typeof config.on === "string" ? [config.on] : [...config.on];
|
|
4069
|
+
if (!on.length || on.some((pattern) => !pattern)) throw new Error(`Event listener '${config.name}' listens for nothing`);
|
|
4070
|
+
if (config.maxAttempts !== void 0 && (!Number.isInteger(config.maxAttempts) || config.maxAttempts < 1)) throw new Error(`Event listener '${config.name}': maxAttempts must be a positive integer`);
|
|
4071
|
+
const { name, run, workflow } = config;
|
|
4072
|
+
if (run !== void 0 && workflow !== void 0) throw new Error(`Event listener '${name}' has both run and workflow; give it one`);
|
|
4073
|
+
if (run === void 0 && workflow === void 0) throw new Error(`Event listener '${name}' needs a run handler or a workflow`);
|
|
4074
|
+
if (run !== void 0 && typeof run !== "function") throw new Error(`Event listener '${name}': run must be a function`);
|
|
4075
|
+
if (workflow !== void 0 && typeof workflow !== "function") throw new Error(`Event listener '${name}': workflow must be a function marked "use workflow"`);
|
|
4076
|
+
return Object.freeze({
|
|
4077
|
+
name: config.name,
|
|
4078
|
+
on: Object.freeze(on),
|
|
4079
|
+
...config.run !== void 0 ? { run: config.run } : {},
|
|
4080
|
+
...config.workflow !== void 0 ? { workflow: config.workflow } : {},
|
|
4081
|
+
...config.maxAttempts !== void 0 ? { maxAttempts: config.maxAttempts } : {}
|
|
4082
|
+
});
|
|
4083
|
+
}
|
|
4084
|
+
//#endregion
|
|
4085
|
+
//#region src/events/emit.ts
|
|
4086
|
+
/**
|
|
4087
|
+
* Record an event of your own from an action, a tool or a hook. Listeners get it
|
|
4088
|
+
* as they get a generated CRUD event, with the payload you give.
|
|
4089
|
+
*
|
|
4090
|
+
* By default the event is committed in a transaction of its own. Pass the `tx`
|
|
4091
|
+
* of a transaction you have open to commit it with your writes instead: then it
|
|
4092
|
+
* is delivered at the dispatcher's next poll, because Brick does not see your
|
|
4093
|
+
* commit. Nothing is recorded when no listener wants the event.
|
|
4094
|
+
*
|
|
4095
|
+
* @param ctx - The action context: its `db` finds the service's outbox, and its
|
|
4096
|
+
* `traceId` follows the event to its listeners.
|
|
4097
|
+
* @param name - The event name, such as `order.refunded`.
|
|
4098
|
+
* @param payload - JSON data for the listeners.
|
|
4099
|
+
* @param options - `tx`: an open transaction on the service's database. `source`:
|
|
4100
|
+
* what emitted it, shown to listeners.
|
|
4101
|
+
* @throws When the context's database has no outbox table, or the app has no
|
|
4102
|
+
* `events`.
|
|
4103
|
+
* @example
|
|
4104
|
+
* refund: service.action({
|
|
4105
|
+
* name: "refund",
|
|
4106
|
+
* execute: async ({ ctx, input }) => {
|
|
4107
|
+
* await ctx.db.update(orders).set({ status: "refunded" }).where(eq(orders.id, input.id));
|
|
4108
|
+
* await emitEvent(ctx, "order.refunded", { id: input.id });
|
|
4109
|
+
* },
|
|
4110
|
+
* });
|
|
4111
|
+
*/
|
|
4112
|
+
async function emitEvent(ctx, name, payload, options = {}) {
|
|
4113
|
+
const events = ctx.db && typeof ctx.db === "object" ? recorders.get(ctx.db) : void 0;
|
|
4114
|
+
if (!events) throw new Error(`Cannot emit '${name}': ctx.db is not the database of a service with an outbox table (the app needs events, and ctx.db must be the service's own database, not a transaction)`);
|
|
4115
|
+
const context = {
|
|
4116
|
+
traceId: ctx.traceId,
|
|
4117
|
+
source: options.source
|
|
4118
|
+
};
|
|
4119
|
+
if (options.tx !== void 0) return events.record(options.tx, context, name, payload);
|
|
4120
|
+
await events.transaction(ctx.db, context, async (_tx, record) => {
|
|
4121
|
+
await record(name, payload);
|
|
4122
|
+
});
|
|
4123
|
+
}
|
|
4124
|
+
//#endregion
|
|
3217
4125
|
//#region src/mcp.ts
|
|
3218
4126
|
var UnknownToolError = class extends Error {
|
|
3219
4127
|
constructor(name) {
|
|
@@ -3335,6 +4243,6 @@ function createMcpHandler(options) {
|
|
|
3335
4243
|
};
|
|
3336
4244
|
}
|
|
3337
4245
|
//#endregion
|
|
3338
|
-
export { ACTION_ACCESS_KEY, ActionExecutionError, BrickServerError, BrickTransportError, SecretRef, ServiceImpl, ServiceRegistry, Table, Type, UnknownToolError, ValidationError, and, asc, attachDatabase, bindCompiledApplication, blob, brick, buildErrorHelpers, buildFtsMatchQuery, compileBrickApplication, compileRoutePlan, compilerRuntime, createActionHandler, createBrickClient, createDatabase, createEndpointHandler, createMcpHandler, createMcpRegistry, createPostgresDatabase, createSQLiteDatabase, createServiceProxy, defineAction, defineDatabase, defineResource, defineResourcePlugin, defineService, defineServicePlugin, defineTool, desc, emitHandlerFactory, endpointPaths, ensureResourceIndexes, ensureSQLiteFts, envSource, eq, executeCompiledRoute, foreignKey, generateOpenApiSpec, generateSQLiteIndexDdl, generateSQLiteTableDdl, getActionAccess, getCompiledCheck, getGlobalRegistry, getPluginActionRoute, getPluginEndpoints, getTableColumns, getTableName, gt, gte, hoistAnchoredSchemas, ilike, inArray, index, integer, is, isBrickApp, isBrickServerError, isBrickTransportError, isDatabaseHandle, isNotNull, isNull, isTool, like, listSecrets, loadCompiledBrickApp, lt, lte, ne, not, notInArray, or, overrideSecret, overrideSecrets, primaryKey, real, referencePlugin, registerReferenceRoute, relations, requestLoggingPlugin, requireBrickApp, resetGlobalRegistry, resetSecrets, resolveEndpointDb, resolveSecrets, scalarDocsHTML, secret, setActionAccess, sql, sqliteTable, swaggerDocsHTML, syncSchema, t, text, toErrorResponse, toOpenApiPath, traceRoute, uniqueIndex, validateWithSchema };
|
|
4246
|
+
export { ACTION_ACCESS_KEY, ActionExecutionError, BrickServerError, BrickTransportError, Dispatcher, EventsHub, SecretRef, ServiceImpl, ServiceRegistry, Table, Type, UnknownToolError, ValidationError, and, asc, attachDatabase, bindCompiledApplication, blob, brick, buildErrorHelpers, buildFtsMatchQuery, compileBrickApplication, compileRoutePlan, compilerRuntime, createActionHandler, createBrickClient, createDatabase, createEndpointHandler, createMcpHandler, createMcpRegistry, createPostgresDatabase, createSQLiteDatabase, createServiceProxy, defineAction, defineDatabase, defineListener, defineResource, defineResourcePlugin, defineService, defineServicePlugin, defineTool, desc, emitEvent, emitHandlerFactory, endpointPaths, ensureResourceIndexes, ensureSQLiteFts, envSource, eq, executeCompiledRoute, foreignKey, generateOpenApiSpec, generateSQLiteIndexDdl, generateSQLiteTableDdl, getActionAccess, getCompiledCheck, getGlobalRegistry, getPluginActionRoute, getPluginEndpoints, getTableColumns, getTableName, gt, gte, hoistAnchoredSchemas, ilike, inArray, index, integer, is, isBrickApp, isBrickServerError, isBrickTransportError, isDatabaseHandle, isNotNull, isNull, isOutboxTable, isTool, like, listSecrets, loadCompiledBrickApp, lt, lte, ne, not, notInArray, or, outboxTable, overrideSecret, overrideSecrets, primaryKey, real, referencePlugin, registerReferenceRoute, relations, requestLoggingPlugin, requireBrickApp, resetGlobalRegistry, resetSecrets, resolveEndpointDb, resolveSecrets, resolveWorkflowPaths, runInTransaction, scalarDocsHTML, secret, setActionAccess, sql, sqliteTable, stopAllEvents, swaggerDocsHTML, syncSchema, t, text, toErrorResponse, toOpenApiPath, traceRoute, uniqueIndex, validateWithSchema };
|
|
3339
4247
|
|
|
3340
4248
|
//# sourceMappingURL=index.js.map
|