@endo/static-module-record

中文文档
1.1.2发布于 2024/05/08Apache-2.0TypeScriptES Module近 30 天下载 14.5万

Shim for the SES StaticModuleRecord and module-to-program transformer

原始使用文档

StaticModuleRecord

This package provides a shim for the StaticModuleRecord constructor, suitable for use with the SES shim importHook. The static module record accepts a JavaScript module and converts it into a form that SES can use to securely emulate JavaScript modules (ESMs, the mjs file format) with compartments.

import 'ses';
import { StaticModuleRecord } from '@endo/static-module-record`;

const c1 = new Compartment({}, {}, {
  name: "first compartment",
  resolveHook: (moduleSpecifier, moduleReferrer) => {
    return resolve(moduleSpecifier, moduleReferrer);
  },
  importHook: async moduleSpecifier => {
    const moduleLocation = locate(moduleSpecifier);
    const moduleText = await retrieve(moduleLocation);
    return new StaticModuleRecord(moduleText, moduleLocation);
  },
});

Source maps

The StaticModuleRecord is a shim for what we hope to eventually call a native ModuleSource constructor. However, in the absence of a native ModuleSource, this produces a serializable object that emulates the behavior of ModuleSource in conjunction with the Compartment constructor from ses. A detail that leaks from the implementation is that the constructor rewrites the source, from an ESM [[Module]] grammar construction to a [[Program]] construction suitable for confining with the compartment's confined evaluator.

This transform attempts to be unobtrusive, but currently causes some alignment changes due to (hopefully temporary) limitations to the underlying code generator. In the interim, generating a source map can help.

The StaticModuleRecord constructor accepts non-standards-track sourceMapHook and sourceMapUrl options.

Previously, the sole option was a string argument for the sourceUrl, such that this would be appended to the generated source. This change allows for the old or new usage:

new StaticModuleRecord(source, sourceUrl);
// or
new StaticModuleRecord(source, { sourceUrl, sourceMapUrl, sourceMapHook });

The sourceMapUrl is necessary for generating a source map. The URL will appear only in the generated source map, so a fully qualified source map URL is safe and allows for continuity if the map is generated and debugged on the same host. This is important because Endo captures precompiled Static Module Records in bundles, excluding source maps, such that a relative path is not useful.

The sourceMapHook will receive a string source map and a details bag including:

  • source
  • sourceUrl
  • sourceMapUrl

Such that the receiver can store the source map somewhere as a side-effect.

Note: the sourceMapHook is synchronous and returns void. Exceptions thrown by the hook will propagate up through the constructor. If the hook returns a promise, it will be dropped and rejections will go uncaught. If the hook must do async work, these should be queued up as a job that the caller can later await.

Bug Disclosure

Please help us practice coordinated security bug disclosure, by using the instructions in SECURITY.md to report security-sensitive bugs privately.

For non-security bugs, please use the regular Issues page.

版本与发布记录

@endo/static-module-record 共发布 52 个版本,首次发布于 2021/06/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.2latest2024/05/082 年前
1.1.12024/04/052 年前
1.1.02024/03/212 年前
1.0.42024/02/232 年前
1.0.32024/02/152 年前
1.0.22024/01/182 年前

关于 @endo/static-module-record 的常见问题

@endo/static-module-record 是什么?

Shim for the SES StaticModuleRecord and module-to-program transformer

如何安装 @endo/static-module-record?

在项目目录运行 npm install @endo/static-module-record,或使用 pnpm add / yarn add / bun add。

@endo/static-module-record 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@endo/static-module-record 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@endo/static-module-record 可以商用吗?

@endo/static-module-record 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@endo/static-module-record 的下载量有多少?

根据 npm 官方下载统计,@endo/static-module-record 在 2026/10/14 – 2026/11/12 这 30 天内被下载 144,787 次,日均约 4,826 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@endo/static-module-record 还在维护吗?

最新版本 1.1.2 发布于 2024/05/08(2 年前);至今共发布 52 个版本,其中过去 12 个月发布了 0 个;首次发布于 2021/06/02。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。