Skip to content

feat: add user module registration for reusable ES modules#45

Open
simongdavies wants to merge 3 commits intohyperlight-dev:mainfrom
simongdavies:add-modules
Open

feat: add user module registration for reusable ES modules#45
simongdavies wants to merge 3 commits intohyperlight-dev:mainfrom
simongdavies:add-modules

Conversation

@simongdavies
Copy link
Copy Markdown
Contributor

Add add_module/remove_module/clear_modules API to JSSandbox, allowing handlers (and other modules) to import reusable ES modules using the 'namespace:name' convention (default namespace: 'user').

Guest runtime:

  • Add UserModuleLoader (Resolver + Loader) for lazy compilation
  • Add register_module guest function
  • Modules compiled on first import, avoiding ordering issues

Host library (hyperlight-js):

  • Add modules HashMap to JSSandbox with full CRUD API
  • add_module / add_module_ns / remove_module / remove_module_ns / clear_modules
  • Modules registered before handlers in get_loaded_sandbox()
  • Export DEFAULT_MODULE_NAMESPACE constant
  • Namespace validation: 'host' reserved, no colons allowed

NAPI wrapper (js-host-api):

  • Add addModule / removeModule / clearModules on JSSandboxWrapper
  • Error enrichment wrapping for new sync methods

Import capabilities:

  • User modules can import other user modules
  • User modules can import built-in modules (crypto, console, etc.)
  • User modules can import host function modules (host:X)
  • Missing modules fail at getLoadedSandbox() with clear errors

@simongdavies simongdavies added the kind/enhancement New feature or improvement label Mar 9, 2026
@simongdavies simongdavies force-pushed the add-modules branch 2 times, most recently from bcd0328 to 164b3f9 Compare March 9, 2026 15:31
Copy link
Copy Markdown
Contributor

@ludfjig ludfjig left a comment

Choose a reason for hiding this comment

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

Reviewed the rust code, looks good!

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds first-class “user module” registration so handlers (and other modules) can import reusable ES modules via the <namespace>:<name> convention (defaulting to user:), spanning the Rust host library, the guest runtime loader, and the Node.js NAPI wrapper.

Changes:

  • Implemented user-module storage/CRUD on JSSandbox and registration into the guest before handlers load.
  • Added guest-side lazy module resolution/loading plus a register_module guest function.
  • Added NAPI/JS wrapper APIs, docs, examples, and comprehensive Rust/JS integration tests.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/js-host-api/tests/user-modules.test.js New vitest suite covering add/remove/clear + import/lifecycle behaviors via NAPI.
src/js-host-api/src/lib.rs Adds NAPI boundary validation and sync add_module/remove_module/clear_modules methods.
src/js-host-api/lib.js Wraps new sync module methods for error-code enrichment (wrapSync).
src/js-host-api/examples/user-modules.js New Node example demonstrating module registration/imports and shared module state.
src/js-host-api/README.md Documents user module APIs and import conventions; adds a “User Modules” section.
src/hyperlight-js/tests/user_modules.rs New Rust integration tests for module registration/import behavior and lifecycle.
src/hyperlight-js/src/sandbox/js_sandbox.rs Implements module storage, namespace validation, and guest registration ordering.
src/hyperlight-js/src/lib.rs Re-exports module-related constants and validation helpers.
src/hyperlight-js/examples/user_modules/main.rs New Rust example demonstrating user modules and shared state patterns.
src/hyperlight-js/Cargo.toml Registers the new user_modules example target.
src/hyperlight-js-runtime/src/main/hyperlight.rs Adds register_module guest function entrypoint.
src/hyperlight-js-runtime/src/lib.rs Adds UserModuleLoader (resolver+loader) and runtime-side module registration storage.
README.md Updates top-level pointer to JS Host API to mention user modules/host functions.
Justfile Runs the new js-host-api user-modules.js example as part of example runs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Add add_module/remove_module/clear_modules API to JSSandbox, allowing
handlers (and other modules) to import reusable ES modules using the
'namespace:name' convention (default namespace: 'user').

Guest runtime:
- Add UserModuleLoader (Resolver + Loader) for lazy compilation
- Add register_module guest function
- Modules compiled on first import, avoiding ordering issues

Host library (hyperlight-js):
- Add modules HashMap to JSSandbox with full CRUD API
- add_module / add_module_ns / remove_module / remove_module_ns / clear_modules
- Modules registered before handlers in get_loaded_sandbox()
- Export DEFAULT_MODULE_NAMESPACE constant
- Namespace validation: 'host' reserved, no colons allowed

NAPI wrapper (js-host-api):
- Add addModule / removeModule / clearModules on JSSandboxWrapper
- Error enrichment wrapping for new sync methods

Import capabilities:
- User modules can import other user modules
- User modules can import built-in modules (crypto, console, etc.)
- User modules can import host function modules (host:X)
- Missing modules fail at getLoadedSandbox() with clear errors

Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Simon Davies <simongdavies@users.noreply.github.com>
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/enhancement New feature or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants