@decohere/core 0.0.1-dev.20261123.374 → 0.0.1-dev.20261123.401
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 +143 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
# @decohere/core
|
|
2
|
+
|
|
3
|
+
The runtime base every Decohere package builds on. It holds the contracts other packages implement (the container, configuration, the app server) and the app's host: `Builder`, `App` and hosted services. It installs no vendor beyond `reflect-metadata`, and imports `@decohere/scribe` for its own logs.
|
|
4
|
+
|
|
5
|
+
There is no root entry: `import … from "@decohere/core"` fails. Import a subpath.
|
|
6
|
+
|
|
7
|
+
## Subpaths
|
|
8
|
+
|
|
9
|
+
| Subpath | What it holds |
|
|
10
|
+
|---|---|
|
|
11
|
+
| `@decohere/core/hosting` | `Builder`, `App`, `HostedService`, start steps, readiness checks, `DependencyUnavailableError`, and the `AppServer` contract. See [Hosting](#hosting). |
|
|
12
|
+
| `@decohere/core/composition` | The container contract: `ContainerBuilder`, `IContainer`, `LifetimeScope`, `ServiceKey`, `LifetimeScopeToken`, and the `inject` and `optional` parameter decorators. [`@decohere/crucible`](../crucible/README.md) implements it. |
|
|
13
|
+
| `@decohere/core/configuration` | The configuration contract: `IConfigurationService` under `ConfigurationServiceToken`, and `IConfigurationDataSource`. Also the config file names (`configFileNames`, `config.{app}.{env}.yaml`). [`@decohere/config`](../config/README.md) implements it. |
|
|
14
|
+
| `@decohere/core/reflection` | `Constructable`, `Type` and the `reflect-metadata` helpers (`getMeta`, `defineMeta`, `isConstructable`, …). Importing it loads `reflect-metadata`. |
|
|
15
|
+
| `@decohere/core/extensibility` | `Pipeline` and `Step`, `mixwith`, the `extension` decorator, and `FunctionDecorator`, the base of the method decorators. |
|
|
16
|
+
| `@decohere/core/caching` | `@cache`, `@memoize` and `@invalidateCache` on methods, over an `ICache` (`MapCache` by default) that `Caching` manages. |
|
|
17
|
+
| `@decohere/core/system` | `@retry`, `debounce`, `Subject`, `isDevelopmentEnv`, `lowerCamelCase`, and function types (`Func`, `Action`, `Predicate`). |
|
|
18
|
+
| `@decohere/core/collections` | The `collections` namespace: `sumBy`, `mergeBy`, `uniqueObjectsBy`, `findDuplicatesBy`, and a `…ByKey` form of each. |
|
|
19
|
+
| `@decohere/core/io` | The `io` namespace: read (also as JSON), write, copy and delete files, remove a folder, check that a file or folder exists, convert between file URLs and paths. |
|
|
20
|
+
| `@decohere/core/objects` | `merge`, a deep merge. |
|
|
21
|
+
|
|
22
|
+
## Contracts other packages implement
|
|
23
|
+
|
|
24
|
+
- **The container** (`composition`): `ServiceContainerBuilder` and `Container` in [`@decohere/crucible`](../crucible/README.md). `Builder.create` takes one, because core sits below the container that implements it.
|
|
25
|
+
- **Configuration** (`configuration`): `ConfigurationService` in [`@decohere/config`](../config/README.md), added with `config(...)`. Without it, `App` uses its defaults (port 3000).
|
|
26
|
+
- **The app server** (`hosting`): `AppServer`, registered under `AppServerToken`. [`@decohere/http`](../http/README.md)'s `HttpProvider` extends it, and [`@decohere/http-fastify`](../http-fastify/README.md)'s `FastifyProvider` implements it. An app with no server, a worker, registers none.
|
|
27
|
+
|
|
28
|
+
## Hosting
|
|
29
|
+
|
|
30
|
+
An app is built with `Builder` on a container, and plugins add to it:
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { Builder } from "@decohere/core/hosting";
|
|
34
|
+
import { ServiceContainerBuilder } from "@decohere/crucible";
|
|
35
|
+
|
|
36
|
+
const app = await Builder.create(new ServiceContainerBuilder(), { applicationName: "orders" })
|
|
37
|
+
.use(config(YamlDataSource, EnvDataSource))
|
|
38
|
+
.use(http(FastifyProvider, { controllers: [OrderController] }))
|
|
39
|
+
.withServices((services) => services.registerSingleton(OrderService))
|
|
40
|
+
.withHostedService(OrderFeed)
|
|
41
|
+
.build();
|
|
42
|
+
|
|
43
|
+
await app.start();
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### `Builder`
|
|
47
|
+
|
|
48
|
+
- `use(plugin)`: a `BuilderPlugin` is a function that registers what it needs on the builder, such as `http(...)`, `config(...)` or a data provider.
|
|
49
|
+
- `withServices(fn)`: register the app's own services on the container builder.
|
|
50
|
+
- `withHostedService(...types)`: run [hosted services](#hosted-services) for the life of the app. Each is registered as a singleton.
|
|
51
|
+
- `registerStartStep(step, name?)`: work the app does when it starts, before its hosted services, in registration order, such as bringing a database's schema up to date. An app that is only built does none of it.
|
|
52
|
+
- `registerPreBuildStep(step)` and `registerPostBuildStep(step)`: work done while building, before the container is built (with the configuration service and the container builder) and after it (with the container).
|
|
53
|
+
- `build(type?)`: run the service registrations and pre-build steps, build the container, run the post-build steps, and resolve the `App` (or a subclass). Building connects to nothing. `buildAndStart(type?)` builds, then starts.
|
|
54
|
+
|
|
55
|
+
### `App`
|
|
56
|
+
|
|
57
|
+
- `start(options?)`: runs the start steps and hosted services, then starts the `AppServer` if there is one, on `options.port`, else the `api.port` config section (`ApiConfiguration`), else 3000. It returns the server's addresses. Options: `port`, `host`, `shutdownSignals` (default `SIGTERM` and `SIGINT`; `false` to handle them yourself), `shutdownTimeoutMs` (default 10 s, then the process exits with code 1), `hostedServices` (`"before-listen"` or `"after-listen"`) and `startRetry`.
|
|
58
|
+
- `hostedServicesStarted()`: resolves with how long each start step and hosted service took, once each has started or is being retried.
|
|
59
|
+
- `dispose()`: stops the app. Calling it again, or a signal, returns the same shutdown. `await using` works too (`Symbol.asyncDispose`).
|
|
60
|
+
- `services`: the root container, for a tool or a test that builds the app without starting it.
|
|
61
|
+
- `addresses`: where the server listens.
|
|
62
|
+
|
|
63
|
+
### Hosted services
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
Work that runs for the life of the app, outside any request: a feed that refreshes on a timer, a poller, a queue consumer. Implement `HostedService` and register it:
|
|
67
|
+
|
|
68
|
+
```ts
|
|
69
|
+
@injectable()
|
|
70
|
+
export class PositionFeed implements HostedService {
|
|
71
|
+
private warm = false;
|
|
72
|
+
private loading?: Promise<void>;
|
|
73
|
+
|
|
74
|
+
public constructor(@inject(PositionBook) private readonly book: PositionBook) {}
|
|
75
|
+
|
|
76
|
+
public start(signal: AbortSignal) {
|
|
77
|
+
// Don't wait here: a first load that takes minutes would keep the HTTP server dark.
|
|
78
|
+
this.loading = this.book.loadAll(signal).then(() => {
|
|
79
|
+
this.warm = true;
|
|
80
|
+
});
|
|
81
|
+
const timer = setInterval(() => void this.book.refreshOpen(signal), 60_000);
|
|
82
|
+
signal.addEventListener("abort", () => clearInterval(timer));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
public async stop() {
|
|
86
|
+
await this.loading; // the signal already told it to stop; wait for it to wind down
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
public async check() {
|
|
90
|
+
if (!this.warm) throw new Error("positions still loading"); // GET /ready answers 503
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
Builder.create(new ServiceContainerBuilder())
|
|
95
|
+
.use(http(FastifyProvider))
|
|
96
|
+
.use(health())
|
|
97
|
+
.use(typeOrm(lp))
|
|
98
|
+
.withHostedService(PositionFeed);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
**Starting.** `app.start()` starts hosted services in registration order, and awaits each `start(signal)`, before the HTTP server listens. So a request never reaches a service that hasn't started. Keep `start()` short: set up what must be ready before traffic, then run long work in the background and return.
|
|
102
|
+
|
|
103
|
+
**Starting after the server listens.** A host with a start budget (Cloud Run's startup check, a probe) counts every service's `start()` against it. `hostedServices: "after-listen"` makes the server listen first, then starts the services in the background, in the same order and one at a time, since registration order carries real dependencies (a seed before the feed that reads it):
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
await app.start({ port, hostedServices: "after-listen" });
|
|
107
|
+
const timings = await app.hostedServicesStarted(); // [{ name: "PositionFeed", ms: 45, started: true }, ...]
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
While they start, `/health` answers 200 and `/ready` answers 503 (`hosted services starting`), so a load balancer that checks readiness sends no traffic to a service that hasn't started.
|
|
111
|
+
|
|
112
|
+
**A start that throws is retried.** The app doesn't stop for it: it logs the error, starts the next service, and starts the failed one again in the background, after 1 s, doubling to 60 s, each wait varied by ±20%. `/ready` answers 503 until it has started (the anonymous probe gives no detail; the log names the service and its error). A dependency that is down at start, a feed or a broker, then costs readiness, not a crash loop. Start steps are retried the same way, and a failed one holds back every step and service after it: a data provider's step connects to the database and migrates it, so nothing starts against a database that isn't there. A `start()` that is retried runs again on the same instance, so it must be safe to call after it threw. Tune the backoff with `startRetry: { initialMs, maxMs }`.
|
|
113
|
+
|
|
114
|
+
`startRetry: false` fails fast instead, for a one-shot job that should exit: a throw stops the services already started and fails `app.start()` (after-listen: it stops the app and rejects `hostedServicesStarted()`; await it in `main.ts`, since left unawaited the rejection is unhandled and ends the process). `startTestApp` starts apps this way, so a test sees the error.
|
|
115
|
+
|
|
116
|
+
`hostedServicesStarted()` works in the default mode too, resolving at once after `start()`. It resolves once each service has started or is being retried: `started: false` marks the second. Each service's time covers building it from the container and its `start()`; the app logs the total at info level and each service at debug.
|
|
117
|
+
|
|
118
|
+
**Warming up.** A service that is started but can't serve yet implements `check()`: it throws until the service is ready, and `GET /ready` (`health()`) answers 503 until then. `/health` is unaffected.
|
|
119
|
+
|
|
120
|
+
**Stopping.** `app.dispose()` runs on `SIGTERM`/`SIGINT` and on every Flux reboot. It:
|
|
121
|
+
|
|
122
|
+
1. aborts `signal`, at once, also while a `start()` is still running. Clear timers and cancel work on it.
|
|
123
|
+
2. lets in-flight HTTP requests finish.
|
|
124
|
+
3. awaits each `stop()` in reverse order. If a `start()` was still running, it waits for it first. Every `stop()` runs even if one throws.
|
|
125
|
+
4. disposes the container, which closes what the services used (DataSources from `typeOrm()`, anything with `Symbol.asyncDispose`).
|
|
126
|
+
|
|
127
|
+
`stop()` is optional: a service with nothing to wait for can rely on the signal, or implement `Symbol.asyncDispose`, which step 4 calls. A hosted service is a singleton, so other services and controllers can inject it by its class. Why this shape: [ADR 0010](../../docs/contributing/adr/0010-hosted-services.md).
|
|
128
|
+
|
|
129
|
+
`GET /health` and `GET /ready` come from `health()` in [`@decohere/http`](../http/README.md). `/ready` runs each `IReadinessCheck` registered under `ReadinessCheckToken`, and the checks the hosted services give it (`HostedServiceHost.readinessChecks()`).
|
|
130
|
+
|
|
131
|
+
### `DependencyUnavailableError`
|
|
132
|
+
|
|
133
|
+
Throw `new DependencyUnavailableError(dependency, reason?)` when something a request needs can't be reached right now, such as a data source still connecting while its start step is retried. `@decohere/http` answers it `503 Service Unavailable` and logs the reason instead of sending it, since it can name hosts.
|
|
134
|
+
|
|
135
|
+
## Working on this package
|
|
136
|
+
|
|
137
|
+
For contributors to Decohere: how the package is laid out and tested. Using it needs nothing below.
|
|
138
|
+
|
|
139
|
+
`src/` has one folder per subpath above, each with an `index.ts` that says what it exports. `hosting/` holds `Builder` (`builder.ts`), `App` (`app.ts`), `HostedServiceHost` (`hosted-service-host.ts`), which starts, retries and stops the steps and services, and the tokens in `const.ts`.
|
|
140
|
+
|
|
141
|
+
There is no canonical suite for core in the [testing table](../../docs/contributing/testing/README.md#canonical-suite-ownership). Its own specs, in `tests/`, cover caching, collections, extensibility and system. Hosting is tested through the packages that use it: `projects/http/tests/app.hosted-services.spec.ts`, `app.shutdown.integration.spec.ts`, `app.spec.ts` and `builder.spec.ts` with a web server, and `projects/crucible/tests/hosting/` without one (start steps, a worker). Specs move beside the code, starting with `core` ([ADR 0027](../../docs/contributing/adr/0027-code-design.md)).
|
|
142
|
+
|
|
143
|
+
Some of core doesn't follow ADR 0027 yet: `collections` and `io` are namespace objects of loose functions, and `isDevelopmentEnv`, `ignoreIf` and the method decorators read `process.env` (`NODE_ENV`, `TEST`).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@decohere/core",
|
|
3
|
-
"version": "0.0.1-dev.20261123.
|
|
3
|
+
"version": "0.0.1-dev.20261123.401",
|
|
4
4
|
"description": "The runtime base every Decohere package builds on: reflection, composition, configuration types, extensibility, collections, objects, caching on a Map, and basic file IO. No side effects on import.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
|
-
"@decohere/scribe": "0.0.1-dev.20261123.
|
|
71
|
+
"@decohere/scribe": "0.0.1-dev.20261123.401",
|
|
72
72
|
"reflect-metadata": "^0.2.2"
|
|
73
73
|
},
|
|
74
74
|
"devDependencies": {}
|