@desert-ant-labs/redact

中文文档
4.3.0发布于 2026/11/06SEE LICENSE IN LICENSE.mdTypeScriptES Module近 30 天下载 1.6万

On-device multilingual PII redaction for JavaScript. Runs in the browser (WebAssembly + LiteRT.js) and server-side in Node (native), from one import.

原始使用文档
文档目录3 节
  1. Choosing what gets redacted
  2. Bundlers and SSR
  3. License

@desert-ant-labs/redact

On-device multilingual PII redaction for JavaScript. Finds names, addresses, emails, phone numbers, cards, IBANs, national IDs and more across 27 languages, fully locally. Full language list.

Two entries share one Redact API:

  • @desert-ant-labs/redact (default): a WebAssembly pipeline with LiteRT.js inference (XNNPACK-accelerated CPU by default, optional WebGPU), for the browser. It has no native dependencies, so a single import builds cleanly for every target of a multi-target bundler (Next.js, Remix, SvelteKit, Nuxt), including the browser bundle and the Client-Component SSR pass those frameworks render in Node. It is safe to import during server-side rendering, but LiteRT.js needs a browser (or Web Worker) to initialize, so Redact.load() runs inference only in the browser; calling it in plain Node throws an actionable error pointing you to /native.
  • @desert-ant-labs/redact/native: a prebuilt native core (LiteRT on Linux, Core ML on macOS), for server-side inference in Node. No @litertjs/core, no build tools, no flags. Import it from server-only code (API routes, server actions, plain Node scripts). Do not import it from a component that also renders in the browser.
# Browser (default entry):
npm i @desert-ant-labs/redact @litertjs/core

# Server-side inference in Node (/native entry) needs no extra install:
npm i @desert-ant-labs/redact
import { Redact } from "@desert-ant-labs/redact";

const redact = await Redact.load();            // downloads the model from HF at the pinned tag, cached
const r = await redact.redaction("Email Anna at [email protected].");

r.redactedText;      // "Email [GIVEN_NAME_1] at [EMAIL_1]."
r.items;             // detections: label, original, placeholder, confidence, offsets
const reply = await llm(r.redactedText);       // the LLM sees only placeholders
r.restore(reply);    // originals filled back in

redact.dispose();    // release the model (both builds)

Server-only code that wants the native core imports the same API from the /native subpath:

import { Redact } from "@desert-ant-labs/redact/native"; // server only

Redact.load() accepts:

  • directory: an explicit model directory to self-host / run offline (native build, or the browser build under Node); files already there are used without a download, otherwise the model is downloaded into it. Omit for the managed cache (~/.cache/desert-ant-models/...).
  • modelBaseUrl (browser build): a base URL you serve the model files from (e.g. "/assets/redact/"), loaded instead of Hugging Face for self-host / offline setups.
  • cacheRoot: base directory for the managed on-disk cache (default ~/.cache; native build, or the browser build under Node).
  • onProgress: download progress callback, fraction in [0, 1].
  • litert (browser): bring-your-own LiteRT.js module (the @litertjs/core namespace, e.g. a bundler-managed import).
  • litertWasmDir (browser): URL/path to the LiteRT.js Wasm files (defaults to the installed package, or the jsDelivr CDN in the browser).
  • accelerator (browser): "wasm" (XNNPACK CPU, default), "webgpu", or "webnn".

By default the model is downloaded from Hugging Face on first use (at the revision pinned to this package version), SHA-256 verified, and cached for later runs, so nothing model-sized ships in the npm tarball. In Node the cache is the OS cache dir; in the browser it is the fetch cache. Use directory (Node) or modelBaseUrl (browser) to self-host / run fully offline. @litertjs/core is an optional peer dependency (browser builds only).

Choosing what gets redacted

redaction(text, options) accepts:

  • minimumConfidence: minimum score for neural detections (default 0.6). Structured recognizers (email, cards, IBANs, …) always apply.
  • labels: restrict redaction to these categories. Omit for DEFAULT_LABELS.

ORG (company / organisation name) is detected but not redacted by default, because a company is not a natural person. It exists so that names like Silverfin or Odoo are recognised as organisations rather than mislabelled as a SURNAME. Opt in when you do want companies masked:

import { Redact, DEFAULT_LABELS, ALL_LABELS } from "@desert-ant-labs/redact";

await redact.redaction(text, { labels: [...DEFAULT_LABELS, "ORG"] });
await redact.redaction(text, { labels: ["EMAIL", "PHONE"] });  // only these

DEFAULT_LABELS and ALL_LABELS are frozen arrays exported from both entries.

Bundlers and SSR

The default @desert-ant-labs/redact import is safe to use directly in components: it is pure JavaScript + WebAssembly with no native modules, so bundlers can build it for the browser and for the Node SSR pass from the same module graph with no configuration.

The @desert-ant-labs/redact/native subpath loads a native addon (via koffi) and is for server-only code. If you import it inside a framework that bundles server code (for example a Next.js Route Handler or Server Action), mark it external so the bundler does not try to bundle the native binary. In Next.js:

// next.config.js
module.exports = { serverExternalPackages: ["@desert-ant-labs/redact"] };

The native server build ships for linux-x64, linux-arm64 (LiteRT), and darwin-arm64 (Core ML). Other platforms fall back to a clear error at load(); use the default WebAssembly build, the Swift package, or a browser for those.

The same model ships as a Swift package (iOS/macOS) and an Android AAR from the same repository: https://github.com/Desert-Ant-Labs/desert-ant-core

License

Desert Ant Labs Source-Available License. Free for most apps, and a commercial license is required at scale. Full terms are at the link. Licensing: [email protected].

Third-party notices for the SDK: THIRD_PARTY_NOTICES.md.

Third-party notices for the weights: THIRD_PARTY_NOTICES.md.

版本与发布记录

@desert-ant-labs/redact 共发布 36 个版本,首次发布于 2026/07/06;过去 12 个月发布了 36 个版本。

版本发布日期距今
4.3.0latest2026/11/066 天前
4.2.32026/10/3112 天前
4.2.22026/10/3112 天前
4.2.12026/10/2815 天前
4.2.02026/10/2716 天前
4.1.02026/10/2023 天前

关于 @desert-ant-labs/redact 的常见问题

@desert-ant-labs/redact 是什么?

On-device multilingual PII redaction for JavaScript. Runs in the browser (WebAssembly + LiteRT.js) and server-side in Node (native), from one import.

如何安装 @desert-ant-labs/redact?

在项目目录运行 npm install @desert-ant-labs/redact,或使用 pnpm add / yarn add / bun add。

@desert-ant-labs/redact 支持 TypeScript 吗?

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

@desert-ant-labs/redact 支持 ESM 还是 CommonJS?

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

@desert-ant-labs/redact 可以商用吗?

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

@desert-ant-labs/redact 的下载量有多少?

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

@desert-ant-labs/redact 还在维护吗?

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