@stll/anonymize 1.5.0 → 2.0.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/ATTRIBUTION.md +70 -0
- package/README.md +86 -48
- package/dist/index.d.mts +3 -1201
- package/dist/index.mjs +3 -16267
- package/dist/index.mjs.map +1 -1
- package/dist/native-node.d.mts +138 -0
- package/dist/native-node.mjs +3 -0
- package/dist/native-node2.d.mts +3 -0
- package/dist/native-node2.mjs +728 -0
- package/dist/native-node2.mjs.map +1 -0
- package/dist/native.d.mts +970 -0
- package/dist/native.mjs +230 -0
- package/dist/native.mjs.map +1 -0
- package/dist/native2.d.mts +2 -0
- package/index.cjs +3 -0
- package/native-pipeline.cs.stlanonpkg +0 -0
- package/native-pipeline.de.stlanonpkg +0 -0
- package/native-pipeline.en.stlanonpkg +0 -0
- package/native-pipeline.stlanonpkg +0 -0
- package/package.json +57 -9
- package/scripts/build-native-pipeline-package.mjs +225 -0
- package/dist/address-boundaries.mjs +0 -195
- package/dist/address-boundaries.mjs.map +0 -1
- package/dist/address-prepositions.mjs +0 -182
- package/dist/address-prepositions.mjs.map +0 -1
- package/dist/address-stop-keywords.mjs +0 -137
- package/dist/address-stop-keywords.mjs.map +0 -1
- package/dist/address-stopwords.mjs +0 -84
- package/dist/address-stopwords.mjs.map +0 -1
- package/dist/allow-list.mjs +0 -196
- package/dist/allow-list.mjs.map +0 -1
- package/dist/clause-noun-heads.mjs +0 -75
- package/dist/clause-noun-heads.mjs.map +0 -1
- package/dist/common-words-en.mjs +0 -9887
- package/dist/common-words-en.mjs.map +0 -1
- package/dist/coreference.cs.mjs +0 -14
- package/dist/coreference.cs.mjs.map +0 -1
- package/dist/coreference.de.mjs +0 -14
- package/dist/coreference.de.mjs.map +0 -1
- package/dist/coreference.en.mjs +0 -14
- package/dist/coreference.en.mjs.map +0 -1
- package/dist/coreference.es.mjs +0 -22
- package/dist/coreference.es.mjs.map +0 -1
- package/dist/coreference.fr.mjs +0 -32
- package/dist/coreference.fr.mjs.map +0 -1
- package/dist/coreference.it.mjs +0 -27
- package/dist/coreference.it.mjs.map +0 -1
- package/dist/coreference.pl.mjs +0 -27
- package/dist/coreference.pl.mjs.map +0 -1
- package/dist/coreference.pt-br.mjs +0 -14
- package/dist/coreference.pt-br.mjs.map +0 -1
- package/dist/coreference.sk.mjs +0 -27
- package/dist/coreference.sk.mjs.map +0 -1
- package/dist/currencies.mjs +0 -231
- package/dist/currencies.mjs.map +0 -1
- package/dist/date-months.mjs +0 -618
- package/dist/date-months.mjs.map +0 -1
- package/dist/document-structure-headings.mjs +0 -90
- package/dist/document-structure-headings.mjs.map +0 -1
- package/dist/generic-roles.mjs +0 -244
- package/dist/generic-roles.mjs.map +0 -1
- package/dist/hotword-rules.mjs +0 -149
- package/dist/hotword-rules.mjs.map +0 -1
- package/dist/legal-form-leading-clauses.mjs +0 -23
- package/dist/legal-form-leading-clauses.mjs.map +0 -1
- package/dist/legal-forms.mjs +0 -2115
- package/dist/legal-forms.mjs.map +0 -1
- package/dist/legal-role-heads.cs.mjs +0 -42
- package/dist/legal-role-heads.cs.mjs.map +0 -1
- package/dist/legal-role-heads.de.mjs +0 -33
- package/dist/legal-role-heads.de.mjs.map +0 -1
- package/dist/legal-role-heads.en.mjs +0 -37
- package/dist/legal-role-heads.en.mjs.map +0 -1
- package/dist/legal-role-heads.es.mjs +0 -54
- package/dist/legal-role-heads.es.mjs.map +0 -1
- package/dist/legal-role-heads.fr.mjs +0 -72
- package/dist/legal-role-heads.fr.mjs.map +0 -1
- package/dist/legal-role-heads.it.mjs +0 -68
- package/dist/legal-role-heads.it.mjs.map +0 -1
- package/dist/legal-role-heads.pl.mjs +0 -84
- package/dist/legal-role-heads.pl.mjs.map +0 -1
- package/dist/legal-role-heads.pt-br.mjs +0 -63
- package/dist/legal-role-heads.pt-br.mjs.map +0 -1
- package/dist/legal-role-heads.sk.mjs +0 -80
- package/dist/legal-role-heads.sk.mjs.map +0 -1
- package/dist/manifest.mjs +0 -69
- package/dist/manifest.mjs.map +0 -1
- package/dist/names-exclusions.mjs +0 -223
- package/dist/names-exclusions.mjs.map +0 -1
- package/dist/names-first.mjs +0 -418
- package/dist/names-first.mjs.map +0 -1
- package/dist/names-nw-ar.mjs +0 -202
- package/dist/names-nw-ar.mjs.map +0 -1
- package/dist/names-nw-excluded-allcaps.mjs +0 -112
- package/dist/names-nw-excluded-allcaps.mjs.map +0 -1
- package/dist/names-nw-fil.mjs +0 -202
- package/dist/names-nw-fil.mjs.map +0 -1
- package/dist/names-nw-id.mjs +0 -210
- package/dist/names-nw-id.mjs.map +0 -1
- package/dist/names-nw-in.mjs +0 -526
- package/dist/names-nw-in.mjs.map +0 -1
- package/dist/names-nw-ja-latn.mjs +0 -260
- package/dist/names-nw-ja-latn.mjs.map +0 -1
- package/dist/names-nw-ko.mjs +0 -162
- package/dist/names-nw-ko.mjs.map +0 -1
- package/dist/names-nw-th.mjs +0 -188
- package/dist/names-nw-th.mjs.map +0 -1
- package/dist/names-nw-vi.mjs +0 -151
- package/dist/names-nw-vi.mjs.map +0 -1
- package/dist/names-nw-zh-latn.mjs +0 -197
- package/dist/names-nw-zh-latn.mjs.map +0 -1
- package/dist/names-surnames.mjs +0 -113
- package/dist/names-surnames.mjs.map +0 -1
- package/dist/names-title-tokens.mjs +0 -40
- package/dist/names-title-tokens.mjs.map +0 -1
- package/dist/person-stopwords.mjs +0 -205
- package/dist/person-stopwords.mjs.map +0 -1
- package/dist/section-headings.mjs +0 -64
- package/dist/section-headings.mjs.map +0 -1
- package/dist/sentence-verb-indicators.mjs +0 -232
- package/dist/sentence-verb-indicators.mjs.map +0 -1
- package/dist/signing-clauses.mjs +0 -78
- package/dist/signing-clauses.mjs.map +0 -1
- package/dist/stopwords.mjs +0 -9915
- package/dist/stopwords.mjs.map +0 -1
- package/dist/structural-single-cap-prefixes.mjs +0 -99
- package/dist/structural-single-cap-prefixes.mjs.map +0 -1
- package/dist/triggers.cs.mjs +0 -569
- package/dist/triggers.cs.mjs.map +0 -1
- package/dist/triggers.de.mjs +0 -139
- package/dist/triggers.de.mjs.map +0 -1
- package/dist/triggers.en.mjs +0 -119
- package/dist/triggers.en.mjs.map +0 -1
- package/dist/triggers.es.mjs +0 -96
- package/dist/triggers.es.mjs.map +0 -1
- package/dist/triggers.fr.mjs +0 -275
- package/dist/triggers.fr.mjs.map +0 -1
- package/dist/triggers.global.mjs +0 -79
- package/dist/triggers.global.mjs.map +0 -1
- package/dist/triggers.hu.mjs +0 -41
- package/dist/triggers.hu.mjs.map +0 -1
- package/dist/triggers.it.mjs +0 -74
- package/dist/triggers.it.mjs.map +0 -1
- package/dist/triggers.pl.mjs +0 -271
- package/dist/triggers.pl.mjs.map +0 -1
- package/dist/triggers.pt-br.mjs +0 -193
- package/dist/triggers.pt-br.mjs.map +0 -1
- package/dist/triggers.ro.mjs +0 -59
- package/dist/triggers.ro.mjs.map +0 -1
- package/dist/triggers.sk.mjs +0 -555
- package/dist/triggers.sk.mjs.map +0 -1
- package/dist/triggers.sv.mjs +0 -58
- package/dist/triggers.sv.mjs.map +0 -1
- package/dist/year-words.mjs +0 -62
- package/dist/year-words.mjs.map +0 -1
package/dist/native.mjs
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
//#region src/native.ts
|
|
2
|
+
var PreparedNativeAnonymizer = class {
|
|
3
|
+
#prepared;
|
|
4
|
+
constructor(prepared) {
|
|
5
|
+
this.#prepared = prepared;
|
|
6
|
+
}
|
|
7
|
+
prepareDiagnosticsJson() {
|
|
8
|
+
return this.#prepared.prepareDiagnosticsJson?.() ?? null;
|
|
9
|
+
}
|
|
10
|
+
prepare_diagnostics_json() {
|
|
11
|
+
return this.prepareDiagnosticsJson();
|
|
12
|
+
}
|
|
13
|
+
warmLazyRegex() {
|
|
14
|
+
if (this.#prepared.warmLazyRegex) {
|
|
15
|
+
this.#prepared.warmLazyRegex();
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
this.#prepared.warm_lazy_regex?.();
|
|
19
|
+
}
|
|
20
|
+
warm_lazy_regex() {
|
|
21
|
+
this.warmLazyRegex();
|
|
22
|
+
}
|
|
23
|
+
warmLazyRegexDiagnosticsJson() {
|
|
24
|
+
if (this.#prepared.warmLazyRegexDiagnosticsJson) return this.#prepared.warmLazyRegexDiagnosticsJson();
|
|
25
|
+
return this.#prepared.warm_lazy_regex_diagnostics_json?.() ?? null;
|
|
26
|
+
}
|
|
27
|
+
warm_lazy_regex_diagnostics_json() {
|
|
28
|
+
return this.warmLazyRegexDiagnosticsJson();
|
|
29
|
+
}
|
|
30
|
+
redactStaticEntities(fullText, operators) {
|
|
31
|
+
return toNativeStaticRedactionResult(this.#prepared.redactStaticEntities(fullText, toBindingOperatorConfig(operators)));
|
|
32
|
+
}
|
|
33
|
+
redact_text(fullText, operators) {
|
|
34
|
+
return this.redactStaticEntities(fullText, operators);
|
|
35
|
+
}
|
|
36
|
+
redact_text_json(fullText, operators) {
|
|
37
|
+
const bindingOperators = toBindingOperatorConfig(operators);
|
|
38
|
+
if (this.#prepared.redactStaticEntitiesJson) return this.#prepared.redactStaticEntitiesJson(fullText, bindingOperators);
|
|
39
|
+
return JSON.stringify(toBindingStaticRedactionResult(toNativeStaticRedactionResult(this.#prepared.redactStaticEntities(fullText, bindingOperators))));
|
|
40
|
+
}
|
|
41
|
+
redactTextJson(fullText, operators) {
|
|
42
|
+
return this.redact_text_json(fullText, operators);
|
|
43
|
+
}
|
|
44
|
+
redactTextStreamJson(fullText, onEvent, operators) {
|
|
45
|
+
if (!this.#prepared.redactStaticEntitiesResultStreamJson) return null;
|
|
46
|
+
return this.#prepared.redactStaticEntitiesResultStreamJson(fullText, toBindingOperatorConfig(operators), onEvent);
|
|
47
|
+
}
|
|
48
|
+
redact_text_stream_json(fullText, onEvent, operators) {
|
|
49
|
+
return this.redactTextStreamJson(fullText, onEvent, operators);
|
|
50
|
+
}
|
|
51
|
+
redactStaticEntitiesDiagnosticsJson(fullText, operators) {
|
|
52
|
+
if (!this.#prepared.redactStaticEntitiesDiagnosticsJson) return null;
|
|
53
|
+
return this.#prepared.redactStaticEntitiesDiagnosticsJson(fullText, toBindingOperatorConfig(operators));
|
|
54
|
+
}
|
|
55
|
+
diagnostics_json(fullText, operators) {
|
|
56
|
+
return this.redactStaticEntitiesDiagnosticsJson(fullText, operators);
|
|
57
|
+
}
|
|
58
|
+
diagnosticsStreamJson(fullText, onBatch, operators) {
|
|
59
|
+
if (!this.#prepared.redactStaticEntitiesDiagnosticsStreamJson) return null;
|
|
60
|
+
return this.#prepared.redactStaticEntitiesDiagnosticsStreamJson(fullText, toBindingOperatorConfig(operators), onBatch);
|
|
61
|
+
}
|
|
62
|
+
diagnostics_stream_json(fullText, onBatch, operators) {
|
|
63
|
+
return this.diagnosticsStreamJson(fullText, onBatch, operators);
|
|
64
|
+
}
|
|
65
|
+
redactStaticEntitiesSummaryDiagnosticsJson(fullText, operators) {
|
|
66
|
+
if (!this.#prepared.redactStaticEntitiesSummaryDiagnosticsJson) return null;
|
|
67
|
+
return this.#prepared.redactStaticEntitiesSummaryDiagnosticsJson(fullText, toBindingOperatorConfig(operators));
|
|
68
|
+
}
|
|
69
|
+
summary_diagnostics_json(fullText, operators) {
|
|
70
|
+
return this.redactStaticEntitiesSummaryDiagnosticsJson(fullText, operators);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
var PreparedNativePipeline = class {
|
|
74
|
+
#anonymizer;
|
|
75
|
+
constructor(anonymizer) {
|
|
76
|
+
this.#anonymizer = anonymizer;
|
|
77
|
+
}
|
|
78
|
+
prepareDiagnosticsJson() {
|
|
79
|
+
return this.#anonymizer.prepareDiagnosticsJson();
|
|
80
|
+
}
|
|
81
|
+
prepare_diagnostics_json() {
|
|
82
|
+
return this.prepareDiagnosticsJson();
|
|
83
|
+
}
|
|
84
|
+
warmLazyRegex() {
|
|
85
|
+
this.#anonymizer.warmLazyRegex();
|
|
86
|
+
}
|
|
87
|
+
warm_lazy_regex() {
|
|
88
|
+
this.warmLazyRegex();
|
|
89
|
+
}
|
|
90
|
+
warmLazyRegexDiagnosticsJson() {
|
|
91
|
+
return this.#anonymizer.warmLazyRegexDiagnosticsJson();
|
|
92
|
+
}
|
|
93
|
+
warm_lazy_regex_diagnostics_json() {
|
|
94
|
+
return this.warmLazyRegexDiagnosticsJson();
|
|
95
|
+
}
|
|
96
|
+
redactText(fullText, operators) {
|
|
97
|
+
return this.#anonymizer.redactStaticEntities(fullText, operators);
|
|
98
|
+
}
|
|
99
|
+
redact_text(fullText, operators) {
|
|
100
|
+
return this.redactText(fullText, operators);
|
|
101
|
+
}
|
|
102
|
+
redact_text_json(fullText, operators) {
|
|
103
|
+
return this.#anonymizer.redact_text_json(fullText, operators);
|
|
104
|
+
}
|
|
105
|
+
redactTextJson(fullText, operators) {
|
|
106
|
+
return this.redact_text_json(fullText, operators);
|
|
107
|
+
}
|
|
108
|
+
redactTextStreamJson(fullText, onEvent, operators) {
|
|
109
|
+
return this.#anonymizer.redactTextStreamJson(fullText, onEvent, operators);
|
|
110
|
+
}
|
|
111
|
+
redact_text_stream_json(fullText, onEvent, operators) {
|
|
112
|
+
return this.redactTextStreamJson(fullText, onEvent, operators);
|
|
113
|
+
}
|
|
114
|
+
redactTextDiagnosticsJson(fullText, operators) {
|
|
115
|
+
return this.#anonymizer.redactStaticEntitiesDiagnosticsJson(fullText, operators);
|
|
116
|
+
}
|
|
117
|
+
diagnostics_json(fullText, operators) {
|
|
118
|
+
return this.redactTextDiagnosticsJson(fullText, operators);
|
|
119
|
+
}
|
|
120
|
+
diagnosticsStreamJson(fullText, onBatch, operators) {
|
|
121
|
+
return this.#anonymizer.diagnosticsStreamJson(fullText, onBatch, operators);
|
|
122
|
+
}
|
|
123
|
+
diagnostics_stream_json(fullText, onBatch, operators) {
|
|
124
|
+
return this.diagnosticsStreamJson(fullText, onBatch, operators);
|
|
125
|
+
}
|
|
126
|
+
redactTextSummaryDiagnosticsJson(fullText, operators) {
|
|
127
|
+
return this.#anonymizer.redactStaticEntitiesSummaryDiagnosticsJson(fullText, operators);
|
|
128
|
+
}
|
|
129
|
+
summary_diagnostics_json(fullText, operators) {
|
|
130
|
+
return this.redactTextSummaryDiagnosticsJson(fullText, operators);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
const encodeNativeSearchConfig = (config) => new TextEncoder().encode(JSON.stringify(config));
|
|
134
|
+
const encodeNativeSearchConfigInput = (config) => {
|
|
135
|
+
if (typeof config === "string") return new TextEncoder().encode(config);
|
|
136
|
+
if (config instanceof Uint8Array) return config;
|
|
137
|
+
return encodeNativeSearchConfig(config);
|
|
138
|
+
};
|
|
139
|
+
const getNativeBindingVersion = (binding) => binding.nativePackageVersion();
|
|
140
|
+
const native_package_version = getNativeBindingVersion;
|
|
141
|
+
const normalize_for_search = ({ binding, text }) => binding.normalizeForSearch(text);
|
|
142
|
+
const assertNativeBindingVersion = ({ binding, expectedVersion }) => {
|
|
143
|
+
const actualVersion = getNativeBindingVersion(binding);
|
|
144
|
+
if (actualVersion !== expectedVersion) throw new Error(`Native anonymize binding version ${actualVersion} does not match ${expectedVersion}`);
|
|
145
|
+
};
|
|
146
|
+
const prepareNativeSearchPackage = ({ binding, config, compressed = false }) => {
|
|
147
|
+
const configBytes = encodeNativeSearchConfig(config);
|
|
148
|
+
return compressed ? binding.prepareStaticSearchCompressedPackageBytes(configBytes) : binding.prepareStaticSearchPackageBytes(configBytes);
|
|
149
|
+
};
|
|
150
|
+
const prepare_search_package = ({ binding, config, compressed = false }) => {
|
|
151
|
+
const configBytes = encodeNativeSearchConfigInput(config);
|
|
152
|
+
return compressed ? binding.prepareStaticSearchCompressedPackageBytes(configBytes) : binding.prepareStaticSearchPackageBytes(configBytes);
|
|
153
|
+
};
|
|
154
|
+
const createNativeAnonymizerFromConfig = ({ binding, config }) => new PreparedNativeAnonymizer(binding.NativePreparedSearch.fromConfigJsonBytes(encodeNativeSearchConfig(config)));
|
|
155
|
+
const createNativeAnonymizerFromPackage = ({ binding, packageBytes }) => new PreparedNativeAnonymizer(binding.NativePreparedSearch.fromPreparedPackageBytes(packageBytes));
|
|
156
|
+
const load_prepared_package = ({ binding, packageBytes }) => createNativeAnonymizerFromPackage({
|
|
157
|
+
binding,
|
|
158
|
+
packageBytes
|
|
159
|
+
});
|
|
160
|
+
const redact_text_json = ({ binding, config, fullText, operators }) => new PreparedNativeAnonymizer(binding.NativePreparedSearch.fromConfigJsonBytes(encodeNativeSearchConfigInput(config))).redact_text_json(fullText, operators);
|
|
161
|
+
const redact_text = ({ binding, config, fullText, operators }) => new PreparedNativeAnonymizer(binding.NativePreparedSearch.fromConfigJsonBytes(encodeNativeSearchConfigInput(config))).redact_text(fullText, operators);
|
|
162
|
+
const redact_text_stream_json = ({ binding, config, fullText, operators, onEvent }) => new PreparedNativeAnonymizer(binding.NativePreparedSearch.fromConfigJsonBytes(encodeNativeSearchConfigInput(config))).redact_text_stream_json(fullText, onEvent, operators);
|
|
163
|
+
const diagnostics_json = ({ binding, config, fullText, operators }) => new PreparedNativeAnonymizer(binding.NativePreparedSearch.fromConfigJsonBytes(encodeNativeSearchConfigInput(config))).diagnostics_json(fullText, operators);
|
|
164
|
+
const diagnostics_stream_json = ({ binding, config, fullText, operators, onBatch }) => new PreparedNativeAnonymizer(binding.NativePreparedSearch.fromConfigJsonBytes(encodeNativeSearchConfigInput(config))).diagnostics_stream_json(fullText, onBatch, operators);
|
|
165
|
+
const summary_diagnostics_json = ({ binding, config, fullText, operators }) => new PreparedNativeAnonymizer(binding.NativePreparedSearch.fromConfigJsonBytes(encodeNativeSearchConfigInput(config))).summary_diagnostics_json(fullText, operators);
|
|
166
|
+
const createNativePipelineFromPackage = ({ binding, packageBytes }) => new PreparedNativePipeline(createNativeAnonymizerFromPackage({
|
|
167
|
+
binding,
|
|
168
|
+
packageBytes
|
|
169
|
+
}));
|
|
170
|
+
const PreparedSearch = PreparedNativeAnonymizer;
|
|
171
|
+
const PreparedAnonymizer = PreparedNativeAnonymizer;
|
|
172
|
+
const toBindingOperatorConfig = (config) => {
|
|
173
|
+
if (!config) return;
|
|
174
|
+
const bindingConfig = {};
|
|
175
|
+
if (config.operators !== void 0) bindingConfig.operators = config.operators;
|
|
176
|
+
if (config.redactString !== void 0) bindingConfig.redactString = config.redactString;
|
|
177
|
+
return bindingConfig;
|
|
178
|
+
};
|
|
179
|
+
const toNativeStaticRedactionResult = (result) => ({
|
|
180
|
+
resolvedEntities: result.resolvedEntities.map(toNativePipelineEntity),
|
|
181
|
+
redaction: toNativeRedactionResult(result.redaction)
|
|
182
|
+
});
|
|
183
|
+
const toBindingStaticRedactionResult = (result) => ({
|
|
184
|
+
resolved_entities: result.resolvedEntities.map(toBindingPipelineEntity),
|
|
185
|
+
redaction: {
|
|
186
|
+
redacted_text: result.redaction.redactedText,
|
|
187
|
+
redaction_map: [...result.redaction.redactionMap.entries()].map(([placeholder, original]) => ({
|
|
188
|
+
placeholder,
|
|
189
|
+
original
|
|
190
|
+
})),
|
|
191
|
+
operator_map: [...result.redaction.operatorMap.entries()].map(([placeholder, operator]) => ({
|
|
192
|
+
placeholder,
|
|
193
|
+
operator
|
|
194
|
+
})),
|
|
195
|
+
entity_count: result.redaction.entityCount
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
const toNativePipelineEntity = (entity) => ({
|
|
199
|
+
start: entity.start,
|
|
200
|
+
end: entity.end,
|
|
201
|
+
label: entity.label,
|
|
202
|
+
text: entity.text,
|
|
203
|
+
score: entity.score,
|
|
204
|
+
source: entity.source,
|
|
205
|
+
...entity.sourceDetail ? { sourceDetail: entity.sourceDetail } : {}
|
|
206
|
+
});
|
|
207
|
+
const toBindingPipelineEntity = ({ sourceDetail, ...entity }) => ({
|
|
208
|
+
...entity,
|
|
209
|
+
source_detail: sourceDetail ?? null
|
|
210
|
+
});
|
|
211
|
+
const toNativeRedactionResult = (result) => ({
|
|
212
|
+
redactedText: result.redactedText,
|
|
213
|
+
redactionMap: toRedactionMap(result.redactionMap),
|
|
214
|
+
operatorMap: toOperatorMap(result.operatorMap),
|
|
215
|
+
entityCount: result.entityCount
|
|
216
|
+
});
|
|
217
|
+
const toRedactionMap = (entries) => {
|
|
218
|
+
const map = /* @__PURE__ */ new Map();
|
|
219
|
+
for (const entry of entries) map.set(entry.placeholder, entry.original);
|
|
220
|
+
return map;
|
|
221
|
+
};
|
|
222
|
+
const toOperatorMap = (entries) => {
|
|
223
|
+
const map = /* @__PURE__ */ new Map();
|
|
224
|
+
for (const entry of entries) map.set(entry.placeholder, entry.operator);
|
|
225
|
+
return map;
|
|
226
|
+
};
|
|
227
|
+
//#endregion
|
|
228
|
+
export { PreparedAnonymizer, PreparedNativeAnonymizer, PreparedNativePipeline, PreparedSearch, assertNativeBindingVersion, createNativeAnonymizerFromConfig, createNativeAnonymizerFromPackage, createNativePipelineFromPackage, diagnostics_json, diagnostics_stream_json, encodeNativeSearchConfig, encodeNativeSearchConfigInput, getNativeBindingVersion, load_prepared_package, native_package_version, normalize_for_search, prepareNativeSearchPackage, prepare_search_package, redact_text, redact_text_json, redact_text_stream_json, summary_diagnostics_json };
|
|
229
|
+
|
|
230
|
+
//# sourceMappingURL=native.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"native.mjs","names":["#prepared","#anonymizer"],"sources":["../src/native.ts"],"sourcesContent":["import type { NativePreparedSearchConfig } from \"./native-search-config\";\nimport type { OperatorType } from \"./types\";\n\nexport type { NativePreparedSearchConfig } from \"./native-search-config\";\n\ntype NativeBindingOperatorConfig = {\n operators?: Record<string, OperatorType>;\n redactString?: string;\n};\n\nexport type NativeDiagnosticsBatchCallback = (diagnosticsJson: string) => void;\nexport type NativeResultEventCallback = (eventJson: string) => void;\n\ntype NativeBindingRedactionEntry = {\n placeholder: string;\n original: string;\n};\n\ntype NativeBindingOperatorEntry = {\n placeholder: string;\n operator: OperatorType;\n};\n\ntype NativeBindingPipelineEntity = {\n start: number;\n end: number;\n label: string;\n text: string;\n score: number;\n source: string;\n sourceDetail?: string | null;\n};\n\ntype NativeBindingRedactionResult = {\n redactedText: string;\n redactionMap: NativeBindingRedactionEntry[];\n operatorMap: NativeBindingOperatorEntry[];\n entityCount: number;\n};\n\ntype NativeBindingStaticRedactionResult = {\n resolvedEntities: NativeBindingPipelineEntity[];\n redaction: NativeBindingRedactionResult;\n};\n\ntype CanonicalPipelineEntity = {\n start: number;\n end: number;\n label: string;\n text: string;\n score: number;\n source: string;\n source_detail?: string | null;\n};\n\ntype CanonicalStaticRedactionResult = {\n resolved_entities: CanonicalPipelineEntity[];\n redaction: {\n redacted_text: string;\n redaction_map: NativeBindingRedactionEntry[];\n operator_map: NativeBindingOperatorEntry[];\n entity_count: number;\n };\n};\n\nexport type NativePreparedSearchBinding = {\n prepareDiagnosticsJson?: () => string;\n warmLazyRegex?: () => void;\n warm_lazy_regex?: () => void;\n warmLazyRegexDiagnosticsJson?: () => string;\n warm_lazy_regex_diagnostics_json?: () => string;\n redactStaticEntities: (\n fullText: string,\n operators?: NativeBindingOperatorConfig,\n ) => NativeBindingStaticRedactionResult;\n redactStaticEntitiesJson?: (\n fullText: string,\n operators?: NativeBindingOperatorConfig,\n ) => string;\n redactStaticEntitiesResultStreamJson?: (\n fullText: string,\n operators: NativeBindingOperatorConfig | undefined,\n onEvent: NativeResultEventCallback,\n ) => string;\n redactStaticEntitiesDiagnosticsJson?: (\n fullText: string,\n operators?: NativeBindingOperatorConfig,\n ) => string;\n redactStaticEntitiesDiagnosticsStreamJson?: (\n fullText: string,\n operators: NativeBindingOperatorConfig | undefined,\n onBatch: NativeDiagnosticsBatchCallback,\n ) => string;\n redactStaticEntitiesSummaryDiagnosticsJson?: (\n fullText: string,\n operators?: NativeBindingOperatorConfig,\n ) => string;\n};\n\nexport type NativeAnonymizeBinding = {\n normalizeForSearch: (text: string) => string;\n nativePackageVersion: () => string;\n NativePreparedSearch: {\n fromConfigJsonBytes: (\n configJson: Uint8Array,\n ) => NativePreparedSearchBinding;\n fromPreparedPackageBytes: (\n packageBytes: Uint8Array,\n ) => NativePreparedSearchBinding;\n fromPreparedPackageBytesWithoutCache?: (\n packageBytes: Uint8Array,\n ) => NativePreparedSearchBinding;\n fromTrustedPreparedPackageBytes?: (\n packageBytes: Uint8Array,\n ) => NativePreparedSearchBinding;\n fromTrustedPreparedPackageBytesWithoutCache?: (\n packageBytes: Uint8Array,\n ) => NativePreparedSearchBinding;\n };\n prepareStaticSearchPackageBytes: (configJson: Uint8Array) => Uint8Array;\n prepareStaticSearchCompressedPackageBytes: (\n configJson: Uint8Array,\n ) => Uint8Array;\n // Rust config assembler (replaces the retired TypeScript config-assembly\n // layer). Takes the pipeline config plus out-of-band dictionaries and\n // gazetteer JSON and returns either the assembled config JSON or ready\n // package bytes. Optional so older bindings without the assembler still\n // satisfy the type; native-node loads them from the same `.node`.\n assembleStaticSearchConfigJson?: (\n pipelineConfigJson: Uint8Array,\n dictionariesJson?: Uint8Array,\n gazetteerJson?: Uint8Array,\n ) => Uint8Array;\n assembleStaticSearchPackageBytes?: (\n pipelineConfigJson: Uint8Array,\n dictionariesJson?: Uint8Array,\n gazetteerJson?: Uint8Array,\n ) => Uint8Array;\n assembleStaticSearchCompressedPackageBytes?: (\n pipelineConfigJson: Uint8Array,\n dictionariesJson?: Uint8Array,\n gazetteerJson?: Uint8Array,\n ) => Uint8Array;\n};\n\nexport type NativeOperatorConfig = {\n operators?: Record<string, OperatorType>;\n redactString?: string;\n};\n\nexport type NativePipelineEntity = {\n start: number;\n end: number;\n label: string;\n text: string;\n score: number;\n source: string;\n sourceDetail?: string;\n};\n\nexport type NativeRedactionResult = {\n redactedText: string;\n redactionMap: Map<string, string>;\n operatorMap: Map<string, OperatorType>;\n entityCount: number;\n};\n\nexport type NativeStaticRedactionResult = {\n resolvedEntities: NativePipelineEntity[];\n redaction: NativeRedactionResult;\n};\n\nexport type NativeSearchPackageOptions = {\n binding: NativeAnonymizeBinding;\n config: NativePreparedSearchConfig;\n compressed?: boolean;\n};\n\nexport type NativeSearchPackageInput =\n | NativePreparedSearchConfig\n | string\n | Uint8Array;\n\nexport type SharedNativeSearchPackageOptions = {\n binding: NativeAnonymizeBinding;\n config: NativeSearchPackageInput;\n compressed?: boolean;\n};\n\nexport type SharedNativePreparedPackageOptions = {\n binding: NativeAnonymizeBinding;\n packageBytes: Uint8Array;\n};\n\nexport type SharedNativeRedactTextJsonOptions = {\n binding: NativeAnonymizeBinding;\n config: NativeSearchPackageInput;\n fullText: string;\n operators?: NativeOperatorConfig;\n};\n\nexport type SharedNativeRedactTextOptions = SharedNativeRedactTextJsonOptions;\n\nexport type SharedNativeDiagnosticsJsonOptions =\n SharedNativeRedactTextJsonOptions;\n\nexport type SharedNativeDiagnosticsStreamJsonOptions =\n SharedNativeRedactTextJsonOptions & {\n onBatch: NativeDiagnosticsBatchCallback;\n };\n\nexport type SharedNativeRedactTextStreamJsonOptions =\n SharedNativeRedactTextJsonOptions & {\n onEvent: NativeResultEventCallback;\n };\n\nexport type NativeNormalizeOptions = {\n binding: NativeAnonymizeBinding;\n text: string;\n};\n\nexport type NativeAnonymizerFromConfigOptions = {\n binding: NativeAnonymizeBinding;\n config: NativePreparedSearchConfig;\n};\n\nexport type NativeAnonymizerFromPackageOptions = {\n binding: NativeAnonymizeBinding;\n packageBytes: Uint8Array;\n};\n\nexport type NativePipelineFromPackageOptions =\n NativeAnonymizerFromPackageOptions;\n\nexport type NativeBindingVersionOptions = {\n binding: NativeAnonymizeBinding;\n expectedVersion: string;\n};\n\nexport class PreparedNativeAnonymizer {\n readonly #prepared: NativePreparedSearchBinding;\n\n constructor(prepared: NativePreparedSearchBinding) {\n this.#prepared = prepared;\n }\n\n prepareDiagnosticsJson(): string | null {\n return this.#prepared.prepareDiagnosticsJson?.() ?? null;\n }\n\n prepare_diagnostics_json(): string | null {\n return this.prepareDiagnosticsJson();\n }\n\n warmLazyRegex(): void {\n if (this.#prepared.warmLazyRegex) {\n this.#prepared.warmLazyRegex();\n return;\n }\n this.#prepared.warm_lazy_regex?.();\n }\n\n warm_lazy_regex(): void {\n this.warmLazyRegex();\n }\n\n warmLazyRegexDiagnosticsJson(): string | null {\n if (this.#prepared.warmLazyRegexDiagnosticsJson) {\n return this.#prepared.warmLazyRegexDiagnosticsJson();\n }\n return this.#prepared.warm_lazy_regex_diagnostics_json?.() ?? null;\n }\n\n warm_lazy_regex_diagnostics_json(): string | null {\n return this.warmLazyRegexDiagnosticsJson();\n }\n\n redactStaticEntities(\n fullText: string,\n operators?: NativeOperatorConfig,\n ): NativeStaticRedactionResult {\n return toNativeStaticRedactionResult(\n this.#prepared.redactStaticEntities(\n fullText,\n toBindingOperatorConfig(operators),\n ),\n );\n }\n\n redact_text(\n fullText: string,\n operators?: NativeOperatorConfig,\n ): NativeStaticRedactionResult {\n return this.redactStaticEntities(fullText, operators);\n }\n\n redact_text_json(fullText: string, operators?: NativeOperatorConfig): string {\n const bindingOperators = toBindingOperatorConfig(operators);\n if (this.#prepared.redactStaticEntitiesJson) {\n return this.#prepared.redactStaticEntitiesJson(\n fullText,\n bindingOperators,\n );\n }\n return JSON.stringify(\n toBindingStaticRedactionResult(\n toNativeStaticRedactionResult(\n this.#prepared.redactStaticEntities(fullText, bindingOperators),\n ),\n ),\n );\n }\n\n redactTextJson(fullText: string, operators?: NativeOperatorConfig): string {\n return this.redact_text_json(fullText, operators);\n }\n\n redactTextStreamJson(\n fullText: string,\n onEvent: NativeResultEventCallback,\n operators?: NativeOperatorConfig,\n ): string | null {\n if (!this.#prepared.redactStaticEntitiesResultStreamJson) {\n return null;\n }\n return this.#prepared.redactStaticEntitiesResultStreamJson(\n fullText,\n toBindingOperatorConfig(operators),\n onEvent,\n );\n }\n\n redact_text_stream_json(\n fullText: string,\n onEvent: NativeResultEventCallback,\n operators?: NativeOperatorConfig,\n ): string | null {\n return this.redactTextStreamJson(fullText, onEvent, operators);\n }\n\n redactStaticEntitiesDiagnosticsJson(\n fullText: string,\n operators?: NativeOperatorConfig,\n ): string | null {\n if (!this.#prepared.redactStaticEntitiesDiagnosticsJson) {\n return null;\n }\n return this.#prepared.redactStaticEntitiesDiagnosticsJson(\n fullText,\n toBindingOperatorConfig(operators),\n );\n }\n\n diagnostics_json(\n fullText: string,\n operators?: NativeOperatorConfig,\n ): string | null {\n return this.redactStaticEntitiesDiagnosticsJson(fullText, operators);\n }\n\n diagnosticsStreamJson(\n fullText: string,\n onBatch: NativeDiagnosticsBatchCallback,\n operators?: NativeOperatorConfig,\n ): string | null {\n if (!this.#prepared.redactStaticEntitiesDiagnosticsStreamJson) {\n return null;\n }\n return this.#prepared.redactStaticEntitiesDiagnosticsStreamJson(\n fullText,\n toBindingOperatorConfig(operators),\n onBatch,\n );\n }\n\n diagnostics_stream_json(\n fullText: string,\n onBatch: NativeDiagnosticsBatchCallback,\n operators?: NativeOperatorConfig,\n ): string | null {\n return this.diagnosticsStreamJson(fullText, onBatch, operators);\n }\n\n redactStaticEntitiesSummaryDiagnosticsJson(\n fullText: string,\n operators?: NativeOperatorConfig,\n ): string | null {\n if (!this.#prepared.redactStaticEntitiesSummaryDiagnosticsJson) {\n return null;\n }\n return this.#prepared.redactStaticEntitiesSummaryDiagnosticsJson(\n fullText,\n toBindingOperatorConfig(operators),\n );\n }\n\n summary_diagnostics_json(\n fullText: string,\n operators?: NativeOperatorConfig,\n ): string | null {\n return this.redactStaticEntitiesSummaryDiagnosticsJson(fullText, operators);\n }\n}\n\nexport class PreparedNativePipeline {\n readonly #anonymizer: PreparedNativeAnonymizer;\n\n constructor(anonymizer: PreparedNativeAnonymizer) {\n this.#anonymizer = anonymizer;\n }\n\n prepareDiagnosticsJson(): string | null {\n return this.#anonymizer.prepareDiagnosticsJson();\n }\n\n prepare_diagnostics_json(): string | null {\n return this.prepareDiagnosticsJson();\n }\n\n warmLazyRegex(): void {\n this.#anonymizer.warmLazyRegex();\n }\n\n warm_lazy_regex(): void {\n this.warmLazyRegex();\n }\n\n warmLazyRegexDiagnosticsJson(): string | null {\n return this.#anonymizer.warmLazyRegexDiagnosticsJson();\n }\n\n warm_lazy_regex_diagnostics_json(): string | null {\n return this.warmLazyRegexDiagnosticsJson();\n }\n\n redactText(\n fullText: string,\n operators?: NativeOperatorConfig,\n ): NativeStaticRedactionResult {\n return this.#anonymizer.redactStaticEntities(fullText, operators);\n }\n\n redact_text(\n fullText: string,\n operators?: NativeOperatorConfig,\n ): NativeStaticRedactionResult {\n return this.redactText(fullText, operators);\n }\n\n redact_text_json(fullText: string, operators?: NativeOperatorConfig): string {\n return this.#anonymizer.redact_text_json(fullText, operators);\n }\n\n redactTextJson(fullText: string, operators?: NativeOperatorConfig): string {\n return this.redact_text_json(fullText, operators);\n }\n\n redactTextStreamJson(\n fullText: string,\n onEvent: NativeResultEventCallback,\n operators?: NativeOperatorConfig,\n ): string | null {\n return this.#anonymizer.redactTextStreamJson(fullText, onEvent, operators);\n }\n\n redact_text_stream_json(\n fullText: string,\n onEvent: NativeResultEventCallback,\n operators?: NativeOperatorConfig,\n ): string | null {\n return this.redactTextStreamJson(fullText, onEvent, operators);\n }\n\n redactTextDiagnosticsJson(\n fullText: string,\n operators?: NativeOperatorConfig,\n ): string | null {\n return this.#anonymizer.redactStaticEntitiesDiagnosticsJson(\n fullText,\n operators,\n );\n }\n\n diagnostics_json(\n fullText: string,\n operators?: NativeOperatorConfig,\n ): string | null {\n return this.redactTextDiagnosticsJson(fullText, operators);\n }\n\n diagnosticsStreamJson(\n fullText: string,\n onBatch: NativeDiagnosticsBatchCallback,\n operators?: NativeOperatorConfig,\n ): string | null {\n return this.#anonymizer.diagnosticsStreamJson(fullText, onBatch, operators);\n }\n\n diagnostics_stream_json(\n fullText: string,\n onBatch: NativeDiagnosticsBatchCallback,\n operators?: NativeOperatorConfig,\n ): string | null {\n return this.diagnosticsStreamJson(fullText, onBatch, operators);\n }\n\n redactTextSummaryDiagnosticsJson(\n fullText: string,\n operators?: NativeOperatorConfig,\n ): string | null {\n return this.#anonymizer.redactStaticEntitiesSummaryDiagnosticsJson(\n fullText,\n operators,\n );\n }\n\n summary_diagnostics_json(\n fullText: string,\n operators?: NativeOperatorConfig,\n ): string | null {\n return this.redactTextSummaryDiagnosticsJson(fullText, operators);\n }\n}\n\nexport const encodeNativeSearchConfig = (\n config: NativePreparedSearchConfig,\n): Uint8Array => new TextEncoder().encode(JSON.stringify(config));\n\nexport const encodeNativeSearchConfigInput = (\n config: NativeSearchPackageInput,\n): Uint8Array => {\n if (typeof config === \"string\") {\n return new TextEncoder().encode(config);\n }\n if (config instanceof Uint8Array) {\n return config;\n }\n return encodeNativeSearchConfig(config);\n};\n\nexport const getNativeBindingVersion = (\n binding: NativeAnonymizeBinding,\n): string => binding.nativePackageVersion();\n\nexport const native_package_version = getNativeBindingVersion;\n\nexport const normalize_for_search = ({\n binding,\n text,\n}: NativeNormalizeOptions): string => binding.normalizeForSearch(text);\n\nexport const assertNativeBindingVersion = ({\n binding,\n expectedVersion,\n}: NativeBindingVersionOptions): void => {\n const actualVersion = getNativeBindingVersion(binding);\n if (actualVersion !== expectedVersion) {\n throw new Error(\n `Native anonymize binding version ${actualVersion} does not match ${expectedVersion}`,\n );\n }\n};\n\nexport const prepareNativeSearchPackage = ({\n binding,\n config,\n compressed = false,\n}: NativeSearchPackageOptions): Uint8Array => {\n const configBytes = encodeNativeSearchConfig(config);\n return compressed\n ? binding.prepareStaticSearchCompressedPackageBytes(configBytes)\n : binding.prepareStaticSearchPackageBytes(configBytes);\n};\n\nexport const prepare_search_package = ({\n binding,\n config,\n compressed = false,\n}: SharedNativeSearchPackageOptions): Uint8Array => {\n const configBytes = encodeNativeSearchConfigInput(config);\n return compressed\n ? binding.prepareStaticSearchCompressedPackageBytes(configBytes)\n : binding.prepareStaticSearchPackageBytes(configBytes);\n};\n\nexport const createNativeAnonymizerFromConfig = ({\n binding,\n config,\n}: NativeAnonymizerFromConfigOptions): PreparedNativeAnonymizer =>\n new PreparedNativeAnonymizer(\n binding.NativePreparedSearch.fromConfigJsonBytes(\n encodeNativeSearchConfig(config),\n ),\n );\n\nexport const createNativeAnonymizerFromPackage = ({\n binding,\n packageBytes,\n}: NativeAnonymizerFromPackageOptions): PreparedNativeAnonymizer =>\n new PreparedNativeAnonymizer(\n binding.NativePreparedSearch.fromPreparedPackageBytes(packageBytes),\n );\n\nexport const load_prepared_package = ({\n binding,\n packageBytes,\n}: SharedNativePreparedPackageOptions): PreparedNativeAnonymizer =>\n createNativeAnonymizerFromPackage({ binding, packageBytes });\n\nexport const redact_text_json = ({\n binding,\n config,\n fullText,\n operators,\n}: SharedNativeRedactTextJsonOptions): string =>\n new PreparedNativeAnonymizer(\n binding.NativePreparedSearch.fromConfigJsonBytes(\n encodeNativeSearchConfigInput(config),\n ),\n ).redact_text_json(fullText, operators);\n\nexport const redact_text = ({\n binding,\n config,\n fullText,\n operators,\n}: SharedNativeRedactTextOptions): NativeStaticRedactionResult =>\n new PreparedNativeAnonymizer(\n binding.NativePreparedSearch.fromConfigJsonBytes(\n encodeNativeSearchConfigInput(config),\n ),\n ).redact_text(fullText, operators);\n\nexport const redact_text_stream_json = ({\n binding,\n config,\n fullText,\n operators,\n onEvent,\n}: SharedNativeRedactTextStreamJsonOptions): string | null =>\n new PreparedNativeAnonymizer(\n binding.NativePreparedSearch.fromConfigJsonBytes(\n encodeNativeSearchConfigInput(config),\n ),\n ).redact_text_stream_json(fullText, onEvent, operators);\n\nexport const diagnostics_json = ({\n binding,\n config,\n fullText,\n operators,\n}: SharedNativeDiagnosticsJsonOptions): string | null =>\n new PreparedNativeAnonymizer(\n binding.NativePreparedSearch.fromConfigJsonBytes(\n encodeNativeSearchConfigInput(config),\n ),\n ).diagnostics_json(fullText, operators);\n\nexport const diagnostics_stream_json = ({\n binding,\n config,\n fullText,\n operators,\n onBatch,\n}: SharedNativeDiagnosticsStreamJsonOptions): string | null =>\n new PreparedNativeAnonymizer(\n binding.NativePreparedSearch.fromConfigJsonBytes(\n encodeNativeSearchConfigInput(config),\n ),\n ).diagnostics_stream_json(fullText, onBatch, operators);\n\nexport const summary_diagnostics_json = ({\n binding,\n config,\n fullText,\n operators,\n}: SharedNativeDiagnosticsJsonOptions): string | null =>\n new PreparedNativeAnonymizer(\n binding.NativePreparedSearch.fromConfigJsonBytes(\n encodeNativeSearchConfigInput(config),\n ),\n ).summary_diagnostics_json(fullText, operators);\n\nexport const createNativePipelineFromPackage = ({\n binding,\n packageBytes,\n}: NativePipelineFromPackageOptions): PreparedNativePipeline =>\n new PreparedNativePipeline(\n createNativeAnonymizerFromPackage({ binding, packageBytes }),\n );\n\nexport const PreparedSearch = PreparedNativeAnonymizer;\nexport type PreparedSearch = PreparedNativeAnonymizer;\nexport const PreparedAnonymizer = PreparedNativeAnonymizer;\nexport type PreparedAnonymizer = PreparedNativeAnonymizer;\n\nconst toBindingOperatorConfig = (\n config: NativeOperatorConfig | undefined,\n): NativeBindingOperatorConfig | undefined => {\n if (!config) {\n return undefined;\n }\n const bindingConfig: NativeBindingOperatorConfig = {};\n if (config.operators !== undefined) {\n bindingConfig.operators = config.operators;\n }\n if (config.redactString !== undefined) {\n bindingConfig.redactString = config.redactString;\n }\n return bindingConfig;\n};\n\nconst toNativeStaticRedactionResult = (\n result: NativeBindingStaticRedactionResult,\n): NativeStaticRedactionResult => ({\n resolvedEntities: result.resolvedEntities.map(toNativePipelineEntity),\n redaction: toNativeRedactionResult(result.redaction),\n});\n\nconst toBindingStaticRedactionResult = (\n result: NativeStaticRedactionResult,\n): CanonicalStaticRedactionResult => ({\n resolved_entities: result.resolvedEntities.map(toBindingPipelineEntity),\n redaction: {\n redacted_text: result.redaction.redactedText,\n redaction_map: [...result.redaction.redactionMap.entries()].map(\n ([placeholder, original]) => ({ placeholder, original }),\n ),\n operator_map: [...result.redaction.operatorMap.entries()].map(\n ([placeholder, operator]) => ({ placeholder, operator }),\n ),\n entity_count: result.redaction.entityCount,\n },\n});\n\nconst toNativePipelineEntity = (\n entity: NativeBindingPipelineEntity,\n): NativePipelineEntity => ({\n start: entity.start,\n end: entity.end,\n label: entity.label,\n text: entity.text,\n score: entity.score,\n source: entity.source,\n ...(entity.sourceDetail ? { sourceDetail: entity.sourceDetail } : {}),\n});\n\nconst toBindingPipelineEntity = ({\n sourceDetail,\n ...entity\n}: NativePipelineEntity): CanonicalPipelineEntity => ({\n ...entity,\n source_detail: sourceDetail ?? null,\n});\n\nconst toNativeRedactionResult = (\n result: NativeBindingRedactionResult,\n): NativeRedactionResult => ({\n redactedText: result.redactedText,\n redactionMap: toRedactionMap(result.redactionMap),\n operatorMap: toOperatorMap(result.operatorMap),\n entityCount: result.entityCount,\n});\n\nconst toRedactionMap = (\n entries: readonly NativeBindingRedactionEntry[],\n): Map<string, string> => {\n const map = new Map<string, string>();\n for (const entry of entries) {\n map.set(entry.placeholder, entry.original);\n }\n return map;\n};\n\nconst toOperatorMap = (\n entries: readonly NativeBindingOperatorEntry[],\n): Map<string, OperatorType> => {\n const map = new Map<string, OperatorType>();\n for (const entry of entries) {\n map.set(entry.placeholder, entry.operator);\n }\n return map;\n};\n"],"mappings":";AA+OA,IAAa,2BAAb,MAAsC;CACpC;CAEA,YAAY,UAAuC;EACjD,KAAKA,YAAY;CACnB;CAEA,yBAAwC;EACtC,OAAO,KAAKA,UAAU,yBAAyB,KAAK;CACtD;CAEA,2BAA0C;EACxC,OAAO,KAAK,uBAAuB;CACrC;CAEA,gBAAsB;EACpB,IAAI,KAAKA,UAAU,eAAe;GAChC,KAAKA,UAAU,cAAc;GAC7B;EACF;EACA,KAAKA,UAAU,kBAAkB;CACnC;CAEA,kBAAwB;EACtB,KAAK,cAAc;CACrB;CAEA,+BAA8C;EAC5C,IAAI,KAAKA,UAAU,8BACjB,OAAO,KAAKA,UAAU,6BAA6B;EAErD,OAAO,KAAKA,UAAU,mCAAmC,KAAK;CAChE;CAEA,mCAAkD;EAChD,OAAO,KAAK,6BAA6B;CAC3C;CAEA,qBACE,UACA,WAC6B;EAC7B,OAAO,8BACL,KAAKA,UAAU,qBACb,UACA,wBAAwB,SAAS,CACnC,CACF;CACF;CAEA,YACE,UACA,WAC6B;EAC7B,OAA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AG,OAAO,UAAU,aAAa,QAAQ,CAAC,CAAC,CAAC,KACzD,CAAC,aAAa,eAAe;GAAE;GAAa;EAAS,EACxD;EACA,cAAc,CAAC,GAAG,OAAO,UAAU,YAAY,QAAQ,CAAC,CAAC,CAAC,KACvD,CAAC,aAAa,eAAe;GAAE;GAAa;EAAS,EACxD;EACA,cAAc,OAAO,UAAU;CACjC;AACF;AAEA,MAAM,0BACJ,YAC0B;CAC1B,OAAO,OAAO;CACd,KAAK,OAAO;CACZ,OAAO,OAAO;CACd,MAAM,OAAO;CACb,OAAO,OAAO;CACd,QAAQ,OAAO;CACf,GAAI,OAAO,eAAe,EAAE,cAAc,OAAO,aAAa,IAAI,CAAC;AACrE;AAEA,MAAM,2BAA2B,EAC/B,cACA,GAAG,cACiD;CACpD,GAAG;CACH,eAAe,gBAAgB;AACjC;AAEA,MAAM,2BACJ,YAC2B;CAC3B,cAAc,OAAO;CACrB,cAAc,eAAe,OAAO,YAAY;CAChD,aAAa,cAAc,OAAO,WAAW;CAC7C,aAAa,OAAO;AACtB;AAEA,MAAM,kBACJ,YACwB;CACxB,MAAM,sBAAM,IAAI,IAAoB;CACpC,KAAK,MAAM,SAAS,SAClB,IAAI,IAAI,MAAM,aAAa,MAAM,QAAQ;CAE3C,OAAO;AACT;AAEA,MAAM,iBACJ,YAC8B;CAC9B,MAAM,sBAAM,IAAI,IAA0B;CAC1C,KAAK,MAAM,SAAS,SAClB,IAAI,IAAI,MAAM,aAAa,MAAM,QAAQ;CAE3C,OAAO;AACT"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { A as createNativePipelineFromPackage, B as prepare_search_package, C as SharedNativeRedactTextJsonOptions, D as assertNativeBindingVersion, E as SharedNativeSearchPackageOptions, F as getNativeBindingVersion, H as redact_text_json, I as load_prepared_package, L as native_package_version, M as diagnostics_stream_json, N as encodeNativeSearchConfig, O as createNativeAnonymizerFromConfig, P as encodeNativeSearchConfigInput, R as normalize_for_search, S as SharedNativePreparedPackageOptions, T as SharedNativeRedactTextStreamJsonOptions, U as redact_text_stream_json, V as redact_text, W as summary_diagnostics_json, _ as PreparedNativeAnonymizer, a as NativeDiagnosticsBatchCallback, b as SharedNativeDiagnosticsJsonOptions, c as NativePipelineEntity, d as NativeRedactionResult, f as NativeResultEventCallback, g as PreparedAnonymizer, h as NativeStaticRedactionResult, i as NativeBindingVersionOptions, j as diagnostics_json, k as createNativeAnonymizerFromPackage, l as NativePipelineFromPackageOptions, lt as NativePreparedSearchConfig, m as NativeSearchPackageOptions, n as NativeAnonymizerFromConfigOptions, o as NativeNormalizeOptions, p as NativeSearchPackageInput, r as NativeAnonymizerFromPackageOptions, s as NativeOperatorConfig, t as NativeAnonymizeBinding, u as NativePreparedSearchBinding, v as PreparedNativePipeline, w as SharedNativeRedactTextOptions, x as SharedNativeDiagnosticsStreamJsonOptions, y as PreparedSearch, z as prepareNativeSearchPackage } from "./native.mjs";
|
|
2
|
+
export { NativeAnonymizeBinding, NativeAnonymizerFromConfigOptions, NativeAnonymizerFromPackageOptions, NativeBindingVersionOptions, NativeDiagnosticsBatchCallback, NativeNormalizeOptions, NativeOperatorConfig, NativePipelineEntity, NativePipelineFromPackageOptions, NativePreparedSearchBinding, type NativePreparedSearchConfig, NativeRedactionResult, NativeResultEventCallback, NativeSearchPackageInput, NativeSearchPackageOptions, NativeStaticRedactionResult, PreparedAnonymizer, PreparedNativeAnonymizer, PreparedNativePipeline, PreparedSearch, SharedNativeDiagnosticsJsonOptions, SharedNativeDiagnosticsStreamJsonOptions, SharedNativePreparedPackageOptions, SharedNativeRedactTextJsonOptions, SharedNativeRedactTextOptions, SharedNativeRedactTextStreamJsonOptions, SharedNativeSearchPackageOptions, assertNativeBindingVersion, createNativeAnonymizerFromConfig, createNativeAnonymizerFromPackage, createNativePipelineFromPackage, diagnostics_json, diagnostics_stream_json, encodeNativeSearchConfig, encodeNativeSearchConfigInput, getNativeBindingVersion, load_prepared_package, native_package_version, normalize_for_search, prepareNativeSearchPackage, prepare_search_package, redact_text, redact_text_json, redact_text_stream_json, summary_diagnostics_json };
|
package/index.cjs
ADDED
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stll/anonymize",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Multi-layer PII detection and anonymization pipeline with regex, NER, deny lists, and coreference resolution",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"anonymization",
|
|
7
|
+
"pii",
|
|
8
|
+
"redaction",
|
|
9
|
+
"ner",
|
|
10
|
+
"pseudonymization",
|
|
11
|
+
"gdpr",
|
|
12
|
+
"privacy"
|
|
13
|
+
],
|
|
5
14
|
"type": "module",
|
|
6
15
|
"exports": {
|
|
7
16
|
".": {
|
|
@@ -13,11 +22,29 @@
|
|
|
13
22
|
"types": "./dist/constants.d.mts",
|
|
14
23
|
"import": "./dist/constants.mjs",
|
|
15
24
|
"default": "./dist/constants.mjs"
|
|
25
|
+
},
|
|
26
|
+
"./native": {
|
|
27
|
+
"types": "./dist/native.d.mts",
|
|
28
|
+
"import": "./dist/native.mjs",
|
|
29
|
+
"default": "./dist/native.mjs"
|
|
30
|
+
},
|
|
31
|
+
"./native-node": {
|
|
32
|
+
"types": "./dist/native-node.d.mts",
|
|
33
|
+
"import": "./dist/native-node.mjs",
|
|
34
|
+
"default": "./dist/native-node.mjs"
|
|
16
35
|
}
|
|
17
36
|
},
|
|
18
37
|
"types": "dist/index.d.mts",
|
|
38
|
+
"bin": {
|
|
39
|
+
"stella-anonymize-build-native-package": "./scripts/build-native-pipeline-package.mjs"
|
|
40
|
+
},
|
|
19
41
|
"files": [
|
|
20
|
-
"
|
|
42
|
+
"ATTRIBUTION.md",
|
|
43
|
+
"dist",
|
|
44
|
+
"index.cjs",
|
|
45
|
+
"native-pipeline.stlanonpkg",
|
|
46
|
+
"native-pipeline.*.stlanonpkg",
|
|
47
|
+
"scripts/build-native-pipeline-package.mjs"
|
|
21
48
|
],
|
|
22
49
|
"publishConfig": {
|
|
23
50
|
"access": "public"
|
|
@@ -26,21 +53,32 @@
|
|
|
26
53
|
"type": "git",
|
|
27
54
|
"url": "git+https://github.com/stella/anonymize.git"
|
|
28
55
|
},
|
|
56
|
+
"homepage": "https://github.com/stella/anonymize",
|
|
29
57
|
"license": "MIT",
|
|
30
58
|
"scripts": {
|
|
31
|
-
"build": "tsdown",
|
|
59
|
+
"build": "bun run build:native-wasm && tsdown && bun scripts/build-native-node.mjs && bun run build:wasm-assets",
|
|
32
60
|
"prepublishOnly": "bun run build",
|
|
33
61
|
"typecheck": "tsc --noEmit -p tsconfig.json && tsc --noEmit -p tsconfig.test.json && tsc --noEmit -p tsconfig.wasm.json",
|
|
34
|
-
"test": "bun test --
|
|
62
|
+
"test": "bun test --timeout 15000",
|
|
35
63
|
"test:fast": "bun run test src/__test__/*.test.ts",
|
|
36
|
-
"
|
|
64
|
+
"test:native-fixtures": "ANONYMIZE_TEST_SLOW_NATIVE_FIXTURE_PARITY=1 bun test --timeout 600000 src/__test__/python-parity.test.ts",
|
|
65
|
+
"perf:native-adapters": "bun scripts/native-adapter-perf.mjs",
|
|
66
|
+
"perf:native-default": "bun scripts/native-default-sdk-perf.mjs",
|
|
67
|
+
"perf:native-default:summary": "ANONYMIZE_NATIVE_DEFAULT_SDK_PERF_OUTPUT=summary bun scripts/native-default-sdk-perf.mjs",
|
|
68
|
+
"check:v1-native": "bun scripts/native-v1-readiness.mjs",
|
|
69
|
+
"check:v1-native:full": "ANONYMIZE_V1_READINESS_MODE=full bun scripts/native-v1-readiness.mjs",
|
|
37
70
|
"smoke:dist": "bun scripts/dist-smoke.mjs",
|
|
71
|
+
"build:native-wasm": "node scripts/build-native-wasm.mjs",
|
|
72
|
+
"build:wasm-assets": "node scripts/build-native-wasm-assets.mjs",
|
|
73
|
+
"smoke:wasm": "node scripts/smoke-wasm.mjs",
|
|
74
|
+
"smoke:wasm-package": "node scripts/smoke-wasm-package.mjs",
|
|
75
|
+
"smoke:wasm-browser": "node scripts/smoke-wasm-browser.mjs",
|
|
38
76
|
"format": "oxfmt ."
|
|
39
77
|
},
|
|
40
78
|
"dependencies": {
|
|
41
79
|
"@huggingface/tokenizers": "^0.1.3",
|
|
42
80
|
"@stll/stdnum": "^2.1.1",
|
|
43
|
-
"@stll/text-search": "^1.0.
|
|
81
|
+
"@stll/text-search": "^1.0.7"
|
|
44
82
|
},
|
|
45
83
|
"peerDependencies": {
|
|
46
84
|
"@stll/anonymize-data": "^0.0.6"
|
|
@@ -50,12 +88,22 @@
|
|
|
50
88
|
"optional": true
|
|
51
89
|
}
|
|
52
90
|
},
|
|
91
|
+
"optionalDependencies": {
|
|
92
|
+
"@stll/anonymize-darwin-arm64": "2.0.0",
|
|
93
|
+
"@stll/anonymize-darwin-x64": "2.0.0",
|
|
94
|
+
"@stll/anonymize-linux-arm64-gnu": "2.0.0",
|
|
95
|
+
"@stll/anonymize-linux-x64-gnu": "2.0.0",
|
|
96
|
+
"@stll/anonymize-win32-x64-msvc": "2.0.0"
|
|
97
|
+
},
|
|
53
98
|
"devDependencies": {
|
|
99
|
+
"@napi-rs/cli": "^3.7.2",
|
|
54
100
|
"@stll/anonymize-data": "workspace:*",
|
|
55
|
-
"@stll/text-search-wasm": "^1.0.
|
|
101
|
+
"@stll/text-search-wasm": "^1.0.7",
|
|
56
102
|
"bun-types": "^1.3.14",
|
|
57
|
-
"
|
|
103
|
+
"fast-check": "^4.8.0",
|
|
104
|
+
"puppeteer-core": "25.2.1",
|
|
105
|
+
"tsdown": "^0.22.3",
|
|
58
106
|
"typescript": "^6.0.3",
|
|
59
|
-
"vite": "^8.0
|
|
107
|
+
"vite": "^8.1.0"
|
|
60
108
|
}
|
|
61
109
|
}
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { dirname, resolve } from "node:path";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
DEFAULT_NATIVE_PIPELINE_CONFIG,
|
|
8
|
+
prepareNativePipelinePackage,
|
|
9
|
+
} from "../dist/index.mjs";
|
|
10
|
+
import { loadNativeAnonymizeBinding } from "../dist/native-node.mjs";
|
|
11
|
+
|
|
12
|
+
const args = parseArgs(process.argv.slice(2));
|
|
13
|
+
const outputPath = resolve(args.out ?? "native-pipeline.stlanonpkg");
|
|
14
|
+
const compressed = args.compressed === true;
|
|
15
|
+
const { config, gazetteerEntries } = await loadPackageInput(args);
|
|
16
|
+
const binding = loadNativeAnonymizeBinding();
|
|
17
|
+
const packageBytes = await prepareNativePipelinePackage({
|
|
18
|
+
binding,
|
|
19
|
+
config,
|
|
20
|
+
gazetteerEntries,
|
|
21
|
+
compressed,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
mkdirSync(dirname(outputPath), { recursive: true });
|
|
25
|
+
writeFileSync(outputPath, packageBytes);
|
|
26
|
+
|
|
27
|
+
console.log(
|
|
28
|
+
JSON.stringify({
|
|
29
|
+
event: "native-pipeline-package",
|
|
30
|
+
outputPath,
|
|
31
|
+
bytes: packageBytes.byteLength,
|
|
32
|
+
compressed,
|
|
33
|
+
nativeVersion: binding.nativePackageVersion(),
|
|
34
|
+
}),
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
function parseArgs(values) {
|
|
38
|
+
const result = {};
|
|
39
|
+
for (let index = 0; index < values.length; index += 1) {
|
|
40
|
+
const value = values[index];
|
|
41
|
+
switch (value) {
|
|
42
|
+
case "--config": {
|
|
43
|
+
result.config = requiredValue(values, index, value);
|
|
44
|
+
index += 1;
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
case "--export": {
|
|
48
|
+
result.exportName = requiredValue(values, index, value);
|
|
49
|
+
index += 1;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
case "--out": {
|
|
53
|
+
result.out = requiredValue(values, index, value);
|
|
54
|
+
index += 1;
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
case "--raw": {
|
|
58
|
+
result.raw = true;
|
|
59
|
+
break;
|
|
60
|
+
}
|
|
61
|
+
case "--compressed": {
|
|
62
|
+
result.compressed = true;
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
65
|
+
case "--default-dictionaries": {
|
|
66
|
+
result.defaultDictionaries = true;
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
case "--language": {
|
|
70
|
+
result.language = requiredValue(values, index, value);
|
|
71
|
+
index += 1;
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
case "--languages": {
|
|
75
|
+
result.languages = requiredValue(values, index, value);
|
|
76
|
+
index += 1;
|
|
77
|
+
break;
|
|
78
|
+
}
|
|
79
|
+
case "--help": {
|
|
80
|
+
printHelp();
|
|
81
|
+
process.exit(0);
|
|
82
|
+
}
|
|
83
|
+
default:
|
|
84
|
+
throw new Error(`Unknown option: ${value}`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (result.raw && result.compressed) {
|
|
88
|
+
throw new Error("Use either --raw or --compressed, not both");
|
|
89
|
+
}
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function requiredValue(values, index, option) {
|
|
94
|
+
const value = values[index + 1];
|
|
95
|
+
if (value === undefined || value.startsWith("--")) {
|
|
96
|
+
throw new Error(`${option} requires a value`);
|
|
97
|
+
}
|
|
98
|
+
return value;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async function loadPackageInput(options) {
|
|
102
|
+
const input = await loadBasePackageInput(options);
|
|
103
|
+
const withDictionaries =
|
|
104
|
+
!options.defaultDictionaries || input.config.dictionaries !== undefined
|
|
105
|
+
? input
|
|
106
|
+
: {
|
|
107
|
+
...input,
|
|
108
|
+
config: {
|
|
109
|
+
...input.config,
|
|
110
|
+
dictionaries: await loadDefaultDictionaries(),
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
return {
|
|
114
|
+
...withDictionaries,
|
|
115
|
+
config: applyCliLanguageScope(withDictionaries.config, options),
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async function loadBasePackageInput(options) {
|
|
120
|
+
if (!options.config) {
|
|
121
|
+
return { config: defaultNativePipelineConfig(), gazetteerEntries: [] };
|
|
122
|
+
}
|
|
123
|
+
const moduleUrl = pathToFileURL(resolve(options.config)).href;
|
|
124
|
+
// eslint-disable-next-line stll/no-dynamic-import-specifier
|
|
125
|
+
const loaded = await import(moduleUrl);
|
|
126
|
+
const exportName = options.exportName ?? "default";
|
|
127
|
+
const candidate =
|
|
128
|
+
exportName === "default" ? loaded.default : loaded[exportName];
|
|
129
|
+
if (candidate === undefined) {
|
|
130
|
+
throw new Error(`Config module does not export ${exportName}`);
|
|
131
|
+
}
|
|
132
|
+
const value =
|
|
133
|
+
typeof candidate === "function" ? await candidate() : await candidate;
|
|
134
|
+
if (!value || typeof value !== "object") {
|
|
135
|
+
throw new TypeError("Native package config export must be an object");
|
|
136
|
+
}
|
|
137
|
+
if ("config" in value) {
|
|
138
|
+
return {
|
|
139
|
+
config: value.config,
|
|
140
|
+
gazetteerEntries: value.gazetteerEntries ?? [],
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
return { config: value, gazetteerEntries: [] };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function defaultNativePipelineConfig() {
|
|
147
|
+
return {
|
|
148
|
+
...DEFAULT_NATIVE_PIPELINE_CONFIG,
|
|
149
|
+
labels: [...DEFAULT_NATIVE_PIPELINE_CONFIG.labels],
|
|
150
|
+
workspaceId: "native-pipeline-package",
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function applyCliLanguageScope(pipelineConfig, options) {
|
|
155
|
+
if (options.language !== undefined && options.languages !== undefined) {
|
|
156
|
+
throw new Error("Use either --language or --languages, not both");
|
|
157
|
+
}
|
|
158
|
+
if (options.language !== undefined) {
|
|
159
|
+
const language = normalizeLanguageOption(options.language, "--language");
|
|
160
|
+
return { ...pipelineConfig, language, languages: undefined };
|
|
161
|
+
}
|
|
162
|
+
if (options.languages === undefined) {
|
|
163
|
+
return pipelineConfig;
|
|
164
|
+
}
|
|
165
|
+
const languages = normalizeLanguageList(options.languages);
|
|
166
|
+
return { ...pipelineConfig, language: undefined, languages };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function normalizeLanguageOption(value, option) {
|
|
170
|
+
const language = value.trim().toLowerCase();
|
|
171
|
+
if (language.length === 0) {
|
|
172
|
+
throw new Error(`${option} requires a non-empty language code`);
|
|
173
|
+
}
|
|
174
|
+
return language;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function normalizeLanguageList(value) {
|
|
178
|
+
const languages = value
|
|
179
|
+
.split(",")
|
|
180
|
+
.map((entry) => normalizeLanguageOption(entry, "--languages"))
|
|
181
|
+
.filter((entry, index, entries) => entries.indexOf(entry) === index);
|
|
182
|
+
if (languages.length === 0) {
|
|
183
|
+
throw new Error("--languages requires at least one language code");
|
|
184
|
+
}
|
|
185
|
+
return languages;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
async function loadDefaultDictionaries() {
|
|
189
|
+
let loaded;
|
|
190
|
+
try {
|
|
191
|
+
loaded = await import("@stll/anonymize-data/dictionaries");
|
|
192
|
+
} catch (error) {
|
|
193
|
+
throw new Error(
|
|
194
|
+
`--default-dictionaries requires @stll/anonymize-data: ${formatError(error)}`,
|
|
195
|
+
);
|
|
196
|
+
}
|
|
197
|
+
if (typeof loaded.loadDictionaryBundle !== "function") {
|
|
198
|
+
throw new TypeError(
|
|
199
|
+
"@stll/anonymize-data/dictionaries does not export loadDictionaryBundle",
|
|
200
|
+
);
|
|
201
|
+
}
|
|
202
|
+
return loaded.loadDictionaryBundle();
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function formatError(error) {
|
|
206
|
+
if (error instanceof Error) {
|
|
207
|
+
return error.message;
|
|
208
|
+
}
|
|
209
|
+
return String(error);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function printHelp() {
|
|
213
|
+
console.log(`Usage: build-native-pipeline-package [options]
|
|
214
|
+
|
|
215
|
+
Options:
|
|
216
|
+
--out <path> Output package path. Defaults to native-pipeline.stlanonpkg.
|
|
217
|
+
--config <path> ESM module exporting a PipelineConfig or { config, gazetteerEntries }.
|
|
218
|
+
--export <name> Export name to read from the config module. Defaults to default.
|
|
219
|
+
--language <code> Build a package scoped to one content language.
|
|
220
|
+
--languages <codes> Build a package scoped to comma-separated content languages.
|
|
221
|
+
--default-dictionaries Load @stll/anonymize-data into configs that do not provide dictionaries.
|
|
222
|
+
--compressed Write an LZ4-compressed package.
|
|
223
|
+
--raw Write an uncompressed package. This is the default.
|
|
224
|
+
`);
|
|
225
|
+
}
|