@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,452 @@
1
+ import { ExposedApiDisposedError, ExposedApiDuplicateExposureError, ExposedApiInvalidNameError, ExposedApiInvalidScopeError, ExposedApiInvalidVersionError, ExposedApiNameCollisionError, ExposedApiNotAttachedError, ExposedApiOwnerPinError, ExposedApiRegistrationClosedError, ExposedApiScopeNarrowingError, ExposedApiUnavailableError, } from '../contracts/errors.js';
2
+ import { evaluateExposedApiPolicy, parseExposedApiPolicy, } from '../contracts/policy.js';
3
+ import { isExposureProvider } from '../contracts/registrant.js';
4
+ import { EXPOSED_API_NAME_PATTERN, exposedApiMajor, exposedApiScopeRank, isExposedApiScope, isExposedApiVersion, } from '../contracts/types.js';
5
+ import { ExposedApiRegistryToken } from '../descriptor/tokens.js';
6
+ import { EXPOSED_API_SCOPE_RECORD_MAX_NAME_LENGTH, loadScopeRecord, persistScopeRecord, protectScopeRecord, releaseScopeRecordProtection, scopeRecordIdentityKey, } from '../storage/scopeRecord.js';
7
+ /** API names accept dotted, kebab, and snake segments. */
8
+ export { EXPOSED_API_NAME_PATTERN };
9
+ /** Source name the registry uses on the kernel's observation stream. */
10
+ const OBSERVATION_SOURCE = ExposedApiRegistryToken.id;
11
+ /**
12
+ * Resolves the caller's parent class from the frame's parent identity.
13
+ *
14
+ * The parent identity is the field the caller frame carries; its class is read
15
+ * from the kernel's admission record, never from the caller's request. A caller
16
+ * with no parent gets no `parentCallerClass`, and a policy that names
17
+ * `parentCallerClasses` refuses it.
18
+ */
19
+ function withParentCallerClass(authority, context) {
20
+ const parentIdentity = context.caller.parentIdentity;
21
+ if (parentIdentity === undefined) {
22
+ return context;
23
+ }
24
+ const parent = authority.callerForIdentity(parentIdentity);
25
+ return parent === undefined
26
+ ? context
27
+ : { ...context, parentCallerClass: parent.callerClass };
28
+ }
29
+ const REFUSAL_MESSAGES = Object.freeze({
30
+ 'no-call-context': 'no kernel call context: exposures are not attached yet (for example during synchronous create)',
31
+ 'registry-closed': 'the resolution primitive is closed',
32
+ 'limit-exceeded': 'the call or request size limit was exceeded',
33
+ 'malformed-request': 'the name or version is malformed',
34
+ unavailable: 'the exposure is not available to this caller (it does not exist, or policy does not admit this caller)',
35
+ });
36
+ /** Builds a refusal. Carries only what the refused caller already knows about itself. */
37
+ function refusalOf(code, context) {
38
+ return Object.freeze({
39
+ code,
40
+ message: REFUSAL_MESSAGES[code],
41
+ ...(context === undefined
42
+ ? {}
43
+ : { callerOrigin: context.origin, callerClass: context.caller.callerClass }),
44
+ });
45
+ }
46
+ /** Default projection: the giver's instance itself. */
47
+ function identityProjection(service) {
48
+ return service;
49
+ }
50
+ /** Builds the caller view a projection receives. */
51
+ function callerView(context) {
52
+ return Object.freeze({
53
+ identity: context.caller.identity,
54
+ callerClass: context.caller.callerClass,
55
+ official: context.caller.official,
56
+ capabilities: context.caller.capabilities,
57
+ origin: context.origin,
58
+ });
59
+ }
60
+ /**
61
+ * The resolution primitive.
62
+ *
63
+ * Holds no participant name, no participant token, no caller identifier, no
64
+ * caller list, and no policy of its own. It stores what each participant's
65
+ * manifest declares, keyed by the owner's kernel-issued identity, and
66
+ * evaluates each giver's declared Layer 1 policy against the kernel-provided
67
+ * execution context.
68
+ */
69
+ export class ExposedApiRegistryService {
70
+ #entries = new Map();
71
+ #owners = new Map();
72
+ #bindings = new Map();
73
+ /** The last refusal per call frame. Weak: an entry dies with its frame. */
74
+ #refusals = new WeakMap();
75
+ #scopes = new Map();
76
+ #storagePath;
77
+ #authority;
78
+ #pins = Object.freeze({});
79
+ #sealed = false;
80
+ #disposed = false;
81
+ #initialized = false;
82
+ #protectedRecord = false;
83
+ #dirty = false;
84
+ /** The only view a participant receives: `resolve` and `lastRefusal`. */
85
+ publicView;
86
+ constructor(dependencies = {}) {
87
+ this.#storagePath = dependencies.scopeRecordPath;
88
+ this.publicView = Object.freeze({
89
+ resolve: (name, options) => this.resolve(name, options),
90
+ lastRefusal: () => this.lastRefusal(),
91
+ });
92
+ }
93
+ /**
94
+ * Loads and integrity-checks the protected scope record.
95
+ *
96
+ * A missing record is the legitimate first-registration case. A corrupt
97
+ * record refuses boot. An existing record is made read-only for the runtime.
98
+ */
99
+ async initialize() {
100
+ if (this.#initialized) {
101
+ return;
102
+ }
103
+ this.#initialized = true;
104
+ if (this.#storagePath === undefined) {
105
+ return;
106
+ }
107
+ const loaded = loadScopeRecord(this.#storagePath);
108
+ if (loaded !== undefined) {
109
+ for (const record of loaded) {
110
+ this.#scopes.set(scopeRecordIdentityKey(record.name, record.version), record);
111
+ }
112
+ protectScopeRecord(this.#storagePath);
113
+ this.#protectedRecord = true;
114
+ }
115
+ }
116
+ /** Seals the window and persists the final scope record. */
117
+ async onRunning() {
118
+ this.seal();
119
+ if (this.#storagePath !== undefined && this.#dirty) {
120
+ this.#persist();
121
+ }
122
+ }
123
+ /** Releases the protected-path claim and forgets every exposure. */
124
+ async dispose() {
125
+ if (this.#storagePath !== undefined && this.#protectedRecord) {
126
+ releaseScopeRecordProtection(this.#storagePath);
127
+ this.#protectedRecord = false;
128
+ }
129
+ this.#disposed = true;
130
+ this.#sealed = true;
131
+ this.#entries.clear();
132
+ this.#owners.clear();
133
+ this.#bindings.clear();
134
+ }
135
+ /** Whether {@link dispose} has run. */
136
+ get isDisposed() {
137
+ return this.#disposed;
138
+ }
139
+ /** Whether the registration window is sealed. */
140
+ get isSealed() {
141
+ return this.#sealed;
142
+ }
143
+ /** Absolute scope-record path, when persistence is configured. */
144
+ get scopeRecordPath() {
145
+ return this.#storagePath;
146
+ }
147
+ /**
148
+ * Attaches the kernel's call authority and the composition's name pins.
149
+ *
150
+ * Called once by the kernel, during admission, before any registration.
151
+ *
152
+ * @param authority - The kernel's call authority.
153
+ * @param ownerPins - Exposure names pinned to an owner identity.
154
+ */
155
+ attachKernel(authority, ownerPins) {
156
+ if (this.#disposed) {
157
+ throw new ExposedApiDisposedError();
158
+ }
159
+ if (this.#sealed || this.#authority !== undefined) {
160
+ throw new ExposedApiRegistrationClosedError('kernel attachment');
161
+ }
162
+ this.#authority = authority;
163
+ this.#pins = Object.freeze({ ...ownerPins });
164
+ }
165
+ register(owner, declaration) {
166
+ const name = typeof declaration?.name === 'string' ? declaration.name : String(declaration?.name);
167
+ if (this.#disposed) {
168
+ throw new ExposedApiDisposedError();
169
+ }
170
+ if (this.#sealed) {
171
+ throw new ExposedApiRegistrationClosedError(name);
172
+ }
173
+ if (this.#authority === undefined) {
174
+ throw new ExposedApiNotAttachedError();
175
+ }
176
+ if (typeof declaration?.name !== 'string'
177
+ || declaration.name.length > EXPOSED_API_SCOPE_RECORD_MAX_NAME_LENGTH
178
+ || !EXPOSED_API_NAME_PATTERN.test(declaration.name)) {
179
+ throw new ExposedApiInvalidNameError(name);
180
+ }
181
+ if (typeof owner !== 'string' || owner.length === 0) {
182
+ throw new ExposedApiNotAttachedError();
183
+ }
184
+ const version = declaration.version ?? 1;
185
+ if (!isExposedApiVersion(version)) {
186
+ throw new ExposedApiInvalidVersionError(version);
187
+ }
188
+ const major = exposedApiMajor(version);
189
+ const requestedScope = declaration.scope ?? 'kernel';
190
+ if (!isExposedApiScope(requestedScope)) {
191
+ throw new ExposedApiInvalidScopeError(requestedScope);
192
+ }
193
+ const policy = parseExposedApiPolicy(declaration.policy);
194
+ const pinned = this.#pins[name];
195
+ if (pinned !== undefined && pinned !== owner) {
196
+ throw new ExposedApiOwnerPinError(name);
197
+ }
198
+ const nameOwner = this.#owners.get(name);
199
+ if (nameOwner !== undefined && nameOwner !== owner) {
200
+ throw new ExposedApiNameCollisionError(name, nameOwner);
201
+ }
202
+ const key = scopeRecordIdentityKey(name, major);
203
+ if (this.#entries.has(key)) {
204
+ throw new ExposedApiDuplicateExposureError(name, major);
205
+ }
206
+ const persisted = this.#scopes.get(key);
207
+ if (persisted !== undefined
208
+ && exposedApiScopeRank(requestedScope) < exposedApiScopeRank(persisted.scope)) {
209
+ throw new ExposedApiScopeNarrowingError(name, String(major), persisted.scope, requestedScope);
210
+ }
211
+ if (persisted === undefined || persisted.scope !== requestedScope) {
212
+ this.#scopes.set(key, { name, version: major, scope: requestedScope });
213
+ this.#dirty = true;
214
+ }
215
+ this.#entries.set(key, Object.freeze({ name, major, scope: requestedScope, policy, owner }));
216
+ this.#owners.set(name, owner);
217
+ }
218
+ /** Seals the registration window. Idempotent. Kernel-only. */
219
+ seal() {
220
+ this.#sealed = true;
221
+ }
222
+ resolve(name, options) {
223
+ const authority = this.#authority;
224
+ const context = authority?.current();
225
+ if (authority === undefined || context === undefined || this.#disposed) {
226
+ return this.#refuse(authority, undefined, 'resolve-refused', {
227
+ name: typeof name === 'string' ? name : null,
228
+ reason: 'no kernel call context',
229
+ }, this.#disposed ? 'registry-closed' : 'no-call-context');
230
+ }
231
+ this.#refusals.delete(context);
232
+ const expectedOwner = options?.expectedOwner;
233
+ const requestBytes = Buffer.byteLength(String(name), 'utf8')
234
+ + Buffer.byteLength(typeof expectedOwner === 'string' ? expectedOwner : '', 'utf8')
235
+ + Buffer.byteLength(String(options?.version ?? ''), 'utf8');
236
+ if (!authority.admitCall(context.caller, requestBytes)) {
237
+ return this.#refuse(authority, context, 'resolve-refused', {
238
+ name: String(name),
239
+ reason: 'call or size limit exceeded',
240
+ }, 'limit-exceeded');
241
+ }
242
+ const version = options?.version ?? 1;
243
+ if (typeof name !== 'string' || !isExposedApiVersion(version)) {
244
+ return this.#refuse(authority, context, 'resolve-refused', {
245
+ name: String(name),
246
+ reason: 'malformed request',
247
+ }, 'malformed-request');
248
+ }
249
+ const entry = this.#entries.get(scopeRecordIdentityKey(name, exposedApiMajor(version)));
250
+ if (entry === undefined) {
251
+ return this.#refuse(authority, context, 'resolve-refused', {
252
+ name,
253
+ reason: 'no such exposure',
254
+ });
255
+ }
256
+ if (!authority.isLive(entry.owner)) {
257
+ return this.#refuse(authority, context, 'resolve-refused', {
258
+ name,
259
+ reason: 'owner identity is not live',
260
+ });
261
+ }
262
+ if (expectedOwner !== undefined && expectedOwner !== entry.owner) {
263
+ return this.#refuse(authority, context, 'resolve-refused', {
264
+ name,
265
+ reason: 'owner differs from the expected owner',
266
+ });
267
+ }
268
+ const decision = evaluateExposedApiPolicy(entry.policy, entry.scope, withParentCallerClass(authority, context));
269
+ if (!decision.allowed) {
270
+ return this.#refuse(authority, context, 'resolve-refused', {
271
+ name,
272
+ reason: decision.reason,
273
+ });
274
+ }
275
+ const binding = this.#bind(authority, entry);
276
+ if (binding === undefined) {
277
+ return this.#refuse(authority, context, 'resolve-refused', {
278
+ name,
279
+ reason: 'owner code does not provide the declared exposure',
280
+ });
281
+ }
282
+ const instance = authority.instanceOf(entry.owner);
283
+ const projection = binding.projection ?? identityProjection;
284
+ const value = authority.runAs(entry.owner, () => projection(instance, callerView(context)));
285
+ if (value === null || value === undefined) {
286
+ // The giver declined to project for this caller. The caller sees the
287
+ // same refusal as for any other policy decision, and the stream records it.
288
+ return this.#refuse(authority, context, 'resolve-refused', {
289
+ name,
290
+ reason: 'giver projection declined this caller',
291
+ });
292
+ }
293
+ return this.#handle(authority, entry, value);
294
+ }
295
+ /**
296
+ * Why the most recent `resolve` in the current call frame returned `null`.
297
+ *
298
+ * Reads only the current frame's own record, so no other caller's refusal is
299
+ * visible. Outside any frame the only possible refusal is the absence of one.
300
+ */
301
+ lastRefusal() {
302
+ const context = this.#authority?.current();
303
+ if (context === undefined) {
304
+ return refusalOf(this.#disposed ? 'registry-closed' : 'no-call-context');
305
+ }
306
+ return this.#refusals.get(context) ?? null;
307
+ }
308
+ /** Records a refusal on the observation stream and returns the uniform refusal. */
309
+ #refuse(authority, context, event, detail, code = 'unavailable') {
310
+ if (context !== undefined) {
311
+ this.#refusals.set(context, refusalOf(code, context));
312
+ }
313
+ authority?.observe(OBSERVATION_SOURCE, event, {
314
+ ...detail,
315
+ callerIdentity: context?.caller.identity ?? null,
316
+ callerClass: context?.caller.callerClass ?? null,
317
+ origin: context?.origin ?? null,
318
+ });
319
+ return null;
320
+ }
321
+ /**
322
+ * Binds one declared exposure to its owner's code, on first use.
323
+ *
324
+ * When the owner's code does not provide a declared exposure, the call fails
325
+ * and the mismatch is recorded. Code cannot bind an undeclared exposure,
326
+ * because only declared exposures are ever looked up.
327
+ */
328
+ #bind(authority, entry) {
329
+ const key = scopeRecordIdentityKey(entry.name, entry.major);
330
+ const cached = this.#bindings.get(key);
331
+ if (cached !== undefined) {
332
+ return cached;
333
+ }
334
+ const instance = authority.instanceOf(entry.owner);
335
+ const binding = isExposureProvider(instance)
336
+ ? authority.runAs(entry.owner, () => instance.provideExposure(entry.name, entry.major))
337
+ : undefined;
338
+ if (binding === undefined
339
+ || binding === null
340
+ || typeof binding !== 'object'
341
+ || (binding.projection !== undefined && typeof binding.projection !== 'function')) {
342
+ authority.observe(OBSERVATION_SOURCE, 'exposure-binding-mismatch', {
343
+ name: entry.name,
344
+ major: entry.major,
345
+ owner: entry.owner,
346
+ });
347
+ return undefined;
348
+ }
349
+ const frozen = Object.freeze({ ...binding });
350
+ this.#bindings.set(key, frozen);
351
+ return frozen;
352
+ }
353
+ /**
354
+ * Wraps a projected value in a handle that is a reference, not a grant.
355
+ *
356
+ * Every use of the handle evaluates Layer 1 again against the root origin of
357
+ * the chain using it, and runs the giver's code as the giver. Primitive
358
+ * values are data, not references, and are returned as they are.
359
+ */
360
+ #handle(authority, entry, value) {
361
+ if (value === null || (typeof value !== 'object' && typeof value !== 'function')) {
362
+ return value;
363
+ }
364
+ const real = value;
365
+ const authorize = () => {
366
+ const context = authority.current();
367
+ const reason = context === undefined
368
+ ? 'no kernel call context'
369
+ : !authority.admitCall(context.caller, 0)
370
+ ? 'call or size limit exceeded'
371
+ : !authority.isLive(entry.owner)
372
+ ? 'owner identity is not live'
373
+ : (() => {
374
+ const decision = evaluateExposedApiPolicy(entry.policy, entry.scope, withParentCallerClass(authority, context));
375
+ return decision.allowed ? undefined : decision.reason;
376
+ })();
377
+ if (reason !== undefined) {
378
+ authority.observe(OBSERVATION_SOURCE, 'invoke-refused', {
379
+ name: entry.name,
380
+ major: entry.major,
381
+ reason,
382
+ callerIdentity: context?.caller.identity ?? null,
383
+ callerClass: context?.caller.callerClass ?? null,
384
+ origin: context?.origin ?? null,
385
+ });
386
+ throw new ExposedApiUnavailableError();
387
+ }
388
+ };
389
+ const invoke = (fn, thisArg, args) => {
390
+ authorize();
391
+ return authority.runAs(entry.owner, () => Reflect.apply(fn, thisArg, args));
392
+ };
393
+ // The proxy target is an empty, extensible stand-in so the handle can wrap
394
+ // frozen projections without breaking proxy invariants.
395
+ const target = typeof real === 'function'
396
+ ? (() => undefined)
397
+ : {};
398
+ return new Proxy(target, {
399
+ get: (_target, property) => {
400
+ authorize();
401
+ const member = Reflect.get(real, property, real);
402
+ return typeof member === 'function'
403
+ ? (...args) => invoke(member, real, args)
404
+ : member;
405
+ },
406
+ apply: (_target, thisArg, args) => invoke(real, thisArg, args),
407
+ has: (_target, property) => {
408
+ authorize();
409
+ return Reflect.has(real, property);
410
+ },
411
+ ownKeys: () => {
412
+ authorize();
413
+ return Reflect.ownKeys(real);
414
+ },
415
+ getOwnPropertyDescriptor: (_target, property) => {
416
+ authorize();
417
+ const descriptor = Reflect.getOwnPropertyDescriptor(real, property);
418
+ return descriptor === undefined ? undefined : { ...descriptor, configurable: true };
419
+ },
420
+ getPrototypeOf: () => Reflect.getPrototypeOf(real),
421
+ set: () => false,
422
+ defineProperty: () => false,
423
+ deleteProperty: () => false,
424
+ setPrototypeOf: () => false,
425
+ preventExtensions: () => false,
426
+ });
427
+ }
428
+ /** Persists the exact in-memory scope record and verifies the written file. */
429
+ #persist() {
430
+ if (this.#storagePath === undefined) {
431
+ return;
432
+ }
433
+ if (this.#protectedRecord) {
434
+ releaseScopeRecordProtection(this.#storagePath);
435
+ this.#protectedRecord = false;
436
+ }
437
+ try {
438
+ persistScopeRecord(this.#storagePath, [...this.#scopes.values()]);
439
+ // Read the document back so the file trusted on the next boot is the
440
+ // file that was integrity-checked in this one.
441
+ loadScopeRecord(this.#storagePath);
442
+ protectScopeRecord(this.#storagePath);
443
+ this.#protectedRecord = true;
444
+ }
445
+ finally {
446
+ if (!this.#protectedRecord) {
447
+ protectScopeRecord(this.#storagePath);
448
+ this.#protectedRecord = true;
449
+ }
450
+ }
451
+ }
452
+ }
@@ -0,0 +1,2 @@
1
+ export { ExposedApiRegistryService, EXPOSED_API_NAME_PATTERN, } from './ExposedApiRegistryService.js';
2
+ export type { ExposedApiRegistryDependencies } from './ExposedApiRegistryService.js';
@@ -0,0 +1 @@
1
+ export { ExposedApiRegistryService, EXPOSED_API_NAME_PATTERN, } from './ExposedApiRegistryService.js';
@@ -0,0 +1,10 @@
1
+ import type { KernelServiceModuleContext } from '../../src/bootstrap/bootTypes.js';
2
+ /**
3
+ * Composes the resolution primitive through the standard service-module
4
+ * contract.
5
+ *
6
+ * The module declares only its own namespaced storage field. It publishes no
7
+ * instance: registration is the kernel's, during admission, and every
8
+ * participant reaches `resolve` through the access broker.
9
+ */
10
+ export declare function createService(context: KernelServiceModuleContext): import("../../index.js").ServiceDescriptor<import("./index.js").IExposedApiRegistry, import("./index.js").ExposedApiRegistryService>;
@@ -0,0 +1,20 @@
1
+ import { EXPOSED_API_REGISTRY_CONTEXT_SCOPE, EXPOSED_API_SCOPE_RECORD_PATH_FIELD, createExposedApiRegistryDescriptor, } from './descriptor/ExposedApiRegistryDescriptor.js';
2
+ /**
3
+ * Composes the resolution primitive through the standard service-module
4
+ * contract.
5
+ *
6
+ * The module declares only its own namespaced storage field. It publishes no
7
+ * instance: registration is the kernel's, during admission, and every
8
+ * participant reaches `resolve` through the access broker.
9
+ */
10
+ export function createService(context) {
11
+ context.declareContextFields(EXPOSED_API_REGISTRY_CONTEXT_SCOPE, [
12
+ {
13
+ name: EXPOSED_API_SCOPE_RECORD_PATH_FIELD,
14
+ type: 'string',
15
+ required: false,
16
+ description: 'Absolute path to the protected exposed API scope record.',
17
+ },
18
+ ]);
19
+ return createExposedApiRegistryDescriptor();
20
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Runtime write protection for the exposed API scope record.
3
+ *
4
+ * The scope record may be edited by an operator only while the kernel is
5
+ * stopped. While a kernel holds the path, the file is made read-only and every
6
+ * mutating filesystem entry point is refused before it reaches the host.
7
+ */
8
+ /**
9
+ * Makes an existing scope record read-only for the running kernel.
10
+ *
11
+ * @param storagePath - Absolute scope record file path.
12
+ */
13
+ export declare function protectScopeRecord(storagePath: string): void;
14
+ /**
15
+ * Releases one claim on a protected scope record.
16
+ *
17
+ * @param storagePath - Absolute scope record file path.
18
+ */
19
+ export declare function releaseScopeRecordProtection(storagePath: string): void;
20
+ /** Reports whether a scope record path is currently protected by this process. */
21
+ export declare function isScopeRecordProtected(storagePath: string): boolean;
@@ -0,0 +1,35 @@
1
+ import { existsSync } from 'node:fs';
2
+ import { resolve as resolvePath } from 'node:path';
3
+ import { ExposedApiScopeRecordWriteError } from '../contracts/errors.js';
4
+ import { isPathProtected, protectPath, releasePath, } from '../../../src/bootstrap/config/pathWriteGuard.js';
5
+ /**
6
+ * Runtime write protection for the exposed API scope record.
7
+ *
8
+ * The scope record may be edited by an operator only while the kernel is
9
+ * stopped. While a kernel holds the path, the file is made read-only and every
10
+ * mutating filesystem entry point is refused before it reaches the host.
11
+ */
12
+ /**
13
+ * Makes an existing scope record read-only for the running kernel.
14
+ *
15
+ * @param storagePath - Absolute scope record file path.
16
+ */
17
+ export function protectScopeRecord(storagePath) {
18
+ const resolvedPath = resolvePath(storagePath);
19
+ if (!existsSync(resolvedPath)) {
20
+ return;
21
+ }
22
+ protectPath(resolvedPath, (key, operation) => new ExposedApiScopeRecordWriteError(key, `scope record is protected and cannot be ${operation} while the kernel is running`));
23
+ }
24
+ /**
25
+ * Releases one claim on a protected scope record.
26
+ *
27
+ * @param storagePath - Absolute scope record file path.
28
+ */
29
+ export function releaseScopeRecordProtection(storagePath) {
30
+ releasePath(storagePath);
31
+ }
32
+ /** Reports whether a scope record path is currently protected by this process. */
33
+ export function isScopeRecordProtected(storagePath) {
34
+ return isPathProtected(storagePath);
35
+ }
@@ -0,0 +1,45 @@
1
+ import { type ExposedApiScope, type ExposedApiVersion } from '../contracts/types.js';
2
+ /** Stable format marker for the protected scope record. */
3
+ export declare const EXPOSED_API_SCOPE_RECORD_FORMAT = "brud.exposed-api-scope-record";
4
+ /** Format version the service understands. */
5
+ export declare const EXPOSED_API_SCOPE_RECORD_VERSION = 1;
6
+ /** Maximum accepted API name length in a scope record. */
7
+ export declare const EXPOSED_API_SCOPE_RECORD_MAX_NAME_LENGTH = 1024;
8
+ /** One persisted scope entry. */
9
+ export interface ScopeRecordEntry {
10
+ readonly name: string;
11
+ readonly version: ExposedApiVersion;
12
+ readonly scope: ExposedApiScope;
13
+ }
14
+ export { isScopeRecordProtected, protectScopeRecord, releaseScopeRecordProtection, } from './protection.js';
15
+ /** Builds the deterministic key for one `(name, version)` identity. */
16
+ export declare function scopeRecordIdentityKey(name: string, version: ExposedApiVersion): string;
17
+ /** Rejects a storage path that is not an absolute file path. */
18
+ export declare function assertScopeRecordPath(storagePath: string): string;
19
+ /**
20
+ * Parses and integrity-checks a scope record document.
21
+ *
22
+ * @param raw - Exact file contents.
23
+ * @param storagePath - Absolute path used in diagnostics.
24
+ */
25
+ export declare function parseScopeRecordDocument(raw: string, storagePath: string): readonly ScopeRecordEntry[];
26
+ /**
27
+ * Serializes a scope record document with a fresh integrity digest.
28
+ *
29
+ * @param records - Current records.
30
+ */
31
+ export declare function serializeScopeRecordDocument(records: readonly ScopeRecordEntry[]): string;
32
+ /**
33
+ * Loads and validates a scope record file.
34
+ *
35
+ * @param storagePath - Absolute scope record file path.
36
+ * @returns The records, or `undefined` when the file does not exist yet.
37
+ */
38
+ export declare function loadScopeRecord(storagePath: string): readonly ScopeRecordEntry[] | undefined;
39
+ /**
40
+ * Persists a scope record file atomically.
41
+ *
42
+ * @param storagePath - Absolute scope record file path.
43
+ * @param records - Current records.
44
+ */
45
+ export declare function persistScopeRecord(storagePath: string, records: readonly ScopeRecordEntry[]): void;