@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,349 @@
1
+ import fs from 'node:fs';
2
+ import { syncBuiltinESMExports } from 'node:module';
3
+ import { resolve as resolvePath, sep } from 'node:path';
4
+ import { fileURLToPath } from 'node:url';
5
+ const ACTIVE_PROTECTIONS = new Map();
6
+ let guardInstalled = false;
7
+ function pathKeys(target) {
8
+ let text;
9
+ if (typeof target === 'string') {
10
+ text = target;
11
+ }
12
+ else if (Buffer.isBuffer(target)) {
13
+ text = target.toString();
14
+ }
15
+ else if (target instanceof URL) {
16
+ try {
17
+ text = fileURLToPath(target);
18
+ }
19
+ catch {
20
+ return [];
21
+ }
22
+ }
23
+ if (text === undefined) {
24
+ return [];
25
+ }
26
+ const resolved = resolvePath(text);
27
+ const keys = [resolved];
28
+ try {
29
+ const real = fs.realpathSync.native(resolved);
30
+ if (real !== resolved) {
31
+ keys.push(real);
32
+ }
33
+ }
34
+ catch {
35
+ // A target that does not exist yet can still equal the protected path.
36
+ }
37
+ return keys;
38
+ }
39
+ function assertMutable(target, operation) {
40
+ for (const key of pathKeys(target)) {
41
+ const entry = ACTIVE_PROTECTIONS.get(key);
42
+ if (entry !== undefined) {
43
+ throw entry.error(key, operation);
44
+ }
45
+ }
46
+ }
47
+ function isStrictAncestor(ancestor, target) {
48
+ return target.startsWith(ancestor.endsWith(sep) ? ancestor : `${ancestor}${sep}`);
49
+ }
50
+ function flagsMutate(flags) {
51
+ if (typeof flags === 'string') {
52
+ return /[wa+]/u.test(flags);
53
+ }
54
+ if (typeof flags === 'number') {
55
+ const constants = fs.constants;
56
+ const mutable = constants.O_WRONLY
57
+ | constants.O_RDWR
58
+ | constants.O_CREAT
59
+ | constants.O_TRUNC
60
+ | constants.O_APPEND;
61
+ return (flags & mutable) !== 0;
62
+ }
63
+ return false;
64
+ }
65
+ function guardSync(original, indexes, operation) {
66
+ return function (...args) {
67
+ for (const index of indexes) {
68
+ assertMutable(args[index], operation);
69
+ }
70
+ return Reflect.apply(original, this, args);
71
+ };
72
+ }
73
+ function guardCallback(original, indexes, operation) {
74
+ return function (...args) {
75
+ try {
76
+ for (const index of indexes) {
77
+ assertMutable(args[index], operation);
78
+ }
79
+ }
80
+ catch (error) {
81
+ const callback = args.at(-1);
82
+ if (typeof callback === 'function') {
83
+ Reflect.apply(callback, undefined, [error]);
84
+ return undefined;
85
+ }
86
+ throw error;
87
+ }
88
+ return Reflect.apply(original, this, args);
89
+ };
90
+ }
91
+ function guardPromise(original, indexes, operation) {
92
+ return function (...args) {
93
+ try {
94
+ for (const index of indexes) {
95
+ assertMutable(args[index], operation);
96
+ }
97
+ }
98
+ catch (error) {
99
+ return Promise.reject(error);
100
+ }
101
+ return Reflect.apply(original, this, args);
102
+ };
103
+ }
104
+ function guardOpen(original) {
105
+ return function (...args) {
106
+ if (flagsMutate(args[1])) {
107
+ assertMutable(args[0], 'opened for writing');
108
+ }
109
+ return Reflect.apply(original, this, args);
110
+ };
111
+ }
112
+ function replace(target, name, wrap) {
113
+ const original = target[name];
114
+ if (typeof original === 'function') {
115
+ target[name] = wrap(original);
116
+ }
117
+ }
118
+ const SYNC_TARGETS = [
119
+ ['writeFileSync', [0], 'written'],
120
+ ['appendFileSync', [0], 'written'],
121
+ ['truncateSync', [0], 'truncated'],
122
+ ['unlinkSync', [0], 'deleted'],
123
+ ['chmodSync', [0], 'modified'],
124
+ ['lchmodSync', [0], 'modified'],
125
+ ['chownSync', [0], 'modified'],
126
+ ['lchownSync', [0], 'modified'],
127
+ ['utimesSync', [0], 'modified'],
128
+ ['lutimesSync', [0], 'modified'],
129
+ ['renameSync', [0, 1], 'renamed'],
130
+ ['copyFileSync', [1], 'overwritten'],
131
+ ['cpSync', [1], 'overwritten'],
132
+ ['linkSync', [1], 'overwritten'],
133
+ ['symlinkSync', [1], 'overwritten'],
134
+ ];
135
+ const CALLBACK_TARGETS = [
136
+ ['writeFile', [0], 'written'],
137
+ ['appendFile', [0], 'written'],
138
+ ['truncate', [0], 'truncated'],
139
+ ['unlink', [0], 'deleted'],
140
+ ['chmod', [0], 'modified'],
141
+ ['lchmod', [0], 'modified'],
142
+ ['chown', [0], 'modified'],
143
+ ['lchown', [0], 'modified'],
144
+ ['utimes', [0], 'modified'],
145
+ ['lutimes', [0], 'modified'],
146
+ ['rename', [0, 1], 'renamed'],
147
+ ['copyFile', [1], 'overwritten'],
148
+ ['cp', [1], 'overwritten'],
149
+ ['link', [1], 'overwritten'],
150
+ ['symlink', [1], 'overwritten'],
151
+ ];
152
+ const PROMISE_TARGETS = [
153
+ ['writeFile', [0], 'written'],
154
+ ['appendFile', [0], 'written'],
155
+ ['truncate', [0], 'truncated'],
156
+ ['unlink', [0], 'deleted'],
157
+ ['chmod', [0], 'modified'],
158
+ ['lchmod', [0], 'modified'],
159
+ ['chown', [0], 'modified'],
160
+ ['lchown', [0], 'modified'],
161
+ ['utimes', [0], 'modified'],
162
+ ['lutimes', [0], 'modified'],
163
+ ['rename', [0, 1], 'renamed'],
164
+ ['copyFile', [1], 'overwritten'],
165
+ ['cp', [1], 'overwritten'],
166
+ ['link', [1], 'overwritten'],
167
+ ['symlink', [1], 'overwritten'],
168
+ ];
169
+ function isRecursiveOptions(value) {
170
+ return value !== null
171
+ && typeof value === 'object'
172
+ && value.recursive === true;
173
+ }
174
+ function recursiveCoversProtected(target) {
175
+ const key = pathKeys(target)[0];
176
+ if (key === undefined) {
177
+ return false;
178
+ }
179
+ for (const protectedKey of ACTIVE_PROTECTIONS.keys()) {
180
+ if (isStrictAncestor(key, protectedKey)) {
181
+ return true;
182
+ }
183
+ }
184
+ return false;
185
+ }
186
+ function assertRemoveMutable(target, options) {
187
+ if (!isRecursiveOptions(options) || !recursiveCoversProtected(target)) {
188
+ assertMutable(target, 'deleted');
189
+ }
190
+ }
191
+ function guardRemoveSync(original) {
192
+ return function (...args) {
193
+ assertRemoveMutable(args[0], args[1]);
194
+ return Reflect.apply(original, this, args);
195
+ };
196
+ }
197
+ function guardRemoveCallback(original) {
198
+ return function (...args) {
199
+ try {
200
+ assertRemoveMutable(args[0], args[1]);
201
+ }
202
+ catch (error) {
203
+ const callback = args.at(-1);
204
+ if (typeof callback === 'function') {
205
+ Reflect.apply(callback, undefined, [error]);
206
+ return undefined;
207
+ }
208
+ throw error;
209
+ }
210
+ return Reflect.apply(original, this, args);
211
+ };
212
+ }
213
+ function guardRemovePromise(original) {
214
+ return function (...args) {
215
+ try {
216
+ assertRemoveMutable(args[0], args[1]);
217
+ }
218
+ catch (error) {
219
+ return Promise.reject(error);
220
+ }
221
+ return Reflect.apply(original, this, args);
222
+ };
223
+ }
224
+ function installWriteGuard() {
225
+ if (guardInstalled) {
226
+ return;
227
+ }
228
+ guardInstalled = true;
229
+ const mutableFs = fs;
230
+ for (const [name, indexes, operation] of SYNC_TARGETS) {
231
+ replace(mutableFs, name, (original) => guardSync(original, indexes, operation));
232
+ }
233
+ for (const [name, indexes, operation] of CALLBACK_TARGETS) {
234
+ replace(mutableFs, name, (original) => guardCallback(original, indexes, operation));
235
+ }
236
+ replace(mutableFs, 'openSync', (original) => guardOpen(original));
237
+ replace(mutableFs, 'open', (original) => guardOpen(original));
238
+ const originalCreateWriteStream = mutableFs['createWriteStream'];
239
+ if (typeof originalCreateWriteStream === 'function') {
240
+ mutableFs['createWriteStream'] = guardSync(originalCreateWriteStream, [0], 'opened for writing');
241
+ }
242
+ for (const name of ['rmSync', 'rmdirSync']) {
243
+ replace(mutableFs, name, (original) => guardRemoveSync(original));
244
+ }
245
+ for (const name of ['rm', 'rmdir']) {
246
+ replace(mutableFs, name, (original) => guardRemoveCallback(original));
247
+ }
248
+ const promises = mutableFs['promises'];
249
+ if (promises !== undefined) {
250
+ for (const name of ['rm', 'rmdir']) {
251
+ replace(promises, name, (original) => guardRemovePromise(original));
252
+ }
253
+ for (const [name, indexes, operation] of PROMISE_TARGETS) {
254
+ replace(promises, name, (original) => guardPromise(original, indexes, operation));
255
+ }
256
+ replace(promises, 'open', (original) => guardOpen(original));
257
+ }
258
+ syncBuiltinESMExports();
259
+ }
260
+ /**
261
+ * Marks a path read-only for the lifetime of one claim.
262
+ *
263
+ * Both the declared path and its real path are protected so an alias cannot
264
+ * bypass the guard. Repeated claims are reference-counted.
265
+ *
266
+ * @param storagePath - Absolute or resolvable path to protect.
267
+ * @param error - Failure factory for operations that target the path.
268
+ */
269
+ export function protectPath(storagePath, error) {
270
+ installWriteGuard();
271
+ const keys = pathKeys(storagePath);
272
+ if (keys.length === 0) {
273
+ return;
274
+ }
275
+ const primary = keys[0];
276
+ if (ACTIVE_PROTECTIONS.has(primary)) {
277
+ for (const key of keys) {
278
+ const entry = ACTIVE_PROTECTIONS.get(key);
279
+ if (entry === undefined) {
280
+ ACTIVE_PROTECTIONS.set(key, { error, count: 1 });
281
+ }
282
+ else {
283
+ entry.count += 1;
284
+ }
285
+ }
286
+ return;
287
+ }
288
+ let originalMode;
289
+ try {
290
+ if (fs.existsSync(primary)) {
291
+ originalMode = fs.statSync(primary).mode;
292
+ fs.chmodSync(primary, 0o444);
293
+ }
294
+ }
295
+ catch {
296
+ // A host that cannot change the mode still gets the API-level guard.
297
+ }
298
+ for (const key of keys) {
299
+ const entry = ACTIVE_PROTECTIONS.get(key);
300
+ if (entry === undefined) {
301
+ ACTIVE_PROTECTIONS.set(key, {
302
+ error,
303
+ count: 1,
304
+ ...(originalMode === undefined ? {} : { originalMode }),
305
+ });
306
+ }
307
+ else {
308
+ entry.count += 1;
309
+ }
310
+ }
311
+ }
312
+ /**
313
+ * Releases one claim on a protected path.
314
+ *
315
+ * @param storagePath - Path whose claim is released.
316
+ */
317
+ export function releasePath(storagePath) {
318
+ const keys = pathKeys(storagePath);
319
+ const primary = keys[0];
320
+ const primaryMode = primary === undefined
321
+ ? undefined
322
+ : ACTIVE_PROTECTIONS.get(primary)?.originalMode;
323
+ for (const key of keys) {
324
+ const entry = ACTIVE_PROTECTIONS.get(key);
325
+ if (entry === undefined) {
326
+ continue;
327
+ }
328
+ if (entry.count <= 1) {
329
+ ACTIVE_PROTECTIONS.delete(key);
330
+ }
331
+ else {
332
+ entry.count -= 1;
333
+ }
334
+ }
335
+ if (primary !== undefined
336
+ && !ACTIVE_PROTECTIONS.has(primary)
337
+ && primaryMode !== undefined) {
338
+ try {
339
+ fs.chmodSync(primary, primaryMode);
340
+ }
341
+ catch {
342
+ // Best effort; the file may have been removed.
343
+ }
344
+ }
345
+ }
346
+ /** Reports whether a path is currently protected by this process. */
347
+ export function isPathProtected(storagePath) {
348
+ return ACTIVE_PROTECTIONS.has(resolvePath(storagePath));
349
+ }
@@ -0,0 +1,41 @@
1
+ /** One service declaration. */
2
+ export interface BrudConfigEntry {
3
+ /** Module path, package name, `kernel:` module, or relative path. */
4
+ readonly path: string;
5
+ /**
6
+ * The composition owner's official declaration for this participant.
7
+ *
8
+ * At boot the kernel builds its identity-to-official mapping from this
9
+ * declaration: the participant's kernel identity maps to `true` when the
10
+ * owner declares it official, and to `false` otherwise. The mapping is a
11
+ * Layer 1 policy input. Hardcore trust is not the owner's to change: a
12
+ * hardcore entry that declares `official: false` fails boot.
13
+ */
14
+ readonly official?: boolean;
15
+ }
16
+ /**
17
+ * Declarative kernel composition.
18
+ *
19
+ * The kernel owns the `services` and `exposureOwners` sections. Other
20
+ * top-level sections belong to the participants that own them and are carried
21
+ * by the document unchanged.
22
+ */
23
+ export interface BrudConfig {
24
+ readonly services: readonly BrudConfigEntry[];
25
+ /**
26
+ * Exposure names pinned to an owner identity.
27
+ *
28
+ * Maps an exposure name to the kernel identity token that alone may register
29
+ * it in this composition.
30
+ */
31
+ readonly exposureOwners?: Readonly<Record<string, string>>;
32
+ }
33
+ /** Stable config failure codes. */
34
+ export type BrudConfigErrorCode = 'CONFIG_READ' | 'CONFIG_PARSE' | 'CONFIG_SCHEMA' | 'CONFIG_INTEGRITY' | 'CONFIG_RELOAD_FROZEN' | 'SEAL_MISMATCH' | 'SEAL_VIOLATION' | 'MODULE_RESOLVE' | 'MODULE_CONTRACT' | 'PARTICIPANT_TIER' | 'PARTICIPANT_REQUIRES_OFFICIAL' | 'HARDCORE_VERIFICATION';
35
+ /** Config or module-resolution failure with optional source line. */
36
+ export declare class BrudConfigError extends Error {
37
+ readonly code: BrudConfigErrorCode;
38
+ readonly configPath: string;
39
+ readonly line?: number;
40
+ constructor(code: BrudConfigErrorCode, message: string, configPath: string, line?: number);
41
+ }
@@ -0,0 +1,15 @@
1
+ /** Config or module-resolution failure with optional source line. */
2
+ export class BrudConfigError extends Error {
3
+ code;
4
+ configPath;
5
+ line;
6
+ constructor(code, message, configPath, line) {
7
+ super(line === undefined ? message : `${message} (line ${String(line)})`);
8
+ this.name = 'BrudConfigError';
9
+ this.code = code;
10
+ this.configPath = configPath;
11
+ if (line !== undefined) {
12
+ this.line = line;
13
+ }
14
+ }
15
+ }
@@ -0,0 +1,212 @@
1
+ import type { KernelCallAuthority } from '../contracts/callChain.js';
2
+ import type { AdmittedManifest } from '../contracts/manifest.js';
3
+ import type { ServiceDescriptor } from '../contracts/serviceDescriptor.js';
4
+ import { BrudConfigError, type BrudConfig } from './config/types.js';
5
+ /**
6
+ * The generic hardcore mechanism.
7
+ *
8
+ * This module knows how to verify and boot any compiled hardcore entry. It
9
+ * does not hold the compiled list and knows nothing about what any listed
10
+ * service does: it never names a service, a token, or a method. Every entry
11
+ * carries its own contract version, its own contract interface, and its own
12
+ * boot hook. The mechanism compares each entry against its own declaration and
13
+ * calls each entry's hook with one uniform signature.
14
+ */
15
+ /** Check that refused a hardcore service. */
16
+ export type HardcoreCheck = 'presence' | 'contract' | 'content-hash' | 'trust';
17
+ /** One composed participant, as recorded by the loader for hardcore hooks. */
18
+ export interface HardcoreBootEntry {
19
+ /** Service token id declared by the descriptor. */
20
+ readonly token: string;
21
+ /** Config path used to compose the service. */
22
+ readonly path: string;
23
+ /**
24
+ * The participant's official status in this composition.
25
+ *
26
+ * The kernel's identity-to-official mapping is built from this value: the
27
+ * composition owner's declaration for an ordinary participant, and always
28
+ * `true` for a hardcore participant, whose trust the owner cannot change.
29
+ */
30
+ readonly official: boolean;
31
+ /** Package content digest used as the provisional content binding. */
32
+ readonly contentHash: string;
33
+ /** The participant's validated, frozen manifest. */
34
+ readonly manifest: AdmittedManifest;
35
+ /**
36
+ * The parent's kernel-issued identity token, for a caged child.
37
+ *
38
+ * `undefined` for a top-level participant. A caged child carries the identity
39
+ * of the participant that loaded it, or the cage's identity, as the kernel
40
+ * issued it at admission. The caller frame records it verbatim.
41
+ */
42
+ readonly parentIdentity?: string;
43
+ }
44
+ /**
45
+ * What the mechanism hands one hardcore entry's boot hook.
46
+ *
47
+ * Every hook receives the same shape: the entry's own token and verified
48
+ * package digest, the composition the loader assembled, the frozen config, the
49
+ * kernel's call authority, and the kernel's record of participant identities.
50
+ */
51
+ export interface HardcoreBootContext {
52
+ /** Token id of the entry whose hook is running. */
53
+ readonly token: string;
54
+ /** Verified package content digest of the entry whose hook is running. */
55
+ readonly contentHash: string;
56
+ /** Every composed participant, in config order. */
57
+ readonly composition: readonly HardcoreBootEntry[];
58
+ /** The composition's frozen config. */
59
+ readonly config: BrudConfig;
60
+ /** The kernel's call authority, handed only to root primitives. */
61
+ readonly authority: KernelCallAuthority;
62
+ /**
63
+ * Records the kernel identity token issued for one participant.
64
+ *
65
+ * A participant's identity can be recorded once. Hooks that run later in
66
+ * list order read it through {@link HardcoreBootContext.identityOf}.
67
+ */
68
+ recordIdentity(participantToken: string, identity: string): void;
69
+ /** The identity recorded for one participant, if any. */
70
+ identityOf(participantToken: string): string | undefined;
71
+ }
72
+ /**
73
+ * Uniform boot hook signature for every hardcore entry.
74
+ *
75
+ * The hook narrows `instance` structurally to whatever its own entry declares.
76
+ * The kernel calls it once, after every service has initialized and before the
77
+ * kernel enters `RUNNING`, while the hardcore service is still mutable.
78
+ */
79
+ export type HardcoreBootHook = (instance: unknown, context: HardcoreBootContext) => void;
80
+ /** Contract a hardcore entry declares for its own service. */
81
+ export interface HardcoreContract {
82
+ /** Contract version the kernel was compiled to expect. */
83
+ readonly contractVersion: string;
84
+ /** Methods the descriptor must declare. */
85
+ readonly contractInterface: readonly string[];
86
+ /** Boot hook the kernel calls while the service is still mutable. */
87
+ readonly boot: HardcoreBootHook;
88
+ }
89
+ /** One fixed hardcore entry compiled into the kernel. */
90
+ export interface HardcoreService extends HardcoreContract {
91
+ /** Service token id. */
92
+ readonly token: string;
93
+ /**
94
+ * Declares that this hardcore is a mechanism, not an official participant.
95
+ *
96
+ * A hardcore that sets this maps to non-official in the identity-to-official
97
+ * mapping; a hardcore without it maps to official, which is what every
98
+ * hardcore entry is today. The field names no service: it is a general
99
+ * category for any hardcore that is a mechanism rather than an official
100
+ * participant, and the mapping reads it in `buildHardcoreBootEntries`.
101
+ */
102
+ readonly mechanism?: boolean;
103
+ /** Expected digest of the source representation of the resolved package. */
104
+ readonly expectedContentHash: string;
105
+ /** Expected digest of the built representation of the resolved package. */
106
+ readonly expectedBuiltContentHash: string;
107
+ }
108
+ /** Composed service as seen by the loader before registration. */
109
+ export interface ComposedServiceDescriptor {
110
+ /** Descriptor returned by the service module. */
111
+ readonly descriptor: ServiceDescriptor<unknown, unknown>;
112
+ /** Config path used to compose the service. */
113
+ readonly path: string;
114
+ /**
115
+ * The composition owner's official declaration, exactly as written.
116
+ *
117
+ * `undefined` when the entry carries no declaration.
118
+ */
119
+ readonly official?: boolean;
120
+ /** Absolute file the config path pointed to. */
121
+ readonly resolvedPath: string;
122
+ /** The participant's validated, frozen manifest. */
123
+ readonly manifest: AdmittedManifest;
124
+ /**
125
+ * The parent's kernel-issued identity token, when the composed service is a
126
+ * caged child. `undefined` for a top-level participant.
127
+ */
128
+ readonly parentIdentity?: string;
129
+ }
130
+ /** Raised when a compiled-in hardcore service fails verification. */
131
+ export declare class HardcoreServiceError extends BrudConfigError {
132
+ /** Hardcore service token id. */
133
+ readonly service: string;
134
+ /** Check that failed. */
135
+ readonly check: HardcoreCheck;
136
+ /** Value the kernel was compiled to expect. */
137
+ readonly expected: string;
138
+ /** Value the composed service supplied. */
139
+ readonly found: string;
140
+ constructor(service: string, check: HardcoreCheck, expected: string, found: string, configPath: string);
141
+ }
142
+ /** Returns the SHA-256 hex digest of one file. */
143
+ export declare function hashPackageFile(filePath: string): string;
144
+ /**
145
+ * Computes the content digest of the resolved package.
146
+ *
147
+ * The digest covers the resolved entry file and every relative import it
148
+ * reaches, canonicalized by relative path and per-file SHA-256. Bare package
149
+ * specifiers and Node builtins are not followed: they are outside the loaded
150
+ * package.
151
+ *
152
+ * @param entryPath - Absolute path the config entry resolved to.
153
+ */
154
+ export declare function computePackageContentHash(entryPath: string): string;
155
+ /**
156
+ * Verifies every compiled hardcore entry against the composed descriptors.
157
+ *
158
+ * Each entry is checked against its own declaration: its own token, its own
159
+ * contract version, its own contract interface, and its own pins. Checks run in
160
+ * this order for each entry: presence, declared contract, package content. All
161
+ * checks run before any service is initialized.
162
+ *
163
+ * @param list - The compiled hardcore list.
164
+ * @param composed - Descriptors returned by the composed service modules.
165
+ * @param configPath - Config path used in diagnostics.
166
+ * @returns Package digests for the hardcore services that passed.
167
+ */
168
+ export declare function verifyHardcoreServices(list: readonly HardcoreService[], composed: readonly ComposedServiceDescriptor[], configPath: string): ReadonlyMap<string, string>;
169
+ /**
170
+ * Builds the boot-call entries for every composed service.
171
+ *
172
+ * Hardcore services use their verified package digest. Other services use the
173
+ * digest of their resolved entry file; that is provisional until the loader
174
+ * supplies verified publisher and content bindings.
175
+ *
176
+ * @param composed - Descriptors returned by the composed service modules.
177
+ * @param packageHashes - Verified hardcore package digests.
178
+ */
179
+ export declare function buildHardcoreBootEntries(composed: readonly ComposedServiceDescriptor[], packageHashes: ReadonlyMap<string, string>, list?: readonly HardcoreService[]): readonly HardcoreBootEntry[];
180
+ /** One initialized service, as the engine reports it to the boot loader. */
181
+ export interface HardcoreInitializedInstance {
182
+ /** Token id of the initialized service. */
183
+ readonly tokenId: string;
184
+ /** The initialized instance. */
185
+ readonly instance: unknown;
186
+ }
187
+ /** Raised when a participant's kernel identity is recorded twice. */
188
+ export declare class HardcoreIdentityError extends Error {
189
+ readonly code: "HARDCORE_IDENTITY";
190
+ /** Token id of the participant. */
191
+ readonly participant: string;
192
+ constructor(participant: string, reason: string);
193
+ }
194
+ /**
195
+ * Calls every compiled entry's boot hook with the uniform signature.
196
+ *
197
+ * Hooks run in list order. Each hook receives its own initialized instance,
198
+ * its own token, its own verified package digest, the composition, the frozen
199
+ * config, and the kernel's call authority. Identities a hook records are
200
+ * visible to every hook that runs after it. An entry with no initialized
201
+ * instance is skipped; the presence check refuses that composition before any
202
+ * service is constructed.
203
+ *
204
+ * @param list - The compiled hardcore list.
205
+ * @param instances - Every initialized hardcore service.
206
+ * @param packageHashes - Verified hardcore package digests from verification.
207
+ * @param composition - Every composed participant, in config order.
208
+ * @param config - The composition's frozen config.
209
+ * @param authority - The kernel's call authority.
210
+ * @returns The identity recorded for each participant token.
211
+ */
212
+ export declare function invokeHardcoreBootHooks(list: readonly HardcoreService[], instances: readonly HardcoreInitializedInstance[], packageHashes: ReadonlyMap<string, string>, composition: readonly HardcoreBootEntry[], config: BrudConfig, authority: KernelCallAuthority): ReadonlyMap<string, string>;