ECMAScript Realms
Stage 2, requesting Stage 3�
TC39 November, 2020� https://github.com/tc39/proposal-realms/
Realm API
Realm API
Perks
Motivations
Prior art
HTML behavior: (PR)�Synthetic Realms behave like parent Realm
Previous key concerns
Ongoing TAG Review
Some clarifications from the ongoing TAG review
New questions from the TAG review
Realms only have the built-in JS APIs available
Yes.
Could hosts add properties if they want to?
The current proposal explains that hosts are expected to not add properties to the Realm's global object on construction. We are considering making this a requirement. (#284)
The HTML integration PR does not add any properties to Realms.
Many libraries won't work unless they add dependencies manually
Yes, the clean slate of Realms is one of the goals of this proposal.
The current Realm cannot be accessed from within the Incubator Realm
Executed code doesn't need to know it's in a realm. Ideally, code executed in a realm would run seamlessly.
Realms API has only `import()` and `globalThis`
This is our MVP max/min proposal. We may explore extensions in the future but not during this proposal.
Frequently Raised Aspects
Not a security boundary and does not encourage insecure application architecture.
Security means different things to many people, but what is worth of web applications, Realms won't introduce a new security boundary.
The Realms are not promoting safe zones for experimentation of insecure features in applications.
The goals aims to the integrity of componentized applications, that is partially done today with prior art features, such as iframes, Node vm, JSCore JSContextGroupCreate(), Android's V8 Context::New, etc.
Async is not an option for virtualization
The mentioned goal for usage with componentized applications requires synchronous execution to get proper states and tailor changes of web applications such as manipulating the DOM. E.g. the Google AMP Virtualization
There are many goals towards process and thread level isolations and we see the Realms as a complement to those, not a replacement or an immediate alternative.
Our place at TC39 and the Web Platform.
Most of the current concerns are related to the current ongoing HTML integration.
There is a given concern on differentiating primordials from the web platform globals to users. We believe this has sailed with NodeJS and the respective native platforms exposing V8 and JSCore to developers.
The de-facto distribution model for JS code via NPM already highlight this issue extensively, and developers, and more important, the tools available for developers, have helped to mitigate this in some extend.
No need to change application configuration to evaluate code
The Realms API does not require enabling eval, much less encourages it.
The usage of dynamic import - realmObj.import() - is strongly encouraged to properly execute code in a synthetic Realm.
There is realmObj.globalThis.eval(), subject to CSP policy, but the given access to the current state of the Realm's globalThis and operation through dynamic import creates a much more appealing approach.
Realms might not be as complex as it seems.
We've seen many applications and usages for Realms while working on this proposal, including reports of multiple sandbox and virtualization frameworks.
Complexity here seems like a opt-in for the user who wants to use Realms, but not to the code to be run in a Realm.
Use Cases
In the last update we covered the following use cases:
Trusted Scripts
Trusted Scripts: Plugin Example
import { api } from 'plugingFramework';
const realm = new Realm();
realm.globalThis.api = api;
await realm.import('./plugin1.js');
Code Testing
Code Testing: Running tests in a realm
import { test } from 'testFramework';
const realm = new Realm();
realm.globalThis.test = test;
await realm.import('./main-spec.js');
test.report();
Code Testing: Running test FW and tests in a realm
const realm = new Realm();
const [ framework, { tap } ] = await Promise.all([
realm.import('testFramework'),
realm.import('reporters')
]);
framework.use(tap);
await realm.import('./main-spec.js');
Codebase segmentation
DOM Virtualization
DOM Virtualization: AMP WorkerDOM Challenge
google.com
AMP
Worker
(Virtual DOM)
???.cdn.ampproject.org
Runs AMP Framework
Runs NYT Scripts
async comm
Problem: Element.getBoundingClientRect() doesn't work over async comm channels (i.e. worker-dom)
Preloaded�
cross-domain iframe
DOM Virtualization
import virtualDocument from 'virtual-document';
const realm = new Realm();
realm.globalThis.document = virtualDocument;
await realm.import('./publisher-amin.js');
Previous key concerns
This resolves #238: "Realms is counter to the current design principles of the web platform"
Other clarifications
Evaluation and security
Evaluation and security
Own module graph
const realm = new Realm();
// imports code that executes within its own environment.
const { doSomething } = await realm.import('./file.js');
doSomething();
Stage 3?
Next steps and requesting stage 3 in January
Review