@brud/kernel 0.0.0-stage → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (278) 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 +44 -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 +112 -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 +61 -0
  60. package/dist/services/ExposedApiRegistry/orchestrator/ExposedApiRegistryService.js +407 -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 +65 -0
  88. package/dist/services/Interception/contracts/IInterceptionService.js +1 -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 +49 -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 +32 -0
  102. package/dist/services/Interception/orchestrator/InterceptionService.js +180 -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 +56 -0
  142. package/dist/services/Router/contracts/errors.js +75 -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 +90 -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 +74 -0
  158. package/dist/services/Router/orchestrator/RouterService.js +332 -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 +104 -0
  170. package/dist/services/WasmCage/src/caged.js +381 -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 +18 -0
  184. package/dist/src/bootstrap/bootKernelInternal.js +420 -0
  185. package/dist/src/bootstrap/bootTypes.d.ts +89 -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 +34 -0
  190. package/dist/src/bootstrap/config/integrity.js +79 -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 +8 -0
  202. package/dist/src/bootstrap/index.js +5 -0
  203. package/dist/src/bootstrap/packageRequirements.d.ts +26 -0
  204. package/dist/src/bootstrap/packageRequirements.js +52 -0
  205. package/dist/src/contracts/accessBroker.d.ts +53 -0
  206. package/dist/src/contracts/accessBroker.js +1 -0
  207. package/dist/src/contracts/brudOperation.d.ts +20 -0
  208. package/dist/src/contracts/brudOperation.js +20 -0
  209. package/dist/src/contracts/callChain.d.ts +161 -0
  210. package/dist/src/contracts/callChain.js +19 -0
  211. package/dist/src/contracts/capability.d.ts +26 -0
  212. package/dist/src/contracts/capability.js +26 -0
  213. package/dist/src/contracts/container.d.ts +17 -0
  214. package/dist/src/contracts/container.js +8 -0
  215. package/dist/src/contracts/context.d.ts +19 -0
  216. package/dist/src/contracts/context.js +1 -0
  217. package/dist/src/contracts/contextContract.d.ts +129 -0
  218. package/dist/src/contracts/contextContract.js +285 -0
  219. package/dist/src/contracts/dispatchView.d.ts +21 -0
  220. package/dist/src/contracts/dispatchView.js +1 -0
  221. package/dist/src/contracts/handler.d.ts +28 -0
  222. package/dist/src/contracts/handler.js +1 -0
  223. package/dist/src/contracts/index.d.ts +25 -0
  224. package/dist/src/contracts/index.js +8 -0
  225. package/dist/src/contracts/kernel.d.ts +60 -0
  226. package/dist/src/contracts/kernel.js +1 -0
  227. package/dist/src/contracts/lifecycle.d.ts +59 -0
  228. package/dist/src/contracts/lifecycle.js +1 -0
  229. package/dist/src/contracts/manifest.d.ts +145 -0
  230. package/dist/src/contracts/manifest.js +295 -0
  231. package/dist/src/contracts/pipeline.d.ts +8 -0
  232. package/dist/src/contracts/pipeline.js +1 -0
  233. package/dist/src/contracts/policy.d.ts +75 -0
  234. package/dist/src/contracts/policy.js +77 -0
  235. package/dist/src/contracts/registry.d.ts +7 -0
  236. package/dist/src/contracts/registry.js +1 -0
  237. package/dist/src/contracts/serviceDescriptor.d.ts +67 -0
  238. package/dist/src/contracts/serviceDescriptor.js +1 -0
  239. package/dist/src/contracts/serviceToken.d.ts +36 -0
  240. package/dist/src/contracts/serviceToken.js +16 -0
  241. package/dist/src/engine/BrudKernel.d.ts +118 -0
  242. package/dist/src/engine/BrudKernel.js +366 -0
  243. package/dist/src/engine/ExecutionPipeline.d.ts +13 -0
  244. package/dist/src/engine/ExecutionPipeline.js +62 -0
  245. package/dist/src/engine/KernelAccessBroker.d.ts +47 -0
  246. package/dist/src/engine/KernelAccessBroker.js +107 -0
  247. package/dist/src/engine/KernelRegistry.d.ts +14 -0
  248. package/dist/src/engine/KernelRegistry.js +33 -0
  249. package/dist/src/engine/OperationRegistry.d.ts +9 -0
  250. package/dist/src/engine/OperationRegistry.js +18 -0
  251. package/dist/src/engine/ServiceContainer.d.ts +13 -0
  252. package/dist/src/engine/ServiceContainer.js +53 -0
  253. package/dist/src/engine/callChain.d.ts +68 -0
  254. package/dist/src/engine/callChain.js +180 -0
  255. package/dist/src/engine/dag.d.ts +2 -0
  256. package/dist/src/engine/dag.js +34 -0
  257. package/dist/src/engine/errors.d.ts +26 -0
  258. package/dist/src/engine/errors.js +52 -0
  259. package/dist/src/engine/index.d.ts +11 -0
  260. package/dist/src/engine/index.js +8 -0
  261. package/dist/src/engine/state.d.ts +1 -0
  262. package/dist/src/engine/state.js +1 -0
  263. package/dist/src/validation/jsonSchema/compile.d.ts +8 -0
  264. package/dist/src/validation/jsonSchema/compile.js +386 -0
  265. package/dist/src/validation/jsonSchema/index.d.ts +3 -0
  266. package/dist/src/validation/jsonSchema/index.js +2 -0
  267. package/dist/src/validation/jsonSchema/jsonValue.d.ts +29 -0
  268. package/dist/src/validation/jsonSchema/jsonValue.js +190 -0
  269. package/dist/src/validation/jsonSchema/pattern.d.ts +28 -0
  270. package/dist/src/validation/jsonSchema/pattern.js +155 -0
  271. package/dist/src/validation/jsonSchema/types.d.ts +71 -0
  272. package/dist/src/validation/jsonSchema/types.js +15 -0
  273. package/package.json +59 -4
  274. package/services/WasmCage/native/brud-wasm-cage.darwin-arm64.node +0 -0
  275. package/services/WasmCage/native/brud-wasm-cage.darwin-x64.node +0 -0
  276. package/services/WasmCage/native/brud-wasm-cage.linux-arm64-gnu.node +0 -0
  277. package/services/WasmCage/native/brud-wasm-cage.linux-x64-gnu.node +0 -0
  278. package/services/WasmCage/native/brud-wasm-cage.win32-x64-msvc.node +0 -0
