0.8.4 • Published 6 months ago

@haohanyang/shadowrealm-api v0.8.4

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
6 months ago

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

IEEdgeFirefoxChromeSafariOpera
1429132182191
414910.1336
14.1

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

Use polyfills:

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

6 months ago

0.8.3

6 months ago

0.8.2

6 months ago