@relaybus/core 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +153 -2
- package/dist/index.cjs +1759 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +744 -0
- package/dist/index.d.ts +744 -0
- package/dist/index.js +1732 -0
- package/dist/index.js.map +1 -0
- package/package.json +55 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Berkcan Şavur
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,154 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @relaybus/core
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The relaybus engine: transactional publishing, per-handler deliveries, leased
|
|
4
|
+
dispatching, retries with jitter, dead letters, ordering keys and transport
|
|
5
|
+
routing. Most applications install it together with a store and, for NestJS,
|
|
6
|
+
the module:
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
pnpm add @relaybus/core @relaybus/nestjs @relaybus/postgres pg
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
| Package | Use it for |
|
|
13
|
+
| ------------------------------------------------------------------------ | ------------------------------------------------------------- |
|
|
14
|
+
| [`@relaybus/nestjs`](https://www.npmjs.com/package/@relaybus/nestjs) | `RelayBusModule` and the `@Subscribe` decorator |
|
|
15
|
+
| [`@relaybus/postgres`](https://www.npmjs.com/package/@relaybus/postgres) | PostgreSQL store (pg, Prisma, TypeORM, Knex, Kysely, Drizzle) |
|
|
16
|
+
| [`@relaybus/mongodb`](https://www.npmjs.com/package/@relaybus/mongodb) | MongoDB store (driver or Mongoose sessions) |
|
|
17
|
+
| [`@relaybus/kafka`](https://www.npmjs.com/package/@relaybus/kafka) | Kafka transport between services |
|
|
18
|
+
| [`@relaybus/rabbitmq`](https://www.npmjs.com/package/@relaybus/rabbitmq) | RabbitMQ transport between services |
|
|
19
|
+
| [`@relaybus/testing`](https://www.npmjs.com/package/@relaybus/testing) | Contract suite, fake clock, in-memory transport |
|
|
20
|
+
|
|
21
|
+
## With NestJS
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
// order.events.ts
|
|
25
|
+
export class OrderPlaced extends defineEvent<{ orderId: string }>('order.placed.v1') {}
|
|
26
|
+
|
|
27
|
+
// app.module.ts
|
|
28
|
+
@Module({ imports: [RelayBusModule.forRoot({ store: new PostgresEventStore({ pool }) })] })
|
|
29
|
+
export class AppModule {}
|
|
30
|
+
|
|
31
|
+
// shipping.handlers.ts: consume with a decorator
|
|
32
|
+
@Injectable()
|
|
33
|
+
export class ShippingHandlers {
|
|
34
|
+
@Subscribe(OrderPlaced, { retry: { attempts: 5 } })
|
|
35
|
+
async ship(event: EventEnvelope<{ orderId: string }>) {
|
|
36
|
+
/* runs after the order transaction commits */
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// order.service.ts: produce inside your transaction
|
|
41
|
+
await this.bus.transaction(async () => {
|
|
42
|
+
await this.orders.insert(order);
|
|
43
|
+
await this.bus.publish(new OrderPlaced({ orderId: order.id }), { key: order.id });
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
See the [project README](https://github.com/berkcansavur/relaybus#readme) for
|
|
48
|
+
guarantees and architecture.
|
|
49
|
+
|
|
50
|
+
## Without a framework
|
|
51
|
+
|
|
52
|
+
```ts
|
|
53
|
+
import { InMemoryEventStore, RelayBus, defineEvent } from '@relaybus/core';
|
|
54
|
+
|
|
55
|
+
class OrderPlaced extends defineEvent<{ orderId: string }>('order.placed.v1') {}
|
|
56
|
+
|
|
57
|
+
const bus = new RelayBus({ store: new InMemoryEventStore(), source: 'orders' });
|
|
58
|
+
|
|
59
|
+
bus.subscribe(
|
|
60
|
+
OrderPlaced,
|
|
61
|
+
async (event, context) => {
|
|
62
|
+
console.log(event.payload.orderId, context.attempt);
|
|
63
|
+
},
|
|
64
|
+
{ id: 'orders.log' },
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
await bus.start();
|
|
68
|
+
await bus.transaction(async () => {
|
|
69
|
+
await bus.publish(new OrderPlaced({ orderId: 'o-1' }), { key: 'o-1' });
|
|
70
|
+
});
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Use a database store in production: [`@relaybus/postgres`](../postgres) or
|
|
74
|
+
[`@relaybus/mongodb`](../mongodb). `InMemoryEventStore` refuses to start when
|
|
75
|
+
`NODE_ENV=production` unless `allowInProduction: true`.
|
|
76
|
+
|
|
77
|
+
## `new RelayBus(options)`
|
|
78
|
+
|
|
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
|
+
| `unroutedEvents` | `'warn'` | `'throw'` rejects events without any handler or route. |
|
|
86
|
+
| `retry` | 10 attempts, `1s`, ×2, max `10m`, full jitter | Default handler retry policy. `attempts: Infinity` retries forever. |
|
|
87
|
+
| `handlerTimeout` | `false` | Default handler timeout. |
|
|
88
|
+
| `dispatcher.enabled` | `true` | Run handlers in this process. |
|
|
89
|
+
| `dispatcher.concurrency` | `10` | Handlers running at once. |
|
|
90
|
+
| `dispatcher.batchSize` | concurrency | Deliveries claimed per query. |
|
|
91
|
+
| `dispatcher.pollInterval` | `'1s'` | Idle wait between claims; retries are picked up on this cadence. |
|
|
92
|
+
| `dispatcher.leaseDuration` | `'30s'` | Lease length, renewed every third while a handler runs. |
|
|
93
|
+
| `dispatcher.shutdownTimeout` | `'30s'` | Wait for running handlers on `stop`, then release them. |
|
|
94
|
+
| `retention.completed` | `'7d'` | Delete completed deliveries after this; also the deduplication horizon. |
|
|
95
|
+
| `retention.dead` | `false` | Delete dead letters after this. |
|
|
96
|
+
| `retention.interval` | `'1h'` | Cleanup cadence. |
|
|
97
|
+
| `transports` / `routes` / `subscriptions` | — | Broker integration, see below. |
|
|
98
|
+
| `setupStore` | `true` | Create tables and indexes on `start`. |
|
|
99
|
+
| `observers` | `[]` | Lifecycle hooks for logs, metrics and tracing. |
|
|
100
|
+
| `logger` | `console` | `{ log, warn, error }`. |
|
|
101
|
+
| `clock`, `generateIdentifier`, `random` | system | Replace in tests. |
|
|
102
|
+
|
|
103
|
+
Durations are milliseconds or strings such as `'250ms'`, `'30s'`, `'5m'`, `'1h'`, `'7d'`.
|
|
104
|
+
|
|
105
|
+
## Publishing
|
|
106
|
+
|
|
107
|
+
`publish(event, options)` and `publishAll(events, options)` write into, in
|
|
108
|
+
order: `options.transaction`, the transaction of `bus.transaction()`, then
|
|
109
|
+
`transactionResolver`. Options: `key`, `delay`, `headers`, `correlationId`,
|
|
110
|
+
and `id` on `publish` for idempotent publishing. Register every handler in each
|
|
111
|
+
process that publishes the event: deliveries are created at publish time.
|
|
112
|
+
|
|
113
|
+
## Handlers
|
|
114
|
+
|
|
115
|
+
`subscribe(event, handler, { id, retry, timeout, concurrency, transactional })`.
|
|
116
|
+
The handler receives the envelope and a context with `attempt`, `maxAttempts`,
|
|
117
|
+
`deliveryId`, `signal` and, for transactional handlers, `transaction`.
|
|
118
|
+
|
|
119
|
+
- Throw `NonRetryableError` to dead-letter at once. Schema failures are never retried.
|
|
120
|
+
- `transactional: true` runs the handler in a store transaction that also completes
|
|
121
|
+
the delivery. It must finish within the lease duration or its `timeout`.
|
|
122
|
+
- After a timeout or lease loss the handler's `signal` is aborted; the retry waits
|
|
123
|
+
for the abandoned run to settle so runs never overlap.
|
|
124
|
+
|
|
125
|
+
## Transports
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
new RelayBus({
|
|
129
|
+
store,
|
|
130
|
+
transports: [kafka],
|
|
131
|
+
routes: [{ event: OrderPlaced, transport: 'kafka', destination: 'orders.events' }],
|
|
132
|
+
subscriptions: [{ transport: 'kafka', destinations: ['payments.events'] }],
|
|
133
|
+
});
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Routes are deliveries like any handler: retried until the broker accepts them
|
|
137
|
+
(unlimited attempts by default) with a 30s publish timeout. Subscriptions call
|
|
138
|
+
`bus.receive(envelope)`, which stores the event once by id for local handlers;
|
|
139
|
+
transports acknowledge only after it resolves and dead-letter `InvalidEnvelopeError`.
|
|
140
|
+
|
|
141
|
+
## Operations
|
|
142
|
+
|
|
143
|
+
`listDeliveries(query)`, `requeue(ids)`, `statistics()`, `purgeExpired()`,
|
|
144
|
+
`handlers()`, `runOnce()` and `drain()` (tests), `start()` and `stop()`.
|
|
145
|
+
|
|
146
|
+
## Writing a store
|
|
147
|
+
|
|
148
|
+
Implement `EventStore` and run `eventStoreContract` from
|
|
149
|
+
[`@relaybus/testing`](../testing) against a real database. The contract defines
|
|
150
|
+
fencing, ordering, commit-order sequencing, visibility and purge semantics.
|
|
151
|
+
|
|
152
|
+
## License
|
|
153
|
+
|
|
154
|
+
MIT
|