@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.
Files changed (286) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +63 -2
  3. package/dist/index.d.ts +15 -0
  4. package/dist/index.js +13 -0
  5. package/dist/serviceModules/events.d.ts +1 -0
  6. package/dist/serviceModules/events.js +1 -0
  7. package/dist/serviceModules/exposedApiRegistry.d.ts +1 -0
  8. package/dist/serviceModules/exposedApiRegistry.js +1 -0
  9. package/dist/serviceModules/identityRegistry.d.ts +1 -0
  10. package/dist/serviceModules/identityRegistry.js +1 -0
  11. package/dist/serviceModules/interception.d.ts +1 -0
  12. package/dist/serviceModules/interception.js +1 -0
  13. package/dist/serviceModules/parser.d.ts +1 -0
  14. package/dist/serviceModules/parser.js +1 -0
  15. package/dist/serviceModules/router.d.ts +1 -0
  16. package/dist/serviceModules/router.js +1 -0
  17. package/dist/serviceModules/wasmCage.d.ts +1 -0
  18. package/dist/serviceModules/wasmCage.js +1 -0
  19. package/dist/services/Events/contracts/IEventsService.d.ts +64 -0
  20. package/dist/services/Events/contracts/IEventsService.js +1 -0
  21. package/dist/services/Events/contracts/errors.d.ts +31 -0
  22. package/dist/services/Events/contracts/errors.js +40 -0
  23. package/dist/services/Events/contracts/index.d.ts +3 -0
  24. package/dist/services/Events/contracts/index.js +3 -0
  25. package/dist/services/Events/contracts/tokens.d.ts +2 -0
  26. package/dist/services/Events/contracts/tokens.js +2 -0
  27. package/dist/services/Events/descriptor/EventsDescriptor.d.ts +9 -0
  28. package/dist/services/Events/descriptor/EventsDescriptor.js +56 -0
  29. package/dist/services/Events/descriptor/tokens.d.ts +1 -0
  30. package/dist/services/Events/descriptor/tokens.js +1 -0
  31. package/dist/services/Events/index.d.ts +4 -0
  32. package/dist/services/Events/index.js +4 -0
  33. package/dist/services/Events/orchestrator/EventsService.d.ts +51 -0
  34. package/dist/services/Events/orchestrator/EventsService.js +339 -0
  35. package/dist/services/Events/orchestrator/index.d.ts +2 -0
  36. package/dist/services/Events/orchestrator/index.js +2 -0
  37. package/dist/services/Events/orchestrator/limits.d.ts +22 -0
  38. package/dist/services/Events/orchestrator/limits.js +33 -0
  39. package/dist/services/Events/serviceModule.d.ts +2 -0
  40. package/dist/services/Events/serviceModule.js +5 -0
  41. package/dist/services/ExposedApiRegistry/contracts/IExposedApiRegistry.d.ts +56 -0
  42. package/dist/services/ExposedApiRegistry/contracts/IExposedApiRegistry.js +1 -0
  43. package/dist/services/ExposedApiRegistry/contracts/errors.d.ts +132 -0
  44. package/dist/services/ExposedApiRegistry/contracts/errors.js +195 -0
  45. package/dist/services/ExposedApiRegistry/contracts/index.d.ts +9 -0
  46. package/dist/services/ExposedApiRegistry/contracts/index.js +4 -0
  47. package/dist/services/ExposedApiRegistry/contracts/policy.d.ts +90 -0
  48. package/dist/services/ExposedApiRegistry/contracts/policy.js +169 -0
  49. package/dist/services/ExposedApiRegistry/contracts/registrant.d.ts +37 -0
  50. package/dist/services/ExposedApiRegistry/contracts/registrant.js +36 -0
  51. package/dist/services/ExposedApiRegistry/contracts/types.d.ts +133 -0
  52. package/dist/services/ExposedApiRegistry/contracts/types.js +73 -0
  53. package/dist/services/ExposedApiRegistry/descriptor/ExposedApiRegistryDescriptor.d.ts +44 -0
  54. package/dist/services/ExposedApiRegistry/descriptor/ExposedApiRegistryDescriptor.js +58 -0
  55. package/dist/services/ExposedApiRegistry/descriptor/tokens.d.ts +8 -0
  56. package/dist/services/ExposedApiRegistry/descriptor/tokens.js +8 -0
  57. package/dist/services/ExposedApiRegistry/index.d.ts +6 -0
  58. package/dist/services/ExposedApiRegistry/index.js +5 -0
  59. package/dist/services/ExposedApiRegistry/orchestrator/ExposedApiRegistryService.d.ts +68 -0
  60. package/dist/services/ExposedApiRegistry/orchestrator/ExposedApiRegistryService.js +452 -0
  61. package/dist/services/ExposedApiRegistry/orchestrator/index.d.ts +2 -0
  62. package/dist/services/ExposedApiRegistry/orchestrator/index.js +1 -0
  63. package/dist/services/ExposedApiRegistry/serviceModule.d.ts +10 -0
  64. package/dist/services/ExposedApiRegistry/serviceModule.js +20 -0
  65. package/dist/services/ExposedApiRegistry/storage/protection.d.ts +21 -0
  66. package/dist/services/ExposedApiRegistry/storage/protection.js +35 -0
  67. package/dist/services/ExposedApiRegistry/storage/scopeRecord.d.ts +45 -0
  68. package/dist/services/ExposedApiRegistry/storage/scopeRecord.js +178 -0
  69. package/dist/services/IdentityRegistry/contracts/errors.d.ts +88 -0
  70. package/dist/services/IdentityRegistry/contracts/errors.js +128 -0
  71. package/dist/services/IdentityRegistry/contracts/index.d.ts +5 -0
  72. package/dist/services/IdentityRegistry/contracts/index.js +3 -0
  73. package/dist/services/IdentityRegistry/contracts/types.d.ts +77 -0
  74. package/dist/services/IdentityRegistry/contracts/types.js +18 -0
  75. package/dist/services/IdentityRegistry/descriptor/descriptor.d.ts +36 -0
  76. package/dist/services/IdentityRegistry/descriptor/descriptor.js +44 -0
  77. package/dist/services/IdentityRegistry/descriptor/tokens.d.ts +8 -0
  78. package/dist/services/IdentityRegistry/descriptor/tokens.js +8 -0
  79. package/dist/services/IdentityRegistry/index.d.ts +11 -0
  80. package/dist/services/IdentityRegistry/index.js +6 -0
  81. package/dist/services/IdentityRegistry/orchestrator/IdentityRegistryService.d.ts +47 -0
  82. package/dist/services/IdentityRegistry/orchestrator/IdentityRegistryService.js +240 -0
  83. package/dist/services/IdentityRegistry/serviceModule.d.ts +10 -0
  84. package/dist/services/IdentityRegistry/serviceModule.js +24 -0
  85. package/dist/services/IdentityRegistry/storage/registryDocument.d.ts +55 -0
  86. package/dist/services/IdentityRegistry/storage/registryDocument.js +235 -0
  87. package/dist/services/Interception/contracts/IInterceptionService.d.ts +155 -0
  88. package/dist/services/Interception/contracts/IInterceptionService.js +12 -0
  89. package/dist/services/Interception/contracts/errors.d.ts +26 -0
  90. package/dist/services/Interception/contracts/errors.js +35 -0
  91. package/dist/services/Interception/contracts/index.d.ts +3 -0
  92. package/dist/services/Interception/contracts/index.js +3 -0
  93. package/dist/services/Interception/contracts/tokens.d.ts +2 -0
  94. package/dist/services/Interception/contracts/tokens.js +2 -0
  95. package/dist/services/Interception/descriptor/InterceptionDescriptor.d.ts +9 -0
  96. package/dist/services/Interception/descriptor/InterceptionDescriptor.js +50 -0
  97. package/dist/services/Interception/descriptor/tokens.d.ts +1 -0
  98. package/dist/services/Interception/descriptor/tokens.js +1 -0
  99. package/dist/services/Interception/index.d.ts +4 -0
  100. package/dist/services/Interception/index.js +4 -0
  101. package/dist/services/Interception/orchestrator/InterceptionService.d.ts +40 -0
  102. package/dist/services/Interception/orchestrator/InterceptionService.js +343 -0
  103. package/dist/services/Interception/orchestrator/index.d.ts +1 -0
  104. package/dist/services/Interception/orchestrator/index.js +1 -0
  105. package/dist/services/Interception/serviceModule.d.ts +2 -0
  106. package/dist/services/Interception/serviceModule.js +5 -0
  107. package/dist/services/Parser/contracts/IParserService.d.ts +21 -0
  108. package/dist/services/Parser/contracts/IParserService.js +1 -0
  109. package/dist/services/Parser/contracts/errors.d.ts +25 -0
  110. package/dist/services/Parser/contracts/errors.js +33 -0
  111. package/dist/services/Parser/contracts/index.d.ts +4 -0
  112. package/dist/services/Parser/contracts/index.js +1 -0
  113. package/dist/services/Parser/contracts/types.d.ts +42 -0
  114. package/dist/services/Parser/contracts/types.js +1 -0
  115. package/dist/services/Parser/descriptor/ParserDescriptor.d.ts +23 -0
  116. package/dist/services/Parser/descriptor/ParserDescriptor.js +50 -0
  117. package/dist/services/Parser/descriptor/tokens.d.ts +8 -0
  118. package/dist/services/Parser/descriptor/tokens.js +8 -0
  119. package/dist/services/Parser/index.d.ts +8 -0
  120. package/dist/services/Parser/index.js +8 -0
  121. package/dist/services/Parser/orchestrator/ParserService.d.ts +20 -0
  122. package/dist/services/Parser/orchestrator/ParserService.js +105 -0
  123. package/dist/services/Parser/orchestrator/index.d.ts +1 -0
  124. package/dist/services/Parser/orchestrator/index.js +1 -0
  125. package/dist/services/Parser/parser/indexSynthesis.d.ts +11 -0
  126. package/dist/services/Parser/parser/indexSynthesis.js +13 -0
  127. package/dist/services/Parser/parser/parameters.d.ts +14 -0
  128. package/dist/services/Parser/parser/parameters.js +89 -0
  129. package/dist/services/Parser/parser/scanner.d.ts +48 -0
  130. package/dist/services/Parser/parser/scanner.js +156 -0
  131. package/dist/services/Parser/parser/syntax.d.ts +30 -0
  132. package/dist/services/Parser/parser/syntax.js +34 -0
  133. package/dist/services/Parser/serviceModule.d.ts +2 -0
  134. package/dist/services/Parser/serviceModule.js +5 -0
  135. package/dist/services/Router/bootOptions.d.ts +2 -0
  136. package/dist/services/Router/bootOptions.js +2 -0
  137. package/dist/services/Router/contracts/IRouterService.d.ts +43 -0
  138. package/dist/services/Router/contracts/IRouterService.js +1 -0
  139. package/dist/services/Router/contracts/caller.d.ts +6 -0
  140. package/dist/services/Router/contracts/caller.js +6 -0
  141. package/dist/services/Router/contracts/errors.d.ts +67 -0
  142. package/dist/services/Router/contracts/errors.js +90 -0
  143. package/dist/services/Router/contracts/index.d.ts +8 -0
  144. package/dist/services/Router/contracts/index.js +4 -0
  145. package/dist/services/Router/contracts/kind.d.ts +7 -0
  146. package/dist/services/Router/contracts/kind.js +10 -0
  147. package/dist/services/Router/contracts/registrant.d.ts +23 -0
  148. package/dist/services/Router/contracts/registrant.js +13 -0
  149. package/dist/services/Router/contracts/types.d.ts +110 -0
  150. package/dist/services/Router/contracts/types.js +1 -0
  151. package/dist/services/Router/descriptor/RouterDescriptor.d.ts +32 -0
  152. package/dist/services/Router/descriptor/RouterDescriptor.js +92 -0
  153. package/dist/services/Router/descriptor/tokens.d.ts +9 -0
  154. package/dist/services/Router/descriptor/tokens.js +9 -0
  155. package/dist/services/Router/index.d.ts +4 -0
  156. package/dist/services/Router/index.js +4 -0
  157. package/dist/services/Router/orchestrator/RouterService.d.ts +93 -0
  158. package/dist/services/Router/orchestrator/RouterService.js +457 -0
  159. package/dist/services/Router/orchestrator/index.d.ts +2 -0
  160. package/dist/services/Router/orchestrator/index.js +1 -0
  161. package/dist/services/Router/serviceModule.d.ts +4 -0
  162. package/dist/services/Router/serviceModule.js +22 -0
  163. package/dist/services/WasmCage/index.d.ts +7 -0
  164. package/dist/services/WasmCage/index.js +4 -0
  165. package/dist/services/WasmCage/scripts/prepin.d.ts +1 -0
  166. package/dist/services/WasmCage/scripts/prepin.js +38 -0
  167. package/dist/services/WasmCage/serviceModule.d.ts +12 -0
  168. package/dist/services/WasmCage/serviceModule.js +18 -0
  169. package/dist/services/WasmCage/src/caged.d.ts +121 -0
  170. package/dist/services/WasmCage/src/caged.js +460 -0
  171. package/dist/services/WasmCage/src/descriptor.d.ts +47 -0
  172. package/dist/services/WasmCage/src/descriptor.js +56 -0
  173. package/dist/services/WasmCage/src/native.d.ts +29 -0
  174. package/dist/services/WasmCage/src/native.js +66 -0
  175. package/dist/services/WasmCage/src/nativeDigests.d.ts +9 -0
  176. package/dist/services/WasmCage/src/nativeDigests.js +15 -0
  177. package/dist/services/WasmCage/src/package.d.ts +36 -0
  178. package/dist/services/WasmCage/src/package.js +122 -0
  179. package/dist/services/shared/canonicalJson.d.ts +29 -0
  180. package/dist/services/shared/canonicalJson.js +93 -0
  181. package/dist/services/shared/documentStore.d.ts +16 -0
  182. package/dist/services/shared/documentStore.js +98 -0
  183. package/dist/src/bootstrap/bootKernelInternal.d.ts +19 -0
  184. package/dist/src/bootstrap/bootKernelInternal.js +633 -0
  185. package/dist/src/bootstrap/bootTypes.d.ts +113 -0
  186. package/dist/src/bootstrap/bootTypes.js +1 -0
  187. package/dist/src/bootstrap/config/index.d.ts +3 -0
  188. package/dist/src/bootstrap/config/index.js +2 -0
  189. package/dist/src/bootstrap/config/integrity.d.ts +45 -0
  190. package/dist/src/bootstrap/config/integrity.js +92 -0
  191. package/dist/src/bootstrap/config/parse.d.ts +15 -0
  192. package/dist/src/bootstrap/config/parse.js +81 -0
  193. package/dist/src/bootstrap/config/pathWriteGuard.d.ts +20 -0
  194. package/dist/src/bootstrap/config/pathWriteGuard.js +349 -0
  195. package/dist/src/bootstrap/config/types.d.ts +41 -0
  196. package/dist/src/bootstrap/config/types.js +15 -0
  197. package/dist/src/bootstrap/hardcore.d.ts +212 -0
  198. package/dist/src/bootstrap/hardcore.js +312 -0
  199. package/dist/src/bootstrap/hardcoreServices.d.ts +10 -0
  200. package/dist/src/bootstrap/hardcoreServices.js +258 -0
  201. package/dist/src/bootstrap/index.d.ts +13 -0
  202. package/dist/src/bootstrap/index.js +7 -0
  203. package/dist/src/bootstrap/loaded.d.ts +30 -0
  204. package/dist/src/bootstrap/loaded.js +45 -0
  205. package/dist/src/bootstrap/packageRequirements.d.ts +26 -0
  206. package/dist/src/bootstrap/packageRequirements.js +52 -0
  207. package/dist/src/bootstrap/reload.d.ts +44 -0
  208. package/dist/src/bootstrap/reload.js +45 -0
  209. package/dist/src/bootstrap/seal.d.ts +32 -0
  210. package/dist/src/bootstrap/seal.js +101 -0
  211. package/dist/src/contracts/accessBroker.d.ts +53 -0
  212. package/dist/src/contracts/accessBroker.js +1 -0
  213. package/dist/src/contracts/brudOperation.d.ts +20 -0
  214. package/dist/src/contracts/brudOperation.js +20 -0
  215. package/dist/src/contracts/callChain.d.ts +161 -0
  216. package/dist/src/contracts/callChain.js +19 -0
  217. package/dist/src/contracts/capability.d.ts +26 -0
  218. package/dist/src/contracts/capability.js +26 -0
  219. package/dist/src/contracts/capabilitySet.d.ts +23 -0
  220. package/dist/src/contracts/capabilitySet.js +72 -0
  221. package/dist/src/contracts/container.d.ts +17 -0
  222. package/dist/src/contracts/container.js +8 -0
  223. package/dist/src/contracts/context.d.ts +19 -0
  224. package/dist/src/contracts/context.js +1 -0
  225. package/dist/src/contracts/contextContract.d.ts +132 -0
  226. package/dist/src/contracts/contextContract.js +274 -0
  227. package/dist/src/contracts/dispatchView.d.ts +21 -0
  228. package/dist/src/contracts/dispatchView.js +1 -0
  229. package/dist/src/contracts/handler.d.ts +21 -0
  230. package/dist/src/contracts/handler.js +1 -0
  231. package/dist/src/contracts/index.d.ts +26 -0
  232. package/dist/src/contracts/index.js +9 -0
  233. package/dist/src/contracts/kernel.d.ts +60 -0
  234. package/dist/src/contracts/kernel.js +1 -0
  235. package/dist/src/contracts/lifecycle.d.ts +59 -0
  236. package/dist/src/contracts/lifecycle.js +1 -0
  237. package/dist/src/contracts/manifest.d.ts +180 -0
  238. package/dist/src/contracts/manifest.js +334 -0
  239. package/dist/src/contracts/pipeline.d.ts +8 -0
  240. package/dist/src/contracts/pipeline.js +1 -0
  241. package/dist/src/contracts/policy.d.ts +75 -0
  242. package/dist/src/contracts/policy.js +77 -0
  243. package/dist/src/contracts/registry.d.ts +7 -0
  244. package/dist/src/contracts/registry.js +1 -0
  245. package/dist/src/contracts/serviceDescriptor.d.ts +67 -0
  246. package/dist/src/contracts/serviceDescriptor.js +1 -0
  247. package/dist/src/contracts/serviceToken.d.ts +36 -0
  248. package/dist/src/contracts/serviceToken.js +16 -0
  249. package/dist/src/engine/BrudKernel.d.ts +120 -0
  250. package/dist/src/engine/BrudKernel.js +395 -0
  251. package/dist/src/engine/ExecutionPipeline.d.ts +13 -0
  252. package/dist/src/engine/ExecutionPipeline.js +62 -0
  253. package/dist/src/engine/KernelAccessBroker.d.ts +47 -0
  254. package/dist/src/engine/KernelAccessBroker.js +107 -0
  255. package/dist/src/engine/KernelRegistry.d.ts +14 -0
  256. package/dist/src/engine/KernelRegistry.js +33 -0
  257. package/dist/src/engine/OperationRegistry.d.ts +9 -0
  258. package/dist/src/engine/OperationRegistry.js +18 -0
  259. package/dist/src/engine/ServiceContainer.d.ts +13 -0
  260. package/dist/src/engine/ServiceContainer.js +53 -0
  261. package/dist/src/engine/callChain.d.ts +68 -0
  262. package/dist/src/engine/callChain.js +175 -0
  263. package/dist/src/engine/dag.d.ts +2 -0
  264. package/dist/src/engine/dag.js +34 -0
  265. package/dist/src/engine/errors.d.ts +26 -0
  266. package/dist/src/engine/errors.js +52 -0
  267. package/dist/src/engine/index.d.ts +11 -0
  268. package/dist/src/engine/index.js +8 -0
  269. package/dist/src/engine/state.d.ts +1 -0
  270. package/dist/src/engine/state.js +1 -0
  271. package/dist/src/validation/jsonSchema/compile.d.ts +8 -0
  272. package/dist/src/validation/jsonSchema/compile.js +386 -0
  273. package/dist/src/validation/jsonSchema/index.d.ts +3 -0
  274. package/dist/src/validation/jsonSchema/index.js +2 -0
  275. package/dist/src/validation/jsonSchema/jsonValue.d.ts +29 -0
  276. package/dist/src/validation/jsonSchema/jsonValue.js +190 -0
  277. package/dist/src/validation/jsonSchema/pattern.d.ts +28 -0
  278. package/dist/src/validation/jsonSchema/pattern.js +155 -0
  279. package/dist/src/validation/jsonSchema/types.d.ts +71 -0
  280. package/dist/src/validation/jsonSchema/types.js +15 -0
  281. package/package.json +59 -4
  282. package/services/WasmCage/native/brud-wasm-cage.darwin-arm64.node +0 -0
  283. package/services/WasmCage/native/brud-wasm-cage.darwin-x64.node +0 -0
  284. package/services/WasmCage/native/brud-wasm-cage.linux-arm64-gnu.node +0 -0
  285. package/services/WasmCage/native/brud-wasm-cage.linux-x64-gnu.node +0 -0
  286. package/services/WasmCage/native/brud-wasm-cage.win32-x64-msvc.node +0 -0
