Skip to content

Commit

Permalink
feat(compartment-mapper): support for dynamic requires
Browse files Browse the repository at this point in the history
This change adds support for _dynamic requires_, where a Node.js script does something like this:

```js
const someModuleSpecifier = getSpecifier();
const idk = require(someModuleSpecifier);
```

This behavior is not enabled by default; specific options and Powers are required. See the signature of `loadFromMap()` in `import-lite.js` for more information. Dynamic loading of exit modules (e.g., if `someModuleSpecifier` was `node:fs`) is handled by a user-provided "exit module import hook". Policy applies. Dynamic requires work as you'd probably expected _except_:

- A compartment may dynamically require from its *dependee*, since this is a common pattern in the ecosystem (see [node-gyp-build](https://npm.im/node-gyp-build)).
- The special "attenuators" compartment may not be dynamically required. Horsefeathers!

Some relevant bits, if you're mining history:

- All internal parsers are now _synchronous_, which was made possible by the introduction of `evadeCensorSync` in `@endo/evasive-transform`.
- Async parsers are still supported, but they would only be user-defined. Async parsers are incompatible with dynamic requires.
- Added property `{Set<string>} compartments` to `CompartmentDescriptor`. This is essentially a view into the compartment names within `CompartmentDescriptor.scopes`.
- The `mapParsers()` function has moved into `map-parser.js`.
- `@endo/trampoline` is leveraged in a couple places (`import-hook.js`, `map-parser.js`) to avoid code duplication.
- Introduced `FsInterface`, `UrlInterface`, `CryptoInterface` and `PathInterface` (and their ilk) for use with `makeReadPowers()` and the new `makeReadNowPowers()`.
  • Loading branch information
boneskull committed Oct 1, 2024
1 parent 4b6d8e6 commit 25724bf
Show file tree
Hide file tree
Showing 57 changed files with 2,318 additions and 463 deletions.
7 changes: 7 additions & 0 deletions packages/compartment-mapper/NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
User-visible changes to `@endo/compartment-mapper`:

# Next release

- Adds support for dynamic requires in CommonJS modules. This requires specific
configuration to be passed in (including new read powers), and is _not_
enabled by default. See the signature of `loadFromMap()` in `import-lite.js`
for details.

# v1.2.0 (2024-07-30)

- Fixes incompatible behavior with Node.js package conditional exports #2276.
Expand Down
6 changes: 5 additions & 1 deletion packages/compartment-mapper/node-powers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export { makeReadPowers, makeWritePowers } from './src/node-powers.js';
export {
makeReadPowers,
makeWritePowers,
makeReadNowPowers,
} from './src/node-powers.js';
// Deprecated:
export { makeNodeReadPowers, makeNodeWritePowers } from './src/node-powers.js';
1 change: 1 addition & 0 deletions packages/compartment-mapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
"dependencies": {
"@endo/cjs-module-analyzer": "workspace:^",
"@endo/module-source": "workspace:^",
"@endo/trampoline": "workspace:^",
"@endo/zip": "workspace:^",
"ses": "workspace:^"
},
Expand Down
Loading

0 comments on commit 25724bf

Please sign in to comment.