anonyma 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (171) hide show
  1. package/CHANGELOG.md +131 -2
  2. package/README.md +125 -7
  3. package/dist/ai/index.cjs +31 -20
  4. package/dist/ai/index.cjs.map +1 -1
  5. package/dist/ai/index.d.cts +5 -5
  6. package/dist/ai/index.d.ts +5 -5
  7. package/dist/ai/index.js +24 -13
  8. package/dist/ai/index.js.map +1 -1
  9. package/dist/audit/index.cjs +14 -14
  10. package/dist/audit/index.d.cts +2 -2
  11. package/dist/audit/index.d.ts +2 -2
  12. package/dist/audit/index.js +9 -9
  13. package/dist/{chunk-YMLR375Z.js → chunk-264R3VFP.js} +4 -3
  14. package/dist/chunk-264R3VFP.js.map +1 -0
  15. package/dist/{chunk-ISL5B4LU.cjs → chunk-32U3DM7M.cjs} +11 -11
  16. package/dist/{chunk-ISL5B4LU.cjs.map → chunk-32U3DM7M.cjs.map} +1 -1
  17. package/dist/{chunk-26IHMQHL.cjs → chunk-42ZZZUMO.cjs} +245 -85
  18. package/dist/chunk-42ZZZUMO.cjs.map +1 -0
  19. package/dist/chunk-4BYN6GVK.js +213 -0
  20. package/dist/chunk-4BYN6GVK.js.map +1 -0
  21. package/dist/{chunk-JKW36IPT.js → chunk-A2OMTO3L.js} +4 -4
  22. package/dist/{chunk-JKW36IPT.js.map → chunk-A2OMTO3L.js.map} +1 -1
  23. package/dist/{chunk-TW7PM3UJ.cjs → chunk-AITKBKQX.cjs} +2 -2
  24. package/dist/{chunk-TW7PM3UJ.cjs.map → chunk-AITKBKQX.cjs.map} +1 -1
  25. package/dist/{chunk-G4AN7FND.js → chunk-BWE4O2ES.js} +23 -9
  26. package/dist/chunk-BWE4O2ES.js.map +1 -0
  27. package/dist/{chunk-GS3FRH4K.js → chunk-CMJXIEBA.js} +2 -2
  28. package/dist/chunk-CMJXIEBA.js.map +1 -0
  29. package/dist/{chunk-GYMG5OI3.cjs → chunk-ESVFPMG4.cjs} +2 -2
  30. package/dist/{chunk-GYMG5OI3.cjs.map → chunk-ESVFPMG4.cjs.map} +1 -1
  31. package/dist/{chunk-2YG7R4KJ.cjs → chunk-EUNLKSYP.cjs} +5 -5
  32. package/dist/{chunk-2YG7R4KJ.cjs.map → chunk-EUNLKSYP.cjs.map} +1 -1
  33. package/dist/{chunk-S7NH6CHY.cjs → chunk-F3BYJFUM.cjs} +5 -3
  34. package/dist/chunk-F3BYJFUM.cjs.map +1 -0
  35. package/dist/{chunk-IJAMLYOT.cjs → chunk-HDHQXVUM.cjs} +50 -36
  36. package/dist/chunk-HDHQXVUM.cjs.map +1 -0
  37. package/dist/{chunk-OGNLB2KA.js → chunk-HFAIBE3T.js} +3 -3
  38. package/dist/{chunk-OGNLB2KA.js.map → chunk-HFAIBE3T.js.map} +1 -1
  39. package/dist/{chunk-PKHZ7CVI.js → chunk-IBDFBUFZ.js} +2 -2
  40. package/dist/chunk-IBDFBUFZ.js.map +1 -0
  41. package/dist/{chunk-3CY6ATKS.cjs → chunk-JBH2EFFF.cjs} +36 -36
  42. package/dist/{chunk-3CY6ATKS.cjs.map → chunk-JBH2EFFF.cjs.map} +1 -1
  43. package/dist/chunk-JPU7KQZX.cjs +82 -0
  44. package/dist/{chunk-CRXRJZEV.cjs.map → chunk-JPU7KQZX.cjs.map} +1 -1
  45. package/dist/{chunk-E3PICZYI.js → chunk-KHT23UVV.js} +3 -3
  46. package/dist/{chunk-E3PICZYI.js.map → chunk-KHT23UVV.js.map} +1 -1
  47. package/dist/{chunk-2ZZHLCEJ.js → chunk-LAZIR73I.js} +18 -87
  48. package/dist/chunk-LAZIR73I.js.map +1 -0
  49. package/dist/{chunk-FB56NAAH.cjs → chunk-NO4ZCIM6.cjs} +19 -92
  50. package/dist/chunk-NO4ZCIM6.cjs.map +1 -0
  51. package/dist/{chunk-S4BJ2ADB.cjs → chunk-PCU6AG7H.cjs} +4 -4
  52. package/dist/{chunk-S4BJ2ADB.cjs.map → chunk-PCU6AG7H.cjs.map} +1 -1
  53. package/dist/{chunk-W6RBCHYF.js → chunk-PSZHBY5V.js} +235 -75
  54. package/dist/chunk-PSZHBY5V.js.map +1 -0
  55. package/dist/{chunk-6LDAIJBL.js → chunk-QBBJR7DZ.js} +2 -2
  56. package/dist/{chunk-6LDAIJBL.js.map → chunk-QBBJR7DZ.js.map} +1 -1
  57. package/dist/{chunk-3D5ZCJ7R.js → chunk-QHQ2AU45.js} +3 -3
  58. package/dist/{chunk-3D5ZCJ7R.js.map → chunk-QHQ2AU45.js.map} +1 -1
  59. package/dist/{chunk-IHEIYZPL.cjs → chunk-RJX27YEX.cjs} +2 -2
  60. package/dist/chunk-RJX27YEX.cjs.map +1 -0
  61. package/dist/{chunk-QYD4F6T7.cjs → chunk-ROXNQKOA.cjs} +2 -2
  62. package/dist/{chunk-QYD4F6T7.cjs.map → chunk-ROXNQKOA.cjs.map} +1 -1
  63. package/dist/chunk-SHEXFU6T.js +107 -0
  64. package/dist/chunk-SHEXFU6T.js.map +1 -0
  65. package/dist/chunk-T2KIN6VL.cjs +218 -0
  66. package/dist/chunk-T2KIN6VL.cjs.map +1 -0
  67. package/dist/{chunk-RCW5IPPR.js → chunk-UCT6HDUD.js} +3 -3
  68. package/dist/{chunk-RCW5IPPR.js.map → chunk-UCT6HDUD.js.map} +1 -1
  69. package/dist/chunk-UN74SYHV.cjs +115 -0
  70. package/dist/chunk-UN74SYHV.cjs.map +1 -0
  71. package/dist/{chunk-F7O2FA3F.cjs → chunk-VAO3Y3HZ.cjs} +7 -6
  72. package/dist/chunk-VAO3Y3HZ.cjs.map +1 -0
  73. package/dist/{chunk-BBJPQPGP.js → chunk-VE3OSDM7.js} +5 -3
  74. package/dist/chunk-VE3OSDM7.js.map +1 -0
  75. package/dist/{chunk-7HK2V5Q4.js → chunk-X7IVXKDE.js} +3 -3
  76. package/dist/{chunk-7HK2V5Q4.js.map → chunk-X7IVXKDE.js.map} +1 -1
  77. package/dist/{chunk-UA3EOFBS.cjs → chunk-XVCVIUZS.cjs} +8 -8
  78. package/dist/{chunk-UA3EOFBS.cjs.map → chunk-XVCVIUZS.cjs.map} +1 -1
  79. package/dist/{chunk-SIHPDZXA.js → chunk-YSTC7WRF.js} +2 -2
  80. package/dist/{chunk-SIHPDZXA.js.map → chunk-YSTC7WRF.js.map} +1 -1
  81. package/dist/compliance/index.cjs +15 -15
  82. package/dist/compliance/index.d.cts +4 -4
  83. package/dist/compliance/index.d.ts +4 -4
  84. package/dist/compliance/index.js +5 -5
  85. package/dist/crypto.cjs +4 -4
  86. package/dist/crypto.d.cts +1 -1
  87. package/dist/crypto.d.ts +1 -1
  88. package/dist/crypto.js +2 -2
  89. package/dist/detectors/index.cjs +38 -38
  90. package/dist/detectors/index.d.cts +1 -1
  91. package/dist/detectors/index.d.ts +1 -1
  92. package/dist/detectors/index.js +3 -3
  93. package/dist/engine/index.cjs +70 -70
  94. package/dist/engine/index.d.cts +3 -3
  95. package/dist/engine/index.d.ts +3 -3
  96. package/dist/engine/index.js +8 -8
  97. package/dist/hash-P2VGKVFP.cjs +14 -0
  98. package/dist/{hash-2PSYNDMM.cjs.map → hash-P2VGKVFP.cjs.map} +1 -1
  99. package/dist/hash-T6YJG3DU.js +5 -0
  100. package/dist/{hash-CELLDNKN.js.map → hash-T6YJG3DU.js.map} +1 -1
  101. package/dist/index.cjs +44 -34
  102. package/dist/index.cjs.map +1 -1
  103. package/dist/index.d.cts +23 -5
  104. package/dist/index.d.ts +23 -5
  105. package/dist/index.js +11 -9
  106. package/dist/index.js.map +1 -1
  107. package/dist/{json-s8ny-RCo.d.cts → json-BXN577tt.d.cts} +3 -2
  108. package/dist/{json-CKDvlRtl.d.ts → json-BudrzkOQ.d.ts} +3 -2
  109. package/dist/mcp/index.cjs +29 -28
  110. package/dist/mcp/index.cjs.map +1 -1
  111. package/dist/mcp/index.js +14 -13
  112. package/dist/mcp/index.js.map +1 -1
  113. package/dist/middleware/express.cjs +11 -11
  114. package/dist/middleware/express.d.cts +3 -3
  115. package/dist/middleware/express.d.ts +3 -3
  116. package/dist/middleware/express.js +10 -10
  117. package/dist/middleware/hono.cjs +11 -11
  118. package/dist/middleware/hono.d.cts +3 -3
  119. package/dist/middleware/hono.d.ts +3 -3
  120. package/dist/middleware/hono.js +10 -10
  121. package/dist/middleware/index.cjs +11 -11
  122. package/dist/middleware/index.d.cts +3 -3
  123. package/dist/middleware/index.d.ts +3 -3
  124. package/dist/middleware/index.js +10 -10
  125. package/dist/schemas.cjs +47 -47
  126. package/dist/schemas.cjs.map +1 -1
  127. package/dist/schemas.d.cts +2 -2
  128. package/dist/schemas.d.ts +2 -2
  129. package/dist/schemas.js +2 -2
  130. package/dist/schemas.js.map +1 -1
  131. package/dist/{session-BdRftyZ5.d.cts → session-CPXEQ2uC.d.cts} +18 -5
  132. package/dist/{session-Cs2zlNQp.d.ts → session-D3WMN6fE.d.ts} +18 -5
  133. package/dist/stream.cjs +10 -8
  134. package/dist/stream.cjs.map +1 -1
  135. package/dist/stream.d.cts +1 -1
  136. package/dist/stream.d.ts +1 -1
  137. package/dist/stream.js +7 -5
  138. package/dist/stream.js.map +1 -1
  139. package/dist/{types-Dtht33aq.d.ts → types-B3VjDuP8.d.cts} +2 -282
  140. package/dist/{types--tPm_AjY.d.ts → types-DHAVKfzT.d.ts} +2 -2
  141. package/dist/types-LZHFEgx5.d.cts +1083 -0
  142. package/dist/types-LZHFEgx5.d.ts +1083 -0
  143. package/dist/{types-0BJH_wqc.d.cts → types-vmONSbz4.d.cts} +2 -2
  144. package/dist/{types-CPUhjvl8.d.cts → types-yZHnS7yv.d.ts} +2 -282
  145. package/dist/validators.cjs +10 -10
  146. package/dist/validators.js +1 -1
  147. package/dist/vault/index.cjs +65 -46
  148. package/dist/vault/index.cjs.map +1 -1
  149. package/dist/vault/index.d.cts +98 -5
  150. package/dist/vault/index.d.ts +98 -5
  151. package/dist/vault/index.js +11 -5
  152. package/dist/vault/index.js.map +1 -1
  153. package/package.json +16 -7
  154. package/dist/chunk-26IHMQHL.cjs.map +0 -1
  155. package/dist/chunk-2ZZHLCEJ.js.map +0 -1
  156. package/dist/chunk-BBJPQPGP.js.map +0 -1
  157. package/dist/chunk-CRXRJZEV.cjs +0 -82
  158. package/dist/chunk-F7O2FA3F.cjs.map +0 -1
  159. package/dist/chunk-FB56NAAH.cjs.map +0 -1
  160. package/dist/chunk-G4AN7FND.js.map +0 -1
  161. package/dist/chunk-GS3FRH4K.js.map +0 -1
  162. package/dist/chunk-IHEIYZPL.cjs.map +0 -1
  163. package/dist/chunk-IJAMLYOT.cjs.map +0 -1
  164. package/dist/chunk-PKHZ7CVI.js.map +0 -1
  165. package/dist/chunk-S7NH6CHY.cjs.map +0 -1
  166. package/dist/chunk-W6RBCHYF.js.map +0 -1
  167. package/dist/chunk-YMLR375Z.js.map +0 -1
  168. package/dist/hash-2PSYNDMM.cjs +0 -14
  169. package/dist/hash-CELLDNKN.js +0 -5
  170. package/dist/types-EJTj_BKY.d.cts +0 -584
  171. package/dist/types-EJTj_BKY.d.ts +0 -584
