@bitakit/core 0.1.2 → 0.2.1
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 +44 -207
- package/discovery/index.d.ts +1 -41
- package/discovery/index.mjs +1 -4
- package/dist/actions/action-list.d.ts +4 -0
- package/dist/actions/action-list.js +58 -16
- package/dist/actions/action.d.ts +2 -0
- package/dist/actions/action.js +4 -0
- package/dist/actions/brand.d.ts +21 -0
- package/dist/actions/brand.js +36 -0
- package/dist/actions/core.d.ts +4 -3
- package/dist/actions/core.js +4 -2
- package/dist/actions/execution.d.ts +2 -2
- package/dist/actions/execution.js +61 -9
- package/dist/actions/feedback.d.ts +2 -0
- package/dist/actions/feedback.js +8 -0
- package/dist/actions/groups.js +5 -1
- package/dist/actions/instance-bindings.js +3 -2
- package/dist/actions/interceptors.d.ts +9 -3
- package/dist/actions/interceptors.js +31 -3
- package/dist/actions/navigation.d.ts +2 -1
- package/dist/actions/navigation.js +10 -5
- package/dist/actions/reference.js +10 -5
- package/dist/actions/types.d.ts +9 -1
- package/dist/app/access.d.ts +4 -4
- package/dist/app/access.js +33 -71
- package/dist/app/action-access.d.ts +2 -2
- package/dist/app/action-access.js +6 -4
- package/dist/app/service-error.d.ts +2 -2
- package/dist/app/service-error.js +10 -6
- package/dist/app/types.d.ts +76 -33
- package/dist/config-service.d.ts +30 -6
- package/dist/config-service.js +25 -9
- package/dist/configuration.d.ts +6 -3
- package/dist/configuration.js +8 -6
- package/dist/contracts.d.ts +4 -2
- package/dist/contracts.js +3 -3
- package/dist/contribution-origin.d.ts +4 -1
- package/dist/contribution-origin.js +14 -1
- package/dist/contributions/action.d.ts +26 -0
- package/dist/contributions/action.js +171 -0
- package/dist/contributions/host.d.ts +14 -11
- package/dist/contributions/index.d.ts +1 -26
- package/dist/contributions/index.js +1 -18
- package/dist/conventions.d.ts +8 -4
- package/dist/conventions.js +12 -31
- package/dist/core.d.ts +41 -18
- package/dist/core.js +25 -13
- package/dist/definitions.d.ts +272 -306
- package/dist/definitions.js +28 -56
- package/dist/destinations.d.ts +25 -0
- package/dist/destinations.js +21 -0
- package/dist/discovered.d.ts +52 -21
- package/dist/discovered.js +89 -35
- package/dist/events/define.d.ts +43 -0
- package/dist/events/define.js +17 -0
- package/dist/events/service.d.ts +35 -8
- package/dist/events/service.js +184 -42
- package/dist/events/types.d.ts +29 -9
- package/dist/inspection/inventory.d.ts +86 -0
- package/dist/inspection/inventory.js +132 -0
- package/dist/interceptors/define.d.ts +76 -0
- package/dist/interceptors/define.js +23 -0
- package/dist/interceptors/service-observers.d.ts +6 -3
- package/dist/interceptors/service-observers.js +13 -10
- package/dist/lifecycle/cleanup.d.ts +5 -0
- package/dist/lifecycle/cleanup.js +41 -0
- package/dist/lifecycle/controller.d.ts +82 -0
- package/dist/lifecycle/controller.js +276 -0
- package/dist/lifecycle/reconcile.d.ts +19 -0
- package/dist/lifecycle/reconcile.js +68 -0
- package/dist/lifecycle/state.d.ts +53 -0
- package/dist/lifecycle/state.js +86 -0
- package/dist/names.d.ts +10 -0
- package/dist/names.js +1 -0
- package/dist/pages.d.ts +39 -6
- package/dist/pages.js +93 -11
- package/dist/presentation.d.ts +9 -0
- package/dist/runtime.d.ts +166 -28
- package/dist/runtime.js +1485 -248
- package/dist/schema.d.ts +13 -0
- package/dist/schema.js +38 -0
- package/dist/scope.d.ts +59 -4
- package/dist/scope.js +133 -11
- package/dist/service-key.d.ts +33 -14
- package/dist/service-key.js +21 -5
- package/dist/service-names.d.ts +39 -0
- package/dist/service-names.js +175 -0
- package/dist/services.d.ts +24 -5
- package/dist/services.js +230 -37
- package/dist/settings/access.d.ts +19 -0
- package/dist/settings/access.js +78 -0
- package/dist/settings/define.d.ts +97 -0
- package/dist/settings/define.js +83 -0
- package/dist/settings/storage.d.ts +45 -0
- package/dist/settings/storage.js +102 -0
- package/dist/settings/store.d.ts +74 -0
- package/dist/settings/store.js +345 -0
- package/dist/slots/action-placement.d.ts +47 -0
- package/dist/slots/action-placement.js +1 -0
- package/dist/slots/define.d.ts +52 -0
- package/dist/slots/define.js +12 -0
- package/dist/slots/normalize.d.ts +7 -0
- package/dist/slots/normalize.js +36 -0
- package/dist/slots/runtime.d.ts +43 -0
- package/dist/slots/runtime.js +348 -0
- package/dist/slots/types.d.ts +113 -0
- package/dist/slots/types.js +1 -0
- package/dist/startup-error.d.ts +21 -0
- package/dist/startup-error.js +29 -0
- package/dist/store.d.ts +24 -0
- package/dist/store.js +40 -0
- package/dist/translation.d.ts +9 -4
- package/dist/translation.js +2 -3
- package/examples/todos/actions/create.mjs +1 -3
- package/examples/todos/actions/delete.mjs +3 -10
- package/examples/todos/actions/list.mjs +1 -3
- package/examples/todos/events.mjs +5 -0
- package/examples/todos/plugin.mjs +4 -2
- package/examples/todos/services/tasks.mjs +14 -20
- package/examples/todos.mjs +8 -7
- package/package.json +36 -9
- package/testing/index.d.ts +59 -0
- package/testing/index.mjs +150 -0
- package/assets/bitakit-banner.png +0 -0
- package/discovery/entries.mjs +0 -124
- package/discovery/generate.mjs +0 -321
- package/discovery/host.mjs +0 -12
- package/discovery/modules.mjs +0 -13
- package/discovery/packages.mjs +0 -57
- package/discovery/watch.mjs +0 -23
- package/dist/app/service-aliases.d.ts +0 -4
- package/dist/app/service-aliases.js +0 -14
- package/dist/contributions/command.d.ts +0 -8
- package/dist/contributions/command.js +0 -59
- package/dist/contributions/dialog.d.ts +0 -8
- package/dist/contributions/dialog.js +0 -18
- package/dist/contributions/form.d.ts +0 -10
- package/dist/contributions/form.js +0 -67
- package/dist/contributions/native.d.ts +0 -11
- package/dist/contributions/native.js +0 -87
- package/dist/contributions/shared.d.ts +0 -19
- package/dist/contributions/shared.js +0 -48
- package/dist/contributions/stateful-action.d.ts +0 -29
- package/dist/contributions/stateful-action.js +0 -33
- package/dist/events/action-setup.d.ts +0 -19
- package/dist/events/action-setup.js +0 -24
- package/dist/interceptors/service.d.ts +0 -37
- package/dist/interceptors/service.js +0 -7
- package/dist/replacements.d.ts +0 -22
- package/dist/replacements.js +0 -54
- package/dist/validation.d.ts +0 -2
- package/dist/validation.js +0 -16
- package/docs/auth.md +0 -106
- package/docs/events.md +0 -157
- package/docs/file-contributions.md +0 -174
- package/docs/reference.md +0 -404
- package/docs/todo-tutorial.md +0 -88
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isServiceInterceptor } from './
|
|
1
|
+
import { isServiceInterceptor, matchesTarget } from './define.js';
|
|
2
2
|
/** Observer errors are isolated from business execution, as with Action lifecycle observers. */
|
|
3
3
|
function notify(callback, event) {
|
|
4
4
|
try {
|
|
@@ -10,18 +10,22 @@ function notify(callback, event) {
|
|
|
10
10
|
/* Observation must not change the service outcome. */
|
|
11
11
|
}
|
|
12
12
|
}
|
|
13
|
-
/**
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Wraps service instances whose ID or alias matches an interceptor's `service` target. All
|
|
15
|
+
* container consumers receive the same wrapper; frozen instances are supported.
|
|
16
|
+
*/
|
|
17
|
+
export function serviceObserver(interceptors, namesOf) {
|
|
18
|
+
const entries = interceptors
|
|
19
|
+
.filter(isServiceInterceptor)
|
|
18
20
|
.sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
19
21
|
return (id, instance, active) => {
|
|
20
|
-
|
|
22
|
+
if (!instance || (typeof instance !== 'object' && typeof instance !== 'function'))
|
|
23
|
+
return instance;
|
|
24
|
+
const names = [id, ...namesOf(id)];
|
|
25
|
+
const selected = entries.filter((entry) => names.some((name) => matchesTarget(entry.service, name)));
|
|
21
26
|
if (!selected.length)
|
|
22
27
|
return instance;
|
|
23
28
|
const cache = new Map();
|
|
24
|
-
// A forwarding shell also supports frozen service objects without mutating their methods.
|
|
25
29
|
return new Proxy(Object.create(Object.getPrototypeOf(instance)), {
|
|
26
30
|
has: (_target, key) => key in instance,
|
|
27
31
|
ownKeys: () => Reflect.ownKeys(instance),
|
|
@@ -49,7 +53,7 @@ export function serviceObserver(modules) {
|
|
|
49
53
|
return;
|
|
50
54
|
for (const entry of observers)
|
|
51
55
|
notify(entry[phase], {
|
|
52
|
-
service:
|
|
56
|
+
service: id,
|
|
53
57
|
method: key,
|
|
54
58
|
args: Object.freeze([...args]),
|
|
55
59
|
...extra,
|
|
@@ -65,7 +69,6 @@ export function serviceObserver(modules) {
|
|
|
65
69
|
throw error;
|
|
66
70
|
}
|
|
67
71
|
if (result && typeof result.then === 'function') {
|
|
68
|
-
// Observe settlement without replacing the original Promise or changing sync methods.
|
|
69
72
|
void Promise.resolve(result).then((value) => emit('after', { result: value }), (error) => emit('onError', { error }));
|
|
70
73
|
}
|
|
71
74
|
else
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** A cleanup may finish immediately or return work that its owner must await. */
|
|
2
|
+
export type Cleanup = () => void | Promise<void>;
|
|
3
|
+
/** Runs in order, including after failures; synchronous cleanups still finish synchronously. */
|
|
4
|
+
export declare function cleanupSequence(tasks: readonly Cleanup[], finish: (errors: unknown[]) => void): Promise<void>;
|
|
5
|
+
export declare function cleanupFailure(errors: unknown[], message: string): void;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** Runs in order, including after failures; synchronous cleanups still finish synchronously. */
|
|
2
|
+
export function cleanupSequence(tasks, finish) {
|
|
3
|
+
const errors = [];
|
|
4
|
+
let resolve;
|
|
5
|
+
let reject;
|
|
6
|
+
const completion = new Promise((done, fail) => {
|
|
7
|
+
resolve = done;
|
|
8
|
+
reject = fail;
|
|
9
|
+
});
|
|
10
|
+
let index = 0;
|
|
11
|
+
const next = () => {
|
|
12
|
+
while (index < tasks.length) {
|
|
13
|
+
try {
|
|
14
|
+
const result = tasks[index++]();
|
|
15
|
+
if (result && typeof result.then === 'function') {
|
|
16
|
+
void Promise.resolve(result).then(next, (error) => {
|
|
17
|
+
errors.push(error);
|
|
18
|
+
next();
|
|
19
|
+
});
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
errors.push(error);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
finish(errors);
|
|
29
|
+
resolve();
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
reject(error);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
next();
|
|
36
|
+
return completion;
|
|
37
|
+
}
|
|
38
|
+
export function cleanupFailure(errors, message) {
|
|
39
|
+
if (errors.length)
|
|
40
|
+
throw new AggregateError(errors, message);
|
|
41
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { type ActionList } from '../actions/core.js';
|
|
2
|
+
import type { BitaKitRuntimeConfig, Plugin } from '../definitions.js';
|
|
3
|
+
import { BitaKitRuntime } from '../runtime.js';
|
|
4
|
+
import { type LifecycleEntry, type LifecyclePhase } from './state.js';
|
|
5
|
+
export type FailurePolicy = 'fail-fast' | 'isolate';
|
|
6
|
+
export type StartupFailure = Readonly<{
|
|
7
|
+
plugin: string;
|
|
8
|
+
app: boolean;
|
|
9
|
+
message: string;
|
|
10
|
+
source?: string;
|
|
11
|
+
}>;
|
|
12
|
+
export type RuntimeControllerSnapshot = Readonly<{
|
|
13
|
+
runtime?: BitaKitRuntime;
|
|
14
|
+
phase: LifecyclePhase;
|
|
15
|
+
failures: readonly StartupFailure[];
|
|
16
|
+
blocked: readonly string[];
|
|
17
|
+
plugins: readonly LifecycleEntry[];
|
|
18
|
+
}>;
|
|
19
|
+
export type RuntimeControllerOptions = {
|
|
20
|
+
actions?: ActionList;
|
|
21
|
+
services?: Readonly<Record<string, unknown>>;
|
|
22
|
+
failures?: FailurePolicy;
|
|
23
|
+
};
|
|
24
|
+
export declare function describeStartupFailure(error: unknown, appId?: string): StartupFailure;
|
|
25
|
+
/** IDs and aliases from declarations, including record keys before normalization. */
|
|
26
|
+
export declare function pluginServiceNames(plugin: Plugin): string[];
|
|
27
|
+
export declare function pluginExposesService(plugin: Plugin, name: string): boolean;
|
|
28
|
+
/** Shared dependency closure for headless and React hosts; configured intent is never mutated. */
|
|
29
|
+
export declare function failedPluginClosure(plugins: readonly Plugin[], failures: readonly StartupFailure[]): Set<string>;
|
|
30
|
+
/**
|
|
31
|
+
* Core startup policy around the existing runtime, not incremental plugin replacement.
|
|
32
|
+
* Each retry fully stops its previous composition before constructing/starting another.
|
|
33
|
+
*/
|
|
34
|
+
export declare class RuntimeController {
|
|
35
|
+
readonly config: BitaKitRuntimeConfig;
|
|
36
|
+
private readonly options;
|
|
37
|
+
private readonly actions;
|
|
38
|
+
private readonly appId;
|
|
39
|
+
private failures;
|
|
40
|
+
private off;
|
|
41
|
+
private readonly store;
|
|
42
|
+
private releaseState?;
|
|
43
|
+
private running;
|
|
44
|
+
private generation;
|
|
45
|
+
private stopPromise?;
|
|
46
|
+
private startup;
|
|
47
|
+
private resolveReady;
|
|
48
|
+
private rejectReady;
|
|
49
|
+
get: () => Readonly<{
|
|
50
|
+
runtime?: BitaKitRuntime;
|
|
51
|
+
phase: LifecyclePhase;
|
|
52
|
+
failures: readonly Readonly<{
|
|
53
|
+
plugin: string;
|
|
54
|
+
app: boolean;
|
|
55
|
+
message: string;
|
|
56
|
+
source?: string;
|
|
57
|
+
}>[];
|
|
58
|
+
blocked: readonly string[];
|
|
59
|
+
plugins: readonly Readonly<{
|
|
60
|
+
id: string;
|
|
61
|
+
plugin: string;
|
|
62
|
+
enabled: boolean;
|
|
63
|
+
phase: LifecyclePhase;
|
|
64
|
+
since: number;
|
|
65
|
+
failure?: Readonly<{
|
|
66
|
+
message: string;
|
|
67
|
+
plugin?: string;
|
|
68
|
+
source?: string;
|
|
69
|
+
}>;
|
|
70
|
+
}>[];
|
|
71
|
+
}>;
|
|
72
|
+
subscribe: (listener: () => void) => () => void;
|
|
73
|
+
get ready(): Promise<void>;
|
|
74
|
+
get stopped(): Promise<void>;
|
|
75
|
+
constructor(config: BitaKitRuntimeConfig, options?: RuntimeControllerOptions);
|
|
76
|
+
private publish;
|
|
77
|
+
private isolate;
|
|
78
|
+
private prepare;
|
|
79
|
+
mount(): () => Promise<void>;
|
|
80
|
+
stop: () => Promise<void>;
|
|
81
|
+
}
|
|
82
|
+
export declare function createRuntimeController(config: BitaKitRuntimeConfig, options?: RuntimeControllerOptions): RuntimeController;
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { createActionList } from '../actions/core.js';
|
|
2
|
+
import { BitaKitRuntime } from '../runtime.js';
|
|
3
|
+
import { isBitaKitStartupError } from '../startup-error.js';
|
|
4
|
+
import { createStore } from '../store.js';
|
|
5
|
+
import { resolveBitaKitConfig } from '../configuration.js';
|
|
6
|
+
import { notifyLifecycleObserver } from './state.js';
|
|
7
|
+
export function describeStartupFailure(error, appId = 'app') {
|
|
8
|
+
return isBitaKitStartupError(error)
|
|
9
|
+
? { plugin: error.plugin, app: error.app, message: error.message, source: error.source }
|
|
10
|
+
: { plugin: appId, app: true, message: error instanceof Error ? error.message : String(error) };
|
|
11
|
+
}
|
|
12
|
+
/** IDs and aliases from declarations, including record keys before normalization. */
|
|
13
|
+
export function pluginServiceNames(plugin) {
|
|
14
|
+
const entries = Array.isArray(plugin.services)
|
|
15
|
+
? plugin.services.map((value) => [value.id ?? value.provides, value])
|
|
16
|
+
: Object.entries(plugin.services ?? {}).map(([name, value]) => [value.id ?? `${plugin.id}.${name}`, value]);
|
|
17
|
+
return entries.flatMap(([id, service]) => [id, service.exposeAs, service.provides].filter((name) => !!name));
|
|
18
|
+
}
|
|
19
|
+
export function pluginExposesService(plugin, name) {
|
|
20
|
+
return pluginServiceNames(plugin).some((candidate) => candidate === name || candidate.startsWith(`${name}.`));
|
|
21
|
+
}
|
|
22
|
+
/** Shared dependency closure for headless and React hosts; configured intent is never mutated. */
|
|
23
|
+
export function failedPluginClosure(plugins, failures) {
|
|
24
|
+
const off = new Set(failures.map((failure) => failure.plugin));
|
|
25
|
+
for (let changed = true; changed;) {
|
|
26
|
+
changed = false;
|
|
27
|
+
const unavailable = new Set(plugins.filter((plugin) => off.has(plugin.id)).flatMap(pluginServiceNames));
|
|
28
|
+
const unavailableProviders = new Set(plugins
|
|
29
|
+
.filter((plugin) => off.has(plugin.id))
|
|
30
|
+
.flatMap((plugin) => plugin.ui?.providers?.map((provider) => provider.id) ?? []));
|
|
31
|
+
for (const plugin of plugins) {
|
|
32
|
+
const requirements = [
|
|
33
|
+
...(plugin.dependencies ?? []).map((id) => off.has(id)),
|
|
34
|
+
...Object.values(plugin.services ?? {})
|
|
35
|
+
.flatMap((value) => value.requires ?? [])
|
|
36
|
+
.map((name) => unavailable.has(name)),
|
|
37
|
+
...Object.values(plugin.actions ?? {})
|
|
38
|
+
.flatMap((value) => value.requires ?? [])
|
|
39
|
+
.map((name) => unavailable.has(name)),
|
|
40
|
+
...(plugin.interceptors ?? [])
|
|
41
|
+
.flatMap((value) => value.requires ?? [])
|
|
42
|
+
.map((name) => unavailable.has(name)),
|
|
43
|
+
...(plugin.ui?.providers ?? [])
|
|
44
|
+
.flatMap((value) => value.requires ?? [])
|
|
45
|
+
.map((name) => unavailableProviders.has(name)),
|
|
46
|
+
];
|
|
47
|
+
if (!off.has(plugin.id) && requirements.some(Boolean)) {
|
|
48
|
+
off.add(plugin.id);
|
|
49
|
+
changed = true;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return off;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Core startup policy around the existing runtime, not incremental plugin replacement.
|
|
57
|
+
* Each retry fully stops its previous composition before constructing/starting another.
|
|
58
|
+
*/
|
|
59
|
+
export class RuntimeController {
|
|
60
|
+
config;
|
|
61
|
+
options;
|
|
62
|
+
actions;
|
|
63
|
+
appId;
|
|
64
|
+
failures = [];
|
|
65
|
+
off = new Set();
|
|
66
|
+
store = createStore(Object.freeze({
|
|
67
|
+
phase: 'idle',
|
|
68
|
+
failures: Object.freeze([]),
|
|
69
|
+
blocked: Object.freeze([]),
|
|
70
|
+
plugins: Object.freeze([]),
|
|
71
|
+
}));
|
|
72
|
+
releaseState;
|
|
73
|
+
running = false;
|
|
74
|
+
generation = 0;
|
|
75
|
+
stopPromise;
|
|
76
|
+
startup = Promise.resolve();
|
|
77
|
+
resolveReady = () => { };
|
|
78
|
+
rejectReady = () => { };
|
|
79
|
+
get = this.store.get;
|
|
80
|
+
subscribe = (listener) => this.store.subscribe(() => notifyLifecycleObserver(listener, (error) => console.error('[bitakit] Runtime observer failed.', error)));
|
|
81
|
+
get ready() {
|
|
82
|
+
return this.startup;
|
|
83
|
+
}
|
|
84
|
+
get stopped() {
|
|
85
|
+
return this.stopPromise ?? Promise.resolve();
|
|
86
|
+
}
|
|
87
|
+
constructor(config, options = {}) {
|
|
88
|
+
this.config = config;
|
|
89
|
+
this.options = options;
|
|
90
|
+
this.actions = options.actions ?? createActionList();
|
|
91
|
+
this.appId = config.appId ?? 'app';
|
|
92
|
+
this.prepare();
|
|
93
|
+
}
|
|
94
|
+
publish(runtime, phase) {
|
|
95
|
+
const failed = new Map(this.failures.map((failure) => [failure.plugin, failure]));
|
|
96
|
+
const states = new Map(runtime?.lifecycle.get().plugins.map((entry) => [entry.id, entry]));
|
|
97
|
+
const ids = [this.appId, ...(this.config.plugins ?? []).map((plugin) => plugin.id)];
|
|
98
|
+
const plugins = ids.map((id) => {
|
|
99
|
+
const failure = failed.get(id);
|
|
100
|
+
const phase = failure
|
|
101
|
+
? 'failed'
|
|
102
|
+
: this.off.has(id)
|
|
103
|
+
? 'blocked'
|
|
104
|
+
: (states.get(id)?.phase ?? 'idle');
|
|
105
|
+
const previous = this.store.get().plugins.find((entry) => entry.id === id);
|
|
106
|
+
return Object.freeze({
|
|
107
|
+
id,
|
|
108
|
+
plugin: id,
|
|
109
|
+
enabled: true,
|
|
110
|
+
phase,
|
|
111
|
+
since: states.get(id)?.phase === phase
|
|
112
|
+
? states.get(id).since
|
|
113
|
+
: previous?.phase === phase
|
|
114
|
+
? previous.since
|
|
115
|
+
: Date.now(),
|
|
116
|
+
...(failure ? { failure } : {}),
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
this.store.set(Object.freeze({
|
|
120
|
+
runtime,
|
|
121
|
+
phase,
|
|
122
|
+
plugins: Object.freeze(plugins),
|
|
123
|
+
failures: Object.freeze([...this.failures]),
|
|
124
|
+
blocked: Object.freeze([...this.off].filter((id) => !failed.has(id))),
|
|
125
|
+
}));
|
|
126
|
+
}
|
|
127
|
+
isolate(error) {
|
|
128
|
+
if (this.options.failures !== 'isolate')
|
|
129
|
+
return false;
|
|
130
|
+
const failure = describeStartupFailure(error, this.appId);
|
|
131
|
+
if (failure.app || this.failures.some((entry) => entry.plugin === failure.plugin))
|
|
132
|
+
return false;
|
|
133
|
+
this.failures.push(Object.freeze(failure));
|
|
134
|
+
this.off = failedPluginClosure(this.config.plugins ?? [], this.failures);
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
prepare() {
|
|
138
|
+
this.releaseState?.();
|
|
139
|
+
for (;;) {
|
|
140
|
+
try {
|
|
141
|
+
const active = this.config.plugins?.filter((plugin) => !this.off.has(plugin.id));
|
|
142
|
+
const removedProviders = new Set((this.config.plugins ?? [])
|
|
143
|
+
.filter((plugin) => this.off.has(plugin.id))
|
|
144
|
+
.flatMap((plugin) => plugin.ui?.providers ?? []));
|
|
145
|
+
// UI composition may already project plugin providers into the app list. Remove
|
|
146
|
+
// those same declarations and disabled config overrides when isolating their owner.
|
|
147
|
+
const selected = {
|
|
148
|
+
...this.config,
|
|
149
|
+
plugins: active,
|
|
150
|
+
configOverrides: this.config.configOverrides &&
|
|
151
|
+
Object.fromEntries(Object.entries(this.config.configOverrides).filter(([id]) => !this.off.has(id))),
|
|
152
|
+
ui: this.config.ui && {
|
|
153
|
+
...this.config.ui,
|
|
154
|
+
providers: this.config.ui.providers?.filter((provider) => !removedProviders.has(provider)),
|
|
155
|
+
},
|
|
156
|
+
};
|
|
157
|
+
if (selected.ui?.providers)
|
|
158
|
+
resolveBitaKitConfig(selected, { providers: selected.ui.providers });
|
|
159
|
+
const runtime = new BitaKitRuntime(this.actions, selected, this.options.services);
|
|
160
|
+
this.publish(runtime, 'idle');
|
|
161
|
+
this.releaseState = runtime.lifecycle.subscribe(() => this.publish(runtime, this.get().phase === 'failed' ? 'failed' : runtime.lifecycle.get().phase));
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
if (this.isolate(error))
|
|
166
|
+
continue;
|
|
167
|
+
if (this.options.failures !== 'isolate')
|
|
168
|
+
throw error;
|
|
169
|
+
this.failures.push(Object.freeze(describeStartupFailure(error, this.appId)));
|
|
170
|
+
this.publish(undefined, 'failed');
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
mount() {
|
|
176
|
+
if (this.get().runtime?.lifecycle.get().cleanup === 'failed')
|
|
177
|
+
throw new Error('Cannot restart after failed cleanup. Recover resources before creating another controller.');
|
|
178
|
+
if (this.running || this.get().phase === 'stopping')
|
|
179
|
+
throw new Error('Runtime controller is already mounted or stopping.');
|
|
180
|
+
if (this.generation) {
|
|
181
|
+
this.failures = [];
|
|
182
|
+
this.off.clear();
|
|
183
|
+
this.prepare();
|
|
184
|
+
}
|
|
185
|
+
this.running = true;
|
|
186
|
+
this.stopPromise = undefined;
|
|
187
|
+
const generation = ++this.generation;
|
|
188
|
+
this.startup = new Promise((resolve, reject) => {
|
|
189
|
+
this.resolveReady = resolve;
|
|
190
|
+
this.rejectReady = reject;
|
|
191
|
+
});
|
|
192
|
+
void this.startup.catch(() => { });
|
|
193
|
+
const attempt = () => {
|
|
194
|
+
if (!this.running || generation !== this.generation)
|
|
195
|
+
return;
|
|
196
|
+
const runtime = this.get().runtime;
|
|
197
|
+
if (!runtime) {
|
|
198
|
+
const error = new Error(this.failures.map((failure) => failure.message).join('\n'));
|
|
199
|
+
this.rejectReady(error);
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
const fail = (error) => {
|
|
203
|
+
if (!this.running || generation !== this.generation)
|
|
204
|
+
return;
|
|
205
|
+
if (this.isolate(error)) {
|
|
206
|
+
this.publish(runtime, 'stopping');
|
|
207
|
+
void runtime.stop().then(() => {
|
|
208
|
+
if (!this.running || generation !== this.generation)
|
|
209
|
+
return;
|
|
210
|
+
this.prepare();
|
|
211
|
+
attempt();
|
|
212
|
+
}, fatal);
|
|
213
|
+
}
|
|
214
|
+
else
|
|
215
|
+
fatal(error);
|
|
216
|
+
};
|
|
217
|
+
const fatal = (error) => {
|
|
218
|
+
if (!this.running || generation !== this.generation)
|
|
219
|
+
return;
|
|
220
|
+
const failure = describeStartupFailure(error, this.appId);
|
|
221
|
+
if (!this.failures.some((entry) => entry.message === failure.message))
|
|
222
|
+
this.failures.push(Object.freeze(failure));
|
|
223
|
+
this.publish(runtime, 'failed');
|
|
224
|
+
this.rejectReady(error);
|
|
225
|
+
};
|
|
226
|
+
try {
|
|
227
|
+
runtime.mount();
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
if (this.options.failures !== 'isolate') {
|
|
231
|
+
fatal(error);
|
|
232
|
+
throw error;
|
|
233
|
+
}
|
|
234
|
+
fail(error);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
void runtime.ready.then(() => {
|
|
238
|
+
if (!this.running || generation !== this.generation)
|
|
239
|
+
return;
|
|
240
|
+
this.publish(runtime, 'ready');
|
|
241
|
+
this.resolveReady();
|
|
242
|
+
}, fail);
|
|
243
|
+
};
|
|
244
|
+
attempt();
|
|
245
|
+
let completion;
|
|
246
|
+
return () => (completion ??= generation === this.generation ? this.stop() : Promise.resolve());
|
|
247
|
+
}
|
|
248
|
+
stop = () => {
|
|
249
|
+
if (this.stopPromise)
|
|
250
|
+
return this.stopPromise;
|
|
251
|
+
let resolve;
|
|
252
|
+
let reject;
|
|
253
|
+
this.stopPromise = new Promise((done, fail) => {
|
|
254
|
+
resolve = done;
|
|
255
|
+
reject = fail;
|
|
256
|
+
});
|
|
257
|
+
void this.stopPromise.catch(() => { });
|
|
258
|
+
this.running = false;
|
|
259
|
+
this.rejectReady(new Error('Runtime controller stopped before startup completed.'));
|
|
260
|
+
const runtime = this.get().runtime;
|
|
261
|
+
this.publish(runtime, 'stopping');
|
|
262
|
+
void (runtime?.stop() ?? Promise.resolve()).then(() => {
|
|
263
|
+
this.releaseState?.();
|
|
264
|
+
this.releaseState = undefined;
|
|
265
|
+
this.publish(runtime, 'stopped');
|
|
266
|
+
resolve();
|
|
267
|
+
}, (error) => {
|
|
268
|
+
this.publish(runtime, 'failed');
|
|
269
|
+
reject(error);
|
|
270
|
+
});
|
|
271
|
+
return this.stopPromise;
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
export function createRuntimeController(config, options = {}) {
|
|
275
|
+
return new RuntimeController(config, options);
|
|
276
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Plugin } from '../definitions.js';
|
|
2
|
+
import type { NormalizedPlugin } from '../runtime.js';
|
|
3
|
+
export type PluginUpdateResult = Readonly<{
|
|
4
|
+
status: 'applied' | 'blocked' | 'failed' | 'restart-required';
|
|
5
|
+
revision: number;
|
|
6
|
+
changed: readonly string[];
|
|
7
|
+
affected: readonly string[];
|
|
8
|
+
reasons: readonly string[];
|
|
9
|
+
}>;
|
|
10
|
+
/** Explicit host policy when other enabled plugins require the selected owner. */
|
|
11
|
+
export type PluginDependentOptions = Readonly<{
|
|
12
|
+
dependents: 'stop' | 'refuse';
|
|
13
|
+
}>;
|
|
14
|
+
export type PluginChangeResult = PluginUpdateResult;
|
|
15
|
+
/** Lifecycle edges include required service aliases, not just explicit plugin dependencies. */
|
|
16
|
+
export declare function pluginDependencies(modules: readonly NormalizedPlugin[]): Map<string, Set<string>>;
|
|
17
|
+
export declare function updateClosure(modules: readonly NormalizedPlugin[], changed: readonly string[]): string[];
|
|
18
|
+
/** Conservative compatibility check. No schema/function serialization or guessed code hashes. */
|
|
19
|
+
export declare function updateRestrictions(previous: Plugin, next: Plugin, before: NormalizedPlugin, after: NormalizedPlugin): string[];
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/** Lifecycle edges include required service aliases, not just explicit plugin dependencies. */
|
|
2
|
+
export function pluginDependencies(modules) {
|
|
3
|
+
const owners = new Map();
|
|
4
|
+
for (const module of modules)
|
|
5
|
+
for (const service of module.services)
|
|
6
|
+
for (const name of [service.id, service.exposeAs, service.provides])
|
|
7
|
+
if (name)
|
|
8
|
+
owners.set(name, module.id);
|
|
9
|
+
return new Map(modules.map((module) => [
|
|
10
|
+
module.id,
|
|
11
|
+
new Set([
|
|
12
|
+
...(module.dependencies ?? []),
|
|
13
|
+
...[...module.services, ...module.actions, ...(module.interceptors ?? [])]
|
|
14
|
+
.flatMap((value) => value.requires ?? [])
|
|
15
|
+
.map((name) => owners.get(name))
|
|
16
|
+
.filter((id) => !!id && id !== module.id),
|
|
17
|
+
]),
|
|
18
|
+
]));
|
|
19
|
+
}
|
|
20
|
+
export function updateClosure(modules, changed) {
|
|
21
|
+
const edges = pluginDependencies(modules);
|
|
22
|
+
const affected = new Set(changed);
|
|
23
|
+
for (let added = true; added;) {
|
|
24
|
+
added = false;
|
|
25
|
+
for (const [id, requirements] of edges)
|
|
26
|
+
if (!affected.has(id) && [...requirements].some((name) => affected.has(name))) {
|
|
27
|
+
affected.add(id);
|
|
28
|
+
added = true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
const order = [];
|
|
32
|
+
const remaining = new Set(affected);
|
|
33
|
+
while (remaining.size) {
|
|
34
|
+
const id = [...remaining].find((id) => ![...(edges.get(id) ?? [])].some((name) => remaining.has(name)));
|
|
35
|
+
if (!id)
|
|
36
|
+
throw new Error('Plugin update has a cyclic lifecycle dependency; a full restart is required.');
|
|
37
|
+
order.push(id);
|
|
38
|
+
remaining.delete(id);
|
|
39
|
+
}
|
|
40
|
+
return order;
|
|
41
|
+
}
|
|
42
|
+
const list = (values = []) => JSON.stringify([...values].sort());
|
|
43
|
+
/** Conservative compatibility check. No schema/function serialization or guessed code hashes. */
|
|
44
|
+
export function updateRestrictions(previous, next, before, after) {
|
|
45
|
+
const reasons = [];
|
|
46
|
+
for (const field of [
|
|
47
|
+
'settings',
|
|
48
|
+
'events',
|
|
49
|
+
'config',
|
|
50
|
+
'slots',
|
|
51
|
+
'interceptors',
|
|
52
|
+
'installs',
|
|
53
|
+
'contributions',
|
|
54
|
+
])
|
|
55
|
+
if (previous[field] !== next[field])
|
|
56
|
+
reasons.push(`${previous.id}: ${field} declarations changed.`);
|
|
57
|
+
if (list(previous.dependencies) !== list(next.dependencies))
|
|
58
|
+
reasons.push(`${previous.id}: dependencies changed.`);
|
|
59
|
+
for (const field of ['providers', 'layouts'])
|
|
60
|
+
if (previous.ui?.[field] !== next.ui?.[field])
|
|
61
|
+
reasons.push(`${previous.id}: UI ${field} changed.`);
|
|
62
|
+
const shape = (module) => module.services
|
|
63
|
+
.map((service) => [service.id, service.exposeAs, service.provides, list(service.requires)])
|
|
64
|
+
.sort((a, b) => String(a[0]).localeCompare(String(b[0])));
|
|
65
|
+
if (JSON.stringify(shape(before)) !== JSON.stringify(shape(after)))
|
|
66
|
+
reasons.push(`${previous.id}: service identities or dependencies changed.`);
|
|
67
|
+
return reasons;
|
|
68
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export type LifecyclePhase = 'idle' | 'starting' | 'ready' | 'stopping' | 'stopped' | 'failed' | 'blocked';
|
|
2
|
+
export type LifecycleFailure = Readonly<{
|
|
3
|
+
message: string;
|
|
4
|
+
plugin?: string;
|
|
5
|
+
source?: string;
|
|
6
|
+
}>;
|
|
7
|
+
export type LifecycleEntry = Readonly<{
|
|
8
|
+
id: string;
|
|
9
|
+
plugin: string;
|
|
10
|
+
enabled: boolean;
|
|
11
|
+
phase: LifecyclePhase;
|
|
12
|
+
since: number;
|
|
13
|
+
failure?: LifecycleFailure;
|
|
14
|
+
}>;
|
|
15
|
+
export type LifecycleSnapshot = Readonly<{
|
|
16
|
+
generation: number;
|
|
17
|
+
phase: LifecyclePhase;
|
|
18
|
+
cleanup: 'none' | 'pending' | 'complete' | 'failed';
|
|
19
|
+
operation: Readonly<{
|
|
20
|
+
kind: 'start' | 'stop' | 'update';
|
|
21
|
+
startedAt: number;
|
|
22
|
+
}> | null;
|
|
23
|
+
plugins: readonly LifecycleEntry[];
|
|
24
|
+
services: readonly LifecycleEntry[];
|
|
25
|
+
}>;
|
|
26
|
+
export type LifecycleState = Readonly<{
|
|
27
|
+
get(): LifecycleSnapshot;
|
|
28
|
+
subscribe(listener: (snapshot: LifecycleSnapshot) => void): () => void;
|
|
29
|
+
/** Subscribes before delivering current state, so an already-ready owner is not missed. */
|
|
30
|
+
watch(listener: (snapshot: LifecycleSnapshot) => void): () => void;
|
|
31
|
+
}>;
|
|
32
|
+
/** Observer failures are diagnostics, including callbacks that return rejected promises. */
|
|
33
|
+
export declare function notifyLifecycleObserver(call: () => unknown, report: (error: unknown) => void): void;
|
|
34
|
+
/** Status only; actual services and registrations remain owned by the existing runtime. */
|
|
35
|
+
export declare function createLifecycleState(pluginIds: readonly string[], services: readonly {
|
|
36
|
+
id: string;
|
|
37
|
+
plugin: string;
|
|
38
|
+
}[], publish: (snapshot: LifecycleSnapshot) => void, report: (error: unknown) => void): {
|
|
39
|
+
state: Readonly<{
|
|
40
|
+
get(): LifecycleSnapshot;
|
|
41
|
+
subscribe(listener: (snapshot: LifecycleSnapshot) => void): () => void;
|
|
42
|
+
/** Subscribes before delivering current state, so an already-ready owner is not missed. */
|
|
43
|
+
watch(listener: (snapshot: LifecycleSnapshot) => void): () => void;
|
|
44
|
+
}>;
|
|
45
|
+
update(patch: Partial<LifecycleSnapshot>): void;
|
|
46
|
+
service(id: string, phase: LifecyclePhase, failure?: LifecycleFailure): void;
|
|
47
|
+
plugin(id: string, phase: LifecyclePhase, failure?: LifecycleFailure): void;
|
|
48
|
+
};
|
|
49
|
+
declare module '../events/types.js' {
|
|
50
|
+
interface EventMap {
|
|
51
|
+
'runtime.lifecycle': LifecycleSnapshot;
|
|
52
|
+
}
|
|
53
|
+
}
|