Description
ShadowRealm is a stage 3 TC39 proposal. ShadowRealm provides a strong guarantee on isolating object graphs between different JavaScript executing contexts and introduces a new built-in to provide a distinct global environment. ShadowRealm also ensures the module graph it uses is isolated from its outer contexts.
It may sound similar to the Node.js vm
module at the first look, but it actually opens up a different possibility for running code modules in one Isolate without interfering with each other. I've posted an issue to discuss the details about this: nodejs/node#42528.
The web is adopting ShadowRealm. They are working on exposing a curated useful web built-in APIs to the ShadowRealm, like EventTarget, Stream, etc. This is what Node.js vm
module is missing all the time: people pass the outer builtin API objects into the VM and get confused by the obscure obj instanceof Object
result, or have to find the alternatives in the wild and require them in the vm
. Apart from the built-in APIs, there are also global events like uncaughtException
and unhandledRejection
, and async_hooks
that need to be designed to fit in the model correctly.
To support exposing Node.js own built-in API in the ShadowRealm (or maybe in the vm
too!), we need to disentangle the native modules from the main context and make it possible to evaluate those native modules in the ShadowRealm's context again. This could be a lot of work since people are not aware of if a native module written in Node.js can be evaluated multiple times in one single Environment. It will be a good effort to refactor the codebase to disentangle this. Also, we may need to be able to snapshot the startup state too to boost the performance of creating the new contexts for ShadowRealm.
This work is likely to be a long-term effort and lots of decisions to make. I believe establishing a strategic initiative will help share progress and increase TSC awareness/attention on the topic. I'd be happy to champion the initiative.