package/CHANGELOG.md CHANGED
@@ -7,7 +7,134 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ---
9
9
 
10
- ## [Unreleased]
10
+ ## [1.2.0] — 2026-11-21
11
+
12
+ Minor release: plugins for `createAnonymizer()`, token vaults, edge runtimes, and companion
13
+ packages for the Vercel AI SDK and MCP. Everything is additive or a fix. Some types are wider:
14
+ the `Anonymizer` methods accept plugin strategy options (see Changed), and
15
+ `SessionSnapshot.format` (and `TokenFormat`) now include `"brace"`, so an exhaustive `switch`
16
+ over a snapshot's format needs a case for it. The new packages, `@anonyma/vercel-ai` 0.1.0 and `@anonyma/mcp` 0.1.0, are versioned on their
17
+ own and require `anonyma` ^1.2.0.
18
+
19
+ `anonyma/schemas` now accepts zod 4 as well as zod 3. Its zod 3 floor rises from 3.23 to 3.25.4, the
20
+ first release whose `zod/v3` entry point loads and type-checks; on an older zod 3, `npm update zod`
21
+ stays within zod 3.
22
+
23
+ ### Added
24
+
25
+ - **Plugins.** `createAnonymizer()` applies the `plugins` of its configuration, which 1.1.0
26
+ listed under "Known limitations" ([#6](https://github.com/izaccavalheiro/anonyma/issues/6)).
27
+ The methods of the anonymizer use their detectors, strategies and validators: a plugin
28
+ detector replaces the built-in detector of its category (`customDetectors` take precedence
29
+ over it), rules, default strategies and field rules name a plugin strategy as
30
+ `{ strategy, options }`, and a match is kept only when every plugin validator of its category
31
+ accepts it. `createAnonymizer()` throws a `ValidationError` when two plugins have the same
32
+ name, register a detector for the same category or a strategy under the same name, or when a
33
+ strategy has the name of a built-in one, and an `UnknownCategoryError` for a detector or
34
+ validator keyed by a name that is not a category. The standalone functions do not apply
35
+ plugins.
36
+ - **Types** `PluginStrategyOptions` and `StrategyFunction`, exported from `"anonyma"`.
37
+ `AnonymizationRule`, `AnonymizeOptions`, `FieldRule` and `FieldRuleMap` take an optional type
38
+ parameter for the strategy options they hold, `StrategyOptions` by default.
39
+ - **Token vaults.** `AnonymaVault` — any object with `store(key, value)` and `retrieve(key)`,
40
+ synchronous or asynchronous — `InMemoryVault`, and `restore(text, vault)`, exported from
41
+ `"anonyma"` and `"anonyma/vault"`. `restore()` scans once and matches whole tokens, so
42
+ `{{PERSON_1}}` is never found inside `{{PERSON_10}}`, and restored values are not scanned again.
43
+ - **`tokenize(text, { vault })`** and **`tokenizeAsync(text, { vault })`** store their tokens in a
44
+ vault, numbered after those it already holds; a value the vault already holds keeps its token.
45
+ With a vault, `format` defaults to `"brace"`.
46
+ - **`createVaultTokenizer()`** (`"anonyma/vault"`): a tokenization provider backed by an
47
+ `AnonymaVault`, for the span engine, the LLM guard and the streaming restorers. It probes the
48
+ vault before issuing a number, so a new tokenizer never overwrites an earlier one's tokens, and
49
+ serialises allocations against an asynchronous vault.
50
+ - **`format: "brace"`** (`{{EMAIL_1}}`) for `tokenize()`, the session tokenizer and the LLM guard,
51
+ with stream hold-back for tokens split inside `{{` or `}}`, and lenient restoration of
52
+ `{EMAIL_1}` and `{{ email_01 }}`.
53
+ - **Runtime checks** (`scripts/runtime/`): the same feature checks run against the build on
54
+ Node.js, Bun, Deno and Cloudflare Workers (workerd through Miniflare, bundled with esbuild as
55
+ `wrangler deploy` does, without `nodejs_compat`). CI runs them in a new `runtimes` job.
56
+ - **`@anonyma/vercel-ai` 0.1.0** (`packages/vercel-ai`): a typed AI SDK 7 middleware, `withAnonyma()`
57
+ and drop-in `generateText()` / `streamText()`, tested against the real SDK.
58
+ - **`@anonyma/mcp` 0.1.0** (`packages/mcp`): a privacy guard for MCP connections, the
59
+ `anonyma-mcp-proxy` command that puts any stdio MCP server behind it, and `guardMcpClient()`
60
+ for MCP SDK clients. Tested against the official MCP SDK.
61
+ - The repository is an npm workspace; `packages/*` are type-checked, tested, built and validated
62
+ in a new `packages` CI job.
63
+
64
+ ### Changed
65
+
66
+ - The methods of `Anonymizer` and `AnonymizerConfig.defaultStrategy` accept
67
+ `PluginStrategyOptions` wherever they accepted `StrategyOptions`; the options of a built-in
68
+ strategy are type-checked as before. An object that implements `Anonymizer` with parameter
69
+ types of its own, such as a test double, may have to widen them.
70
+ - `createAnonymizer()` throws `UnsupportedStrategyError` when `defaultStrategy` names a
71
+ strategy that neither anonyma nor one of the plugins provides, instead of when it first
72
+ applies it.
73
+ - **The optional `zod` peer dependency** is `^3.25.4 || ^4.0.0` (was `^3.23.0`).
74
+ `"anonyma/schemas"` imports `zod/v3`, which both majors provide, so its declarations are the
75
+ same whichever zod is installed. They were built against zod 3 and named types that zod 4 does
76
+ not have. Every zod 3 release is subject to a high-severity advisory fixed in zod 4.6.6.
77
+
78
+ ### Fixed
79
+
80
+ - **`detokenize()`** and **`restoreFromLLM()`** left tokens whose prefix contains a digit —
81
+ `[IPV4_0001]`, `[IPV6_0001]` — unresolved.
82
+ - **`toLanguageModelMiddleware()`** restored tokens in streamed text but not in streamed
83
+ reasoning (`reasoning-delta`), although `wrapGenerate` restored both.
84
+ - **`sanitizeJson()`** and the LLM guard listed exempted members whose value is `undefined` or
85
+ `null` as `unscanned`. The Vercel AI SDK sends `providerOptions: undefined` on every message, so
86
+ an `onSanitized` hook that refuses unscanned content refused every request.
87
+
88
+ ---
89
+
90
+ ## [1.1.1] — 2026-10-05
91
+
92
+ Patch release. The 1.x functions now exempt a detected value only when it equals an `allowlist`
93
+ entry, as documented; since 1.0.0 they also exempted every value that contained one. Code that
94
+ relied on such partial matches will see those values anonymized, and can use `allowlistPatterns`
95
+ to match a part of a value instead. The span engine (`"anonyma/engine"`) is unchanged.
96
+
97
+ ### Fixed
98
+
99
+ - **`allowlist`** skips a detected value only when the value equals an entry, as documented.
100
+ Entries were matched as substrings, so `allowlist: ["bob@example.com"]` also left
101
+ `jimbob@example.com` in the output of `anonymize()`, `anonymizeAsync()`, `tokenize()`,
102
+ `sanitizeForLLM()` and the methods of `createAnonymizer()`; an entry naming a domain left
103
+ every address of that domain, and an empty entry left every value. A value that merely
104
+ contains an entry is now anonymized. `allowlistPatterns` still match a part of a value.
105
+ - **`createAnonymizer().tokenize()`** matched `allowlist` entries as regular expressions: an
106
+ entry containing `(` threw a `SyntaxError`, and `"x.y@example.com"` also exempted
107
+ `"xzy@example.com"`.
108
+ - **`allowlistPatterns`** with the `g` or `y` flag skipped only every other matching value,
109
+ because `RegExp.test()` kept its `lastIndex` between values, and left the caller's pattern
110
+ with a changed `lastIndex`. These flags are now ignored.
111
+
112
+ ### Added
113
+
114
+ - **Plugins.** `createAnonymizer()` applies the `plugins` of its configuration, which 1.1.0
115
+ listed under "Known limitations" ([#6](https://github.com/izaccavalheiro/anonyma/issues/6)).
116
+ The methods of the anonymizer use their detectors, strategies and validators: a plugin
117
+ detector replaces the built-in detector of its category (`customDetectors` take precedence
118
+ over it), rules, default strategies and field rules name a plugin strategy as
119
+ `{ strategy, options }`, and a match is kept only when every plugin validator of its category
120
+ accepts it. `createAnonymizer()` throws a `ValidationError` when two plugins have the same
121
+ name, register a detector for the same category or a strategy under the same name, or when a
122
+ strategy has the name of a built-in one, and an `UnknownCategoryError` for a detector or
123
+ validator keyed by a name that is not a category. The standalone functions do not apply
124
+ plugins.
125
+ - **Types** `PluginStrategyOptions` and `StrategyFunction`, exported from `"anonyma"`.
126
+ `AnonymizationRule`, `AnonymizeOptions`, `FieldRule` and `FieldRuleMap` take an optional type
127
+ parameter for the strategy options they hold, `StrategyOptions` by default.
128
+
129
+ ### Changed
130
+
131
+ - The methods of `Anonymizer` and `AnonymizerConfig.defaultStrategy` accept
132
+ `PluginStrategyOptions` wherever they accepted `StrategyOptions`; the options of a built-in
133
+ strategy are type-checked as before. An object that implements `Anonymizer` with parameter
134
+ types of its own, such as a test double, may have to widen them.
135
+ - `createAnonymizer()` throws `UnsupportedStrategyError` when `defaultStrategy` names a
136
+ strategy that neither anonyma nor one of the plugins provides, instead of when it first
137
+ applies it.
11
138
 
12
139
  ---
13
140
 
@@ -580,7 +707,9 @@ All types are zero-cost, type-only exports (stripped by `tsc`; no runtime overhe
580
707
  - **Prettier** code formatting.
581
708
  - **MIT License**.
582
709
 
583
- [Unreleased]: https://github.com/izaccavalheiro/anonyma/compare/v1.1.0...HEAD
710
+ [Unreleased]: https://github.com/izaccavalheiro/anonyma/compare/v1.2.0...HEAD
711
+ [1.2.0]: https://github.com/izaccavalheiro/anonyma/releases/tag/v1.2.0
712
+ [1.1.1]: https://github.com/izaccavalheiro/anonyma/releases/tag/v1.1.1
584
713
  [1.1.0]: https://github.com/izaccavalheiro/anonyma/releases/tag/v1.1.0
585
714
  [1.1.0-beta.0]: https://github.com/izaccavalheiro/anonyma/releases/tag/v1.1.0-beta.0
586
715
  [1.0.0]: https://github.com/izaccavalheiro/anonyma/releases/tag/v1.0.0
package/README.md CHANGED
@@ -15,6 +15,7 @@
15
15
  - 🔍 **Detect 27 PII categories** — email, phone, SSN, credit card, IBAN, passport, address, VIN, API keys, crypto wallets, and more
16
16
  - 🛡️ **8 anonymization strategies** — mask, redact, pseudonymize, hash (SHA-256), generalize, tokenize, encrypt (AES-GCM), synthesize
17
17
  - 🔁 **Reversible tokenization** — `tokenize()` / `detokenize()` for round-trip fidelity
18
+ - 🗄️ **Token vaults** — `{{EMAIL_1}}` tokens kept in any `AnonymaVault` (in memory, Redis, KV) and put back by `restore(text, vault)`
18
19
  - 🤖 **LLM pipeline helpers** — `sanitizeForLLM()` / `restoreFromLLM()` for safe prompt injection with reversible tokens
19
20
  - 📋 **Compliance presets** — built-in `gdpr`, `lgpd`, `pipeda`, `hipaa`, `ccpa`, `pci-dss`, `sox` and `ferpa` presets
20
21
  - 🌍 **International identifiers** — national IDs, tax IDs, passports, licence plates and company registrations of many countries, detected without configuration
@@ -24,7 +25,7 @@
24
25
  - 🌳 **Deep object anonymization** — `anonymizeObject()` recursively cleans entire JSON trees
25
26
  - ✅ **PII presence check** — `hasPII()` with early-exit for fast gating
26
27
  - 🔎 **Aggressive mode** — expanded, permissive patterns for obfuscated PII
27
- - 🧩 **Custom patterns & detectors** — inject ad-hoc `RegExp` patterns, fully replace per-category detectors, or define detectors for your own categories
28
+ - 🧩 **Custom patterns, detectors & plugins** — inject ad-hoc `RegExp` patterns, fully replace per-category detectors, define detectors for your own categories, or give an anonymizer detectors, strategies and validators as plugins
28
29
  - 🚫 **Allowlist support** — skip known-safe values by exact string or `RegExp` pattern
29
30
  - 🔢 **Confidence threshold** — filter out low-confidence matches
30
31
  - 🔒 **Encryption** — reversible AES-GCM `encrypt()` / `decrypt()` strategy (Web Crypto API)
@@ -40,6 +41,8 @@
40
41
  - 💬 **LLM guard** — chat messages and JSON sanitized with reversible tokens, restored in complete and streamed model output (`"anonyma/ai"`)
41
42
  - 🔌 **MCP server and HTTP middleware** — a dependency-free Model Context Protocol server, and payload scrubbing for Express, Hono and Fetch handlers (`"anonyma/mcp"`, `"anonyma/middleware"`)
42
43
  - 📦 **Dual ESM + CJS** — works everywhere Node.js ≥ 18 runs
44
+ - 🌐 **Edge-ready** — tested on Bun, Deno and Cloudflare Workers; Web Crypto only, no Node.js modules
45
+ - 🧱 **Companion packages** — [`@anonyma/vercel-ai`](packages/vercel-ai#readme) for the Vercel AI SDK, [`@anonyma/mcp`](packages/mcp#readme) to put any MCP server behind a privacy proxy
43
46
  - 🔒 **Strict TypeScript** — no `any`, full declaration files
44
47
 
45
48
  ---
@@ -380,8 +383,58 @@ const { text: restored } = detokenize(text, mapping);
380
383
  // "alice@example.com called 555-867-5309"
381
384
  ```
382
385
 
383
- Identical values share a token. `TokenizeOptions` also declares `format: "custom"` with
384
- `tokenTemplate`, `deterministic: false` and `locales`, which `tokenize()` does not apply.
386
+ Identical values share a token. `format: "brace"` produces `{{EMAIL_1}}`. `TokenizeOptions`
387
+ also declares `format: "custom"` with `tokenTemplate`, `deterministic: false` and `locales`,
388
+ which `tokenize()` does not apply.
389
+
390
+ ### Token vaults
391
+
392
+ Pass a `vault` and the tokens are stored in it instead of living only in the returned mapping.
393
+ `restore(text, vault)` then puts the original values back into any text — a model's answer, a
394
+ later request, another process that reaches the same store.
395
+
396
+ ```ts
397
+ import { InMemoryVault, restore, tokenize } from "anonyma";
398
+
399
+ const vault = new InMemoryVault(); // one per request or conversation
400
+
401
+ tokenize("Mail john@example.com, cc jane@example.com", { vault }).text;
402
+ // "Mail {{EMAIL_1}}, cc {{EMAIL_2}}"
403
+
404
+ // A later turn continues the numbering; a known value keeps its token.
405
+ tokenize("Reply to jane@example.com and ada@example.com", { vault }).text;
406
+ // "Reply to {{EMAIL_2}} and {{EMAIL_3}}"
407
+
408
+ restore("Sent to {{EMAIL_2}} and {{EMAIL_1}}.", vault).text;
409
+ // "Sent to jane@example.com and john@example.com."
410
+ ```
411
+
412
+ `restore()` scans the text once and matches whole tokens, so `{{PERSON_1}}` is never found
413
+ inside `{{PERSON_10}}`, and a restored value is never scanned again. Unknown tokens stay in
414
+ place and are listed in `unresolved`. With a vault, `format` defaults to `"brace"`; avoid it
415
+ when prompts go through a template engine that treats `{{…}}` as a placeholder.
416
+
417
+ Any object with `store(key, value)` and `retrieve(key)` is a vault. Both may return promises —
418
+ use `tokenizeAsync()`, and `await restore(…)`:
419
+
420
+ ```ts
421
+ import type { AnonymaVault } from "anonyma";
422
+
423
+ // A Cloudflare KV namespace, with one key prefix per conversation.
424
+ const kvVault = (kv: KVNamespace, conversation: string): AnonymaVault => ({
425
+ store: (key, value) => kv.put(`${conversation}:${key}`, value, { expirationTtl: 3600 }),
426
+ retrieve: async (key) => (await kv.get(`${conversation}:${key}`)) ?? undefined,
427
+ });
428
+
429
+ const vault = kvVault(env.TOKENS, conversationId);
430
+ const { text } = await tokenizeAsync(prompt, { vault });
431
+ const { text: answer } = await restore(await callLLM(text), vault);
432
+ ```
433
+
434
+ A vault holds original values in clear: keep one per conversation or user. Restoring a user's
435
+ text against a vault shared with other users would hand them data that is not theirs if they
436
+ type somebody else's token. `createVaultTokenizer()` in `"anonyma/vault"` plugs a vault into the
437
+ span engine and the streaming restorers.
385
438
 
386
439
  ---
387
440
 
@@ -579,8 +632,61 @@ pipeline.transform("EMP-001234 wrote from alice@example.com").text;
579
632
  // "[EMPLOYEE] wrote from [REDACTED]"
580
633
  ```
581
634
 
582
- `AnonymaPlugin` and the `plugins` option of `createAnonymizer()` are declared in the types, but
583
- `createAnonymizer()` does not apply plugins.
635
+ ---
636
+
637
+ ## Plugins
638
+
639
+ A plugin gives the anonymizer that `createAnonymizer()` creates detectors, strategies and
640
+ validators of its own:
641
+
642
+ - A **detector** replaces the built-in detector of its category. `customDetectors` take
643
+ precedence over it.
644
+ - A **strategy** is named in rules and default strategies as `{ strategy, options }`, and
645
+ receives the `options` as its second argument.
646
+ - A **validator** decides which matches of its category count: a match is anonymized only when
647
+ every validator of its category accepts it.
648
+
649
+ ```ts
650
+ import { createAnonymizer, type AnonymaPlugin } from "anonyma";
651
+ import { cpfChecksum } from "anonyma/validators";
652
+
653
+ const acme: AnonymaPlugin = {
654
+ name: "acme",
655
+ detectors: {
656
+ "case-number": (text) =>
657
+ [...text.matchAll(/\bACME-\d{6}\b/g)].map((m) => ({
658
+ category: "case-number" as const,
659
+ value: m[0],
660
+ start: m.index,
661
+ end: m.index + m[0].length,
662
+ confidence: 0.95,
663
+ })),
664
+ },
665
+ strategies: {
666
+ "keep-domain": (value, options) =>
667
+ `${String(options?.["user"] ?? "[USER]")}@${value.slice(value.indexOf("@") + 1)}`,
668
+ },
669
+ validators: {
670
+ // A CPF-shaped national ID counts only with the right check digits.
671
+ "national-id": (value) => !/^\d{3}\.\d{3}\.\d{3}-\d{2}$/.test(value) || cpfChecksum(value),
672
+ },
673
+ };
674
+
675
+ const anonymizer = createAnonymizer({ plugins: [acme] });
676
+ anonymizer.anonymize("ACME-004211: alice@example.com, CPF 529.982.247-25, ref 529.982.247-26", {
677
+ rules: [
678
+ { category: "case-number", strategy: { strategy: "redact", label: "[CASE]" } },
679
+ { category: "email", strategy: { strategy: "keep-domain" } },
680
+ { category: "national-id", strategy: { strategy: "redact" } },
681
+ ],
682
+ }).text;
683
+ // "[CASE]: [USER]@example.com, CPF [REDACTED], ref 529.982.247-26"
684
+ ```
685
+
686
+ Plugins apply to the methods of their anonymizer only: `anonymize()` and the other standalone
687
+ functions do not use them. `createAnonymizer()` throws a `ValidationError` when two plugins have
688
+ the same name, register a detector for the same category or a strategy under the same name, or
689
+ when a strategy has the name of a built-in one.
584
690
 
585
691
  ---
586
692
 
@@ -870,11 +976,23 @@ Full API reference: [docs/api.md](docs/api.md)
870
976
 
871
977
  ---
872
978
 
979
+ ## Companion Packages
980
+
981
+ | Package | What it does |
982
+ | ------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
983
+ | [`@anonyma/vercel-ai`](packages/vercel-ai#readme) | Middleware and drop-in `generateText` / `streamText` for the Vercel AI SDK: prompts are tokenized, and text, reasoning and tool calls come back with the original values, also when streamed. |
984
+ | [`@anonyma/mcp`](packages/mcp#readme) | A privacy guard for MCP connections, and `anonyma-mcp-proxy`, which puts any stdio MCP server behind it: tool results reach the model as tokens, tool arguments reach the server as real values. |
985
+
986
+ ---
987
+
873
988
  ## Requirements
874
989
 
875
- - **Node.js** ≥ 18 (for `hash`, `encrypt`, and streaming features — Web Crypto + `TransformStream`)
990
+ - **Node.js** ≥ 18, **Bun**, **Deno**, **Cloudflare Workers** and browsers. The package uses
991
+ only the Web platform — Web Crypto, `TransformStream`, `TextEncoder` — and no Node.js module
992
+ (on Node.js 18, which has no global `crypto`, it loads `node:crypto` lazily). CI runs the
993
+ same feature checks on every one of these runtimes; Workers run them without `nodejs_compat`.
876
994
  - TypeScript ≥ 5.0 (optional)
877
- - Zod ≥ 3.23 (optional peer-dependency for `"anonyma/schemas"`)
995
+ - Zod ≥ 3.25.4 or 4 (optional peer-dependency for `"anonyma/schemas"`, which uses the `zod/v3` entry point both provide, so its types are the same with either)
878
996
 
879
997
  ---
880
998
 
package/dist/ai/index.cjs CHANGED
@@ -1,17 +1,18 @@
1
1
  'use strict';
2
2
 
3
- var chunkS7NH6CHY_cjs = require('../chunk-S7NH6CHY.cjs');
4
- var chunkIJAMLYOT_cjs = require('../chunk-IJAMLYOT.cjs');
5
- require('../chunk-3CY6ATKS.cjs');
3
+ var chunkF3BYJFUM_cjs = require('../chunk-F3BYJFUM.cjs');
4
+ var chunkHDHQXVUM_cjs = require('../chunk-HDHQXVUM.cjs');
5
+ require('../chunk-JBH2EFFF.cjs');
6
6
  require('../chunk-KOCQBDI4.cjs');
7
- require('../chunk-S4BJ2ADB.cjs');
8
- require('../chunk-2YG7R4KJ.cjs');
9
- require('../chunk-IHEIYZPL.cjs');
10
- require('../chunk-UA3EOFBS.cjs');
11
- require('../chunk-GYMG5OI3.cjs');
7
+ require('../chunk-PCU6AG7H.cjs');
8
+ require('../chunk-EUNLKSYP.cjs');
9
+ require('../chunk-RJX27YEX.cjs');
10
+ require('../chunk-XVCVIUZS.cjs');
11
+ require('../chunk-ESVFPMG4.cjs');
12
12
  var chunk2NNMJ55L_cjs = require('../chunk-2NNMJ55L.cjs');
13
- var chunkFB56NAAH_cjs = require('../chunk-FB56NAAH.cjs');
14
- require('../chunk-TW7PM3UJ.cjs');
13
+ var chunkNO4ZCIM6_cjs = require('../chunk-NO4ZCIM6.cjs');
14
+ require('../chunk-UN74SYHV.cjs');
15
+ require('../chunk-AITKBKQX.cjs');
15
16
  var chunkWLJN7GIS_cjs = require('../chunk-WLJN7GIS.cjs');
16
17
 
17
18
  // src/ai/restore.ts
@@ -71,7 +72,7 @@ async function restoreJsonText(text, provider) {
71
72
  if (typeof text !== "string") throw new chunkWLJN7GIS_cjs.ValidationError("text", "must be a string");
72
73
  let parsed;
73
74
  try {
74
- parsed = chunkS7NH6CHY_cjs.parseJsonLossless(text);
75
+ parsed = chunkF3BYJFUM_cjs.parseJsonLossless(text);
75
76
  } catch {
76
77
  return (await chunk2NNMJ55L_cjs.restoreTokens(text, provider)).text;
77
78
  }
@@ -105,7 +106,7 @@ async function restoreJsonText(text, provider) {
105
106
  return out;
106
107
  };
107
108
  const value = await walk(parsed.value, 0);
108
- return restored === 0 ? text : chunkS7NH6CHY_cjs.stringifyJsonLossless(value, marker);
109
+ return restored === 0 ? text : chunkF3BYJFUM_cjs.stringifyJsonLossless(value, marker);
109
110
  }
110
111
  var textDeltaLens = Object.freeze({
111
112
  get(chunk) {
@@ -264,14 +265,14 @@ function createLlmGuard(options = {}) {
264
265
  const exemptInMedia = (key, parent) => passthrough.has(key) || isMediaPayload(key, parent);
265
266
  const sessionOptions = { lenient: true, ...options.session };
266
267
  const skip = new Set(skipRoles);
267
- const probe = chunkFB56NAAH_cjs.createSessionTokenizer(sessionOptions);
268
- const compile = (session) => chunkIJAMLYOT_cjs.compilePipeline(
268
+ const probe = chunkNO4ZCIM6_cjs.createSessionTokenizer(sessionOptions);
269
+ const compile = (session) => chunkHDHQXVUM_cjs.compilePipeline(
269
270
  { ...options.spec, defaultStrategy: { strategy: "tokenize" } },
270
271
  { tokenization: session }
271
272
  );
272
273
  compile(probe);
273
274
  function begin(snapshot) {
274
- const session = chunkFB56NAAH_cjs.createSessionTokenizer({
275
+ const session = chunkNO4ZCIM6_cjs.createSessionTokenizer({
275
276
  ...sessionOptions,
276
277
  ...snapshot !== void 0 ? { snapshot } : {}
277
278
  });
@@ -286,7 +287,7 @@ function createLlmGuard(options = {}) {
286
287
  const flaggedKeys = [];
287
288
  let replaced = 0;
288
289
  const json = (value, path, skipMember = exempt) => {
289
- const result = chunkS7NH6CHY_cjs.sanitizeJson(value, {
290
+ const result = chunkF3BYJFUM_cjs.sanitizeJson(value, {
290
291
  pipeline,
291
292
  jsonStringKeys: ["arguments"],
292
293
  skipMember,
@@ -470,10 +471,20 @@ function toLanguageModelMiddleware(guard, options = {}) {
470
471
  reader.releaseLock();
471
472
  }
472
473
  }
474
+ const asText = (part) => {
475
+ if (!isPlainObject(part)) return void 0;
476
+ return part["type"] === "reasoning-delta" ? { ...part, type: "text-delta" } : part;
477
+ };
473
478
  const lens = {
474
- get: (part) => isPlainObject(part) ? textDeltaLens.get(part) : void 0,
479
+ get: (part) => {
480
+ const text = asText(part);
481
+ return text === void 0 ? void 0 : textDeltaLens.get(text);
482
+ },
475
483
  set: (part, text) => textDeltaLens.set(part, text),
476
- block: (part) => isPlainObject(part) ? textDeltaLens.block?.(part) : void 0
484
+ block: (part) => {
485
+ const id = textDeltaLens.block?.(part);
486
+ return id === void 0 ? void 0 : `${String(part.type)}:${id}`;
487
+ }
477
488
  };
478
489
  const restored = restoreChunks(parts(), lens, exchange.session);
479
490
  const stream = new ReadableStream({
@@ -503,11 +514,11 @@ function toLanguageModelMiddleware(guard, options = {}) {
503
514
 
504
515
  Object.defineProperty(exports, "sanitizeJson", {
505
516
  enumerable: true,
506
- get: function () { return chunkS7NH6CHY_cjs.sanitizeJson; }
517
+ get: function () { return chunkF3BYJFUM_cjs.sanitizeJson; }
507
518
  });
508
519
  Object.defineProperty(exports, "sanitizeJsonAsync", {
509
520
  enumerable: true,
510
- get: function () { return chunkS7NH6CHY_cjs.sanitizeJsonAsync; }
521
+ get: function () { return chunkF3BYJFUM_cjs.sanitizeJsonAsync; }
511
522
  });
512
523
  exports.contentLens = contentLens;
513
524
  exports.createLlmGuard = createLlmGuard;