@gobing-ai/ts-infra 0.3.4 → 0.3.6
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 +196 -48
- package/dist/application/index.d.ts +55 -0
- package/dist/application/index.d.ts.map +1 -0
- package/dist/application/index.js +173 -0
- package/dist/application/plugins/builtins.d.ts +64 -0
- package/dist/application/plugins/builtins.d.ts.map +1 -0
- package/dist/application/plugins/builtins.js +146 -0
- package/dist/application/plugins/host.d.ts +61 -0
- package/dist/application/plugins/host.d.ts.map +1 -0
- package/dist/application/plugins/host.js +131 -0
- package/dist/application/plugins/index.d.ts +3 -0
- package/dist/application/plugins/index.d.ts.map +1 -0
- package/dist/application/plugins/index.js +2 -0
- package/dist/application/plugins/types.d.ts +69 -0
- package/dist/application/plugins/types.d.ts.map +1 -0
- package/dist/application/plugins/types.js +10 -0
- package/dist/application/types.d.ts +195 -0
- package/dist/application/types.d.ts.map +1 -0
- package/dist/application/types.js +9 -0
- package/dist/application-node.d.ts +68 -0
- package/dist/application-node.d.ts.map +1 -0
- package/dist/application-node.js +228 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/scheduler/cloudflare.d.ts.map +1 -1
- package/dist/scheduler/cloudflare.js +9 -2
- package/dist/scheduler/factory.d.ts +4 -8
- package/dist/scheduler/factory.d.ts.map +1 -1
- package/dist/scheduler/factory.js +12 -22
- package/dist/scheduler/index.d.ts +1 -1
- package/dist/scheduler/index.d.ts.map +1 -1
- package/dist/scheduler/index.js +1 -1
- package/dist/scheduler/wrap-handler.d.ts +8 -4
- package/dist/scheduler/wrap-handler.d.ts.map +1 -1
- package/dist/scheduler/wrap-handler.js +8 -4
- package/dist/telemetry/index.d.ts +1 -2
- package/dist/telemetry/index.d.ts.map +1 -1
- package/dist/telemetry/index.js +1 -2
- package/dist/telemetry/metrics.d.ts +9 -1
- package/dist/telemetry/metrics.d.ts.map +1 -1
- package/dist/telemetry/metrics.js +22 -1
- package/dist/telemetry/sdk.d.ts +33 -1
- package/dist/telemetry/sdk.d.ts.map +1 -1
- package/dist/telemetry/sdk.js +14 -1
- package/package.json +16 -3
- package/src/application/index.ts +248 -0
- package/src/application/plugins/builtins.ts +178 -0
- package/src/application/plugins/host.ts +143 -0
- package/src/application/plugins/index.ts +3 -0
- package/src/application/plugins/types.ts +86 -0
- package/src/application/types.ts +210 -0
- package/src/application-node.ts +311 -0
- package/src/index.ts +0 -2
- package/src/scheduler/cloudflare.ts +16 -5
- package/src/scheduler/factory.ts +15 -26
- package/src/scheduler/index.ts +1 -1
- package/src/scheduler/wrap-handler.ts +8 -4
- package/src/telemetry/index.ts +9 -2
- package/src/telemetry/metrics.ts +22 -1
- package/src/telemetry/sdk.ts +51 -2
- package/dist/telemetry/config.d.ts +0 -41
- package/dist/telemetry/config.d.ts.map +0 -1
- package/dist/telemetry/config.js +0 -21
- package/src/telemetry/config.ts +0 -59
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bare `PluginHost` — owns an insertion-ordered set of plugins and drives
|
|
3
|
+
* lifecycle fan-out (load → start → stop → unload) with fail-soft semantics
|
|
4
|
+
* for start/stop/unload and fail-fast for load.
|
|
5
|
+
*
|
|
6
|
+
* This is a runtime concern: it needs a logger and event bus, both provided
|
|
7
|
+
* by the application bootstrap. No capabilities, no trust ladder.
|
|
8
|
+
*
|
|
9
|
+
* @module application/plugins
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { EventBus } from '../../event-bus/event-bus';
|
|
13
|
+
import type { EventMap } from '../../event-bus/types';
|
|
14
|
+
import { getLogger, type Logger } from '../../logger';
|
|
15
|
+
import type { Plugin, PluginSummary } from './types';
|
|
16
|
+
|
|
17
|
+
// ── PluginHost ─────────────────────────────────────────────────────────────
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Plugin host: registers plugins in insertion order and drives their lifecycle
|
|
21
|
+
* fan-out (load → start → stop → unload).
|
|
22
|
+
*
|
|
23
|
+
* The host stores `EventBus<EventMap>` (the base event contract) rather than
|
|
24
|
+
* a narrower `TEvents` subtype, because `EventBus` is invariant in its type
|
|
25
|
+
* parameter and plugins only need the base contract.
|
|
26
|
+
*/
|
|
27
|
+
export class PluginHost {
|
|
28
|
+
readonly logger: Logger;
|
|
29
|
+
readonly events: EventBus<EventMap>;
|
|
30
|
+
|
|
31
|
+
private readonly _plugins = new Map<string, Plugin>();
|
|
32
|
+
|
|
33
|
+
constructor(events: EventBus<EventMap>, opts?: { logger?: Logger }) {
|
|
34
|
+
this.events = events;
|
|
35
|
+
this.logger = opts?.logger ?? getLogger('plugin-host');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ── Registration ────────────────────────────────────────────────────
|
|
39
|
+
|
|
40
|
+
/** Register a plugin. Throws on duplicate name. */
|
|
41
|
+
register(plugin: Plugin): void {
|
|
42
|
+
if (this._plugins.has(plugin.name)) {
|
|
43
|
+
throw new Error(`Plugin already registered: ${plugin.name}`);
|
|
44
|
+
}
|
|
45
|
+
this._plugins.set(plugin.name, plugin);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Remove a plugin by name. No-op if absent. */
|
|
49
|
+
unregister(name: string): void {
|
|
50
|
+
this._plugins.delete(name);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Check whether a plugin is registered. */
|
|
54
|
+
has(name: string): boolean {
|
|
55
|
+
return this._plugins.has(name);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** List registered plugins in registration order. */
|
|
59
|
+
list(): readonly PluginSummary[] {
|
|
60
|
+
const result: PluginSummary[] = [];
|
|
61
|
+
for (const p of this._plugins.values()) {
|
|
62
|
+
result.push({ name: p.name, version: p.version });
|
|
63
|
+
}
|
|
64
|
+
return result;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ── Lifecycle fan-out ───────────────────────────────────────────────
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Fail-fast: calls `onLoad` on every plugin in registration order.
|
|
71
|
+
* A throwing hook aborts the bootstrap.
|
|
72
|
+
*/
|
|
73
|
+
async loadAll(): Promise<void> {
|
|
74
|
+
for (const plugin of this._plugins.values()) {
|
|
75
|
+
this.logger.debug(`Loading plugin: ${plugin.name}`, { name: plugin.name });
|
|
76
|
+
await plugin.onLoad(this);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Calls `onStart` on every plugin in registration order.
|
|
82
|
+
* Plugins with `failFast: true` rethrow (aborting boot); others log + skip.
|
|
83
|
+
*/
|
|
84
|
+
async startAll(): Promise<void> {
|
|
85
|
+
for (const plugin of this._plugins.values()) {
|
|
86
|
+
if (!plugin.onStart) continue;
|
|
87
|
+
try {
|
|
88
|
+
this.logger.debug(`Starting plugin: ${plugin.name}`, { name: plugin.name });
|
|
89
|
+
await plugin.onStart(this);
|
|
90
|
+
} catch (err) {
|
|
91
|
+
if (plugin.failFast) {
|
|
92
|
+
throw err;
|
|
93
|
+
}
|
|
94
|
+
this.logger.error(`Plugin start hook failed: ${plugin.name}`, {
|
|
95
|
+
name: plugin.name,
|
|
96
|
+
error: (err as Error).message,
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Fail-soft: calls `onStop` on every plugin in **reverse** registration order.
|
|
104
|
+
* A throwing hook is logged + skipped.
|
|
105
|
+
* `reason` is forwarded to each plugin's `onStop(host, reason?)`.
|
|
106
|
+
*/
|
|
107
|
+
async stopAll(reason?: string): Promise<void> {
|
|
108
|
+
const reversed = [...this._plugins.values()].reverse();
|
|
109
|
+
for (const plugin of reversed) {
|
|
110
|
+
if (!plugin.onStop) continue;
|
|
111
|
+
try {
|
|
112
|
+
this.logger.debug(`Stopping plugin: ${plugin.name}`, { name: plugin.name, reason });
|
|
113
|
+
await plugin.onStop(this, reason);
|
|
114
|
+
} catch (err) {
|
|
115
|
+
this.logger.error(`Plugin stop hook failed: ${plugin.name}`, {
|
|
116
|
+
name: plugin.name,
|
|
117
|
+
error: (err as Error).message,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Fail-soft: calls `onUnload` on every plugin in **reverse** registration order.
|
|
125
|
+
* A throwing hook is logged + skipped.
|
|
126
|
+
* `reason` is forwarded to each plugin's `onUnload(host, reason?)`.
|
|
127
|
+
*/
|
|
128
|
+
async unloadAll(reason?: string): Promise<void> {
|
|
129
|
+
const reversed = [...this._plugins.values()].reverse();
|
|
130
|
+
for (const plugin of reversed) {
|
|
131
|
+
if (!plugin.onUnload) continue;
|
|
132
|
+
try {
|
|
133
|
+
this.logger.debug(`Unloading plugin: ${plugin.name}`, { name: plugin.name, reason });
|
|
134
|
+
await plugin.onUnload(this, reason);
|
|
135
|
+
} catch (err) {
|
|
136
|
+
this.logger.error(`Plugin unload hook failed: ${plugin.name}`, {
|
|
137
|
+
name: plugin.name,
|
|
138
|
+
error: (err as Error).message,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Portable plugin lifecycle contract.
|
|
3
|
+
*
|
|
4
|
+
* The `Plugin` interface is a minimal runtime-neutral lifecycle contract:
|
|
5
|
+
* no capabilities, no trust ladder, no manifest schema — just `onLoad` / `onUnload`
|
|
6
|
+
* and `onStart` / `onStop` hooks. Names are deliberately runtime-neutral so
|
|
7
|
+
* CLI and long-lived server apps share the same semantics.
|
|
8
|
+
*
|
|
9
|
+
* @module application/plugins
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { EventBus } from '../../event-bus/event-bus';
|
|
13
|
+
import type { EventMap } from '../../event-bus/types';
|
|
14
|
+
import type { Logger } from '../../logger';
|
|
15
|
+
|
|
16
|
+
// ── Plugin contract ────────────────────────────────────────────────────────
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Minimal plugin lifecycle contract.
|
|
20
|
+
*
|
|
21
|
+
* All hooks receive the host reference so a plugin can access the runtime
|
|
22
|
+
* logger, event bus, and other plugins.
|
|
23
|
+
*/
|
|
24
|
+
export interface Plugin {
|
|
25
|
+
/** Unique name. Used for dedup and lookup in the host. */
|
|
26
|
+
readonly name: string;
|
|
27
|
+
|
|
28
|
+
/** Semver-compatible version string. Informational only in this cut. */
|
|
29
|
+
readonly version: string;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* When `true`, a throwing `onStart` aborts the bootstrap (fail-fast).
|
|
33
|
+
* When absent/false, a throwing `onStart` is logged and skipped (fail-soft).
|
|
34
|
+
* Has no effect on `loadAll` (always fail-fast) or `stopAll`/`unloadAll`
|
|
35
|
+
* (always fail-soft — teardown is best-effort).
|
|
36
|
+
*/
|
|
37
|
+
readonly failFast?: boolean;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Called during `PluginHost.loadAll()` — fail-fast.
|
|
41
|
+
* A throwing `onLoad` aborts the bootstrap.
|
|
42
|
+
*/
|
|
43
|
+
onLoad(host: PluginHost): void | Promise<void>;
|
|
44
|
+
|
|
45
|
+
/** Called during `PluginHost.unloadAll()` — fail-soft. */
|
|
46
|
+
onUnload?(host: PluginHost, reason?: string): void | Promise<void>;
|
|
47
|
+
|
|
48
|
+
/** Called during `PluginHost.startAll()` — fail-soft. */
|
|
49
|
+
onStart?(host: PluginHost): void | Promise<void>;
|
|
50
|
+
|
|
51
|
+
/** Called during `PluginHost.stopAll()` — fail-soft. */
|
|
52
|
+
onStop?(host: PluginHost, reason?: string): void | Promise<void>;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ── Plugin host public shape ───────────────────────────────────────────────
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Summary view of a registered plugin (no references, just metadata).
|
|
59
|
+
*/
|
|
60
|
+
export interface PluginSummary {
|
|
61
|
+
readonly name: string;
|
|
62
|
+
readonly version: string;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ── PluginHost structural interface ────────────────────────────────────────
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Structural contract for the plugin host.
|
|
69
|
+
*
|
|
70
|
+
* This is the shape exposed on `ApplicationRuntime.pluginHost`. Consumers
|
|
71
|
+
* that need to register/unregister plugins at runtime use this interface.
|
|
72
|
+
*/
|
|
73
|
+
export interface PluginHost {
|
|
74
|
+
readonly logger: Logger;
|
|
75
|
+
readonly events: EventBus<EventMap>;
|
|
76
|
+
|
|
77
|
+
register(plugin: Plugin): void;
|
|
78
|
+
unregister(name: string): void;
|
|
79
|
+
has(name: string): boolean;
|
|
80
|
+
list(): readonly PluginSummary[];
|
|
81
|
+
|
|
82
|
+
loadAll(): Promise<void>;
|
|
83
|
+
startAll(): Promise<void>;
|
|
84
|
+
stopAll(reason?: string): Promise<void>;
|
|
85
|
+
unloadAll(reason?: string): Promise<void>;
|
|
86
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Portable application bootstrap types.
|
|
3
|
+
*
|
|
4
|
+
* These types define the DI contract for `runApplication` — a thin orchestration
|
|
5
|
+
* layer over existing ts-infra primitives. The portable subpath does not import
|
|
6
|
+
* any runtime-specific adapters; everything injectable comes through the options.
|
|
7
|
+
*
|
|
8
|
+
* @module application/types
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { EventBus } from '../event-bus/event-bus';
|
|
12
|
+
import type { BusLifecycleEvents, EventMap } from '../event-bus/types';
|
|
13
|
+
import type { InfraEvents } from '../events';
|
|
14
|
+
import type { Logger, LogLevel } from '../logger';
|
|
15
|
+
import type { SchedulerAdapter } from '../scheduler/types';
|
|
16
|
+
import type { PluginHost } from './plugins/host';
|
|
17
|
+
import type { Plugin } from './plugins/types';
|
|
18
|
+
|
|
19
|
+
// ── Feature flag option groups ────────────────────────────────────────────
|
|
20
|
+
|
|
21
|
+
/** Logging feature flags. */
|
|
22
|
+
export interface LoggingOptions {
|
|
23
|
+
/** Enable logging. Default `true`. */
|
|
24
|
+
enabled?: boolean;
|
|
25
|
+
/** Minimum log level. Default `'info'`. */
|
|
26
|
+
level?: LogLevel;
|
|
27
|
+
/** Enable console output. Default `true`. */
|
|
28
|
+
console?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* File sink writer. The portable bootstrap never opens files — the caller
|
|
31
|
+
* (or Node convenience subpath) provides a writer.
|
|
32
|
+
*/
|
|
33
|
+
fileSink?: (line: string) => void;
|
|
34
|
+
/** JSON Lines format. Default `true`. */
|
|
35
|
+
json?: boolean;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Event bus feature flags. */
|
|
39
|
+
export interface EventsOptions<TEvents extends EventMap = InfraEvents> {
|
|
40
|
+
/** Enable event bus. Default `true`. */
|
|
41
|
+
enabled?: boolean;
|
|
42
|
+
/** Create and attach a lifecycle bus. Default `true`. */
|
|
43
|
+
lifecycle?: boolean;
|
|
44
|
+
/** Attach default observers (log + telemetry). Default `true`. */
|
|
45
|
+
defaultObservers?: boolean;
|
|
46
|
+
/** Pre-built event bus (skips creation when provided). */
|
|
47
|
+
bus?: EventBus<TEvents>;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Telemetry feature flags. */
|
|
51
|
+
export interface TelemetryOptions {
|
|
52
|
+
/** Enable telemetry instrumentation. Default `true`. */
|
|
53
|
+
enabled?: boolean;
|
|
54
|
+
/** Service name for spans. Default `'ts-libs'`. */
|
|
55
|
+
serviceName?: string;
|
|
56
|
+
/** Deployment environment. Default `'development'`. */
|
|
57
|
+
environment?: string;
|
|
58
|
+
/** Capture sanitized SQL in DB spans. Default `false`. */
|
|
59
|
+
dbStatementDebug?: boolean;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Scheduler feature flags. */
|
|
63
|
+
export interface SchedulerOptions {
|
|
64
|
+
/** Enable scheduler. Default `false`. */
|
|
65
|
+
enabled?: boolean;
|
|
66
|
+
/** Injected adapter (skips noop default when provided). */
|
|
67
|
+
adapter?: SchedulerAdapter;
|
|
68
|
+
/** Cron entries to register: `[cron, action][]`. */
|
|
69
|
+
entries?: Array<[string, () => Promise<void>]>;
|
|
70
|
+
/** Start scheduler immediately after registration. Default `true` when enabled. */
|
|
71
|
+
autoStart?: boolean;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// ── Resolved bootstrap config ─────────────────────────────────────────────
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Fully-resolved bootstrap config (all optionals filled with defaults).
|
|
78
|
+
* Constructed internally by `resolveBootstrapConfig`.
|
|
79
|
+
*/
|
|
80
|
+
export interface ApplicationBootstrapConfig {
|
|
81
|
+
readonly logging: Readonly<
|
|
82
|
+
Required<Pick<LoggingOptions, 'enabled' | 'level' | 'console' | 'json'>> & { fileSink?: (line: string) => void }
|
|
83
|
+
>;
|
|
84
|
+
readonly events: { enabled: boolean; lifecycle: boolean; defaultObservers: boolean };
|
|
85
|
+
readonly telemetry: { enabled: boolean; serviceName: string; environment: string; dbStatementDebug: boolean };
|
|
86
|
+
readonly scheduler: { enabled: boolean; autoStart: boolean };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// ── Injected services ─────────────────────────────────────────────────────
|
|
90
|
+
|
|
91
|
+
/** Services that may be pre-injected instead of created by the bootstrap. */
|
|
92
|
+
export interface ApplicationServices<TEvents extends EventMap = InfraEvents> {
|
|
93
|
+
logger?: Logger;
|
|
94
|
+
events?: EventBus<TEvents>;
|
|
95
|
+
lifecycleBus?: EventBus<BusLifecycleEvents>;
|
|
96
|
+
db?: DbAdapterLike;
|
|
97
|
+
scheduler?: SchedulerAdapter;
|
|
98
|
+
/** Pre-built plugin host (when injecting instead of constructing). */
|
|
99
|
+
pluginHost?: PluginHost;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Minimal DB adapter shape the bootstrap cares about: `close()` for lifecycle.
|
|
104
|
+
* This avoids importing `ts-db` (an optional peer) from the portable subpath.
|
|
105
|
+
*/
|
|
106
|
+
export interface DbAdapterLike {
|
|
107
|
+
close(): void;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// ── Stop reason ───────────────────────────────────────────────────────────
|
|
111
|
+
|
|
112
|
+
/** Why the application is stopping. */
|
|
113
|
+
export type ApplicationStopReason = 'manual' | 'signal' | 'error' | 'shutdown';
|
|
114
|
+
|
|
115
|
+
// ── Runtime handle ────────────────────────────────────────────────────────
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Runtime handle returned by `runApplication`.
|
|
119
|
+
*
|
|
120
|
+
* Exposes all resolved services and a `stop()` method for graceful shutdown.
|
|
121
|
+
*/
|
|
122
|
+
export interface ApplicationRuntime<TAppConfig = unknown, TEvents extends EventMap = InfraEvents> {
|
|
123
|
+
/** Fully-resolved bootstrap config (feature flags + defaults). */
|
|
124
|
+
readonly config: ApplicationBootstrapConfig;
|
|
125
|
+
/** Caller-provided application config, or `undefined`. */
|
|
126
|
+
readonly appConfig: TAppConfig;
|
|
127
|
+
/** Structured logger. */
|
|
128
|
+
readonly logger: Logger;
|
|
129
|
+
/** Application event bus. */
|
|
130
|
+
readonly events: EventBus<TEvents>;
|
|
131
|
+
/** Lifecycle bus (when enabled). */
|
|
132
|
+
readonly lifecycleBus?: EventBus<BusLifecycleEvents>;
|
|
133
|
+
/** DB adapter (when enabled and injected). */
|
|
134
|
+
readonly db?: DbAdapterLike;
|
|
135
|
+
/** Scheduler adapter (when enabled). */
|
|
136
|
+
readonly scheduler?: SchedulerAdapter;
|
|
137
|
+
readonly pluginHost: PluginHost;
|
|
138
|
+
/** Graceful shutdown. Idempotent — safe to call multiple times. */
|
|
139
|
+
stop(reason?: ApplicationStopReason): Promise<void>;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// ── Options ───────────────────────────────────────────────────────────────
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Options for the portable `runApplication`.
|
|
146
|
+
*/
|
|
147
|
+
export interface ApplicationBootstrapOptions<TAppConfig = unknown, TEvents extends EventMap = InfraEvents> {
|
|
148
|
+
/** Bootstrap feature flags (partial — defaults applied internally). */
|
|
149
|
+
readonly config?: {
|
|
150
|
+
logging?: LoggingOptions;
|
|
151
|
+
events?: EventsOptions<TEvents>;
|
|
152
|
+
telemetry?: TelemetryOptions;
|
|
153
|
+
scheduler?: SchedulerOptions;
|
|
154
|
+
};
|
|
155
|
+
/** Already-resolved application config. Portable — no file reads. */
|
|
156
|
+
readonly appConfig?: TAppConfig;
|
|
157
|
+
/** Pre-built services to inject instead of creating defaults. */
|
|
158
|
+
readonly services?: Partial<ApplicationServices<TEvents>>;
|
|
159
|
+
/** Plugins to register and lifecycle-manage via PluginHost. */
|
|
160
|
+
readonly plugins?: Plugin[];
|
|
161
|
+
/** User callback: application logic. Called after all services are ready. */
|
|
162
|
+
readonly start: (app: ApplicationRuntime<TAppConfig, TEvents>) => Promise<void> | void;
|
|
163
|
+
/** User callback: cleanup before services shut down. */
|
|
164
|
+
readonly stop?: (
|
|
165
|
+
app: ApplicationRuntime<TAppConfig, TEvents>,
|
|
166
|
+
reason: ApplicationStopReason,
|
|
167
|
+
) => Promise<void> | void;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// ── Config validation (Node/Bun convenience subpath) ──────────────────────
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* Validation result matching the structural `safeParse` pattern.
|
|
174
|
+
* Avoids hard-coupling to a specific validation library.
|
|
175
|
+
*/
|
|
176
|
+
export interface ConfigValidationResult<T> {
|
|
177
|
+
readonly success: boolean;
|
|
178
|
+
readonly data?: T;
|
|
179
|
+
readonly errors?: ReadonlyArray<{ path: string; message: string }>;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Config validator: structural `safeParse`-compatible adapter.
|
|
184
|
+
* Accepts any validation library that produces `{ success, data?, errors? }`.
|
|
185
|
+
*/
|
|
186
|
+
export type ApplicationConfigValidator<TAppConfig> =
|
|
187
|
+
| { safeParse(raw: unknown): ConfigValidationResult<TAppConfig> }
|
|
188
|
+
| ((raw: unknown) => TAppConfig)
|
|
189
|
+
| { validate(raw: unknown): TAppConfig }
|
|
190
|
+
| { parse(raw: unknown): TAppConfig };
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Config loader options for the Node/Bun convenience subpath.
|
|
194
|
+
*/
|
|
195
|
+
export interface ApplicationConfigLoader<TAppConfig = unknown> {
|
|
196
|
+
/** Path to the YAML config file. */
|
|
197
|
+
readonly configFile?: string;
|
|
198
|
+
/** YAML section name for bootstrap config. Default `'bootstrap'`. */
|
|
199
|
+
readonly bootstrapSection?: string;
|
|
200
|
+
/** YAML section name for app-specific config. Default: remaining object. */
|
|
201
|
+
readonly appSection?: string;
|
|
202
|
+
/** Caller-provided validator for the app config section. */
|
|
203
|
+
readonly appConfig?: ApplicationConfigValidator<TAppConfig>;
|
|
204
|
+
/** Override values merged after loading. */
|
|
205
|
+
readonly overrides?: Record<string, unknown>;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Re-export event types needed by consumers
|
|
209
|
+
export type { BusLifecycleEvents, EventMap } from '../event-bus/types';
|
|
210
|
+
export type { InfraEvents } from '../events';
|