@datadog/wasm-js-rewriter

中文文档
5.0.4发布于 2026/08/20Apache-2.0TypeScriptCommonJS构建来源已验证近 30 天下载 4702.5万

Datadog instrumentation addon for Node.js

原始使用文档
文档目录6 节
  1. Workflow
  2. Usage
  3. Configuration options
  4. Example
  5. Local setup
  6. Build

dd-wasm-js-rewriter

Node.js native AST rewriter heavily based on Speedy Web Compiler o SWC compiler used to instrument Javascript source files.

Workflow

  1. Parse Javascript code to obtain the AST
  2. Replace certain AST expressions -> Currently it is focused in certain operators like + and +=, template literals and some String methods
  3. Generate the new Javascript code as from the modified AST -> In addition to the Javascript code, the corresponding source map is returned chaining it with the original source map if necessary.

Usage

const Rewriter = require('@datadog/wasm-js-rewriter')

const rewriter = new Rewriter(rewriterConfig)
const result = rewriter.rewrite(code, filename)

Configuration options


RewriterConfig {
  // enable/disable sourceMap chaining - false by default
  chainSourceMap?: boolean

  // enable/disable comments printing - false by default
  comments?: boolean

  // establishes the prefix for the injected local variables - 6 random characters by default
  localVarPrefix?: string

  // sets the list of methods or operators to be rewritten
  csiMethods?: Array<CsiMethod>

  // extracts hardcoded string literals - true by default
  literals?: boolean
}

CsiMethod {
  // name of the String method to rewrite
  src: string

  // optional name of the replacement method. If not specified a convention shall be used
  dst?: string

  // indicates if it is an operator like +
  operator?: boolean
}

Example

const Rewriter = require('@datadog/wasm-js-rewriter')

const rewriterConfig = {
  csiMethods: [{ src: 'substring' }],
  localVarPrefix: 'test',
}

const rewriter = new Rewriter(rewriterConfig)

const code = `function sub(a) {
  return a.substring(1)
}`
const result = rewriter.rewrite(code, filename)

console.log(result.content)
/*
function sub(a) {
  let __datadog_test_0, __datadog_test_1;
  return (__datadog_test_0 = a, __datadog_test_1 = __datadog_test_0.substring, _ddiast.stringSubstring(__datadog_test_1.call(__datadog_test_0, 1), __datadog_test_1, __datadog_test_0, 1));
}
*/

Local setup

To set up the project locally, you should install cargo and wasm-pack:

$ curl https://sh.rustup.rs -sSf | sh

$ cargo install wasm-pack

and project dependencies:

$ npm install

Build

Build the project with

$ npm run build

It will compile WASM binaries by default and then it will be possible to run the tests with

$ npm t

版本与发布记录

@datadog/wasm-js-rewriter 共发布 9 个版本,首次发布于 2025/03/24;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
5.0.4latest2026/08/202 个月前
5.0.32026/08/132 个月前
5.0.22026/03/177 个月前
5.0.12025/11/1211 个月前
5.0.02025/11/061 年前
4.0.12025/04/161.6 年前

关于 @datadog/wasm-js-rewriter 的常见问题

@datadog/wasm-js-rewriter 是什么?

Datadog instrumentation addon for Node.js

如何安装 @datadog/wasm-js-rewriter?

在项目目录运行 npm install @datadog/wasm-js-rewriter,或使用 pnpm add / yarn add / bun add。

@datadog/wasm-js-rewriter 支持 TypeScript 吗?

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

@datadog/wasm-js-rewriter 支持 ESM 还是 CommonJS?

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

@datadog/wasm-js-rewriter 需要什么版本的 Node.js?

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

@datadog/wasm-js-rewriter 可以商用吗?

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

@datadog/wasm-js-rewriter 的下载量有多少?

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

@datadog/wasm-js-rewriter 还在维护吗?

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