@relaybus/core 0.1.0 → 0.3.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 +302 -25
- package/dist/chunk-3OHMEMZA.cjs +205 -0
- package/dist/chunk-3OHMEMZA.cjs.map +1 -0
- package/dist/chunk-WS274DL3.js +183 -0
- package/dist/chunk-WS274DL3.js.map +1 -0
- package/dist/event-DCRTuYKI.d.cts +142 -0
- package/dist/event-DCRTuYKI.d.ts +142 -0
- package/dist/index.cjs +2183 -671
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +871 -247
- package/dist/index.d.ts +871 -247
- package/dist/index.js +2054 -605
- package/dist/index.js.map +1 -1
- package/dist/internal/index.cjs +444 -0
- package/dist/internal/index.cjs.map +1 -0
- package/dist/internal/index.d.cts +171 -0
- package/dist/internal/index.d.ts +171 -0
- package/dist/internal/index.js +425 -0
- package/dist/internal/index.js.map +1 -0
- package/package.json +37 -8
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# @relaybus/core
|
|
2
2
|
|
|
3
|
+
@relaybus/core is the engine of relaybus, a reliable, storage-agnostic event bus for NestJS and Node.js: it writes events in the same database transaction as your data (the transactional outbox pattern) and delivers them to every handler with per-handler retries, leases, dead letters and ordering keys.
|
|
4
|
+
|
|
3
5
|
The relaybus engine: transactional publishing, per-handler deliveries, leased
|
|
4
6
|
dispatching, retries with jitter, dead letters, ordering keys and transport
|
|
5
7
|
routing. Most applications install it together with a store and, for NestJS,
|
|
@@ -11,7 +13,7 @@ pnpm add @relaybus/core @relaybus/nestjs @relaybus/postgres pg
|
|
|
11
13
|
|
|
12
14
|
| Package | Use it for |
|
|
13
15
|
| ------------------------------------------------------------------------ | ------------------------------------------------------------- |
|
|
14
|
-
| [`@relaybus/nestjs`](https://www.npmjs.com/package/@relaybus/nestjs) | `RelayBusModule` and the `@
|
|
16
|
+
| [`@relaybus/nestjs`](https://www.npmjs.com/package/@relaybus/nestjs) | `RelayBusModule` and the `@RelayBusConsumer` decorator |
|
|
15
17
|
| [`@relaybus/postgres`](https://www.npmjs.com/package/@relaybus/postgres) | PostgreSQL store (pg, Prisma, TypeORM, Knex, Kysely, Drizzle) |
|
|
16
18
|
| [`@relaybus/mongodb`](https://www.npmjs.com/package/@relaybus/mongodb) | MongoDB store (driver or Mongoose sessions) |
|
|
17
19
|
| [`@relaybus/kafka`](https://www.npmjs.com/package/@relaybus/kafka) | Kafka transport between services |
|
|
@@ -31,7 +33,7 @@ export class AppModule {}
|
|
|
31
33
|
// shipping.handlers.ts: consume with a decorator
|
|
32
34
|
@Injectable()
|
|
33
35
|
export class ShippingHandlers {
|
|
34
|
-
@
|
|
36
|
+
@RelayBusConsumer(OrderPlaced, { retry: { attempts: 5 } })
|
|
35
37
|
async ship(event: EventEnvelope<{ orderId: string }>) {
|
|
36
38
|
/* runs after the order transaction commits */
|
|
37
39
|
}
|
|
@@ -76,32 +78,46 @@ Use a database store in production: [`@relaybus/postgres`](../postgres) or
|
|
|
76
78
|
|
|
77
79
|
## `new RelayBus(options)`
|
|
78
80
|
|
|
79
|
-
| Option | Default | Description
|
|
80
|
-
| ----------------------------------------- | --------------------------------------------- |
|
|
81
|
-
| `store` | required | `EventStore` implementation.
|
|
82
|
-
| `source` | `'relaybus'` | Service name recorded on every event.
|
|
83
|
-
| `transactionMode` | `'required'` | `'optional'` lets `publish` open its own transaction.
|
|
84
|
-
| `transactionResolver` | — | Returns the caller's active transaction, e.g. from `@nestjs-cls/transactional`.
|
|
85
|
-
| `
|
|
86
|
-
| `
|
|
87
|
-
| `
|
|
88
|
-
| `
|
|
89
|
-
| `dispatcher.
|
|
90
|
-
| `dispatcher.
|
|
91
|
-
| `dispatcher.
|
|
92
|
-
| `dispatcher.
|
|
93
|
-
| `dispatcher.
|
|
94
|
-
| `
|
|
95
|
-
| `
|
|
96
|
-
| `
|
|
97
|
-
| `
|
|
98
|
-
| `
|
|
99
|
-
| `
|
|
100
|
-
| `
|
|
101
|
-
| `
|
|
81
|
+
| Option | Default | Description |
|
|
82
|
+
| ----------------------------------------- | --------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
83
|
+
| `store` | required | `EventStore` implementation. |
|
|
84
|
+
| `source` | `'relaybus'` | Service name recorded on every event. |
|
|
85
|
+
| `transactionMode` | `'required'` | `'optional'` lets `publish` open its own transaction. |
|
|
86
|
+
| `transactionResolver` | — | Returns the caller's active transaction, e.g. from `@nestjs-cls/transactional`. |
|
|
87
|
+
| `transactionTimeout` | `'5s'` | Budget of every `bus.transaction`; the database enforces it where it can, an overrun rolls back with `TransactionTimeoutError`. |
|
|
88
|
+
| `unroutedEvents` | `'warn'` | `'throw'` rejects events without any handler or route. |
|
|
89
|
+
| `retry` | 10 attempts, `1s`, ×2, max `10m`, full jitter | Default handler retry policy. `attempts: Infinity` retries forever. |
|
|
90
|
+
| `handlerTimeout` | `false` | Default handler timeout. |
|
|
91
|
+
| `dispatcher.enabled` | `true` | Run handlers in this process. |
|
|
92
|
+
| `dispatcher.concurrency` | `10` | Handlers running at once. |
|
|
93
|
+
| `dispatcher.batchSize` | concurrency | Deliveries claimed per query. |
|
|
94
|
+
| `dispatcher.pollInterval` | `'1s'` | Idle wait between claims; retries are picked up on this cadence. |
|
|
95
|
+
| `dispatcher.leaseDuration` | `'30s'` | Lease length, renewed every third while a handler runs. |
|
|
96
|
+
| `dispatcher.shutdownTimeout` | `'30s'` | Longest `stop` takes in all (default of `stop({ timeout })`); handlers are aborted and released at four fifths of it. |
|
|
97
|
+
| `dispatcher.completionBatchWindow` | `'2ms'` | Deliveries that finish within this window are completed with one store call; `0` completes one by one. |
|
|
98
|
+
| `dispatcher.completionBatchSize` | `200` | Most deliveries completed in one store call. |
|
|
99
|
+
| `retention.completed` | `'7d'` | Delete events whose deliveries all completed after this; also the deduplication horizon. A completed delivery is deleted at once (see below). |
|
|
100
|
+
| `retention.dead` | `false` | Delete dead letters after this. |
|
|
101
|
+
| `retention.interval` | `'1h'` | Cleanup cadence. |
|
|
102
|
+
| `transports` / `routes` / `subscriptions` | — | Broker integration, see below. |
|
|
103
|
+
| `setupStore` | `true` | Create tables and indexes on `start`. |
|
|
104
|
+
| `observers` | `[]` | Lifecycle hooks for logs, metrics and tracing (see Observability below). |
|
|
105
|
+
| `tracer` | none | Spans around publish, send and handler runs; writes `traceparent`/`tracestate` into published events. `@relaybus/otel` provides one. |
|
|
106
|
+
| `backlogInterval` | off | Reads `statistics()` this often while running and reports it to `onBacklog`. |
|
|
107
|
+
| `logger` | `console` | `{ log, warn, error }`. |
|
|
108
|
+
| `clock`, `generateIdentifier`, `random` | system | Replace in tests. A clock with `schedule` also runs the bus's timers. |
|
|
102
109
|
|
|
103
110
|
Durations are milliseconds or strings such as `'250ms'`, `'30s'`, `'5m'`, `'1h'`, `'7d'`.
|
|
104
111
|
|
|
112
|
+
**How the dispatcher schedules claims.** A claim that fills its batch is followed by the next one at once. Otherwise the
|
|
113
|
+
dispatcher sleeps until it is woken: by a publish, by the store reporting new deliveries from any process (the PostgreSQL
|
|
114
|
+
store through `LISTEN/NOTIFY`, the MongoDB store through a change stream), or by settled deliveries once 70% of a batch
|
|
115
|
+
(of `min(concurrency, batchSize)`) is free again, so slots are refilled in groups rather than one by one. A smaller number
|
|
116
|
+
of free slots is refilled after 10 ms without a claim, and the poll interval stays the safety net. Wake-ups within 5 ms
|
|
117
|
+
of a claim are merged into one claim. Settled unkeyed deliveries do not trigger a claim when their handler's last claim
|
|
118
|
+
got less than it asked for (nothing more was due); a settled keyed delivery always may, since it unblocks the next of
|
|
119
|
+
its key.
|
|
120
|
+
|
|
105
121
|
## Publishing
|
|
106
122
|
|
|
107
123
|
`publish(event, options)` and `publishAll(events, options)` write into, in
|
|
@@ -110,6 +126,11 @@ order: `options.transaction`, the transaction of `bus.transaction()`, then
|
|
|
110
126
|
and `id` on `publish` for idempotent publishing. Register every handler in each
|
|
111
127
|
process that publishes the event: deliveries are created at publish time.
|
|
112
128
|
|
|
129
|
+
Declare the ordering key on the event to apply it to every publish: `defineEvent('order.placed.v1',
|
|
130
|
+
{ schema, key: (payload) => payload.orderId })` (or `defineEvent<Payload>(type, { key })`). The
|
|
131
|
+
function is typed from the payload, so a wrong field fails to compile. An explicit `key` option
|
|
132
|
+
overrides it; in `publishAll` an explicit key applies to every event of the call.
|
|
133
|
+
|
|
113
134
|
## Handlers
|
|
114
135
|
|
|
115
136
|
`subscribe(event, handler, { id, retry, timeout, concurrency, transactional })`.
|
|
@@ -122,6 +143,125 @@ The handler receives the envelope and a context with `attempt`, `maxAttempts`,
|
|
|
122
143
|
- After a timeout or lease loss the handler's `signal` is aborted; the retry waits
|
|
123
144
|
for the abandoned run to settle so runs never overlap.
|
|
124
145
|
|
|
146
|
+
## Event catalogs
|
|
147
|
+
|
|
148
|
+
A catalog groups the events of one domain so the compiler checks that every event
|
|
149
|
+
has a handler and that only those events are published through it.
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
class OrderPlaced extends defineEvent<{ orderId: string }>('order.placed.v1') {}
|
|
153
|
+
class OrderShipped extends defineEvent<{ orderId: string; carrier: string }>('order.shipped.v1') {}
|
|
154
|
+
|
|
155
|
+
export const OrderEvents = defineCatalog({ OrderPlaced, OrderShipped }, { name: 'OrderEvents' });
|
|
156
|
+
|
|
157
|
+
// A class that misses a handler does not compile:
|
|
158
|
+
class ShippingHandlers implements HandlersOf<typeof OrderEvents> {
|
|
159
|
+
onOrderPlaced(event: EventOf<typeof OrderPlaced>, context: HandlerContext) {}
|
|
160
|
+
onOrderShipped(event: EventOf<typeof OrderShipped>) {}
|
|
161
|
+
}
|
|
162
|
+
bus.subscribeCatalog(OrderEvents, new ShippingHandlers()); // ids: OrderEvents.onOrderPlaced, ...
|
|
163
|
+
|
|
164
|
+
// Or declare the handlers as an object; missing or extra keys do not compile:
|
|
165
|
+
bus.subscribeCatalog(
|
|
166
|
+
OrderEvents,
|
|
167
|
+
defineHandlers(OrderEvents, {
|
|
168
|
+
onOrderPlaced: (event) => reserveStock(event.payload.orderId),
|
|
169
|
+
onOrderShipped: (event) => notify(event.payload.carrier),
|
|
170
|
+
}),
|
|
171
|
+
{ idPrefix: 'fulfilment' },
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
// Publish through a view that only accepts the catalog's events:
|
|
175
|
+
const orders = bus.forCatalog(OrderEvents);
|
|
176
|
+
await orders.publish(new OrderPlaced({ orderId }), { transaction });
|
|
177
|
+
await orders.publish(new UserRegistered({ userId })); // compile error
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
- `defineCatalog(events, { name? })` returns a frozen catalog with frozen entries. It rejects
|
|
181
|
+
empty catalogs, entries that are not event classes, names that are not identifiers of letters,
|
|
182
|
+
digits and underscores (a name becomes part of a method name and a handler id) and two entries
|
|
183
|
+
with one event type.
|
|
184
|
+
- `HandlersOf<C>` requires an `on${Key}` method per event; `HandlersOf<C, 'A' | 'B'>` requires
|
|
185
|
+
only those (`PartialHandlersOf<C, 'A' | 'B'>` is the same type), and `HandlersOf<C, K, Tx>` types
|
|
186
|
+
`context.transaction`. `EventOf<typeof OrderPlaced>` is the envelope type of one event;
|
|
187
|
+
`CatalogEvents<C, K?>` is the union of the catalog's (selected) events.
|
|
188
|
+
- `bus.subscribeCatalog(catalog, handlers, { events?, idPrefix?, ...subscribeOptions })` registers
|
|
189
|
+
the `on${Key}` method of every event (or of the `events` listed) with the stable id
|
|
190
|
+
`${idPrefix ?? catalog name ?? class name}.on${Key}` and `this` bound to `handlers`. Methods of
|
|
191
|
+
events that are not listed are ignored. A missing method does not compile and throws a
|
|
192
|
+
`ConfigurationError` naming it. Handlers are narrowed only by an `events` list the compiler can see;
|
|
193
|
+
options whose `events` may be absent require every handler.
|
|
194
|
+
- `defineHandlers(catalog, handlers, { events? })` checks completeness, extra keys (including
|
|
195
|
+
handlers of events that are not listed) and payload types at compile time and at runtime. A
|
|
196
|
+
handler may type its context, for example `HandlerContext<PoolClient>`.
|
|
197
|
+
- **Boot check:** `new RelayBus({ catalogs: [OrderEvents] })` (or `bus.registerCatalog`) makes
|
|
198
|
+
`start()` throw a `ConfigurationError` listing every catalog event that has no handler or route
|
|
199
|
+
and the method it expects. A failed check leaves the bus idle, before any I/O, so it can be fixed
|
|
200
|
+
and started again. For services that share a catalog, declare the role instead; see below.
|
|
201
|
+
- `forCatalog(...).publish` also checks the event type at runtime. TypeScript compares event
|
|
202
|
+
classes by shape, so two events with identical payload types are interchangeable to the compiler;
|
|
203
|
+
the runtime check still rejects the one that is not in the catalog, before anything is stored.
|
|
204
|
+
|
|
205
|
+
### Microservices: shared catalogs
|
|
206
|
+
|
|
207
|
+
In a system of services the catalog usually lives in a shared contracts package, and each service
|
|
208
|
+
produces some of its events and consumes others. Declare what this service does with each part:
|
|
209
|
+
|
|
210
|
+
```ts
|
|
211
|
+
import { consume, produce } from '@relaybus/core';
|
|
212
|
+
import { OrderEvents } from '@acme/contracts';
|
|
213
|
+
|
|
214
|
+
// orders service: publishes OrderPlaced to the broker, handles OrderShipped
|
|
215
|
+
class DeliveryStatusHandlers implements HandlersOf<typeof OrderEvents, 'OrderShipped'> {
|
|
216
|
+
onOrderShipped(event: EventOf<typeof OrderShipped>) {}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const bus = new RelayBus({
|
|
220
|
+
store,
|
|
221
|
+
transports: [kafka],
|
|
222
|
+
routes: [{ event: OrderPlaced, transport: 'kafka', destination: 'orders' }],
|
|
223
|
+
subscriptions: [{ transport: 'kafka', destinations: ['shipments'] }],
|
|
224
|
+
catalogs: [
|
|
225
|
+
produce(OrderEvents, { events: ['OrderPlaced'] }),
|
|
226
|
+
consume(OrderEvents, { events: ['OrderShipped'] }),
|
|
227
|
+
],
|
|
228
|
+
});
|
|
229
|
+
bus.subscribeCatalog(OrderEvents, new DeliveryStatusHandlers(), { events: ['OrderShipped'] });
|
|
230
|
+
const orders = bus.forCatalog(OrderEvents, { events: ['OrderPlaced'] });
|
|
231
|
+
await orders.publish(new OrderPlaced({ orderId }), { transaction, key: orderId });
|
|
232
|
+
await orders.publish(new OrderShipped({ orderId, carrier })); // compile error: not produced here
|
|
233
|
+
|
|
234
|
+
// shipping service: handles OrderPlaced only
|
|
235
|
+
class ShipmentHandlers implements HandlersOf<typeof OrderEvents, 'OrderPlaced'> {
|
|
236
|
+
onOrderPlaced(event: EventOf<typeof OrderPlaced>) {}
|
|
237
|
+
}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
- `consume(catalog, { events? })`: each listed event (default: all) needs a **local handler**;
|
|
241
|
+
a route does not count. `produce(catalog, { events? })`: each listed event needs a **route to a
|
|
242
|
+
transport**, so nothing is published without a destination; a local handler does not count.
|
|
243
|
+
Both can be declared for one catalog, and for one event. Unknown names do not compile and are
|
|
244
|
+
rejected at runtime, as are empty and duplicate lists. A bare catalog keeps its meaning: every
|
|
245
|
+
event needs a handler or a route.
|
|
246
|
+
- `start()` lists every gap, per event:
|
|
247
|
+
|
|
248
|
+
```text
|
|
249
|
+
ConfigurationError: Catalog events this service does not cover:
|
|
250
|
+
- OrderEvents.OrderShipped (order.shipped.v1): consumed here, needs handler method onOrderShipped
|
|
251
|
+
- OrderEvents.OrderPlaced (order.placed.v1): produced here, needs a route to a transport
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
- `bus.forCatalog(catalog, { events })` publishes only the listed events: others do not compile
|
|
255
|
+
and are rejected at runtime before anything is stored.
|
|
256
|
+
- Received events (`bus.receive`, called by Kafka and RabbitMQ consumers) are matched to the
|
|
257
|
+
catalog handlers by event type, idempotently on the event id, and the payload is validated
|
|
258
|
+
against the event's schema before the handler runs; a payload that fails it is dead-lettered.
|
|
259
|
+
- Per-key order on the wire holds **per destination**: every route to one destination of one
|
|
260
|
+
transport shares one queue per key, so `OrderPlaced` and `OrderCancelled` of one key reach the
|
|
261
|
+
broker in publish order even when a send of the first is retried (Kafka: same partition;
|
|
262
|
+
RabbitMQ: `ordered: true`). On the consuming side order holds **per handler**: two handlers of
|
|
263
|
+
two types run independently and can interleave.
|
|
264
|
+
|
|
125
265
|
## Transports
|
|
126
266
|
|
|
127
267
|
```ts
|
|
@@ -138,17 +278,154 @@ Routes are deliveries like any handler: retried until the broker accepts them
|
|
|
138
278
|
`bus.receive(envelope)`, which stores the event once by id for local handlers;
|
|
139
279
|
transports acknowledge only after it resolves and dead-letter `InvalidEnvelopeError`.
|
|
140
280
|
|
|
281
|
+
Every route to one destination of one transport is **one handler**, with the id
|
|
282
|
+
`relaybus.transport:<transport>:<destination>` (`handlers()` lists it once per routed
|
|
283
|
+
event type). Its deliveries of one key are sent one at a time in commit order, whatever
|
|
284
|
+
their event types: while the oldest is retried, the later ones of that key wait until it
|
|
285
|
+
is sent or dead-lettered (a dead-lettered head releases them, as for any handler). Other
|
|
286
|
+
keys and unkeyed events are not held back. Because the routes share one queue, they must
|
|
287
|
+
resolve to the same `retry` and `timeout`; `new RelayBus` throws a `ConfigurationError`
|
|
288
|
+
when they differ, and when one event type is routed to the same destination twice. Each
|
|
289
|
+
delivery is one transport call: a hot key is sent one event per round trip (sending a run
|
|
290
|
+
of one key's deliveries in one ordered call is a possible follow-up, not implemented).
|
|
291
|
+
|
|
292
|
+
### Upgrading from 0.2.0
|
|
293
|
+
|
|
294
|
+
0.2.0 registered one route per event type, with the id
|
|
295
|
+
`relaybus.transport:<transport>:<destination>:<eventType>`, so events of one key but two
|
|
296
|
+
types could reach the broker out of order. 0.3.0 groups them under
|
|
297
|
+
`relaybus.transport:<transport>:<destination>`. Nothing has to be migrated: deliveries
|
|
298
|
+
stored under a 0.2.0 id, including those written by 0.2.0 processes during a rolling
|
|
299
|
+
deploy, are still claimed and sent by the new route; the store rows keep their old id.
|
|
300
|
+
|
|
301
|
+
- **Order during the upgrade.** A delivery under a 0.2.0 id and one under the new id are
|
|
302
|
+
two queues, so one key's backlog from 0.2.0 is not ordered against events published
|
|
303
|
+
after the upgrade. Where that matters, let the route backlog drain first
|
|
304
|
+
(`listDeliveries({ handler: '<0.2.0 id>', status: 'pending' })` returns nothing) and
|
|
305
|
+
stop 0.2.0 publishers before new ones start.
|
|
306
|
+
- **Routes to one destination with different `retry` or `timeout`** now fail at
|
|
307
|
+
construction; give them the same settings.
|
|
308
|
+
- Code or dashboards that match route ids (`handlers()`, `listDeliveries({ handler })`,
|
|
309
|
+
`HandlerContext.handler`, observer events) see the new id for new deliveries.
|
|
310
|
+
|
|
141
311
|
## Operations
|
|
142
312
|
|
|
143
313
|
`listDeliveries(query)`, `requeue(ids)`, `statistics()`, `purgeExpired()`,
|
|
144
314
|
`handlers()`, `runOnce()` and `drain()` (tests), `start()` and `stop()`.
|
|
145
315
|
|
|
316
|
+
**`stop({ timeout })` is bounded.** The whole shutdown (stop claiming, wait for running
|
|
317
|
+
handlers, hand back leases, close transports and the store) takes at most `timeout`, default
|
|
318
|
+
`dispatcher.shutdownTimeout` (30 s). Running handlers get four fifths of it; then their
|
|
319
|
+
`context.signal` aborts and their deliveries are released for another process. If the deadline
|
|
320
|
+
itself passes (a hung query, a broker that does not answer), `stop` gives up waiting, force-closes
|
|
321
|
+
what relaybus opened itself (the pool a store created from `connectionString`, its listening
|
|
322
|
+
connection, a RabbitMQ transport's connection) and resolves; it never closes a pool or client you
|
|
323
|
+
passed in, and never rejects because of the deadline. It resolves with a `StopResult`:
|
|
324
|
+
|
|
325
|
+
```ts
|
|
326
|
+
const { graceful, abandonedDeliveries, durationMs } = await bus.stop({ timeout: '25s' });
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
`graceful` is false when the deadline passed, and a warning is logged
|
|
330
|
+
(`[RelayBus](stop) Shutdown deadline passed, forced close | abandoned=1 | timeoutMs=25000`).
|
|
331
|
+
Abandoned deliveries keep their lease until it expires (`dispatcher.leaseDuration`), then another
|
|
332
|
+
process runs them again: at-least-once still holds, so keep handlers idempotent. Kafka's
|
|
333
|
+
`disconnect` has no forced variant; past the deadline the bus simply stops waiting for it.
|
|
334
|
+
|
|
335
|
+
**Observability.** Besides the lifecycle hooks, observers can implement timed hooks:
|
|
336
|
+
`onPublishFinished` (event types, duration, outcome), `onClaimFinished` (requested, claimed,
|
|
337
|
+
lanes, duration), `onHandlerStarted` (end-to-end latency from `occurredAt`),
|
|
338
|
+
`onHandlerFinished` (duration, outcome, error with its code), `onTransportSent` (transport,
|
|
339
|
+
system, destination, duration, outcome), `onReceiveFinished` (transport, destination, source,
|
|
340
|
+
trace context; `accepted`, `duplicate`, `ignored`, `invalid` or `error`), `onInvalidMessage`
|
|
341
|
+
(a transport could not decode an inbound message, so it never reached the bus: transport,
|
|
342
|
+
destination, reason) and `onBacklog`
|
|
343
|
+
(pending deliveries and age of the oldest due one, every `backlogInterval`). A hook no observer
|
|
344
|
+
implements costs nothing, not even a clock read. Hooks run synchronously and must not throw; a
|
|
345
|
+
throwing hook is reported to `onFailure` with stage `observer`. `traceContextOf(event.headers)`
|
|
346
|
+
returns the W3C trace context an event carries. `@relaybus/otel` maps all of it to OpenTelemetry.
|
|
347
|
+
|
|
348
|
+
**Completed deliveries are deleted.** Completing a delivery removes its row instead
|
|
349
|
+
of updating it, which is what keeps the write cost of a delivery constant (see
|
|
350
|
+
`docs/performance/write-amplification.md`). `listDeliveries({ status: 'completed' })`
|
|
351
|
+
and `statistics().completed` therefore report nothing unless the store keeps a
|
|
352
|
+
completion history (`completionHistory: true` on the SQL stores,
|
|
353
|
+
`retainCompleted: true` on `InMemoryEventStore`). Deduplication does not depend
|
|
354
|
+
on it: an event remembers every handler it ever had a delivery for, so a
|
|
355
|
+
redelivered or republished event never runs a completed handler again, for as
|
|
356
|
+
long as the event is kept (`retention.completed`).
|
|
357
|
+
|
|
358
|
+
## Error codes
|
|
359
|
+
|
|
360
|
+
Every relaybus error has a stable, machine-readable `code` next to its `name`. Codes are
|
|
361
|
+
never renamed or reused once released, so branch on them in alerts and retry rules. The
|
|
362
|
+
`lastError` of a dead or retried delivery and the `error` of observer events carry the
|
|
363
|
+
`code` of relaybus errors; other errors keep `name`, `message` and `stack` only.
|
|
364
|
+
|
|
365
|
+
| Code | Error class | Package |
|
|
366
|
+
| ------------------------------------------ | ------------------------------- | -------------------- |
|
|
367
|
+
| `RELAYBUS_ERROR` | `RelayBusError` | `@relaybus/core` |
|
|
368
|
+
| `RELAYBUS_CONFIGURATION` | `ConfigurationError` | `@relaybus/core` |
|
|
369
|
+
| `RELAYBUS_TRANSACTION_REQUIRED` | `TransactionRequiredError` | `@relaybus/core` |
|
|
370
|
+
| `RELAYBUS_TRANSACTION_TIMEOUT` | `TransactionTimeoutError` | `@relaybus/core` |
|
|
371
|
+
| `RELAYBUS_NON_RETRYABLE` | `NonRetryableError` | `@relaybus/core` |
|
|
372
|
+
| `RELAYBUS_EVENT_VALIDATION` | `EventValidationError` | `@relaybus/core` |
|
|
373
|
+
| `RELAYBUS_INVALID_ENVELOPE` | `InvalidEnvelopeError` | `@relaybus/core` |
|
|
374
|
+
| `RELAYBUS_LEASE_LOST` | `LeaseLostError` | `@relaybus/core` |
|
|
375
|
+
| `RELAYBUS_HANDLER_TIMEOUT` | `HandlerTimeoutError` | `@relaybus/core` |
|
|
376
|
+
| `RELAYBUS_DISPATCHER_STOPPED` | `DispatcherStoppedError` | `@relaybus/core` |
|
|
377
|
+
| `RELAYBUS_ATTEMPTS_EXHAUSTED` | `AttemptsExhaustedError` | `@relaybus/core` |
|
|
378
|
+
| `RELAYBUS_LIFECYCLE` | `LifecycleError` | `@relaybus/core` |
|
|
379
|
+
| `RELAYBUS_DRAIN_INCOMPLETE` | `DrainIncompleteError` | `@relaybus/core` |
|
|
380
|
+
| `RELAYBUS_KAFKA_INVALID_MESSAGE` | `InvalidKafkaMessageError` | `@relaybus/kafka` |
|
|
381
|
+
| `RELAYBUS_KAFKA_UNENCODABLE_EVENT` | `UnencodableEventError` | `@relaybus/kafka` |
|
|
382
|
+
| `RELAYBUS_KAFKA_PUBLISH_TIMEOUT` | `PublishTimeoutError` | `@relaybus/kafka` |
|
|
383
|
+
| `RELAYBUS_KAFKA_HANDLER_FAILED` | `HandlerFailedError` | `@relaybus/kafka` |
|
|
384
|
+
| `RELAYBUS_KAFKA_TRANSPORT_CLOSED` | `TransportClosedError` | `@relaybus/kafka` |
|
|
385
|
+
| `RELAYBUS_KAFKA_GROUP_JOIN_TIMEOUT` | `GroupJoinTimeoutError` | `@relaybus/kafka` |
|
|
386
|
+
| `RELAYBUS_POSTGRES_RECONCILE_FAILED` | `ReconcileFailedError` | `@relaybus/postgres` |
|
|
387
|
+
| `RELAYBUS_RABBITMQ_UNAVAILABLE` | `RabbitMqUnavailableError` | `@relaybus/rabbitmq` |
|
|
388
|
+
| `RELAYBUS_RABBITMQ_PUBLISH_REJECTED` | `RabbitMqPublishRejectedError` | `@relaybus/rabbitmq` |
|
|
389
|
+
| `RELAYBUS_RABBITMQ_UNROUTABLE_MESSAGE` | `UnroutableMessageError` | `@relaybus/rabbitmq` |
|
|
390
|
+
| `RELAYBUS_RABBITMQ_MALFORMED_MESSAGE` | `MalformedMessageError` | `@relaybus/rabbitmq` |
|
|
391
|
+
| `RELAYBUS_RABBITMQ_UNSERIALIZABLE_PAYLOAD` | `UnserializablePayloadError` | `@relaybus/rabbitmq` |
|
|
392
|
+
| `RELAYBUS_RABBITMQ_UNENCODABLE_EVENT` | `RabbitMqUnencodableEventError` | `@relaybus/rabbitmq` |
|
|
393
|
+
| `RELAYBUS_RABBITMQ_AMQP_LIMIT_EXCEEDED` | `AmqpLimitExceededError` | `@relaybus/rabbitmq` |
|
|
394
|
+
|
|
395
|
+
An error class you derive from `RelayBusError` inherits `RELAYBUS_ERROR` unless it sets
|
|
396
|
+
its own `code`. See [Error codes](https://berkcansavur.github.io/relaybus/concepts/error-codes) for what each one means.
|
|
397
|
+
|
|
146
398
|
## Writing a store
|
|
147
399
|
|
|
148
400
|
Implement `EventStore` and run `eventStoreContract` from
|
|
149
401
|
[`@relaybus/testing`](../testing) against a real database. The contract defines
|
|
150
402
|
fencing, ordering, commit-order sequencing, visibility and purge semantics.
|
|
151
403
|
|
|
404
|
+
## When to use @relaybus/core, and when not to
|
|
405
|
+
|
|
406
|
+
Use it directly when you are not on NestJS, or when you write a custom `EventStore` or transport. Do not run it alone in production: add a store (`@relaybus/postgres` or `@relaybus/mongodb`). `InMemoryEventStore` refuses to start when `NODE_ENV=production`.
|
|
407
|
+
|
|
408
|
+
Documentation: <https://berkcansavur.github.io/relaybus/packages/core>
|
|
409
|
+
|
|
410
|
+
### relaybus vs alternatives
|
|
411
|
+
|
|
412
|
+
<!-- relaybus:snippet:comparison -->
|
|
413
|
+
|
|
414
|
+
| | What it is | Survives a restart | Written in your database transaction | Choose it when |
|
|
415
|
+
| -------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------- | ------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
|
|
416
|
+
| **relaybus** | Event bus built on the transactional outbox: PostgreSQL and MongoDB stores, Kafka and RabbitMQ transports, per-handler deliveries | Yes, events live in your database | Yes | Events must never be lost or phantom, and handlers need retries, dead letters and ordering keys |
|
|
417
|
+
| [`@nestjs/event-emitter`](https://docs.nestjs.com/techniques/events) | In-process event emitter built on `eventemitter2` | No persistence is documented | No | Simple in-process notifications where losing an event on restart is acceptable |
|
|
418
|
+
| [`@nestjs/cqrs`](https://docs.nestjs.com/recipes/cqrs) | CommandBus, QueryBus, EventBus and sagas; the docs say sagas and event handlers run in memory | No built-in persistence | No | You want CQRS structure; it can sit on top of an outbox |
|
|
419
|
+
| [`@nestjs/outbox`](https://github.com/nestjs/outbox) | Transactional outbox module from the NestJS organisation: PostgreSQL, MySQL and in-memory stores, relay with leases and retries, dead-letter table, inbox | Yes with the database stores | Yes | You need MySQL, or a NestJS-organisation module; compare features with relaybus before choosing |
|
|
420
|
+
| [BullMQ](https://docs.bullmq.io/) | Job queue on Redis with retries, delayed jobs and flows; at-least-once in the worst case | Yes, in Redis | No, jobs are stored in Redis and cannot join a SQL or MongoDB transaction | Your stack is Redis-based and background jobs matter more than event atomicity |
|
|
421
|
+
| [pg-boss](https://github.com/timgit/pg-boss) | Job queue on PostgreSQL using `SKIP LOCKED`, with retries, dead letter queues, scheduling and pub/sub; jobs can be created in your own transaction | Yes, in PostgreSQL | Yes, through its database adapters | You want a PostgreSQL job queue with scheduling; relaybus is an event bus with per-handler deliveries and transports |
|
|
422
|
+
| Kafka client directly | A distributed log; the idempotent producer and transactions give exactly-once within Kafka | Yes, in Kafka | No, producing to Kafka is a second write | You consume and produce only inside Kafka; add an outbox when a database write must be announced |
|
|
423
|
+
|
|
424
|
+
This table summarises the vendors' own documentation as of October 2026; check their docs for current details.
|
|
425
|
+
Full comparison and sources: <https://berkcansavur.github.io/relaybus/comparison>.
|
|
426
|
+
|
|
427
|
+
<!-- relaybus:end -->
|
|
428
|
+
|
|
152
429
|
## License
|
|
153
430
|
|
|
154
431
|
MIT
|
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/errors.ts
|
|
4
|
+
var RelayBusError = class extends Error {
|
|
5
|
+
// Explicit names survive minification, unlike `new.target.name`.
|
|
6
|
+
name = "RelayBusError";
|
|
7
|
+
/**
|
|
8
|
+
* Stable, machine-readable identifier of the error class, such as `RELAYBUS_LEASE_LOST`.
|
|
9
|
+
* Unlike `name` and `message`, a code is never renamed once released; branch on it in
|
|
10
|
+
* alerts and dashboards. Subclasses declared outside relaybus inherit `RELAYBUS_ERROR`.
|
|
11
|
+
*/
|
|
12
|
+
code = "RELAYBUS_ERROR";
|
|
13
|
+
};
|
|
14
|
+
var ConfigurationError = class extends RelayBusError {
|
|
15
|
+
name = "ConfigurationError";
|
|
16
|
+
code = "RELAYBUS_CONFIGURATION";
|
|
17
|
+
};
|
|
18
|
+
var TransactionRequiredError = class extends RelayBusError {
|
|
19
|
+
name = "TransactionRequiredError";
|
|
20
|
+
code = "RELAYBUS_TRANSACTION_REQUIRED";
|
|
21
|
+
};
|
|
22
|
+
var TransactionTimeoutError = class extends RelayBusError {
|
|
23
|
+
name = "TransactionTimeoutError";
|
|
24
|
+
code = "RELAYBUS_TRANSACTION_TIMEOUT";
|
|
25
|
+
};
|
|
26
|
+
var NonRetryableError = class extends RelayBusError {
|
|
27
|
+
name = "NonRetryableError";
|
|
28
|
+
code = "RELAYBUS_NON_RETRYABLE";
|
|
29
|
+
};
|
|
30
|
+
var EventValidationError = class extends NonRetryableError {
|
|
31
|
+
constructor(message, issues) {
|
|
32
|
+
super(message);
|
|
33
|
+
this.issues = issues;
|
|
34
|
+
}
|
|
35
|
+
issues;
|
|
36
|
+
name = "EventValidationError";
|
|
37
|
+
code = "RELAYBUS_EVENT_VALIDATION";
|
|
38
|
+
};
|
|
39
|
+
var InvalidEnvelopeError = class extends NonRetryableError {
|
|
40
|
+
name = "InvalidEnvelopeError";
|
|
41
|
+
code = "RELAYBUS_INVALID_ENVELOPE";
|
|
42
|
+
};
|
|
43
|
+
var LeaseLostError = class extends RelayBusError {
|
|
44
|
+
name = "LeaseLostError";
|
|
45
|
+
code = "RELAYBUS_LEASE_LOST";
|
|
46
|
+
/**
|
|
47
|
+
* Whether the store said so (a fenced write returned false). A worker that only noticed
|
|
48
|
+
* locally that its lease ran out may still own the delivery in the store and has to give it back.
|
|
49
|
+
*/
|
|
50
|
+
storeConfirmed;
|
|
51
|
+
constructor(message, options = {}) {
|
|
52
|
+
super(message);
|
|
53
|
+
this.storeConfirmed = options.storeConfirmed ?? false;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
var HandlerTimeoutError = class extends RelayBusError {
|
|
57
|
+
name = "HandlerTimeoutError";
|
|
58
|
+
code = "RELAYBUS_HANDLER_TIMEOUT";
|
|
59
|
+
};
|
|
60
|
+
var DispatcherStoppedError = class extends RelayBusError {
|
|
61
|
+
name = "DispatcherStoppedError";
|
|
62
|
+
code = "RELAYBUS_DISPATCHER_STOPPED";
|
|
63
|
+
};
|
|
64
|
+
var AttemptsExhaustedError = class extends RelayBusError {
|
|
65
|
+
name = "AttemptsExhaustedError";
|
|
66
|
+
code = "RELAYBUS_ATTEMPTS_EXHAUSTED";
|
|
67
|
+
};
|
|
68
|
+
var LifecycleError = class extends RelayBusError {
|
|
69
|
+
name = "LifecycleError";
|
|
70
|
+
code = "RELAYBUS_LIFECYCLE";
|
|
71
|
+
};
|
|
72
|
+
var DrainIncompleteError = class extends RelayBusError {
|
|
73
|
+
name = "DrainIncompleteError";
|
|
74
|
+
code = "RELAYBUS_DRAIN_INCOMPLETE";
|
|
75
|
+
};
|
|
76
|
+
function toErrorInfo(error) {
|
|
77
|
+
if (error instanceof RelayBusError) {
|
|
78
|
+
return { name: error.name, message: error.message, stack: error.stack, code: error.code };
|
|
79
|
+
}
|
|
80
|
+
if (error instanceof Error) {
|
|
81
|
+
return { name: error.name, message: error.message, stack: error.stack };
|
|
82
|
+
}
|
|
83
|
+
return { name: "NonError", message: safeStringify(error) };
|
|
84
|
+
}
|
|
85
|
+
function safeStringify(value) {
|
|
86
|
+
if (typeof value === "string") {
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
try {
|
|
90
|
+
return JSON.stringify(value) ?? String(value);
|
|
91
|
+
} catch {
|
|
92
|
+
return String(value);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// src/event.ts
|
|
97
|
+
var EVENT_TYPE_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:/-]{0,254}$/;
|
|
98
|
+
function assertEventType(type) {
|
|
99
|
+
if (!EVENT_TYPE_PATTERN.test(type)) {
|
|
100
|
+
throw new ConfigurationError(
|
|
101
|
+
`Event type '${type}' must be 1-255 characters of letters, digits, '.', '_', ':', '/' or '-', starting with a letter or digit`
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function defineEvent(type, options) {
|
|
106
|
+
assertEventType(type);
|
|
107
|
+
const schema = options?.schema;
|
|
108
|
+
const key = options?.key;
|
|
109
|
+
return class DefinedEvent {
|
|
110
|
+
constructor(payload) {
|
|
111
|
+
this.payload = payload;
|
|
112
|
+
}
|
|
113
|
+
payload;
|
|
114
|
+
static type = type;
|
|
115
|
+
static schema = schema;
|
|
116
|
+
static key = key;
|
|
117
|
+
type = type;
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
async function validatePayload(type, schema, payload) {
|
|
121
|
+
if (schema === void 0) {
|
|
122
|
+
return payload;
|
|
123
|
+
}
|
|
124
|
+
const result = await schema["~standard"].validate(payload);
|
|
125
|
+
if (result.issues !== void 0) {
|
|
126
|
+
const issues = result.issues.map((issue) => ({
|
|
127
|
+
message: issue.message,
|
|
128
|
+
path: issue.path?.map((segment) => typeof segment === "object" ? segment.key : segment)
|
|
129
|
+
}));
|
|
130
|
+
throw new EventValidationError(
|
|
131
|
+
`Payload of '${type}' failed validation: ${issues.map((issue) => issue.message).join("; ")}`,
|
|
132
|
+
issues
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
return result.value;
|
|
136
|
+
}
|
|
137
|
+
function keyFromDefinition(event, payload) {
|
|
138
|
+
const keyOf = event.constructor.key;
|
|
139
|
+
if (keyOf === void 0) {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
const key = keyOf(payload);
|
|
143
|
+
if (typeof key !== "string" || key.length === 0) {
|
|
144
|
+
throw new ConfigurationError(
|
|
145
|
+
`The key function of '${event.type}' must return a non-empty string | returned=${typeof key === "string" ? "''" : typeof key}`
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
return key;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/internal/timers.ts
|
|
152
|
+
function schedule(clock, task) {
|
|
153
|
+
return clock.schedule === void 0 ? scheduleOnPlatform(task) : clock.schedule(task);
|
|
154
|
+
}
|
|
155
|
+
function sleep(clock, milliseconds) {
|
|
156
|
+
return new Promise((resolve) => {
|
|
157
|
+
schedule(clock, { run: resolve, delayMs: milliseconds });
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
function scheduleOnPlatform(task) {
|
|
161
|
+
if (task.repeat === true) {
|
|
162
|
+
const interval = setInterval(task.run, task.delayMs);
|
|
163
|
+
if (task.unref === true) {
|
|
164
|
+
interval.unref();
|
|
165
|
+
}
|
|
166
|
+
return () => clearInterval(interval);
|
|
167
|
+
}
|
|
168
|
+
const timeout = setTimeout(task.run, task.delayMs);
|
|
169
|
+
if (task.unref === true) {
|
|
170
|
+
timeout.unref();
|
|
171
|
+
}
|
|
172
|
+
return () => clearTimeout(timeout);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// src/internal/validation.ts
|
|
176
|
+
function assertPositiveInteger(value, name) {
|
|
177
|
+
if (!Number.isInteger(value) || value < 1) {
|
|
178
|
+
throw new ConfigurationError(`${name} must be a positive integer, received ${String(value)}`);
|
|
179
|
+
}
|
|
180
|
+
return value;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
exports.AttemptsExhaustedError = AttemptsExhaustedError;
|
|
184
|
+
exports.ConfigurationError = ConfigurationError;
|
|
185
|
+
exports.DispatcherStoppedError = DispatcherStoppedError;
|
|
186
|
+
exports.DrainIncompleteError = DrainIncompleteError;
|
|
187
|
+
exports.EventValidationError = EventValidationError;
|
|
188
|
+
exports.HandlerTimeoutError = HandlerTimeoutError;
|
|
189
|
+
exports.InvalidEnvelopeError = InvalidEnvelopeError;
|
|
190
|
+
exports.LeaseLostError = LeaseLostError;
|
|
191
|
+
exports.LifecycleError = LifecycleError;
|
|
192
|
+
exports.NonRetryableError = NonRetryableError;
|
|
193
|
+
exports.RelayBusError = RelayBusError;
|
|
194
|
+
exports.TransactionRequiredError = TransactionRequiredError;
|
|
195
|
+
exports.TransactionTimeoutError = TransactionTimeoutError;
|
|
196
|
+
exports.assertEventType = assertEventType;
|
|
197
|
+
exports.assertPositiveInteger = assertPositiveInteger;
|
|
198
|
+
exports.defineEvent = defineEvent;
|
|
199
|
+
exports.keyFromDefinition = keyFromDefinition;
|
|
200
|
+
exports.schedule = schedule;
|
|
201
|
+
exports.sleep = sleep;
|
|
202
|
+
exports.toErrorInfo = toErrorInfo;
|
|
203
|
+
exports.validatePayload = validatePayload;
|
|
204
|
+
//# sourceMappingURL=chunk-3OHMEMZA.cjs.map
|
|
205
|
+
//# sourceMappingURL=chunk-3OHMEMZA.cjs.map
|