node icon indicating copy to clipboard operation
node copied to clipboard

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

Open legendecas opened this issue 1 year ago • 7 comments

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 avatar Feb 08 '23 07:02 legendecas

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

cjihrig avatar Feb 08 '23 08:02 cjihrig

I'm experimenting with a local setup to expose Web globals like URL in ShadowRealm (based on https://github.com/nodejs/node/pull/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.

legendecas avatar Feb 09 '23 17:02 legendecas

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

mcollina avatar Feb 09 '23 18:02 mcollina

This is sorta #28823 and #31852, right?

SimenB avatar Feb 14 '23 08:02 SimenB

@SimenB This is sorta https://github.com/nodejs/node/issues/28823 and https://github.com/nodejs/node/issues/31852, right?

Effectively, yes.

legendecas avatar Feb 14 '23 10:02 legendecas

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[bot] avatar Aug 14 '23 01:08 github-actions[bot]

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[bot] avatar Feb 13 '24 01:02 github-actions[bot]