@endo/module-source

中文文档
1.5.0发布于 2026/08/14Apache-2.0TypeScriptES Module近 30 天下载 9.5万

Ponyfill for the SES ModuleSource and module-to-program transformer

原始使用文档
文档目录4 节
  1. Source maps
  2. XS Specific Variant
  3. CommonJS-Specific Variant
  4. Bug Disclosure

ModuleSource

This package provides a ponyfill for the ModuleSource constructor, suitable for use in the SES shim's module descriptors. The module source accepts a JavaScript module and converts it into a form that SES can use to emulate and confine JavaScript modules (ESMs, the mjs file format) with compartments.

import 'ses';
import { ModuleSource } from '@endo/module-source';

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 ModuleSource(moduleText, moduleLocation);
  },
});

Source maps

The ModuleSource 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 ModuleSource 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 ModuleSource(source, sourceUrl);
// or
new ModuleSource(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.

XS Specific Variant

With the xs condition, @endo/module-source will not entrain Babel and will just adapt the native ModuleSource to the older interface presented by this package. That is, the XS native bindings will be translated to imports, exports, and reexports getters. This form of ModuleSource ignores all options.

CommonJS-Specific Variant

In the default/Node variant of @endo/module-source, a CjsModuleSource constructor provides a similar interface for CommonJS modules. It accepts the same options as ModuleSource and produces a CjsModuleSourceRecord object containing the analysis data plus a pre-built functor source string.

Note: the xs condition currently provides only ModuleSource; CjsModuleSource is not available under xs.

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/module-source 共发布 14 个版本,首次发布于 2024/07/31;过去 12 个月发布了 3 个版本。

版本发布日期距今
1.5.0latest2026/08/142 个月前
1.4.12026/04/166 个月前
1.4.02026/02/278 个月前
1.3.32025/07/121.3 年前
1.3.22025/06/181.4 年前
1.3.12025/06/031.4 年前

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

@endo/module-source 是什么?

Ponyfill for the SES ModuleSource and module-to-program transformer

如何安装 @endo/module-source?

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

@endo/module-source 支持 TypeScript 吗?

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

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

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

@endo/module-source 可以商用吗?

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

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

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

@endo/module-source 还在维护吗?

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