@desert-ant-labs/redact
v3.2.0
Published
On-device multilingual PII redaction for JavaScript. Runs in the browser (WebAssembly + LiteRT.js) and server-side in Node (native), from one import.
Readme
@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, soRedact.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/redactimport { 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 onlyRedact.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 the Hub 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/corenamespace, 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 the Hugging Face Hub 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 (default0.6). Structured recognizers (email, cards, IBANs, …) always apply.labels: restrict redaction to these categories. Omit forDEFAULT_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 theseDEFAULT_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 1.0: free below 100,000 monthly active devices per platform; above that a commercial license is required ([email protected]). Full terms: https://license.desertant.com/1.0
