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