@btravstack/core 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/LICENSE +21 -0
- package/README.md +150 -0
- package/dist/index.cjs +580 -0
- package/dist/index.d.cts +458 -0
- package/dist/index.d.mts +458 -0
- package/dist/index.mjs +567 -0
- package/package.json +75 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Benoit TRAVERS
|
|
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
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# @btravstack/core
|
|
2
|
+
|
|
3
|
+
> The application kernel: boot a [`@btravstack/di`](../di) module into a
|
|
4
|
+
> running process, and stop it again without losing work โ one lifecycle state
|
|
5
|
+
> machine, one unit-of-work registry, one `Runtime` contract. It knows nothing
|
|
6
|
+
> about HTTP, AMQP or Temporal, never throws, and never calls `process.exit`.
|
|
7
|
+
|
|
8
|
+
๐ **[Documentation](https://btravstack.github.io/btravstack/)** ยท
|
|
9
|
+
[Get Started](https://btravstack.github.io/btravstack/tutorial/getting-started) ยท
|
|
10
|
+
[Reference](https://btravstack.github.io/btravstack/reference/core/start) ยท
|
|
11
|
+
[API Reference](https://btravstack.github.io/btravstack/api/core/)
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
pnpm add @btravstack/core @btravstack/config @btravstack/di unthrown
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`@btravstack/config`, `@btravstack/di` and `unthrown` are **peer
|
|
18
|
+
dependencies** โ install all four. The kernel itself depends on `node:`
|
|
19
|
+
builtins only. Node `>=20`.
|
|
20
|
+
|
|
21
|
+
## A worked example
|
|
22
|
+
|
|
23
|
+
An application is a di module. A **runtime** is a service of that module,
|
|
24
|
+
provided on a port declared over `RuntimePort`; `start` finds it there, builds
|
|
25
|
+
the graph, and drives what it finds. The transport starters
|
|
26
|
+
([`@btravstack/http-server`](../http-server), [`@btravstack/temporal-worker`](../temporal-worker),
|
|
27
|
+
[`@btravstack/amqp-worker`](../amqp-worker)) provide real runtimes; this one is a timer, so
|
|
28
|
+
the sample stays self-contained:
|
|
29
|
+
|
|
30
|
+
```ts
|
|
31
|
+
import { Module, Port, Provider } from "@btravstack/di";
|
|
32
|
+
import {
|
|
33
|
+
RuntimePort,
|
|
34
|
+
runMain,
|
|
35
|
+
start,
|
|
36
|
+
type Runtime,
|
|
37
|
+
type Serving,
|
|
38
|
+
} from "@btravstack/core";
|
|
39
|
+
import { Ok, OkAsync } from "unthrown";
|
|
40
|
+
|
|
41
|
+
class Greeter extends Port("Greeter")<{
|
|
42
|
+
readonly greet: (name: string) => string;
|
|
43
|
+
}> {}
|
|
44
|
+
|
|
45
|
+
const AppModule = Module("App")({
|
|
46
|
+
provides: [
|
|
47
|
+
Provider(Greeter)({ value: { greet: (name: string) => `hello, ${name}` } }),
|
|
48
|
+
],
|
|
49
|
+
exports: [Greeter],
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// A runtime owns the transport; the kernel owns the lifecycle. This one is a
|
|
53
|
+
// timer, so the sample stays self-contained โ no published runtime models a
|
|
54
|
+
// timer, and `@btravstack/http-server` would pull in a real dependency this
|
|
55
|
+
// sample doesn't need.
|
|
56
|
+
const ticker: Runtime<typeof Greeter> = {
|
|
57
|
+
name: "ticker",
|
|
58
|
+
resolves: [Greeter],
|
|
59
|
+
start: (host) => {
|
|
60
|
+
const timer = setInterval(() => {
|
|
61
|
+
// Every piece of work goes through `host.run`: that is what makes it
|
|
62
|
+
// count towards the drain, and what gives it an `AbortSignal`.
|
|
63
|
+
//
|
|
64
|
+
// The unit's `Result` is the runtime's to map โ the kernel hands it back
|
|
65
|
+
// and stays out of it. A timer has nowhere to return one, so it observes
|
|
66
|
+
// it instead; dropping it would hide the work's `Err` *and* a `Defect`.
|
|
67
|
+
void host
|
|
68
|
+
.run({ kind: "tick", id: `${Date.now()}` }, (ctx, signal) =>
|
|
69
|
+
signal.aborted ? Ok("") : Ok(ctx.get(Greeter).greet("world")),
|
|
70
|
+
)
|
|
71
|
+
.tapFailure((failure) => {
|
|
72
|
+
process.stderr.write(`${JSON.stringify({ tick: failure.tag })}\n`);
|
|
73
|
+
});
|
|
74
|
+
}, 1_000);
|
|
75
|
+
|
|
76
|
+
const serving: Serving = {
|
|
77
|
+
// Stop accepting new work. In-flight units are the kernel's business.
|
|
78
|
+
drain: () => {
|
|
79
|
+
clearInterval(timer);
|
|
80
|
+
return OkAsync();
|
|
81
|
+
},
|
|
82
|
+
stop: () => OkAsync(),
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
return OkAsync(serving);
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// A runtime is a service the module provides, on a port declared over
|
|
90
|
+
// `RuntimePort` โ `start` finds it by that port in the module's exports. The
|
|
91
|
+
// composition root is what differs between an `api`, a `worker` and a
|
|
92
|
+
// `consumer` process; the application module is the same in all three.
|
|
93
|
+
class Ticker extends RuntimePort<Runtime<typeof Greeter>> {}
|
|
94
|
+
|
|
95
|
+
const TickerApp = Module("TickerApp")({
|
|
96
|
+
imports: [AppModule],
|
|
97
|
+
provides: [Provider(Ticker)({ value: ticker })],
|
|
98
|
+
exports: [Greeter, Ticker],
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
await runMain(TickerApp);
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
`runMain` is the front door: it boots the module, awaits the application's exit
|
|
105
|
+
and turns the outcome into a process exit code โ one call, and the whole of a
|
|
106
|
+
`main.ts`. Underneath it is `start`, which returns immediately with a
|
|
107
|
+
`RunningApp` and decides nothing about the process. The runtime's declared
|
|
108
|
+
`needs` are checked against the module's exports **at compile time** โ a
|
|
109
|
+
`TickerApp` that does not export `Greeter` is a type error at the call, and so
|
|
110
|
+
is a module that exports no runtime port at all.
|
|
111
|
+
|
|
112
|
+
## What you get
|
|
113
|
+
|
|
114
|
+
- **A drain that survives Kubernetes.** On SIGTERM: readiness flips false, the
|
|
115
|
+
kernel waits `preDrainDelayMs` (default `5_000`) before telling the runtime
|
|
116
|
+
to stop accepting โ endpoint removal is eventually consistent โ then
|
|
117
|
+
in-flight work gets `drainTimeoutMs` (default `20_000`); whatever is still
|
|
118
|
+
open is aborted and reported `abandoned`. A second signal skips the drain.
|
|
119
|
+
- **Probes from the state machine.** `GET /livez` and `GET /readyz` on
|
|
120
|
+
`PROBE_PORT` (default `9000`), up before the graph is built.
|
|
121
|
+
- **Configuration from the environment, typed.** The kernel provides
|
|
122
|
+
[`@btravstack/config`](../config)'s `Env` port to every graph it boots; a
|
|
123
|
+
bad value is a `ConfigInvalid` naming every fault at once, and exit `78`.
|
|
124
|
+
- **Teardown on every path**, with failing finalisers in
|
|
125
|
+
`ExitReport.teardownErrors`, never masking the exit reason.
|
|
126
|
+
- **A per-unit scope no handler manages** โ pass a module as
|
|
127
|
+
`StartOptions.unit` and the kernel forks it around every unit.
|
|
128
|
+
- **An ambient record, not an ambient container** โ `currentUnit()` reads
|
|
129
|
+
`{ unitId, traceId, tenantId, deadline, signal }`; services never travel
|
|
130
|
+
there. `signal` is the very `AbortSignal` the unit's work callback is handed,
|
|
131
|
+
so a runtime whose work is a library's `next()` โ a Temporal activity, an
|
|
132
|
+
AMQP delivery โ can still honour the drain deadline.
|
|
133
|
+
- **Nothing throws.** Every async surface is an
|
|
134
|
+
[`unthrown`](https://github.com/btravstack/unthrown) `AsyncResult`;
|
|
135
|
+
`runMain` sets `process.exitCode` โ `0` clean, `1` a modeled startup error,
|
|
136
|
+
`2` abandoned work or a failed finaliser, `70` a crash, `78` a bad
|
|
137
|
+
environment.
|
|
138
|
+
|
|
139
|
+
Every sample above is compiled by
|
|
140
|
+
[`src/docs-examples.test-d.ts`](./src/docs-examples.test-d.ts). The rest โ
|
|
141
|
+
the `Runtime` contract, the drain, embedding without `runMain` โ is on the
|
|
142
|
+
[documentation site](https://btravstack.github.io/btravstack/reference/core/start).
|
|
143
|
+
Testing what the kernel boots is [`@btravstack/testing`](../testing)'s job โ a
|
|
144
|
+
`bootFixture` for `test.extend`, `tapped` to read services out of a running
|
|
145
|
+
app, an in-memory runtime and a fake clock โ kept out of this package so a
|
|
146
|
+
production bundle never pulls the fakes in.
|
|
147
|
+
|
|
148
|
+
## License
|
|
149
|
+
|
|
150
|
+
[MIT](./LICENSE) ยฉ Benoit TRAVERS
|