@@ -0,0 +1,73 @@
1
+ import { CALL_ORIGINS, } from '../../../src/contracts/callChain.js';
2
+ /** API names accept dotted, kebab, and snake segments. */
3
+ export const EXPOSED_API_NAME_PATTERN = /^[A-Za-z][A-Za-z0-9_.-]*$/;
4
+ /** The four reachability scopes, ordered from narrowest to widest. */
5
+ export const EXPOSED_API_SCOPES = ['kernel', 'runtime', 'host', 'any'];
6
+ /** The three root origins a call chain can carry. Assigned by the kernel. */
7
+ export const EXPOSED_API_ORIGINS = CALL_ORIGINS;
8
+ /**
9
+ * Reports whether a value is a supported scope.
10
+ *
11
+ * @param value - Candidate value.
12
+ */
13
+ export function isExposedApiScope(value) {
14
+ return typeof value === 'string'
15
+ && EXPOSED_API_SCOPES.includes(value);
16
+ }
17
+ /**
18
+ * Reports whether a value is a supported root origin.
19
+ *
20
+ * @param value - Candidate value.
21
+ */
22
+ export function isExposedApiOrigin(value) {
23
+ return typeof value === 'string'
24
+ && EXPOSED_API_ORIGINS.includes(value);
25
+ }
26
+ const SEMANTIC_VERSION_PATTERN = /^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/u;
27
+ /**
28
+ * Reports whether a value is a valid API version.
29
+ *
30
+ * @param value - Candidate value.
31
+ */
32
+ export function isExposedApiVersion(value) {
33
+ if (typeof value === 'number') {
34
+ return Number.isSafeInteger(value) && value >= 1;
35
+ }
36
+ return typeof value === 'string' && SEMANTIC_VERSION_PATTERN.test(value);
37
+ }
38
+ /**
39
+ * Returns the order rank of a scope. Higher is wider.
40
+ *
41
+ * @param scope - Supported scope.
42
+ */
43
+ export function exposedApiScopeRank(scope) {
44
+ return EXPOSED_API_SCOPES.indexOf(scope);
45
+ }
46
+ /**
47
+ * Applies the four-case origin-and-scope rule.
48
+ *
49
+ * @param scope - Scope declared by the API identity.
50
+ * @param origin - Root origin of the call chain.
51
+ */
52
+ export function exposedApiScopeAllowsOrigin(scope, origin) {
53
+ switch (scope) {
54
+ case 'kernel':
55
+ return origin === 'kernel';
56
+ case 'runtime':
57
+ return origin === 'kernel' || origin === 'runtime';
58
+ case 'host':
59
+ return origin === 'host';
60
+ case 'any':
61
+ return true;
62
+ }
63
+ }
64
+ /**
65
+ * Returns the major version of a valid API version.
66
+ *
67
+ * @param version - A valid API version.
68
+ */
69
+ export function exposedApiMajor(version) {
70
+ return typeof version === 'number'
71
+ ? version
72
+ : Number.parseInt(version.split('.')[0] ?? '', 10);
73
+ }
@@ -0,0 +1,44 @@
1
+ import type { ServiceTier } from '../../../src/contracts/lifecycle.js';
2
+ import type { ServiceDescriptor } from '../../../src/contracts/serviceDescriptor.js';
3
+ import type { IExposedApiRegistry } from '../contracts/IExposedApiRegistry.js';
4
+ import { ExposedApiRegistryService } from '../orchestrator/index.js';
5
+ /** Tier assigned to the Registry descriptor. */
6
+ export declare const EXPOSED_API_REGISTRY_SERVICE_TIER: ServiceTier;
7
+ /** Namespace owned by the registry in the context contract. */
8
+ export declare const EXPOSED_API_REGISTRY_CONTEXT_SCOPE = "exposedApi";
9
+ /** Contract version declared for the Registry interface. */
10
+ export declare const EXPOSED_API_REGISTRY_CONTRACT_VERSION = "1.0.0";
11
+ /**
12
+ * Methods declared for the Registry interface.
13
+ *
14
+ * `resolve` is open to every participant. `register` is kernel-only: the
15
+ * kernel calls it during admission, and it is never part of the view a
16
+ * participant receives.
17
+ */
18
+ export declare const EXPOSED_API_REGISTRY_CONTRACT_METHODS: readonly ["register", "resolve"];
19
+ /**
20
+ * Optional context field holding the absolute protected scope-record path.
21
+ *
22
+ * This is the registry's own field, declared in its own namespace. Hosts that
23
+ * need cross-boot scope persistence supply it. When it is absent the registry
24
+ * keeps the scope record in memory for one boot.
25
+ */
26
+ export declare const EXPOSED_API_SCOPE_RECORD_PATH_FIELD = "exposedApi.scopeRecord.path";
27
+ /** Configuration for {@link createExposedApiRegistryDescriptor}. */
28
+ export interface ExposedApiRegistryDescriptorOptions {
29
+ /** Absolute scope-record path used instead of the context field. */
30
+ readonly scopeRecordPath?: string;
31
+ }
32
+ /**
33
+ * Builds the resolution primitive's descriptor.
34
+ *
35
+ * The descriptor carries no caller list and no access policy of its own:
36
+ * `resolve` is open to every participant, and the projection hands every
37
+ * caller only `resolve`. Registration is reachable only by the kernel, which
38
+ * holds the instance during admission.
39
+ *
40
+ * @param options - Storage override.
41
+ */
42
+ export declare function createExposedApiRegistryDescriptor(options?: ExposedApiRegistryDescriptorOptions): ServiceDescriptor<IExposedApiRegistry, ExposedApiRegistryService>;
43
+ /** Standard Registry descriptor with no overrides. */
44
+ export declare const ExposedApiRegistryDescriptor: ServiceDescriptor<IExposedApiRegistry, ExposedApiRegistryService>;
@@ -0,0 +1,58 @@
1
+ import { ExposedApiRegistryService } from '../orchestrator/index.js';
2
+ import { ExposedApiRegistryToken } from './tokens.js';
3
+ /** Tier assigned to the Registry descriptor. */
4
+ export const EXPOSED_API_REGISTRY_SERVICE_TIER = 'core';
5
+ /** Namespace owned by the registry in the context contract. */
6
+ export const EXPOSED_API_REGISTRY_CONTEXT_SCOPE = 'exposedApi';
7
+ /** Contract version declared for the Registry interface. */
8
+ export const EXPOSED_API_REGISTRY_CONTRACT_VERSION = '1.0.0';
9
+ /**
10
+ * Methods declared for the Registry interface.
11
+ *
12
+ * `resolve` is open to every participant. `register` is kernel-only: the
13
+ * kernel calls it during admission, and it is never part of the view a
14
+ * participant receives.
15
+ */
16
+ export const EXPOSED_API_REGISTRY_CONTRACT_METHODS = ['register', 'resolve'];
17
+ /**
18
+ * Optional context field holding the absolute protected scope-record path.
19
+ *
20
+ * This is the registry's own field, declared in its own namespace. Hosts that
21
+ * need cross-boot scope persistence supply it. When it is absent the registry
22
+ * keeps the scope record in memory for one boot.
23
+ */
24
+ export const EXPOSED_API_SCOPE_RECORD_PATH_FIELD = 'exposedApi.scopeRecord.path';
25
+ /** Reads an optional non-empty string from the registry's own context field. */
26
+ function optionalContextString(options, field) {
27
+ const value = options[field];
28
+ return typeof value === 'string' && value.length > 0 ? value : undefined;
29
+ }
30
+ /**
31
+ * Builds the resolution primitive's descriptor.
32
+ *
33
+ * The descriptor carries no caller list and no access policy of its own:
34
+ * `resolve` is open to every participant, and the projection hands every
35
+ * caller only `resolve`. Registration is reachable only by the kernel, which
36
+ * holds the instance during admission.
37
+ *
38
+ * @param options - Storage override.
39
+ */
40
+ export function createExposedApiRegistryDescriptor(options) {
41
+ return {
42
+ token: ExposedApiRegistryToken,
43
+ tier: EXPOSED_API_REGISTRY_SERVICE_TIER,
44
+ dependencies: [],
45
+ contract: {
46
+ version: EXPOSED_API_REGISTRY_CONTRACT_VERSION,
47
+ methods: EXPOSED_API_REGISTRY_CONTRACT_METHODS,
48
+ },
49
+ factory: (context) => {
50
+ const scopeRecordPath = options?.scopeRecordPath
51
+ ?? optionalContextString(context.options, EXPOSED_API_SCOPE_RECORD_PATH_FIELD);
52
+ return new ExposedApiRegistryService(scopeRecordPath === undefined ? {} : { scopeRecordPath });
53
+ },
54
+ project: (instance) => instance.publicView,
55
+ };
56
+ }
57
+ /** Standard Registry descriptor with no overrides. */
58
+ export const ExposedApiRegistryDescriptor = createExposedApiRegistryDescriptor();
@@ -0,0 +1,8 @@
1
+ import type { IExposedApiRegistry } from '../contracts/IExposedApiRegistry.js';
2
+ /**
3
+ * Kernel service token of the resolution primitive.
4
+ *
5
+ * Every participant reaches `resolve` through this token. Registration is the
6
+ * kernel's own, during admission, and is never reachable through it.
7
+ */
8
+ export declare const ExposedApiRegistryToken: import("../../../src/contracts/serviceToken.js").ServiceToken<IExposedApiRegistry>;
@@ -0,0 +1,8 @@
1
+ import { createServiceToken } from '../../../src/contracts/serviceToken.js';
2
+ /**
3
+ * Kernel service token of the resolution primitive.
4
+ *
5
+ * Every participant reaches `resolve` through this token. Registration is the
6
+ * kernel's own, during admission, and is never reachable through it.
7
+ */
8
+ export const ExposedApiRegistryToken = createServiceToken('brud.core.exposedApiRegistry', 'Resolution primitive for manifest-declared exposures');
@@ -0,0 +1,6 @@
1
+ export * from './contracts/index.js';
2
+ export * from './descriptor/ExposedApiRegistryDescriptor.js';
3
+ export * from './descriptor/tokens.js';
4
+ export * from './orchestrator/index.js';
5
+ export { EXPOSED_API_SCOPE_RECORD_FORMAT, EXPOSED_API_SCOPE_RECORD_MAX_NAME_LENGTH, EXPOSED_API_SCOPE_RECORD_VERSION, assertScopeRecordPath, isScopeRecordProtected, loadScopeRecord, parseScopeRecordDocument, persistScopeRecord, protectScopeRecord, releaseScopeRecordProtection, scopeRecordIdentityKey, serializeScopeRecordDocument, } from './storage/scopeRecord.js';
6
+ export type { ScopeRecordEntry } from './storage/scopeRecord.js';
@@ -0,0 +1,5 @@
1
+ export * from './contracts/index.js';
2
+ export * from './descriptor/ExposedApiRegistryDescriptor.js';
3
+ export * from './descriptor/tokens.js';
4
+ export * from './orchestrator/index.js';
5
+ export { EXPOSED_API_SCOPE_RECORD_FORMAT, EXPOSED_API_SCOPE_RECORD_MAX_NAME_LENGTH, EXPOSED_API_SCOPE_RECORD_VERSION, assertScopeRecordPath, isScopeRecordProtected, loadScopeRecord, parseScopeRecordDocument, persistScopeRecord, protectScopeRecord, releaseScopeRecordProtection, scopeRecordIdentityKey, serializeScopeRecordDocument, } from './storage/scopeRecord.js';
@@ -0,0 +1,61 @@
1
+ import type { KernelCallAuthority } from '../../../src/contracts/callChain.js';
2
+ import type { IServiceLifecycle } from '../../../src/contracts/lifecycle.js';
3
+ import type { ExposureDeclaration } from '../../../src/contracts/manifest.js';
4
+ import type { IExposedApiAdmission, IExposedApiRegistry } from '../contracts/IExposedApiRegistry.js';
5
+ import { EXPOSED_API_NAME_PATTERN, type ExposedApiResolveOptions } from '../contracts/types.js';
6
+ /** API names accept dotted, kebab, and snake segments. */
7
+ export { EXPOSED_API_NAME_PATTERN };
8
+ /** Collaborators injected into {@link ExposedApiRegistryService}. */
9
+ export interface ExposedApiRegistryDependencies {
10
+ /**
11
+ * Absolute path to the protected per-composition scope record.
12
+ *
13
+ * When omitted, scope is tracked in memory for the lifetime of the service.
14
+ */
15
+ readonly scopeRecordPath?: string;
16
+ }
17
+ /**
18
+ * The resolution primitive.
19
+ *
20
+ * Holds no participant name, no participant token, no caller identifier, no
21
+ * caller list, and no policy of its own. It stores what each participant's
22
+ * manifest declares, keyed by the owner's kernel-issued identity, and
23
+ * evaluates each giver's declared Layer 1 policy against the kernel-provided
24
+ * execution context.
25
+ */
26
+ export declare class ExposedApiRegistryService implements IExposedApiRegistry, IExposedApiAdmission, IServiceLifecycle {
27
+ #private;
28
+ /** The only view a participant receives: `resolve`. */
29
+ readonly publicView: IExposedApiRegistry;
30
+ constructor(dependencies?: ExposedApiRegistryDependencies);
31
+ /**
32
+ * Loads and integrity-checks the protected scope record.
33
+ *
34
+ * A missing record is the legitimate first-registration case. A corrupt
35
+ * record refuses boot. An existing record is made read-only for the runtime.
36
+ */
37
+ initialize(): Promise<void>;
38
+ /** Seals the window and persists the final scope record. */
39
+ onRunning(): Promise<void>;
40
+ /** Releases the protected-path claim and forgets every exposure. */
41
+ dispose(): Promise<void>;
42
+ /** Whether {@link dispose} has run. */
43
+ get isDisposed(): boolean;
44
+ /** Whether the registration window is sealed. */
45
+ get isSealed(): boolean;
46
+ /** Absolute scope-record path, when persistence is configured. */
47
+ get scopeRecordPath(): string | undefined;
48
+ /**
49
+ * Attaches the kernel's call authority and the composition's name pins.
50
+ *
51
+ * Called once by the kernel, during admission, before any registration.
52
+ *
53
+ * @param authority - The kernel's call authority.
54
+ * @param ownerPins - Exposure names pinned to an owner identity.
55
+ */
56
+ attachKernel(authority: KernelCallAuthority, ownerPins: Readonly<Record<string, string>>): void;
57
+ register(owner: string, declaration: ExposureDeclaration): void;
58
+ /** Seals the registration window. Idempotent. Kernel-only. */
59
+ seal(): void;
60
+ resolve<T = unknown>(name: string, options?: ExposedApiResolveOptions): T | null;
61
+ }
@@ -0,0 +1,407 @@
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
+ /** Default projection: the giver's instance itself. */
30
+ function identityProjection(service) {
31
+ return service;
32
+ }
33
+ /** Builds the caller view a projection receives. */
34
+ function callerView(context) {
35
+ return Object.freeze({
36
+ identity: context.caller.identity,
37
+ callerClass: context.caller.callerClass,
38
+ official: context.caller.official,
39
+ capabilities: context.caller.capabilities,
40
+ origin: context.origin,
41
+ });
42
+ }
43
+ /**
44
+ * The resolution primitive.
45
+ *
46
+ * Holds no participant name, no participant token, no caller identifier, no
47
+ * caller list, and no policy of its own. It stores what each participant's
48
+ * manifest declares, keyed by the owner's kernel-issued identity, and
49
+ * evaluates each giver's declared Layer 1 policy against the kernel-provided
50
+ * execution context.
51
+ */
52
+ export class ExposedApiRegistryService {
53
+ #entries = new Map();
54
+ #owners = new Map();
55
+ #bindings = new Map();
56
+ #scopes = new Map();
57
+ #storagePath;
58
+ #authority;
59
+ #pins = Object.freeze({});
60
+ #sealed = false;
61
+ #disposed = false;
62
+ #initialized = false;
63
+ #protectedRecord = false;
64
+ #dirty = false;
65
+ /** The only view a participant receives: `resolve`. */
66
+ publicView;
67
+ constructor(dependencies = {}) {
68
+ this.#storagePath = dependencies.scopeRecordPath;
69
+ this.publicView = Object.freeze({
70
+ resolve: (name, options) => this.resolve(name, options),
71
+ });
72
+ }
73
+ /**
74
+ * Loads and integrity-checks the protected scope record.
75
+ *
76
+ * A missing record is the legitimate first-registration case. A corrupt
77
+ * record refuses boot. An existing record is made read-only for the runtime.
78
+ */
79
+ async initialize() {
80
+ if (this.#initialized) {
81
+ return;
82
+ }
83
+ this.#initialized = true;
84
+ if (this.#storagePath === undefined) {
85
+ return;
86
+ }
87
+ const loaded = loadScopeRecord(this.#storagePath);
88
+ if (loaded !== undefined) {
89
+ for (const record of loaded) {
90
+ this.#scopes.set(scopeRecordIdentityKey(record.name, record.version), record);
91
+ }
92
+ protectScopeRecord(this.#storagePath);
93
+ this.#protectedRecord = true;
94
+ }
95
+ }
96
+ /** Seals the window and persists the final scope record. */
97
+ async onRunning() {
98
+ this.seal();
99
+ if (this.#storagePath !== undefined && this.#dirty) {
100
+ this.#persist();
101
+ }
102
+ }
103
+ /** Releases the protected-path claim and forgets every exposure. */
104
+ async dispose() {
105
+ if (this.#storagePath !== undefined && this.#protectedRecord) {
106
+ releaseScopeRecordProtection(this.#storagePath);
107
+ this.#protectedRecord = false;
108
+ }
109
+ this.#disposed = true;
110
+ this.#sealed = true;
111
+ this.#entries.clear();
112
+ this.#owners.clear();
113
+ this.#bindings.clear();
114
+ }
115
+ /** Whether {@link dispose} has run. */
116
+ get isDisposed() {
117
+ return this.#disposed;
118
+ }
119
+ /** Whether the registration window is sealed. */
120
+ get isSealed() {
121
+ return this.#sealed;
122
+ }
123
+ /** Absolute scope-record path, when persistence is configured. */
124
+ get scopeRecordPath() {
125
+ return this.#storagePath;
126
+ }
127
+ /**
128
+ * Attaches the kernel's call authority and the composition's name pins.
129
+ *
130
+ * Called once by the kernel, during admission, before any registration.
131
+ *
132
+ * @param authority - The kernel's call authority.
133
+ * @param ownerPins - Exposure names pinned to an owner identity.
134
+ */
135
+ attachKernel(authority, ownerPins) {
136
+ if (this.#disposed) {
137
+ throw new ExposedApiDisposedError();
138
+ }
139
+ if (this.#sealed || this.#authority !== undefined) {
140
+ throw new ExposedApiRegistrationClosedError('kernel attachment');
141
+ }
142
+ this.#authority = authority;
143
+ this.#pins = Object.freeze({ ...ownerPins });
144
+ }
145
+ register(owner, declaration) {
146
+ const name = typeof declaration?.name === 'string' ? declaration.name : String(declaration?.name);
147
+ if (this.#disposed) {
148
+ throw new ExposedApiDisposedError();
149
+ }
150
+ if (this.#sealed) {
151
+ throw new ExposedApiRegistrationClosedError(name);
152
+ }
153
+ if (this.#authority === undefined) {
154
+ throw new ExposedApiNotAttachedError();
155
+ }
156
+ if (typeof declaration?.name !== 'string'
157
+ || declaration.name.length > EXPOSED_API_SCOPE_RECORD_MAX_NAME_LENGTH
158
+ || !EXPOSED_API_NAME_PATTERN.test(declaration.name)) {
159
+ throw new ExposedApiInvalidNameError(name);
160
+ }
161
+ if (typeof owner !== 'string' || owner.length === 0) {
162
+ throw new ExposedApiNotAttachedError();
163
+ }
164
+ const version = declaration.version ?? 1;
165
+ if (!isExposedApiVersion(version)) {
166
+ throw new ExposedApiInvalidVersionError(version);
167
+ }
168
+ const major = exposedApiMajor(version);
169
+ const requestedScope = declaration.scope ?? 'kernel';
170
+ if (!isExposedApiScope(requestedScope)) {
171
+ throw new ExposedApiInvalidScopeError(requestedScope);
172
+ }
173
+ const policy = parseExposedApiPolicy(declaration.policy);
174
+ const pinned = this.#pins[name];
175
+ if (pinned !== undefined && pinned !== owner) {
176
+ throw new ExposedApiOwnerPinError(name);
177
+ }
178
+ const nameOwner = this.#owners.get(name);
179
+ if (nameOwner !== undefined && nameOwner !== owner) {
180
+ throw new ExposedApiNameCollisionError(name, nameOwner);
181
+ }
182
+ const key = scopeRecordIdentityKey(name, major);
183
+ if (this.#entries.has(key)) {
184
+ throw new ExposedApiDuplicateExposureError(name, major);
185
+ }
186
+ const persisted = this.#scopes.get(key);
187
+ if (persisted !== undefined
188
+ && exposedApiScopeRank(requestedScope) < exposedApiScopeRank(persisted.scope)) {
189
+ throw new ExposedApiScopeNarrowingError(name, String(major), persisted.scope, requestedScope);
190
+ }
191
+ if (persisted === undefined || persisted.scope !== requestedScope) {
192
+ this.#scopes.set(key, { name, version: major, scope: requestedScope });
193
+ this.#dirty = true;
194
+ }
195
+ this.#entries.set(key, Object.freeze({ name, major, scope: requestedScope, policy, owner }));
196
+ this.#owners.set(name, owner);
197
+ }
198
+ /** Seals the registration window. Idempotent. Kernel-only. */
199
+ seal() {
200
+ this.#sealed = true;
201
+ }
202
+ resolve(name, options) {
203
+ const authority = this.#authority;
204
+ const context = authority?.current();
205
+ if (authority === undefined || context === undefined || this.#disposed) {
206
+ return this.#refuse(authority, undefined, 'resolve-refused', {
207
+ name: typeof name === 'string' ? name : null,
208
+ reason: 'no kernel call context',
209
+ });
210
+ }
211
+ const expectedOwner = options?.expectedOwner;
212
+ const requestBytes = Buffer.byteLength(String(name), 'utf8')
213
+ + Buffer.byteLength(typeof expectedOwner === 'string' ? expectedOwner : '', 'utf8')
214
+ + Buffer.byteLength(String(options?.version ?? ''), 'utf8');
215
+ if (!authority.admitCall(context.caller, requestBytes)) {
216
+ return this.#refuse(authority, context, 'resolve-refused', {
217
+ name: String(name),
218
+ reason: 'call or size limit exceeded',
219
+ });
220
+ }
221
+ const version = options?.version ?? 1;
222
+ if (typeof name !== 'string' || !isExposedApiVersion(version)) {
223
+ return this.#refuse(authority, context, 'resolve-refused', {
224
+ name: String(name),
225
+ reason: 'malformed request',
226
+ });
227
+ }
228
+ const entry = this.#entries.get(scopeRecordIdentityKey(name, exposedApiMajor(version)));
229
+ if (entry === undefined) {
230
+ return this.#refuse(authority, context, 'resolve-refused', {
231
+ name,
232
+ reason: 'no such exposure',
233
+ });
234
+ }
235
+ if (!authority.isLive(entry.owner)) {
236
+ return this.#refuse(authority, context, 'resolve-refused', {
237
+ name,
238
+ reason: 'owner identity is not live',
239
+ });
240
+ }
241
+ if (expectedOwner !== undefined && expectedOwner !== entry.owner) {
242
+ return this.#refuse(authority, context, 'resolve-refused', {
243
+ name,
244
+ reason: 'owner differs from the expected owner',
245
+ });
246
+ }
247
+ const decision = evaluateExposedApiPolicy(entry.policy, entry.scope, withParentCallerClass(authority, context));
248
+ if (!decision.allowed) {
249
+ return this.#refuse(authority, context, 'resolve-refused', {
250
+ name,
251
+ reason: decision.reason,
252
+ });
253
+ }
254
+ const binding = this.#bind(authority, entry);
255
+ if (binding === undefined) {
256
+ return this.#refuse(authority, context, 'resolve-refused', {
257
+ name,
258
+ reason: 'owner code does not provide the declared exposure',
259
+ });
260
+ }
261
+ const instance = authority.instanceOf(entry.owner);
262
+ const projection = binding.projection ?? identityProjection;
263
+ const value = authority.runAs(entry.owner, () => projection(instance, callerView(context)));
264
+ return this.#handle(authority, entry, value);
265
+ }
266
+ /** Records a refusal on the observation stream and returns the uniform refusal. */
267
+ #refuse(authority, context, event, detail) {
268
+ authority?.observe(OBSERVATION_SOURCE, event, {
269
+ ...detail,
270
+ callerIdentity: context?.caller.identity ?? null,
271
+ callerClass: context?.caller.callerClass ?? null,
272
+ origin: context?.origin ?? null,
273
+ });
274
+ return null;
275
+ }
276
+ /**
277
+ * Binds one declared exposure to its owner's code, on first use.
278
+ *
279
+ * When the owner's code does not provide a declared exposure, the call fails
280
+ * and the mismatch is recorded. Code cannot bind an undeclared exposure,
281
+ * because only declared exposures are ever looked up.
282
+ */
283
+ #bind(authority, entry) {
284
+ const key = scopeRecordIdentityKey(entry.name, entry.major);
285
+ const cached = this.#bindings.get(key);
286
+ if (cached !== undefined) {
287
+ return cached;
288
+ }
289
+ const instance = authority.instanceOf(entry.owner);
290
+ const binding = isExposureProvider(instance)
291
+ ? authority.runAs(entry.owner, () => instance.provideExposure(entry.name, entry.major))
292
+ : undefined;
293
+ if (binding === undefined
294
+ || binding === null
295
+ || typeof binding !== 'object'
296
+ || (binding.projection !== undefined && typeof binding.projection !== 'function')) {
297
+ authority.observe(OBSERVATION_SOURCE, 'exposure-binding-mismatch', {
298
+ name: entry.name,
299
+ major: entry.major,
300
+ owner: entry.owner,
301
+ });
302
+ return undefined;
303
+ }
304
+ const frozen = Object.freeze({ ...binding });
305
+ this.#bindings.set(key, frozen);
306
+ return frozen;
307
+ }
308
+ /**
309
+ * Wraps a projected value in a handle that is a reference, not a grant.
310
+ *
311
+ * Every use of the handle evaluates Layer 1 again against the root origin of
312
+ * the chain using it, and runs the giver's code as the giver. Primitive
313
+ * values are data, not references, and are returned as they are.
314
+ */
315
+ #handle(authority, entry, value) {
316
+ if (value === null || (typeof value !== 'object' && typeof value !== 'function')) {
317
+ return value;
318
+ }
319
+ const real = value;
320
+ const authorize = () => {
321
+ const context = authority.current();
322
+ const reason = context === undefined
323
+ ? 'no kernel call context'
324
+ : !authority.admitCall(context.caller, 0)
325
+ ? 'call or size limit exceeded'
326
+ : !authority.isLive(entry.owner)
327
+ ? 'owner identity is not live'
328
+ : (() => {
329
+ const decision = evaluateExposedApiPolicy(entry.policy, entry.scope, withParentCallerClass(authority, context));
330
+ return decision.allowed ? undefined : decision.reason;
331
+ })();
332
+ if (reason !== undefined) {
333
+ authority.observe(OBSERVATION_SOURCE, 'invoke-refused', {
334
+ name: entry.name,
335
+ major: entry.major,
336
+ reason,
337
+ callerIdentity: context?.caller.identity ?? null,
338
+ callerClass: context?.caller.callerClass ?? null,
339
+ origin: context?.origin ?? null,
340
+ });
341
+ throw new ExposedApiUnavailableError();
342
+ }
343
+ };
344
+ const invoke = (fn, thisArg, args) => {
345
+ authorize();
346
+ return authority.runAs(entry.owner, () => Reflect.apply(fn, thisArg, args));
347
+ };
348
+ // The proxy target is an empty, extensible stand-in so the handle can wrap
349
+ // frozen projections without breaking proxy invariants.
350
+ const target = typeof real === 'function'
351
+ ? (() => undefined)
352
+ : {};
353
+ return new Proxy(target, {
354
+ get: (_target, property) => {
355
+ authorize();
356
+ const member = Reflect.get(real, property, real);
357
+ return typeof member === 'function'
358
+ ? (...args) => invoke(member, real, args)
359
+ : member;
360
+ },
361
+ apply: (_target, thisArg, args) => invoke(real, thisArg, args),
362
+ has: (_target, property) => {
363
+ authorize();
364
+ return Reflect.has(real, property);
365
+ },
366
+ ownKeys: () => {
367
+ authorize();
368
+ return Reflect.ownKeys(real);
369
+ },
370
+ getOwnPropertyDescriptor: (_target, property) => {
371
+ authorize();
372
+ const descriptor = Reflect.getOwnPropertyDescriptor(real, property);
373
+ return descriptor === undefined ? undefined : { ...descriptor, configurable: true };
374
+ },
375
+ getPrototypeOf: () => Reflect.getPrototypeOf(real),
376
+ set: () => false,
377
+ defineProperty: () => false,
378
+ deleteProperty: () => false,
379
+ setPrototypeOf: () => false,
380
+ preventExtensions: () => false,
381
+ });
382
+ }
383
+ /** Persists the exact in-memory scope record and verifies the written file. */
384
+ #persist() {
385
+ if (this.#storagePath === undefined) {
386
+ return;
387
+ }
388
+ if (this.#protectedRecord) {
389
+ releaseScopeRecordProtection(this.#storagePath);
390
+ this.#protectedRecord = false;
391
+ }
392
+ try {
393
+ persistScopeRecord(this.#storagePath, [...this.#scopes.values()]);
394
+ // Read the document back so the file trusted on the next boot is the
395
+ // file that was integrity-checked in this one.
396
+ loadScopeRecord(this.#storagePath);
397
+ protectScopeRecord(this.#storagePath);
398
+ this.#protectedRecord = true;
399
+ }
400
+ finally {
401
+ if (!this.#protectedRecord) {
402
+ protectScopeRecord(this.#storagePath);
403
+ this.#protectedRecord = true;
404
+ }
405
+ }
406
+ }
407
+ }