Skip to content

Add support for requiring node internal modules with "node:" prefix#134

Open
Lily0603 wants to merge 1 commit into
metarhia:masterfrom
Lily0603:prefix-support
Open

Add support for requiring node internal modules with "node:" prefix#134
Lily0603 wants to merge 1 commit into
metarhia:masterfrom
Lily0603:prefix-support

Conversation

@Lily0603

@Lily0603 Lily0603 commented Feb 2, 2026

Copy link
Copy Markdown
  • tests and linter show no problems (npm t)
  • tests are added/updated for bug fixes and new features
  • code is properly formatted (npm run fix)
  • description of changes is added in CHANGELOG.md
  • update .d.ts typings
    Support node: prefix for built-in modules #95

Comment thread metavm.js
const builtin = name.startsWith('node:');
if (builtin) {
const builtinName = name.slice(5);
if (builtinModules.includes(builtinName)) return internalRequire(name);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I have strong concerns about exposing raw result of interal require. The results are cached and potentialy be shared between different modules which defeats metavm isolations purposes

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The internalRequire was added before my changes.
Also node: guarantees that exactly the built-in module is loaded, and not something from the cache or the file system.

Built-in modules can be identified using the node: prefix, in which case it bypasses the require cache. For instance, require('node:http') will always return the built in HTTP module, even if there is require.cache entry by that name

https://nodejs.org/api/modules.html#built-in-modules

Comment thread test/string.js
Comment on lines +91 to +92
assert.strictEqual(typeof ms.exports, 'object');
assert.strictEqual(typeof ms.exports.fs.promises, 'object');

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It might be worth doing more than just checking that the exports is an object.
To ensure the correct requiring built-in modules from node namespace, we could assert the presence of a few "Stable" API functions, rather than relying on shallow type checks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants