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

Node.js built-in module support in the vm context #46558

Open
legendecas opened this issue Feb 8, 2023 · 10 comments
Open

Node.js built-in module support in the vm context #46558

legendecas opened this issue Feb 8, 2023 · 10 comments
Labels
feature request Issues that request new features to be added to Node.js. never-stale Mark issue so that it is never considered stale realm Issues and PRs related to the ShadowRealm API and node::Realm vm Issues and PRs related to the vm subsystem.

Comments

@legendecas
Copy link
Member

Scripts/Modules running in the context created with vm.createContext can not access various Node.js built-in apis/modules like URL, node:assert, node:http, etc. This makes it cumbersome to create a disposable context for use cases like hot-module-reload to run existing node.js apps.

We can provide a built-in API to create a context (or a new NodeRealm for compatibility) with full-fledged Node.js built-in modules support. It allows object exchanges between realms and shares the same loop with the main context.
, similar to the existing vm.context.

/cc @mcollina @nodejs/realm @nodejs/vm

@legendecas legendecas added vm Issues and PRs related to the vm subsystem. feature request Issues that request new features to be added to Node.js. realm Issues and PRs related to the ShadowRealm API and node::Realm labels Feb 8, 2023
@cjihrig
Copy link
Contributor

cjihrig commented Feb 8, 2023

Thanks for opening this @legendecas. Do you have any plan or idea for next steps towards a NodeRealm?

@legendecas
Copy link
Member Author

legendecas commented Feb 9, 2023

I'm experimenting with a local setup to expose Web globals like URL in ShadowRealm (based on #46556). I believe many infrastructures can be shared between ShadowRealm and the NodeRealm. The following steps for NodeRealm can be:

  • Defines the APIs to create a VM context with Node.js built-ins.
    • CommonJS (e.g. createRequire from the outer realm).
    • ES Modules (e.g. import from the outer realm).
    • Synthetic modules (vm.Script and vm.Module)
  • Defines the APIs to be exposed in the NodeRealm (Similar to https://docs.google.com/document/d/12_CkX6KbM9kt_lj1pdEgLB8-HQaozkJb7_nwQnHfTTg/edit#heading=h.drlp0amd3twr):
    • globalThis.process events like uncaughtException and unhandledRejection.
    • Should the built-in modules that can manipulate the process state and isolate state be exposed in the realm? e.g. node:v8 and process.exit.
  • For each built-in modules that can be exposed in the NodeRealm:
    • Verify their binding data and handles/requests can be disposed once the realm is being disposed.
    • Run existing tests in the NodeRealm.

@mcollina
Copy link
Member

mcollina commented Feb 9, 2023

Regarding process.exit(): it should "close" the Realm, similarly to how it works in worker_threads

@SimenB
Copy link
Member

SimenB commented Feb 14, 2023

This is sorta #28823 and #31852, right?

@legendecas
Copy link
Member Author

@SimenB This is sorta #28823 and #31852, right?

Effectively, yes.

@github-actions
Copy link
Contributor

There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions github-actions bot added the stale label Aug 14, 2023
@mcollina mcollina removed the stale label Aug 16, 2023
Copy link
Contributor

There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment.

For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions github-actions bot added the stale label Feb 13, 2024
@mcollina mcollina removed the stale label Feb 13, 2024
Copy link
Contributor

There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the never-stale Mark issue so that it is never considered stale label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment.
For more information on how the project manages feature requests, please consult the feature request management document.

@github-actions github-actions bot added the stale label Aug 12, 2024
@legendecas legendecas added never-stale Mark issue so that it is never considered stale and removed stale labels Aug 12, 2024
@karankraina
Copy link

karankraina commented Oct 12, 2024

@mcollina @cjihrig @addaleax I tried to achieve something of this sort in my project and ended up creating a package.

Can you guys have a look and see if this is relevant to what we need here ?

https:/karankraina/safer-vm/blob/main/src/context.ts

The idea is from node core only -

const globalBuiltins =

@mcollina
Copy link
Member

@karankraina not really. You are exposing the parent native objects, while we are talking about exposing them natively in child. This would ensure safety.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. never-stale Mark issue so that it is never considered stale realm Issues and PRs related to the ShadowRealm API and node::Realm vm Issues and PRs related to the vm subsystem.
Projects
Status: Awaiting Triage
Development

No branches or pull requests

5 participants