Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

shadowrealm-api

ambit-tsai2.8kApache-2.00.8.3TypeScript support: included

ShadowRealm polyfill for browser

shadowrealm, shadowrealms, realm, realms, sandbox, polyfill, ponyfill, shim, shims

readme

ShadowRealm API Polyfill

依照 ShadowRealm API 提案实现的 JavaScript 沙箱,使用 TC39 Test262 的用例进行测试。

简体中文 | English

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

✨ 在线试用一下

安装

npm i -S shadowrealm-api

使用

Ponyfill: 无侵入性

import ShadowRealm from 'shadowrealm-api'

const realm = new ShadowRealm();

Polyfill: 修补全局对象

import 'shadowrealm-api/dist/polyfill'

const realm = new ShadowRealm();

调试

打印内部调试信息

ShadowRealm.__debug = true;

限制

  1. 在 ShadowRealm 中运行的所有代码都处于严格模式下;
  2. ESM 语句不能含有冗余的注释; `js // ❌ import/* /defaultExport from "module-name"; export default/ */'xxx';

// ✅ import defaultExport from "module-name"; export default 'xxx';

3. 不支持导出变量声明;
```js
// ❌
export const obj = {...}, fn = () => {...};

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

兼容性

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. ESM 语句不支持解构赋值;
  2. 顶层作用域需要fetch垫片;
  3. 顶层作用域需要URL垫片;

使用垫片:

import "fetch polyfill";
import "URL polyfill";
import "shadowrealm-api/dist/polyfill";
// 你的代码

联系

  1. 微信: cai_fanwei
  2. QQ群: 663286147
  3. 邮箱: ambit_tsai@qq.com