Skip to content

fix dynamic require of node:-namespaced builtins #2755

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

Merged
merged 4 commits into from
Apr 15, 2025

Conversation

boneskull
Copy link
Member

@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

@boneskull boneskull self-assigned this Apr 3, 2025
@boneskull boneskull added the bug Something isn't working label Apr 3, 2025
@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)
@boneskull boneskull force-pushed the boneskull/fix-dynamic-builtin-2754 branch from 133bc0e to 24b48c4 Compare April 5, 2025 00:14
boneskull added a commit to LavaMoat/LavaMoat that referenced this pull request Apr 14, 2025
- add failing test for dynamic require of node:-namespaced builtin modules (see endojs/endo#2755)

const builtin = 'node:cluster';

require(builtin);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing newline. Windows?

@boneskull boneskull force-pushed the boneskull/fix-dynamic-builtin-2754 branch 2 times, most recently from 6413cdb to 2f244b1 Compare April 14, 2025 23:57
I saw this in a package somewhere; it is plainly invalid. But it would throw an exception unless we ignore it.
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.
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 2f244b1 to df973b2 Compare April 15, 2025 20:15
@boneskull boneskull enabled auto-merge (rebase) April 15, 2025 20:16
@boneskull boneskull merged commit 384e904 into master Apr 15, 2025
16 checks passed
@boneskull boneskull deleted the boneskull/fix-dynamic-builtin-2754 branch April 15, 2025 20:29
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
2 participants