@@ -0,0 +1,457 @@
1
+ import { evaluateExposedApiPolicy, parseExposedApiPolicy, } from '../../ExposedApiRegistry/contracts/policy.js';
2
+ import { isValidRouterKind } from '../contracts/kind.js';
3
+ import { RouterDisposedError, RouterInvalidDeclarationError, RouterInvalidKindError, RouterKindCollisionError, RouterRegistrationClosedError, } from '../contracts/errors.js';
4
+ /**
5
+ * Level-1 Brud kind Router.
6
+ *
7
+ * The registration window is open only while the kernel is booting. The
8
+ * standard composition calls {@link closeRegistration} at the end of boot,
9
+ * before the kernel announces `RUNNING`; the `onRunning` hook is a second
10
+ * guard for alternate compositions.
11
+ *
12
+ * Dispatch resolves every target through the access broker with the token the
13
+ * owning service supplied, so access policies still apply and only the
14
+ * owning service can be a routing target.
15
+ */
16
+ export class RouterService {
17
+ registrations = new Map();
18
+ /** The parsed permission of every declared operation, by kind then operation. Frozen at registration. */
19
+ permissions = new Map();
20
+ /** The one permission of every kind whose owner defines its operation names. */
21
+ openKinds = new Map();
22
+ authority;
23
+ dependencies;
24
+ parser;
25
+ closed = false;
26
+ disposed = false;
27
+ /**
28
+ * @param dependencies - Broker, router identity, and lazy parser resolution.
29
+ */
30
+ constructor(dependencies) {
31
+ this.dependencies = dependencies;
32
+ }
33
+ /** Lifecycle no-op: registrations arrive from the boot pinner. */
34
+ /** Binds the kernel call chain (hardcore boot). */
35
+ bindAuthority(value) {
36
+ this.authority = value;
37
+ }
38
+ /** The kernel-derived caller of the current chain, if any. */
39
+ currentCaller() {
40
+ const context = this.authority?.current();
41
+ if (context === undefined) {
42
+ return undefined;
43
+ }
44
+ return Object.freeze({
45
+ identity: context.caller.identity,
46
+ callerClass: context.caller.callerClass,
47
+ official: context.caller.official,
48
+ capabilities: context.caller.capabilities,
49
+ origin: context.origin,
50
+ });
51
+ }
52
+ async initialize() {
53
+ return undefined;
54
+ }
55
+ /** Seals the window even when no boot pinner ran. */
56
+ async onRunning() {
57
+ this.closeRegistration();
58
+ }
59
+ /** Clears every claim and refuses later routing. */
60
+ async dispose() {
61
+ this.disposed = true;
62
+ this.closed = true;
63
+ this.registrations.clear();
64
+ }
65
+ /** Whether {@link dispose} has run. */
66
+ get isDisposed() {
67
+ return this.disposed;
68
+ }
69
+ /**
70
+ * Registers a kind a participant declared in `manifest.kinds` (hardcore
71
+ * boot). The owner's kernel identity is pinned, so only that participant's
72
+ * exposure can serve the kind.
73
+ */
74
+ registerDeclared(owner, declaration) {
75
+ this.register({
76
+ kind: declaration.kind,
77
+ exposure: declaration.exposure,
78
+ ...(declaration.version === undefined ? {} : { version: Number(declaration.version) }),
79
+ ...(declaration.description === undefined ? {} : { description: declaration.description }),
80
+ operations: declaration.operations,
81
+ ...(declaration.permission === undefined ? {} : { permission: declaration.permission }),
82
+ }, owner);
83
+ }
84
+ register(registration, owner) {
85
+ if (this.disposed) {
86
+ throw new RouterDisposedError();
87
+ }
88
+ if (this.closed) {
89
+ throw new RouterRegistrationClosedError(registration.kind);
90
+ }
91
+ if (!isValidRouterKind(registration.kind)) {
92
+ throw new RouterInvalidKindError(registration.kind);
93
+ }
94
+ const existing = this.registrations.get(registration.kind);
95
+ if (existing !== undefined) {
96
+ throw new RouterKindCollisionError(registration.kind, existing.exposure);
97
+ }
98
+ if (typeof registration.exposure !== 'string' || registration.exposure.length === 0) {
99
+ throw new RouterInvalidKindError(registration.kind);
100
+ }
101
+ const compiled = this.compilePermissions(registration);
102
+ this.registrations.set(registration.kind, Object.freeze({ ...registration, ...(owner === undefined ? {} : { owner }) }));
103
+ if (compiled instanceof Map) {
104
+ this.permissions.set(registration.kind, compiled);
105
+ }
106
+ else {
107
+ this.openKinds.set(registration.kind, compiled);
108
+ }
109
+ }
110
+ /**
111
+ * Builds the operation registry for one kind: every operation must be
112
+ * declared and carry a valid permission, its own or the kind's. Refuses the
113
+ * declaration, and so the boot, otherwise.
114
+ */
115
+ compilePermissions(registration) {
116
+ const refuse = (reason) => {
117
+ throw new RouterInvalidDeclarationError(registration.kind, reason);
118
+ };
119
+ const operations = registration.operations;
120
+ if (operations === 'open') {
121
+ if (registration.permission === undefined) {
122
+ return refuse('an open operation vocabulary needs a kind-level permission');
123
+ }
124
+ }
125
+ else if (typeof operations !== 'object' || operations === null || Array.isArray(operations) || Object.keys(operations).length === 0) {
126
+ return refuse('it must declare every operation it serves');
127
+ }
128
+ const parse = (value, where) => {
129
+ let policy;
130
+ try {
131
+ policy = parseExposedApiPolicy(value);
132
+ }
133
+ catch (error) {
134
+ return refuse(`${where} permission is invalid (${describe(error)})`);
135
+ }
136
+ if (policy.parentCallerClasses !== undefined) {
137
+ return refuse(`${where} permission cannot use parentCallerClasses: the Router does not know the caller's parent`);
138
+ }
139
+ return policy;
140
+ };
141
+ const fallback = registration.permission === undefined ? undefined : parse(registration.permission, 'the kind');
142
+ if (registration.operations === 'open') {
143
+ return fallback;
144
+ }
145
+ const compiled = new Map();
146
+ for (const [name, operation] of Object.entries(registration.operations)) {
147
+ const own = operation.permission === undefined ? undefined : parse(operation.permission, `operation "${name}"`);
148
+ const effective = own ?? fallback;
149
+ if (effective === undefined) {
150
+ return refuse(`operation "${name}" has no permission and the kind declares none`);
151
+ }
152
+ compiled.set(name, effective);
153
+ }
154
+ return compiled;
155
+ }
156
+ closeRegistration() {
157
+ this.closed = true;
158
+ }
159
+ isRegistrationOpen() {
160
+ return !this.closed && !this.disposed;
161
+ }
162
+ registeredKinds() {
163
+ return Object.freeze(Array.from(this.registrations.keys()));
164
+ }
165
+ /**
166
+ * The public vocabulary: every registered kind with its declared
167
+ * description and operations. What lives inside a kind is its owner's
168
+ * business (an optional `describe` operation by convention).
169
+ */
170
+ kinds() {
171
+ return Object.freeze(Array.from(this.registrations.values(), (registration) => Object.freeze({
172
+ kind: registration.kind,
173
+ ...(registration.description === undefined ? {} : { description: registration.description }),
174
+ ...(registration.operations === 'open' ? { open: true } : { operations: Object.freeze(Object.fromEntries(Object.entries(registration.operations).map(([name, operation]) => [
175
+ name,
176
+ Object.freeze({
177
+ ...(operation.description === undefined ? {} : { description: operation.description }),
178
+ ...(operation.input === undefined ? {} : { input: operation.input }),
179
+ }),
180
+ ]))) }),
181
+ })));
182
+ }
183
+ async route(rawInput, options = {}, caller) {
184
+ if (this.disposed) {
185
+ return this.failureResult(new RouterDisposedError());
186
+ }
187
+ const parsed = this.resolveParser().parse(rawInput, options.maxOperations === undefined
188
+ ? {}
189
+ : { maxOperations: options.maxOperations });
190
+ if (!parsed.ok) {
191
+ return {
192
+ status: 'failed',
193
+ operations: Object.freeze([]),
194
+ parseError: parsed.error,
195
+ dispatched: 0,
196
+ failed: 0,
197
+ };
198
+ }
199
+ return this.dispatch(parsed.operations, options, caller);
200
+ }
201
+ async dispatch(operations, options = {}, caller) {
202
+ if (this.disposed) {
203
+ return this.failureResult(new RouterDisposedError());
204
+ }
205
+ const routeCaller = caller ?? this.currentCaller();
206
+ if (routeCaller === undefined) {
207
+ return this.failureResult({ code: 'ROUTE_NO_CALL_CONTEXT', message: 'routing requires a kernel call chain' });
208
+ }
209
+ const context = this.buildDispatchContext(options, routeCaller);
210
+ const stopOnFailure = options.stopOnFailure === true;
211
+ const outcomes = [];
212
+ let dispatched = 0;
213
+ let failed = 0;
214
+ for (let sequence = 0; sequence < operations.length; sequence += 1) {
215
+ const operation = operations[sequence];
216
+ if (options.signal?.aborted === true) {
217
+ appendRemaining(outcomes, operations, sequence, 'aborted');
218
+ break;
219
+ }
220
+ const registration = this.registrations.get(operation.kind);
221
+ if (registration === undefined) {
222
+ failed += 1;
223
+ outcomes.push({
224
+ operationIndex: operation.index,
225
+ sequence,
226
+ kind: operation.kind,
227
+ name: operation.name,
228
+ status: 'kind_not_registered',
229
+ error: `KIND_NOT_REGISTERED: kind "${operation.kind}" has no registered service`,
230
+ });
231
+ if (stopOnFailure) {
232
+ appendRemaining(outcomes, operations, sequence + 1, 'skipped');
233
+ break;
234
+ }
235
+ continue;
236
+ }
237
+ const refusal = this.refuseBeforeOwner(registration.kind, operation, routeCaller);
238
+ if (refusal !== undefined) {
239
+ failed += 1;
240
+ outcomes.push({
241
+ operationIndex: operation.index,
242
+ sequence,
243
+ kind: operation.kind,
244
+ name: operation.name,
245
+ status: refusal.status,
246
+ error: refusal.error,
247
+ });
248
+ if (stopOnFailure) {
249
+ appendRemaining(outcomes, operations, sequence + 1, 'skipped');
250
+ break;
251
+ }
252
+ continue;
253
+ }
254
+ let result;
255
+ try {
256
+ // The access broker is the only resolution path: the registered token
257
+ // decides the target and its access policy still applies.
258
+ const owner = this.dependencies.resolveOwner(registration.exposure, registration.version, registration.owner);
259
+ if (owner === null) {
260
+ throw new Error(`KIND_OWNER_UNAVAILABLE: exposure "${registration.exposure}" for kind "${operation.kind}" is not resolvable`);
261
+ }
262
+ result = await owner.dispatch(operation, context);
263
+ }
264
+ catch (error) {
265
+ failed += 1;
266
+ outcomes.push({
267
+ operationIndex: operation.index,
268
+ sequence,
269
+ kind: operation.kind,
270
+ name: operation.name,
271
+ status: 'failed',
272
+ error: `ROUTE_DISPATCH_ERROR: ${describe(error)}`,
273
+ });
274
+ if (stopOnFailure) {
275
+ appendRemaining(outcomes, operations, sequence + 1, 'skipped');
276
+ break;
277
+ }
278
+ continue;
279
+ }
280
+ if (!options.dryRun) {
281
+ dispatched += 1;
282
+ }
283
+ if (!isValidOperationResult(result)) {
284
+ failed += 1;
285
+ const error = 'HANDLER_INVALID_RESULT: handler returned an invalid result shape';
286
+ outcomes.push({
287
+ operationIndex: operation.index,
288
+ sequence,
289
+ kind: operation.kind,
290
+ name: operation.name,
291
+ status: 'failed',
292
+ error,
293
+ result: { status: 'failed', error },
294
+ });
295
+ if (stopOnFailure) {
296
+ appendRemaining(outcomes, operations, sequence + 1, 'skipped');
297
+ break;
298
+ }
299
+ continue;
300
+ }
301
+ outcomes.push({
302
+ operationIndex: operation.index,
303
+ sequence,
304
+ kind: operation.kind,
305
+ name: operation.name,
306
+ status: result.status,
307
+ result,
308
+ });
309
+ if (result.status === 'failed') {
310
+ failed += 1;
311
+ if (stopOnFailure) {
312
+ appendRemaining(outcomes, operations, sequence + 1, 'skipped');
313
+ break;
314
+ }
315
+ }
316
+ }
317
+ return {
318
+ status: aggregateStatus(outcomes),
319
+ operations: Object.freeze(outcomes),
320
+ dispatched,
321
+ failed,
322
+ };
323
+ }
324
+ /**
325
+ * The operation registry check, run before the owner is resolved: the
326
+ * operation must be declared for its kind, and the caller must hold the
327
+ * operation's permission. The reason a permission is denied goes to the
328
+ * observation stream, never to the caller.
329
+ */
330
+ refuseBeforeOwner(kind, operation, caller) {
331
+ const policy = this.openKinds.get(kind) ?? this.permissions.get(kind)?.get(operation.name);
332
+ if (policy === undefined) {
333
+ this.observeRefusal('operation-not-declared', kind, operation.name, caller, undefined);
334
+ return {
335
+ status: 'operation_not_declared',
336
+ error: `OPERATION_NOT_DECLARED: kind "${kind}" does not declare operation "${operation.name}"`,
337
+ };
338
+ }
339
+ const decision = evaluateExposedApiPolicy(policy, 'any', {
340
+ origin: caller.origin,
341
+ caller: {
342
+ callerClass: caller.callerClass,
343
+ identity: caller.identity,
344
+ official: caller.official,
345
+ capabilities: caller.capabilities,
346
+ },
347
+ });
348
+ if (decision.allowed) {
349
+ return undefined;
350
+ }
351
+ this.observeRefusal('permission-denied', kind, operation.name, caller, decision.reason);
352
+ return {
353
+ status: 'permission_denied',
354
+ error: `PERMISSION_DENIED: the caller may not call operation "${operation.name}" of kind "${kind}"`,
355
+ };
356
+ }
357
+ observeRefusal(event, kind, name, caller, reason) {
358
+ this.authority?.observe('router', event, {
359
+ kind,
360
+ operation: name,
361
+ ...(reason === undefined ? {} : { reason }),
362
+ callerIdentity: caller.identity,
363
+ callerClass: caller.callerClass,
364
+ origin: caller.origin,
365
+ });
366
+ }
367
+ resolveParser() {
368
+ this.parser ??= this.dependencies.getParser();
369
+ return this.parser;
370
+ }
371
+ buildDispatchContext(options, caller) {
372
+ return Object.freeze({
373
+ caller,
374
+ ...(options.signal === undefined ? {} : { signal: options.signal }),
375
+ ...(options.dryRun === true ? { dryRun: true } : {}),
376
+ });
377
+ }
378
+ failureResult(error) {
379
+ const parseError = {
380
+ code: error.code,
381
+ friendly: error.message,
382
+ details: error.message,
383
+ };
384
+ return {
385
+ status: 'failed',
386
+ operations: Object.freeze([]),
387
+ parseError,
388
+ dispatched: 0,
389
+ failed: 0,
390
+ };
391
+ }
392
+ }
393
+ /** Appends outcomes for operations that were never attempted. */
394
+ function appendRemaining(outcomes, operations, from, status) {
395
+ for (let sequence = from; sequence < operations.length; sequence += 1) {
396
+ const operation = operations[sequence];
397
+ outcomes.push({
398
+ operationIndex: operation.index,
399
+ sequence,
400
+ kind: operation.kind,
401
+ name: operation.name,
402
+ status,
403
+ });
404
+ }
405
+ }
406
+ /** Aggregates per-operation outcomes into one document status. */
407
+ function aggregateStatus(outcomes) {
408
+ if (outcomes.length === 0) {
409
+ return 'empty';
410
+ }
411
+ const successes = outcomes.filter((outcome) => outcome.status === 'success').length;
412
+ if (successes === outcomes.length) {
413
+ return 'success';
414
+ }
415
+ if (successes === 0) {
416
+ return 'failed';
417
+ }
418
+ return 'partial';
419
+ }
420
+ /** Renders an unknown thrown value for a diagnostic message. */
421
+ function describe(error) {
422
+ if (error instanceof Error && 'code' in error) {
423
+ const code = error.code;
424
+ if (typeof code === 'string') {
425
+ return `${code}: ${error.message}`;
426
+ }
427
+ }
428
+ return error instanceof Error ? error.message : String(error);
429
+ }
430
+ /** Whether a handler returned a structurally valid {@link OperationResult}. */
431
+ function isValidOperationResult(value) {
432
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
433
+ return false;
434
+ }
435
+ const candidate = value;
436
+ // The OperationResult contract allows all four statuses; a cancelled
437
+ // operation reports `aborted`, and that status is passed through unchanged.
438
+ if (candidate.status !== 'success'
439
+ && candidate.status !== 'failed'
440
+ && candidate.status !== 'aborted'
441
+ && candidate.status !== 'skipped') {
442
+ return false;
443
+ }
444
+ if (candidate.message !== undefined && typeof candidate.message !== 'string') {
445
+ return false;
446
+ }
447
+ if (candidate.error !== undefined
448
+ && typeof candidate.error !== 'string'
449
+ && !(candidate.error instanceof Error)) {
450
+ return false;
451
+ }
452
+ if (candidate.durationMs !== undefined
453
+ && (typeof candidate.durationMs !== 'number' || !Number.isFinite(candidate.durationMs))) {
454
+ return false;
455
+ }
456
+ return true;
457
+ }
@@ -0,0 +1,2 @@
1
+ export { RouterService } from './RouterService.js';
2
+ export type { RouterDependencies } from './RouterService.js';
@@ -0,0 +1 @@
1
+ export { RouterService } from './RouterService.js';
@@ -0,0 +1,4 @@
1
+ import type { KernelServiceModuleContext } from '../../src/bootstrap/bootTypes.js';
2
+ import type { IRouterService } from './contracts/IRouterService.js';
3
+ export declare function createService(context: KernelServiceModuleContext): import("../../index.js").ServiceDescriptor<IRouterService, import("./index.js").RouterService>;
4
+ export declare function registerBootRegistrants(context: KernelServiceModuleContext, instances: readonly object[]): void;
@@ -0,0 +1,22 @@
1
+ import { createRouterDescriptor } from './descriptor/RouterDescriptor.js';
2
+ import { isRouterRegistrant } from './contracts/registrant.js';
3
+ const ROUTER_SLOT = 'brud.core.router.instance';
4
+ export function createService(context) {
5
+ return createRouterDescriptor({
6
+ onConstruct: (service) => {
7
+ context.shared[ROUTER_SLOT] = service;
8
+ },
9
+ });
10
+ }
11
+ export function registerBootRegistrants(context, instances) {
12
+ const router = context.shared[ROUTER_SLOT];
13
+ if (router === undefined) {
14
+ return;
15
+ }
16
+ for (const instance of instances) {
17
+ if (isRouterRegistrant(instance)) {
18
+ instance.registerWithRouter(router);
19
+ }
20
+ }
21
+ router.closeRegistration();
22
+ }
@@ -0,0 +1,7 @@
1
+ export { WASM_CAGE_CONTRACT_METHODS, WASM_CAGE_CONTRACT_VERSION, WASM_CAGE_SERVICE_TIER, WasmCageToken, createWasmCageDescriptor, } from './src/descriptor.js';
2
+ export type { IWasmCage } from './src/descriptor.js';
3
+ export { CagedGuestError, DEFAULT_CAGED_LIMITS, createCagedParticipant, } from './src/caged.js';
4
+ export type { CagedParticipantLimits, CagedParticipantOptions } from './src/caged.js';
5
+ export { CageNativeError, nativePlatformKey } from './src/native.js';
6
+ export type { CageNativeErrorCode } from './src/native.js';
7
+ export { PARTICIPANT_PACKAGE_FORMAT, ParticipantPackageError, loadParticipantPackage } from './src/package.js';
@@ -0,0 +1,4 @@
1
+ export { WASM_CAGE_CONTRACT_METHODS, WASM_CAGE_CONTRACT_VERSION, WASM_CAGE_SERVICE_TIER, WasmCageToken, createWasmCageDescriptor, } from './src/descriptor.js';
2
+ export { CagedGuestError, DEFAULT_CAGED_LIMITS, createCagedParticipant, } from './src/caged.js';
3
+ export { CageNativeError, nativePlatformKey } from './src/native.js';
4
+ export { PARTICIPANT_PACKAGE_FORMAT, ParticipantPackageError, loadParticipantPackage } from './src/package.js';
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,38 @@
1
+ // The cage's pre-pin hook, run by packages/kernel/scripts/repin.ts before the
2
+ // hardcore pins are computed. Regenerates src/nativeDigests.ts from the
3
+ // binaries in native/; with --check, fails if the table is stale.
4
+ import { createHash } from 'node:crypto';
5
+ import { readdirSync, readFileSync, writeFileSync } from 'node:fs';
6
+ import { dirname, join } from 'node:path';
7
+ import { fileURLToPath } from 'node:url';
8
+ const cage = join(dirname(fileURLToPath(import.meta.url)), '..');
9
+ const nativeDir = join(cage, 'native');
10
+ const table = join(cage, 'src/nativeDigests.ts');
11
+ const entries = readdirSync(nativeDir)
12
+ .filter((name) => /^brud-wasm-cage\..+\.node$/u.test(name))
13
+ .sort()
14
+ .map((name) => {
15
+ const key = name.slice('brud-wasm-cage.'.length, -'.node'.length);
16
+ const digest = createHash('sha256').update(readFileSync(join(nativeDir, name))).digest('hex');
17
+ return ` ['${key}', '${digest}'],`;
18
+ });
19
+ const next = `/**
20
+ * SHA-256 digests of the cage's native binaries, as \`[platform key, digest]\`.
21
+ *
22
+ * Generated by \`services/WasmCage/scripts/prepin.ts\` (run through the kernel's
23
+ * \`npm run repin\`); do not edit by hand. The table is part of the cage's pinned
24
+ * package graph, so the hardcore content pins cover it; the loader refuses any
25
+ * binary whose digest is not listed here.
26
+ */
27
+ export const WASM_CAGE_NATIVE_DIGESTS: readonly (readonly [string, string])[] = Object.freeze([
28
+ ${entries.join('\n')}
29
+ ]);
30
+ `;
31
+ if (readFileSync(table, 'utf8') !== next) {
32
+ if (process.argv.includes('--check')) {
33
+ console.error('native digests: stale');
34
+ process.exit(1);
35
+ }
36
+ writeFileSync(table, next, 'utf8');
37
+ console.log('native digests: updated');
38
+ }
@@ -0,0 +1,12 @@
1
+ import type { KernelServiceModuleContext } from '../../src/bootstrap/bootTypes.js';
2
+ export { nativePlatformKey } from './src/native.js';
3
+ export { loadParticipantPackage } from './src/package.js';
4
+ /**
5
+ * Composes the WASM cage through the standard service-module contract.
6
+ *
7
+ * The module has no context fields of its own at this step: the cage's
8
+ * configuration (resource profile, the loader capability) lands in a later
9
+ * plan step. The module returns the descriptor the compiled hardcore list
10
+ * verifies and the kernel registers.
11
+ */
12
+ export declare function createService(_context: KernelServiceModuleContext): import("../../index.js").ServiceDescriptor<import("./src/descriptor.js").IWasmCage, import("./src/descriptor.js").IWasmCage>;
@@ -0,0 +1,18 @@
1
+ import { createWasmCageDescriptor } from './src/descriptor.js';
2
+ // The native loader and its pinned digest table are part of the cage's package
3
+ // graph, so the hardcore content pins cover the digests the loader trusts.
4
+ export { nativePlatformKey } from './src/native.js';
5
+ // W-01: participants not declared official reach the kernel as packages, and
6
+ // this hardcore is the one that loads them (the kernel's loader asks for it).
7
+ export { loadParticipantPackage } from './src/package.js';
8
+ /**
9
+ * Composes the WASM cage through the standard service-module contract.
10
+ *
11
+ * The module has no context fields of its own at this step: the cage's
12
+ * configuration (resource profile, the loader capability) lands in a later
13
+ * plan step. The module returns the descriptor the compiled hardcore list
14
+ * verifies and the kernel registers.
15
+ */
16
+ export function createService(_context) {
17
+ return createWasmCageDescriptor();
18
+ }