@brud/kernel 0.0.0-stage → 0.0.2
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 +56 -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 +133 -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 +68 -0
- package/dist/services/ExposedApiRegistry/orchestrator/ExposedApiRegistryService.js +452 -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 +155 -0
- package/dist/services/Interception/contracts/IInterceptionService.js +12 -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 +50 -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 +40 -0
- package/dist/services/Interception/orchestrator/InterceptionService.js +343 -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 +67 -0
- package/dist/services/Router/contracts/errors.js +90 -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 +110 -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 +93 -0
- package/dist/services/Router/orchestrator/RouterService.js +457 -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 +121 -0
- package/dist/services/WasmCage/src/caged.js +460 -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 +19 -0
- package/dist/src/bootstrap/bootKernelInternal.js +633 -0
- package/dist/src/bootstrap/bootTypes.d.ts +113 -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 +45 -0
- package/dist/src/bootstrap/config/integrity.js +92 -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 +13 -0
- package/dist/src/bootstrap/index.js +7 -0
- package/dist/src/bootstrap/loaded.d.ts +30 -0
- package/dist/src/bootstrap/loaded.js +45 -0
- package/dist/src/bootstrap/packageRequirements.d.ts +26 -0
- package/dist/src/bootstrap/packageRequirements.js +52 -0
- package/dist/src/bootstrap/reload.d.ts +44 -0
- package/dist/src/bootstrap/reload.js +45 -0
- package/dist/src/bootstrap/seal.d.ts +32 -0
- package/dist/src/bootstrap/seal.js +101 -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/capabilitySet.d.ts +23 -0
- package/dist/src/contracts/capabilitySet.js +72 -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 +132 -0
- package/dist/src/contracts/contextContract.js +274 -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 +21 -0
- package/dist/src/contracts/handler.js +1 -0
- package/dist/src/contracts/index.d.ts +26 -0
- package/dist/src/contracts/index.js +9 -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 +180 -0
- package/dist/src/contracts/manifest.js +334 -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 +120 -0
- package/dist/src/engine/BrudKernel.js +395 -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 +175 -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,56 @@
|
|
|
1
|
+
import { createServiceToken } from '../../../src/contracts/index.js';
|
|
2
|
+
import { ExposedApiRegistryToken } from '../../ExposedApiRegistry/index.js';
|
|
3
|
+
/** Tier assigned to the cage's descriptor. */
|
|
4
|
+
export const WASM_CAGE_SERVICE_TIER = 'core';
|
|
5
|
+
/** Contract version declared for the cage's interface. */
|
|
6
|
+
export const WASM_CAGE_CONTRACT_VERSION = '1.0.0';
|
|
7
|
+
/**
|
|
8
|
+
* Methods declared for the cage's interface.
|
|
9
|
+
*
|
|
10
|
+
* The cage's only participant-facing surface is the exposed-API registry's
|
|
11
|
+
* `resolve` (design record W-02, W-09). The cage is not a second giver; it is
|
|
12
|
+
* the mechanism that carries a caged participant's call to the registry.
|
|
13
|
+
*/
|
|
14
|
+
export const WASM_CAGE_CONTRACT_METHODS = ['resolve'];
|
|
15
|
+
/** Token identifying the cage. */
|
|
16
|
+
export const WasmCageToken = createServiceToken('brud.core.wasmCage', 'WASM isolation primitive: runs a caged participant\'s calls through the exposed-API registry');
|
|
17
|
+
export function createWasmCageDescriptor() {
|
|
18
|
+
return {
|
|
19
|
+
token: WasmCageToken,
|
|
20
|
+
tier: WASM_CAGE_SERVICE_TIER,
|
|
21
|
+
dependencies: [],
|
|
22
|
+
contract: {
|
|
23
|
+
version: WASM_CAGE_CONTRACT_VERSION,
|
|
24
|
+
methods: WASM_CAGE_CONTRACT_METHODS,
|
|
25
|
+
},
|
|
26
|
+
factory: (context, broker) => {
|
|
27
|
+
// The cage starts a runtime-origin chain per caged call. The chain-start
|
|
28
|
+
// member needs the kernel's call authority, which the factory does not
|
|
29
|
+
// receive. The cage captures it in `bindAuthority`, called from its boot
|
|
30
|
+
// hook (a later step). Until then a caged call is refused rather than
|
|
31
|
+
// run without a chain.
|
|
32
|
+
let authority;
|
|
33
|
+
const registryFor = (caller) => broker.requestAccess(caller, ExposedApiRegistryToken);
|
|
34
|
+
const instance = {
|
|
35
|
+
bindAuthority(value) {
|
|
36
|
+
authority = value;
|
|
37
|
+
},
|
|
38
|
+
resolve(participantIdentity, name, options) {
|
|
39
|
+
const callAuthority = authority;
|
|
40
|
+
if (callAuthority === undefined) {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
const caller = {
|
|
44
|
+
callerId: participantIdentity,
|
|
45
|
+
tier: context.tier,
|
|
46
|
+
options: context.options,
|
|
47
|
+
};
|
|
48
|
+
// Registry access and the resolve both happen inside the caged
|
|
49
|
+
// participant's own frame, so the broker binds the request to it.
|
|
50
|
+
return callAuthority.runAsOrigin('runtime', participantIdentity, () => registryFor(caller).resolve(name, options));
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
return instance;
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Per-platform native binary loading for the WASM cage.
|
|
3
|
+
*
|
|
4
|
+
* Binaries are named `brud-wasm-cage.<platform-key>.node` and live in the
|
|
5
|
+
* package's `services/WasmCage/native/` folder, which ships in the npm package.
|
|
6
|
+
* The loader finds that folder from source and from `dist/`, checks the binary's
|
|
7
|
+
* SHA-256 against the pinned digest table, and only then loads it. A platform
|
|
8
|
+
* with no listed binary, a missing file, or a digest mismatch fails closed; the
|
|
9
|
+
* kernel itself still boots, and only caged participants are unavailable.
|
|
10
|
+
*/
|
|
11
|
+
/** Error codes of the native loader. */
|
|
12
|
+
export type CageNativeErrorCode = 'WASM_CAGE_NATIVE_UNAVAILABLE' | 'WASM_CAGE_NATIVE_TAMPERED';
|
|
13
|
+
/** The cage's native binary is unavailable or failed its digest check. */
|
|
14
|
+
export declare class CageNativeError extends Error {
|
|
15
|
+
readonly code: CageNativeErrorCode;
|
|
16
|
+
constructor(code: CageNativeErrorCode, message: string);
|
|
17
|
+
}
|
|
18
|
+
/** The running platform's key, e.g. `linux-x64-gnu`, `darwin-arm64`, `win32-x64-msvc`. */
|
|
19
|
+
export declare function nativePlatformKey(): string;
|
|
20
|
+
/** The binary's file name for a platform key. */
|
|
21
|
+
export declare function nativeBinaryName(key: string): string;
|
|
22
|
+
/**
|
|
23
|
+
* Loads the verified native binary for the running platform.
|
|
24
|
+
*
|
|
25
|
+
* @returns The binary's exports.
|
|
26
|
+
* @throws {CageNativeError} When no binary is listed or present for the
|
|
27
|
+
* platform, or the binary's digest does not match the pinned table.
|
|
28
|
+
*/
|
|
29
|
+
export declare function loadVerifiedNative<T>(): T;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
3
|
+
import { createRequire } from 'node:module';
|
|
4
|
+
import { sep } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { WASM_CAGE_NATIVE_DIGESTS } from './nativeDigests.js';
|
|
7
|
+
/** The cage's native binary is unavailable or failed its digest check. */
|
|
8
|
+
export class CageNativeError extends Error {
|
|
9
|
+
code;
|
|
10
|
+
constructor(code, message) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = 'CageNativeError';
|
|
13
|
+
this.code = code;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** The running platform's key, e.g. `linux-x64-gnu`, `darwin-arm64`, `win32-x64-msvc`. */
|
|
17
|
+
export function nativePlatformKey() {
|
|
18
|
+
const { platform, arch } = process;
|
|
19
|
+
if (platform === 'linux') {
|
|
20
|
+
const report = process.report.getReport();
|
|
21
|
+
return `linux-${arch}-${report.header?.glibcVersionRuntime === undefined ? 'musl' : 'gnu'}`;
|
|
22
|
+
}
|
|
23
|
+
return platform === 'win32' ? `win32-${arch}-msvc` : `${platform}-${arch}`;
|
|
24
|
+
}
|
|
25
|
+
/** The binary's file name for a platform key. */
|
|
26
|
+
export function nativeBinaryName(key) {
|
|
27
|
+
return `brud-wasm-cage.${key}.node`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Candidate native folders: next to this module, and, when this module runs
|
|
31
|
+
* from `dist/`, the same folder in the package's shipped source tree.
|
|
32
|
+
*/
|
|
33
|
+
function nativeFolders() {
|
|
34
|
+
const here = fileURLToPath(new URL('../native/', import.meta.url));
|
|
35
|
+
const dist = here.lastIndexOf(`${sep}dist${sep}`);
|
|
36
|
+
return dist === -1 ? [here] : [here, here.slice(0, dist) + here.slice(dist + `${sep}dist`.length)];
|
|
37
|
+
}
|
|
38
|
+
const DIGESTS = new Map(WASM_CAGE_NATIVE_DIGESTS);
|
|
39
|
+
let loaded;
|
|
40
|
+
/**
|
|
41
|
+
* Loads the verified native binary for the running platform.
|
|
42
|
+
*
|
|
43
|
+
* @returns The binary's exports.
|
|
44
|
+
* @throws {CageNativeError} When no binary is listed or present for the
|
|
45
|
+
* platform, or the binary's digest does not match the pinned table.
|
|
46
|
+
*/
|
|
47
|
+
export function loadVerifiedNative() {
|
|
48
|
+
if (loaded !== undefined) {
|
|
49
|
+
return loaded;
|
|
50
|
+
}
|
|
51
|
+
const key = nativePlatformKey();
|
|
52
|
+
const expected = DIGESTS.get(key);
|
|
53
|
+
if (expected === undefined) {
|
|
54
|
+
throw new CageNativeError('WASM_CAGE_NATIVE_UNAVAILABLE', `no WASM cage binary is published for platform "${key}"`);
|
|
55
|
+
}
|
|
56
|
+
const path = nativeFolders().map((folder) => folder + nativeBinaryName(key)).find((candidate) => existsSync(candidate));
|
|
57
|
+
if (path === undefined) {
|
|
58
|
+
throw new CageNativeError('WASM_CAGE_NATIVE_UNAVAILABLE', `the WASM cage binary for platform "${key}" is missing`);
|
|
59
|
+
}
|
|
60
|
+
const actual = createHash('sha256').update(readFileSync(path)).digest('hex');
|
|
61
|
+
if (actual !== expected) {
|
|
62
|
+
throw new CageNativeError('WASM_CAGE_NATIVE_TAMPERED', `the WASM cage binary for platform "${key}" failed its digest check`);
|
|
63
|
+
}
|
|
64
|
+
loaded = createRequire(import.meta.url)(path);
|
|
65
|
+
return loaded;
|
|
66
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SHA-256 digests of the cage's native binaries, as `[platform key, digest]`.
|
|
3
|
+
*
|
|
4
|
+
* Generated by `services/WasmCage/scripts/prepin.ts` (run through the kernel's
|
|
5
|
+
* `npm run repin`); do not edit by hand. The table is part of the cage's pinned
|
|
6
|
+
* package graph, so the hardcore content pins cover it; the loader refuses any
|
|
7
|
+
* binary whose digest is not listed here.
|
|
8
|
+
*/
|
|
9
|
+
export declare const WASM_CAGE_NATIVE_DIGESTS: readonly (readonly [string, string])[];
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SHA-256 digests of the cage's native binaries, as `[platform key, digest]`.
|
|
3
|
+
*
|
|
4
|
+
* Generated by `services/WasmCage/scripts/prepin.ts` (run through the kernel's
|
|
5
|
+
* `npm run repin`); do not edit by hand. The table is part of the cage's pinned
|
|
6
|
+
* package graph, so the hardcore content pins cover it; the loader refuses any
|
|
7
|
+
* binary whose digest is not listed here.
|
|
8
|
+
*/
|
|
9
|
+
export const WASM_CAGE_NATIVE_DIGESTS = Object.freeze([
|
|
10
|
+
['darwin-arm64', '47af7e64c8481e42cd7b73bd89ac3f69da182591b64a1b656a33725cb5e64cad'],
|
|
11
|
+
['darwin-x64', 'c833ad031943aa522720805a418edb36add03da0f79bf4421aa908d7644f324a'],
|
|
12
|
+
['linux-arm64-gnu', '5c420e2cf34c26dbd15951d8e9d4c0444f89ac8c3e789637d0a75661699ed25c'],
|
|
13
|
+
['linux-x64-gnu', '7c7d46dfe4b3c72c2612b9742bb474d97f05670332f175055a33d37e61ef5ffa'],
|
|
14
|
+
['win32-x64-msvc', '5be0f4f1b82ab17284afa78b0248d300a8a1589ae288dacbfe2f628d9bed85e2'],
|
|
15
|
+
]);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { ServiceDescriptor } from '../../../src/contracts/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* Participant packages: how a participant that is not declared official reaches
|
|
4
|
+
* the kernel (W-01). The package is declarative data plus a WASM module; no
|
|
5
|
+
* author JavaScript is loaded. `brud.participant.json`:
|
|
6
|
+
*
|
|
7
|
+
* ```json
|
|
8
|
+
* { "format": 1, "id": "acme.notes", "manifest": { … },
|
|
9
|
+
* "cage": { "module": "participant.wasm", "sha256": "<hex>", "limits": { "fuel": …, "maxMemoryBytes": …, "deadlineMs": … } },
|
|
10
|
+
* "native": "native/index.js" }
|
|
11
|
+
* ```
|
|
12
|
+
*
|
|
13
|
+
* `native` is optional and used only when the composition declares the entry
|
|
14
|
+
* official; the kernel's loader reads it, never this module. `requires` is
|
|
15
|
+
* optional too: `{ "official": true, "powers": [...] }` makes the kernel refuse
|
|
16
|
+
* the entry unless the composition declares it official. The kernel's loader
|
|
17
|
+
* enforces it; this module only accepts the key.
|
|
18
|
+
*/
|
|
19
|
+
export declare const PARTICIPANT_PACKAGE_FORMAT = 1;
|
|
20
|
+
/** A participant package is malformed or its module failed its digest check. */
|
|
21
|
+
export declare class ParticipantPackageError extends Error {
|
|
22
|
+
readonly code: 'PARTICIPANT_PACKAGE_INVALID' | 'PARTICIPANT_PACKAGE_TAMPERED';
|
|
23
|
+
constructor(code: 'PARTICIPANT_PACKAGE_INVALID' | 'PARTICIPANT_PACKAGE_TAMPERED', message: string);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Loads a participant package into the cage: validates the package, checks
|
|
27
|
+
* the module's SHA-256 against the package, and returns a service module whose
|
|
28
|
+
* descriptor runs the participant caged.
|
|
29
|
+
*
|
|
30
|
+
* @param manifestPath - Absolute path of `brud.participant.json`.
|
|
31
|
+
* @returns A service module (`createService`).
|
|
32
|
+
* @throws {ParticipantPackageError} For a malformed package or a digest mismatch.
|
|
33
|
+
*/
|
|
34
|
+
export declare function loadParticipantPackage(manifestPath: string): Promise<{
|
|
35
|
+
createService(): ServiceDescriptor<object>;
|
|
36
|
+
}>;
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import { dirname, isAbsolute, relative, resolve } from 'node:path';
|
|
4
|
+
import { createCagedParticipant } from './caged.js';
|
|
5
|
+
/**
|
|
6
|
+
* Participant packages: how a participant that is not declared official reaches
|
|
7
|
+
* the kernel (W-01). The package is declarative data plus a WASM module; no
|
|
8
|
+
* author JavaScript is loaded. `brud.participant.json`:
|
|
9
|
+
*
|
|
10
|
+
* ```json
|
|
11
|
+
* { "format": 1, "id": "acme.notes", "manifest": { … },
|
|
12
|
+
* "cage": { "module": "participant.wasm", "sha256": "<hex>", "limits": { "fuel": …, "maxMemoryBytes": …, "deadlineMs": … } },
|
|
13
|
+
* "native": "native/index.js" }
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* `native` is optional and used only when the composition declares the entry
|
|
17
|
+
* official; the kernel's loader reads it, never this module. `requires` is
|
|
18
|
+
* optional too: `{ "official": true, "powers": [...] }` makes the kernel refuse
|
|
19
|
+
* the entry unless the composition declares it official. The kernel's loader
|
|
20
|
+
* enforces it; this module only accepts the key.
|
|
21
|
+
*/
|
|
22
|
+
export const PARTICIPANT_PACKAGE_FORMAT = 1;
|
|
23
|
+
/** A participant package is malformed or its module failed its digest check. */
|
|
24
|
+
export class ParticipantPackageError extends Error {
|
|
25
|
+
code;
|
|
26
|
+
constructor(code, message) {
|
|
27
|
+
super(message);
|
|
28
|
+
this.name = 'ParticipantPackageError';
|
|
29
|
+
this.code = code;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
const PACKAGE_KEYS = new Set(['format', 'id', 'manifest', 'cage', 'native', 'requires']);
|
|
33
|
+
const CAGE_KEYS = new Set(['module', 'sha256', 'limits']);
|
|
34
|
+
const LIMIT_KEYS = new Set(['fuel', 'maxMemoryBytes', 'deadlineMs']);
|
|
35
|
+
const isRecord = (value) => value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
36
|
+
function invalid(path, reason) {
|
|
37
|
+
return new ParticipantPackageError('PARTICIPANT_PACKAGE_INVALID', `participant package "${path}": ${reason}`);
|
|
38
|
+
}
|
|
39
|
+
function onlyKeys(value, allowed, path, where) {
|
|
40
|
+
const unknown = Object.keys(value).filter((key) => !allowed.has(key));
|
|
41
|
+
if (unknown.length > 0) {
|
|
42
|
+
throw invalid(path, `unknown ${where} key(s) ${unknown.join(', ')}`);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function limitsOf(value, path) {
|
|
46
|
+
if (value === undefined) {
|
|
47
|
+
return undefined;
|
|
48
|
+
}
|
|
49
|
+
if (!isRecord(value)) {
|
|
50
|
+
throw invalid(path, 'cage.limits must be an object');
|
|
51
|
+
}
|
|
52
|
+
onlyKeys(value, LIMIT_KEYS, path, 'cage.limits');
|
|
53
|
+
for (const key of Object.keys(value)) {
|
|
54
|
+
if (typeof value[key] !== 'number' || !Number.isFinite(value[key]) || value[key] <= 0) {
|
|
55
|
+
throw invalid(path, `cage.limits.${key} must be a positive number`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (value.fuel === undefined || value.maxMemoryBytes === undefined) {
|
|
59
|
+
throw invalid(path, 'cage.limits needs fuel and maxMemoryBytes');
|
|
60
|
+
}
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Loads a participant package into the cage: validates the package, checks
|
|
65
|
+
* the module's SHA-256 against the package, and returns a service module whose
|
|
66
|
+
* descriptor runs the participant caged.
|
|
67
|
+
*
|
|
68
|
+
* @param manifestPath - Absolute path of `brud.participant.json`.
|
|
69
|
+
* @returns A service module (`createService`).
|
|
70
|
+
* @throws {ParticipantPackageError} For a malformed package or a digest mismatch.
|
|
71
|
+
*/
|
|
72
|
+
export async function loadParticipantPackage(manifestPath) {
|
|
73
|
+
let pkg;
|
|
74
|
+
try {
|
|
75
|
+
pkg = JSON.parse(readFileSync(manifestPath, 'utf8'));
|
|
76
|
+
}
|
|
77
|
+
catch (error) {
|
|
78
|
+
throw invalid(manifestPath, `unreadable (${error.message})`);
|
|
79
|
+
}
|
|
80
|
+
if (!isRecord(pkg)) {
|
|
81
|
+
throw invalid(manifestPath, 'must be a JSON object');
|
|
82
|
+
}
|
|
83
|
+
onlyKeys(pkg, PACKAGE_KEYS, manifestPath, 'package');
|
|
84
|
+
if (pkg.format !== PARTICIPANT_PACKAGE_FORMAT) {
|
|
85
|
+
throw invalid(manifestPath, `format must be ${String(PARTICIPANT_PACKAGE_FORMAT)}`);
|
|
86
|
+
}
|
|
87
|
+
if (typeof pkg.id !== 'string' || pkg.id.length === 0) {
|
|
88
|
+
throw invalid(manifestPath, 'id must be a non-empty string');
|
|
89
|
+
}
|
|
90
|
+
if (!isRecord(pkg.manifest)) {
|
|
91
|
+
throw invalid(manifestPath, 'manifest must be an object');
|
|
92
|
+
}
|
|
93
|
+
if (!isRecord(pkg.cage)) {
|
|
94
|
+
throw invalid(manifestPath, 'cage must be an object (a package not declared official runs caged)');
|
|
95
|
+
}
|
|
96
|
+
onlyKeys(pkg.cage, CAGE_KEYS, manifestPath, 'cage');
|
|
97
|
+
if (typeof pkg.cage.module !== 'string' || typeof pkg.cage.sha256 !== 'string' || !/^[0-9a-f]{64}$/u.test(pkg.cage.sha256)) {
|
|
98
|
+
throw invalid(manifestPath, 'cage needs a module path and a lower-case hex sha256');
|
|
99
|
+
}
|
|
100
|
+
const root = dirname(manifestPath);
|
|
101
|
+
const modulePath = resolve(root, pkg.cage.module);
|
|
102
|
+
const inside = relative(root, modulePath);
|
|
103
|
+
if (inside.startsWith('..') || isAbsolute(inside)) {
|
|
104
|
+
throw invalid(manifestPath, 'cage.module must stay inside the package');
|
|
105
|
+
}
|
|
106
|
+
let bytes;
|
|
107
|
+
try {
|
|
108
|
+
bytes = readFileSync(modulePath);
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
throw invalid(manifestPath, `cage.module unreadable (${error.message})`);
|
|
112
|
+
}
|
|
113
|
+
if (createHash('sha256').update(bytes).digest('hex') !== pkg.cage.sha256) {
|
|
114
|
+
throw new ParticipantPackageError('PARTICIPANT_PACKAGE_TAMPERED', `participant package "${manifestPath}": cage.module failed its digest check`);
|
|
115
|
+
}
|
|
116
|
+
const id = pkg.id;
|
|
117
|
+
const manifest = pkg.manifest;
|
|
118
|
+
const limits = limitsOf(pkg.cage.limits, manifestPath);
|
|
119
|
+
return {
|
|
120
|
+
createService: () => createCagedParticipant({ token: id, manifest, wasm: new Uint8Array(bytes), ...(limits === undefined ? {} : { limits }) }),
|
|
121
|
+
};
|
|
122
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical JSON helpers for the kernel's integrity-protected documents.
|
|
3
|
+
*
|
|
4
|
+
* Integrity digests must survive key reordering, so serialization is
|
|
5
|
+
* canonical: object keys are sorted, arrays keep their order, and only
|
|
6
|
+
* JSON-native values are accepted.
|
|
7
|
+
*/
|
|
8
|
+
/** Raised when a value cannot be represented as JSON. */
|
|
9
|
+
export declare class JsonValueError extends Error {
|
|
10
|
+
constructor(message: string);
|
|
11
|
+
}
|
|
12
|
+
/** Reports whether a value is a plain JSON object. */
|
|
13
|
+
export declare function isPlainRecord(value: unknown): value is Record<string, unknown>;
|
|
14
|
+
/**
|
|
15
|
+
* Serializes a value as canonical JSON.
|
|
16
|
+
*
|
|
17
|
+
* @param value - JSON-native value.
|
|
18
|
+
* @param seen - Cycle detector used by recursive calls.
|
|
19
|
+
*/
|
|
20
|
+
export declare function canonicalJson(value: unknown, seen?: Set<object>): string;
|
|
21
|
+
/**
|
|
22
|
+
* Validates and deep-clones a JSON object.
|
|
23
|
+
*
|
|
24
|
+
* @param value - Candidate object.
|
|
25
|
+
* @returns A deeply frozen clone with canonical key order.
|
|
26
|
+
*/
|
|
27
|
+
export declare function cloneJsonRecord(value: unknown): Readonly<Record<string, unknown>>;
|
|
28
|
+
/** Recursively freezes an object graph. */
|
|
29
|
+
export declare function deepFreeze<T>(value: T): T;
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical JSON helpers for the kernel's integrity-protected documents.
|
|
3
|
+
*
|
|
4
|
+
* Integrity digests must survive key reordering, so serialization is
|
|
5
|
+
* canonical: object keys are sorted, arrays keep their order, and only
|
|
6
|
+
* JSON-native values are accepted.
|
|
7
|
+
*/
|
|
8
|
+
/** Raised when a value cannot be represented as JSON. */
|
|
9
|
+
export class JsonValueError extends Error {
|
|
10
|
+
constructor(message) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = 'JsonValueError';
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
/** Reports whether a value is a plain JSON object. */
|
|
16
|
+
export function isPlainRecord(value) {
|
|
17
|
+
if (value === null || typeof value !== 'object' || Array.isArray(value)) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
const prototype = Object.getPrototypeOf(value);
|
|
21
|
+
return prototype === Object.prototype || prototype === null;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Serializes a value as canonical JSON.
|
|
25
|
+
*
|
|
26
|
+
* @param value - JSON-native value.
|
|
27
|
+
* @param seen - Cycle detector used by recursive calls.
|
|
28
|
+
*/
|
|
29
|
+
export function canonicalJson(value, seen = new Set()) {
|
|
30
|
+
if (value === null) {
|
|
31
|
+
return 'null';
|
|
32
|
+
}
|
|
33
|
+
switch (typeof value) {
|
|
34
|
+
case 'string':
|
|
35
|
+
return JSON.stringify(value);
|
|
36
|
+
case 'boolean':
|
|
37
|
+
return value ? 'true' : 'false';
|
|
38
|
+
case 'number':
|
|
39
|
+
if (!Number.isFinite(value)) {
|
|
40
|
+
throw new JsonValueError('Non-finite numbers are not valid JSON');
|
|
41
|
+
}
|
|
42
|
+
return JSON.stringify(value);
|
|
43
|
+
case 'object': {
|
|
44
|
+
if (seen.has(value)) {
|
|
45
|
+
throw new JsonValueError('Circular references are not valid JSON');
|
|
46
|
+
}
|
|
47
|
+
seen.add(value);
|
|
48
|
+
try {
|
|
49
|
+
if (Array.isArray(value)) {
|
|
50
|
+
const items = [];
|
|
51
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
52
|
+
items.push(canonicalJson(value[index], seen));
|
|
53
|
+
}
|
|
54
|
+
return `[${items.join(',')}]`;
|
|
55
|
+
}
|
|
56
|
+
if (!isPlainRecord(value)) {
|
|
57
|
+
throw new JsonValueError('Only plain objects and arrays are valid JSON');
|
|
58
|
+
}
|
|
59
|
+
const keys = Object.keys(value).sort();
|
|
60
|
+
const entries = keys.map((key) => `${JSON.stringify(key)}:${canonicalJson(value[key], seen)}`);
|
|
61
|
+
return `{${entries.join(',')}}`;
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
seen.delete(value);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
default:
|
|
68
|
+
throw new JsonValueError(`Unsupported JSON value type: ${typeof value}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Validates and deep-clones a JSON object.
|
|
73
|
+
*
|
|
74
|
+
* @param value - Candidate object.
|
|
75
|
+
* @returns A deeply frozen clone with canonical key order.
|
|
76
|
+
*/
|
|
77
|
+
export function cloneJsonRecord(value) {
|
|
78
|
+
if (!isPlainRecord(value)) {
|
|
79
|
+
throw new JsonValueError('Value must be a plain object');
|
|
80
|
+
}
|
|
81
|
+
const cloned = JSON.parse(canonicalJson(value));
|
|
82
|
+
return deepFreeze(cloned);
|
|
83
|
+
}
|
|
84
|
+
/** Recursively freezes an object graph. */
|
|
85
|
+
export function deepFreeze(value) {
|
|
86
|
+
if (value !== null && typeof value === 'object' && !Object.isFrozen(value)) {
|
|
87
|
+
Object.freeze(value);
|
|
88
|
+
for (const child of Object.values(value)) {
|
|
89
|
+
deepFreeze(child);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Raises a storage failure selected by the calling store. */
|
|
2
|
+
export type StorageFailure = (storagePath: string, reason: string) => never;
|
|
3
|
+
/** Builds the write failure selected by the calling store. */
|
|
4
|
+
export type WriteErrorFactory = (storagePath: string, reason: string) => Error;
|
|
5
|
+
/** Builds the path failure selected by the calling store. */
|
|
6
|
+
export type PathFailure = (storagePath: string) => never;
|
|
7
|
+
/** Computes the SHA-256 digest over a body's canonical JSON. */
|
|
8
|
+
export declare function canonicalDigest(body: unknown): string;
|
|
9
|
+
/** Compares two lowercase hex digests without short-circuiting. */
|
|
10
|
+
export declare function digestsEqual(expected: string, actual: string): boolean;
|
|
11
|
+
/** Asserts that an object contains exactly the expected keys. */
|
|
12
|
+
export declare function assertExactKeys(value: Record<string, unknown>, expected: ReadonlySet<string>, storagePath: string, label: string, fail: StorageFailure): void;
|
|
13
|
+
/** Resolves an absolute storage path, or raises the caller's path failure. */
|
|
14
|
+
export declare function resolveAbsoluteStoragePath(storagePath: string, fail: PathFailure): string;
|
|
15
|
+
/** Writes bytes through an exclusive sibling file and an atomic rename. */
|
|
16
|
+
export declare function writeAtomically(storagePath: string, raw: string, makeError: WriteErrorFactory): void;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { createHash, randomUUID, timingSafeEqual } from 'node:crypto';
|
|
2
|
+
import { closeSync, fsyncSync, mkdirSync, openSync, renameSync, unlinkSync, writeFileSync, } from 'node:fs';
|
|
3
|
+
import { basename, dirname, isAbsolute, join, resolve } from 'node:path';
|
|
4
|
+
import { canonicalJson } from './canonicalJson.js';
|
|
5
|
+
/**
|
|
6
|
+
* Shared document primitives for the kernel's integrity-protected stores.
|
|
7
|
+
*
|
|
8
|
+
* Both stores canonicalize a body, digest it, write through an exclusive
|
|
9
|
+
* sibling file and an atomic rename, and compare digests without
|
|
10
|
+
* short-circuiting. Keeping one implementation is what keeps their integrity
|
|
11
|
+
* answers identical.
|
|
12
|
+
*/
|
|
13
|
+
const DIGEST_ALGORITHM = 'sha256';
|
|
14
|
+
const DIGEST_PATTERN = /^[0-9a-f]{64}$/u;
|
|
15
|
+
/** Computes the SHA-256 digest over a body's canonical JSON. */
|
|
16
|
+
export function canonicalDigest(body) {
|
|
17
|
+
return createHash(DIGEST_ALGORITHM)
|
|
18
|
+
.update(canonicalJson(body), 'utf8')
|
|
19
|
+
.digest('hex');
|
|
20
|
+
}
|
|
21
|
+
/** Compares two lowercase hex digests without short-circuiting. */
|
|
22
|
+
export function digestsEqual(expected, actual) {
|
|
23
|
+
if (!DIGEST_PATTERN.test(expected) || !DIGEST_PATTERN.test(actual)) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
const expectedBytes = Buffer.from(expected, 'hex');
|
|
27
|
+
const actualBytes = Buffer.from(actual, 'hex');
|
|
28
|
+
if (expectedBytes.length !== actualBytes.length) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
return timingSafeEqual(expectedBytes, actualBytes);
|
|
32
|
+
}
|
|
33
|
+
/** Asserts that an object contains exactly the expected keys. */
|
|
34
|
+
export function assertExactKeys(value, expected, storagePath, label, fail) {
|
|
35
|
+
const keys = Object.keys(value);
|
|
36
|
+
if (keys.length !== expected.size || keys.some((key) => !expected.has(key))) {
|
|
37
|
+
fail(storagePath, `${label} has an unexpected shape`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** Resolves an absolute storage path, or raises the caller's path failure. */
|
|
41
|
+
export function resolveAbsoluteStoragePath(storagePath, fail) {
|
|
42
|
+
if (typeof storagePath !== 'string'
|
|
43
|
+
|| storagePath.length === 0
|
|
44
|
+
|| storagePath.includes('\0')
|
|
45
|
+
|| !isAbsolute(storagePath)) {
|
|
46
|
+
fail(storagePath);
|
|
47
|
+
}
|
|
48
|
+
return resolve(storagePath);
|
|
49
|
+
}
|
|
50
|
+
/** Writes bytes through an exclusive sibling file and an atomic rename. */
|
|
51
|
+
export function writeAtomically(storagePath, raw, makeError) {
|
|
52
|
+
const directory = dirname(storagePath);
|
|
53
|
+
const temporaryPath = join(directory, `.${basename(storagePath)}.${String(process.pid)}.${randomUUID()}.tmp`);
|
|
54
|
+
let descriptor;
|
|
55
|
+
try {
|
|
56
|
+
mkdirSync(directory, { recursive: true });
|
|
57
|
+
descriptor = openSync(temporaryPath, 'wx', 0o600);
|
|
58
|
+
writeFileSync(descriptor, raw, 'utf8');
|
|
59
|
+
fsyncSync(descriptor);
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
if (descriptor !== undefined) {
|
|
63
|
+
try {
|
|
64
|
+
closeSync(descriptor);
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
// The original write failure is the useful one.
|
|
68
|
+
}
|
|
69
|
+
descriptor = undefined;
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
unlinkSync(temporaryPath);
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
// Nothing to clean up.
|
|
76
|
+
}
|
|
77
|
+
const code = error.code;
|
|
78
|
+
throw makeError(storagePath, code === undefined ? 'unknown filesystem error' : code);
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
if (descriptor !== undefined) {
|
|
82
|
+
closeSync(descriptor);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
try {
|
|
86
|
+
renameSync(temporaryPath, storagePath);
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
try {
|
|
90
|
+
unlinkSync(temporaryPath);
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
// Nothing to clean up.
|
|
94
|
+
}
|
|
95
|
+
const code = error.code;
|
|
96
|
+
throw makeError(storagePath, code === undefined ? 'unknown filesystem error' : code);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type ReloadableKernel } from './reload.js';
|
|
2
|
+
import type { BootKernelOptions, ComposeKernelOptions } from './bootTypes.js';
|
|
3
|
+
/**
|
|
4
|
+
* Reads, validates, composes, and boots a kernel from `brud.config.json`.
|
|
5
|
+
*
|
|
6
|
+
* @param configPath - Path to the config file.
|
|
7
|
+
* @param options - Generic options bag and logger.
|
|
8
|
+
*/
|
|
9
|
+
export declare function bootKernel(configPath: string, options: BootKernelOptions): Promise<ReloadableKernel>;
|
|
10
|
+
/**
|
|
11
|
+
* Builds a kernel whose config is composed lazily on the first `boot()`.
|
|
12
|
+
*
|
|
13
|
+
* The host supplies the config path explicitly; the kernel never chooses or
|
|
14
|
+
* discovers a config. Deferring module imports until boot keeps module
|
|
15
|
+
* instances in the ESM cache instead of loading a second copy through
|
|
16
|
+
* `require`.
|
|
17
|
+
*/
|
|
18
|
+
export declare function createDeferredKernel(configPath: string, options: ComposeKernelOptions): ReloadableKernel;
|
|
19
|
+
export declare function resolveModuleId(declaredPath: string, configPath: string): string;
|