@haohanyang/shadowrealm-api
TypeScript icon, indicating that this package has built-in type declarations

0.8.4 • Public • Published

ShadowRealm API Polyfill

A implementation of the ShadowRealm API Proposal, a JavaScript sandbox, test with TC39 Test262 cases.

declare class ShadowRealm {
    constructor();
    evaluate(sourceText: string): Primitive | Function;
    importValue(specifier: string, bindingName: string): Promise<Primitive | Function>;
}

Try it now 🎉

Install

npm i -S @haohanyang/shadowrealm-api

Usage

Ponyfill: non-invasive

import ShadowRealm from '@haohanyang/shadowrealm-api'

const realm = new ShadowRealm();

Polyfill: patch up the global object

import '@haohanyang/shadowrealm-api/dist/polyfill'

const realm = new ShadowRealm();

Debugging

Print internal info for debugging

ShadowRealm.__debug = true;

Limitations

  1. All code evaluated inside a ShadowRealm runs in strict mode;
  2. The ESM statement must not contain redundant comments;
// ❌
import/* */defaultExport from "module-name";
export default/* */'xxx';

// ✅
import defaultExport from "module-name";
export default 'xxx';
  1. Exporting variable declarations is not supported;
// ❌
export const obj = {...}, fn = () => {...};

// ✅
const obj = {...}, fn = () => {...};
export { obj, fn };

Compatibility

IE Edge Firefox Chrome Safari Opera
14 29[1][2] 32[1][2] 8[2][3] 19[1][2]
41 49 10.1[3] 36
14.1

Notes:

  1. Don't support destructuring assignment in ESM statement;
  2. Need fetch polyfill in top window;
  3. Need URL polyfill in top window;

Use polyfills:

import "fetch polyfill";
import "URL polyfill";
import "@haohanyang/shadowrealm-api/dist/polyfill";
// Your codes

Package Sidebar

Install

npm i @haohanyang/shadowrealm-api

Weekly Downloads

7

Version

0.8.4

License

Apache-2.0

Unpacked Size

39.6 kB

Total Files

15

Last publish

Collaborators

  • haohanyang