@alwaysmeticulous/redaction

中文文档
2.339.0发布于 2026/09/11ISCTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 12.9万

Utilities for implementing common redaction logic for the Meticulous Recorder Middleware API.

原始使用文档
文档目录8 节
  1. Utilities for implementing redaction middleware
  2. Example Usage
  3. Utilties for redacting javascript objects
  4. asterixOut
  5. redactString
  6. redactUrl
  7. NestedFieldsRedactor
  8. redactRecursively

Redaction

Utilities for implementing common redaction logic for the Meticulous Recorder Middleware API.

Utilities for implementing redaction middleware

Example Usage

import {
  dropRequestHeader,
  transformJsonResponse,
  redactRecursively,
  asterixOut,
} from "@alwaysmeticulous/redaction";

const middleware = [
  dropRequestHeader("Authorization"),
  transformJsonResponse({
    urlRegExp: /https:\/\/api\.example\.com\/.*/,
    transform: (data) => {
      return {
        ...data,
        ...(data.sensitive ? { sensitive: asterixOut(data.sensitive) } : {}),
      };
    },
  }),
  transformJsonResponse({
    urlRegExp: /https:\/\/api\.example\.com\/sensitive.*/,
    transform: (data) =>
      redactRecursively(data, {
        redactString: (str, path) =>
          path[path.length - 1] === "uuid" ? str : asterixOut(str),
      }),
  }),
];

Utilties for redacting javascript objects

To make it easier to redact large complex javascript types we provide a number of helpers.

asterixOut

This ensures the redacted text is the same length and has the same whitespace as the original text, thereby allowing you to test the same layout cases.

import { asterixOut } from "@alwaysmeticulous/redaction";

const redacted = asterixOut("some sensitive text"); // returns "**** ********* ****"

redactString

Redact string intelligently redacts the string by checking for common data formats. This reduces the risk of your app error'ing during the replay of Meticulous tests due to it failing to operate on the redacted data (e.g. "***" is not a valid URL):

import { redactString } from "@alwaysmeticulous/redaction";

const redacted = redactString("some sensitive text"); // returns "**** ********* ****"
const redacted2 = redactString("[email protected]"); // returns "[email protected]"
// etc.

See redactString.spec.ts for more examples.

redactUrl

redactUrl replaces the hostname, path, query, hash, and userinfo. By default the hostname becomes redacted.com; pass baseDomain to choose a different replacement hostname:

import { redactUrl } from "@alwaysmeticulous/redaction";

redactUrl("https://customer.example/private/path");
// => "https://redacted.com/redacted/redacted"

redactUrl("https://customer.example/private/path", {
  baseDomain: "redacted.meticulous.ai",
});
// => "https://redacted.meticulous.ai/redacted/redacted"

NestedFieldsRedactor

NestedFieldsRedactor allows you to specify a redaction policy for each distinct field name (for example 'ssn' or 'email'). It'll then recursively apply this redaction policy across all nested fields inside an object. Type safety ensures that compile time errors will be produced if you provide an object that has a field name (nested at any level inside the object) that you have not specified a redaction policy for.

Basic Usage

import {
  transformJsonResponse,
  NestedFieldsRedactor,
  redactString,
} from "@alwaysmeticulous/redaction";

interface MyComplexApiType {
  details: {
    ssn: string;
    phoneNumbers: Array<{ mobile?: string; home?: string }>;
  };

  // By default we do not require redacting boolean fields.
  isUSBased: boolean;
}

// Important: include your API type explictly in the call to createRedactor (`createRedactor<MyComplexApiType>`)
const complexApiTypeRedactor =
  NestedFieldsRedactor.builder().createRedactor<MyComplexApiType>({
    strings: {
      ssn: redactString,
      mobile: redactString,
      home: redactString,
    },
  });

const middleware = [
  transformJsonResponse({
    urlRegExp: /https:\/\/api\.example\.com\/.*/,
    transform: (data: MyComplexApiType) => {
      return complexApiTypeRedactor(data);
    },
  }),
];

If you update MyComplexApiType to add a new name string field:

interface MyComplexApiType {
  name: string; // <<< ADDED

  details: {
    ssn: string;
    phoneNumbers: Array<{ mobile?: string; home?: string }>;
  };

  isUSBased: boolean;
}

But don't add a corresponding redaction policy for the new name field to the 'createRedactor' call, then your code will fail to compile. This ensures that the redaction is exhaustive.

createRedactor will force you to redact all non-enum string fields, but it won't force exhaustive redaction of other types of fields (dates, booleans, numbers, etc.). See createRedactorStrict if you need to enforce exhaustive redaction at compile time of additional data types.

With Defaults

We recommend however using NestedFieldsRedactor.builderWithDefaults(), which will provide default redactors for most common field names. If there are any string fields not covered by those defaults then the compiler will force you to specify a redaction policy for them:

const complexApiTypeRedactor =
  NestedFieldsRedactor.builderWithDefaults().createRedactor<MyComplexApiType>({
    strings: {
      // Don't need to specify a redaction policy for `ssn` as it's covered by the defaults,
      // but we do need to specify a redaction policy for `mobile` and `home` as they're not covered by the defaults.
      mobile: redactString,
      home: redactString,
    },
  });

Pattern Based Redactors

You can also specify redactors that match field names that end with a given postfix, while preserving compile-time type safety. See common-redactors.ts and redact-nested-fields.ts for some examples.

redactRecursively

Recursively iterates through a JSON object applying the provided redaction function. See redact-recursively.spec.ts for more details.

This can be combined with NestedFieldsRedactor to provide extra safety. For example:

const complexApiTypeRedactor =
  NestedFieldsRedactor.builder().createRedactor<MyComplexApiType>({
    strings: {
      ssn: redactString,
      mobile: redactString,
      home: redactString,
    },
  });

const redactAnythingThatLooksLikeAnSSN = <T>(data: T) =>
  redactRecursively(data, {
    redactString: (str) => (looksLikeAnSSN(str) ? asterixOut(str) : str),
  });

const middleware = [
  transformJsonResponse({
    urlRegExp: /https:\/\/api\.example\.com\/.*/,
    transform: (data: MyComplexApiType) => {
      return redactAnythingThatLooksLikeAnSSN(complexApiTypeRedactor(data));
    },
  }),
];

版本与发布记录

@alwaysmeticulous/redaction 共发布 122 个版本,首次发布于 2024/10/15;过去 12 个月发布了 38 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.339.0latest2026/09/111 个月前
2.333.12026/08/262 个月前
2.283.12026/05/145 个月前
2.280.02026/05/056 个月前
2.277.02026/04/246 个月前
2.276.22026/04/236 个月前

其他发布通道:next → 2.226.1-beta.0。

关于 @alwaysmeticulous/redaction 的常见问题

@alwaysmeticulous/redaction 是什么?

Utilities for implementing common redaction logic for the Meticulous Recorder Middleware API.

如何安装 @alwaysmeticulous/redaction?

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

@alwaysmeticulous/redaction 支持 TypeScript 吗?

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

@alwaysmeticulous/redaction 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@alwaysmeticulous/redaction 可以商用吗?

@alwaysmeticulous/redaction 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@alwaysmeticulous/redaction 的下载量有多少?

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

@alwaysmeticulous/redaction 还在维护吗?

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