Commit 3319469
authored
feat(compartment-mapper)!: new hooks & type overhaul (#2988)
## Description
This introduces a set of new hooks to user-visible functions and overhauls many types.
### Description of changes to Types
- The types around `CompartmentMapDescriptor` have been vastly expanded to reflect the different "flavors" of `CompartmentMapDescriptor`, `CompartmentDescriptor`, `ModuleDescriptor` (now `ModuleDescriptorConfiguration` to differentiate it from `ses`' `ModuleDescriptor`), and the formatting of the compartment names (keys).
- `CompartmentDescriptor.label` is now a canonical name.
- `CompartmentDescriptor.path` is removed
- `CompartmentDescriptor.compartments` is removed
- Added many type guards for validation of these differing types
- Validation of different types of compartment map
- Stricter validation
- Validation of all types of compartment descriptors, module descriptor configurations, etc.
### New Default Behavior in `mapNodeModules()`
Currently, `mapNodeModules()` avoids adding a `ModuleDescriptor` (`ModuleDescriptorConfiguration`) to a nascent `CompartmentDescriptor`'s `modules` prop if policy disallows it. This PR changes the behavior to consider policy & excise the _dependency_ from the `Node` itself _before_ the (dependency) `Graph` is translated into a `CompartmentDescriptor`. This has implications around what sort of errors are thrown when Compartment A attempts to load a Compartment B to which it has no access (see note about policy tests below).
### Other Notes
- Since `CompartmentDescriptor.compartments` has been removed, that means implicitly allowing dynamic requires of parent/ancestor Compartments from a given Compartment via absolute path _is no longer supported_. That's fine, since:
- This should be explicit in policy _anyway_
- `@lavamoat/node` was the only user of this feature and no longer needs it
- The entry compartment now has a canonical name of `$root$`. It _is_ valid within `PackagePolicy`; i.e. some other package can be allowed to access the `$root$` compartment.
- Many tests in `test/policy.test.ts` needed to change because of how `mapNodeModules`' `packageDependencies` hook works. Instead of rejecting module descriptors (`ModuleDescriptorConfiguration`s) based on policy, we reject entire _dependencies_ before they can be "digested" into module descriptors. This results in `ScopeDescriptor`s not being populated, in addition to `ModuleDescriptorConfiguration`s, and causes downstream effects. Different exceptions are thrown at different times, but the _intent_ of these tests doesn't deviate from "ensure this naughty behavior is not allowed."
- **This PR does _not_ change the archive format.**
* * *
This commit introduces a comprehensive universal hook system for `@endo/compartment-mapper`,
enabling extensible customization of module mapping, bundling, and policy enforcement.
BREAKING CHANGES: `CompartmentDescriptor` objects no longer have a `path` property and the `label` property is now a canonical name. Types have changed dramatically. Enhanced validation of `CompartmentMapDescriptor` objects.
## New Options
### `mapNodeModules()`
- `packageDataHook`: Called once before `translateGraph` with data about all packages found while crawling `node_modules`. Receives a read-only `Map<CanonicalName, PackageData>` where each entry contains:
- `name`: Package name
- `packageDescriptor`: The package.json contents
- `location`: File URL to the package
- `canonicalName`: The canonical name used in policy
- `packageDependenciesHook`: Called for each package's dependencies during graph translation, allowing dynamic modification of the dependency graph. Can add or remove dependencies from packages based on policy or other criteria.
- `unknownCanonicalNameHook`: Called when policy references canonical names that don't exist in the dependency graph, with optional suggestions for typos/similar names.
### `makeImportHookMaker()`
- `moduleSourceHook()`: Called when module source objects are created (either from local files containing `bytes`, exit modules, or "error-type" sources)
### `captureFromMap()`
- `packageConnectionsHook`: Called during digest; surfaces "connections" between compartment descriptors
## Type System Overhaul (`src/types/`)
### Major Breaking Type Changes in `compartment-map-schema.ts`:
#### CompartmentDescriptor Interface Restructure:
- **BREAKING**: Removed `path` property - compartment descriptors no longer track dependency paths
- **BREAKING**: `label` property type changed from `string` to `CanonicalName<U>` - labels are now type-safe canonical names
- **BREAKING**: Made `CompartmentDescriptor` generic with `<T extends ModuleDescriptorConfiguration, U extends string>` for better type safety
- Added `location: string` as a required property for all compartment descriptors
- Added optional `sourceDirname` as found in the sources
#### CompartmentMapDescriptor Genericization (Is That A Word? No):
- **BREAKING**: `CompartmentMapDescriptor` is now generic: `CompartmentMapDescriptor<T, Name, EntryName>`
- **BREAKING**: `compartments` property changed from `Record<string, CompartmentDescriptor>` to `CompartmentDescriptors<T, Name>`
- **BREAKING**: `entry` property type changed from `EntryDescriptor` to `EntryDescriptor<EntryName>`
- New specialized types: `PackageCompartmentMapDescriptor`, `FileCompartmentMapDescriptor`, and `DigestedCompartmentMapDescriptor`
#### Enhanced Module Descriptor System:
- `ModuleDescriptor` is now `ModuleDescriptorConfiguration` to differentiate between it & the `ModuleDescriptor` from `ses`
- Added `ModuleDescriptorConfigurationCreator` enum tracking module creation source (`'link' | 'transform' | 'import-hook' | 'digest' | 'node-modules'`)
- Enhanced `BaseModuleDescriptorConfiguration` with `__createdBy` property for debugging
- Added `ErrorModuleDescriptorConfiguration` for deferred error handling
- Improved type discrimination with `ModuleDescriptorConfigurationKindToType` utility type
### New Type Infrastructure:
#### `canonical-name.ts` - Canonical Name Type System:
- **NEW**: Type-level canonical name validation using template literal types
- `CanonicalName<S>` - validates npm package name chains separated by '>' (e.g., "foo>bar", "@scope/pkg>dep")
- `ScopedPackageName` and `UnscopedPackageName` for npm package name validation
- `SplitOnGt<S>` and `AllValidPackageNames<Parts>` for compile-time canonical name parsing
- `IsCanonicalName<S>` predicate type for conditional type logic
-
### Enhanced Type Safety Features:
#### Compartment Descriptor Validation:
- `DigestedCompartmentDescriptor` - restricted descriptor for archived compartment maps
- Properties marked as `never` for digested descriptors: `path`, `retained`, `scopes`, `parsers`, `types`, `__createdBy`, `sourceDirname`
- `CompartmentDescriptorWithPolicy<T>` - enforces policy presence where required
#### Module Configuration Type Safety:
- `ModuleDescriptorConfigurationKind` union for module type discrimination
- Type-safe module configuration creators with `__createdBy` tracking
#### Policy Integration:
- Enhanced policy types in `PackageCompartmentDescriptor` with canonical name constraints
- `LiteralUnion` usage for special canonical names (`ATTENUATORS_COMPARTMENT`, `ENTRY_COMPARTMENT`)
- Policy-aware compartment descriptor types with enhanced validation
### Type System Utilities:
#### `typescript.ts` Enhancements:
- Enhanced `LiteralUnion<L, B>` for better literal type preservation
- Type utilities supporting the new generic compartment map architecture
- Moved `Simplify` from tests here (because it's useful dammit)
## Enhanced Policy Validation
### Policy Validation:
- Unknown canonical name detection with suggestion system
- Cross-reference policy resources against actual compartment contents
- Detailed error reporting with path information for policy issues
- Hook integration for custom policy validation logicFile tree
58 files changed
+6928
-1271
lines changed- packages
- compartment-mapper
- src
- types
- test
- fixtures-dynamic-ancestor
- node_modules
- pantspack-folder-runner
- pantspack
- fixtures-implicit-reexport
- fixtures-optional-peer-dependencies
- snapshots
- env-options
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
58 files changed
+6928
-1271
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
5 | 31 | | |
6 | 32 | | |
7 | 33 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
| 71 | + | |
71 | 72 | | |
72 | 73 | | |
73 | 74 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
| 30 | + | |
| 31 | + | |
29 | 32 | | |
30 | 33 | | |
31 | 34 | | |
| |||
36 | 39 | | |
37 | 40 | | |
38 | 41 | | |
39 | | - | |
| 42 | + | |
| 43 | + | |
40 | 44 | | |
| 45 | + | |
41 | 46 | | |
42 | 47 | | |
43 | 48 | | |
| |||
55 | 60 | | |
56 | 61 | | |
57 | 62 | | |
| 63 | + | |
| 64 | + | |
58 | 65 | | |
59 | 66 | | |
60 | 67 | | |
61 | | - | |
62 | | - | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
69 | | - | |
70 | | - | |
| 68 | + | |
71 | 69 | | |
72 | 70 | | |
73 | 71 | | |
| |||
77 | 75 | | |
78 | 76 | | |
79 | 77 | | |
80 | | - | |
81 | 78 | | |
82 | | - | |
83 | | - | |
84 | | - | |
85 | | - | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
86 | 82 | | |
87 | 83 | | |
88 | 84 | | |
| |||
100 | 96 | | |
101 | 97 | | |
102 | 98 | | |
103 | | - | |
104 | | - | |
| 99 | + | |
| 100 | + | |
105 | 101 | | |
106 | 102 | | |
107 | 103 | | |
108 | 104 | | |
109 | 105 | | |
110 | 106 | | |
111 | 107 | | |
112 | | - | |
| 108 | + | |
113 | 109 | | |
114 | 110 | | |
115 | 111 | | |
116 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
117 | 163 | | |
118 | 164 | | |
119 | 165 | | |
120 | 166 | | |
121 | 167 | | |
122 | 168 | | |
123 | | - | |
| 169 | + | |
| 170 | + | |
124 | 171 | | |
125 | 172 | | |
126 | 173 | | |
| |||
130 | 177 | | |
131 | 178 | | |
132 | 179 | | |
| 180 | + | |
| 181 | + | |
133 | 182 | | |
134 | 183 | | |
135 | | - | |
| 184 | + | |
136 | 185 | | |
137 | 186 | | |
138 | 187 | | |
| |||
146 | 195 | | |
147 | 196 | | |
148 | 197 | | |
| 198 | + | |
149 | 199 | | |
150 | 200 | | |
151 | 201 | | |
| |||
179 | 229 | | |
180 | 230 | | |
181 | 231 | | |
| 232 | + | |
182 | 233 | | |
183 | 234 | | |
184 | 235 | | |
| |||
229 | 280 | | |
230 | 281 | | |
231 | 282 | | |
232 | | - | |
| 283 | + | |
233 | 284 | | |
234 | 285 | | |
235 | 286 | | |
| |||
254 | 305 | | |
255 | 306 | | |
256 | 307 | | |
257 | | - | |
| 308 | + | |
258 | 309 | | |
259 | 310 | | |
260 | 311 | | |
| |||
269 | 320 | | |
270 | 321 | | |
271 | 322 | | |
272 | | - | |
| 323 | + | |
273 | 324 | | |
274 | 325 | | |
275 | 326 | | |
| |||
284 | 335 | | |
285 | 336 | | |
286 | 337 | | |
287 | | - | |
| 338 | + | |
288 | 339 | | |
289 | 340 | | |
290 | 341 | | |
| |||
302 | 353 | | |
303 | 354 | | |
304 | 355 | | |
305 | | - | |
| 356 | + | |
306 | 357 | | |
307 | 358 | | |
308 | 359 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
24 | 24 | | |
25 | 25 | | |
26 | 26 | | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
| |||
169 | 170 | | |
170 | 171 | | |
171 | 172 | | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
172 | 176 | | |
173 | 177 | | |
174 | 178 | | |
| |||
191 | 195 | | |
192 | 196 | | |
193 | 197 | | |
| 198 | + | |
194 | 199 | | |
195 | 200 | | |
196 | 201 | | |
197 | 202 | | |
| 203 | + | |
198 | 204 | | |
199 | 205 | | |
200 | 206 | | |
| |||
212 | 218 | | |
213 | 219 | | |
214 | 220 | | |
| 221 | + | |
215 | 222 | | |
216 | 223 | | |
217 | 224 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
10 | | - | |
11 | | - | |
12 | 10 | | |
| 11 | + | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
110 | 110 | | |
111 | 111 | | |
112 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
113 | 118 | | |
114 | 119 | | |
115 | 120 | | |
| |||
144 | 149 | | |
145 | 150 | | |
146 | 151 | | |
147 | | - | |
| 152 | + | |
148 | 153 | | |
149 | 154 | | |
150 | 155 | | |
| |||
188 | 193 | | |
189 | 194 | | |
190 | 195 | | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
| 196 | + | |
209 | 197 | | |
210 | | - | |
| 198 | + | |
211 | 199 | | |
212 | 200 | | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
213 | 208 | | |
214 | 209 | | |
215 | 210 | | |
| |||
309 | 304 | | |
310 | 305 | | |
311 | 306 | | |
312 | | - | |
| 307 | + | |
313 | 308 | | |
314 | 309 | | |
315 | 310 | | |
| |||
651 | 646 | | |
652 | 647 | | |
653 | 648 | | |
654 | | - | |
| 649 | + | |
655 | 650 | | |
656 | 651 | | |
657 | 652 | | |
| |||
0 commit comments