@contrast/esm-hooks

中文文档
2.47.1发布于 2026/11/05SEE LICENSE IN LICENSEES Module近 30 天下载 1.5万

Support for loading and instrumenting ECMAScript modules

原始使用文档

@contrast/esm-hooks

ECMAScript module (ESM) loading and instrumentation for the Contrast Node.js agent. You don't use this package directly. The agent's ESM entry point loads it when an application starts with node --import @contrast/agent (or --loader on Node.js 18.7 to 18.18). See the @contrast/agent README for how to start an application.

How it works

The hooks install after the agent onboards with Contrast, so they know whether Assess, Protect, or both are active. When Module.register() is available, the hooks run on Node's loader thread. Otherwise they are exported for the --loader entry point. import-in-the-middle is registered earlier, as soon as esm-hooks is composed onto core, and only wraps a short list of packages (wrapPackages).

  • Resolve hook (lib/hooks.mjs) redirects modules that need instrumentation to wrappers under lib/redirects/ (builtin/, cjs/, esm/), tagging the URL with ?csi-flag=builtin, commonjs, or module.
  • Load hook (lib/hooks.mjs) serves those redirect files and source rewritten by @contrast/rewriter.
  • The loader thread and the main thread communicate over a MessageChannel (lib/post-message/).

Debugging

Set CSI_HOOKS_LOG to a bitmask to log hook activity:

ValueLogs
1ESM load() and CJS Module._load() calls
2ESM resolve() calls
4every CJS require() and compile

Combine bits to log more than one, e.g. CSI_HOOKS_LOG=3 for load and resolve.

版本与发布记录

@contrast/esm-hooks 共发布 118 个版本,首次发布于 2022/06/16;过去 12 个月发布了 23 个版本。

版本发布日期距今
2.47.1latest2026/11/056 天前
2.47.02026/10/3011 天前
2.46.02026/10/2318 天前
2.45.02026/10/1724 天前
2.44.02026/09/261 个月前
2.43.02026/08/192 个月前

关于 @contrast/esm-hooks 的常见问题

@contrast/esm-hooks 是什么?

Support for loading and instrumenting ECMAScript modules

如何安装 @contrast/esm-hooks?

在项目目录运行 npm install @contrast/esm-hooks,或使用 pnpm add / yarn add / bun add。

@contrast/esm-hooks 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/contrast__esm-hooks 是否存在。

@contrast/esm-hooks 支持 ESM 还是 CommonJS?

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

@contrast/esm-hooks 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.9.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@contrast/esm-hooks 可以商用吗?

@contrast/esm-hooks 使用 SEE LICENSE IN LICENSE 许可。许可条款写在包内的独立文件中,请阅读该文件确认可否商用。以上为概括,不构成法律意见,请以许可原文为准。

@contrast/esm-hooks 的下载量有多少?

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

@contrast/esm-hooks 还在维护吗?

最新版本 2.47.1 发布于 2026/11/05(6 天前);至今共发布 118 个版本,其中过去 12 个月发布了 23 个;首次发布于 2022/06/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。