@stll/anonymize 2.8.3 → 2.9.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.
@@ -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
@@ -395,6 +256,98 @@ const DEFAULT_NATIVE_PIPELINE_CONFIG = {
395
256
  workspaceId: "native-pipeline-default"
396
257
  };
397
258
  //#endregion
259
+ //#region src/pipeline-language.ts
260
+ const isSupportedLanguage = (language) => Object.hasOwn(language_scopes_default.languages, language);
261
+ const SUPPORTED_LANGUAGES = Object.freeze(Object.keys(language_scopes_default.languages).filter(isSupportedLanguage).toSorted());
262
+ const normalizeLanguage = (language) => {
263
+ if (typeof language !== "string") throw new TypeError("Pipeline language codes must be strings");
264
+ const normalized = language.trim().toLowerCase();
265
+ if (!isSupportedLanguage(normalized)) throw new RangeError(`Unsupported pipeline language ${JSON.stringify(language)}; expected one of: ${SUPPORTED_LANGUAGES.join(", ")}`);
266
+ return normalized;
267
+ };
268
+ const normalizePipelineLanguageSelection = (selection) => {
269
+ if (selection === void 0 || typeof selection === "string" && selection.trim().toLowerCase() === "all") return { type: "all" };
270
+ const requested = Array.isArray(selection) ? selection : [selection];
271
+ if (requested.length === 0) throw new RangeError("Pipeline language selection must not be empty");
272
+ const normalized = [...new Set(requested.map(normalizeLanguage))].toSorted();
273
+ const first = normalized.at(0);
274
+ if (first === void 0) throw new RangeError("Pipeline language selection must not be empty");
275
+ return {
276
+ type: "languages",
277
+ languages: [first, ...normalized.slice(1)]
278
+ };
279
+ };
280
+ const pipelineLanguageSelectionKey = (selection) => selection.type === "all" ? "all" : selection.languages.join(",");
281
+ //#endregion
282
+ //#region src/create-pipeline.ts
283
+ const dictionaryCache = /* @__PURE__ */ new Map();
284
+ const semanticPipelineCache = /* @__PURE__ */ new WeakMap();
285
+ const MAX_SEMANTIC_PIPELINE_CACHE_ENTRIES = 8;
286
+ const getCachedEntry = (cache, key) => {
287
+ const cached = cache.get(key);
288
+ if (cached === void 0) return;
289
+ cache.delete(key);
290
+ cache.set(key, cached);
291
+ return cached;
292
+ };
293
+ const setCachedEntry = (cache, key, value) => {
294
+ cache.set(key, value);
295
+ if (cache.size <= MAX_SEMANTIC_PIPELINE_CACHE_ENTRIES) return;
296
+ const oldestKey = cache.keys().next().value;
297
+ if (oldestKey !== void 0) cache.delete(oldestKey);
298
+ };
299
+ const loadSemanticDictionaries = (key, config) => {
300
+ const cached = getCachedEntry(dictionaryCache, key);
301
+ if (cached !== void 0) return cached;
302
+ let dictionaries;
303
+ dictionaries = import("@stll/anonymize-data/cities").then(({ loadDictionaryBundle }) => loadDictionaryBundle(defaultDictionaryBundleOptions(config))).catch((error) => {
304
+ if (dictionaryCache.get(key) === dictionaries) dictionaryCache.delete(key);
305
+ throw error;
306
+ });
307
+ setCachedEntry(dictionaryCache, key, dictionaries);
308
+ return dictionaries;
309
+ };
310
+ const pipelineConfigFor = (selection) => {
311
+ if (selection.type === "all") return {
312
+ ...DEFAULT_NATIVE_PIPELINE_CONFIG,
313
+ labels: [...DEFAULT_NATIVE_PIPELINE_CONFIG.labels]
314
+ };
315
+ const [language, ...languages] = selection.languages;
316
+ return applyPipelineLanguageScope({
317
+ ...DEFAULT_NATIVE_PIPELINE_CONFIG,
318
+ labels: [...DEFAULT_NATIVE_PIPELINE_CONFIG.labels],
319
+ workspaceId: `default-pipeline:${pipelineLanguageSelectionKey(selection)}`,
320
+ ...languages.length === 0 ? { language } : { languages: [language, ...languages] }
321
+ });
322
+ };
323
+ const semanticPipelineCacheFor = (binding) => {
324
+ const cached = semanticPipelineCache.get(binding);
325
+ if (cached !== void 0) return cached;
326
+ const created = /* @__PURE__ */ new Map();
327
+ semanticPipelineCache.set(binding, created);
328
+ return created;
329
+ };
330
+ const createSemanticPipeline = ({ binding, selection }) => {
331
+ const key = pipelineLanguageSelectionKey(selection);
332
+ const cache = semanticPipelineCacheFor(binding);
333
+ const cached = getCachedEntry(cache, key);
334
+ if (cached !== void 0) return cached;
335
+ const config = pipelineConfigFor(selection);
336
+ let pipeline;
337
+ pipeline = loadSemanticDictionaries(key, config).then((dictionaries) => createNativePipelineFromConfig({
338
+ binding,
339
+ config: {
340
+ ...config,
341
+ dictionaries
342
+ }
343
+ })).catch((error) => {
344
+ if (cache.get(key) === pipeline) cache.delete(key);
345
+ throw error;
346
+ });
347
+ setCachedEntry(cache, key, pipeline);
348
+ return pipeline;
349
+ };
350
+ //#endregion
398
351
  //#region src/native-node.ts
399
352
  const DEFAULT_NATIVE_PIPELINE_WARMUPS = {
400
353
  lazyRegex: "lazy-regex",
@@ -595,6 +548,26 @@ const getDefaultNativePipeline = (options = {}) => {
595
548
  return applyDefaultNativePipelineWarmup(pipeline, resolvedOptions.warmup);
596
549
  };
597
550
  const get_default_native_pipeline = (options = {}) => getDefaultNativePipeline(options);
551
+ const createPipeline = async ({ language, warmup, ...bindingOptions } = {}) => {
552
+ const selection = normalizePipelineLanguageSelection(language);
553
+ if (selection.type === "all") return getDefaultNativePipeline({
554
+ ...bindingOptions,
555
+ ...warmup !== void 0 ? { warmup } : {}
556
+ });
557
+ const [singleLanguage, ...additionalLanguages] = selection.languages;
558
+ if (additionalLanguages.length === 0 && existsSync(defaultNativePipelineLanguagePackageUrl(singleLanguage))) return getDefaultNativePipeline({
559
+ ...bindingOptions,
560
+ language: singleLanguage,
561
+ ...warmup !== void 0 ? { warmup } : {}
562
+ });
563
+ const resolvedWarmup = normalizeDefaultNativePipelineWarmup(warmup);
564
+ const pipeline = await createSemanticPipeline({
565
+ binding: resolveNativeSdkBinding(bindingOptions),
566
+ selection
567
+ });
568
+ return applyDefaultNativePipelineWarmup(pipeline, resolvedWarmup);
569
+ };
570
+ const create_pipeline = createPipeline;
598
571
  const preloadDefaultNativePipeline = (options = {}) => {
599
572
  const pipeline = getDefaultNativePipeline(options);
600
573
  return applyDefaultNativePipelineWarmup(pipeline, DEFAULT_NATIVE_PIPELINE_WARMUPS.lazyRegex);
@@ -797,6 +770,6 @@ const formatLoadError = (error) => {
797
770
  return String(error);
798
771
  };
799
772
  //#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 };
773
+ 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
774
 
802
775
  //# sourceMappingURL=native-node2.mjs.map