Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix dynamic require of node:-namespaced builtins #2755

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

boneskull
Copy link
Contributor

@boneskull boneskull commented Apr 3, 2025

Closes: #2754

Description

This fixes a couple of bugs, actually:

  • inferExports no longer throws when encountering the plainly-invalid-but-first-discovered-in-the-wild export alias of ./. This alias is now ignored.
  • importNowHook now handles dynamically-required builtins without considering them to first be packages, which a) is faster and b) avoids a potential thrown exception when a FileUrlToPathFn encounters a module specifier that appears to have a scheme but is not the file: scheme (like node:fs). This was the root cause of the issue in compartment-mapper: dynamic require of "node:"-prefixed builtins fails #2754.

And some tangents:

  • While fixing my inferExports issue, I refactored type signatures to use PackageDescriptor instead of object. This required defining a few more fields in PackageDescriptor which infer-exports.js considers.
  • WILDCARD_POLICY_VALUE is now interally-exported from policy-format.js which should probably be used instead of 'any' in the context of policy (wherever possible). It's now used in the test suite for dynamic requires.

And with shame:

  • I had refactored the type signature for mapParsers to have the signature that I had originally intended. And I didn't feel like it deserved an entire PR. This is completely unrelated to anything else here

Security Considerations

n/a

Scaling Considerations

It should be faster for @endo/compartment-mapper to crunch dynamically-required builtins.

Documentation Considerations

I don't think this needs any documentation changes.

Testing Considerations

I've added coverage for #2754, but did not add coverage for anything else. I should probably add coverage for the change to inferExports.

Compatibility Considerations

Backwards compatible

Upgrade Considerations

I saw this in a package somewhere; it is plainly invalid. But it would throw an exception unless we ignore it.
@boneskull boneskull self-assigned this Apr 3, 2025
@boneskull boneskull added the bug Something isn't working label Apr 3, 2025
It will now return `AsyncParseFn` xor `SyncParseFn` instead of a union of the two.  The function now requires a boolean value for `preferSynchronous`.

This is an internal function, so this isn't a breaking change.

Type arguments are unfriendly to default parameter values, which is why `= false` needed removal.
@boneskull boneskull force-pushed the boneskull/fix-dynamic-builtin-2754 branch from 93c3756 to 133bc0e Compare April 3, 2025 00:25
@boneskull boneskull requested review from kriskowal and naugtur April 3, 2025 00:26
boneskull added a commit to LavaMoat/LavaMoat that referenced this pull request Apr 5, 2025
- add failing test for dynamic require of node:-namespaced builtin modules (see endojs/endo#2755)
closes #2754

Dynamically-required builtins were going through the entire candidate-specifier-guess-'n'-check loop, which shouldn't happen.

Specifically, the call to a `MaybeReadNowFn` in this loop caused the original error as reported in #2754 because it uses a `FileUrlToPathFn`; the default implementation (Node.js' `url.fileURLToPath`) throws when provided `node:foo` _but does not throw_ when provided `foo`.

Also exports `WILDCARD_POLICY_VALUE` from `policy-format` so we can use it instead of magic strings all over the place.

Fixes #2754
@boneskull boneskull force-pushed the boneskull/fix-dynamic-builtin-2754 branch from 133bc0e to 24b48c4 Compare April 5, 2025 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

compartment-mapper: dynamic require of "node:"-prefixed builtins fails
1 participant