@contrast/patcher

中文文档
1.43.1发布于 2026/11/05SEE LICENSE IN LICENSETypeScriptCommonJS近 30 天下载 1.7万

Advanced monkey patching--registers hooks to run in and around functions

原始使用文档

@contrast/patcher

Advanced monkey patching: registers hooks to run before, around, and after functions, methods, and class constructors.

Usage

@contrast/agentify composes the patcher onto core as core.patcher.

// patch a method in place; returns the object
core.patcher.patch(obj, 'method', {
  name: 'lib.obj.method',
  patchType: 'assess-dataflow-source',
  pre(data) {},
  around(next, data) { return next(); },
  post(data) {},
});

// patch a function (or class constructor) directly; returns the patched function
const patched = core.patcher.patch(fn, { name: 'lib.fn', patchType: 'protect-input-analysis', post(data) {} });

Options

  • name (required): identifies the patched function, e.g. 'http.Server.prototype.emit'.
  • patchType (required): the category of instrumentation, e.g. assess-dataflow-source or protect-input-analysis.
  • pre(data): runs before the original.
  • around(next, data): wraps the original. Call next() to run it (or the next around hook) and return its result.
  • post(data): runs after the original. data.result holds the return value, and assigning to it changes what the caller receives.

Hook data includes args, obj (the this value), orig (the original function), hooked (the patched function), name, and funcKey.

Hooks are keyed by `${patchType}:${name}`. Patching the same function again with the same key is ignored, so different features can hook one function by using different patchType values.

unpatch(fn, name, patchType) removes one hook, unwrap(fn) returns the original function, and isContrastHooked(fn) reports whether a function is patched. Type definitions are in lib/index.d.ts.

版本与发布记录

@contrast/patcher 共发布 67 个版本,首次发布于 2022/06/16;过去 12 个月发布了 19 个版本。

版本发布日期距今
1.43.1latest2026/11/056 天前
1.43.02026/10/3011 天前
1.42.02026/10/2318 天前
1.41.02026/10/1724 天前
1.40.02026/08/192 个月前
1.39.02026/07/313 个月前

关于 @contrast/patcher 的常见问题

@contrast/patcher 是什么?

Advanced monkey patching--registers hooks to run in and around functions

如何安装 @contrast/patcher?

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

@contrast/patcher 支持 TypeScript 吗?

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

@contrast/patcher 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@contrast/patcher 需要什么版本的 Node.js?

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

@contrast/patcher 可以商用吗?

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

@contrast/patcher 的下载量有多少?

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

@contrast/patcher 还在维护吗?

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