@brud/kernel 0.0.0-stage → 0.0.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/LICENSE +201 -0
- package/README.md +63 -2
- package/dist/index.d.ts +15 -0
- package/dist/index.js +13 -0
- package/dist/serviceModules/events.d.ts +1 -0
- package/dist/serviceModules/events.js +1 -0
- package/dist/serviceModules/exposedApiRegistry.d.ts +1 -0
- package/dist/serviceModules/exposedApiRegistry.js +1 -0
- package/dist/serviceModules/identityRegistry.d.ts +1 -0
- package/dist/serviceModules/identityRegistry.js +1 -0
- package/dist/serviceModules/interception.d.ts +1 -0
- package/dist/serviceModules/interception.js +1 -0
- package/dist/serviceModules/parser.d.ts +1 -0
- package/dist/serviceModules/parser.js +1 -0
- package/dist/serviceModules/router.d.ts +1 -0
- package/dist/serviceModules/router.js +1 -0
- package/dist/serviceModules/wasmCage.d.ts +1 -0
- package/dist/serviceModules/wasmCage.js +1 -0
- package/dist/services/Events/contracts/IEventsService.d.ts +64 -0
- package/dist/services/Events/contracts/IEventsService.js +1 -0
- package/dist/services/Events/contracts/errors.d.ts +31 -0
- package/dist/services/Events/contracts/errors.js +40 -0
- package/dist/services/Events/contracts/index.d.ts +3 -0
- package/dist/services/Events/contracts/index.js +3 -0
- package/dist/services/Events/contracts/tokens.d.ts +2 -0
- package/dist/services/Events/contracts/tokens.js +2 -0
- package/dist/services/Events/descriptor/EventsDescriptor.d.ts +9 -0
- package/dist/services/Events/descriptor/EventsDescriptor.js +56 -0
- package/dist/services/Events/descriptor/tokens.d.ts +1 -0
- package/dist/services/Events/descriptor/tokens.js +1 -0
- package/dist/services/Events/index.d.ts +4 -0
- package/dist/services/Events/index.js +4 -0
- package/dist/services/Events/orchestrator/EventsService.d.ts +51 -0
- package/dist/services/Events/orchestrator/EventsService.js +339 -0
- package/dist/services/Events/orchestrator/index.d.ts +2 -0
- package/dist/services/Events/orchestrator/index.js +2 -0
- package/dist/services/Events/orchestrator/limits.d.ts +22 -0
- package/dist/services/Events/orchestrator/limits.js +33 -0
- package/dist/services/Events/serviceModule.d.ts +2 -0
- package/dist/services/Events/serviceModule.js +5 -0
- package/dist/services/ExposedApiRegistry/contracts/IExposedApiRegistry.d.ts +44 -0
- package/dist/services/ExposedApiRegistry/contracts/IExposedApiRegistry.js +1 -0
- package/dist/services/ExposedApiRegistry/contracts/errors.d.ts +132 -0
- package/dist/services/ExposedApiRegistry/contracts/errors.js +195 -0
- package/dist/services/ExposedApiRegistry/contracts/index.d.ts +9 -0
- package/dist/services/ExposedApiRegistry/contracts/index.js +4 -0
- package/dist/services/ExposedApiRegistry/contracts/policy.d.ts +90 -0
- package/dist/services/ExposedApiRegistry/contracts/policy.js +169 -0
- package/dist/services/ExposedApiRegistry/contracts/registrant.d.ts +37 -0
- package/dist/services/ExposedApiRegistry/contracts/registrant.js +36 -0
- package/dist/services/ExposedApiRegistry/contracts/types.d.ts +112 -0
- package/dist/services/ExposedApiRegistry/contracts/types.js +73 -0
- package/dist/services/ExposedApiRegistry/descriptor/ExposedApiRegistryDescriptor.d.ts +44 -0
- package/dist/services/ExposedApiRegistry/descriptor/ExposedApiRegistryDescriptor.js +58 -0
- package/dist/services/ExposedApiRegistry/descriptor/tokens.d.ts +8 -0
- package/dist/services/ExposedApiRegistry/descriptor/tokens.js +8 -0
- package/dist/services/ExposedApiRegistry/index.d.ts +6 -0
- package/dist/services/ExposedApiRegistry/index.js +5 -0
- package/dist/services/ExposedApiRegistry/orchestrator/ExposedApiRegistryService.d.ts +61 -0
- package/dist/services/ExposedApiRegistry/orchestrator/ExposedApiRegistryService.js +407 -0
- package/dist/services/ExposedApiRegistry/orchestrator/index.d.ts +2 -0
- package/dist/services/ExposedApiRegistry/orchestrator/index.js +1 -0
- package/dist/services/ExposedApiRegistry/serviceModule.d.ts +10 -0
- package/dist/services/ExposedApiRegistry/serviceModule.js +20 -0
- package/dist/services/ExposedApiRegistry/storage/protection.d.ts +21 -0
- package/dist/services/ExposedApiRegistry/storage/protection.js +35 -0
- package/dist/services/ExposedApiRegistry/storage/scopeRecord.d.ts +45 -0
- package/dist/services/ExposedApiRegistry/storage/scopeRecord.js +178 -0
- package/dist/services/IdentityRegistry/contracts/errors.d.ts +88 -0
- package/dist/services/IdentityRegistry/contracts/errors.js +128 -0
- package/dist/services/IdentityRegistry/contracts/index.d.ts +5 -0
- package/dist/services/IdentityRegistry/contracts/index.js +3 -0
- package/dist/services/IdentityRegistry/contracts/types.d.ts +77 -0
- package/dist/services/IdentityRegistry/contracts/types.js +18 -0
- package/dist/services/IdentityRegistry/descriptor/descriptor.d.ts +36 -0
- package/dist/services/IdentityRegistry/descriptor/descriptor.js +44 -0
- package/dist/services/IdentityRegistry/descriptor/tokens.d.ts +8 -0
- package/dist/services/IdentityRegistry/descriptor/tokens.js +8 -0
- package/dist/services/IdentityRegistry/index.d.ts +11 -0
- package/dist/services/IdentityRegistry/index.js +6 -0
- package/dist/services/IdentityRegistry/orchestrator/IdentityRegistryService.d.ts +47 -0
- package/dist/services/IdentityRegistry/orchestrator/IdentityRegistryService.js +240 -0
- package/dist/services/IdentityRegistry/serviceModule.d.ts +10 -0
- package/dist/services/IdentityRegistry/serviceModule.js +24 -0
- package/dist/services/IdentityRegistry/storage/registryDocument.d.ts +55 -0
- package/dist/services/IdentityRegistry/storage/registryDocument.js +235 -0
- package/dist/services/Interception/contracts/IInterceptionService.d.ts +65 -0
- package/dist/services/Interception/contracts/IInterceptionService.js +1 -0
- package/dist/services/Interception/contracts/errors.d.ts +26 -0
- package/dist/services/Interception/contracts/errors.js +35 -0
- package/dist/services/Interception/contracts/index.d.ts +3 -0
- package/dist/services/Interception/contracts/index.js +3 -0
- package/dist/services/Interception/contracts/tokens.d.ts +2 -0
- package/dist/services/Interception/contracts/tokens.js +2 -0
- package/dist/services/Interception/descriptor/InterceptionDescriptor.d.ts +9 -0
- package/dist/services/Interception/descriptor/InterceptionDescriptor.js +49 -0
- package/dist/services/Interception/descriptor/tokens.d.ts +1 -0
- package/dist/services/Interception/descriptor/tokens.js +1 -0
- package/dist/services/Interception/index.d.ts +4 -0
- package/dist/services/Interception/index.js +4 -0
- package/dist/services/Interception/orchestrator/InterceptionService.d.ts +32 -0
- package/dist/services/Interception/orchestrator/InterceptionService.js +180 -0
- package/dist/services/Interception/orchestrator/index.d.ts +1 -0
- package/dist/services/Interception/orchestrator/index.js +1 -0
- package/dist/services/Interception/serviceModule.d.ts +2 -0
- package/dist/services/Interception/serviceModule.js +5 -0
- package/dist/services/Parser/contracts/IParserService.d.ts +21 -0
- package/dist/services/Parser/contracts/IParserService.js +1 -0
- package/dist/services/Parser/contracts/errors.d.ts +25 -0
- package/dist/services/Parser/contracts/errors.js +33 -0
- package/dist/services/Parser/contracts/index.d.ts +4 -0
- package/dist/services/Parser/contracts/index.js +1 -0
- package/dist/services/Parser/contracts/types.d.ts +42 -0
- package/dist/services/Parser/contracts/types.js +1 -0
- package/dist/services/Parser/descriptor/ParserDescriptor.d.ts +23 -0
- package/dist/services/Parser/descriptor/ParserDescriptor.js +50 -0
- package/dist/services/Parser/descriptor/tokens.d.ts +8 -0
- package/dist/services/Parser/descriptor/tokens.js +8 -0
- package/dist/services/Parser/index.d.ts +8 -0
- package/dist/services/Parser/index.js +8 -0
- package/dist/services/Parser/orchestrator/ParserService.d.ts +20 -0
- package/dist/services/Parser/orchestrator/ParserService.js +105 -0
- package/dist/services/Parser/orchestrator/index.d.ts +1 -0
- package/dist/services/Parser/orchestrator/index.js +1 -0
- package/dist/services/Parser/parser/indexSynthesis.d.ts +11 -0
- package/dist/services/Parser/parser/indexSynthesis.js +13 -0
- package/dist/services/Parser/parser/parameters.d.ts +14 -0
- package/dist/services/Parser/parser/parameters.js +89 -0
- package/dist/services/Parser/parser/scanner.d.ts +48 -0
- package/dist/services/Parser/parser/scanner.js +156 -0
- package/dist/services/Parser/parser/syntax.d.ts +30 -0
- package/dist/services/Parser/parser/syntax.js +34 -0
- package/dist/services/Parser/serviceModule.d.ts +2 -0
- package/dist/services/Parser/serviceModule.js +5 -0
- package/dist/services/Router/bootOptions.d.ts +2 -0
- package/dist/services/Router/bootOptions.js +2 -0
- package/dist/services/Router/contracts/IRouterService.d.ts +43 -0
- package/dist/services/Router/contracts/IRouterService.js +1 -0
- package/dist/services/Router/contracts/caller.d.ts +6 -0
- package/dist/services/Router/contracts/caller.js +6 -0
- package/dist/services/Router/contracts/errors.d.ts +56 -0
- package/dist/services/Router/contracts/errors.js +75 -0
- package/dist/services/Router/contracts/index.d.ts +8 -0
- package/dist/services/Router/contracts/index.js +4 -0
- package/dist/services/Router/contracts/kind.d.ts +7 -0
- package/dist/services/Router/contracts/kind.js +10 -0
- package/dist/services/Router/contracts/registrant.d.ts +23 -0
- package/dist/services/Router/contracts/registrant.js +13 -0
- package/dist/services/Router/contracts/types.d.ts +90 -0
- package/dist/services/Router/contracts/types.js +1 -0
- package/dist/services/Router/descriptor/RouterDescriptor.d.ts +32 -0
- package/dist/services/Router/descriptor/RouterDescriptor.js +92 -0
- package/dist/services/Router/descriptor/tokens.d.ts +9 -0
- package/dist/services/Router/descriptor/tokens.js +9 -0
- package/dist/services/Router/index.d.ts +4 -0
- package/dist/services/Router/index.js +4 -0
- package/dist/services/Router/orchestrator/RouterService.d.ts +74 -0
- package/dist/services/Router/orchestrator/RouterService.js +332 -0
- package/dist/services/Router/orchestrator/index.d.ts +2 -0
- package/dist/services/Router/orchestrator/index.js +1 -0
- package/dist/services/Router/serviceModule.d.ts +4 -0
- package/dist/services/Router/serviceModule.js +22 -0
- package/dist/services/WasmCage/index.d.ts +7 -0
- package/dist/services/WasmCage/index.js +4 -0
- package/dist/services/WasmCage/scripts/prepin.d.ts +1 -0
- package/dist/services/WasmCage/scripts/prepin.js +38 -0
- package/dist/services/WasmCage/serviceModule.d.ts +12 -0
- package/dist/services/WasmCage/serviceModule.js +18 -0
- package/dist/services/WasmCage/src/caged.d.ts +104 -0
- package/dist/services/WasmCage/src/caged.js +381 -0
- package/dist/services/WasmCage/src/descriptor.d.ts +47 -0
- package/dist/services/WasmCage/src/descriptor.js +56 -0
- package/dist/services/WasmCage/src/native.d.ts +29 -0
- package/dist/services/WasmCage/src/native.js +66 -0
- package/dist/services/WasmCage/src/nativeDigests.d.ts +9 -0
- package/dist/services/WasmCage/src/nativeDigests.js +15 -0
- package/dist/services/WasmCage/src/package.d.ts +36 -0
- package/dist/services/WasmCage/src/package.js +122 -0
- package/dist/services/shared/canonicalJson.d.ts +29 -0
- package/dist/services/shared/canonicalJson.js +93 -0
- package/dist/services/shared/documentStore.d.ts +16 -0
- package/dist/services/shared/documentStore.js +98 -0
- package/dist/src/bootstrap/bootKernelInternal.d.ts +18 -0
- package/dist/src/bootstrap/bootKernelInternal.js +420 -0
- package/dist/src/bootstrap/bootTypes.d.ts +89 -0
- package/dist/src/bootstrap/bootTypes.js +1 -0
- package/dist/src/bootstrap/config/index.d.ts +3 -0
- package/dist/src/bootstrap/config/index.js +2 -0
- package/dist/src/bootstrap/config/integrity.d.ts +34 -0
- package/dist/src/bootstrap/config/integrity.js +79 -0
- package/dist/src/bootstrap/config/parse.d.ts +15 -0
- package/dist/src/bootstrap/config/parse.js +81 -0
- package/dist/src/bootstrap/config/pathWriteGuard.d.ts +20 -0
- package/dist/src/bootstrap/config/pathWriteGuard.js +349 -0
- package/dist/src/bootstrap/config/types.d.ts +41 -0
- package/dist/src/bootstrap/config/types.js +15 -0
- package/dist/src/bootstrap/hardcore.d.ts +212 -0
- package/dist/src/bootstrap/hardcore.js +312 -0
- package/dist/src/bootstrap/hardcoreServices.d.ts +10 -0
- package/dist/src/bootstrap/hardcoreServices.js +258 -0
- package/dist/src/bootstrap/index.d.ts +8 -0
- package/dist/src/bootstrap/index.js +5 -0
- package/dist/src/bootstrap/packageRequirements.d.ts +26 -0
- package/dist/src/bootstrap/packageRequirements.js +52 -0
- package/dist/src/contracts/accessBroker.d.ts +53 -0
- package/dist/src/contracts/accessBroker.js +1 -0
- package/dist/src/contracts/brudOperation.d.ts +20 -0
- package/dist/src/contracts/brudOperation.js +20 -0
- package/dist/src/contracts/callChain.d.ts +161 -0
- package/dist/src/contracts/callChain.js +19 -0
- package/dist/src/contracts/capability.d.ts +26 -0
- package/dist/src/contracts/capability.js +26 -0
- package/dist/src/contracts/container.d.ts +17 -0
- package/dist/src/contracts/container.js +8 -0
- package/dist/src/contracts/context.d.ts +19 -0
- package/dist/src/contracts/context.js +1 -0
- package/dist/src/contracts/contextContract.d.ts +129 -0
- package/dist/src/contracts/contextContract.js +285 -0
- package/dist/src/contracts/dispatchView.d.ts +21 -0
- package/dist/src/contracts/dispatchView.js +1 -0
- package/dist/src/contracts/handler.d.ts +28 -0
- package/dist/src/contracts/handler.js +1 -0
- package/dist/src/contracts/index.d.ts +25 -0
- package/dist/src/contracts/index.js +8 -0
- package/dist/src/contracts/kernel.d.ts +60 -0
- package/dist/src/contracts/kernel.js +1 -0
- package/dist/src/contracts/lifecycle.d.ts +59 -0
- package/dist/src/contracts/lifecycle.js +1 -0
- package/dist/src/contracts/manifest.d.ts +145 -0
- package/dist/src/contracts/manifest.js +295 -0
- package/dist/src/contracts/pipeline.d.ts +8 -0
- package/dist/src/contracts/pipeline.js +1 -0
- package/dist/src/contracts/policy.d.ts +75 -0
- package/dist/src/contracts/policy.js +77 -0
- package/dist/src/contracts/registry.d.ts +7 -0
- package/dist/src/contracts/registry.js +1 -0
- package/dist/src/contracts/serviceDescriptor.d.ts +67 -0
- package/dist/src/contracts/serviceDescriptor.js +1 -0
- package/dist/src/contracts/serviceToken.d.ts +36 -0
- package/dist/src/contracts/serviceToken.js +16 -0
- package/dist/src/engine/BrudKernel.d.ts +118 -0
- package/dist/src/engine/BrudKernel.js +366 -0
- package/dist/src/engine/ExecutionPipeline.d.ts +13 -0
- package/dist/src/engine/ExecutionPipeline.js +62 -0
- package/dist/src/engine/KernelAccessBroker.d.ts +47 -0
- package/dist/src/engine/KernelAccessBroker.js +107 -0
- package/dist/src/engine/KernelRegistry.d.ts +14 -0
- package/dist/src/engine/KernelRegistry.js +33 -0
- package/dist/src/engine/OperationRegistry.d.ts +9 -0
- package/dist/src/engine/OperationRegistry.js +18 -0
- package/dist/src/engine/ServiceContainer.d.ts +13 -0
- package/dist/src/engine/ServiceContainer.js +53 -0
- package/dist/src/engine/callChain.d.ts +68 -0
- package/dist/src/engine/callChain.js +180 -0
- package/dist/src/engine/dag.d.ts +2 -0
- package/dist/src/engine/dag.js +34 -0
- package/dist/src/engine/errors.d.ts +26 -0
- package/dist/src/engine/errors.js +52 -0
- package/dist/src/engine/index.d.ts +11 -0
- package/dist/src/engine/index.js +8 -0
- package/dist/src/engine/state.d.ts +1 -0
- package/dist/src/engine/state.js +1 -0
- package/dist/src/validation/jsonSchema/compile.d.ts +8 -0
- package/dist/src/validation/jsonSchema/compile.js +386 -0
- package/dist/src/validation/jsonSchema/index.d.ts +3 -0
- package/dist/src/validation/jsonSchema/index.js +2 -0
- package/dist/src/validation/jsonSchema/jsonValue.d.ts +29 -0
- package/dist/src/validation/jsonSchema/jsonValue.js +190 -0
- package/dist/src/validation/jsonSchema/pattern.d.ts +28 -0
- package/dist/src/validation/jsonSchema/pattern.js +155 -0
- package/dist/src/validation/jsonSchema/types.d.ts +71 -0
- package/dist/src/validation/jsonSchema/types.js +15 -0
- package/package.json +59 -4
- package/services/WasmCage/native/brud-wasm-cage.darwin-arm64.node +0 -0
- package/services/WasmCage/native/brud-wasm-cage.darwin-x64.node +0 -0
- package/services/WasmCage/native/brud-wasm-cage.linux-arm64-gnu.node +0 -0
- package/services/WasmCage/native/brud-wasm-cage.linux-x64-gnu.node +0 -0
- package/services/WasmCage/native/brud-wasm-cage.win32-x64-msvc.node +0 -0
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
export class ExecutionPipeline {
|
|
2
|
+
middlewares = [];
|
|
3
|
+
serviceContainer;
|
|
4
|
+
constructor(serviceContainer) {
|
|
5
|
+
this.serviceContainer = serviceContainer;
|
|
6
|
+
}
|
|
7
|
+
use(middleware) {
|
|
8
|
+
this.middlewares.push(middleware);
|
|
9
|
+
}
|
|
10
|
+
async execute(handler, context, input) {
|
|
11
|
+
const start = performance.now();
|
|
12
|
+
const missing = this.verifyCapabilities(handler);
|
|
13
|
+
if (missing) {
|
|
14
|
+
return {
|
|
15
|
+
status: "failed",
|
|
16
|
+
error: `Missing required capability: ${missing.description ?? String(missing)}`,
|
|
17
|
+
durationMs: 0,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
const composed = this.compose(handler);
|
|
22
|
+
const enhancedContext = {
|
|
23
|
+
...context,
|
|
24
|
+
metadata: { ...context.metadata, operationKind: handler.kind },
|
|
25
|
+
};
|
|
26
|
+
const result = await composed(enhancedContext, input);
|
|
27
|
+
result.durationMs = performance.now() - start;
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
catch (err) {
|
|
31
|
+
return {
|
|
32
|
+
status: "failed",
|
|
33
|
+
error: err instanceof Error ? err : String(err),
|
|
34
|
+
durationMs: performance.now() - start,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
verifyCapabilities(handler) {
|
|
39
|
+
if (!handler.requiredCapabilities) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
for (const token of handler.requiredCapabilities) {
|
|
43
|
+
if (!this.serviceContainer.has(token)) {
|
|
44
|
+
return token;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
compose(handler) {
|
|
50
|
+
let runner = (ctx, inp) => handler.execute(ctx, inp);
|
|
51
|
+
for (let i = this.middlewares.length - 1; i >= 0; i--) {
|
|
52
|
+
const mw = this.middlewares[i];
|
|
53
|
+
const next = runner;
|
|
54
|
+
runner = async (context, input) => {
|
|
55
|
+
const nextWrapper = () => next(context, input);
|
|
56
|
+
const mwResult = await mw(context, input, nextWrapper);
|
|
57
|
+
return mwResult;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
return runner;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { IKernelAccessBroker, ServiceInfo } from '../contracts/accessBroker.js';
|
|
2
|
+
import type { ServiceContext } from '../contracts/lifecycle.js';
|
|
3
|
+
import type { KernelCallContext } from '../contracts/callChain.js';
|
|
4
|
+
import type { ServiceToken } from '../contracts/serviceToken.js';
|
|
5
|
+
import type { KernelRegistry } from './KernelRegistry.js';
|
|
6
|
+
import type { KernelLifecycleState } from './state.js';
|
|
7
|
+
export type KernelStateChecker = () => KernelLifecycleState;
|
|
8
|
+
/** The call-chain view the broker binds token access to. */
|
|
9
|
+
export interface KernelAccessAuthority {
|
|
10
|
+
current(): KernelCallContext | undefined;
|
|
11
|
+
tokenIdOf(identity: string): string | undefined;
|
|
12
|
+
callerForToken(tokenId: string): unknown;
|
|
13
|
+
}
|
|
14
|
+
export declare class KernelAccessBroker implements IKernelAccessBroker {
|
|
15
|
+
private readonly registry;
|
|
16
|
+
private readonly readState;
|
|
17
|
+
private paused;
|
|
18
|
+
private readonly authority;
|
|
19
|
+
constructor(registry: KernelRegistry, readState: KernelStateChecker, authority?: KernelAccessAuthority);
|
|
20
|
+
/**
|
|
21
|
+
* Binds the asserted caller id to the active call chain.
|
|
22
|
+
*
|
|
23
|
+
* - Kernel frames (hardcores, kernel work): trusted as before.
|
|
24
|
+
* - Participant frames with a kernel-issued identity: the caller id must be
|
|
25
|
+
* the participant's own token id or kernel identity; anything else is
|
|
26
|
+
* refused. Unidentified frames (no identity registry) are unchanged.
|
|
27
|
+
* - Host frames: may not claim `kernel` or a live participant.
|
|
28
|
+
* - No active chain (host process code outside any chain): unchanged.
|
|
29
|
+
*/
|
|
30
|
+
private assertCallerBinding;
|
|
31
|
+
/**
|
|
32
|
+
* Suspends the lifecycle gate for a bounded boot-window operation.
|
|
33
|
+
*
|
|
34
|
+
* The kernel composition uses this around the boot pinner so any service
|
|
35
|
+
* module's `pinBootPolicies` hook can ingest its boot snapshot before the
|
|
36
|
+
* kernel announces RUNNING. The rule is general: it is available to every
|
|
37
|
+
* participant on the same terms. Access policies, denial lists, and
|
|
38
|
+
* allowlists still apply, so a pause cannot widen anyone's authority; it
|
|
39
|
+
* only removes the "broker is idle" guard.
|
|
40
|
+
*
|
|
41
|
+
* @param operation - Work performed while the gate is suspended.
|
|
42
|
+
*/
|
|
43
|
+
withLifecycleGateSuspended<T>(operation: () => Promise<T>): Promise<T>;
|
|
44
|
+
requestAccess<TService>(caller: ServiceContext, token: ServiceToken<TService>): TService;
|
|
45
|
+
hasService(token: ServiceToken<unknown>): boolean;
|
|
46
|
+
listServices(): readonly ServiceInfo[];
|
|
47
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { AllowlistViolationError, BlacklistedAccessViolationError, CallerIdentityMismatchError, } from '../contracts/policy.js';
|
|
2
|
+
import { LifecycleStateError, ServiceNotFoundError } from './errors.js';
|
|
3
|
+
function matchesCaller(entry, callerId) {
|
|
4
|
+
return typeof entry === 'string' ? entry === callerId : entry.id === callerId;
|
|
5
|
+
}
|
|
6
|
+
export class KernelAccessBroker {
|
|
7
|
+
registry;
|
|
8
|
+
readState;
|
|
9
|
+
paused = false;
|
|
10
|
+
authority;
|
|
11
|
+
constructor(registry, readState, authority) {
|
|
12
|
+
this.registry = registry;
|
|
13
|
+
this.readState = readState;
|
|
14
|
+
this.authority = authority;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Binds the asserted caller id to the active call chain.
|
|
18
|
+
*
|
|
19
|
+
* - Kernel frames (hardcores, kernel work): trusted as before.
|
|
20
|
+
* - Participant frames with a kernel-issued identity: the caller id must be
|
|
21
|
+
* the participant's own token id or kernel identity; anything else is
|
|
22
|
+
* refused. Unidentified frames (no identity registry) are unchanged.
|
|
23
|
+
* - Host frames: may not claim `kernel` or a live participant.
|
|
24
|
+
* - No active chain (host process code outside any chain): unchanged.
|
|
25
|
+
*/
|
|
26
|
+
assertCallerBinding(caller, targetServiceId) {
|
|
27
|
+
const context = this.authority?.current();
|
|
28
|
+
if (context === undefined || context.caller.callerClass === 'kernel') {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (context.caller.callerClass === 'participant') {
|
|
32
|
+
const identity = context.caller.identity;
|
|
33
|
+
// No kernel-issued identity in this composition (no identity registry):
|
|
34
|
+
// there is nothing to bind to, so the asserted id is taken as before.
|
|
35
|
+
if (identity === null) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const bound = this.authority?.tokenIdOf(identity);
|
|
39
|
+
if (bound === undefined || (caller.callerId !== bound && caller.callerId !== identity)) {
|
|
40
|
+
throw new CallerIdentityMismatchError(caller.callerId, targetServiceId, bound);
|
|
41
|
+
}
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
if (caller.callerId === 'kernel' || this.authority?.callerForToken(caller.callerId) !== undefined) {
|
|
45
|
+
throw new CallerIdentityMismatchError(caller.callerId, targetServiceId, undefined);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Suspends the lifecycle gate for a bounded boot-window operation.
|
|
50
|
+
*
|
|
51
|
+
* The kernel composition uses this around the boot pinner so any service
|
|
52
|
+
* module's `pinBootPolicies` hook can ingest its boot snapshot before the
|
|
53
|
+
* kernel announces RUNNING. The rule is general: it is available to every
|
|
54
|
+
* participant on the same terms. Access policies, denial lists, and
|
|
55
|
+
* allowlists still apply, so a pause cannot widen anyone's authority; it
|
|
56
|
+
* only removes the "broker is idle" guard.
|
|
57
|
+
*
|
|
58
|
+
* @param operation - Work performed while the gate is suspended.
|
|
59
|
+
*/
|
|
60
|
+
async withLifecycleGateSuspended(operation) {
|
|
61
|
+
this.paused = true;
|
|
62
|
+
try {
|
|
63
|
+
return await operation();
|
|
64
|
+
}
|
|
65
|
+
finally {
|
|
66
|
+
this.paused = false;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
requestAccess(caller, token) {
|
|
70
|
+
const state = this.readState();
|
|
71
|
+
if (state !== 'RUNNING' && !this.paused) {
|
|
72
|
+
throw new LifecycleStateError('requestAccess', state, 'RUNNING');
|
|
73
|
+
}
|
|
74
|
+
const descriptor = this.registry.getDescriptor(token.id);
|
|
75
|
+
if (descriptor === undefined) {
|
|
76
|
+
throw new ServiceNotFoundError(token.id);
|
|
77
|
+
}
|
|
78
|
+
const instance = this.registry.getInstance(token.id);
|
|
79
|
+
if (instance === undefined) {
|
|
80
|
+
throw new ServiceNotFoundError(token.id);
|
|
81
|
+
}
|
|
82
|
+
this.assertCallerBinding(caller, token.id);
|
|
83
|
+
const policy = descriptor.accessPolicy;
|
|
84
|
+
if (policy?.deniedCallers?.some((entry) => matchesCaller(entry, caller.callerId))) {
|
|
85
|
+
throw new BlacklistedAccessViolationError(caller.callerId, token.id, 'caller is explicitly denied by the service access policy');
|
|
86
|
+
}
|
|
87
|
+
if (policy?.allowedCallers !== undefined
|
|
88
|
+
&& !policy.allowedCallers.some((entry) => matchesCaller(entry, caller.callerId))) {
|
|
89
|
+
throw new AllowlistViolationError(caller.callerId, token.id);
|
|
90
|
+
}
|
|
91
|
+
if (descriptor.project !== undefined) {
|
|
92
|
+
return descriptor.project(instance, caller);
|
|
93
|
+
}
|
|
94
|
+
return instance;
|
|
95
|
+
}
|
|
96
|
+
hasService(token) {
|
|
97
|
+
return this.registry.getDescriptor(token.id) !== undefined;
|
|
98
|
+
}
|
|
99
|
+
listServices() {
|
|
100
|
+
return this.registry.listDescriptors().map((descriptor) => ({
|
|
101
|
+
id: descriptor.token.id,
|
|
102
|
+
description: descriptor.token.description,
|
|
103
|
+
tier: descriptor.tier,
|
|
104
|
+
initialized: this.registry.getInstance(descriptor.token.id) !== undefined,
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ServiceDescriptor } from '../contracts/serviceDescriptor.js';
|
|
2
|
+
export declare class KernelRegistry {
|
|
3
|
+
private readonly descriptors;
|
|
4
|
+
private readonly instances;
|
|
5
|
+
private frozen;
|
|
6
|
+
register<TService, TInternal extends TService = TService>(descriptor: ServiceDescriptor<TService, TInternal>): void;
|
|
7
|
+
getDescriptor(tokenId: string): ServiceDescriptor<unknown> | undefined;
|
|
8
|
+
getInstance(tokenId: string): unknown | undefined;
|
|
9
|
+
setInstance(tokenId: string, instance: unknown): void;
|
|
10
|
+
listDescriptors(): readonly ServiceDescriptor<unknown>[];
|
|
11
|
+
freeze(): void;
|
|
12
|
+
get isFrozen(): boolean;
|
|
13
|
+
clearInstances(): void;
|
|
14
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { LifecycleStateError } from './errors.js';
|
|
2
|
+
export class KernelRegistry {
|
|
3
|
+
descriptors = new Map();
|
|
4
|
+
instances = new Map();
|
|
5
|
+
frozen = false;
|
|
6
|
+
register(descriptor) {
|
|
7
|
+
if (this.frozen) {
|
|
8
|
+
throw new LifecycleStateError('register service after the registry was frozen');
|
|
9
|
+
}
|
|
10
|
+
this.descriptors.set(descriptor.token.id, descriptor);
|
|
11
|
+
}
|
|
12
|
+
getDescriptor(tokenId) {
|
|
13
|
+
return this.descriptors.get(tokenId);
|
|
14
|
+
}
|
|
15
|
+
getInstance(tokenId) {
|
|
16
|
+
return this.instances.get(tokenId);
|
|
17
|
+
}
|
|
18
|
+
setInstance(tokenId, instance) {
|
|
19
|
+
this.instances.set(tokenId, instance);
|
|
20
|
+
}
|
|
21
|
+
listDescriptors() {
|
|
22
|
+
return Array.from(this.descriptors.values());
|
|
23
|
+
}
|
|
24
|
+
freeze() {
|
|
25
|
+
this.frozen = true;
|
|
26
|
+
}
|
|
27
|
+
get isFrozen() {
|
|
28
|
+
return this.frozen;
|
|
29
|
+
}
|
|
30
|
+
clearInstances() {
|
|
31
|
+
this.instances.clear();
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { OperationHandler } from '../contracts/handler.js';
|
|
2
|
+
import type { IOperationRegistry } from '../contracts/registry.js';
|
|
3
|
+
export declare class OperationRegistry implements IOperationRegistry {
|
|
4
|
+
private readonly handlers;
|
|
5
|
+
register(handler: OperationHandler): void;
|
|
6
|
+
get<TInput = unknown, TOutput = unknown>(kind: string): OperationHandler<TInput, TOutput> | undefined;
|
|
7
|
+
has(kind: string): boolean;
|
|
8
|
+
list(): readonly OperationHandler[];
|
|
9
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export class OperationRegistry {
|
|
2
|
+
handlers = new Map();
|
|
3
|
+
register(handler) {
|
|
4
|
+
if (this.handlers.has(handler.kind)) {
|
|
5
|
+
throw new Error(`Operation "${handler.kind}" is already registered`);
|
|
6
|
+
}
|
|
7
|
+
this.handlers.set(handler.kind, handler);
|
|
8
|
+
}
|
|
9
|
+
get(kind) {
|
|
10
|
+
return this.handlers.get(kind);
|
|
11
|
+
}
|
|
12
|
+
has(kind) {
|
|
13
|
+
return this.handlers.has(kind);
|
|
14
|
+
}
|
|
15
|
+
list() {
|
|
16
|
+
return Array.from(this.handlers.values());
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { IServiceContainer, Token } from '../contracts/container.js';
|
|
2
|
+
export declare class ServiceContainer implements IServiceContainer {
|
|
3
|
+
private readonly registry;
|
|
4
|
+
private readonly parent;
|
|
5
|
+
private _isFrozen;
|
|
6
|
+
constructor(parent?: IServiceContainer);
|
|
7
|
+
get isFrozen(): boolean;
|
|
8
|
+
freeze(): void;
|
|
9
|
+
register<T>(token: Token<T>, provider: T | (() => T)): void;
|
|
10
|
+
resolve<T>(token: Token<T>): T;
|
|
11
|
+
has<T>(token: Token<T>): boolean;
|
|
12
|
+
createChildScope(): IServiceContainer;
|
|
13
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export class ServiceContainer {
|
|
2
|
+
registry = new Map();
|
|
3
|
+
parent;
|
|
4
|
+
_isFrozen = false;
|
|
5
|
+
constructor(parent) {
|
|
6
|
+
this.parent = parent ?? null;
|
|
7
|
+
}
|
|
8
|
+
get isFrozen() {
|
|
9
|
+
return this._isFrozen;
|
|
10
|
+
}
|
|
11
|
+
freeze() {
|
|
12
|
+
this._isFrozen = true;
|
|
13
|
+
}
|
|
14
|
+
register(token, provider) {
|
|
15
|
+
if (this._isFrozen) {
|
|
16
|
+
throw new Error("SecurityFault: Cannot register services into a frozen ServiceContainer at runtime.");
|
|
17
|
+
}
|
|
18
|
+
if (typeof provider === 'function' && !('__brand' in provider)) {
|
|
19
|
+
this.registry.set(token, { factory: provider });
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
this.registry.set(token, { value: provider });
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
resolve(token) {
|
|
26
|
+
const entry = this.registry.get(token);
|
|
27
|
+
if (entry) {
|
|
28
|
+
if ('factory' in entry) {
|
|
29
|
+
const instance = entry.factory();
|
|
30
|
+
this.registry.set(token, { value: instance });
|
|
31
|
+
return instance;
|
|
32
|
+
}
|
|
33
|
+
return entry.value;
|
|
34
|
+
}
|
|
35
|
+
if (this.parent) {
|
|
36
|
+
return this.parent.resolve(token);
|
|
37
|
+
}
|
|
38
|
+
throw new Error(`Unresolved token: ${token.description ?? String(token)}`);
|
|
39
|
+
}
|
|
40
|
+
has(token) {
|
|
41
|
+
if (this.registry.has(token)) {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
return this.parent ? this.parent.has(token) : false;
|
|
45
|
+
}
|
|
46
|
+
createChildScope() {
|
|
47
|
+
const child = new ServiceContainer(this);
|
|
48
|
+
if (this._isFrozen) {
|
|
49
|
+
child._isFrozen = true;
|
|
50
|
+
}
|
|
51
|
+
return child;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { type CallOrigin, type KernelCallAuthority, type KernelCallContext, type KernelCallLimits, type KernelCaller, type KernelHostSession, type KernelObservation } from '../contracts/callChain.js';
|
|
2
|
+
/** The kernel itself as a caller. */
|
|
3
|
+
export declare const KERNEL_CALLER: KernelCaller;
|
|
4
|
+
/** A participant the kernel has not assigned an identity to. */
|
|
5
|
+
export declare const UNIDENTIFIED_PARTICIPANT: KernelCaller;
|
|
6
|
+
/** One admitted participant as the call chain records it. */
|
|
7
|
+
export interface ChainParticipant {
|
|
8
|
+
/** Descriptor token id of the participant. */
|
|
9
|
+
readonly tokenId: string;
|
|
10
|
+
/** The participant as a caller. */
|
|
11
|
+
readonly caller: KernelCaller;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* One kernel's call chain.
|
|
15
|
+
*
|
|
16
|
+
* Starts root frames, switches the current caller when the kernel mediates a
|
|
17
|
+
* call into a participant, applies per-identity limits, and keeps the
|
|
18
|
+
* observation stream.
|
|
19
|
+
*/
|
|
20
|
+
export declare class KernelCallChain implements KernelCallAuthority {
|
|
21
|
+
#private;
|
|
22
|
+
/**
|
|
23
|
+
* @param instanceOf - Reads the initialized instance registered for a token.
|
|
24
|
+
* @param limits - Per-identity call and size limits.
|
|
25
|
+
*/
|
|
26
|
+
constructor(instanceOf: (tokenId: string) => unknown, limits?: KernelCallLimits);
|
|
27
|
+
/**
|
|
28
|
+
* Records the admitted participants for this boot.
|
|
29
|
+
*
|
|
30
|
+
* @param participants - Every admitted participant with its kernel caller.
|
|
31
|
+
*/
|
|
32
|
+
admitParticipants(participants: readonly ChainParticipant[]): void;
|
|
33
|
+
/** Forgets every participant; nothing is live after shutdown. */
|
|
34
|
+
clearParticipants(): void;
|
|
35
|
+
/** The kernel caller recorded for a descriptor token, if any. */
|
|
36
|
+
callerForToken(tokenId: string): KernelCaller | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Starts a root frame. The origin is fixed for everything that runs inside.
|
|
39
|
+
*
|
|
40
|
+
* @param origin - Root origin of the new chain.
|
|
41
|
+
* @param caller - Caller that starts the chain.
|
|
42
|
+
* @param operation - Work performed inside the chain.
|
|
43
|
+
*/
|
|
44
|
+
runRoot<T>(origin: CallOrigin, caller: KernelCaller, operation: () => T): T;
|
|
45
|
+
/**
|
|
46
|
+
* Starts a host-origin chain for one host session.
|
|
47
|
+
*
|
|
48
|
+
* @param session - The host session's declaration.
|
|
49
|
+
* @param operation - Work performed inside the chain.
|
|
50
|
+
*/
|
|
51
|
+
runHost<T>(session: KernelHostSession, operation: () => T): T;
|
|
52
|
+
current(): KernelCallContext | undefined;
|
|
53
|
+
runAsOrigin<T>(origin: CallOrigin, identity: string, operation: () => T): T;
|
|
54
|
+
runAs<T>(identity: string, operation: () => T): T;
|
|
55
|
+
/**
|
|
56
|
+
* Token id of the live participant holding `identity`, if any.
|
|
57
|
+
*
|
|
58
|
+
* @param identity - Kernel-issued identity.
|
|
59
|
+
*/
|
|
60
|
+
tokenIdOf(identity: string): string | undefined;
|
|
61
|
+
isLive(identity: string): boolean;
|
|
62
|
+
instanceOf(identity: string): unknown;
|
|
63
|
+
callerForIdentity(identity: string): KernelCaller | undefined;
|
|
64
|
+
admitCall(caller: KernelCaller, requestBytes: number): boolean;
|
|
65
|
+
observe(source: string, event: string, detail: Readonly<Record<string, unknown>>): void;
|
|
66
|
+
/** A snapshot of the observation stream, oldest first. */
|
|
67
|
+
observations(): readonly KernelObservation[];
|
|
68
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import { DEFAULT_KERNEL_CALL_LIMITS, } from '../contracts/callChain.js';
|
|
3
|
+
/**
|
|
4
|
+
* Process-wide carrier of active frames.
|
|
5
|
+
*
|
|
6
|
+
* Several kernels may share one process. Each frame names the chain that owns
|
|
7
|
+
* it, and a chain reads only its own frames.
|
|
8
|
+
*/
|
|
9
|
+
const frames = new AsyncLocalStorage();
|
|
10
|
+
/** Largest number of records the observation stream retains. */
|
|
11
|
+
const OBSERVATION_CAPACITY = 1_000;
|
|
12
|
+
const EMPTY_CAPABILITIES = Object.freeze(new Set());
|
|
13
|
+
/** The kernel itself as a caller. */
|
|
14
|
+
export const KERNEL_CALLER = Object.freeze({
|
|
15
|
+
callerClass: 'kernel',
|
|
16
|
+
identity: null,
|
|
17
|
+
official: true,
|
|
18
|
+
capabilities: EMPTY_CAPABILITIES,
|
|
19
|
+
});
|
|
20
|
+
/** A participant the kernel has not assigned an identity to. */
|
|
21
|
+
export const UNIDENTIFIED_PARTICIPANT = Object.freeze({
|
|
22
|
+
callerClass: 'participant',
|
|
23
|
+
identity: null,
|
|
24
|
+
official: false,
|
|
25
|
+
capabilities: EMPTY_CAPABILITIES,
|
|
26
|
+
});
|
|
27
|
+
/**
|
|
28
|
+
* One kernel's call chain.
|
|
29
|
+
*
|
|
30
|
+
* Starts root frames, switches the current caller when the kernel mediates a
|
|
31
|
+
* call into a participant, applies per-identity limits, and keeps the
|
|
32
|
+
* observation stream.
|
|
33
|
+
*/
|
|
34
|
+
export class KernelCallChain {
|
|
35
|
+
#limits;
|
|
36
|
+
#instanceOf;
|
|
37
|
+
#byToken = new Map();
|
|
38
|
+
#byIdentity = new Map();
|
|
39
|
+
#usage = new Map();
|
|
40
|
+
#observations = [];
|
|
41
|
+
/**
|
|
42
|
+
* @param instanceOf - Reads the initialized instance registered for a token.
|
|
43
|
+
* @param limits - Per-identity call and size limits.
|
|
44
|
+
*/
|
|
45
|
+
constructor(instanceOf, limits = DEFAULT_KERNEL_CALL_LIMITS) {
|
|
46
|
+
this.#instanceOf = instanceOf;
|
|
47
|
+
this.#limits = Object.freeze({ ...limits });
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Records the admitted participants for this boot.
|
|
51
|
+
*
|
|
52
|
+
* @param participants - Every admitted participant with its kernel caller.
|
|
53
|
+
*/
|
|
54
|
+
admitParticipants(participants) {
|
|
55
|
+
this.#byToken.clear();
|
|
56
|
+
this.#byIdentity.clear();
|
|
57
|
+
for (const participant of participants) {
|
|
58
|
+
this.#byToken.set(participant.tokenId, participant);
|
|
59
|
+
if (participant.caller.identity !== null) {
|
|
60
|
+
this.#byIdentity.set(participant.caller.identity, participant);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** Forgets every participant; nothing is live after shutdown. */
|
|
65
|
+
clearParticipants() {
|
|
66
|
+
this.#byToken.clear();
|
|
67
|
+
this.#byIdentity.clear();
|
|
68
|
+
this.#usage.clear();
|
|
69
|
+
}
|
|
70
|
+
/** The kernel caller recorded for a descriptor token, if any. */
|
|
71
|
+
callerForToken(tokenId) {
|
|
72
|
+
return this.#byToken.get(tokenId)?.caller;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Starts a root frame. The origin is fixed for everything that runs inside.
|
|
76
|
+
*
|
|
77
|
+
* @param origin - Root origin of the new chain.
|
|
78
|
+
* @param caller - Caller that starts the chain.
|
|
79
|
+
* @param operation - Work performed inside the chain.
|
|
80
|
+
*/
|
|
81
|
+
runRoot(origin, caller, operation) {
|
|
82
|
+
return frames.run({ chain: this, context: Object.freeze({ origin, caller }) }, operation);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Starts a host-origin chain for one host session.
|
|
86
|
+
*
|
|
87
|
+
* @param session - The host session's declaration.
|
|
88
|
+
* @param operation - Work performed inside the chain.
|
|
89
|
+
*/
|
|
90
|
+
runHost(session, operation) {
|
|
91
|
+
const capabilities = new Set();
|
|
92
|
+
for (const capability of session.capabilities ?? []) {
|
|
93
|
+
if (typeof capability === 'string' && capability.length > 0) {
|
|
94
|
+
capabilities.add(capability);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const caller = Object.freeze({
|
|
98
|
+
callerClass: 'host',
|
|
99
|
+
identity: null,
|
|
100
|
+
official: false,
|
|
101
|
+
capabilities: Object.freeze(capabilities),
|
|
102
|
+
});
|
|
103
|
+
return this.runRoot('host', caller, operation);
|
|
104
|
+
}
|
|
105
|
+
current() {
|
|
106
|
+
const frame = frames.getStore();
|
|
107
|
+
return frame?.chain === this ? frame.context : undefined;
|
|
108
|
+
}
|
|
109
|
+
runAsOrigin(origin, identity, operation) {
|
|
110
|
+
const participant = this.#byIdentity.get(identity);
|
|
111
|
+
if (participant === undefined) {
|
|
112
|
+
throw new Error('Identity is not live in this composition');
|
|
113
|
+
}
|
|
114
|
+
return frames.run({
|
|
115
|
+
chain: this,
|
|
116
|
+
context: Object.freeze({ origin, caller: participant.caller }),
|
|
117
|
+
}, operation);
|
|
118
|
+
}
|
|
119
|
+
runAs(identity, operation) {
|
|
120
|
+
const context = this.current();
|
|
121
|
+
if (context === undefined) {
|
|
122
|
+
throw new Error('No kernel call chain is active');
|
|
123
|
+
}
|
|
124
|
+
const participant = this.#byIdentity.get(identity);
|
|
125
|
+
if (participant === undefined) {
|
|
126
|
+
throw new Error('Identity is not live in this composition');
|
|
127
|
+
}
|
|
128
|
+
return frames.run({
|
|
129
|
+
chain: this,
|
|
130
|
+
context: Object.freeze({ origin: context.origin, caller: participant.caller }),
|
|
131
|
+
}, operation);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Token id of the live participant holding `identity`, if any.
|
|
135
|
+
*
|
|
136
|
+
* @param identity - Kernel-issued identity.
|
|
137
|
+
*/
|
|
138
|
+
tokenIdOf(identity) {
|
|
139
|
+
return this.#byIdentity.get(identity)?.tokenId;
|
|
140
|
+
}
|
|
141
|
+
isLive(identity) {
|
|
142
|
+
return this.#byIdentity.has(identity);
|
|
143
|
+
}
|
|
144
|
+
instanceOf(identity) {
|
|
145
|
+
const participant = this.#byIdentity.get(identity);
|
|
146
|
+
return participant === undefined ? undefined : this.#instanceOf(participant.tokenId);
|
|
147
|
+
}
|
|
148
|
+
callerForIdentity(identity) {
|
|
149
|
+
return this.#byIdentity.get(identity)?.caller;
|
|
150
|
+
}
|
|
151
|
+
admitCall(caller, requestBytes) {
|
|
152
|
+
if (!Number.isFinite(requestBytes) || requestBytes > this.#limits.maxRequestBytes) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
const key = caller.identity ?? `class:${caller.callerClass}`;
|
|
156
|
+
const now = Date.now();
|
|
157
|
+
const usage = this.#usage.get(key);
|
|
158
|
+
if (usage === undefined || now - usage.windowStart >= this.#limits.windowMs) {
|
|
159
|
+
this.#usage.set(key, { windowStart: now, calls: 1 });
|
|
160
|
+
return 1 <= this.#limits.maxCallsPerWindow;
|
|
161
|
+
}
|
|
162
|
+
usage.calls += 1;
|
|
163
|
+
return usage.calls <= this.#limits.maxCallsPerWindow;
|
|
164
|
+
}
|
|
165
|
+
observe(source, event, detail) {
|
|
166
|
+
this.#observations.push(Object.freeze({
|
|
167
|
+
at: Date.now(),
|
|
168
|
+
source,
|
|
169
|
+
event,
|
|
170
|
+
detail: Object.freeze({ ...detail }),
|
|
171
|
+
}));
|
|
172
|
+
if (this.#observations.length > OBSERVATION_CAPACITY) {
|
|
173
|
+
this.#observations.splice(0, this.#observations.length - OBSERVATION_CAPACITY);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/** A snapshot of the observation stream, oldest first. */
|
|
177
|
+
observations() {
|
|
178
|
+
return Object.freeze([...this.#observations]);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { CircularDependencyError, MissingDependencyError } from './errors.js';
|
|
2
|
+
export function topologicalSort(descriptors) {
|
|
3
|
+
const visitStates = new Map();
|
|
4
|
+
const path = [];
|
|
5
|
+
const ordered = [];
|
|
6
|
+
const visit = (tokenId, dependentTokenId) => {
|
|
7
|
+
const visitState = visitStates.get(tokenId);
|
|
8
|
+
if (visitState === 'VISITED') {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
if (visitState === 'VISITING') {
|
|
12
|
+
const cycleStart = path.indexOf(tokenId);
|
|
13
|
+
const cyclePath = path.slice(cycleStart === -1 ? 0 : cycleStart);
|
|
14
|
+
cyclePath.push(tokenId);
|
|
15
|
+
throw new CircularDependencyError(cyclePath);
|
|
16
|
+
}
|
|
17
|
+
const descriptor = descriptors.get(tokenId);
|
|
18
|
+
if (descriptor === undefined) {
|
|
19
|
+
throw new MissingDependencyError(tokenId, dependentTokenId ?? tokenId);
|
|
20
|
+
}
|
|
21
|
+
visitStates.set(tokenId, 'VISITING');
|
|
22
|
+
path.push(tokenId);
|
|
23
|
+
for (const dependency of descriptor.dependencies) {
|
|
24
|
+
visit(dependency.id, tokenId);
|
|
25
|
+
}
|
|
26
|
+
path.pop();
|
|
27
|
+
visitStates.set(tokenId, 'VISITED');
|
|
28
|
+
ordered.push(descriptor);
|
|
29
|
+
};
|
|
30
|
+
for (const tokenId of descriptors.keys()) {
|
|
31
|
+
visit(tokenId, undefined);
|
|
32
|
+
}
|
|
33
|
+
return ordered;
|
|
34
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { KernelLifecycleState } from './state.js';
|
|
2
|
+
export declare class LifecycleStateError extends Error {
|
|
3
|
+
readonly code: "INVALID_LIFECYCLE_STATE";
|
|
4
|
+
readonly operation: string;
|
|
5
|
+
readonly currentState: KernelLifecycleState | undefined;
|
|
6
|
+
readonly expectedState: KernelLifecycleState | undefined;
|
|
7
|
+
constructor(operation: string, currentState?: KernelLifecycleState, expectedState?: KernelLifecycleState);
|
|
8
|
+
private static formatMessage;
|
|
9
|
+
}
|
|
10
|
+
export declare class CircularDependencyError extends Error {
|
|
11
|
+
readonly code: "CIRCULAR_DEPENDENCY";
|
|
12
|
+
readonly cyclePath: readonly string[];
|
|
13
|
+
constructor(cyclePath: readonly string[]);
|
|
14
|
+
}
|
|
15
|
+
export declare class MissingDependencyError extends Error {
|
|
16
|
+
readonly code: "MISSING_DEPENDENCY";
|
|
17
|
+
readonly missingTokenId: string;
|
|
18
|
+
readonly dependentTokenId: string;
|
|
19
|
+
constructor(missingTokenId: string, dependentTokenId: string);
|
|
20
|
+
}
|
|
21
|
+
export declare class ServiceNotFoundError extends Error {
|
|
22
|
+
readonly code: "SERVICE_NOT_FOUND";
|
|
23
|
+
readonly tokenId: string;
|
|
24
|
+
constructor(tokenId: string);
|
|
25
|
+
}
|
|
26
|
+
export { AccessDeniedError, BlacklistedAccessViolationError, AllowlistViolationError, } from '../contracts/policy.js';
|