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.
- package/CHANGELOG.md +131 -2
- package/README.md +125 -7
- package/dist/ai/index.cjs +31 -20
- package/dist/ai/index.cjs.map +1 -1
- package/dist/ai/index.d.cts +5 -5
- package/dist/ai/index.d.ts +5 -5
- package/dist/ai/index.js +24 -13
- package/dist/ai/index.js.map +1 -1
- package/dist/audit/index.cjs +14 -14
- package/dist/audit/index.d.cts +2 -2
- package/dist/audit/index.d.ts +2 -2
- package/dist/audit/index.js +9 -9
- package/dist/{chunk-YMLR375Z.js → chunk-264R3VFP.js} +4 -3
- package/dist/chunk-264R3VFP.js.map +1 -0
- package/dist/{chunk-ISL5B4LU.cjs → chunk-32U3DM7M.cjs} +11 -11
- package/dist/{chunk-ISL5B4LU.cjs.map → chunk-32U3DM7M.cjs.map} +1 -1
- package/dist/{chunk-26IHMQHL.cjs → chunk-42ZZZUMO.cjs} +245 -85
- package/dist/chunk-42ZZZUMO.cjs.map +1 -0
- package/dist/chunk-4BYN6GVK.js +213 -0
- package/dist/chunk-4BYN6GVK.js.map +1 -0
- package/dist/{chunk-JKW36IPT.js → chunk-A2OMTO3L.js} +4 -4
- package/dist/{chunk-JKW36IPT.js.map → chunk-A2OMTO3L.js.map} +1 -1
- package/dist/{chunk-TW7PM3UJ.cjs → chunk-AITKBKQX.cjs} +2 -2
- package/dist/{chunk-TW7PM3UJ.cjs.map → chunk-AITKBKQX.cjs.map} +1 -1
- package/dist/{chunk-G4AN7FND.js → chunk-BWE4O2ES.js} +23 -9
- package/dist/chunk-BWE4O2ES.js.map +1 -0
- package/dist/{chunk-GS3FRH4K.js → chunk-CMJXIEBA.js} +2 -2
- package/dist/chunk-CMJXIEBA.js.map +1 -0
- package/dist/{chunk-GYMG5OI3.cjs → chunk-ESVFPMG4.cjs} +2 -2
- package/dist/{chunk-GYMG5OI3.cjs.map → chunk-ESVFPMG4.cjs.map} +1 -1
- package/dist/{chunk-2YG7R4KJ.cjs → chunk-EUNLKSYP.cjs} +5 -5
- package/dist/{chunk-2YG7R4KJ.cjs.map → chunk-EUNLKSYP.cjs.map} +1 -1
- package/dist/{chunk-S7NH6CHY.cjs → chunk-F3BYJFUM.cjs} +5 -3
- package/dist/chunk-F3BYJFUM.cjs.map +1 -0
- package/dist/{chunk-IJAMLYOT.cjs → chunk-HDHQXVUM.cjs} +50 -36
- package/dist/chunk-HDHQXVUM.cjs.map +1 -0
- package/dist/{chunk-OGNLB2KA.js → chunk-HFAIBE3T.js} +3 -3
- package/dist/{chunk-OGNLB2KA.js.map → chunk-HFAIBE3T.js.map} +1 -1
- package/dist/{chunk-PKHZ7CVI.js → chunk-IBDFBUFZ.js} +2 -2
- package/dist/chunk-IBDFBUFZ.js.map +1 -0
- package/dist/{chunk-3CY6ATKS.cjs → chunk-JBH2EFFF.cjs} +36 -36
- package/dist/{chunk-3CY6ATKS.cjs.map → chunk-JBH2EFFF.cjs.map} +1 -1
- package/dist/chunk-JPU7KQZX.cjs +82 -0
- package/dist/{chunk-CRXRJZEV.cjs.map → chunk-JPU7KQZX.cjs.map} +1 -1
- package/dist/{chunk-E3PICZYI.js → chunk-KHT23UVV.js} +3 -3
- package/dist/{chunk-E3PICZYI.js.map → chunk-KHT23UVV.js.map} +1 -1
- package/dist/{chunk-2ZZHLCEJ.js → chunk-LAZIR73I.js} +18 -87
- package/dist/chunk-LAZIR73I.js.map +1 -0
- package/dist/{chunk-FB56NAAH.cjs → chunk-NO4ZCIM6.cjs} +19 -92
- package/dist/chunk-NO4ZCIM6.cjs.map +1 -0
- package/dist/{chunk-S4BJ2ADB.cjs → chunk-PCU6AG7H.cjs} +4 -4
- package/dist/{chunk-S4BJ2ADB.cjs.map → chunk-PCU6AG7H.cjs.map} +1 -1
- package/dist/{chunk-W6RBCHYF.js → chunk-PSZHBY5V.js} +235 -75
- package/dist/chunk-PSZHBY5V.js.map +1 -0
- package/dist/{chunk-6LDAIJBL.js → chunk-QBBJR7DZ.js} +2 -2
- package/dist/{chunk-6LDAIJBL.js.map → chunk-QBBJR7DZ.js.map} +1 -1
- package/dist/{chunk-3D5ZCJ7R.js → chunk-QHQ2AU45.js} +3 -3
- package/dist/{chunk-3D5ZCJ7R.js.map → chunk-QHQ2AU45.js.map} +1 -1
- package/dist/{chunk-IHEIYZPL.cjs → chunk-RJX27YEX.cjs} +2 -2
- package/dist/chunk-RJX27YEX.cjs.map +1 -0
- package/dist/{chunk-QYD4F6T7.cjs → chunk-ROXNQKOA.cjs} +2 -2
- package/dist/{chunk-QYD4F6T7.cjs.map → chunk-ROXNQKOA.cjs.map} +1 -1
- package/dist/chunk-SHEXFU6T.js +107 -0
- package/dist/chunk-SHEXFU6T.js.map +1 -0
- package/dist/chunk-T2KIN6VL.cjs +218 -0
- package/dist/chunk-T2KIN6VL.cjs.map +1 -0
- package/dist/{chunk-RCW5IPPR.js → chunk-UCT6HDUD.js} +3 -3
- package/dist/{chunk-RCW5IPPR.js.map → chunk-UCT6HDUD.js.map} +1 -1
- package/dist/chunk-UN74SYHV.cjs +115 -0
- package/dist/chunk-UN74SYHV.cjs.map +1 -0
- package/dist/{chunk-F7O2FA3F.cjs → chunk-VAO3Y3HZ.cjs} +7 -6
- package/dist/chunk-VAO3Y3HZ.cjs.map +1 -0
- package/dist/{chunk-BBJPQPGP.js → chunk-VE3OSDM7.js} +5 -3
- package/dist/chunk-VE3OSDM7.js.map +1 -0
- package/dist/{chunk-7HK2V5Q4.js → chunk-X7IVXKDE.js} +3 -3
- package/dist/{chunk-7HK2V5Q4.js.map → chunk-X7IVXKDE.js.map} +1 -1
- package/dist/{chunk-UA3EOFBS.cjs → chunk-XVCVIUZS.cjs} +8 -8
- package/dist/{chunk-UA3EOFBS.cjs.map → chunk-XVCVIUZS.cjs.map} +1 -1
- package/dist/{chunk-SIHPDZXA.js → chunk-YSTC7WRF.js} +2 -2
- package/dist/{chunk-SIHPDZXA.js.map → chunk-YSTC7WRF.js.map} +1 -1
- package/dist/compliance/index.cjs +15 -15
- package/dist/compliance/index.d.cts +4 -4
- package/dist/compliance/index.d.ts +4 -4
- package/dist/compliance/index.js +5 -5
- package/dist/crypto.cjs +4 -4
- package/dist/crypto.d.cts +1 -1
- package/dist/crypto.d.ts +1 -1
- package/dist/crypto.js +2 -2
- package/dist/detectors/index.cjs +38 -38
- package/dist/detectors/index.d.cts +1 -1
- package/dist/detectors/index.d.ts +1 -1
- package/dist/detectors/index.js +3 -3
- package/dist/engine/index.cjs +70 -70
- package/dist/engine/index.d.cts +3 -3
- package/dist/engine/index.d.ts +3 -3
- package/dist/engine/index.js +8 -8
- package/dist/hash-P2VGKVFP.cjs +14 -0
- package/dist/{hash-2PSYNDMM.cjs.map → hash-P2VGKVFP.cjs.map} +1 -1
- package/dist/hash-T6YJG3DU.js +5 -0
- package/dist/{hash-CELLDNKN.js.map → hash-T6YJG3DU.js.map} +1 -1
- package/dist/index.cjs +44 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +23 -5
- package/dist/index.d.ts +23 -5
- package/dist/index.js +11 -9
- package/dist/index.js.map +1 -1
- package/dist/{json-s8ny-RCo.d.cts → json-BXN577tt.d.cts} +3 -2
- package/dist/{json-CKDvlRtl.d.ts → json-BudrzkOQ.d.ts} +3 -2
- package/dist/mcp/index.cjs +29 -28
- package/dist/mcp/index.cjs.map +1 -1
- package/dist/mcp/index.js +14 -13
- package/dist/mcp/index.js.map +1 -1
- package/dist/middleware/express.cjs +11 -11
- package/dist/middleware/express.d.cts +3 -3
- package/dist/middleware/express.d.ts +3 -3
- package/dist/middleware/express.js +10 -10
- package/dist/middleware/hono.cjs +11 -11
- package/dist/middleware/hono.d.cts +3 -3
- package/dist/middleware/hono.d.ts +3 -3
- package/dist/middleware/hono.js +10 -10
- package/dist/middleware/index.cjs +11 -11
- package/dist/middleware/index.d.cts +3 -3
- package/dist/middleware/index.d.ts +3 -3
- package/dist/middleware/index.js +10 -10
- package/dist/schemas.cjs +47 -47
- package/dist/schemas.cjs.map +1 -1
- package/dist/schemas.d.cts +2 -2
- package/dist/schemas.d.ts +2 -2
- package/dist/schemas.js +2 -2
- package/dist/schemas.js.map +1 -1
- package/dist/{session-BdRftyZ5.d.cts → session-CPXEQ2uC.d.cts} +18 -5
- package/dist/{session-Cs2zlNQp.d.ts → session-D3WMN6fE.d.ts} +18 -5
- package/dist/stream.cjs +10 -8
- package/dist/stream.cjs.map +1 -1
- package/dist/stream.d.cts +1 -1
- package/dist/stream.d.ts +1 -1
- package/dist/stream.js +7 -5
- package/dist/stream.js.map +1 -1
- package/dist/{types-Dtht33aq.d.ts → types-B3VjDuP8.d.cts} +2 -282
- package/dist/{types--tPm_AjY.d.ts → types-DHAVKfzT.d.ts} +2 -2
- package/dist/types-LZHFEgx5.d.cts +1083 -0
- package/dist/types-LZHFEgx5.d.ts +1083 -0
- package/dist/{types-0BJH_wqc.d.cts → types-vmONSbz4.d.cts} +2 -2
- package/dist/{types-CPUhjvl8.d.cts → types-yZHnS7yv.d.ts} +2 -282
- package/dist/validators.cjs +10 -10
- package/dist/validators.js +1 -1
- package/dist/vault/index.cjs +65 -46
- package/dist/vault/index.cjs.map +1 -1
- package/dist/vault/index.d.cts +98 -5
- package/dist/vault/index.d.ts +98 -5
- package/dist/vault/index.js +11 -5
- package/dist/vault/index.js.map +1 -1
- package/package.json +16 -7
- package/dist/chunk-26IHMQHL.cjs.map +0 -1
- package/dist/chunk-2ZZHLCEJ.js.map +0 -1
- package/dist/chunk-BBJPQPGP.js.map +0 -1
- package/dist/chunk-CRXRJZEV.cjs +0 -82
- package/dist/chunk-F7O2FA3F.cjs.map +0 -1
- package/dist/chunk-FB56NAAH.cjs.map +0 -1
- package/dist/chunk-G4AN7FND.js.map +0 -1
- package/dist/chunk-GS3FRH4K.js.map +0 -1
- package/dist/chunk-IHEIYZPL.cjs.map +0 -1
- package/dist/chunk-IJAMLYOT.cjs.map +0 -1
- package/dist/chunk-PKHZ7CVI.js.map +0 -1
- package/dist/chunk-S7NH6CHY.cjs.map +0 -1
- package/dist/chunk-W6RBCHYF.js.map +0 -1
- package/dist/chunk-YMLR375Z.js.map +0 -1
- package/dist/hash-2PSYNDMM.cjs +0 -14
- package/dist/hash-CELLDNKN.js +0 -5
- package/dist/types-EJTj_BKY.d.cts +0 -584
- package/dist/types-EJTj_BKY.d.ts +0 -584
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { AGGRESSIVE_DETECTOR_REGISTRY, DETECTOR_REGISTRY } from './chunk-
|
|
2
|
-
import { getPreset, redact, generalize, pseudonymize, mask } from './chunk-
|
|
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
|
|
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);
|
|
@@ -34,6 +35,16 @@ function detokenizeText(text, store) {
|
|
|
34
35
|
return { text: result, unresolved };
|
|
35
36
|
}
|
|
36
37
|
|
|
38
|
+
// src/internal/allowlist.ts
|
|
39
|
+
function compileAllowlist(entries, patterns, caseSensitive) {
|
|
40
|
+
if (entries.length === 0 && patterns.length === 0) return () => false;
|
|
41
|
+
const exact = new Set(entries.map((entry) => caseSensitive ? entry : entry.toLowerCase()));
|
|
42
|
+
const compiled = patterns.map(
|
|
43
|
+
(pattern) => new RegExp(pattern.source, pattern.flags.replace(/[gy]/g, ""))
|
|
44
|
+
);
|
|
45
|
+
return (value) => exact.has(caseSensitive ? value : value.toLowerCase()) || compiled.some((pattern) => pattern.test(value));
|
|
46
|
+
}
|
|
47
|
+
|
|
37
48
|
// src/anonymize.ts
|
|
38
49
|
var ALL_CATEGORIES = [
|
|
39
50
|
"email",
|
|
@@ -93,6 +104,27 @@ var TOKEN_PREFIX_MAP = {
|
|
|
93
104
|
"case-number": "CASE_NUMBER",
|
|
94
105
|
"company-registration": "COMPANY_REG"
|
|
95
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
|
+
}
|
|
96
128
|
function applyStrategySync(value, opts) {
|
|
97
129
|
switch (opts.strategy) {
|
|
98
130
|
case "mask":
|
|
@@ -115,12 +147,23 @@ function applyStrategySync(value, opts) {
|
|
|
115
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.`
|
|
116
148
|
);
|
|
117
149
|
return redact(value);
|
|
150
|
+
/* v8 ignore start -- applyStrategy() passes built-in strategies only */
|
|
118
151
|
default: {
|
|
119
152
|
const _exhaustive = opts;
|
|
120
153
|
throw new UnsupportedStrategyError(_exhaustive.strategy);
|
|
121
154
|
}
|
|
122
155
|
}
|
|
123
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
|
+
}
|
|
124
167
|
function deduplicateMatches(matches) {
|
|
125
168
|
const sorted = [...matches].sort((a, b) => a.start - b.start || b.confidence - a.confidence);
|
|
126
169
|
const result = [];
|
|
@@ -133,10 +176,18 @@ function deduplicateMatches(matches) {
|
|
|
133
176
|
}
|
|
134
177
|
return result;
|
|
135
178
|
}
|
|
136
|
-
function resolveDetector(category, customDetectors, aggressive) {
|
|
179
|
+
function resolveDetector(category, customDetectors, aggressive, plugins) {
|
|
137
180
|
if (customDetectors?.[category]) return customDetectors[category];
|
|
181
|
+
const pluginDetector = plugins.detectors[category];
|
|
182
|
+
if (pluginDetector) return pluginDetector;
|
|
138
183
|
return aggressive ? AGGRESSIVE_DETECTOR_REGISTRY[category] : DETECTOR_REGISTRY[category];
|
|
139
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
|
+
}
|
|
140
191
|
function detectCustomPatterns(text, customPatterns) {
|
|
141
192
|
const results = [];
|
|
142
193
|
for (const cp of customPatterns) {
|
|
@@ -173,6 +224,9 @@ function resolveCategories(options) {
|
|
|
173
224
|
return [...ALL_CATEGORIES];
|
|
174
225
|
}
|
|
175
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) {
|
|
176
230
|
if (typeof text !== "string") {
|
|
177
231
|
throw new ValidationError("text", "must be a string");
|
|
178
232
|
}
|
|
@@ -181,12 +235,14 @@ function detect(text, categories = ALL_CATEGORIES, customDetectors, aggressive)
|
|
|
181
235
|
if (!ALL_CATEGORIES.includes(category)) {
|
|
182
236
|
throw new UnknownCategoryError(category);
|
|
183
237
|
}
|
|
184
|
-
|
|
185
|
-
raw.push(...detector(text));
|
|
238
|
+
raw.push(...runDetector(text, category, customDetectors, aggressive, plugins));
|
|
186
239
|
}
|
|
187
240
|
return deduplicateMatches(raw);
|
|
188
241
|
}
|
|
189
242
|
function hasPII(text, categories = ALL_CATEGORIES, customDetectors) {
|
|
243
|
+
return hasPIIWith(text, categories, customDetectors, NO_PLUGINS);
|
|
244
|
+
}
|
|
245
|
+
function hasPIIWith(text, categories, customDetectors, plugins) {
|
|
190
246
|
if (typeof text !== "string") {
|
|
191
247
|
throw new ValidationError("text", "must be a string");
|
|
192
248
|
}
|
|
@@ -194,12 +250,14 @@ function hasPII(text, categories = ALL_CATEGORIES, customDetectors) {
|
|
|
194
250
|
if (!ALL_CATEGORIES.includes(category)) {
|
|
195
251
|
throw new UnknownCategoryError(category);
|
|
196
252
|
}
|
|
197
|
-
|
|
198
|
-
if (detector(text).length > 0) return true;
|
|
253
|
+
if (runDetector(text, category, customDetectors, void 0, plugins).length > 0) return true;
|
|
199
254
|
}
|
|
200
255
|
return false;
|
|
201
256
|
}
|
|
202
257
|
function anonymize(text, options = {}) {
|
|
258
|
+
return anonymizeWith(text, options, NO_PLUGINS);
|
|
259
|
+
}
|
|
260
|
+
function anonymizeWith(text, options, plugins) {
|
|
203
261
|
if (typeof text !== "string") {
|
|
204
262
|
throw new ValidationError("text", "must be a string");
|
|
205
263
|
}
|
|
@@ -243,17 +301,8 @@ function anonymize(text, options = {}) {
|
|
|
243
301
|
rules,
|
|
244
302
|
...enabledCategories !== void 0 ? { enabledCategories } : {}
|
|
245
303
|
});
|
|
246
|
-
const
|
|
247
|
-
|
|
248
|
-
...allowlist.map(
|
|
249
|
-
(entry) => new RegExp(entry.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), allowlistCaseSensitive ? "" : "i")
|
|
250
|
-
)
|
|
251
|
-
];
|
|
252
|
-
function isAllowlisted(value) {
|
|
253
|
-
if (compiledAllowlistPatterns.length === 0) return false;
|
|
254
|
-
return compiledAllowlistPatterns.some((re) => re.test(value));
|
|
255
|
-
}
|
|
256
|
-
const builtInMatches = detect(text, categories, customDetectors, aggressive);
|
|
304
|
+
const isAllowlisted = compileAllowlist(allowlist, allowlistPatterns, allowlistCaseSensitive);
|
|
305
|
+
const builtInMatches = detectWith(text, categories, customDetectors, aggressive, plugins);
|
|
257
306
|
const customMatches = customPatterns.length > 0 ? detectCustomPatterns(text, customPatterns) : [];
|
|
258
307
|
const filteredBuiltInMatches = builtInMatches.filter(
|
|
259
308
|
(m) => m.confidence >= confidenceThreshold && !isAllowlisted(m.value)
|
|
@@ -285,7 +334,7 @@ function anonymize(text, options = {}) {
|
|
|
285
334
|
replacement = match.replacement;
|
|
286
335
|
} else {
|
|
287
336
|
const strategy = ruleMap.get(match.category) ?? defaultStrategy;
|
|
288
|
-
replacement =
|
|
337
|
+
replacement = applyStrategy(match.value, strategy, plugins);
|
|
289
338
|
}
|
|
290
339
|
result = result.slice(0, match.start) + replacement + result.slice(match.end);
|
|
291
340
|
}
|
|
@@ -296,15 +345,18 @@ function anonymize(text, options = {}) {
|
|
|
296
345
|
};
|
|
297
346
|
}
|
|
298
347
|
async function anonymizeAsync(text, options = {}) {
|
|
348
|
+
return anonymizeAsyncWith(text, options, NO_PLUGINS);
|
|
349
|
+
}
|
|
350
|
+
async function anonymizeAsyncWith(text, options, plugins) {
|
|
299
351
|
if (typeof text !== "string") {
|
|
300
352
|
throw new ValidationError("text", "must be a string");
|
|
301
353
|
}
|
|
302
354
|
const hasAsyncStrategy = (opts) => opts.strategy === "hash" || opts.strategy === "encrypt" || opts.strategy === "tokenize" || opts.strategy === "synthesize";
|
|
303
355
|
const needsAsync = options.defaultStrategy != null && hasAsyncStrategy(options.defaultStrategy) || (options.rules ?? []).some((r) => hasAsyncStrategy(r.strategy));
|
|
304
356
|
if (!needsAsync) {
|
|
305
|
-
return
|
|
357
|
+
return anonymizeWith(text, options, plugins);
|
|
306
358
|
}
|
|
307
|
-
const { hash: hashFn } = await import('./hash-
|
|
359
|
+
const { hash: hashFn } = await import('./hash-T6YJG3DU.js');
|
|
308
360
|
let resolvedPreset;
|
|
309
361
|
if (options.preset) {
|
|
310
362
|
try {
|
|
@@ -340,15 +392,9 @@ async function anonymizeAsync(text, options = {}) {
|
|
|
340
392
|
rules,
|
|
341
393
|
...enabledCategories !== void 0 ? { enabledCategories } : {}
|
|
342
394
|
});
|
|
343
|
-
const builtInMatches =
|
|
395
|
+
const builtInMatches = detectWith(text, categories, customDetectors, aggressive, plugins);
|
|
344
396
|
const customMatches = customPatterns.length > 0 ? detectCustomPatterns(text, customPatterns) : [];
|
|
345
|
-
const
|
|
346
|
-
...allowlistPatterns,
|
|
347
|
-
...allowlist.map(
|
|
348
|
-
(entry) => new RegExp(entry.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), allowlistCaseSensitive ? "" : "i")
|
|
349
|
-
)
|
|
350
|
-
];
|
|
351
|
-
const isAllowlisted = (value) => compiledAllowlistPatterns.some((re) => re.test(value));
|
|
397
|
+
const isAllowlisted = compileAllowlist(allowlist, allowlistPatterns, allowlistCaseSensitive);
|
|
352
398
|
const filteredBuiltInMatches = builtInMatches.filter(
|
|
353
399
|
(m) => m.confidence >= confidenceThreshold && !isAllowlisted(m.value)
|
|
354
400
|
);
|
|
@@ -379,10 +425,10 @@ async function anonymizeAsync(text, options = {}) {
|
|
|
379
425
|
replacement = match.replacement;
|
|
380
426
|
} else {
|
|
381
427
|
const strategy = ruleMap.get(match.category) ?? defaultStrategy;
|
|
382
|
-
if (strategy.strategy === "hash") {
|
|
428
|
+
if (isBuiltinStrategy(strategy) && strategy.strategy === "hash") {
|
|
383
429
|
replacement = await hashFn(match.value, strategy);
|
|
384
430
|
} else {
|
|
385
|
-
replacement =
|
|
431
|
+
replacement = applyStrategy(match.value, strategy, plugins);
|
|
386
432
|
}
|
|
387
433
|
}
|
|
388
434
|
result = result.slice(0, match.start) + replacement + result.slice(match.end);
|
|
@@ -393,15 +439,18 @@ async function anonymizeAsync(text, options = {}) {
|
|
|
393
439
|
};
|
|
394
440
|
}
|
|
395
441
|
function anonymizeObject(obj, options) {
|
|
442
|
+
return anonymizeObjectWith(obj, options, NO_PLUGINS);
|
|
443
|
+
}
|
|
444
|
+
function anonymizeObjectWith(obj, options, plugins) {
|
|
396
445
|
if (typeof obj !== "object") {
|
|
397
446
|
throw new ValidationError("obj", "must be an object");
|
|
398
447
|
}
|
|
399
|
-
return deepAnonymizeValue(obj, options, /* @__PURE__ */ new WeakSet());
|
|
448
|
+
return deepAnonymizeValue(obj, options, /* @__PURE__ */ new WeakSet(), plugins);
|
|
400
449
|
}
|
|
401
|
-
function deepAnonymizeValue(value, options, seen) {
|
|
450
|
+
function deepAnonymizeValue(value, options, seen, plugins) {
|
|
402
451
|
if (value === null || value === void 0) return value;
|
|
403
452
|
if (typeof value === "string") {
|
|
404
|
-
return
|
|
453
|
+
return anonymizeWith(value, options ?? {}, plugins).text;
|
|
405
454
|
}
|
|
406
455
|
if (typeof value !== "object") {
|
|
407
456
|
return value;
|
|
@@ -411,13 +460,18 @@ function deepAnonymizeValue(value, options, seen) {
|
|
|
411
460
|
}
|
|
412
461
|
seen.add(value);
|
|
413
462
|
if (Array.isArray(value)) {
|
|
414
|
-
const result2 = value.map((item) => deepAnonymizeValue(item, options, seen));
|
|
463
|
+
const result2 = value.map((item) => deepAnonymizeValue(item, options, seen, plugins));
|
|
415
464
|
seen.delete(value);
|
|
416
465
|
return result2;
|
|
417
466
|
}
|
|
418
467
|
const result = {};
|
|
419
468
|
for (const key of Object.keys(value)) {
|
|
420
|
-
result[key] = deepAnonymizeValue(
|
|
469
|
+
result[key] = deepAnonymizeValue(
|
|
470
|
+
value[key],
|
|
471
|
+
options,
|
|
472
|
+
seen,
|
|
473
|
+
plugins
|
|
474
|
+
);
|
|
421
475
|
}
|
|
422
476
|
seen.delete(value);
|
|
423
477
|
return result;
|
|
@@ -446,6 +500,9 @@ function setByPath(obj, path, value) {
|
|
|
446
500
|
};
|
|
447
501
|
}
|
|
448
502
|
function anonymizeRecord(record, rules) {
|
|
503
|
+
return anonymizeRecordWith(record, rules, NO_PLUGINS);
|
|
504
|
+
}
|
|
505
|
+
function anonymizeRecordWith(record, rules, plugins) {
|
|
449
506
|
if (typeof record !== "object" || Array.isArray(record)) {
|
|
450
507
|
throw new ValidationError("record", "must be a plain object");
|
|
451
508
|
}
|
|
@@ -456,12 +513,65 @@ function anonymizeRecord(record, rules) {
|
|
|
456
513
|
if (raw === void 0 || raw === null) continue;
|
|
457
514
|
if (typeof raw !== "string" && typeof raw !== "number" && typeof raw !== "boolean") continue;
|
|
458
515
|
const strValue = String(raw);
|
|
459
|
-
const anonymized =
|
|
516
|
+
const anonymized = applyStrategy(strValue, rule.strategy, plugins);
|
|
460
517
|
result = setByPath(result, path, anonymized);
|
|
461
518
|
}
|
|
462
519
|
}
|
|
463
520
|
return result;
|
|
464
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
|
+
}
|
|
465
575
|
function createAnonymizer(config = {}) {
|
|
466
576
|
let presetConfig;
|
|
467
577
|
if (config.preset) {
|
|
@@ -478,13 +588,18 @@ function createAnonymizer(config = {}) {
|
|
|
478
588
|
customPatterns,
|
|
479
589
|
globalReplacement,
|
|
480
590
|
consistentTokens,
|
|
481
|
-
aggressive
|
|
591
|
+
aggressive,
|
|
592
|
+
plugins = []
|
|
482
593
|
} = config;
|
|
483
594
|
for (const cat of categories) {
|
|
484
595
|
if (!ALL_CATEGORIES.includes(cat)) {
|
|
485
596
|
throw new UnknownCategoryError(cat);
|
|
486
597
|
}
|
|
487
598
|
}
|
|
599
|
+
const registry = resolvePlugins(plugins);
|
|
600
|
+
if (!isBuiltinStrategy(defaultStrategy) && !registry.strategies.has(defaultStrategy.strategy)) {
|
|
601
|
+
throw new UnsupportedStrategyError(defaultStrategy.strategy);
|
|
602
|
+
}
|
|
488
603
|
function baseOptions(overrides) {
|
|
489
604
|
const base = {
|
|
490
605
|
defaultStrategy,
|
|
@@ -506,19 +621,19 @@ function createAnonymizer(config = {}) {
|
|
|
506
621
|
}
|
|
507
622
|
return {
|
|
508
623
|
anonymize(text, options) {
|
|
509
|
-
return
|
|
624
|
+
return anonymizeWith(text, baseOptions(options), registry);
|
|
510
625
|
},
|
|
511
626
|
detect(text) {
|
|
512
|
-
return
|
|
627
|
+
return detectWith(text, categories, customDetectors, aggressive, registry);
|
|
513
628
|
},
|
|
514
629
|
anonymizeRecord(record, rules) {
|
|
515
|
-
return
|
|
630
|
+
return anonymizeRecordWith(record, rules, registry);
|
|
516
631
|
},
|
|
517
632
|
anonymizeObject(obj, options) {
|
|
518
|
-
return
|
|
633
|
+
return anonymizeObjectWith(obj, baseOptions(options), registry);
|
|
519
634
|
},
|
|
520
635
|
async anonymizeAsync(text, options) {
|
|
521
|
-
return
|
|
636
|
+
return anonymizeAsyncWith(text, baseOptions(options), registry);
|
|
522
637
|
},
|
|
523
638
|
tokenize(text, options) {
|
|
524
639
|
const {
|
|
@@ -530,13 +645,14 @@ function createAnonymizer(config = {}) {
|
|
|
530
645
|
allowlist: tAllowlist = [],
|
|
531
646
|
allowlistCaseSensitive: tCaseSensitive = false
|
|
532
647
|
} = options ?? {};
|
|
533
|
-
const
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
648
|
+
const isAllowlisted = compileAllowlist(tAllowlist, [], tCaseSensitive);
|
|
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));
|
|
540
656
|
const store = createTokenStore();
|
|
541
657
|
let tResult = text;
|
|
542
658
|
const tTokens = [];
|
|
@@ -555,7 +671,7 @@ function createAnonymizer(config = {}) {
|
|
|
555
671
|
return { text: tResult, mapping: store.tokens, tokens: tTokens };
|
|
556
672
|
},
|
|
557
673
|
hasPII(text) {
|
|
558
|
-
return
|
|
674
|
+
return hasPIIWith(text, categories, customDetectors, registry);
|
|
559
675
|
}
|
|
560
676
|
};
|
|
561
677
|
}
|
|
@@ -591,30 +707,78 @@ var TOKEN_PREFIX_MAP2 = {
|
|
|
591
707
|
"company-registration": "COMPANY_REG"
|
|
592
708
|
};
|
|
593
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) {
|
|
594
756
|
if (typeof text !== "string") throw new ValidationError("text", "must be a string");
|
|
595
757
|
const {
|
|
596
758
|
categories,
|
|
597
|
-
format = "bracket",
|
|
598
759
|
customDetectors,
|
|
599
760
|
aggressive = false,
|
|
600
761
|
confidenceThreshold = 0,
|
|
601
762
|
allowlist = [],
|
|
602
763
|
allowlistCaseSensitive = false
|
|
603
764
|
} = options;
|
|
604
|
-
const
|
|
605
|
-
|
|
606
|
-
);
|
|
607
|
-
const isAllowlisted = (v) => compiledAllowlist.some((re) => re.test(v));
|
|
608
|
-
const allMatches = detect(text, categories, customDetectors, aggressive).filter(
|
|
765
|
+
const isAllowlisted = compileAllowlist(allowlist, [], allowlistCaseSensitive);
|
|
766
|
+
return detect(text, categories, customDetectors, aggressive).filter(
|
|
609
767
|
(m) => m.confidence >= confidenceThreshold && !isAllowlisted(m.value)
|
|
610
768
|
);
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
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();
|
|
614
776
|
const tokenMatches = [];
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
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);
|
|
618
782
|
tokenMatches.push({
|
|
619
783
|
token,
|
|
620
784
|
original: match.value,
|
|
@@ -622,12 +786,9 @@ function tokenize(text, options = {}) {
|
|
|
622
786
|
start: match.start,
|
|
623
787
|
end: match.end
|
|
624
788
|
});
|
|
625
|
-
result = result.slice(0, match.start) + token + result.slice(match.end);
|
|
626
789
|
}
|
|
627
|
-
|
|
628
|
-
}
|
|
629
|
-
function tokenizeAsync(text, options = {}) {
|
|
630
|
-
return Promise.resolve(tokenize(text, options));
|
|
790
|
+
parts.push(text.slice(cursor));
|
|
791
|
+
return { text: parts.join(""), mapping, tokens: tokenMatches };
|
|
631
792
|
}
|
|
632
793
|
function detokenize(text, mapping) {
|
|
633
794
|
if (typeof text !== "string") throw new ValidationError("text", "must be a string");
|
|
@@ -636,8 +797,7 @@ function detokenize(text, mapping) {
|
|
|
636
797
|
store.tokens.set(token, value);
|
|
637
798
|
}
|
|
638
799
|
const result = detokenizeText(text, store);
|
|
639
|
-
const
|
|
640
|
-
const totalInText = (text.match(TOKEN_RE) ?? []).length;
|
|
800
|
+
const totalInText = (text.match(LEGACY_TOKEN_PATTERN) ?? []).length;
|
|
641
801
|
const replacedCount = totalInText - result.unresolved.length;
|
|
642
802
|
return {
|
|
643
803
|
text: result.text,
|
|
@@ -647,5 +807,5 @@ function detokenize(text, mapping) {
|
|
|
647
807
|
}
|
|
648
808
|
|
|
649
809
|
export { anonymize, anonymizeAsync, anonymizeObject, anonymizeRecord, createAnonymizer, detect, detokenize, hasPII, tokenize, tokenizeAsync };
|
|
650
|
-
//# sourceMappingURL=chunk-
|
|
651
|
-
//# sourceMappingURL=chunk-
|
|
810
|
+
//# sourceMappingURL=chunk-PSZHBY5V.js.map
|
|
811
|
+
//# sourceMappingURL=chunk-PSZHBY5V.js.map
|