anonyma 1.0.0 → 1.1.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 +273 -144
- package/README.md +427 -143
- package/dist/ai/index.cjs +522 -0
- package/dist/ai/index.cjs.map +1 -0
- package/dist/ai/index.d.cts +322 -0
- package/dist/ai/index.d.ts +322 -0
- package/dist/ai/index.js +505 -0
- package/dist/ai/index.js.map +1 -0
- package/dist/audit/index.cjs +359 -0
- package/dist/audit/index.cjs.map +1 -0
- package/dist/audit/index.d.cts +287 -0
- package/dist/audit/index.d.ts +287 -0
- package/dist/audit/index.js +340 -0
- package/dist/audit/index.js.map +1 -0
- package/dist/chunk-26IHMQHL.cjs +662 -0
- package/dist/chunk-26IHMQHL.cjs.map +1 -0
- package/dist/chunk-2F2GWGTB.js +174 -0
- package/dist/chunk-2F2GWGTB.js.map +1 -0
- package/dist/chunk-2NNMJ55L.cjs +45 -0
- package/dist/chunk-2NNMJ55L.cjs.map +1 -0
- package/dist/chunk-2YG7R4KJ.cjs +114 -0
- package/dist/chunk-2YG7R4KJ.cjs.map +1 -0
- package/dist/chunk-2ZZHLCEJ.js +237 -0
- package/dist/chunk-2ZZHLCEJ.js.map +1 -0
- package/dist/chunk-3CY6ATKS.cjs +308 -0
- package/dist/chunk-3CY6ATKS.cjs.map +1 -0
- package/dist/chunk-3D5ZCJ7R.js +268 -0
- package/dist/chunk-3D5ZCJ7R.js.map +1 -0
- package/dist/chunk-6LDAIJBL.js +103 -0
- package/dist/chunk-6LDAIJBL.js.map +1 -0
- package/dist/chunk-7HK2V5Q4.js +111 -0
- package/dist/chunk-7HK2V5Q4.js.map +1 -0
- package/dist/chunk-AAP472NG.js +177 -0
- package/dist/chunk-AAP472NG.js.map +1 -0
- package/dist/chunk-AP4JIUNW.js +42 -0
- package/dist/chunk-AP4JIUNW.js.map +1 -0
- package/dist/chunk-BBJPQPGP.js +457 -0
- package/dist/chunk-BBJPQPGP.js.map +1 -0
- package/dist/chunk-CRXRJZEV.cjs +82 -0
- package/dist/chunk-CRXRJZEV.cjs.map +1 -0
- package/dist/chunk-E3PICZYI.js +20 -0
- package/dist/chunk-E3PICZYI.js.map +1 -0
- package/dist/chunk-F7O2FA3F.cjs +1255 -0
- package/dist/chunk-F7O2FA3F.cjs.map +1 -0
- package/dist/chunk-FB56NAAH.cjs +243 -0
- package/dist/chunk-FB56NAAH.cjs.map +1 -0
- package/dist/chunk-G4AN7FND.js +1063 -0
- package/dist/chunk-G4AN7FND.js.map +1 -0
- package/dist/chunk-GS3FRH4K.js +294 -0
- package/dist/chunk-GS3FRH4K.js.map +1 -0
- package/dist/chunk-GYMG5OI3.cjs +186 -0
- package/dist/chunk-GYMG5OI3.cjs.map +1 -0
- package/dist/chunk-IHEIYZPL.cjs +301 -0
- package/dist/chunk-IHEIYZPL.cjs.map +1 -0
- package/dist/chunk-IJAMLYOT.cjs +1087 -0
- package/dist/chunk-IJAMLYOT.cjs.map +1 -0
- package/dist/chunk-ISL5B4LU.cjs +181 -0
- package/dist/chunk-ISL5B4LU.cjs.map +1 -0
- package/dist/chunk-JKW36IPT.js +179 -0
- package/dist/chunk-JKW36IPT.js.map +1 -0
- package/dist/chunk-KOCQBDI4.cjs +179 -0
- package/dist/chunk-KOCQBDI4.cjs.map +1 -0
- package/dist/chunk-OGNLB2KA.js +79 -0
- package/dist/chunk-OGNLB2KA.js.map +1 -0
- package/dist/chunk-PKHZ7CVI.js +28 -0
- package/dist/chunk-PKHZ7CVI.js.map +1 -0
- package/dist/chunk-QYD4F6T7.cjs +114 -0
- package/dist/chunk-QYD4F6T7.cjs.map +1 -0
- package/dist/chunk-RCW5IPPR.js +1288 -0
- package/dist/chunk-RCW5IPPR.js.map +1 -0
- package/dist/chunk-RMMKYHUN.cjs +35 -0
- package/dist/chunk-RMMKYHUN.cjs.map +1 -0
- package/dist/chunk-S4BJ2ADB.cjs +22 -0
- package/dist/chunk-S4BJ2ADB.cjs.map +1 -0
- package/dist/chunk-S7NH6CHY.cjs +462 -0
- package/dist/chunk-S7NH6CHY.cjs.map +1 -0
- package/dist/chunk-SIHPDZXA.js +176 -0
- package/dist/chunk-SIHPDZXA.js.map +1 -0
- package/dist/chunk-TW7PM3UJ.cjs +30 -0
- package/dist/chunk-TW7PM3UJ.cjs.map +1 -0
- package/dist/chunk-UA3EOFBS.cjs +1322 -0
- package/dist/chunk-UA3EOFBS.cjs.map +1 -0
- package/dist/chunk-VE4CCW7W.js +32 -0
- package/dist/chunk-VE4CCW7W.js.map +1 -0
- package/dist/chunk-W6RBCHYF.js +651 -0
- package/dist/chunk-W6RBCHYF.js.map +1 -0
- package/dist/chunk-WLJN7GIS.cjs +189 -0
- package/dist/chunk-WLJN7GIS.cjs.map +1 -0
- package/dist/chunk-YMLR375Z.js +1244 -0
- package/dist/chunk-YMLR375Z.js.map +1 -0
- package/dist/compliance/index.cjs +88 -0
- package/dist/compliance/index.cjs.map +1 -0
- package/dist/compliance/index.d.cts +277 -0
- package/dist/compliance/index.d.ts +277 -0
- package/dist/compliance/index.js +47 -0
- package/dist/compliance/index.js.map +1 -0
- package/dist/crypto.cjs +12 -136
- package/dist/crypto.cjs.map +1 -1
- package/dist/crypto.d.cts +4 -3
- package/dist/crypto.d.ts +4 -3
- package/dist/crypto.js +3 -137
- package/dist/crypto.js.map +1 -1
- package/dist/detectors/index.cjs +146 -1483
- package/dist/detectors/index.cjs.map +1 -1
- package/dist/detectors/index.d.cts +1 -1
- package/dist/detectors/index.d.ts +1 -1
- package/dist/detectors/index.js +3 -1449
- package/dist/detectors/index.js.map +1 -1
- package/dist/engine/index.cjs +441 -0
- package/dist/engine/index.cjs.map +1 -0
- package/dist/engine/index.d.cts +728 -0
- package/dist/engine/index.d.ts +728 -0
- package/dist/engine/index.js +189 -0
- package/dist/engine/index.js.map +1 -0
- package/dist/hash-2PSYNDMM.cjs +14 -0
- package/dist/hash-2PSYNDMM.cjs.map +1 -0
- package/dist/hash-CELLDNKN.js +5 -0
- package/dist/hash-CELLDNKN.js.map +1 -0
- package/dist/index.cjs +153 -2816
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +93 -11
- package/dist/index.d.ts +93 -11
- package/dist/index.js +13 -2791
- package/dist/index.js.map +1 -1
- package/dist/json-CKDvlRtl.d.ts +147 -0
- package/dist/json-s8ny-RCo.d.cts +147 -0
- package/dist/mcp/index.cjs +751 -0
- package/dist/mcp/index.cjs.map +1 -0
- package/dist/mcp/index.d.cts +226 -0
- package/dist/mcp/index.d.ts +226 -0
- package/dist/mcp/index.js +744 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/middleware/express.cjs +93 -0
- package/dist/middleware/express.cjs.map +1 -0
- package/dist/middleware/express.d.cts +90 -0
- package/dist/middleware/express.d.ts +90 -0
- package/dist/middleware/express.js +91 -0
- package/dist/middleware/express.js.map +1 -0
- package/dist/middleware/hono.cjs +58 -0
- package/dist/middleware/hono.cjs.map +1 -0
- package/dist/middleware/hono.d.cts +75 -0
- package/dist/middleware/hono.d.ts +75 -0
- package/dist/middleware/hono.js +56 -0
- package/dist/middleware/hono.js.map +1 -0
- package/dist/middleware/index.cjs +24 -0
- package/dist/middleware/index.cjs.map +1 -0
- package/dist/middleware/index.d.cts +106 -0
- package/dist/middleware/index.d.ts +106 -0
- package/dist/middleware/index.js +15 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/schemas.cjs +5 -5
- package/dist/schemas.cjs.map +1 -1
- package/dist/schemas.d.cts +20 -10
- package/dist/schemas.d.ts +20 -10
- package/dist/schemas.js +5 -5
- package/dist/schemas.js.map +1 -1
- package/dist/session-BdRftyZ5.d.cts +102 -0
- package/dist/session-Cs2zlNQp.d.ts +102 -0
- package/dist/stream.cjs +13 -2262
- 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 +6 -2255
- package/dist/stream.js.map +1 -1
- package/dist/types--tPm_AjY.d.ts +199 -0
- package/dist/types-0BJH_wqc.d.cts +199 -0
- package/dist/types-CPUhjvl8.d.cts +629 -0
- package/dist/types-Dtht33aq.d.ts +629 -0
- package/dist/{types-D-9HzVuy.d.cts → types-EJTj_BKY.d.cts} +38 -7
- package/dist/{types-D-9HzVuy.d.ts → types-EJTj_BKY.d.ts} +38 -7
- package/dist/types-Zt_bZvwS.d.cts +182 -0
- package/dist/types-Zt_bZvwS.d.ts +182 -0
- package/dist/validators.cjs +39 -181
- package/dist/validators.cjs.map +1 -1
- package/dist/validators.js +1 -174
- package/dist/validators.js.map +1 -1
- package/dist/vault/index.cjs +688 -0
- package/dist/vault/index.cjs.map +1 -0
- package/dist/vault/index.d.cts +385 -0
- package/dist/vault/index.d.ts +385 -0
- package/dist/vault/index.js +669 -0
- package/dist/vault/index.js.map +1 -0
- package/package.json +128 -11
package/dist/stream.cjs
CHANGED
|
@@ -1,2265 +1,16 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
// src/errors.ts
|
|
14
|
-
var AnonymaError, ValidationError, UnsupportedStrategyError, UnknownCategoryError, CryptoNotAvailableError, PresetNotFoundError;
|
|
15
|
-
var init_errors = __esm({
|
|
16
|
-
"src/errors.ts"() {
|
|
17
|
-
AnonymaError = class extends Error {
|
|
18
|
-
/** Machine-readable error code. */
|
|
19
|
-
code;
|
|
20
|
-
constructor(message, code) {
|
|
21
|
-
super(message);
|
|
22
|
-
this.name = "AnonymaError";
|
|
23
|
-
this.code = code;
|
|
24
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
25
|
-
}
|
|
26
|
-
};
|
|
27
|
-
ValidationError = class extends AnonymaError {
|
|
28
|
-
/** The name of the invalid field or parameter. */
|
|
29
|
-
field;
|
|
30
|
-
constructor(field, reason) {
|
|
31
|
-
super(`Validation failed for \`${field}\`: ${reason}`, "VALIDATION_ERROR");
|
|
32
|
-
this.name = "ValidationError";
|
|
33
|
-
this.field = field;
|
|
34
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
UnsupportedStrategyError = class extends AnonymaError {
|
|
38
|
-
/** The strategy name that was not recognised. */
|
|
39
|
-
strategy;
|
|
40
|
-
constructor(strategy) {
|
|
41
|
-
super(
|
|
42
|
-
`Unsupported anonymization strategy: "${strategy}". Supported strategies are: mask, redact, pseudonymize, hash, generalize.`,
|
|
43
|
-
"UNSUPPORTED_STRATEGY"
|
|
44
|
-
);
|
|
45
|
-
this.name = "UnsupportedStrategyError";
|
|
46
|
-
this.strategy = strategy;
|
|
47
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
UnknownCategoryError = class extends AnonymaError {
|
|
51
|
-
/** The category name that was not recognised. */
|
|
52
|
-
category;
|
|
53
|
-
constructor(category) {
|
|
54
|
-
super(
|
|
55
|
-
`Unknown PII category: "${category}". Supported categories are: email, phone, ssn, credit-card, ipv4, ipv6, url, iban, date-of-birth.`,
|
|
56
|
-
"UNKNOWN_CATEGORY"
|
|
57
|
-
);
|
|
58
|
-
this.name = "UnknownCategoryError";
|
|
59
|
-
this.category = category;
|
|
60
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
CryptoNotAvailableError = class extends AnonymaError {
|
|
64
|
-
constructor() {
|
|
65
|
-
super(
|
|
66
|
-
"The Web Crypto API (globalThis.crypto.subtle) is not available in this environment. Upgrade to Node.js \u2265 18 or use a polyfill.",
|
|
67
|
-
"CRYPTO_NOT_AVAILABLE"
|
|
68
|
-
);
|
|
69
|
-
this.name = "CryptoNotAvailableError";
|
|
70
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
71
|
-
}
|
|
72
|
-
};
|
|
73
|
-
PresetNotFoundError = class extends AnonymaError {
|
|
74
|
-
/** The preset name that was not recognised. */
|
|
75
|
-
preset;
|
|
76
|
-
constructor(preset) {
|
|
77
|
-
super(
|
|
78
|
-
`Unknown compliance preset: "${preset}". Available presets are: gdpr, hipaa, ccpa, pci-dss, sox, ferpa.`,
|
|
79
|
-
"PRESET_NOT_FOUND"
|
|
80
|
-
);
|
|
81
|
-
this.name = "PresetNotFoundError";
|
|
82
|
-
this.preset = preset;
|
|
83
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
// src/strategies/hash.ts
|
|
90
|
-
var hash_exports = {};
|
|
91
|
-
__export(hash_exports, {
|
|
92
|
-
hash: () => hash
|
|
93
|
-
});
|
|
94
|
-
function assertCrypto() {
|
|
95
|
-
const subtle = globalThis.crypto?.subtle;
|
|
96
|
-
if (typeof subtle === "undefined") {
|
|
97
|
-
throw new CryptoNotAvailableError();
|
|
98
|
-
}
|
|
99
|
-
return subtle;
|
|
100
|
-
}
|
|
101
|
-
async function hash(value, options = {}) {
|
|
102
|
-
const { truncate = 16, pepper } = options;
|
|
103
|
-
if (!Number.isInteger(truncate) || truncate < 1 || truncate > 64) {
|
|
104
|
-
throw new ValidationError("truncate", "must be an integer between 1 and 64 (inclusive)");
|
|
105
|
-
}
|
|
106
|
-
const subtle = assertCrypto();
|
|
107
|
-
const input = pepper !== void 0 ? `${pepper}:${value}` : value;
|
|
108
|
-
const encoded = new TextEncoder().encode(input);
|
|
109
|
-
const buffer = await subtle.digest("SHA-256", encoded);
|
|
110
|
-
const hex = Array.from(new Uint8Array(buffer)).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
111
|
-
return hex.slice(0, truncate);
|
|
112
|
-
}
|
|
113
|
-
var init_hash = __esm({
|
|
114
|
-
"src/strategies/hash.ts"() {
|
|
115
|
-
init_errors();
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
// src/detectors/email.ts
|
|
120
|
-
var EMAIL_PATTERN = /\b[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*\.[a-zA-Z]{2,}\b/g;
|
|
121
|
-
var EMAIL_AGGRESSIVE_PATTERN = /\b[a-z0-9][a-z0-9._-]*\s*(?:@|\[at\]|\(at\))\s*[a-z0-9][\w.-]*\s*(?:\.|\[dot\]|\(dot\))\s*[a-z]{2,}\b/gi;
|
|
122
|
-
function detectEmail(text) {
|
|
123
|
-
const matches = [];
|
|
124
|
-
const pattern = new RegExp(EMAIL_PATTERN.source, "g");
|
|
125
|
-
let match;
|
|
126
|
-
while ((match = pattern.exec(text)) !== null) {
|
|
127
|
-
matches.push({
|
|
128
|
-
category: "email",
|
|
129
|
-
value: match[0],
|
|
130
|
-
start: match.index,
|
|
131
|
-
end: match.index + match[0].length,
|
|
132
|
-
confidence: 0.99
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
return matches;
|
|
136
|
-
}
|
|
137
|
-
function detectEmailAggressive(text) {
|
|
138
|
-
const standard = detectEmail(text);
|
|
139
|
-
const standardRanges = standard.map((m) => [m.start, m.end]);
|
|
140
|
-
const extra = [];
|
|
141
|
-
const aggressivePattern = new RegExp(EMAIL_AGGRESSIVE_PATTERN.source, "gi");
|
|
142
|
-
let match;
|
|
143
|
-
while ((match = aggressivePattern.exec(text)) !== null) {
|
|
144
|
-
const start = match.index;
|
|
145
|
-
const end = start + match[0].length;
|
|
146
|
-
const overlaps = standardRanges.some(([s, e]) => start < e && end > s);
|
|
147
|
-
if (!overlaps) {
|
|
148
|
-
extra.push({
|
|
149
|
-
category: "email",
|
|
150
|
-
value: match[0],
|
|
151
|
-
start,
|
|
152
|
-
end,
|
|
153
|
-
confidence: 0.75
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
return [...standard, ...extra];
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
// src/detectors/phone.ts
|
|
161
|
-
var PHONE_PATTERN = /(?<!\d)(?:\+?1[-.\s]?)?(?:\(\d{3}\)|\d{3})[-.\s]?\d{3}[-.\s]?\d{4}(?!\d)|(?:\+(?:[0-9] ?){6,14}[0-9])(?!\d)/g;
|
|
162
|
-
var PHONE_SEVEN_DIGIT_PATTERN = /\b\d{3}[-.\s]\d{4}\b/g;
|
|
163
|
-
function detectPhone(text) {
|
|
164
|
-
const matches = [];
|
|
165
|
-
const pattern = new RegExp(PHONE_PATTERN.source, "g");
|
|
166
|
-
let match;
|
|
167
|
-
while ((match = pattern.exec(text)) !== null) {
|
|
168
|
-
const value = match[0].trim();
|
|
169
|
-
if (value.replace(/\D/g, "").length < 7) continue;
|
|
170
|
-
matches.push({
|
|
171
|
-
category: "phone",
|
|
172
|
-
value,
|
|
173
|
-
start: match.index,
|
|
174
|
-
end: match.index + match[0].length,
|
|
175
|
-
confidence: 0.9
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
return matches;
|
|
179
|
-
}
|
|
180
|
-
function detectPhoneAggressive(text) {
|
|
181
|
-
const standard = detectPhone(text);
|
|
182
|
-
const standardRanges = standard.map((m) => [m.start, m.end]);
|
|
183
|
-
const extra = [];
|
|
184
|
-
const sevenDigit = new RegExp(PHONE_SEVEN_DIGIT_PATTERN.source, "g");
|
|
185
|
-
let match;
|
|
186
|
-
while ((match = sevenDigit.exec(text)) !== null) {
|
|
187
|
-
const start = match.index;
|
|
188
|
-
const end = start + match[0].length;
|
|
189
|
-
const overlaps = standardRanges.some(([s, e]) => start < e && end > s);
|
|
190
|
-
if (!overlaps) {
|
|
191
|
-
extra.push({
|
|
192
|
-
category: "phone",
|
|
193
|
-
value: match[0],
|
|
194
|
-
start,
|
|
195
|
-
end,
|
|
196
|
-
confidence: 0.7
|
|
197
|
-
});
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
return [...standard, ...extra];
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
// src/detectors/ssn.ts
|
|
204
|
-
var SSN_PATTERN = /\b(?!000|666|9\d{2})\d{3}[- ]?(?!00)\d{2}[- ]?(?!0{4})\d{4}\b/g;
|
|
205
|
-
var SSN_AGGRESSIVE_PATTERN = /\b(?!000|666|9\d{2})\d{3}(?!00)\d{2}(?!0{4})\d{4}\b/g;
|
|
206
|
-
function detectSsn(text) {
|
|
207
|
-
const matches = [];
|
|
208
|
-
const pattern = new RegExp(SSN_PATTERN.source, "g");
|
|
209
|
-
let match;
|
|
210
|
-
while ((match = pattern.exec(text)) !== null) {
|
|
211
|
-
matches.push({
|
|
212
|
-
category: "ssn",
|
|
213
|
-
value: match[0],
|
|
214
|
-
start: match.index,
|
|
215
|
-
end: match.index + match[0].length,
|
|
216
|
-
confidence: 0.95
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
return matches;
|
|
220
|
-
}
|
|
221
|
-
function detectSsnAggressive(text) {
|
|
222
|
-
const standard = detectSsn(text);
|
|
223
|
-
const standardRanges = standard.map((m) => [m.start, m.end]);
|
|
224
|
-
const extra = [];
|
|
225
|
-
const aggressive = new RegExp(SSN_AGGRESSIVE_PATTERN.source, "g");
|
|
226
|
-
let match;
|
|
227
|
-
while ((match = aggressive.exec(text)) !== null) {
|
|
228
|
-
const start = match.index;
|
|
229
|
-
const end = start + match[0].length;
|
|
230
|
-
const overlaps = standardRanges.some(([s, e]) => start < e && end > s);
|
|
231
|
-
if (!overlaps) {
|
|
232
|
-
extra.push({
|
|
233
|
-
category: "ssn",
|
|
234
|
-
value: match[0],
|
|
235
|
-
start,
|
|
236
|
-
end,
|
|
237
|
-
confidence: 0.8
|
|
238
|
-
});
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
return [...standard, ...extra];
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
// src/detectors/credit-card.ts
|
|
245
|
-
var CARD_PATTERN = /\b(?:\d[ -]?){13,19}\b/g;
|
|
246
|
-
var CARD_MASKED_PATTERN = /\*{4}[ -]?\*{4}[ -]?\*{4}[ -]?\d{4}\b/g;
|
|
247
|
-
function luhn(digits) {
|
|
248
|
-
let sum = 0;
|
|
249
|
-
let isEven = false;
|
|
250
|
-
for (let i = digits.length - 1; i >= 0; i--) {
|
|
251
|
-
let d = parseInt(digits[i] ?? "0", 10);
|
|
252
|
-
if (isEven) {
|
|
253
|
-
d *= 2;
|
|
254
|
-
if (d > 9) d -= 9;
|
|
255
|
-
}
|
|
256
|
-
sum += d;
|
|
257
|
-
isEven = !isEven;
|
|
258
|
-
}
|
|
259
|
-
return sum % 10 === 0;
|
|
260
|
-
}
|
|
261
|
-
function detectCreditCard(text) {
|
|
262
|
-
const matches = [];
|
|
263
|
-
const pattern = new RegExp(CARD_PATTERN.source, "g");
|
|
264
|
-
let match;
|
|
265
|
-
while ((match = pattern.exec(text)) !== null) {
|
|
266
|
-
const raw = match[0];
|
|
267
|
-
const digitsOnly = raw.replace(/[ -]/g, "");
|
|
268
|
-
if (digitsOnly.length < 13 || digitsOnly.length > 19) continue;
|
|
269
|
-
if (!luhn(digitsOnly)) continue;
|
|
270
|
-
matches.push({
|
|
271
|
-
category: "credit-card",
|
|
272
|
-
value: raw,
|
|
273
|
-
start: match.index,
|
|
274
|
-
end: match.index + raw.length,
|
|
275
|
-
confidence: 0.97
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
return matches;
|
|
279
|
-
}
|
|
280
|
-
function detectCreditCardAggressive(text) {
|
|
281
|
-
const standard = detectCreditCard(text);
|
|
282
|
-
const standardRanges = standard.map((m) => [m.start, m.end]);
|
|
283
|
-
const extra = [];
|
|
284
|
-
const maskedPattern = new RegExp(CARD_MASKED_PATTERN.source, "g");
|
|
285
|
-
let match;
|
|
286
|
-
while ((match = maskedPattern.exec(text)) !== null) {
|
|
287
|
-
const start = match.index;
|
|
288
|
-
const end = start + match[0].length;
|
|
289
|
-
const overlaps = standardRanges.some(([s, e]) => start < e && end > s);
|
|
290
|
-
if (!overlaps) {
|
|
291
|
-
extra.push({
|
|
292
|
-
category: "credit-card",
|
|
293
|
-
value: match[0],
|
|
294
|
-
start,
|
|
295
|
-
end,
|
|
296
|
-
confidence: 0.8
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
return [...standard, ...extra];
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
// src/detectors/ip-address.ts
|
|
304
|
-
var IPV4_PATTERN = /\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)(?:\/\d{1,2})?\b/g;
|
|
305
|
-
var IPV6_PATTERN = /\b(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}\b|\b(?:[0-9a-fA-F]{1,4}:){1,7}:\b|\b(?:[0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}\b|\b(?:[0-9a-fA-F]{1,4}:){1,5}(?::[0-9a-fA-F]{1,4}){1,2}\b|\b(?:[0-9a-fA-F]{1,4}:){1,4}(?::[0-9a-fA-F]{1,4}){1,3}\b|\b(?:[0-9a-fA-F]{1,4}:){1,3}(?::[0-9a-fA-F]{1,4}){1,4}\b|\b(?:[0-9a-fA-F]{1,4}:){1,2}(?::[0-9a-fA-F]{1,4}){1,5}\b|\b[0-9a-fA-F]{1,4}:(?::[0-9a-fA-F]{1,4}){1,6}\b|\b:(?::[0-9a-fA-F]{1,4}){1,7}\b|\b::(?:[fF]{4}(?::0{1,4})?:)?(?:25[0-5]|2[0-4]\d|[01]?\d\d?)(?:\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)){3}\b/g;
|
|
306
|
-
function detectIpv4(text) {
|
|
307
|
-
const matches = [];
|
|
308
|
-
const pattern = new RegExp(IPV4_PATTERN.source, "g");
|
|
309
|
-
let match;
|
|
310
|
-
while ((match = pattern.exec(text)) !== null) {
|
|
311
|
-
matches.push({
|
|
312
|
-
category: "ipv4",
|
|
313
|
-
value: match[0],
|
|
314
|
-
start: match.index,
|
|
315
|
-
end: match.index + match[0].length,
|
|
316
|
-
confidence: 0.98
|
|
317
|
-
});
|
|
318
|
-
}
|
|
319
|
-
return matches;
|
|
320
|
-
}
|
|
321
|
-
function detectIpv6(text) {
|
|
322
|
-
const matches = [];
|
|
323
|
-
const pattern = new RegExp(IPV6_PATTERN.source, "g");
|
|
324
|
-
let match;
|
|
325
|
-
while ((match = pattern.exec(text)) !== null) {
|
|
326
|
-
matches.push({
|
|
327
|
-
category: "ipv6",
|
|
328
|
-
value: match[0],
|
|
329
|
-
start: match.index,
|
|
330
|
-
end: match.index + match[0].length,
|
|
331
|
-
confidence: 0.98
|
|
332
|
-
});
|
|
333
|
-
}
|
|
334
|
-
return matches;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
// src/detectors/url.ts
|
|
338
|
-
var URL_PATTERN = /\bhttps?:\/\/(?:(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}|localhost|(?:\d{1,3}\.){3}\d{1,3})(?::\d{1,5})?(?:\/[^\s,;'"<>()[\]{}]*)?(?:\?[^\s,;'"<>()[\]{}]*)?(?:#[^\s,;'"<>()[\]{}]*)?\b/g;
|
|
339
|
-
function detectUrl(text) {
|
|
340
|
-
const matches = [];
|
|
341
|
-
const pattern = new RegExp(URL_PATTERN.source, "g");
|
|
342
|
-
let match;
|
|
343
|
-
while ((match = pattern.exec(text)) !== null) {
|
|
344
|
-
matches.push({
|
|
345
|
-
category: "url",
|
|
346
|
-
value: match[0],
|
|
347
|
-
start: match.index,
|
|
348
|
-
end: match.index + match[0].length,
|
|
349
|
-
confidence: 0.95
|
|
350
|
-
});
|
|
351
|
-
}
|
|
352
|
-
return matches;
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
// src/detectors/iban.ts
|
|
356
|
-
var IBAN_PATTERN = /\b[A-Z]{2}\d{2}[ ]?(?:[A-Z0-9]{4}[ ]?){1,7}[A-Z0-9]{1,4}\b/g;
|
|
357
|
-
function isValidIban(iban) {
|
|
358
|
-
const cleaned = iban.replace(/\s/g, "").toUpperCase();
|
|
359
|
-
if (cleaned.length < 5 || cleaned.length > 34) return false;
|
|
360
|
-
const rearranged = cleaned.slice(4) + cleaned.slice(0, 4);
|
|
361
|
-
const numeric = rearranged.split("").map((ch) => {
|
|
362
|
-
const code = ch.charCodeAt(0);
|
|
363
|
-
return code >= 65 && code <= 90 ? String(code - 55) : ch;
|
|
364
|
-
}).join("");
|
|
365
|
-
return BigInt(numeric) % 97n === 1n;
|
|
366
|
-
}
|
|
367
|
-
function detectIban(text) {
|
|
368
|
-
const matches = [];
|
|
369
|
-
const pattern = new RegExp(IBAN_PATTERN.source, "g");
|
|
370
|
-
let match;
|
|
371
|
-
while ((match = pattern.exec(text)) !== null) {
|
|
372
|
-
const value = match[0];
|
|
373
|
-
if (!isValidIban(value)) continue;
|
|
374
|
-
matches.push({
|
|
375
|
-
category: "iban",
|
|
376
|
-
value,
|
|
377
|
-
start: match.index,
|
|
378
|
-
end: match.index + value.length,
|
|
379
|
-
confidence: 0.99
|
|
380
|
-
});
|
|
381
|
-
}
|
|
382
|
-
return matches;
|
|
383
|
-
}
|
|
384
|
-
|
|
385
|
-
// src/detectors/date-of-birth.ts
|
|
386
|
-
var MONTH_NAMES = "(?:January|February|March|April|May|June|July|August|September|October|November|December|Jan|Feb|Mar|Apr|Jun|Jul|Aug|Sep|Oct|Nov|Dec)";
|
|
387
|
-
var ISO_PATTERN = /\b(19|20)\d{2}[- /](0[1-9]|1[0-2])[- /](0[1-9]|[12]\d|3[01])\b/g;
|
|
388
|
-
var US_PATTERN = /\b(0[1-9]|1[0-2])[-/](0[1-9]|[12]\d|3[01])[-/](19|20)\d{2}\b/g;
|
|
389
|
-
var EU_PATTERN = /\b(0[1-9]|[12]\d|3[01])[/.](0[1-9]|1[0-2])[/.](19|20)\d{2}\b/g;
|
|
390
|
-
var LONG_US_PATTERN = new RegExp(
|
|
391
|
-
`\\b${MONTH_NAMES}\\s+(0?[1-9]|[12]\\d|3[01]),?\\s+(19|20)\\d{2}\\b`,
|
|
392
|
-
"g"
|
|
393
|
-
);
|
|
394
|
-
var LONG_EU_PATTERN = new RegExp(
|
|
395
|
-
`\\b(0?[1-9]|[12]\\d|3[01])\\s+${MONTH_NAMES}\\s+(19|20)\\d{2}\\b`,
|
|
396
|
-
"g"
|
|
397
|
-
);
|
|
398
|
-
function collect(text, source, confidence) {
|
|
399
|
-
const matches = [];
|
|
400
|
-
const pattern = new RegExp(source, "g");
|
|
401
|
-
let match;
|
|
402
|
-
while ((match = pattern.exec(text)) !== null) {
|
|
403
|
-
matches.push({
|
|
404
|
-
category: "date-of-birth",
|
|
405
|
-
value: match[0],
|
|
406
|
-
start: match.index,
|
|
407
|
-
end: match.index + match[0].length,
|
|
408
|
-
confidence
|
|
409
|
-
});
|
|
410
|
-
}
|
|
411
|
-
return matches;
|
|
412
|
-
}
|
|
413
|
-
function detectDateOfBirth(text) {
|
|
414
|
-
const seen = /* @__PURE__ */ new Set();
|
|
415
|
-
const all = [];
|
|
416
|
-
const candidates = [
|
|
417
|
-
...collect(text, ISO_PATTERN.source, 0.85),
|
|
418
|
-
...collect(text, US_PATTERN.source, 0.8),
|
|
419
|
-
...collect(text, EU_PATTERN.source, 0.78),
|
|
420
|
-
...collect(text, LONG_US_PATTERN.source, 0.88),
|
|
421
|
-
...collect(text, LONG_EU_PATTERN.source, 0.88)
|
|
422
|
-
];
|
|
423
|
-
const sorted = [...candidates].sort((a, b) => b.confidence - a.confidence);
|
|
424
|
-
for (const match of sorted) {
|
|
425
|
-
let overlaps = false;
|
|
426
|
-
for (const pos of seen) {
|
|
427
|
-
if (match.start < pos) {
|
|
428
|
-
overlaps = true;
|
|
429
|
-
break;
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
if (!overlaps) {
|
|
433
|
-
for (let i = match.start; i < match.end; i++) seen.add(i);
|
|
434
|
-
all.push(match);
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
return all.sort((a, b) => a.start - b.start);
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
// src/detectors/name.ts
|
|
441
|
-
var NAME_GREETING_PATTERN = /(?:^|\.\s+)(?:dear|hi|hello|greetings|hey there|hey)\s+([A-Z][a-z]+(?:\s+[A-Z][a-z]+)+)/gi;
|
|
442
|
-
var NAME_TITLE_PATTERN = /\b(?:Mr|Mrs|Ms|Miss|Mx|Dr|Prof(?:essor)?|Rev(?:erend)?|Hon(?:orable)?|Capt(?:ain)?|Cmdr|Commander|Lt|Lieu?t(?:enant)?|Sgt|Sergeant|Fr|Father|Sr|Sister|Br|Brother|Esq|Esquire)\.?\s+([A-Z][a-z]+(?:\s+[A-Z][a-z]+)*)/g;
|
|
443
|
-
var NAME_CONTEXT_KEYWORDS = "patient|client|defendant|plaintiff|applicant|appellant|respondent|petitioner|claimant|employee|employer|teacher|student|author|by|from|to|signed by|authored by|submitted by|prepared by|filed by|reviewed by|approved by|written by|sent by|received by|referring to|regarding|re:|concerning|subject:|for|name:|full name:|contact:";
|
|
444
|
-
var NAME_CONTEXT_PATTERN = new RegExp(
|
|
445
|
-
`\\b(?:${NAME_CONTEXT_KEYWORDS})\\s+([A-Z][a-z]+(?:\\s+[A-Z][a-z]+)+)`,
|
|
446
|
-
"g"
|
|
447
|
-
);
|
|
448
|
-
var NAME_AGGRESSIVE_PATTERN = new RegExp(
|
|
449
|
-
// Greeting context
|
|
450
|
-
`(?:(?:^|\\.[\\s]+)(?:dear|hi|hello|greetings|hey there|hey)\\s+([A-Z][a-z]+(?:\\s+[A-Z][a-z]+)*))|(?:\\b(?:Mr|Mrs|Ms|Miss|Mx|Dr|Prof(?:essor)?|Rev(?:erend)?|Hon(?:orable)?|Capt(?:ain)?|Cmdr|Commander|Lt|Lieu?t(?:enant)?|Sgt|Sergeant|Fr|Father|Sr|Sister|Br|Brother|Esq|Esquire)\\.?\\s+([A-Z][a-z]+(?:\\s+[A-Z][a-z]+)*))|(?:\\b(?:${NAME_CONTEXT_KEYWORDS.replace(/[-()\]{}*+?.,\\^$|#\s[]/g, "\\$&")})\\s+([A-Z][a-z]+(?:\\s+[A-Z][a-z]+)+))`,
|
|
451
|
-
"gi"
|
|
452
|
-
);
|
|
453
|
-
function extractNameMatches(text, pattern, confidence) {
|
|
454
|
-
const matches = [];
|
|
455
|
-
const flags = pattern.flags.includes("g") ? pattern.flags : pattern.flags + "g";
|
|
456
|
-
const re = new RegExp(pattern.source, flags);
|
|
457
|
-
let m;
|
|
458
|
-
while ((m = re.exec(text)) !== null) {
|
|
459
|
-
const rawCapture = (m[1] ?? m[2] ?? m[3] ?? "").trim();
|
|
460
|
-
if (!rawCapture) continue;
|
|
461
|
-
const captureStart = text.indexOf(rawCapture, m.index);
|
|
462
|
-
if (captureStart === -1) continue;
|
|
463
|
-
const firstChar = text[captureStart];
|
|
464
|
-
if (!firstChar || !/[A-Z]/.test(firstChar)) continue;
|
|
465
|
-
const nameMatch = /^(?:[A-Z][a-z]+)(?:\s+[A-Z][a-z]+)*/.exec(text.slice(captureStart));
|
|
466
|
-
if (!nameMatch) continue;
|
|
467
|
-
const name = nameMatch[0];
|
|
468
|
-
matches.push({
|
|
469
|
-
category: "name",
|
|
470
|
-
value: name,
|
|
471
|
-
start: captureStart,
|
|
472
|
-
end: captureStart + name.length,
|
|
473
|
-
confidence
|
|
474
|
-
});
|
|
475
|
-
}
|
|
476
|
-
return matches;
|
|
477
|
-
}
|
|
478
|
-
function deduplicateNameMatches(matches) {
|
|
479
|
-
const sorted = [...matches].sort((a, b) => a.start - b.start || b.confidence - a.confidence);
|
|
480
|
-
const result = [];
|
|
481
|
-
let cursor = 0;
|
|
482
|
-
for (const m of sorted) {
|
|
483
|
-
if (m.start >= cursor) {
|
|
484
|
-
result.push(m);
|
|
485
|
-
cursor = m.end;
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
return result;
|
|
489
|
-
}
|
|
490
|
-
function detectName(text) {
|
|
491
|
-
const greetingMatches = extractNameMatches(text, NAME_GREETING_PATTERN, 0.75);
|
|
492
|
-
const titleMatches = extractNameMatches(text, NAME_TITLE_PATTERN, 0.78);
|
|
493
|
-
const contextMatches = extractNameMatches(text, NAME_CONTEXT_PATTERN, 0.7);
|
|
494
|
-
return deduplicateNameMatches([...greetingMatches, ...titleMatches, ...contextMatches]);
|
|
495
|
-
}
|
|
496
|
-
function detectNameAggressive(text) {
|
|
497
|
-
return deduplicateNameMatches(extractNameMatches(text, NAME_AGGRESSIVE_PATTERN, 0.65));
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
// src/detectors/address.ts
|
|
501
|
-
var US_STREET_PATTERN = /\b\d{1,5}\s+(?:(?:N|S|E|W|NE|NW|SE|SW|North|South|East|West|Northeast|Northwest|Southeast|Southwest)\.?\s+)?(?:[A-Z][a-z]+\s+){1,4}(?:St(?:reet)?|Ave(?:nue)?|Blvd|Boulevard|Dr(?:ive)?|Rd|Road|Ln|Lane|Ct|Court|Pl|Place|Cir|Circle|Way|Pkwy|Parkway|Hwy|Highway|Trl|Trail|Terr(?:ace)?|Terr)\.?(?:\s+(?:Apt|Apartment|Suite|Ste|Unit|#)\s*\w+)?\b/gi;
|
|
502
|
-
var UK_POSTCODE_PATTERN = /\b[A-Z]{1,2}\d[A-Z\d]?\s*\d[A-Z]{2}\b/gi;
|
|
503
|
-
var PO_BOX_PATTERN = /\bP\.?O\.?\s+Box\s+\d+\b/gi;
|
|
504
|
-
var ADDRESS_CONTEXT_PATTERN = /(?:address(?:ed to)?|lives at|located at|shipped? to|mailing address|billing address|residence|resides at|home address|headquartered at|office address)\s*:?\s*([^\n,]{10,80})/gi;
|
|
505
|
-
function detectAddress(text) {
|
|
506
|
-
const matches = [];
|
|
507
|
-
const seen = /* @__PURE__ */ new Set();
|
|
508
|
-
function push(value, start, end, confidence) {
|
|
509
|
-
const key = `${String(start)}-${String(end)}`;
|
|
510
|
-
if (seen.has(key)) return;
|
|
511
|
-
seen.add(key);
|
|
512
|
-
matches.push({ category: "address", value, start, end, confidence });
|
|
513
|
-
}
|
|
514
|
-
const usPattern = new RegExp(US_STREET_PATTERN.source, "gi");
|
|
515
|
-
let m;
|
|
516
|
-
while ((m = usPattern.exec(text)) !== null) {
|
|
517
|
-
push(m[0], m.index, m.index + m[0].length, 0.8);
|
|
518
|
-
}
|
|
519
|
-
const ukPattern = new RegExp(UK_POSTCODE_PATTERN.source, "gi");
|
|
520
|
-
while ((m = ukPattern.exec(text)) !== null) {
|
|
521
|
-
push(m[0], m.index, m.index + m[0].length, 0.78);
|
|
522
|
-
}
|
|
523
|
-
const poPattern = new RegExp(PO_BOX_PATTERN.source, "gi");
|
|
524
|
-
while ((m = poPattern.exec(text)) !== null) {
|
|
525
|
-
push(m[0], m.index, m.index + m[0].length, 0.85);
|
|
526
|
-
}
|
|
527
|
-
const ctxPattern = new RegExp(ADDRESS_CONTEXT_PATTERN.source, "gi");
|
|
528
|
-
while ((m = ctxPattern.exec(text)) !== null) {
|
|
529
|
-
const captured = (m[1] ?? m[0]).trim();
|
|
530
|
-
const start = text.indexOf(captured, m.index);
|
|
531
|
-
if (start !== -1) {
|
|
532
|
-
push(captured, start, start + captured.length, 0.82);
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
return matches;
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
// src/detectors/passport.ts
|
|
539
|
-
var PASSPORT_PATTERNS = [
|
|
540
|
-
// US: 1 letter + 8 digits
|
|
541
|
-
{ pattern: /\b[A-Z]\d{8}\b/g, confidence: 0.75 },
|
|
542
|
-
// Canada: 2 letters + 6 digits
|
|
543
|
-
{ pattern: /\b[A-Z]{2}\d{6}\b/g, confidence: 0.75 },
|
|
544
|
-
// Australia: 1 letter + 7 digits
|
|
545
|
-
{ pattern: /\b[A-Z]\d{7}\b/g, confidence: 0.73 },
|
|
546
|
-
// UK: 9 digits (no letters)
|
|
547
|
-
{ pattern: /\b\d{9}\b/g, confidence: 0.6 },
|
|
548
|
-
// lower confidence — many false positives
|
|
549
|
-
// EU generic: 1-2 letters + 5-8 digits
|
|
550
|
-
{ pattern: /\b[A-Z]{1,2}\d{5,8}\b/g, confidence: 0.7 }
|
|
551
|
-
];
|
|
552
|
-
var PASSPORT_CONTEXT_RE = /\b(?:passport(?:\s+(?:number|no\.?|#))?)\s*:?\s*/gi;
|
|
553
|
-
function detectPassport(text) {
|
|
554
|
-
const matches = [];
|
|
555
|
-
const contextPositions = [];
|
|
556
|
-
const ctxRe = new RegExp(PASSPORT_CONTEXT_RE.source, "gi");
|
|
557
|
-
let cm;
|
|
558
|
-
while ((cm = ctxRe.exec(text)) !== null) {
|
|
559
|
-
contextPositions.push(cm.index + cm[0].length);
|
|
560
|
-
}
|
|
561
|
-
const seen = /* @__PURE__ */ new Set();
|
|
562
|
-
for (const { pattern, confidence } of PASSPORT_PATTERNS) {
|
|
563
|
-
const re = new RegExp(pattern.source, "g");
|
|
564
|
-
let m;
|
|
565
|
-
while ((m = re.exec(text)) !== null) {
|
|
566
|
-
const match = m;
|
|
567
|
-
const key = `${String(match.index)}-${String(match.index + match[0].length)}`;
|
|
568
|
-
if (seen.has(key)) continue;
|
|
569
|
-
const hasContext = contextPositions.some(
|
|
570
|
-
(pos) => pos <= match.index && match.index - pos <= 30
|
|
571
|
-
);
|
|
572
|
-
const adjustedConfidence = hasContext ? Math.min(confidence + 0.15, 0.95) : confidence;
|
|
573
|
-
if (adjustedConfidence < 0.7 && !hasContext) continue;
|
|
574
|
-
seen.add(key);
|
|
575
|
-
matches.push({
|
|
576
|
-
category: "passport",
|
|
577
|
-
value: match[0],
|
|
578
|
-
start: match.index,
|
|
579
|
-
end: match.index + match[0].length,
|
|
580
|
-
confidence: adjustedConfidence
|
|
581
|
-
});
|
|
582
|
-
}
|
|
583
|
-
}
|
|
584
|
-
return matches;
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
// src/detectors/drivers-license.ts
|
|
588
|
-
var DL_CONTEXT_PATTERN = /\b(?:driver(?:'?s)?\s+licen[cs]e|driving\s+licen[cs]e|DL\s*#?|D\.?L\.?(?:\s*#)?|licen[cs]e\s+(?:no|number|#))\s*:?\s*/gi;
|
|
589
|
-
var UK_DVLA_PATTERN = /\b[A-Z9]{5}\d{6}[02-9]\d[A-Z]{2}\w{2}\b/g;
|
|
590
|
-
var GENERIC_DL_PATTERN = /\b[A-Z0-9]{5,15}\b/g;
|
|
591
|
-
function detectDriversLicense(text) {
|
|
592
|
-
const matches = [];
|
|
593
|
-
const seen = /* @__PURE__ */ new Set();
|
|
594
|
-
const contextPositions = [];
|
|
595
|
-
const ctxRe = new RegExp(DL_CONTEXT_PATTERN.source, "gi");
|
|
596
|
-
let cm;
|
|
597
|
-
while ((cm = ctxRe.exec(text)) !== null) {
|
|
598
|
-
contextPositions.push(cm.index + cm[0].length);
|
|
599
|
-
}
|
|
600
|
-
function push(value, start, end, confidence) {
|
|
601
|
-
const key = `${String(start)}-${String(end)}`;
|
|
602
|
-
if (seen.has(key)) return;
|
|
603
|
-
seen.add(key);
|
|
604
|
-
matches.push({ category: "drivers-license", value, start, end, confidence });
|
|
605
|
-
}
|
|
606
|
-
const ukRe = new RegExp(UK_DVLA_PATTERN.source, "g");
|
|
607
|
-
let m;
|
|
608
|
-
while ((m = ukRe.exec(text)) !== null) {
|
|
609
|
-
push(m[0], m.index, m.index + m[0].length, 0.78);
|
|
610
|
-
}
|
|
611
|
-
const genericRe = new RegExp(GENERIC_DL_PATTERN.source, "g");
|
|
612
|
-
while ((m = genericRe.exec(text)) !== null) {
|
|
613
|
-
const match = m;
|
|
614
|
-
const hasContext = contextPositions.some(
|
|
615
|
-
(pos) => pos <= match.index && match.index - pos <= 20
|
|
616
|
-
);
|
|
617
|
-
if (hasContext) {
|
|
618
|
-
push(match[0], match.index, match.index + match[0].length, 0.85);
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
return matches;
|
|
622
|
-
}
|
|
623
|
-
|
|
624
|
-
// src/detectors/national-id.ts
|
|
625
|
-
var CA_SIN_PATTERN = /\b\d{3}[\s-]\d{3}[\s-]\d{3}\b/g;
|
|
626
|
-
var MX_CURP_PATTERN = /\b[A-Z]{4}\d{6}[HM][A-Z]{2}[A-Z]{3}[A-Z0-9]\d\b/g;
|
|
627
|
-
var MX_RFC_PATTERN = /\b[A-ZÑ&]{3,4}\d{6}[A-Z0-9]{3}\b/g;
|
|
628
|
-
var UK_NINO_PATTERN = /\b[A-CEGHJ-PR-TW-Z]{2}[\s-]?\d{2}[\s-]?\d{2}[\s-]?\d{2}[\s-]?[A-D]\b/gi;
|
|
629
|
-
var IN_AADHAAR_PATTERN = /\b[2-9]\d{3}[\s-]?\d{4}[\s-]?\d{4}\b/g;
|
|
630
|
-
var CN_RESIDENT_ID_PATTERN = /\b\d{17}[\dX]\b/gi;
|
|
631
|
-
var KR_RRN_PATTERN = /\b\d{6}[\s-]\d{7}\b/g;
|
|
632
|
-
var DE_PERSONALAUSWEIS_PATTERN = /\b[A-Z0-9]{9}\b/g;
|
|
633
|
-
var FR_NIR_PATTERN = /\b[12]\s?\d{2}\s?\d{2}\s?\d{2}\s?\d{3}\s?\d{3}\s?\d{2}\b/g;
|
|
634
|
-
var BR_CPF_PATTERN = /\b\d{3}\.\d{3}\.\d{3}[-\s]\d{2}\b/g;
|
|
635
|
-
var AR_DNI_PATTERN = /\b\d{2,3}[.\s]?\d{3}[.\s]?\d{3}\b/g;
|
|
636
|
-
var ZA_ID_PATTERN = /\b\d{13}\b/g;
|
|
637
|
-
var NG_NIN_PATTERN = /\b\d{11}\b/g;
|
|
638
|
-
var AU_TFN_PATTERN = /\b\d{3}[\s-]?\d{3}[\s-]?\d{3}\b/g;
|
|
639
|
-
var SE_PERSONNUMMER_PATTERN = /\b\d{6}[-+]\d{4}\b/g;
|
|
640
|
-
var ES_DNIE_PATTERN = /\b(?:\d{8}[A-Z]|[XYZ]\d{7}[A-Z])\b/gi;
|
|
641
|
-
var IT_CF_PATTERN = /\b[A-Z]{6}\d{2}[A-EHLMPRST]\d{2}[A-Z]\d{3}[A-Z]\b/gi;
|
|
642
|
-
var PT_NIF_PATTERN = /\b[1-9]\d{8}\b/g;
|
|
643
|
-
var NL_BSN_PATTERN = /\b\d{9}\b/g;
|
|
644
|
-
var NATIONAL_ID_CONTEXT_RE = /\b(?:national\s+(?:id|identification|identity)\s*(?:number|no\.?|card|#)?|id\s+(?:number|no\.?|#)|citizen(?:ship)?\s+(?:number|id)|personal\s+(?:id|identification)\s*(?:number|no\.?)?|identificat(?:ion|or))\s*:?\s*/gi;
|
|
645
|
-
var NATIONAL_ID_ENTRIES = [
|
|
646
|
-
{ pattern: CA_SIN_PATTERN, confidence: 0.85, requiresContext: false },
|
|
647
|
-
{ pattern: MX_CURP_PATTERN, confidence: 0.9, requiresContext: false },
|
|
648
|
-
{ pattern: MX_RFC_PATTERN, confidence: 0.78, requiresContext: true },
|
|
649
|
-
{ pattern: UK_NINO_PATTERN, confidence: 0.9, requiresContext: false },
|
|
650
|
-
{ pattern: IN_AADHAAR_PATTERN, confidence: 0.82, requiresContext: false },
|
|
651
|
-
{ pattern: CN_RESIDENT_ID_PATTERN, confidence: 0.8, requiresContext: true },
|
|
652
|
-
{ pattern: KR_RRN_PATTERN, confidence: 0.87, requiresContext: false },
|
|
653
|
-
{ pattern: BR_CPF_PATTERN, confidence: 0.92, requiresContext: false },
|
|
654
|
-
{ pattern: SE_PERSONNUMMER_PATTERN, confidence: 0.87, requiresContext: false },
|
|
655
|
-
{ pattern: ES_DNIE_PATTERN, confidence: 0.82, requiresContext: false },
|
|
656
|
-
{ pattern: IT_CF_PATTERN, confidence: 0.88, requiresContext: false },
|
|
657
|
-
{ pattern: FR_NIR_PATTERN, confidence: 0.84, requiresContext: false },
|
|
658
|
-
// More ambiguous — require context
|
|
659
|
-
{ pattern: AR_DNI_PATTERN, confidence: 0.75, requiresContext: true },
|
|
660
|
-
{ pattern: ZA_ID_PATTERN, confidence: 0.72, requiresContext: true },
|
|
661
|
-
{ pattern: NG_NIN_PATTERN, confidence: 0.7, requiresContext: true },
|
|
662
|
-
{ pattern: AU_TFN_PATTERN, confidence: 0.75, requiresContext: true },
|
|
663
|
-
{ pattern: DE_PERSONALAUSWEIS_PATTERN, confidence: 0.75, requiresContext: true },
|
|
664
|
-
{ pattern: PT_NIF_PATTERN, confidence: 0.7, requiresContext: true },
|
|
665
|
-
{ pattern: NL_BSN_PATTERN, confidence: 0.7, requiresContext: true }
|
|
666
|
-
];
|
|
667
|
-
function detectNationalId(text) {
|
|
668
|
-
const matches = [];
|
|
669
|
-
const seen = /* @__PURE__ */ new Set();
|
|
670
|
-
const contextPositions = [];
|
|
671
|
-
const ctxRe = new RegExp(NATIONAL_ID_CONTEXT_RE.source, "gi");
|
|
672
|
-
let cm;
|
|
673
|
-
while ((cm = ctxRe.exec(text)) !== null) {
|
|
674
|
-
contextPositions.push(cm.index + cm[0].length);
|
|
675
|
-
}
|
|
676
|
-
for (const entry of NATIONAL_ID_ENTRIES) {
|
|
677
|
-
const re = new RegExp(entry.pattern.source, "g");
|
|
678
|
-
let m;
|
|
679
|
-
while ((m = re.exec(text)) !== null) {
|
|
680
|
-
const match = m;
|
|
681
|
-
const key = `${String(match.index)}-${String(match.index + match[0].length)}`;
|
|
682
|
-
if (seen.has(key)) continue;
|
|
683
|
-
const hasContext = contextPositions.some(
|
|
684
|
-
(pos) => pos <= match.index && match.index - pos <= 40
|
|
685
|
-
);
|
|
686
|
-
if (entry.requiresContext && !hasContext) continue;
|
|
687
|
-
const confidence = hasContext ? Math.min(entry.confidence + 0.05, 0.97) : entry.confidence;
|
|
688
|
-
seen.add(key);
|
|
689
|
-
matches.push({
|
|
690
|
-
category: "national-id",
|
|
691
|
-
value: match[0],
|
|
692
|
-
start: match.index,
|
|
693
|
-
end: match.index + match[0].length,
|
|
694
|
-
confidence
|
|
695
|
-
});
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
return matches;
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
// src/detectors/bank-account.ts
|
|
702
|
-
var BANK_CONTEXT_RE = /\b(?:routing\s+(?:number|no\.?|#)|sort\s+code|account\s+(?:number|no\.?|#)|bank\s+account|IBAN|BIC|SWIFT|ABA)\s*:?\s*/gi;
|
|
703
|
-
var US_ABA_ROUTING_PATTERN = /\b(?:0[1-9]|1[0-2]|2[1-9]|3[0-2]|6[1-9]|7[0-2]|80)\d{7}\b/g;
|
|
704
|
-
var UK_SORT_CODE_PATTERN = /\b\d{2}-\d{2}-\d{2}\b/g;
|
|
705
|
-
var SWIFT_BIC_PATTERN = /\b[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}(?:[A-Z0-9]{3})?\b/g;
|
|
706
|
-
var GENERIC_ACCOUNT_PATTERN = /\b\d{7,18}\b/g;
|
|
707
|
-
function detectBankAccount(text) {
|
|
708
|
-
const matches = [];
|
|
709
|
-
const seen = /* @__PURE__ */ new Set();
|
|
710
|
-
const contextPositions = [];
|
|
711
|
-
const ctxRe = new RegExp(BANK_CONTEXT_RE.source, "gi");
|
|
712
|
-
let cm;
|
|
713
|
-
while ((cm = ctxRe.exec(text)) !== null) {
|
|
714
|
-
contextPositions.push(cm.index + cm[0].length);
|
|
715
|
-
}
|
|
716
|
-
function hasContext(index) {
|
|
717
|
-
return contextPositions.some((pos) => pos <= index && index - pos <= 30);
|
|
718
|
-
}
|
|
719
|
-
function push(value, start, end, confidence) {
|
|
720
|
-
const key = `${String(start)}-${String(end)}`;
|
|
721
|
-
if (seen.has(key)) return;
|
|
722
|
-
seen.add(key);
|
|
723
|
-
matches.push({ category: "bank-account", value, start, end, confidence });
|
|
724
|
-
}
|
|
725
|
-
let m;
|
|
726
|
-
const abaRe = new RegExp(US_ABA_ROUTING_PATTERN.source, "g");
|
|
727
|
-
while ((m = abaRe.exec(text)) !== null) {
|
|
728
|
-
if (hasContext(m.index)) push(m[0], m.index, m.index + m[0].length, 0.85);
|
|
729
|
-
}
|
|
730
|
-
const scRe = new RegExp(UK_SORT_CODE_PATTERN.source, "g");
|
|
731
|
-
while ((m = scRe.exec(text)) !== null) {
|
|
732
|
-
push(m[0], m.index, m.index + m[0].length, 0.8);
|
|
733
|
-
}
|
|
734
|
-
const swiftRe = new RegExp(SWIFT_BIC_PATTERN.source, "g");
|
|
735
|
-
while ((m = swiftRe.exec(text)) !== null) {
|
|
736
|
-
const countryCode = m[0].slice(4, 6);
|
|
737
|
-
if (/^[A-Z]{2}$/.test(countryCode)) {
|
|
738
|
-
push(m[0], m.index, m.index + m[0].length, 0.82);
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
const genericRe = new RegExp(GENERIC_ACCOUNT_PATTERN.source, "g");
|
|
742
|
-
while ((m = genericRe.exec(text)) !== null) {
|
|
743
|
-
if (hasContext(m.index)) push(m[0], m.index, m.index + m[0].length, 0.8);
|
|
744
|
-
}
|
|
745
|
-
return matches;
|
|
746
|
-
}
|
|
747
|
-
|
|
748
|
-
// src/detectors/cryptocurrency.ts
|
|
749
|
-
var BTC_LEGACY_PATTERN = /\b1[a-km-zA-HJ-NP-Z1-9]{25,34}\b/g;
|
|
750
|
-
var BTC_P2SH_PATTERN = /\b3[a-km-zA-HJ-NP-Z1-9]{25,34}\b/g;
|
|
751
|
-
var BTC_BECH32_PATTERN = /\bbc1[a-z0-9]{39,59}\b/g;
|
|
752
|
-
var ETH_PATTERN = /\b0x[a-fA-F0-9]{40}\b/g;
|
|
753
|
-
var LTC_LEGACY_PATTERN = /\bL[a-km-zA-HJ-NP-Z1-9]{26,33}\b/g;
|
|
754
|
-
var LTC_M_PATTERN = /\bM[a-km-zA-HJ-NP-Z1-9]{26,33}\b/g;
|
|
755
|
-
var XMR_PATTERN = /\b4[0-9AB][1-9A-HJ-NP-Za-km-z]{93}\b/g;
|
|
756
|
-
var XRP_PATTERN = /\br[a-km-zA-HJ-NP-Z1-9]{24,33}\b/g;
|
|
757
|
-
var SOL_PATTERN = /\b[1-9A-HJ-NP-Za-km-z]{43,44}\b/g;
|
|
758
|
-
var ADA_PATTERN = /\baddr1[a-z0-9]{58,98}\b/g;
|
|
759
|
-
var CRYPTO_ENTRIES = [
|
|
760
|
-
{ pattern: ETH_PATTERN, confidence: 0.97, requiresContext: false },
|
|
761
|
-
{ pattern: BTC_BECH32_PATTERN, confidence: 0.95, requiresContext: false },
|
|
762
|
-
{ pattern: ADA_PATTERN, confidence: 0.92, requiresContext: false },
|
|
763
|
-
{ pattern: BTC_LEGACY_PATTERN, confidence: 0.88, requiresContext: false },
|
|
764
|
-
{ pattern: BTC_P2SH_PATTERN, confidence: 0.85, requiresContext: false },
|
|
765
|
-
{ pattern: LTC_LEGACY_PATTERN, confidence: 0.83, requiresContext: false },
|
|
766
|
-
{ pattern: LTC_M_PATTERN, confidence: 0.8, requiresContext: false },
|
|
767
|
-
{ pattern: XMR_PATTERN, confidence: 0.9, requiresContext: false },
|
|
768
|
-
{ pattern: XRP_PATTERN, confidence: 0.78, requiresContext: true },
|
|
769
|
-
{ pattern: SOL_PATTERN, confidence: 0.72, requiresContext: true }
|
|
770
|
-
];
|
|
771
|
-
var CRYPTO_CONTEXT_RE = /\b(?:wallet(?:\s+address)?|crypto(?:currency)?(?:\s+address)?|bitcoin|btc|ethereum|eth|litecoin|ltc|monero|xmr|ripple|xrp|solana|sol|cardano|ada|address)\s*:?\s*/gi;
|
|
772
|
-
function detectCryptocurrency(text) {
|
|
773
|
-
const matches = [];
|
|
774
|
-
const seen = /* @__PURE__ */ new Set();
|
|
775
|
-
const contextPositions = [];
|
|
776
|
-
const ctxRe = new RegExp(CRYPTO_CONTEXT_RE.source, "gi");
|
|
777
|
-
let cm;
|
|
778
|
-
while ((cm = ctxRe.exec(text)) !== null) {
|
|
779
|
-
contextPositions.push(cm.index + cm[0].length);
|
|
780
|
-
}
|
|
781
|
-
function hasContext(index) {
|
|
782
|
-
return contextPositions.some((pos) => pos <= index && index - pos <= 40);
|
|
783
|
-
}
|
|
784
|
-
for (const entry of CRYPTO_ENTRIES) {
|
|
785
|
-
const re = new RegExp(entry.pattern.source, entry.pattern.flags.includes("i") ? "gi" : "g");
|
|
786
|
-
let m;
|
|
787
|
-
while ((m = re.exec(text)) !== null) {
|
|
788
|
-
if (entry.requiresContext && !hasContext(m.index)) continue;
|
|
789
|
-
const key = `${String(m.index)}-${String(m.index + m[0].length)}`;
|
|
790
|
-
if (seen.has(key)) continue;
|
|
791
|
-
seen.add(key);
|
|
792
|
-
const confidence = hasContext(m.index) ? Math.min(entry.confidence + 0.03, 0.99) : entry.confidence;
|
|
793
|
-
matches.push({
|
|
794
|
-
category: "cryptocurrency",
|
|
795
|
-
value: m[0],
|
|
796
|
-
start: m.index,
|
|
797
|
-
end: m.index + m[0].length,
|
|
798
|
-
confidence
|
|
799
|
-
});
|
|
800
|
-
}
|
|
801
|
-
}
|
|
802
|
-
return matches;
|
|
803
|
-
}
|
|
804
|
-
|
|
805
|
-
// src/detectors/tax-id.ts
|
|
806
|
-
var US_EIN_PATTERN = /\b\d{2}-\d{7}\b/g;
|
|
807
|
-
var US_ITIN_PATTERN = /\b9\d{2}[\s-](?:5[0-9]|6[0-5]|7[0-9]|8[0-8]|90|91|92|9[4-9])[\s-]\d{4}\b/g;
|
|
808
|
-
var UK_UTR_PATTERN = /\b\d{10}\b/g;
|
|
809
|
-
var EU_VAT_PATTERN = /\b(?:AT|BE|BG|CY|CZ|DE|DK|EE|EL|ES|FI|FR|GB|HR|HU|IE|IT|LT|LU|LV|MT|NL|PL|PT|RO|SE|SI|SK)[\s-]?[A-Z0-9]{8,12}\b/g;
|
|
810
|
-
var AU_ABN_PATTERN = /\b\d{2}[\s]?\d{3}[\s]?\d{3}[\s]?\d{3}\b/g;
|
|
811
|
-
var TAX_CONTEXT_RE = /\b(?:tax(?:\s+(?:id(?:entification)?|number|no\.?|#|reference))?|T\.?I\.?N\.?|EIN|ITIN|UTR|VAT(?:\s+(?:number|no\.?|reg(?:istration)?|#))?|ABN|TFN)\s*:?\s*/gi;
|
|
812
|
-
function detectTaxId(text) {
|
|
813
|
-
const matches = [];
|
|
814
|
-
const seen = /* @__PURE__ */ new Set();
|
|
815
|
-
const contextPositions = [];
|
|
816
|
-
const ctxRe = new RegExp(TAX_CONTEXT_RE.source, "gi");
|
|
817
|
-
let cm;
|
|
818
|
-
while ((cm = ctxRe.exec(text)) !== null) {
|
|
819
|
-
contextPositions.push(cm.index + cm[0].length);
|
|
820
|
-
}
|
|
821
|
-
function hasContext(index) {
|
|
822
|
-
return contextPositions.some((pos) => pos <= index && index - pos <= 30);
|
|
823
|
-
}
|
|
824
|
-
function push(value, start, end, confidence) {
|
|
825
|
-
const key = `${String(start)}-${String(end)}`;
|
|
826
|
-
if (seen.has(key)) return;
|
|
827
|
-
seen.add(key);
|
|
828
|
-
matches.push({ category: "tax-id", value, start, end, confidence });
|
|
829
|
-
}
|
|
830
|
-
let m;
|
|
831
|
-
const einRe = new RegExp(US_EIN_PATTERN.source, "g");
|
|
832
|
-
while ((m = einRe.exec(text)) !== null) {
|
|
833
|
-
push(m[0], m.index, m.index + m[0].length, hasContext(m.index) ? 0.92 : 0.8);
|
|
834
|
-
}
|
|
835
|
-
const itinRe = new RegExp(US_ITIN_PATTERN.source, "g");
|
|
836
|
-
while ((m = itinRe.exec(text)) !== null) {
|
|
837
|
-
push(m[0], m.index, m.index + m[0].length, 0.9);
|
|
838
|
-
}
|
|
839
|
-
const vatRe = new RegExp(EU_VAT_PATTERN.source, "g");
|
|
840
|
-
while ((m = vatRe.exec(text)) !== null) {
|
|
841
|
-
push(m[0], m.index, m.index + m[0].length, hasContext(m.index) ? 0.9 : 0.78);
|
|
842
|
-
}
|
|
843
|
-
const abnRe = new RegExp(AU_ABN_PATTERN.source, "g");
|
|
844
|
-
while ((m = abnRe.exec(text)) !== null) {
|
|
845
|
-
if (hasContext(m.index)) push(m[0], m.index, m.index + m[0].length, 0.82);
|
|
846
|
-
}
|
|
847
|
-
const utrRe = new RegExp(UK_UTR_PATTERN.source, "g");
|
|
848
|
-
while ((m = utrRe.exec(text)) !== null) {
|
|
849
|
-
if (hasContext(m.index)) push(m[0], m.index, m.index + m[0].length, 0.85);
|
|
850
|
-
}
|
|
851
|
-
return matches;
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
// src/validators.ts
|
|
855
|
-
function luhn2(digits) {
|
|
856
|
-
const stripped = digits.replace(/[\s-]/g, "");
|
|
857
|
-
if (!/^\d+$/.test(stripped) || stripped.length < 2) return false;
|
|
858
|
-
let sum = 0;
|
|
859
|
-
let isEven = false;
|
|
860
|
-
for (let i = stripped.length - 1; i >= 0; i--) {
|
|
861
|
-
let d = parseInt(stripped[i] ?? "0", 10);
|
|
862
|
-
if (isEven) {
|
|
863
|
-
d *= 2;
|
|
864
|
-
if (d > 9) d -= 9;
|
|
865
|
-
}
|
|
866
|
-
sum += d;
|
|
867
|
-
isEven = !isEven;
|
|
868
|
-
}
|
|
869
|
-
return sum % 10 === 0;
|
|
870
|
-
}
|
|
871
|
-
function nhsMod11(value) {
|
|
872
|
-
const stripped = value.replace(/[\s-]/g, "");
|
|
873
|
-
if (!/^\d{10}$/.test(stripped)) return false;
|
|
874
|
-
let sum = 0;
|
|
875
|
-
for (let i = 0; i < 9; i++) {
|
|
876
|
-
sum += parseInt(stripped[i] ?? "0", 10) * (10 - i);
|
|
877
|
-
}
|
|
878
|
-
const remainder = sum % 11;
|
|
879
|
-
if (remainder === 1) return false;
|
|
880
|
-
const checkDigit = remainder === 0 ? 0 : 11 - remainder;
|
|
881
|
-
return parseInt(stripped[9] ?? "0", 10) === checkDigit;
|
|
882
|
-
}
|
|
883
|
-
var VIN_TRANSLITERATION = {
|
|
884
|
-
A: 1,
|
|
885
|
-
B: 2,
|
|
886
|
-
C: 3,
|
|
887
|
-
D: 4,
|
|
888
|
-
E: 5,
|
|
889
|
-
F: 6,
|
|
890
|
-
G: 7,
|
|
891
|
-
H: 8,
|
|
892
|
-
J: 1,
|
|
893
|
-
K: 2,
|
|
894
|
-
L: 3,
|
|
895
|
-
M: 4,
|
|
896
|
-
N: 5,
|
|
897
|
-
P: 7,
|
|
898
|
-
R: 9,
|
|
899
|
-
S: 2,
|
|
900
|
-
T: 3,
|
|
901
|
-
U: 4,
|
|
902
|
-
V: 5,
|
|
903
|
-
W: 6,
|
|
904
|
-
X: 7,
|
|
905
|
-
Y: 8,
|
|
906
|
-
Z: 9,
|
|
907
|
-
0: 0,
|
|
908
|
-
1: 1,
|
|
909
|
-
2: 2,
|
|
910
|
-
3: 3,
|
|
911
|
-
4: 4,
|
|
912
|
-
5: 5,
|
|
913
|
-
6: 6,
|
|
914
|
-
7: 7,
|
|
915
|
-
8: 8,
|
|
916
|
-
9: 9
|
|
917
|
-
};
|
|
918
|
-
var VIN_WEIGHTS = [8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2];
|
|
919
|
-
function vinChecksum(vin) {
|
|
920
|
-
const upper = vin.toUpperCase();
|
|
921
|
-
if (!/^[A-HJ-NPR-Z0-9]{17}$/.test(upper)) return false;
|
|
922
|
-
let sum = 0;
|
|
923
|
-
for (let i = 0; i < 17; i++) {
|
|
924
|
-
const char = upper[i] ?? "";
|
|
925
|
-
const val = VIN_TRANSLITERATION[char];
|
|
926
|
-
const weight = VIN_WEIGHTS[i];
|
|
927
|
-
if (val === void 0 || weight === void 0) return false;
|
|
928
|
-
sum += val * weight;
|
|
929
|
-
}
|
|
930
|
-
const remainder = sum % 11;
|
|
931
|
-
const checkChar = upper[8] ?? "";
|
|
932
|
-
const expected = remainder === 10 ? "X" : String(remainder);
|
|
933
|
-
return checkChar === expected;
|
|
934
|
-
}
|
|
935
|
-
function deaChecksum(value) {
|
|
936
|
-
const stripped = value.replace(/[\s-]/g, "").toUpperCase();
|
|
937
|
-
if (!/^[ABCDEFGHJKLMNPRSTUX][A-Z9]\d{7}$/.test(stripped)) return false;
|
|
938
|
-
const digits = stripped.slice(2);
|
|
939
|
-
const oddSum = parseInt(digits[0] ?? "0", 10) + parseInt(digits[2] ?? "0", 10) + parseInt(digits[4] ?? "0", 10);
|
|
940
|
-
const evenSum = parseInt(digits[1] ?? "0", 10) + parseInt(digits[3] ?? "0", 10) + parseInt(digits[5] ?? "0", 10);
|
|
941
|
-
const checksum = (oddSum + 2 * evenSum) % 10;
|
|
942
|
-
return checksum === parseInt(digits[6] ?? "0", 10);
|
|
943
|
-
}
|
|
944
|
-
|
|
945
|
-
// src/detectors/medical-record.ts
|
|
946
|
-
var MRN_CONTEXT_RE = /\b(?:MRN|medical\s+record\s+(?:number|no\.?|#)|patient\s+(?:id|number|no\.?|#)|chart\s+(?:number|no\.?|#))\s*:?\s*/gi;
|
|
947
|
-
var US_NPI_PATTERN = /\b[12]\d{9}\b/g;
|
|
948
|
-
var DEA_PATTERN = /\b[ABCDEFGHJKLMNPRSTUX][A-Z9]\d{7}\b/gi;
|
|
949
|
-
var MRN_PATTERN = /\b[A-Z0-9]{6,12}\b/g;
|
|
950
|
-
function detectMedicalRecord(text) {
|
|
951
|
-
const matches = [];
|
|
952
|
-
const seen = /* @__PURE__ */ new Set();
|
|
953
|
-
const contextPositions = [];
|
|
954
|
-
const ctxRe = new RegExp(MRN_CONTEXT_RE.source, "gi");
|
|
955
|
-
let cm;
|
|
956
|
-
while ((cm = ctxRe.exec(text)) !== null) {
|
|
957
|
-
contextPositions.push(cm.index + cm[0].length);
|
|
958
|
-
}
|
|
959
|
-
function hasContextAt(index) {
|
|
960
|
-
return contextPositions.some((pos) => pos <= index && index - pos <= 30);
|
|
961
|
-
}
|
|
962
|
-
function push(value, start, end, confidence) {
|
|
963
|
-
const key = `${String(start)}-${String(end)}`;
|
|
964
|
-
if (seen.has(key)) return;
|
|
965
|
-
seen.add(key);
|
|
966
|
-
matches.push({ category: "medical-record", value, start, end, confidence });
|
|
967
|
-
}
|
|
968
|
-
let m;
|
|
969
|
-
const deaRe = new RegExp(DEA_PATTERN.source, "gi");
|
|
970
|
-
while ((m = deaRe.exec(text)) !== null) {
|
|
971
|
-
if (deaChecksum(m[0])) {
|
|
972
|
-
push(m[0], m.index, m.index + m[0].length, 0.9);
|
|
973
|
-
}
|
|
974
|
-
}
|
|
975
|
-
const npiRe = new RegExp(US_NPI_PATTERN.source, "g");
|
|
976
|
-
while ((m = npiRe.exec(text)) !== null) {
|
|
977
|
-
const withPrefix = "80840" + m[0];
|
|
978
|
-
if (luhn2(withPrefix)) {
|
|
979
|
-
push(m[0], m.index, m.index + m[0].length, hasContextAt(m.index) ? 0.88 : 0.75);
|
|
980
|
-
}
|
|
981
|
-
}
|
|
982
|
-
const mrnRe = new RegExp(MRN_PATTERN.source, "g");
|
|
983
|
-
while ((m = mrnRe.exec(text)) !== null) {
|
|
984
|
-
if (hasContextAt(m.index)) {
|
|
985
|
-
push(m[0], m.index, m.index + m[0].length, 0.85);
|
|
986
|
-
}
|
|
987
|
-
}
|
|
988
|
-
return matches;
|
|
989
|
-
}
|
|
990
|
-
|
|
991
|
-
// src/detectors/health-insurance.ts
|
|
992
|
-
var HI_CONTEXT_RE = /\b(?:health\s+insurance(?:\s+(?:number|id|no\.?|#|card))?|insurance\s+(?:number|id|no\.?|#|member\s+id)|member\s+(?:id|number|no\.?)|policy\s+(?:number|no\.?|#)|medicare|medicaid|NHS(?:\s+(?:number|no\.?|#))?|EHIC|beneficiary\s+(?:id|number))\s*:?\s*/gi;
|
|
993
|
-
var US_MBI_PATTERN = /\b[1-9][AC-HJ-NP-RT-Y][0-9][AC-HJ-NP-RT-Y][AC-HJ-NP-RT-Y0-9]\d[AC-HJ-NP-RT-Y][AC-HJ-NP-RT-Y0-9]{2}\d{2}\b/gi;
|
|
994
|
-
var UK_NHS_PATTERN = /\b\d{3}[\s-]\d{3}[\s-]\d{4}\b|\b\d{10}\b/g;
|
|
995
|
-
var EU_EHIC_PATTERN = /\b[A-Z]{2}\d{6,10}\b/g;
|
|
996
|
-
var GENERIC_MEMBER_ID = /\b[A-Z0-9]{8,14}\b/g;
|
|
997
|
-
function detectHealthInsurance(text) {
|
|
998
|
-
const matches = [];
|
|
999
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1000
|
-
const contextPositions = [];
|
|
1001
|
-
const ctxRe = new RegExp(HI_CONTEXT_RE.source, "gi");
|
|
1002
|
-
let cm;
|
|
1003
|
-
while ((cm = ctxRe.exec(text)) !== null) {
|
|
1004
|
-
contextPositions.push(cm.index + cm[0].length);
|
|
1005
|
-
}
|
|
1006
|
-
function hasContextAt(index) {
|
|
1007
|
-
return contextPositions.some((pos) => pos <= index && index - pos <= 40);
|
|
1008
|
-
}
|
|
1009
|
-
function push(value, start, end, confidence) {
|
|
1010
|
-
const key = `${String(start)}-${String(end)}`;
|
|
1011
|
-
if (seen.has(key)) return;
|
|
1012
|
-
seen.add(key);
|
|
1013
|
-
matches.push({ category: "health-insurance", value, start, end, confidence });
|
|
1014
|
-
}
|
|
1015
|
-
let m;
|
|
1016
|
-
const mbiRe = new RegExp(US_MBI_PATTERN.source, "gi");
|
|
1017
|
-
while ((m = mbiRe.exec(text)) !== null) {
|
|
1018
|
-
push(m[0], m.index, m.index + m[0].length, hasContextAt(m.index) ? 0.9 : 0.8);
|
|
1019
|
-
}
|
|
1020
|
-
const nhsRe = new RegExp(UK_NHS_PATTERN.source, "g");
|
|
1021
|
-
while ((m = nhsRe.exec(text)) !== null) {
|
|
1022
|
-
if (nhsMod11(m[0])) {
|
|
1023
|
-
push(m[0], m.index, m.index + m[0].length, hasContextAt(m.index) ? 0.92 : 0.82);
|
|
1024
|
-
}
|
|
1025
|
-
}
|
|
1026
|
-
const ehicRe = new RegExp(EU_EHIC_PATTERN.source, "g");
|
|
1027
|
-
while ((m = ehicRe.exec(text)) !== null) {
|
|
1028
|
-
if (hasContextAt(m.index)) {
|
|
1029
|
-
push(m[0], m.index, m.index + m[0].length, 0.78);
|
|
1030
|
-
}
|
|
1031
|
-
}
|
|
1032
|
-
const genericRe = new RegExp(GENERIC_MEMBER_ID.source, "g");
|
|
1033
|
-
while ((m = genericRe.exec(text)) !== null) {
|
|
1034
|
-
if (hasContextAt(m.index)) {
|
|
1035
|
-
push(m[0], m.index, m.index + m[0].length, 0.75);
|
|
1036
|
-
}
|
|
1037
|
-
}
|
|
1038
|
-
return matches;
|
|
1039
|
-
}
|
|
1040
|
-
|
|
1041
|
-
// src/detectors/prescription.ts
|
|
1042
|
-
var RX_CONTEXT_RE = /\b(?:Rx\s*(?:number|no\.?|#)?|prescription\s*(?:number|no\.?|#)?|prescription\s+id|NDC|drug\s+code|DEA\s*(?:number|no\.?|#)?)\s*:?\s*/gi;
|
|
1043
|
-
var DEA_PATTERN2 = /\b[ABCDEFGHJKLMNPRSTUX][A-Z9]\d{7}\b/gi;
|
|
1044
|
-
var NDC_PATTERN = /\b\d{4}-\d{4}-\d{2}\b|\b\d{5}-\d{3}-\d{2}\b|\b\d{5}-\d{4}-\d{1}\b|\b\d{5}-\d{4}-\d{2}\b/g;
|
|
1045
|
-
var RX_NUMBER_PATTERN = /\b\d{4,12}\b/g;
|
|
1046
|
-
function detectPrescription(text) {
|
|
1047
|
-
const matches = [];
|
|
1048
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1049
|
-
const contextPositions = [];
|
|
1050
|
-
const ctxRe = new RegExp(RX_CONTEXT_RE.source, "gi");
|
|
1051
|
-
let cm;
|
|
1052
|
-
while ((cm = ctxRe.exec(text)) !== null) {
|
|
1053
|
-
contextPositions.push(cm.index + cm[0].length);
|
|
1054
|
-
}
|
|
1055
|
-
function hasContextAt(index) {
|
|
1056
|
-
return contextPositions.some((pos) => pos <= index && index - pos <= 30);
|
|
1057
|
-
}
|
|
1058
|
-
function push(value, start, end, confidence) {
|
|
1059
|
-
const key = `${String(start)}-${String(end)}`;
|
|
1060
|
-
if (seen.has(key)) return;
|
|
1061
|
-
seen.add(key);
|
|
1062
|
-
matches.push({ category: "prescription", value, start, end, confidence });
|
|
1063
|
-
}
|
|
1064
|
-
let m;
|
|
1065
|
-
const deaRe = new RegExp(DEA_PATTERN2.source, "gi");
|
|
1066
|
-
while ((m = deaRe.exec(text)) !== null) {
|
|
1067
|
-
if (deaChecksum(m[0])) {
|
|
1068
|
-
push(m[0], m.index, m.index + m[0].length, 0.9);
|
|
1069
|
-
}
|
|
1070
|
-
}
|
|
1071
|
-
const ndcRe = new RegExp(NDC_PATTERN.source, "g");
|
|
1072
|
-
while ((m = ndcRe.exec(text)) !== null) {
|
|
1073
|
-
push(m[0], m.index, m.index + m[0].length, hasContextAt(m.index) ? 0.9 : 0.78);
|
|
1074
|
-
}
|
|
1075
|
-
const rxRe = new RegExp(RX_NUMBER_PATTERN.source, "g");
|
|
1076
|
-
while ((m = rxRe.exec(text)) !== null) {
|
|
1077
|
-
if (hasContextAt(m.index)) {
|
|
1078
|
-
push(m[0], m.index, m.index + m[0].length, 0.82);
|
|
1079
|
-
}
|
|
1080
|
-
}
|
|
1081
|
-
return matches;
|
|
1082
|
-
}
|
|
1083
|
-
|
|
1084
|
-
// src/detectors/api-key.ts
|
|
1085
|
-
var API_KEY_ENTRIES = [
|
|
1086
|
-
// AWS Access Key ID
|
|
1087
|
-
{
|
|
1088
|
-
pattern: /\bAKIA[0-9A-Z]{16}\b/g,
|
|
1089
|
-
confidence: 0.99,
|
|
1090
|
-
requiresContext: false
|
|
1091
|
-
},
|
|
1092
|
-
// Stripe live secret key
|
|
1093
|
-
{
|
|
1094
|
-
pattern: /\bsk_live_[a-zA-Z0-9]{24,99}\b/g,
|
|
1095
|
-
confidence: 0.99,
|
|
1096
|
-
requiresContext: false
|
|
1097
|
-
},
|
|
1098
|
-
// Stripe live publishable key
|
|
1099
|
-
{
|
|
1100
|
-
pattern: /\bpk_live_[a-zA-Z0-9]{24,99}\b/g,
|
|
1101
|
-
confidence: 0.99,
|
|
1102
|
-
requiresContext: false
|
|
1103
|
-
},
|
|
1104
|
-
// Stripe test keys
|
|
1105
|
-
{
|
|
1106
|
-
pattern: /\b(?:sk|pk)_test_[a-zA-Z0-9]{24,99}\b/g,
|
|
1107
|
-
confidence: 0.97,
|
|
1108
|
-
requiresContext: false
|
|
1109
|
-
},
|
|
1110
|
-
// GitHub personal access tokens (classic)
|
|
1111
|
-
{
|
|
1112
|
-
pattern: /\bghp_[a-zA-Z0-9]{36}\b/g,
|
|
1113
|
-
confidence: 0.99,
|
|
1114
|
-
requiresContext: false
|
|
1115
|
-
},
|
|
1116
|
-
// GitHub OAuth tokens
|
|
1117
|
-
{
|
|
1118
|
-
pattern: /\bgho_[a-zA-Z0-9]{36}\b/g,
|
|
1119
|
-
confidence: 0.99,
|
|
1120
|
-
requiresContext: false
|
|
1121
|
-
},
|
|
1122
|
-
// GitHub server-to-server tokens
|
|
1123
|
-
{
|
|
1124
|
-
pattern: /\bghs_[a-zA-Z0-9]{36}\b/g,
|
|
1125
|
-
confidence: 0.99,
|
|
1126
|
-
requiresContext: false
|
|
1127
|
-
},
|
|
1128
|
-
// GitHub refresh tokens
|
|
1129
|
-
{
|
|
1130
|
-
pattern: /\bghr_[a-zA-Z0-9]{36,76}\b/g,
|
|
1131
|
-
confidence: 0.99,
|
|
1132
|
-
requiresContext: false
|
|
1133
|
-
},
|
|
1134
|
-
// GitHub fine-grained PAT
|
|
1135
|
-
{
|
|
1136
|
-
pattern: /\bgithub_pat_[a-zA-Z0-9_]{59,}\b/g,
|
|
1137
|
-
confidence: 0.99,
|
|
1138
|
-
requiresContext: false
|
|
1139
|
-
},
|
|
1140
|
-
// Slack bot tokens
|
|
1141
|
-
{
|
|
1142
|
-
pattern: /\bxoxb-[0-9A-Za-z-]{40,}\b/g,
|
|
1143
|
-
confidence: 0.98,
|
|
1144
|
-
requiresContext: false
|
|
1145
|
-
},
|
|
1146
|
-
// Slack user tokens
|
|
1147
|
-
{
|
|
1148
|
-
pattern: /\bxoxp-[0-9A-Za-z-]{40,}\b/g,
|
|
1149
|
-
confidence: 0.98,
|
|
1150
|
-
requiresContext: false
|
|
1151
|
-
},
|
|
1152
|
-
// Slack app tokens
|
|
1153
|
-
{
|
|
1154
|
-
pattern: /\bxoxa-[0-9A-Za-z-]{40,}\b/g,
|
|
1155
|
-
confidence: 0.98,
|
|
1156
|
-
requiresContext: false
|
|
1157
|
-
},
|
|
1158
|
-
// Slack workspace tokens
|
|
1159
|
-
{
|
|
1160
|
-
pattern: /\bxoxs-[0-9A-Za-z-]{40,}\b/g,
|
|
1161
|
-
confidence: 0.96,
|
|
1162
|
-
requiresContext: false
|
|
1163
|
-
},
|
|
1164
|
-
// OpenAI API keys
|
|
1165
|
-
{
|
|
1166
|
-
pattern: /\bsk-[a-zA-Z0-9]{32,}\b/g,
|
|
1167
|
-
confidence: 0.9,
|
|
1168
|
-
requiresContext: false
|
|
1169
|
-
},
|
|
1170
|
-
// Google API keys
|
|
1171
|
-
{
|
|
1172
|
-
pattern: /\bAIza[0-9A-Za-z_-]{35}\b/g,
|
|
1173
|
-
confidence: 0.99,
|
|
1174
|
-
requiresContext: false
|
|
1175
|
-
},
|
|
1176
|
-
// JWT tokens (only the full 3-part form)
|
|
1177
|
-
{
|
|
1178
|
-
pattern: /\beyJ[a-zA-Z0-9_-]{10,}\.eyJ[a-zA-Z0-9_-]{10,}\.[a-zA-Z0-9_-]{20,}\b/g,
|
|
1179
|
-
confidence: 0.95,
|
|
1180
|
-
requiresContext: false
|
|
1181
|
-
}
|
|
1182
|
-
];
|
|
1183
|
-
var API_KEY_CONTEXT_RE = /\b(?:api[_-\s]?key|api[_-\s]?token|access[_-\s]?token|secret[_-\s]?key|auth(?:orization)?[_-\s]?(?:key|token)|bearer(?:\s+token)?|x-api-key|credential[s]?)\s*[:=]\s*/gi;
|
|
1184
|
-
var GENERIC_KEY_PATTERN = /\b[a-zA-Z0-9_.-]{20,80}\b/g;
|
|
1185
|
-
function detectApiKey(text) {
|
|
1186
|
-
const matches = [];
|
|
1187
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1188
|
-
const contextPositions = [];
|
|
1189
|
-
const ctxRe = new RegExp(API_KEY_CONTEXT_RE.source, "gi");
|
|
1190
|
-
let cm;
|
|
1191
|
-
while ((cm = ctxRe.exec(text)) !== null) {
|
|
1192
|
-
contextPositions.push(cm.index + cm[0].length);
|
|
1193
|
-
}
|
|
1194
|
-
function hasContextAt(index) {
|
|
1195
|
-
return contextPositions.some((pos) => pos <= index && index - pos <= 10);
|
|
1196
|
-
}
|
|
1197
|
-
function push(value, start, end, confidence) {
|
|
1198
|
-
const key = `${String(start)}-${String(end)}`;
|
|
1199
|
-
if (seen.has(key)) return;
|
|
1200
|
-
seen.add(key);
|
|
1201
|
-
matches.push({ category: "api-key", value, start, end, confidence });
|
|
1202
|
-
}
|
|
1203
|
-
for (const entry of API_KEY_ENTRIES) {
|
|
1204
|
-
const re = new RegExp(entry.pattern.source, "g");
|
|
1205
|
-
let m2;
|
|
1206
|
-
while ((m2 = re.exec(text)) !== null) {
|
|
1207
|
-
push(m2[0], m2.index, m2.index + m2[0].length, entry.confidence);
|
|
1208
|
-
}
|
|
1209
|
-
}
|
|
1210
|
-
const genericRe = new RegExp(GENERIC_KEY_PATTERN.source, "g");
|
|
1211
|
-
let m;
|
|
1212
|
-
while ((m = genericRe.exec(text)) !== null) {
|
|
1213
|
-
if (hasContextAt(m.index)) {
|
|
1214
|
-
push(m[0], m.index, m.index + m[0].length, 0.82);
|
|
1215
|
-
}
|
|
1216
|
-
}
|
|
1217
|
-
return matches;
|
|
1218
|
-
}
|
|
1219
|
-
|
|
1220
|
-
// src/detectors/social-media.ts
|
|
1221
|
-
var TWITTER_HANDLE_PATTERN = /(?<![a-zA-Z0-9])@[a-zA-Z0-9_]{1,15}(?![a-zA-Z0-9_@])/g;
|
|
1222
|
-
var DISCORD_ID_PATTERN = /\b\d{17,19}\b/g;
|
|
1223
|
-
var YT_CHANNEL_PATTERN = /\bUC[a-zA-Z0-9_-]{22}\b/g;
|
|
1224
|
-
var REDDIT_USER_PATTERN = /\b(?:u|\/u)\/[a-zA-Z0-9_-]{3,20}\b/g;
|
|
1225
|
-
var LINKEDIN_PATTERN = /\blinkedin\.com\/in\/[a-zA-Z0-9-]{3,100}\b/gi;
|
|
1226
|
-
var DISCORD_CONTEXT_RE = /\b(?:discord(?:\s+(?:id|user|server|guild))?|user\s+id|uid)\s*:?\s*/gi;
|
|
1227
|
-
function detectSocialMedia(text) {
|
|
1228
|
-
const matches = [];
|
|
1229
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1230
|
-
const discordContextPositions = [];
|
|
1231
|
-
const ctxRe = new RegExp(DISCORD_CONTEXT_RE.source, "gi");
|
|
1232
|
-
let cm;
|
|
1233
|
-
while ((cm = ctxRe.exec(text)) !== null) {
|
|
1234
|
-
discordContextPositions.push(cm.index + cm[0].length);
|
|
1235
|
-
}
|
|
1236
|
-
function push(value, start, end, confidence) {
|
|
1237
|
-
const key = `${String(start)}-${String(end)}`;
|
|
1238
|
-
if (seen.has(key)) return;
|
|
1239
|
-
seen.add(key);
|
|
1240
|
-
matches.push({ category: "social-media", value, start, end, confidence });
|
|
1241
|
-
}
|
|
1242
|
-
let m;
|
|
1243
|
-
const twitterRe = new RegExp(TWITTER_HANDLE_PATTERN.source, "g");
|
|
1244
|
-
while ((m = twitterRe.exec(text)) !== null) {
|
|
1245
|
-
push(m[0], m.index, m.index + m[0].length, 0.78);
|
|
1246
|
-
}
|
|
1247
|
-
const ytRe = new RegExp(YT_CHANNEL_PATTERN.source, "g");
|
|
1248
|
-
while ((m = ytRe.exec(text)) !== null) {
|
|
1249
|
-
push(m[0], m.index, m.index + m[0].length, 0.9);
|
|
1250
|
-
}
|
|
1251
|
-
const redditRe = new RegExp(REDDIT_USER_PATTERN.source, "g");
|
|
1252
|
-
while ((m = redditRe.exec(text)) !== null) {
|
|
1253
|
-
push(m[0], m.index, m.index + m[0].length, 0.85);
|
|
1254
|
-
}
|
|
1255
|
-
const linkedinRe = new RegExp(LINKEDIN_PATTERN.source, "gi");
|
|
1256
|
-
while ((m = linkedinRe.exec(text)) !== null) {
|
|
1257
|
-
push(m[0], m.index, m.index + m[0].length, 0.92);
|
|
1258
|
-
}
|
|
1259
|
-
const discordRe = new RegExp(DISCORD_ID_PATTERN.source, "g");
|
|
1260
|
-
while ((m = discordRe.exec(text)) !== null) {
|
|
1261
|
-
const match = m;
|
|
1262
|
-
const hasContext = discordContextPositions.some(
|
|
1263
|
-
(pos) => pos <= match.index && match.index - pos <= 30
|
|
1264
|
-
);
|
|
1265
|
-
if (hasContext) push(match[0], match.index, match.index + match[0].length, 0.85);
|
|
1266
|
-
}
|
|
1267
|
-
return matches;
|
|
1268
|
-
}
|
|
1269
|
-
|
|
1270
|
-
// src/detectors/vin.ts
|
|
1271
|
-
var VIN_PATTERN = /\b[A-HJ-NPR-Z0-9]{17}\b/gi;
|
|
1272
|
-
function detectVin(text) {
|
|
1273
|
-
const matches = [];
|
|
1274
|
-
const re = new RegExp(VIN_PATTERN.source, "gi");
|
|
1275
|
-
let m;
|
|
1276
|
-
while ((m = re.exec(text)) !== null) {
|
|
1277
|
-
const upper = m[0].toUpperCase();
|
|
1278
|
-
const valid = vinChecksum(upper);
|
|
1279
|
-
if (valid) {
|
|
1280
|
-
matches.push({
|
|
1281
|
-
category: "vin",
|
|
1282
|
-
value: m[0],
|
|
1283
|
-
start: m.index,
|
|
1284
|
-
end: m.index + m[0].length,
|
|
1285
|
-
confidence: 0.95
|
|
1286
|
-
});
|
|
1287
|
-
}
|
|
1288
|
-
}
|
|
1289
|
-
return matches;
|
|
1290
|
-
}
|
|
1291
|
-
function detectVinAggressive(text) {
|
|
1292
|
-
const strict = detectVin(text);
|
|
1293
|
-
const strictRanges = new Set(strict.map((m2) => `${String(m2.start)}-${String(m2.end)}`));
|
|
1294
|
-
const matches = [...strict];
|
|
1295
|
-
const re = new RegExp(VIN_PATTERN.source, "gi");
|
|
1296
|
-
let m;
|
|
1297
|
-
while ((m = re.exec(text)) !== null) {
|
|
1298
|
-
const key = `${String(m.index)}-${String(m.index + m[0].length)}`;
|
|
1299
|
-
if (!strictRanges.has(key)) {
|
|
1300
|
-
matches.push({
|
|
1301
|
-
category: "vin",
|
|
1302
|
-
value: m[0],
|
|
1303
|
-
start: m.index,
|
|
1304
|
-
end: m.index + m[0].length,
|
|
1305
|
-
confidence: 0.7
|
|
1306
|
-
});
|
|
1307
|
-
}
|
|
1308
|
-
}
|
|
1309
|
-
return matches;
|
|
1310
|
-
}
|
|
1311
|
-
|
|
1312
|
-
// src/detectors/license-plate.ts
|
|
1313
|
-
var LP_CONTEXT_RE = /\b(?:licen[sc]e\s+plate|number\s+plate|registration\s+(?:plate|number)|reg(?:istration)?\s*(?:no\.?|#|:)|plate(?:\s+(?:number|no\.?))?)\s*:?\s*/gi;
|
|
1314
|
-
var UK_CURRENT_LP_PATTERN = /\b[A-Z]{2}\d{2}[\s]?[A-Z]{3}\b/gi;
|
|
1315
|
-
var UK_OLD_LP_PATTERN = /\b[A-Z]\d{1,4}[\s]?[A-Z]{3}\b/gi;
|
|
1316
|
-
var EU_LP_PATTERN = /\b[A-Z]{1,2}[\s-]?\d{1,4}[\s-]?[A-Z]{1,3}\b/gi;
|
|
1317
|
-
var US_LP_PATTERN = /\b[A-Z0-9]{2,8}\b/g;
|
|
1318
|
-
function detectLicensePlate(text) {
|
|
1319
|
-
const matches = [];
|
|
1320
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1321
|
-
const contextPositions = [];
|
|
1322
|
-
const ctxRe = new RegExp(LP_CONTEXT_RE.source, "gi");
|
|
1323
|
-
let cm;
|
|
1324
|
-
while ((cm = ctxRe.exec(text)) !== null) {
|
|
1325
|
-
contextPositions.push(cm.index + cm[0].length);
|
|
1326
|
-
}
|
|
1327
|
-
function hasContextAt(index) {
|
|
1328
|
-
return contextPositions.some((pos) => pos <= index && index - pos <= 20);
|
|
1329
|
-
}
|
|
1330
|
-
function push(value, start, end, confidence) {
|
|
1331
|
-
const key = `${String(start)}-${String(end)}`;
|
|
1332
|
-
if (seen.has(key)) return;
|
|
1333
|
-
seen.add(key);
|
|
1334
|
-
matches.push({ category: "license-plate", value, start, end, confidence });
|
|
1335
|
-
}
|
|
1336
|
-
let m;
|
|
1337
|
-
const ukCurrentRe = new RegExp(UK_CURRENT_LP_PATTERN.source, "gi");
|
|
1338
|
-
while ((m = ukCurrentRe.exec(text)) !== null) {
|
|
1339
|
-
push(m[0], m.index, m.index + m[0].length, hasContextAt(m.index) ? 0.9 : 0.78);
|
|
1340
|
-
}
|
|
1341
|
-
const ukOldRe = new RegExp(UK_OLD_LP_PATTERN.source, "gi");
|
|
1342
|
-
while ((m = ukOldRe.exec(text)) !== null) {
|
|
1343
|
-
push(m[0], m.index, m.index + m[0].length, hasContextAt(m.index) ? 0.85 : 0.7);
|
|
1344
|
-
}
|
|
1345
|
-
const euRe = new RegExp(EU_LP_PATTERN.source, "gi");
|
|
1346
|
-
while ((m = euRe.exec(text)) !== null) {
|
|
1347
|
-
const conf = hasContextAt(m.index) ? 0.82 : 0.65;
|
|
1348
|
-
if (conf >= 0.7) push(m[0], m.index, m.index + m[0].length, conf);
|
|
1349
|
-
}
|
|
1350
|
-
const usRe = new RegExp(US_LP_PATTERN.source, "g");
|
|
1351
|
-
while ((m = usRe.exec(text)) !== null) {
|
|
1352
|
-
if (hasContextAt(m.index)) push(m[0], m.index, m.index + m[0].length, 0.8);
|
|
1353
|
-
}
|
|
1354
|
-
return matches;
|
|
1355
|
-
}
|
|
1356
|
-
|
|
1357
|
-
// src/detectors/tracking-number.ts
|
|
1358
|
-
var UPS_PATTERN = /\b1Z[A-Z0-9]{16}\b/gi;
|
|
1359
|
-
var USPS_LONG_PATTERN = /\b\d{20,22}\b/g;
|
|
1360
|
-
var USPS_FORMAT_PATTERN = /\b(?:9[2-5]\d{18,20}|[A-Z]{2}\d{9}US)\b/gi;
|
|
1361
|
-
var FEDEX_PATTERN = /\b(?:\d{12}|\d{15}|\d{20,22})\b/g;
|
|
1362
|
-
var DHL_PATTERN = /\bJD\d{9}\b|\b\d{10,11}\b/gi;
|
|
1363
|
-
var AMAZON_PATTERN = /\bTBA\d{12}\b/gi;
|
|
1364
|
-
var TRACKING_CONTEXT_RE = /\b(?:tracking(?:\s+(?:number|no\.?|#|id))?|shipment(?:\s+(?:number|id|tracking))?|parcel(?:\s+(?:number|id))?|package(?:\s+(?:number|id|tracking))?|(?:fedex|ups|usps|dhl|amazon)\s+(?:tracking|shipment)?)\s*:?\s*/gi;
|
|
1365
|
-
function detectTrackingNumber(text) {
|
|
1366
|
-
const matches = [];
|
|
1367
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1368
|
-
const contextPositions = [];
|
|
1369
|
-
const ctxRe = new RegExp(TRACKING_CONTEXT_RE.source, "gi");
|
|
1370
|
-
let cm;
|
|
1371
|
-
while ((cm = ctxRe.exec(text)) !== null) {
|
|
1372
|
-
contextPositions.push(cm.index + cm[0].length);
|
|
1373
|
-
}
|
|
1374
|
-
function hasContextAt(index) {
|
|
1375
|
-
return contextPositions.some((pos) => pos <= index && index - pos <= 30);
|
|
1376
|
-
}
|
|
1377
|
-
function push(value, start, end, confidence) {
|
|
1378
|
-
const key = `${String(start)}-${String(end)}`;
|
|
1379
|
-
if (seen.has(key)) return;
|
|
1380
|
-
seen.add(key);
|
|
1381
|
-
matches.push({ category: "tracking-number", value, start, end, confidence });
|
|
1382
|
-
}
|
|
1383
|
-
let m;
|
|
1384
|
-
const upsRe = new RegExp(UPS_PATTERN.source, "gi");
|
|
1385
|
-
while ((m = upsRe.exec(text)) !== null) {
|
|
1386
|
-
push(m[0], m.index, m.index + m[0].length, 0.96);
|
|
1387
|
-
}
|
|
1388
|
-
const amazonRe = new RegExp(AMAZON_PATTERN.source, "gi");
|
|
1389
|
-
while ((m = amazonRe.exec(text)) !== null) {
|
|
1390
|
-
push(m[0], m.index, m.index + m[0].length, 0.97);
|
|
1391
|
-
}
|
|
1392
|
-
const uspsFormatRe = new RegExp(USPS_FORMAT_PATTERN.source, "gi");
|
|
1393
|
-
while ((m = uspsFormatRe.exec(text)) !== null) {
|
|
1394
|
-
push(m[0], m.index, m.index + m[0].length, 0.9);
|
|
1395
|
-
}
|
|
1396
|
-
const dhlRe = new RegExp(DHL_PATTERN.source, "gi");
|
|
1397
|
-
while ((m = dhlRe.exec(text)) !== null) {
|
|
1398
|
-
if (m[0].startsWith("JD") || m[0].startsWith("jd")) {
|
|
1399
|
-
push(m[0], m.index, m.index + m[0].length, 0.88);
|
|
1400
|
-
} else if (hasContextAt(m.index)) {
|
|
1401
|
-
push(m[0], m.index, m.index + m[0].length, 0.78);
|
|
1402
|
-
}
|
|
1403
|
-
}
|
|
1404
|
-
const uspsLongRe = new RegExp(USPS_LONG_PATTERN.source, "g");
|
|
1405
|
-
while ((m = uspsLongRe.exec(text)) !== null) {
|
|
1406
|
-
if (hasContextAt(m.index)) push(m[0], m.index, m.index + m[0].length, 0.8);
|
|
1407
|
-
}
|
|
1408
|
-
const fedexRe = new RegExp(FEDEX_PATTERN.source, "g");
|
|
1409
|
-
while ((m = fedexRe.exec(text)) !== null) {
|
|
1410
|
-
if (hasContextAt(m.index)) push(m[0], m.index, m.index + m[0].length, 0.78);
|
|
1411
|
-
}
|
|
1412
|
-
return matches;
|
|
1413
|
-
}
|
|
1414
|
-
|
|
1415
|
-
// src/detectors/case-number.ts
|
|
1416
|
-
var FEDERAL_CASE_PATTERN = /\b(?:\d:\s*)?\d{2}[\s-](?:cv|cr|civ|crim|mc|mj|po|br|ap|adv|bk)[\s-]\d{4,6}\b/gi;
|
|
1417
|
-
var GENERAL_CASE_PATTERN = /\b(?:[A-Z]{1,4}[-\s])?\d{2,4}[-/]\d{2,6}(?:[-/][A-Z0-9]{1,5})?\b/gi;
|
|
1418
|
-
var CASE_CONTEXT_RE = /\b(?:case(?:\s+(?:number|no\.?|#|id))?|docket(?:\s+(?:number|no\.?|#))?|civil\s+action|criminal\s+(?:case|no\.?|#)|court\s+(?:case|file(?:\s+no\.?)?)|matter\s+no\.?|file\s+(?:number|no\.?|#))\s*:?\s*/gi;
|
|
1419
|
-
function detectCaseNumber(text) {
|
|
1420
|
-
const matches = [];
|
|
1421
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1422
|
-
const contextPositions = [];
|
|
1423
|
-
const ctxRe = new RegExp(CASE_CONTEXT_RE.source, "gi");
|
|
1424
|
-
let cm;
|
|
1425
|
-
while ((cm = ctxRe.exec(text)) !== null) {
|
|
1426
|
-
contextPositions.push(cm.index + cm[0].length);
|
|
1427
|
-
}
|
|
1428
|
-
function hasContextAt(index) {
|
|
1429
|
-
return contextPositions.some((pos) => pos <= index && index - pos <= 30);
|
|
1430
|
-
}
|
|
1431
|
-
function push(value, start, end, confidence) {
|
|
1432
|
-
const key = `${String(start)}-${String(end)}`;
|
|
1433
|
-
if (seen.has(key)) return;
|
|
1434
|
-
seen.add(key);
|
|
1435
|
-
matches.push({ category: "case-number", value, start, end, confidence });
|
|
1436
|
-
}
|
|
1437
|
-
let m;
|
|
1438
|
-
const fedRe = new RegExp(FEDERAL_CASE_PATTERN.source, "gi");
|
|
1439
|
-
while ((m = fedRe.exec(text)) !== null) {
|
|
1440
|
-
push(m[0], m.index, m.index + m[0].length, hasContextAt(m.index) ? 0.92 : 0.8);
|
|
1441
|
-
}
|
|
1442
|
-
const generalRe = new RegExp(GENERAL_CASE_PATTERN.source, "gi");
|
|
1443
|
-
while ((m = generalRe.exec(text)) !== null) {
|
|
1444
|
-
if (hasContextAt(m.index)) push(m[0], m.index, m.index + m[0].length, 0.78);
|
|
1445
|
-
}
|
|
1446
|
-
return matches;
|
|
1447
|
-
}
|
|
1448
|
-
|
|
1449
|
-
// src/detectors/company-registration.ts
|
|
1450
|
-
var CR_CONTEXT_RE = /\b(?:company\s+(?:registration|reg(?:istration)?)\s*(?:number|no\.?|#)?|companies\s+house\s*(?:number|no\.?|#)?|registered\s+(?:number|company\s+number|no\.?)|incorporation\s*(?:number|no\.?|#)?|company\s+(?:number|no\.?|#)|corporate\s+(?:id|number|no\.?|registration)|business\s+(?:registration|number)|ABN|ACN|CIK)\s*:?\s*/gi;
|
|
1451
|
-
var UK_CH_PATTERN = /\b(?:[A-Z]{2}\d{6}|\d{8})\b/gi;
|
|
1452
|
-
var US_EIN_PATTERN2 = /\b\d{2}-\d{7}\b/g;
|
|
1453
|
-
var AU_ACN_PATTERN = /\b\d{3}[\s]?\d{3}[\s]?\d{3}\b/g;
|
|
1454
|
-
var EU_CO_REG_PATTERN = /\b[A-Z]{2}[\s-]?[A-Z0-9]{5,12}\b/g;
|
|
1455
|
-
function detectCompanyRegistration(text) {
|
|
1456
|
-
const matches = [];
|
|
1457
|
-
const seen = /* @__PURE__ */ new Set();
|
|
1458
|
-
const contextPositions = [];
|
|
1459
|
-
const ctxRe = new RegExp(CR_CONTEXT_RE.source, "gi");
|
|
1460
|
-
let cm;
|
|
1461
|
-
while ((cm = ctxRe.exec(text)) !== null) {
|
|
1462
|
-
contextPositions.push(cm.index + cm[0].length);
|
|
1463
|
-
}
|
|
1464
|
-
function hasContextAt(index) {
|
|
1465
|
-
return contextPositions.some((pos) => pos <= index && index - pos <= 30);
|
|
1466
|
-
}
|
|
1467
|
-
function push(value, start, end, confidence) {
|
|
1468
|
-
const key = `${String(start)}-${String(end)}`;
|
|
1469
|
-
if (seen.has(key)) return;
|
|
1470
|
-
seen.add(key);
|
|
1471
|
-
matches.push({ category: "company-registration", value, start, end, confidence });
|
|
1472
|
-
}
|
|
1473
|
-
let m;
|
|
1474
|
-
const einRe = new RegExp(US_EIN_PATTERN2.source, "g");
|
|
1475
|
-
while ((m = einRe.exec(text)) !== null) {
|
|
1476
|
-
push(m[0], m.index, m.index + m[0].length, hasContextAt(m.index) ? 0.88 : 0.72);
|
|
1477
|
-
}
|
|
1478
|
-
const ukRe = new RegExp(UK_CH_PATTERN.source, "gi");
|
|
1479
|
-
while ((m = ukRe.exec(text)) !== null) {
|
|
1480
|
-
if (hasContextAt(m.index)) push(m[0], m.index, m.index + m[0].length, 0.85);
|
|
1481
|
-
}
|
|
1482
|
-
const acnRe = new RegExp(AU_ACN_PATTERN.source, "g");
|
|
1483
|
-
while ((m = acnRe.exec(text)) !== null) {
|
|
1484
|
-
if (hasContextAt(m.index)) push(m[0], m.index, m.index + m[0].length, 0.82);
|
|
1485
|
-
}
|
|
1486
|
-
const euRe = new RegExp(EU_CO_REG_PATTERN.source, "g");
|
|
1487
|
-
while ((m = euRe.exec(text)) !== null) {
|
|
1488
|
-
if (hasContextAt(m.index)) push(m[0], m.index, m.index + m[0].length, 0.78);
|
|
1489
|
-
}
|
|
1490
|
-
return matches;
|
|
1491
|
-
}
|
|
1492
|
-
|
|
1493
|
-
// src/detectors/index.ts
|
|
1494
|
-
var DETECTOR_REGISTRY = {
|
|
1495
|
-
// Original
|
|
1496
|
-
email: detectEmail,
|
|
1497
|
-
phone: detectPhone,
|
|
1498
|
-
ssn: detectSsn,
|
|
1499
|
-
"credit-card": detectCreditCard,
|
|
1500
|
-
ipv4: detectIpv4,
|
|
1501
|
-
ipv6: detectIpv6,
|
|
1502
|
-
url: detectUrl,
|
|
1503
|
-
iban: detectIban,
|
|
1504
|
-
"date-of-birth": detectDateOfBirth,
|
|
1505
|
-
name: detectName,
|
|
1506
|
-
// Personal Information
|
|
1507
|
-
address: detectAddress,
|
|
1508
|
-
passport: detectPassport,
|
|
1509
|
-
"drivers-license": detectDriversLicense,
|
|
1510
|
-
"national-id": detectNationalId,
|
|
1511
|
-
// Financial
|
|
1512
|
-
"bank-account": detectBankAccount,
|
|
1513
|
-
cryptocurrency: detectCryptocurrency,
|
|
1514
|
-
"tax-id": detectTaxId,
|
|
1515
|
-
// Healthcare
|
|
1516
|
-
"medical-record": detectMedicalRecord,
|
|
1517
|
-
"health-insurance": detectHealthInsurance,
|
|
1518
|
-
prescription: detectPrescription,
|
|
1519
|
-
// Digital Identity
|
|
1520
|
-
"api-key": detectApiKey,
|
|
1521
|
-
"social-media": detectSocialMedia,
|
|
1522
|
-
// Vehicles & Transportation
|
|
1523
|
-
vin: detectVin,
|
|
1524
|
-
"license-plate": detectLicensePlate,
|
|
1525
|
-
"tracking-number": detectTrackingNumber,
|
|
1526
|
-
// Government & Legal
|
|
1527
|
-
"case-number": detectCaseNumber,
|
|
1528
|
-
"company-registration": detectCompanyRegistration
|
|
1529
|
-
};
|
|
1530
|
-
var AGGRESSIVE_DETECTOR_REGISTRY = {
|
|
1531
|
-
// Original (aggressive variants where available)
|
|
1532
|
-
email: detectEmailAggressive,
|
|
1533
|
-
phone: detectPhoneAggressive,
|
|
1534
|
-
ssn: detectSsnAggressive,
|
|
1535
|
-
"credit-card": detectCreditCardAggressive,
|
|
1536
|
-
ipv4: detectIpv4,
|
|
1537
|
-
ipv6: detectIpv6,
|
|
1538
|
-
url: detectUrl,
|
|
1539
|
-
iban: detectIban,
|
|
1540
|
-
"date-of-birth": detectDateOfBirth,
|
|
1541
|
-
name: detectNameAggressive,
|
|
1542
|
-
// Personal Information
|
|
1543
|
-
address: detectAddress,
|
|
1544
|
-
passport: detectPassport,
|
|
1545
|
-
"drivers-license": detectDriversLicense,
|
|
1546
|
-
"national-id": detectNationalId,
|
|
1547
|
-
// Financial
|
|
1548
|
-
"bank-account": detectBankAccount,
|
|
1549
|
-
cryptocurrency: detectCryptocurrency,
|
|
1550
|
-
"tax-id": detectTaxId,
|
|
1551
|
-
// Healthcare
|
|
1552
|
-
"medical-record": detectMedicalRecord,
|
|
1553
|
-
"health-insurance": detectHealthInsurance,
|
|
1554
|
-
prescription: detectPrescription,
|
|
1555
|
-
// Digital Identity
|
|
1556
|
-
"api-key": detectApiKey,
|
|
1557
|
-
"social-media": detectSocialMedia,
|
|
1558
|
-
// Vehicles & Transportation
|
|
1559
|
-
vin: detectVinAggressive,
|
|
1560
|
-
"license-plate": detectLicensePlate,
|
|
1561
|
-
"tracking-number": detectTrackingNumber,
|
|
1562
|
-
// Government & Legal
|
|
1563
|
-
"case-number": detectCaseNumber,
|
|
1564
|
-
"company-registration": detectCompanyRegistration
|
|
1565
|
-
};
|
|
1566
|
-
|
|
1567
|
-
// src/strategies/tokenize.ts
|
|
1568
|
-
function createTokenStore() {
|
|
1569
|
-
return {
|
|
1570
|
-
tokens: /* @__PURE__ */ new Map(),
|
|
1571
|
-
index: /* @__PURE__ */ new Map(),
|
|
1572
|
-
counters: /* @__PURE__ */ new Map()
|
|
1573
|
-
};
|
|
1574
|
-
}
|
|
1575
|
-
function assignToken(store, category, value, prefix, format = "bracket") {
|
|
1576
|
-
const indexKey = `${prefix}:${value.toLowerCase()}`;
|
|
1577
|
-
const existing = store.index.get(indexKey);
|
|
1578
|
-
if (existing !== void 0) return existing;
|
|
1579
|
-
const count = (store.counters.get(prefix) ?? 0) + 1;
|
|
1580
|
-
store.counters.set(prefix, count);
|
|
1581
|
-
const id = format === "bracket" ? String(count).padStart(4, "0") : String(count);
|
|
1582
|
-
const token = format === "bracket" ? `[${prefix}_${id}]` : `<${prefix}_${id}>`;
|
|
1583
|
-
store.index.set(indexKey, token);
|
|
1584
|
-
store.tokens.set(token, value);
|
|
1585
|
-
return token;
|
|
1586
|
-
}
|
|
1587
|
-
|
|
1588
|
-
// src/strategies/mask.ts
|
|
1589
|
-
init_errors();
|
|
1590
|
-
function mask(value, options = {}) {
|
|
1591
|
-
const { maskChar = "*", keepLeading = 0, keepTrailing = 0, preserveFormat = false } = options;
|
|
1592
|
-
if (maskChar.length !== 1) {
|
|
1593
|
-
throw new ValidationError("maskChar", "must be exactly one character");
|
|
1594
|
-
}
|
|
1595
|
-
if (!Number.isInteger(keepLeading) || keepLeading < 0) {
|
|
1596
|
-
throw new ValidationError("keepLeading", "must be a non-negative integer");
|
|
1597
|
-
}
|
|
1598
|
-
if (!Number.isInteger(keepTrailing) || keepTrailing < 0) {
|
|
1599
|
-
throw new ValidationError("keepTrailing", "must be a non-negative integer");
|
|
1600
|
-
}
|
|
1601
|
-
if (value.length === 0) return value;
|
|
1602
|
-
if (preserveFormat) {
|
|
1603
|
-
const totalVisible2 = keepLeading + keepTrailing;
|
|
1604
|
-
if (totalVisible2 >= value.length) {
|
|
1605
|
-
return value;
|
|
1606
|
-
}
|
|
1607
|
-
const chars = value.split("");
|
|
1608
|
-
const interior = chars.slice(keepLeading, value.length - (keepTrailing > 0 ? keepTrailing : 0));
|
|
1609
|
-
const maskedInterior = interior.map((ch) => {
|
|
1610
|
-
if (/[a-zA-Z]/.test(ch)) return ch === ch.toUpperCase() ? "X" : "x";
|
|
1611
|
-
if (/\d/.test(ch)) return "0";
|
|
1612
|
-
return ch;
|
|
1613
|
-
});
|
|
1614
|
-
return chars.slice(0, keepLeading).join("") + maskedInterior.join("") + (keepTrailing > 0 ? chars.slice(-keepTrailing).join("") : "");
|
|
1615
|
-
}
|
|
1616
|
-
const totalVisible = keepLeading + keepTrailing;
|
|
1617
|
-
if (totalVisible >= value.length) {
|
|
1618
|
-
return maskChar.repeat(value.length);
|
|
1619
|
-
}
|
|
1620
|
-
const leading = value.slice(0, keepLeading);
|
|
1621
|
-
const trailing = keepTrailing > 0 ? value.slice(-keepTrailing) : "";
|
|
1622
|
-
const maskedLength = value.length - keepLeading - keepTrailing;
|
|
1623
|
-
return leading + maskChar.repeat(maskedLength) + trailing;
|
|
1624
|
-
}
|
|
1625
|
-
|
|
1626
|
-
// src/strategies/redact.ts
|
|
1627
|
-
init_errors();
|
|
1628
|
-
function redact(value, options = {}) {
|
|
1629
|
-
const { label = "[REDACTED]" } = options;
|
|
1630
|
-
if (label.trim().length === 0) {
|
|
1631
|
-
throw new ValidationError("label", "must not be an empty string");
|
|
1632
|
-
}
|
|
1633
|
-
return label;
|
|
1634
|
-
}
|
|
1635
|
-
|
|
1636
|
-
// src/strategies/pseudonymize.ts
|
|
1637
|
-
init_errors();
|
|
1638
|
-
function djb2(str) {
|
|
1639
|
-
let hash2 = 5381;
|
|
1640
|
-
for (let i = 0; i < str.length; i++) {
|
|
1641
|
-
hash2 = (hash2 << 5) + hash2 ^ str.charCodeAt(i);
|
|
1642
|
-
hash2 = hash2 >>> 0;
|
|
1643
|
-
}
|
|
1644
|
-
return hash2;
|
|
1645
|
-
}
|
|
1646
|
-
function toHex8(n) {
|
|
1647
|
-
return (n >>> 0).toString(16).padStart(8, "0");
|
|
1648
|
-
}
|
|
1649
|
-
function pseudonymize(value, options = {}) {
|
|
1650
|
-
const { seed, prefix = "id_" } = options;
|
|
1651
|
-
if (/\s/.test(prefix)) {
|
|
1652
|
-
throw new ValidationError("prefix", "must not contain whitespace characters");
|
|
1653
|
-
}
|
|
1654
|
-
if (seed !== void 0) {
|
|
1655
|
-
const combined = `${seed}:${value}`;
|
|
1656
|
-
const h1 = djb2(combined);
|
|
1657
|
-
const h2 = djb2(`${combined}:v2`);
|
|
1658
|
-
return `${prefix}${toHex8(h1)}${toHex8(h2)}`;
|
|
1659
|
-
}
|
|
1660
|
-
if (typeof globalThis.crypto !== "undefined" && typeof globalThis.crypto.getRandomValues === "function") {
|
|
1661
|
-
const bytes = new Uint8Array(8);
|
|
1662
|
-
globalThis.crypto.getRandomValues(bytes);
|
|
1663
|
-
const hex = Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
1664
|
-
return `${prefix}${hex}`;
|
|
1665
|
-
}
|
|
1666
|
-
const fallback = Math.floor(Math.random() * 4294967295);
|
|
1667
|
-
return `${prefix}${toHex8(fallback)}`;
|
|
1668
|
-
}
|
|
1669
|
-
|
|
1670
|
-
// src/strategies/generalize.ts
|
|
1671
|
-
init_errors();
|
|
1672
|
-
function generalize(value, options = {}) {
|
|
1673
|
-
const { bucketSize = 10 } = options;
|
|
1674
|
-
if (!Number.isInteger(bucketSize) || bucketSize < 1) {
|
|
1675
|
-
throw new ValidationError("bucketSize", "must be a positive integer");
|
|
1676
|
-
}
|
|
1677
|
-
const parsed = Number(value);
|
|
1678
|
-
if (!Number.isFinite(parsed)) {
|
|
1679
|
-
return value;
|
|
1680
|
-
}
|
|
1681
|
-
const n = Math.floor(parsed);
|
|
1682
|
-
const low = Math.floor(n / bucketSize) * bucketSize;
|
|
1683
|
-
const high = low + bucketSize - 1;
|
|
1684
|
-
return `${String(low)}-${String(high)}`;
|
|
1685
|
-
}
|
|
1686
|
-
|
|
1687
|
-
// src/anonymize.ts
|
|
1688
|
-
init_errors();
|
|
1689
|
-
|
|
1690
|
-
// src/presets.ts
|
|
1691
|
-
init_errors();
|
|
1692
|
-
var GDPR_PRESET = {
|
|
1693
|
-
name: "gdpr",
|
|
1694
|
-
description: "EU General Data Protection Regulation \u2014 Covers all categories of personal data that can identify a natural person.",
|
|
1695
|
-
categories: [
|
|
1696
|
-
"name",
|
|
1697
|
-
"email",
|
|
1698
|
-
"phone",
|
|
1699
|
-
"address",
|
|
1700
|
-
"date-of-birth",
|
|
1701
|
-
"ssn",
|
|
1702
|
-
"national-id",
|
|
1703
|
-
"passport",
|
|
1704
|
-
"drivers-license",
|
|
1705
|
-
"iban",
|
|
1706
|
-
"bank-account",
|
|
1707
|
-
"credit-card",
|
|
1708
|
-
"ipv4",
|
|
1709
|
-
"ipv6",
|
|
1710
|
-
"url",
|
|
1711
|
-
"medical-record",
|
|
1712
|
-
"health-insurance",
|
|
1713
|
-
"prescription",
|
|
1714
|
-
"social-media",
|
|
1715
|
-
"cryptocurrency",
|
|
1716
|
-
"tax-id"
|
|
1717
|
-
],
|
|
1718
|
-
defaultStrategy: { strategy: "pseudonymize" }
|
|
1719
|
-
};
|
|
1720
|
-
var HIPAA_PRESET = {
|
|
1721
|
-
name: "hipaa",
|
|
1722
|
-
description: "US HIPAA Safe Harbor \u2014 Covers all 18 protected health information (PHI) identifiers to de-identify medical records.",
|
|
1723
|
-
categories: [
|
|
1724
|
-
"name",
|
|
1725
|
-
"address",
|
|
1726
|
-
"date-of-birth",
|
|
1727
|
-
"phone",
|
|
1728
|
-
"email",
|
|
1729
|
-
"ssn",
|
|
1730
|
-
"medical-record",
|
|
1731
|
-
"health-insurance",
|
|
1732
|
-
"prescription",
|
|
1733
|
-
"bank-account",
|
|
1734
|
-
"drivers-license",
|
|
1735
|
-
"vin",
|
|
1736
|
-
"url",
|
|
1737
|
-
"ipv4",
|
|
1738
|
-
"ipv6",
|
|
1739
|
-
"national-id",
|
|
1740
|
-
"tax-id",
|
|
1741
|
-
"company-registration"
|
|
1742
|
-
],
|
|
1743
|
-
defaultStrategy: { strategy: "redact" }
|
|
1744
|
-
};
|
|
1745
|
-
var CCPA_PRESET = {
|
|
1746
|
-
name: "ccpa",
|
|
1747
|
-
description: "California Consumer Privacy Act \u2014 Covers personal information categories defined by CCPA, including identifiers, financial data, and online activity.",
|
|
1748
|
-
categories: [
|
|
1749
|
-
"name",
|
|
1750
|
-
"email",
|
|
1751
|
-
"phone",
|
|
1752
|
-
"address",
|
|
1753
|
-
"ssn",
|
|
1754
|
-
"national-id",
|
|
1755
|
-
"passport",
|
|
1756
|
-
"drivers-license",
|
|
1757
|
-
"bank-account",
|
|
1758
|
-
"credit-card",
|
|
1759
|
-
"tax-id",
|
|
1760
|
-
"ipv4",
|
|
1761
|
-
"ipv6",
|
|
1762
|
-
"url",
|
|
1763
|
-
"social-media",
|
|
1764
|
-
"date-of-birth"
|
|
1765
|
-
],
|
|
1766
|
-
defaultStrategy: { strategy: "redact" }
|
|
1767
|
-
};
|
|
1768
|
-
var PCI_DSS_PRESET = {
|
|
1769
|
-
name: "pci-dss",
|
|
1770
|
-
description: "PCI-DSS Cardholder Data \u2014 Covers credit/debit card numbers, bank accounts, and associated cardholder identifiers.",
|
|
1771
|
-
categories: [
|
|
1772
|
-
"credit-card",
|
|
1773
|
-
"bank-account",
|
|
1774
|
-
"name",
|
|
1775
|
-
"address",
|
|
1776
|
-
"email",
|
|
1777
|
-
"phone"
|
|
1778
|
-
],
|
|
1779
|
-
defaultStrategy: { strategy: "redact" },
|
|
1780
|
-
rules: [
|
|
1781
|
-
{
|
|
1782
|
-
category: "credit-card",
|
|
1783
|
-
strategy: { strategy: "mask", keepTrailing: 4 }
|
|
1784
|
-
},
|
|
1785
|
-
{
|
|
1786
|
-
category: "bank-account",
|
|
1787
|
-
strategy: { strategy: "mask", keepTrailing: 4 }
|
|
1788
|
-
}
|
|
1789
|
-
]
|
|
1790
|
-
};
|
|
1791
|
-
var SOX_PRESET = {
|
|
1792
|
-
name: "sox",
|
|
1793
|
-
description: "Sarbanes-Oxley Act \u2014 Covers financial records, corporate officer identifiers, and audit trail data.",
|
|
1794
|
-
categories: [
|
|
1795
|
-
"name",
|
|
1796
|
-
"email",
|
|
1797
|
-
"tax-id",
|
|
1798
|
-
"bank-account",
|
|
1799
|
-
"company-registration",
|
|
1800
|
-
"ssn",
|
|
1801
|
-
"address",
|
|
1802
|
-
"phone"
|
|
1803
|
-
],
|
|
1804
|
-
defaultStrategy: { strategy: "redact" }
|
|
1805
|
-
};
|
|
1806
|
-
var FERPA_PRESET = {
|
|
1807
|
-
name: "ferpa",
|
|
1808
|
-
description: "FERPA \u2014 Covers education records and student personally identifiable information (PII).",
|
|
1809
|
-
categories: [
|
|
1810
|
-
"name",
|
|
1811
|
-
"email",
|
|
1812
|
-
"phone",
|
|
1813
|
-
"address",
|
|
1814
|
-
"date-of-birth",
|
|
1815
|
-
"ssn",
|
|
1816
|
-
"national-id"
|
|
1817
|
-
],
|
|
1818
|
-
defaultStrategy: { strategy: "redact" }
|
|
1819
|
-
};
|
|
1820
|
-
var PRESET_REGISTRY = {
|
|
1821
|
-
gdpr: GDPR_PRESET,
|
|
1822
|
-
hipaa: HIPAA_PRESET,
|
|
1823
|
-
ccpa: CCPA_PRESET,
|
|
1824
|
-
"pci-dss": PCI_DSS_PRESET,
|
|
1825
|
-
sox: SOX_PRESET,
|
|
1826
|
-
ferpa: FERPA_PRESET
|
|
1827
|
-
};
|
|
1828
|
-
function getPreset(name) {
|
|
1829
|
-
const preset = PRESET_REGISTRY[name];
|
|
1830
|
-
if (preset === void 0) {
|
|
1831
|
-
throw new PresetNotFoundError(name);
|
|
1832
|
-
}
|
|
1833
|
-
return preset;
|
|
1834
|
-
}
|
|
1835
|
-
|
|
1836
|
-
// src/anonymize.ts
|
|
1837
|
-
var ALL_CATEGORIES = [
|
|
1838
|
-
"email",
|
|
1839
|
-
"phone",
|
|
1840
|
-
"ssn",
|
|
1841
|
-
"credit-card",
|
|
1842
|
-
"ipv4",
|
|
1843
|
-
"ipv6",
|
|
1844
|
-
"url",
|
|
1845
|
-
"iban",
|
|
1846
|
-
"date-of-birth",
|
|
1847
|
-
"name",
|
|
1848
|
-
"address",
|
|
1849
|
-
"passport",
|
|
1850
|
-
"drivers-license",
|
|
1851
|
-
"national-id",
|
|
1852
|
-
"bank-account",
|
|
1853
|
-
"cryptocurrency",
|
|
1854
|
-
"tax-id",
|
|
1855
|
-
"medical-record",
|
|
1856
|
-
"health-insurance",
|
|
1857
|
-
"prescription",
|
|
1858
|
-
"api-key",
|
|
1859
|
-
"social-media",
|
|
1860
|
-
"vin",
|
|
1861
|
-
"license-plate",
|
|
1862
|
-
"tracking-number",
|
|
1863
|
-
"case-number",
|
|
1864
|
-
"company-registration"
|
|
1865
|
-
];
|
|
1866
|
-
var TOKEN_PREFIX_MAP = {
|
|
1867
|
-
email: "EMAIL",
|
|
1868
|
-
phone: "PHONE",
|
|
1869
|
-
ssn: "SSN",
|
|
1870
|
-
"credit-card": "CREDIT_CARD",
|
|
1871
|
-
ipv4: "IPV4",
|
|
1872
|
-
ipv6: "IPV6",
|
|
1873
|
-
url: "URL",
|
|
1874
|
-
iban: "IBAN",
|
|
1875
|
-
"date-of-birth": "DATE",
|
|
1876
|
-
name: "PERSON",
|
|
1877
|
-
address: "ADDRESS",
|
|
1878
|
-
passport: "PASSPORT",
|
|
1879
|
-
"drivers-license": "DRIVERS_LICENSE",
|
|
1880
|
-
"national-id": "NATIONAL_ID",
|
|
1881
|
-
"bank-account": "BANK_ACCOUNT",
|
|
1882
|
-
cryptocurrency: "CRYPTO",
|
|
1883
|
-
"tax-id": "TAX_ID",
|
|
1884
|
-
"medical-record": "MEDICAL_RECORD",
|
|
1885
|
-
"health-insurance": "HEALTH_INSURANCE",
|
|
1886
|
-
prescription: "PRESCRIPTION",
|
|
1887
|
-
"api-key": "API_KEY",
|
|
1888
|
-
"social-media": "SOCIAL_MEDIA",
|
|
1889
|
-
vin: "VIN",
|
|
1890
|
-
"license-plate": "LICENSE_PLATE",
|
|
1891
|
-
"tracking-number": "TRACKING_NUMBER",
|
|
1892
|
-
"case-number": "CASE_NUMBER",
|
|
1893
|
-
"company-registration": "COMPANY_REG"
|
|
1894
|
-
};
|
|
1895
|
-
function applyStrategySync(value, opts) {
|
|
1896
|
-
switch (opts.strategy) {
|
|
1897
|
-
case "mask":
|
|
1898
|
-
return mask(value, opts);
|
|
1899
|
-
case "redact":
|
|
1900
|
-
return redact(value, opts);
|
|
1901
|
-
case "pseudonymize":
|
|
1902
|
-
return pseudonymize(value, opts);
|
|
1903
|
-
case "hash":
|
|
1904
|
-
console.warn(
|
|
1905
|
-
"[anonyma] The `hash` strategy is async and cannot be used in the synchronous `anonymize()` pipeline. Falling back to a deterministic pseudonym. Use `hash()` directly if you need true SHA-256 output."
|
|
1906
|
-
);
|
|
1907
|
-
return pseudonymize(value, { seed: opts.pepper ?? "__hash_fallback__", prefix: "hsh_" });
|
|
1908
|
-
case "generalize":
|
|
1909
|
-
return generalize(value, opts);
|
|
1910
|
-
case "tokenize":
|
|
1911
|
-
case "encrypt":
|
|
1912
|
-
case "synthesize":
|
|
1913
|
-
console.warn(
|
|
1914
|
-
`[anonyma] The \`${opts.strategy}\` strategy requires async execution. Falling back to redact in the synchronous pipeline. Use anonymizeAsync() instead.`
|
|
1915
|
-
);
|
|
1916
|
-
return redact();
|
|
1917
|
-
default: {
|
|
1918
|
-
const _exhaustive = opts;
|
|
1919
|
-
throw new UnsupportedStrategyError(_exhaustive.strategy);
|
|
1920
|
-
}
|
|
1921
|
-
}
|
|
1922
|
-
}
|
|
1923
|
-
function deduplicateMatches(matches) {
|
|
1924
|
-
const sorted = [...matches].sort(
|
|
1925
|
-
(a, b) => a.start - b.start || b.confidence - a.confidence
|
|
1926
|
-
);
|
|
1927
|
-
const result = [];
|
|
1928
|
-
let cursor = 0;
|
|
1929
|
-
for (const m of sorted) {
|
|
1930
|
-
if (m.start >= cursor) {
|
|
1931
|
-
result.push(m);
|
|
1932
|
-
cursor = m.end;
|
|
1933
|
-
}
|
|
1934
|
-
}
|
|
1935
|
-
return result;
|
|
1936
|
-
}
|
|
1937
|
-
function resolveDetector(category, customDetectors, aggressive) {
|
|
1938
|
-
if (customDetectors?.[category]) return customDetectors[category];
|
|
1939
|
-
return aggressive ? AGGRESSIVE_DETECTOR_REGISTRY[category] : DETECTOR_REGISTRY[category];
|
|
1940
|
-
}
|
|
1941
|
-
function detectCustomPatterns(text, customPatterns) {
|
|
1942
|
-
const results = [];
|
|
1943
|
-
for (const cp of customPatterns) {
|
|
1944
|
-
if (!(cp.pattern instanceof RegExp)) {
|
|
1945
|
-
throw new ValidationError("customPatterns[].pattern", "must be a RegExp instance");
|
|
1946
|
-
}
|
|
1947
|
-
const flags = cp.pattern.flags.includes("g") ? cp.pattern.flags : cp.pattern.flags + "g";
|
|
1948
|
-
const re = new RegExp(cp.pattern.source, flags);
|
|
1949
|
-
const category = cp.category ?? "custom";
|
|
1950
|
-
const confidence = cp.confidence ?? 0.85;
|
|
1951
|
-
const label = cp.label ?? "[REDACTED]";
|
|
1952
|
-
let m;
|
|
1953
|
-
while ((m = re.exec(text)) !== null) {
|
|
1954
|
-
results.push({
|
|
1955
|
-
category,
|
|
1956
|
-
value: m[0],
|
|
1957
|
-
start: m.index,
|
|
1958
|
-
end: m.index + m[0].length,
|
|
1959
|
-
confidence,
|
|
1960
|
-
replacement: label
|
|
1961
|
-
});
|
|
1962
|
-
}
|
|
1963
|
-
}
|
|
1964
|
-
return results;
|
|
1965
|
-
}
|
|
1966
|
-
function resolveCategories(options) {
|
|
1967
|
-
const { rules = [], enabledCategories } = options;
|
|
1968
|
-
if (rules.length > 0) {
|
|
1969
|
-
return rules.map((r) => r.category);
|
|
1970
|
-
}
|
|
1971
|
-
if (enabledCategories) {
|
|
1972
|
-
return ALL_CATEGORIES.filter((cat) => enabledCategories[cat] === true);
|
|
1973
|
-
}
|
|
1974
|
-
return [...ALL_CATEGORIES];
|
|
1975
|
-
}
|
|
1976
|
-
function detect(text, categories = ALL_CATEGORIES, customDetectors, aggressive) {
|
|
1977
|
-
if (typeof text !== "string") {
|
|
1978
|
-
throw new ValidationError("text", "must be a string");
|
|
1979
|
-
}
|
|
1980
|
-
const raw = [];
|
|
1981
|
-
for (const category of categories) {
|
|
1982
|
-
if (!ALL_CATEGORIES.includes(category)) {
|
|
1983
|
-
throw new UnknownCategoryError(category);
|
|
1984
|
-
}
|
|
1985
|
-
const detector = resolveDetector(category, customDetectors, aggressive);
|
|
1986
|
-
raw.push(...detector(text));
|
|
1987
|
-
}
|
|
1988
|
-
return deduplicateMatches(raw);
|
|
1989
|
-
}
|
|
1990
|
-
function anonymize(text, options = {}) {
|
|
1991
|
-
if (typeof text !== "string") {
|
|
1992
|
-
throw new ValidationError("text", "must be a string");
|
|
1993
|
-
}
|
|
1994
|
-
if (options.globalReplacement?.trim().length === 0) {
|
|
1995
|
-
throw new ValidationError("globalReplacement", "must not be an empty string");
|
|
1996
|
-
}
|
|
1997
|
-
let resolvedPreset;
|
|
1998
|
-
if (options.preset) {
|
|
1999
|
-
try {
|
|
2000
|
-
resolvedPreset = getPreset(options.preset);
|
|
2001
|
-
} catch {
|
|
2002
|
-
throw new PresetNotFoundError(options.preset);
|
|
2003
|
-
}
|
|
2004
|
-
}
|
|
2005
|
-
const {
|
|
2006
|
-
rules = resolvedPreset ? resolvedPreset.categories.map((category) => ({
|
|
2007
|
-
category,
|
|
2008
|
-
strategy: resolvedPreset.rules?.find((r) => r.category === category)?.strategy ?? resolvedPreset.defaultStrategy
|
|
2009
|
-
})) : [],
|
|
2010
|
-
defaultStrategy = resolvedPreset?.defaultStrategy ?? { strategy: "redact" },
|
|
2011
|
-
includeMatches = false,
|
|
2012
|
-
customDetectors,
|
|
2013
|
-
customPatterns = [],
|
|
2014
|
-
globalReplacement,
|
|
2015
|
-
consistentTokens = false,
|
|
2016
|
-
aggressive = false,
|
|
2017
|
-
enabledCategories,
|
|
2018
|
-
allowlist = [],
|
|
2019
|
-
allowlistPatterns = [],
|
|
2020
|
-
allowlistCaseSensitive = false,
|
|
2021
|
-
confidenceThreshold = 0
|
|
2022
|
-
} = options;
|
|
2023
|
-
const ruleMap = /* @__PURE__ */ new Map();
|
|
2024
|
-
for (const rule of rules) {
|
|
2025
|
-
if (!ALL_CATEGORIES.includes(rule.category)) {
|
|
2026
|
-
throw new UnknownCategoryError(rule.category);
|
|
2027
|
-
}
|
|
2028
|
-
ruleMap.set(rule.category, rule.strategy);
|
|
2029
|
-
}
|
|
2030
|
-
const categories = resolveCategories({
|
|
2031
|
-
rules,
|
|
2032
|
-
...enabledCategories !== void 0 ? { enabledCategories } : {}
|
|
2033
|
-
});
|
|
2034
|
-
const compiledAllowlistPatterns = [
|
|
2035
|
-
...allowlistPatterns,
|
|
2036
|
-
...allowlist.map(
|
|
2037
|
-
(entry) => new RegExp(
|
|
2038
|
-
entry.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"),
|
|
2039
|
-
allowlistCaseSensitive ? "" : "i"
|
|
2040
|
-
)
|
|
2041
|
-
)
|
|
2042
|
-
];
|
|
2043
|
-
function isAllowlisted(value) {
|
|
2044
|
-
if (compiledAllowlistPatterns.length === 0) return false;
|
|
2045
|
-
return compiledAllowlistPatterns.some((re) => re.test(value));
|
|
2046
|
-
}
|
|
2047
|
-
const builtInMatches = detect(text, categories, customDetectors, aggressive);
|
|
2048
|
-
const customMatches = customPatterns.length > 0 ? detectCustomPatterns(text, customPatterns) : [];
|
|
2049
|
-
const filteredBuiltInMatches = builtInMatches.filter(
|
|
2050
|
-
(m) => m.confidence >= confidenceThreshold && !isAllowlisted(m.value)
|
|
2051
|
-
);
|
|
2052
|
-
const allRaw = [...filteredBuiltInMatches, ...customMatches];
|
|
2053
|
-
const allDeduped = deduplicateMatches(allRaw);
|
|
2054
|
-
const tokenMap = /* @__PURE__ */ new Map();
|
|
2055
|
-
const tokenCounters = /* @__PURE__ */ new Map();
|
|
2056
|
-
function assignToken2(category, value) {
|
|
2057
|
-
const prefix = TOKEN_PREFIX_MAP[category] ?? category.toUpperCase();
|
|
2058
|
-
const key = `${prefix}:${value.toLowerCase()}`;
|
|
2059
|
-
const existing = tokenMap.get(key);
|
|
2060
|
-
if (existing) return existing;
|
|
2061
|
-
const count = (tokenCounters.get(prefix) ?? 0) + 1;
|
|
2062
|
-
tokenCounters.set(prefix, count);
|
|
2063
|
-
const token = `${prefix}_${String(count)}`;
|
|
2064
|
-
tokenMap.set(key, token);
|
|
2065
|
-
return token;
|
|
2066
|
-
}
|
|
2067
|
-
let result = text;
|
|
2068
|
-
const reversed = [...allDeduped].reverse();
|
|
2069
|
-
for (const match of reversed) {
|
|
2070
|
-
let replacement;
|
|
2071
|
-
if (globalReplacement !== void 0) {
|
|
2072
|
-
replacement = globalReplacement;
|
|
2073
|
-
} else if (consistentTokens) {
|
|
2074
|
-
replacement = assignToken2(match.category, match.value);
|
|
2075
|
-
} else if ("replacement" in match) {
|
|
2076
|
-
replacement = match.replacement;
|
|
2077
|
-
} else {
|
|
2078
|
-
const strategy = ruleMap.get(match.category) ?? defaultStrategy;
|
|
2079
|
-
replacement = applyStrategySync(match.value, strategy);
|
|
2080
|
-
}
|
|
2081
|
-
result = result.slice(0, match.start) + replacement + result.slice(match.end);
|
|
2082
|
-
}
|
|
2083
|
-
const reportedMatches = includeMatches ? filteredBuiltInMatches : [];
|
|
2084
|
-
return {
|
|
2085
|
-
text: result,
|
|
2086
|
-
matches: reportedMatches
|
|
2087
|
-
};
|
|
2088
|
-
}
|
|
2089
|
-
async function anonymizeAsync(text, options = {}) {
|
|
2090
|
-
if (typeof text !== "string") {
|
|
2091
|
-
throw new ValidationError("text", "must be a string");
|
|
2092
|
-
}
|
|
2093
|
-
const hasAsyncStrategy = (opts) => opts.strategy === "hash" || opts.strategy === "encrypt" || opts.strategy === "tokenize" || opts.strategy === "synthesize";
|
|
2094
|
-
const needsAsync = options.defaultStrategy != null && hasAsyncStrategy(options.defaultStrategy) || (options.rules ?? []).some((r) => hasAsyncStrategy(r.strategy));
|
|
2095
|
-
if (!needsAsync) {
|
|
2096
|
-
return anonymize(text, options);
|
|
2097
|
-
}
|
|
2098
|
-
const { hash: hashFn } = await Promise.resolve().then(() => (init_hash(), hash_exports));
|
|
2099
|
-
let resolvedPreset;
|
|
2100
|
-
if (options.preset) {
|
|
2101
|
-
try {
|
|
2102
|
-
resolvedPreset = getPreset(options.preset);
|
|
2103
|
-
} catch {
|
|
2104
|
-
throw new PresetNotFoundError(options.preset);
|
|
2105
|
-
}
|
|
2106
|
-
}
|
|
2107
|
-
const {
|
|
2108
|
-
rules = resolvedPreset ? resolvedPreset.categories.map((category) => ({
|
|
2109
|
-
category,
|
|
2110
|
-
strategy: resolvedPreset.rules?.find((r) => r.category === category)?.strategy ?? resolvedPreset.defaultStrategy
|
|
2111
|
-
})) : [],
|
|
2112
|
-
defaultStrategy = resolvedPreset?.defaultStrategy ?? { strategy: "redact" },
|
|
2113
|
-
includeMatches = false,
|
|
2114
|
-
customDetectors,
|
|
2115
|
-
customPatterns = [],
|
|
2116
|
-
globalReplacement,
|
|
2117
|
-
consistentTokens = false,
|
|
2118
|
-
aggressive = false,
|
|
2119
|
-
enabledCategories,
|
|
2120
|
-
allowlist = [],
|
|
2121
|
-
allowlistPatterns = [],
|
|
2122
|
-
allowlistCaseSensitive = false,
|
|
2123
|
-
confidenceThreshold = 0
|
|
2124
|
-
} = options;
|
|
2125
|
-
const ruleMap = /* @__PURE__ */ new Map();
|
|
2126
|
-
for (const rule of rules) {
|
|
2127
|
-
if (!ALL_CATEGORIES.includes(rule.category)) throw new UnknownCategoryError(rule.category);
|
|
2128
|
-
ruleMap.set(rule.category, rule.strategy);
|
|
2129
|
-
}
|
|
2130
|
-
const categories = resolveCategories({
|
|
2131
|
-
rules,
|
|
2132
|
-
...enabledCategories !== void 0 ? { enabledCategories } : {}
|
|
2133
|
-
});
|
|
2134
|
-
const builtInMatches = detect(text, categories, customDetectors, aggressive);
|
|
2135
|
-
const customMatches = customPatterns.length > 0 ? detectCustomPatterns(text, customPatterns) : [];
|
|
2136
|
-
const compiledAllowlistPatterns = [
|
|
2137
|
-
...allowlistPatterns,
|
|
2138
|
-
...allowlist.map(
|
|
2139
|
-
(entry) => new RegExp(
|
|
2140
|
-
entry.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"),
|
|
2141
|
-
allowlistCaseSensitive ? "" : "i"
|
|
2142
|
-
)
|
|
2143
|
-
)
|
|
2144
|
-
];
|
|
2145
|
-
const isAllowlisted = (value) => compiledAllowlistPatterns.some((re) => re.test(value));
|
|
2146
|
-
const filteredBuiltInMatches = builtInMatches.filter(
|
|
2147
|
-
(m) => m.confidence >= confidenceThreshold && !isAllowlisted(m.value)
|
|
2148
|
-
);
|
|
2149
|
-
const allRaw = [...filteredBuiltInMatches, ...customMatches];
|
|
2150
|
-
const allDeduped = deduplicateMatches(allRaw);
|
|
2151
|
-
const tokenMap = /* @__PURE__ */ new Map();
|
|
2152
|
-
const tokenCounters = /* @__PURE__ */ new Map();
|
|
2153
|
-
function assignToken2(category, value) {
|
|
2154
|
-
const prefix = TOKEN_PREFIX_MAP[category] ?? category.toUpperCase();
|
|
2155
|
-
const key = `${prefix}:${value.toLowerCase()}`;
|
|
2156
|
-
const existing = tokenMap.get(key);
|
|
2157
|
-
if (existing) return existing;
|
|
2158
|
-
const count = (tokenCounters.get(prefix) ?? 0) + 1;
|
|
2159
|
-
tokenCounters.set(prefix, count);
|
|
2160
|
-
const token = `${prefix}_${String(count)}`;
|
|
2161
|
-
tokenMap.set(key, token);
|
|
2162
|
-
return token;
|
|
2163
|
-
}
|
|
2164
|
-
let result = text;
|
|
2165
|
-
const reversed = [...allDeduped].reverse();
|
|
2166
|
-
for (const match of reversed) {
|
|
2167
|
-
let replacement;
|
|
2168
|
-
if (globalReplacement !== void 0) {
|
|
2169
|
-
replacement = globalReplacement;
|
|
2170
|
-
} else if (consistentTokens) {
|
|
2171
|
-
replacement = assignToken2(match.category, match.value);
|
|
2172
|
-
} else if ("replacement" in match) {
|
|
2173
|
-
replacement = match.replacement;
|
|
2174
|
-
} else {
|
|
2175
|
-
const strategy = ruleMap.get(match.category) ?? defaultStrategy;
|
|
2176
|
-
if (strategy.strategy === "hash") {
|
|
2177
|
-
replacement = await hashFn(match.value, strategy);
|
|
2178
|
-
} else {
|
|
2179
|
-
replacement = applyStrategySync(match.value, strategy);
|
|
2180
|
-
}
|
|
2181
|
-
}
|
|
2182
|
-
result = result.slice(0, match.start) + replacement + result.slice(match.end);
|
|
2183
|
-
}
|
|
2184
|
-
return {
|
|
2185
|
-
text: result,
|
|
2186
|
-
matches: includeMatches ? filteredBuiltInMatches : []
|
|
2187
|
-
};
|
|
2188
|
-
}
|
|
2189
|
-
|
|
2190
|
-
// src/tokenize.ts
|
|
2191
|
-
init_errors();
|
|
2192
|
-
var TOKEN_PREFIX_MAP2 = {
|
|
2193
|
-
email: "EMAIL",
|
|
2194
|
-
phone: "PHONE",
|
|
2195
|
-
ssn: "SSN",
|
|
2196
|
-
"credit-card": "CREDIT_CARD",
|
|
2197
|
-
ipv4: "IPV4",
|
|
2198
|
-
ipv6: "IPV6",
|
|
2199
|
-
url: "URL",
|
|
2200
|
-
iban: "IBAN",
|
|
2201
|
-
"date-of-birth": "DATE",
|
|
2202
|
-
name: "PERSON",
|
|
2203
|
-
address: "ADDRESS",
|
|
2204
|
-
passport: "PASSPORT",
|
|
2205
|
-
"drivers-license": "DRIVERS_LICENSE",
|
|
2206
|
-
"national-id": "NATIONAL_ID",
|
|
2207
|
-
"bank-account": "BANK_ACCOUNT",
|
|
2208
|
-
cryptocurrency: "CRYPTO",
|
|
2209
|
-
"tax-id": "TAX_ID",
|
|
2210
|
-
"medical-record": "MEDICAL_RECORD",
|
|
2211
|
-
"health-insurance": "HEALTH_INSURANCE",
|
|
2212
|
-
prescription: "PRESCRIPTION",
|
|
2213
|
-
"api-key": "API_KEY",
|
|
2214
|
-
"social-media": "SOCIAL_MEDIA",
|
|
2215
|
-
vin: "VIN",
|
|
2216
|
-
"license-plate": "LICENSE_PLATE",
|
|
2217
|
-
"tracking-number": "TRACKING_NUMBER",
|
|
2218
|
-
"case-number": "CASE_NUMBER",
|
|
2219
|
-
"company-registration": "COMPANY_REG"
|
|
2220
|
-
};
|
|
2221
|
-
function tokenize(text, options = {}) {
|
|
2222
|
-
if (typeof text !== "string") throw new ValidationError("text", "must be a string");
|
|
2223
|
-
const {
|
|
2224
|
-
categories,
|
|
2225
|
-
format = "bracket",
|
|
2226
|
-
customDetectors,
|
|
2227
|
-
aggressive = false,
|
|
2228
|
-
confidenceThreshold = 0,
|
|
2229
|
-
allowlist = [],
|
|
2230
|
-
allowlistCaseSensitive = false
|
|
2231
|
-
} = options;
|
|
2232
|
-
const compiledAllowlist = allowlist.map(
|
|
2233
|
-
(entry) => new RegExp(entry.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), allowlistCaseSensitive ? "" : "i")
|
|
2234
|
-
);
|
|
2235
|
-
const isAllowlisted = (v) => compiledAllowlist.some((re) => re.test(v));
|
|
2236
|
-
const allMatches = detect(text, categories, customDetectors, aggressive).filter(
|
|
2237
|
-
(m) => m.confidence >= confidenceThreshold && !isAllowlisted(m.value)
|
|
2238
|
-
);
|
|
2239
|
-
const store = createTokenStore();
|
|
2240
|
-
let result = text;
|
|
2241
|
-
const reversed = [...allMatches].reverse();
|
|
2242
|
-
const tokenMatches = [];
|
|
2243
|
-
for (const match of reversed) {
|
|
2244
|
-
const prefix = TOKEN_PREFIX_MAP2[match.category] ?? match.category.toUpperCase();
|
|
2245
|
-
const token = assignToken(store, match.category, match.value, prefix, format);
|
|
2246
|
-
tokenMatches.push({
|
|
2247
|
-
token,
|
|
2248
|
-
original: match.value,
|
|
2249
|
-
category: match.category,
|
|
2250
|
-
start: match.start,
|
|
2251
|
-
end: match.end
|
|
2252
|
-
});
|
|
2253
|
-
result = result.slice(0, match.start) + token + result.slice(match.end);
|
|
2254
|
-
}
|
|
2255
|
-
return { text: result, mapping: store.tokens, tokens: tokenMatches };
|
|
2256
|
-
}
|
|
3
|
+
var chunk26IHMQHL_cjs = require('./chunk-26IHMQHL.cjs');
|
|
4
|
+
require('./chunk-CRXRJZEV.cjs');
|
|
5
|
+
require('./chunk-IHEIYZPL.cjs');
|
|
6
|
+
require('./chunk-UA3EOFBS.cjs');
|
|
7
|
+
require('./chunk-GYMG5OI3.cjs');
|
|
8
|
+
var chunkWLJN7GIS_cjs = require('./chunk-WLJN7GIS.cjs');
|
|
2257
9
|
|
|
2258
10
|
// src/stream.ts
|
|
2259
|
-
init_errors();
|
|
2260
11
|
function requireTransformStream() {
|
|
2261
12
|
if (typeof TransformStream === "undefined") {
|
|
2262
|
-
throw new ValidationError(
|
|
13
|
+
throw new chunkWLJN7GIS_cjs.ValidationError(
|
|
2263
14
|
"TransformStream",
|
|
2264
15
|
"is not available in this environment. Use Node.js \u2265 18 or a polyfill."
|
|
2265
16
|
);
|
|
@@ -2271,10 +22,10 @@ function createAnonymizeStream(options = {}) {
|
|
|
2271
22
|
return new TS({
|
|
2272
23
|
transform(chunk, controller) {
|
|
2273
24
|
if (typeof chunk !== "string") {
|
|
2274
|
-
controller.error(new ValidationError("chunk", "must be a string"));
|
|
25
|
+
controller.error(new chunkWLJN7GIS_cjs.ValidationError("chunk", "must be a string"));
|
|
2275
26
|
return;
|
|
2276
27
|
}
|
|
2277
|
-
controller.enqueue(anonymize(chunk, options));
|
|
28
|
+
controller.enqueue(chunk26IHMQHL_cjs.anonymize(chunk, options));
|
|
2278
29
|
}
|
|
2279
30
|
});
|
|
2280
31
|
}
|
|
@@ -2283,11 +34,11 @@ function createAnonymizeStreamAsync(options = {}) {
|
|
|
2283
34
|
return new TS({
|
|
2284
35
|
async transform(chunk, controller) {
|
|
2285
36
|
if (typeof chunk !== "string") {
|
|
2286
|
-
controller.error(new ValidationError("chunk", "must be a string"));
|
|
37
|
+
controller.error(new chunkWLJN7GIS_cjs.ValidationError("chunk", "must be a string"));
|
|
2287
38
|
return;
|
|
2288
39
|
}
|
|
2289
40
|
try {
|
|
2290
|
-
const result = await anonymizeAsync(chunk, options);
|
|
41
|
+
const result = await chunk26IHMQHL_cjs.anonymizeAsync(chunk, options);
|
|
2291
42
|
controller.enqueue(result);
|
|
2292
43
|
} catch (err) {
|
|
2293
44
|
controller.error(err);
|
|
@@ -2300,10 +51,10 @@ function createTokenizeStream(options = {}) {
|
|
|
2300
51
|
return new TS({
|
|
2301
52
|
transform(chunk, controller) {
|
|
2302
53
|
if (typeof chunk !== "string") {
|
|
2303
|
-
controller.error(new ValidationError("chunk", "must be a string"));
|
|
54
|
+
controller.error(new chunkWLJN7GIS_cjs.ValidationError("chunk", "must be a string"));
|
|
2304
55
|
return;
|
|
2305
56
|
}
|
|
2306
|
-
controller.enqueue(tokenize(chunk, options));
|
|
57
|
+
controller.enqueue(chunk26IHMQHL_cjs.tokenize(chunk, options));
|
|
2307
58
|
}
|
|
2308
59
|
});
|
|
2309
60
|
}
|