@stll/anonymize 2.8.3 → 2.9.1

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.
@@ -1,4 +1,5 @@
1
1
  import { PreparedNativeAnonymizer, PreparedNativePipeline, assertNativeBindingVersion, convert_external_detection_batch as convert_external_detection_batch$1, createNativePipelineFromPackage, diagnostics_json as diagnostics_json$1, diagnostics_stream_json as diagnostics_stream_json$1, isNativeAnonymizeBinding, load_prepared_package as load_prepared_package$1, native_package_version as native_package_version$1, normalize_for_search as normalize_for_search$1, prepare_search_package as prepare_search_package$1, redact_text as redact_text$1, redact_text_json as redact_text_json$1, redact_text_stream_json as redact_text_stream_json$1, summary_diagnostics_json as summary_diagnostics_json$1 } from "./native.mjs";
2
+ import { n as applyPipelineLanguageScope, r as language_scopes_default, t as defaultDictionaryBundleOptions } from "./build-native-package2.mjs";
2
3
  import { DEFAULT_ENTITY_LABELS } from "./constants.mjs";
3
4
  import { createRequire } from "node:module";
4
5
  import { existsSync, readFileSync, readdirSync } from "node:fs";
@@ -44,146 +45,6 @@ const createPipelineContext = () => ({
44
45
  */
45
46
  const defaultContext = createPipelineContext();
46
47
  //#endregion
47
- //#region src/language-scope.ts
48
- const scopeData = {
49
- _comment: "Default dictionary scopes for content language hints. Lower-level caller config can still override name corpus languages and deny-list countries independently.",
50
- languages: {
51
- "cs": {
52
- "nameCorpusLanguages": ["cs", "sk"],
53
- "denyListCountries": ["CZ", "SK"]
54
- },
55
- "de": {
56
- "nameCorpusLanguages": ["de"],
57
- "denyListCountries": [
58
- "DE",
59
- "AT",
60
- "CH"
61
- ]
62
- },
63
- "en": {
64
- "nameCorpusLanguages": ["en"],
65
- "denyListCountries": [
66
- "US",
67
- "GB",
68
- "CA",
69
- "AU",
70
- "IE"
71
- ]
72
- },
73
- "es": {
74
- "nameCorpusLanguages": ["es"],
75
- "denyListCountries": [
76
- "ES",
77
- "MX",
78
- "AR",
79
- "CL",
80
- "CO",
81
- "PE",
82
- "EC",
83
- "VE",
84
- "UY",
85
- "PY",
86
- "BO",
87
- "CR",
88
- "PA",
89
- "DO",
90
- "GT",
91
- "HN",
92
- "SV",
93
- "NI",
94
- "CU"
95
- ]
96
- },
97
- "fr": {
98
- "nameCorpusLanguages": ["fr"],
99
- "denyListCountries": [
100
- "FR",
101
- "BE",
102
- "CH",
103
- "CA",
104
- "LU",
105
- "MC"
106
- ]
107
- },
108
- "hu": {
109
- "nameCorpusLanguages": ["hu"],
110
- "denyListCountries": ["HU"]
111
- },
112
- "it": {
113
- "nameCorpusLanguages": ["it"],
114
- "denyListCountries": ["IT", "CH"]
115
- },
116
- "lv": {
117
- "nameCorpusLanguages": [],
118
- "denyListCountries": ["LV"]
119
- },
120
- "pl": {
121
- "nameCorpusLanguages": ["pl"],
122
- "denyListCountries": ["PL"]
123
- },
124
- "pt-br": {
125
- "nameCorpusLanguages": ["pt-br"],
126
- "denyListCountries": ["BR"]
127
- },
128
- "ro": {
129
- "nameCorpusLanguages": ["ro"],
130
- "denyListCountries": ["RO", "MD"]
131
- },
132
- "sk": {
133
- "nameCorpusLanguages": ["sk", "cs"],
134
- "denyListCountries": ["SK", "CZ"]
135
- },
136
- "sv": {
137
- "nameCorpusLanguages": ["sv"],
138
- "denyListCountries": ["SE", "FI"]
139
- }
140
- }
141
- };
142
- const normalizeLanguage = (language) => language.trim().toLowerCase();
143
- const fallbackLanguage = (language) => {
144
- const index = language.indexOf("-");
145
- return index === -1 ? null : language.slice(0, index);
146
- };
147
- const uniquePush = (target, values) => {
148
- const seen = new Set(target);
149
- for (const value of values) {
150
- if (seen.has(value)) continue;
151
- seen.add(value);
152
- target.push(value);
153
- }
154
- };
155
- const resolveLanguageScope = (language) => {
156
- const normalized = normalizeLanguage(language);
157
- if (normalized.length === 0) return null;
158
- const exact = scopeData.languages[normalized];
159
- if (exact !== void 0) return exact;
160
- const fallback = fallbackLanguage(normalized);
161
- return fallback === null ? null : scopeData.languages[fallback] ?? null;
162
- };
163
- const configuredLanguages = (config) => {
164
- if (config.languages !== void 0) return config.languages;
165
- return config.language === void 0 ? [] : [config.language];
166
- };
167
- const applyPipelineLanguageScope = (config) => {
168
- const languages = configuredLanguages(config);
169
- if (languages.length === 0) return config;
170
- const nameCorpusLanguages = [];
171
- const denyListCountries = [];
172
- for (const language of languages) {
173
- const scope = resolveLanguageScope(language);
174
- if (scope === null) continue;
175
- uniquePush(nameCorpusLanguages, scope.nameCorpusLanguages ?? []);
176
- uniquePush(denyListCountries, scope.denyListCountries ?? []);
177
- }
178
- const next = {};
179
- if (config.nameCorpusLanguages === void 0 && nameCorpusLanguages.length > 0) next.nameCorpusLanguages = nameCorpusLanguages;
180
- if (config.denyListCountries === void 0 && denyListCountries.length > 0) next.denyListCountries = denyListCountries;
181
- return Object.keys(next).length === 0 ? config : {
182
- ...config,
183
- ...next
184
- };
185
- };
186
- //#endregion
187
48
  //#region src/types.ts
188
49
  const isLegalFormsEnabled = (config) => config.enableLegalForms !== false;
189
50
  //#endregion
@@ -198,7 +59,8 @@ const languageSelectionKey = (languages) => {
198
59
  //#region src/pipeline-cache-key.ts
199
60
  const DEFAULT_CUSTOM_REGEX_SCORE = .9;
200
61
  const contentLanguageFingerprint = (config) => {
201
- return languageSelectionKey(config.languages ?? (config.language === void 0 ? [] : [config.language]));
62
+ const languages = config.languages ?? (config.language === void 0 ? [] : [config.language]);
63
+ return languageSelectionKey(languages);
202
64
  };
203
65
  const pipelineConfigKey = (config, gazetteerEntries) => {
204
66
  const legalFormsEnabled = isLegalFormsEnabled(config);
@@ -308,14 +170,15 @@ const prepareNativePipelinePackage = async ({ binding, config, gazetteerEntries
308
170
  return new Uint8Array(packageBytes);
309
171
  };
310
172
  const createNativePipelineFromConfig = async ({ binding, config, gazetteerEntries = [], context }) => {
173
+ const packageBytes = await getCachedNativePipelinePackage({
174
+ binding,
175
+ config,
176
+ gazetteerEntries,
177
+ ...context ? { context } : {}
178
+ });
311
179
  return createNativePipelineFromPackage({
312
180
  binding,
313
- packageBytes: await getCachedNativePipelinePackage({
314
- binding,
315
- config,
316
- gazetteerEntries,
317
- ...context ? { context } : {}
318
- })
181
+ packageBytes
319
182
  });
320
183
  };
321
184
  const getCachedNativePipelinePackage = async ({ binding, config, gazetteerEntries = [], context, compressed = false }) => {
@@ -395,6 +258,98 @@ const DEFAULT_NATIVE_PIPELINE_CONFIG = {
395
258
  workspaceId: "native-pipeline-default"
396
259
  };
397
260
  //#endregion
261
+ //#region src/pipeline-language.ts
262
+ const isSupportedLanguage = (language) => Object.hasOwn(language_scopes_default.languages, language);
263
+ const SUPPORTED_LANGUAGES = Object.freeze(Object.keys(language_scopes_default.languages).filter(isSupportedLanguage).toSorted());
264
+ const normalizeLanguage = (language) => {
265
+ if (typeof language !== "string") throw new TypeError("Pipeline language codes must be strings");
266
+ const normalized = language.trim().toLowerCase();
267
+ if (!isSupportedLanguage(normalized)) throw new RangeError(`Unsupported pipeline language ${JSON.stringify(language)}; expected one of: ${SUPPORTED_LANGUAGES.join(", ")}`);
268
+ return normalized;
269
+ };
270
+ const normalizePipelineLanguageSelection = (selection) => {
271
+ if (selection === void 0 || typeof selection === "string" && selection.trim().toLowerCase() === "all") return { type: "all" };
272
+ const requested = Array.isArray(selection) ? selection : [selection];
273
+ if (requested.length === 0) throw new RangeError("Pipeline language selection must not be empty");
274
+ const normalized = [...new Set(requested.map(normalizeLanguage))].toSorted();
275
+ const first = normalized.at(0);
276
+ if (first === void 0) throw new RangeError("Pipeline language selection must not be empty");
277
+ return {
278
+ type: "languages",
279
+ languages: [first, ...normalized.slice(1)]
280
+ };
281
+ };
282
+ const pipelineLanguageSelectionKey = (selection) => selection.type === "all" ? "all" : selection.languages.join(",");
283
+ //#endregion
284
+ //#region src/create-pipeline.ts
285
+ const dictionaryCache = /* @__PURE__ */ new Map();
286
+ const semanticPipelineCache = /* @__PURE__ */ new WeakMap();
287
+ const MAX_SEMANTIC_PIPELINE_CACHE_ENTRIES = 8;
288
+ const getCachedEntry = (cache, key) => {
289
+ const cached = cache.get(key);
290
+ if (cached === void 0) return;
291
+ cache.delete(key);
292
+ cache.set(key, cached);
293
+ return cached;
294
+ };
295
+ const setCachedEntry = (cache, key, value) => {
296
+ cache.set(key, value);
297
+ if (cache.size <= MAX_SEMANTIC_PIPELINE_CACHE_ENTRIES) return;
298
+ const oldestKey = cache.keys().next().value;
299
+ if (oldestKey !== void 0) cache.delete(oldestKey);
300
+ };
301
+ const loadSemanticDictionaries = (key, config) => {
302
+ const cached = getCachedEntry(dictionaryCache, key);
303
+ if (cached !== void 0) return cached;
304
+ let dictionaries;
305
+ dictionaries = import("@stll/anonymize-data/cities").then(({ loadDictionaryBundle }) => loadDictionaryBundle(defaultDictionaryBundleOptions(config))).catch((error) => {
306
+ if (dictionaryCache.get(key) === dictionaries) dictionaryCache.delete(key);
307
+ throw error;
308
+ });
309
+ setCachedEntry(dictionaryCache, key, dictionaries);
310
+ return dictionaries;
311
+ };
312
+ const pipelineConfigFor = (selection) => {
313
+ if (selection.type === "all") return {
314
+ ...DEFAULT_NATIVE_PIPELINE_CONFIG,
315
+ labels: [...DEFAULT_NATIVE_PIPELINE_CONFIG.labels]
316
+ };
317
+ const [language, ...languages] = selection.languages;
318
+ return applyPipelineLanguageScope({
319
+ ...DEFAULT_NATIVE_PIPELINE_CONFIG,
320
+ labels: [...DEFAULT_NATIVE_PIPELINE_CONFIG.labels],
321
+ workspaceId: `default-pipeline:${pipelineLanguageSelectionKey(selection)}`,
322
+ ...languages.length === 0 ? { language } : { languages: [language, ...languages] }
323
+ });
324
+ };
325
+ const semanticPipelineCacheFor = (binding) => {
326
+ const cached = semanticPipelineCache.get(binding);
327
+ if (cached !== void 0) return cached;
328
+ const created = /* @__PURE__ */ new Map();
329
+ semanticPipelineCache.set(binding, created);
330
+ return created;
331
+ };
332
+ const createSemanticPipeline = ({ binding, selection }) => {
333
+ const key = pipelineLanguageSelectionKey(selection);
334
+ const cache = semanticPipelineCacheFor(binding);
335
+ const cached = getCachedEntry(cache, key);
336
+ if (cached !== void 0) return cached;
337
+ const config = pipelineConfigFor(selection);
338
+ let pipeline;
339
+ pipeline = loadSemanticDictionaries(key, config).then((dictionaries) => createNativePipelineFromConfig({
340
+ binding,
341
+ config: {
342
+ ...config,
343
+ dictionaries
344
+ }
345
+ })).catch((error) => {
346
+ if (cache.get(key) === pipeline) cache.delete(key);
347
+ throw error;
348
+ });
349
+ setCachedEntry(cache, key, pipeline);
350
+ return pipeline;
351
+ };
352
+ //#endregion
398
353
  //#region src/native-node.ts
399
354
  const DEFAULT_NATIVE_PIPELINE_WARMUPS = {
400
355
  lazyRegex: "lazy-regex",
@@ -482,10 +437,11 @@ const convert_external_detection_batch = (document, batch, options = {}) => conv
482
437
  batch
483
438
  });
484
439
  const normalize_for_search = (text, options = {}) => {
485
- return normalize_for_search$1({
440
+ const args = {
486
441
  binding: resolveNativeSdkBinding(options),
487
442
  text
488
- });
443
+ };
444
+ return normalize_for_search$1(args);
489
445
  };
490
446
  const prepare_search_package = (config, { compressed = false, ...options } = {}) => prepare_search_package$1({
491
447
  binding: resolveNativeSdkBinding(options),
@@ -595,6 +551,26 @@ const getDefaultNativePipeline = (options = {}) => {
595
551
  return applyDefaultNativePipelineWarmup(pipeline, resolvedOptions.warmup);
596
552
  };
597
553
  const get_default_native_pipeline = (options = {}) => getDefaultNativePipeline(options);
554
+ const createPipeline = async ({ language, warmup, ...bindingOptions } = {}) => {
555
+ const selection = normalizePipelineLanguageSelection(language);
556
+ if (selection.type === "all") return getDefaultNativePipeline({
557
+ ...bindingOptions,
558
+ ...warmup !== void 0 ? { warmup } : {}
559
+ });
560
+ const [singleLanguage, ...additionalLanguages] = selection.languages;
561
+ if (additionalLanguages.length === 0 && existsSync(defaultNativePipelineLanguagePackageUrl(singleLanguage))) return getDefaultNativePipeline({
562
+ ...bindingOptions,
563
+ language: singleLanguage,
564
+ ...warmup !== void 0 ? { warmup } : {}
565
+ });
566
+ const resolvedWarmup = normalizeDefaultNativePipelineWarmup(warmup);
567
+ const pipeline = await createSemanticPipeline({
568
+ binding: resolveNativeSdkBinding(bindingOptions),
569
+ selection
570
+ });
571
+ return applyDefaultNativePipelineWarmup(pipeline, resolvedWarmup);
572
+ };
573
+ const create_pipeline = createPipeline;
598
574
  const preloadDefaultNativePipeline = (options = {}) => {
599
575
  const pipeline = getDefaultNativePipeline(options);
600
576
  return applyDefaultNativePipelineWarmup(pipeline, DEFAULT_NATIVE_PIPELINE_WARMUPS.lazyRegex);
@@ -702,10 +678,12 @@ const defaultNativePipelinePackageUrl = (language) => {
702
678
  const defaultNativePipelineLanguagePackageUrl = (language) => new URL(`../native-pipeline.${language}.stlanonpkg`, import.meta.url);
703
679
  const resolveDefaultNativePipelineLanguage = (language) => {
704
680
  const normalized = normalizeDefaultNativePipelineLanguage(language);
705
- if (existsSync(defaultNativePipelineLanguagePackageUrl(normalized))) return normalized;
681
+ const exactUrl = defaultNativePipelineLanguagePackageUrl(normalized);
682
+ if (existsSync(exactUrl)) return normalized;
706
683
  const baseLanguage = normalized.split("-").at(0);
707
684
  if (baseLanguage === void 0 || baseLanguage === normalized) return normalized;
708
- if (existsSync(defaultNativePipelineLanguagePackageUrl(baseLanguage))) return baseLanguage;
685
+ const baseUrl = defaultNativePipelineLanguagePackageUrl(baseLanguage);
686
+ if (existsSync(baseUrl)) return baseLanguage;
709
687
  return normalized;
710
688
  };
711
689
  const defaultNativePipelinePackageDescription = (language) => language === void 0 ? "Default native pipeline package" : `Default native pipeline package for language "${resolveDefaultNativePipelineLanguage(language)}"`;
@@ -797,6 +775,6 @@ const formatLoadError = (error) => {
797
775
  return String(error);
798
776
  };
799
777
  //#endregion
800
- export { redact_text as A, prepareNativePipelinePackage as B, readNativePipelinePackageFile as C, redactDefaultTextJson as D, redactDefaultText as E, DEFAULT_NATIVE_PIPELINE_CONFIG as F, assertNativePipelineSupported as I, createNativePipelineFromConfig as L, redact_text_stream_json as M, setNativeBindingOverride as N, redact_default_text as O, summary_diagnostics_json as P, getNativePipelineCompatibility as R, readDefaultNativePipelinePackageFileAsync as S, read_default_native_pipeline_package_file as T, createPipelineContext as V, preloadDefaultNativePipeline as _, createNativePipelineFromDefaultPackage as a, prepare_search_package as b, diagnostics_json as c, get_default_native_pipeline as d, loadNativeAnonymizeBinding as f, normalize_for_search as g, native_package_version as h, convert_external_detection_batch as i, redact_text_json as j, redact_default_text_json as k, diagnostics_stream_json as l, load_prepared_package_file as m, availableDefaultNativePipelineLanguages as n, createNativePipelineFromPackageFile as o, load_prepared_package as p, available_default_native_pipeline_languages as r, create_native_pipeline_from_default_package as s, DEFAULT_NATIVE_PIPELINE_WARMUPS as t, getDefaultNativePipeline as u, preloadDefaultNativePipelineAsync as v, readNativePipelinePackageFileAsync as w, readDefaultNativePipelinePackageFile as x, preload_default_native_pipeline as y, prepareNativePipelineConfig as z };
778
+ export { redact_default_text as A, createNativePipelineFromConfig as B, readDefaultNativePipelinePackageFile as C, read_default_native_pipeline_package_file as D, readNativePipelinePackageFileAsync as E, setNativeBindingOverride as F, prepareNativePipelineConfig as H, summary_diagnostics_json as I, SUPPORTED_LANGUAGES as L, redact_text as M, redact_text_json as N, redactDefaultText as O, redact_text_stream_json as P, DEFAULT_NATIVE_PIPELINE_CONFIG as R, prepare_search_package as S, readNativePipelinePackageFile as T, prepareNativePipelinePackage as U, getNativePipelineCompatibility as V, createPipelineContext as W, native_package_version as _, createNativePipelineFromDefaultPackage as a, preloadDefaultNativePipelineAsync as b, create_native_pipeline_from_default_package as c, diagnostics_stream_json as d, getDefaultNativePipeline as f, load_prepared_package_file as g, load_prepared_package as h, convert_external_detection_batch as i, redact_default_text_json as j, redactDefaultTextJson as k, create_pipeline as l, loadNativeAnonymizeBinding as m, availableDefaultNativePipelineLanguages as n, createNativePipelineFromPackageFile as o, get_default_native_pipeline as p, available_default_native_pipeline_languages as r, createPipeline as s, DEFAULT_NATIVE_PIPELINE_WARMUPS as t, diagnostics_json as u, normalize_for_search as v, readDefaultNativePipelinePackageFileAsync as w, preload_default_native_pipeline as x, preloadDefaultNativePipeline as y, assertNativePipelineSupported as z };
801
779
 
802
780
  //# sourceMappingURL=native-node2.mjs.map