anonyma 1.1.1 → 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 +109 -1
  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-ZXPZ7W37.cjs → chunk-42ZZZUMO.cjs} +231 -60
  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-GBYHQQUY.js → chunk-PSZHBY5V.js} +221 -50
  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-CvKtPL8l.d.cts → json-BXN577tt.d.cts} +3 -2
  108. package/dist/{json-BruY_-tV.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-CEFGgjFV.d.ts → session-CPXEQ2uC.d.cts} +18 -5
  132. package/dist/{session-DEQrPea7.d.cts → 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-oxllMFYF.d.ts → types-B3VjDuP8.d.cts} +2 -282
  140. package/dist/{types-D5BeXwnX.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-BtvX8QTd.d.cts → types-vmONSbz4.d.cts} +2 -2
  144. package/dist/{types-BL5UWH0y.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-2ZZHLCEJ.js.map +0 -1
  155. package/dist/chunk-BBJPQPGP.js.map +0 -1
  156. package/dist/chunk-CRXRJZEV.cjs +0 -82
  157. package/dist/chunk-F7O2FA3F.cjs.map +0 -1
  158. package/dist/chunk-FB56NAAH.cjs.map +0 -1
  159. package/dist/chunk-G4AN7FND.js.map +0 -1
  160. package/dist/chunk-GBYHQQUY.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-YMLR375Z.js.map +0 -1
  167. package/dist/chunk-ZXPZ7W37.cjs.map +0 -1
  168. package/dist/hash-2PSYNDMM.cjs +0 -14
  169. package/dist/hash-CELLDNKN.js +0 -5
  170. package/dist/types-cbGVW8cD.d.cts +0 -592
  171. package/dist/types-cbGVW8cD.d.ts +0 -592
@@ -1,5 +1,6 @@
1
- import { AGGRESSIVE_DETECTOR_REGISTRY, DETECTOR_REGISTRY } from './chunk-OGNLB2KA.js';
2
- import { getPreset, redact, generalize, pseudonymize, mask } from './chunk-GS3FRH4K.js';
1
+ import { AGGRESSIVE_DETECTOR_REGISTRY, DETECTOR_REGISTRY } from './chunk-HFAIBE3T.js';
2
+ import { getPreset, redact, generalize, pseudonymize, mask } from './chunk-CMJXIEBA.js';
3
+ import { isThenable, createVaultTokenizer } from './chunk-4BYN6GVK.js';
3
4
  import { ValidationError, UnknownCategoryError, PresetNotFoundError, UnsupportedStrategyError } from './chunk-2F2GWGTB.js';
4
5
 
5
6
  // src/strategies/tokenize.ts
@@ -17,15 +18,15 @@ function assignToken(store, category, value, prefix, format = "bracket") {
17
18
  const count = (store.counters.get(prefix) ?? 0) + 1;
18
19
  store.counters.set(prefix, count);
19
20
  const id = format === "bracket" ? String(count).padStart(4, "0") : String(count);
20
- const token = format === "bracket" ? `[${prefix}_${id}]` : `<${prefix}_${id}>`;
21
+ const token = format === "bracket" ? `[${prefix}_${id}]` : format === "brace" ? `{{${prefix}_${id}}}` : `<${prefix}_${id}>`;
21
22
  store.index.set(indexKey, token);
22
23
  store.tokens.set(token, value);
23
24
  return token;
24
25
  }
26
+ var LEGACY_TOKEN_PATTERN = /\[[A-Z][A-Z0-9_]{0,60}_\d{1,12}\]|<[A-Z][A-Z0-9_]{0,60}_\d{1,12}>|\{\{[A-Z][A-Z0-9_]{0,60}_\d{1,12}\}\}/g;
25
27
  function detokenizeText(text, store) {
26
28
  const unresolved = [];
27
- const re = /(\[[A-Z_]+_\d+\]|<[A-Z_]+_\d+>)/g;
28
- const result = text.replace(re, (token) => {
29
+ const result = text.replace(LEGACY_TOKEN_PATTERN, (token) => {
29
30
  const original = store.tokens.get(token);
30
31
  if (original !== void 0) return original;
31
32
  unresolved.push(token);
@@ -103,6 +104,27 @@ var TOKEN_PREFIX_MAP = {
103
104
  "case-number": "CASE_NUMBER",
104
105
  "company-registration": "COMPANY_REG"
105
106
  };
107
+ var STRATEGY_NAMES = /* @__PURE__ */ new Set([
108
+ "mask",
109
+ "redact",
110
+ "pseudonymize",
111
+ "hash",
112
+ "generalize",
113
+ "tokenize",
114
+ "encrypt",
115
+ "synthesize"
116
+ ]);
117
+ var NO_PLUGINS = {
118
+ detectors: {},
119
+ strategies: /* @__PURE__ */ new Map(),
120
+ validators: /* @__PURE__ */ new Map()
121
+ };
122
+ function isCategory(name) {
123
+ return ALL_CATEGORIES.includes(name);
124
+ }
125
+ function isBuiltinStrategy(opts) {
126
+ return STRATEGY_NAMES.has(opts.strategy);
127
+ }
106
128
  function applyStrategySync(value, opts) {
107
129
  switch (opts.strategy) {
108
130
  case "mask":
@@ -125,12 +147,23 @@ function applyStrategySync(value, opts) {
125
147
  `[anonyma] anonymize() and anonymizeAsync() do not apply the \`${opts.strategy}\` strategy; the value is redacted instead. Use a pipeline of "anonyma/engine" to apply it.`
126
148
  );
127
149
  return redact(value);
150
+ /* v8 ignore start -- applyStrategy() passes built-in strategies only */
128
151
  default: {
129
152
  const _exhaustive = opts;
130
153
  throw new UnsupportedStrategyError(_exhaustive.strategy);
131
154
  }
132
155
  }
133
156
  }
157
+ function applyStrategy(value, opts, plugins) {
158
+ if (isBuiltinStrategy(opts)) return applyStrategySync(value, opts);
159
+ const strategy = plugins.strategies.get(opts.strategy);
160
+ if (strategy === void 0) throw new UnsupportedStrategyError(opts.strategy);
161
+ const replacement = strategy(value, opts.options);
162
+ if (typeof replacement !== "string") {
163
+ throw new ValidationError(`strategies.${opts.strategy}`, "must return a string");
164
+ }
165
+ return replacement;
166
+ }
134
167
  function deduplicateMatches(matches) {
135
168
  const sorted = [...matches].sort((a, b) => a.start - b.start || b.confidence - a.confidence);
136
169
  const result = [];
@@ -143,10 +176,18 @@ function deduplicateMatches(matches) {
143
176
  }
144
177
  return result;
145
178
  }
146
- function resolveDetector(category, customDetectors, aggressive) {
179
+ function resolveDetector(category, customDetectors, aggressive, plugins) {
147
180
  if (customDetectors?.[category]) return customDetectors[category];
181
+ const pluginDetector = plugins.detectors[category];
182
+ if (pluginDetector) return pluginDetector;
148
183
  return aggressive ? AGGRESSIVE_DETECTOR_REGISTRY[category] : DETECTOR_REGISTRY[category];
149
184
  }
185
+ function runDetector(text, category, customDetectors, aggressive, plugins) {
186
+ const matches = resolveDetector(category, customDetectors, aggressive, plugins)(text);
187
+ const validators = plugins.validators.get(category);
188
+ if (validators === void 0) return matches;
189
+ return matches.filter((match) => validators.every((validate) => validate(match.value)));
190
+ }
150
191
  function detectCustomPatterns(text, customPatterns) {
151
192
  const results = [];
152
193
  for (const cp of customPatterns) {
@@ -183,6 +224,9 @@ function resolveCategories(options) {
183
224
  return [...ALL_CATEGORIES];
184
225
  }
185
226
  function detect(text, categories = ALL_CATEGORIES, customDetectors, aggressive) {
227
+ return detectWith(text, categories, customDetectors, aggressive, NO_PLUGINS);
228
+ }
229
+ function detectWith(text, categories, customDetectors, aggressive, plugins) {
186
230
  if (typeof text !== "string") {
187
231
  throw new ValidationError("text", "must be a string");
188
232
  }
@@ -191,12 +235,14 @@ function detect(text, categories = ALL_CATEGORIES, customDetectors, aggressive)
191
235
  if (!ALL_CATEGORIES.includes(category)) {
192
236
  throw new UnknownCategoryError(category);
193
237
  }
194
- const detector = resolveDetector(category, customDetectors, aggressive);
195
- raw.push(...detector(text));
238
+ raw.push(...runDetector(text, category, customDetectors, aggressive, plugins));
196
239
  }
197
240
  return deduplicateMatches(raw);
198
241
  }
199
242
  function hasPII(text, categories = ALL_CATEGORIES, customDetectors) {
243
+ return hasPIIWith(text, categories, customDetectors, NO_PLUGINS);
244
+ }
245
+ function hasPIIWith(text, categories, customDetectors, plugins) {
200
246
  if (typeof text !== "string") {
201
247
  throw new ValidationError("text", "must be a string");
202
248
  }
@@ -204,12 +250,14 @@ function hasPII(text, categories = ALL_CATEGORIES, customDetectors) {
204
250
  if (!ALL_CATEGORIES.includes(category)) {
205
251
  throw new UnknownCategoryError(category);
206
252
  }
207
- const detector = resolveDetector(category, customDetectors);
208
- if (detector(text).length > 0) return true;
253
+ if (runDetector(text, category, customDetectors, void 0, plugins).length > 0) return true;
209
254
  }
210
255
  return false;
211
256
  }
212
257
  function anonymize(text, options = {}) {
258
+ return anonymizeWith(text, options, NO_PLUGINS);
259
+ }
260
+ function anonymizeWith(text, options, plugins) {
213
261
  if (typeof text !== "string") {
214
262
  throw new ValidationError("text", "must be a string");
215
263
  }
@@ -254,7 +302,7 @@ function anonymize(text, options = {}) {
254
302
  ...enabledCategories !== void 0 ? { enabledCategories } : {}
255
303
  });
256
304
  const isAllowlisted = compileAllowlist(allowlist, allowlistPatterns, allowlistCaseSensitive);
257
- const builtInMatches = detect(text, categories, customDetectors, aggressive);
305
+ const builtInMatches = detectWith(text, categories, customDetectors, aggressive, plugins);
258
306
  const customMatches = customPatterns.length > 0 ? detectCustomPatterns(text, customPatterns) : [];
259
307
  const filteredBuiltInMatches = builtInMatches.filter(
260
308
  (m) => m.confidence >= confidenceThreshold && !isAllowlisted(m.value)
@@ -286,7 +334,7 @@ function anonymize(text, options = {}) {
286
334
  replacement = match.replacement;
287
335
  } else {
288
336
  const strategy = ruleMap.get(match.category) ?? defaultStrategy;
289
- replacement = applyStrategySync(match.value, strategy);
337
+ replacement = applyStrategy(match.value, strategy, plugins);
290
338
  }
291
339
  result = result.slice(0, match.start) + replacement + result.slice(match.end);
292
340
  }
@@ -297,15 +345,18 @@ function anonymize(text, options = {}) {
297
345
  };
298
346
  }
299
347
  async function anonymizeAsync(text, options = {}) {
348
+ return anonymizeAsyncWith(text, options, NO_PLUGINS);
349
+ }
350
+ async function anonymizeAsyncWith(text, options, plugins) {
300
351
  if (typeof text !== "string") {
301
352
  throw new ValidationError("text", "must be a string");
302
353
  }
303
354
  const hasAsyncStrategy = (opts) => opts.strategy === "hash" || opts.strategy === "encrypt" || opts.strategy === "tokenize" || opts.strategy === "synthesize";
304
355
  const needsAsync = options.defaultStrategy != null && hasAsyncStrategy(options.defaultStrategy) || (options.rules ?? []).some((r) => hasAsyncStrategy(r.strategy));
305
356
  if (!needsAsync) {
306
- return anonymize(text, options);
357
+ return anonymizeWith(text, options, plugins);
307
358
  }
308
- const { hash: hashFn } = await import('./hash-CELLDNKN.js');
359
+ const { hash: hashFn } = await import('./hash-T6YJG3DU.js');
309
360
  let resolvedPreset;
310
361
  if (options.preset) {
311
362
  try {
@@ -341,7 +392,7 @@ async function anonymizeAsync(text, options = {}) {
341
392
  rules,
342
393
  ...enabledCategories !== void 0 ? { enabledCategories } : {}
343
394
  });
344
- const builtInMatches = detect(text, categories, customDetectors, aggressive);
395
+ const builtInMatches = detectWith(text, categories, customDetectors, aggressive, plugins);
345
396
  const customMatches = customPatterns.length > 0 ? detectCustomPatterns(text, customPatterns) : [];
346
397
  const isAllowlisted = compileAllowlist(allowlist, allowlistPatterns, allowlistCaseSensitive);
347
398
  const filteredBuiltInMatches = builtInMatches.filter(
@@ -374,10 +425,10 @@ async function anonymizeAsync(text, options = {}) {
374
425
  replacement = match.replacement;
375
426
  } else {
376
427
  const strategy = ruleMap.get(match.category) ?? defaultStrategy;
377
- if (strategy.strategy === "hash") {
428
+ if (isBuiltinStrategy(strategy) && strategy.strategy === "hash") {
378
429
  replacement = await hashFn(match.value, strategy);
379
430
  } else {
380
- replacement = applyStrategySync(match.value, strategy);
431
+ replacement = applyStrategy(match.value, strategy, plugins);
381
432
  }
382
433
  }
383
434
  result = result.slice(0, match.start) + replacement + result.slice(match.end);
@@ -388,15 +439,18 @@ async function anonymizeAsync(text, options = {}) {
388
439
  };
389
440
  }
390
441
  function anonymizeObject(obj, options) {
442
+ return anonymizeObjectWith(obj, options, NO_PLUGINS);
443
+ }
444
+ function anonymizeObjectWith(obj, options, plugins) {
391
445
  if (typeof obj !== "object") {
392
446
  throw new ValidationError("obj", "must be an object");
393
447
  }
394
- return deepAnonymizeValue(obj, options, /* @__PURE__ */ new WeakSet());
448
+ return deepAnonymizeValue(obj, options, /* @__PURE__ */ new WeakSet(), plugins);
395
449
  }
396
- function deepAnonymizeValue(value, options, seen) {
450
+ function deepAnonymizeValue(value, options, seen, plugins) {
397
451
  if (value === null || value === void 0) return value;
398
452
  if (typeof value === "string") {
399
- return anonymize(value, options).text;
453
+ return anonymizeWith(value, options ?? {}, plugins).text;
400
454
  }
401
455
  if (typeof value !== "object") {
402
456
  return value;
@@ -406,13 +460,18 @@ function deepAnonymizeValue(value, options, seen) {
406
460
  }
407
461
  seen.add(value);
408
462
  if (Array.isArray(value)) {
409
- const result2 = value.map((item) => deepAnonymizeValue(item, options, seen));
463
+ const result2 = value.map((item) => deepAnonymizeValue(item, options, seen, plugins));
410
464
  seen.delete(value);
411
465
  return result2;
412
466
  }
413
467
  const result = {};
414
468
  for (const key of Object.keys(value)) {
415
- result[key] = deepAnonymizeValue(value[key], options, seen);
469
+ result[key] = deepAnonymizeValue(
470
+ value[key],
471
+ options,
472
+ seen,
473
+ plugins
474
+ );
416
475
  }
417
476
  seen.delete(value);
418
477
  return result;
@@ -441,6 +500,9 @@ function setByPath(obj, path, value) {
441
500
  };
442
501
  }
443
502
  function anonymizeRecord(record, rules) {
503
+ return anonymizeRecordWith(record, rules, NO_PLUGINS);
504
+ }
505
+ function anonymizeRecordWith(record, rules, plugins) {
444
506
  if (typeof record !== "object" || Array.isArray(record)) {
445
507
  throw new ValidationError("record", "must be a plain object");
446
508
  }
@@ -451,12 +513,65 @@ function anonymizeRecord(record, rules) {
451
513
  if (raw === void 0 || raw === null) continue;
452
514
  if (typeof raw !== "string" && typeof raw !== "number" && typeof raw !== "boolean") continue;
453
515
  const strValue = String(raw);
454
- const anonymized = applyStrategySync(strValue, rule.strategy);
516
+ const anonymized = applyStrategy(strValue, rule.strategy, plugins);
455
517
  result = setByPath(result, path, anonymized);
456
518
  }
457
519
  }
458
520
  return result;
459
521
  }
522
+ function resolvePlugins(plugins) {
523
+ const names = /* @__PURE__ */ new Set();
524
+ const detectors = {};
525
+ const detectorOwners = /* @__PURE__ */ new Map();
526
+ const strategies = /* @__PURE__ */ new Map();
527
+ const strategyOwners = /* @__PURE__ */ new Map();
528
+ const validators = /* @__PURE__ */ new Map();
529
+ plugins.forEach((plugin, index) => {
530
+ const field = `plugins[${String(index)}]`;
531
+ const { name } = plugin;
532
+ if (typeof name !== "string" || name.length === 0) {
533
+ throw new ValidationError(`${field}.name`, "must be a non-empty string");
534
+ }
535
+ if (names.has(name)) {
536
+ throw new ValidationError(`${field}.name`, `another plugin is named "${name}"`);
537
+ }
538
+ names.add(name);
539
+ for (const [category, detector] of Object.entries(plugin.detectors ?? {})) {
540
+ if (!isCategory(category)) throw new UnknownCategoryError(category);
541
+ const owner = detectorOwners.get(category);
542
+ if (owner !== void 0) {
543
+ throw new ValidationError(
544
+ `${field}.detectors.${category}`,
545
+ `plugin "${owner}" registers a detector for this category`
546
+ );
547
+ }
548
+ detectorOwners.set(category, name);
549
+ detectors[category] = detector;
550
+ }
551
+ for (const [strategyName, strategy] of Object.entries(plugin.strategies ?? {})) {
552
+ if (STRATEGY_NAMES.has(strategyName)) {
553
+ throw new ValidationError(
554
+ `${field}.strategies.${strategyName}`,
555
+ "a built-in strategy has this name"
556
+ );
557
+ }
558
+ const owner = strategyOwners.get(strategyName);
559
+ if (owner !== void 0) {
560
+ throw new ValidationError(
561
+ `${field}.strategies.${strategyName}`,
562
+ `plugin "${owner}" registers a strategy with this name`
563
+ );
564
+ }
565
+ strategyOwners.set(strategyName, name);
566
+ strategies.set(strategyName, strategy);
567
+ }
568
+ for (const [category, validator] of Object.entries(plugin.validators ?? {})) {
569
+ if (!isCategory(category)) throw new UnknownCategoryError(category);
570
+ validators.set(category, [...validators.get(category) ?? [], validator]);
571
+ }
572
+ });
573
+ return { detectors, strategies, validators };
574
+ }
460
575
  function createAnonymizer(config = {}) {
461
576
  let presetConfig;
462
577
  if (config.preset) {
@@ -473,13 +588,18 @@ function createAnonymizer(config = {}) {
473
588
  customPatterns,
474
589
  globalReplacement,
475
590
  consistentTokens,
476
- aggressive
591
+ aggressive,
592
+ plugins = []
477
593
  } = config;
478
594
  for (const cat of categories) {
479
595
  if (!ALL_CATEGORIES.includes(cat)) {
480
596
  throw new UnknownCategoryError(cat);
481
597
  }
482
598
  }
599
+ const registry = resolvePlugins(plugins);
600
+ if (!isBuiltinStrategy(defaultStrategy) && !registry.strategies.has(defaultStrategy.strategy)) {
601
+ throw new UnsupportedStrategyError(defaultStrategy.strategy);
602
+ }
483
603
  function baseOptions(overrides) {
484
604
  const base = {
485
605
  defaultStrategy,
@@ -501,19 +621,19 @@ function createAnonymizer(config = {}) {
501
621
  }
502
622
  return {
503
623
  anonymize(text, options) {
504
- return anonymize(text, baseOptions(options));
624
+ return anonymizeWith(text, baseOptions(options), registry);
505
625
  },
506
626
  detect(text) {
507
- return detect(text, categories, customDetectors, aggressive);
627
+ return detectWith(text, categories, customDetectors, aggressive, registry);
508
628
  },
509
629
  anonymizeRecord(record, rules) {
510
- return anonymizeRecord(record, rules);
630
+ return anonymizeRecordWith(record, rules, registry);
511
631
  },
512
632
  anonymizeObject(obj, options) {
513
- return anonymizeObject(obj, baseOptions(options));
633
+ return anonymizeObjectWith(obj, baseOptions(options), registry);
514
634
  },
515
635
  async anonymizeAsync(text, options) {
516
- return anonymizeAsync(text, baseOptions(options));
636
+ return anonymizeAsyncWith(text, baseOptions(options), registry);
517
637
  },
518
638
  tokenize(text, options) {
519
639
  const {
@@ -526,9 +646,13 @@ function createAnonymizer(config = {}) {
526
646
  allowlistCaseSensitive: tCaseSensitive = false
527
647
  } = options ?? {};
528
648
  const isAllowlisted = compileAllowlist(tAllowlist, [], tCaseSensitive);
529
- const tMatches = detect(text, tCats ?? categories, tCustDet ?? customDetectors, tAgg).filter(
530
- (m) => m.confidence >= tConfidence && !isAllowlisted(m.value)
531
- );
649
+ const tMatches = detectWith(
650
+ text,
651
+ tCats ?? categories,
652
+ tCustDet ?? customDetectors,
653
+ tAgg,
654
+ registry
655
+ ).filter((m) => m.confidence >= tConfidence && !isAllowlisted(m.value));
532
656
  const store = createTokenStore();
533
657
  let tResult = text;
534
658
  const tTokens = [];
@@ -547,7 +671,7 @@ function createAnonymizer(config = {}) {
547
671
  return { text: tResult, mapping: store.tokens, tokens: tTokens };
548
672
  },
549
673
  hasPII(text) {
550
- return hasPII(text, categories, customDetectors);
674
+ return hasPIIWith(text, categories, customDetectors, registry);
551
675
  }
552
676
  };
553
677
  }
@@ -583,10 +707,55 @@ var TOKEN_PREFIX_MAP2 = {
583
707
  "company-registration": "COMPANY_REG"
584
708
  };
585
709
  function tokenize(text, options = {}) {
710
+ const matches = detectForTokenize(text, options);
711
+ if (options.vault !== void 0) {
712
+ const tokenizer = vaultTokenizerFor(options.vault, options.format);
713
+ const placed = matches.map((match) => {
714
+ const token = tokenizer.tokenize(match.value, { category: match.category });
715
+ if (isThenable(token)) {
716
+ void token.catch(() => void 0);
717
+ throw new ValidationError("vault", "answers asynchronously: use tokenizeAsync()");
718
+ }
719
+ return { match, token };
720
+ });
721
+ return replaceInOrder(text, placed);
722
+ }
723
+ const store = createTokenStore();
724
+ let result = text;
725
+ const reversed = [...matches].reverse();
726
+ const tokenMatches = [];
727
+ const format = options.format ?? "bracket";
728
+ for (const match of reversed) {
729
+ const prefix = TOKEN_PREFIX_MAP2[match.category] ?? match.category.toUpperCase();
730
+ const token = assignToken(store, match.category, match.value, prefix, format);
731
+ tokenMatches.push({
732
+ token,
733
+ original: match.value,
734
+ category: match.category,
735
+ start: match.start,
736
+ end: match.end
737
+ });
738
+ result = result.slice(0, match.start) + token + result.slice(match.end);
739
+ }
740
+ return { text: result, mapping: store.tokens, tokens: tokenMatches };
741
+ }
742
+ async function tokenizeAsync(text, options = {}) {
743
+ if (options.vault === void 0) return tokenize(text, options);
744
+ const matches = detectForTokenize(text, options);
745
+ const tokenizer = vaultTokenizerFor(options.vault, options.format);
746
+ const placed = [];
747
+ for (const match of matches) {
748
+ placed.push({
749
+ match,
750
+ token: await tokenizer.tokenize(match.value, { category: match.category })
751
+ });
752
+ }
753
+ return replaceInOrder(text, placed);
754
+ }
755
+ function detectForTokenize(text, options) {
586
756
  if (typeof text !== "string") throw new ValidationError("text", "must be a string");
587
757
  const {
588
758
  categories,
589
- format = "bracket",
590
759
  customDetectors,
591
760
  aggressive = false,
592
761
  confidenceThreshold = 0,
@@ -594,16 +763,22 @@ function tokenize(text, options = {}) {
594
763
  allowlistCaseSensitive = false
595
764
  } = options;
596
765
  const isAllowlisted = compileAllowlist(allowlist, [], allowlistCaseSensitive);
597
- const allMatches = detect(text, categories, customDetectors, aggressive).filter(
766
+ return detect(text, categories, customDetectors, aggressive).filter(
598
767
  (m) => m.confidence >= confidenceThreshold && !isAllowlisted(m.value)
599
768
  );
600
- const store = createTokenStore();
601
- let result = text;
602
- const reversed = [...allMatches].reverse();
769
+ }
770
+ function vaultTokenizerFor(vault, format = "brace") {
771
+ return createVaultTokenizer({ vault, format: format === "custom" ? "angle" : format });
772
+ }
773
+ function replaceInOrder(text, placed) {
774
+ const parts = [];
775
+ const mapping = /* @__PURE__ */ new Map();
603
776
  const tokenMatches = [];
604
- for (const match of reversed) {
605
- const prefix = TOKEN_PREFIX_MAP2[match.category] ?? match.category.toUpperCase();
606
- const token = assignToken(store, match.category, match.value, prefix, format);
777
+ let cursor = 0;
778
+ for (const { match, token } of placed) {
779
+ parts.push(text.slice(cursor, match.start), token);
780
+ cursor = match.end;
781
+ if (!mapping.has(token)) mapping.set(token, match.value);
607
782
  tokenMatches.push({
608
783
  token,
609
784
  original: match.value,
@@ -611,12 +786,9 @@ function tokenize(text, options = {}) {
611
786
  start: match.start,
612
787
  end: match.end
613
788
  });
614
- result = result.slice(0, match.start) + token + result.slice(match.end);
615
789
  }
616
- return { text: result, mapping: store.tokens, tokens: tokenMatches };
617
- }
618
- function tokenizeAsync(text, options = {}) {
619
- return Promise.resolve(tokenize(text, options));
790
+ parts.push(text.slice(cursor));
791
+ return { text: parts.join(""), mapping, tokens: tokenMatches };
620
792
  }
621
793
  function detokenize(text, mapping) {
622
794
  if (typeof text !== "string") throw new ValidationError("text", "must be a string");
@@ -625,8 +797,7 @@ function detokenize(text, mapping) {
625
797
  store.tokens.set(token, value);
626
798
  }
627
799
  const result = detokenizeText(text, store);
628
- const TOKEN_RE = /(\[[A-Z_]+_\d+\]|<[A-Z_]+_\d+>)/g;
629
- const totalInText = (text.match(TOKEN_RE) ?? []).length;
800
+ const totalInText = (text.match(LEGACY_TOKEN_PATTERN) ?? []).length;
630
801
  const replacedCount = totalInText - result.unresolved.length;
631
802
  return {
632
803
  text: result.text,
@@ -636,5 +807,5 @@ function detokenize(text, mapping) {
636
807
  }
637
808
 
638
809
  export { anonymize, anonymizeAsync, anonymizeObject, anonymizeRecord, createAnonymizer, detect, detokenize, hasPII, tokenize, tokenizeAsync };
639
- //# sourceMappingURL=chunk-GBYHQQUY.js.map
640
- //# sourceMappingURL=chunk-GBYHQQUY.js.map
810
+ //# sourceMappingURL=chunk-PSZHBY5V.js.map
811
+ //# sourceMappingURL=chunk-PSZHBY5V.js.map