@stll/anonymize-wasm 2.0.1 → 2.0.2

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/dist/wasm.mjs CHANGED
@@ -1,5 +1,166 @@
1
- import { DEFAULT_ENTITY_LABELS, DETECTION_SOURCES, DETECTOR_PRIORITY, OPERATOR_TYPES } from "./constants.mjs";
1
+ import { DEFAULT_ENTITY_LABELS, DETECTION_SOURCES, DETECTOR_PRIORITY, ENTITY_CAPABILITIES, ENTITY_LABELS, ENTITY_SELECTIONS, OPERATOR_TYPES } from "./constants.mjs";
2
+ import { CAPABILITY_MANIFEST, CAPABILITY_MANIFEST_SCHEMA_VERSION, CAPABILITY_RUNTIMES } from "./capabilities.mjs";
2
3
  //#region src/native.ts
4
+ const CALLER_DETECTION_CONTRACT_VERSION = 2;
5
+ const callerDetectionRequestJson = (detections) => JSON.stringify({
6
+ version: 2,
7
+ detections: detections.map((detection) => ({
8
+ start: detection.start,
9
+ end: detection.end,
10
+ label: detection.label,
11
+ score: detection.score,
12
+ provider_id: detection.providerId,
13
+ detection_id: detection.detectionId
14
+ }))
15
+ });
16
+ var PreparedNativeRedactionSession = class {
17
+ #session;
18
+ constructor(session) {
19
+ this.#session = session;
20
+ }
21
+ sessionId() {
22
+ return this.#session.sessionId();
23
+ }
24
+ session_id() {
25
+ return this.sessionId();
26
+ }
27
+ mappingCount() {
28
+ return this.#session.mappingCount();
29
+ }
30
+ mapping_count() {
31
+ return this.mappingCount();
32
+ }
33
+ restoreText(fullText, observedAtEpochSeconds) {
34
+ if (observedAtEpochSeconds === void 0) {
35
+ const restore = this.#session.restoreText;
36
+ if (!restore) throw new Error("Native anonymize binding does not support session restoration");
37
+ return restore.call(this.#session, fullText);
38
+ }
39
+ const restore = this.#session.restoreTextAt;
40
+ if (!restore) throw new Error("Native anonymize binding does not support session restoration lifecycle controls");
41
+ return restore.call(this.#session, fullText, observedAtEpochSeconds);
42
+ }
43
+ restore_text(fullText, observedAtEpochSeconds) {
44
+ return this.restoreText(fullText, observedAtEpochSeconds);
45
+ }
46
+ toPlaintextJson() {
47
+ return this.#session.toPlaintextJson();
48
+ }
49
+ to_plaintext_json() {
50
+ return this.toPlaintextJson();
51
+ }
52
+ toPlaintextJsonAt(observedAtEpochSeconds) {
53
+ const serialize = this.#session.toPlaintextJsonAt;
54
+ if (!serialize) throw new Error("Native anonymize binding does not support session lifecycle controls");
55
+ return serialize.call(this.#session, observedAtEpochSeconds);
56
+ }
57
+ to_plaintext_json_at(observedAtEpochSeconds) {
58
+ return this.toPlaintextJsonAt(observedAtEpochSeconds);
59
+ }
60
+ toEncryptedArchive(key) {
61
+ const serialize = this.#session.toEncryptedArchive;
62
+ if (!serialize) throw new Error("Native anonymize binding does not support encrypted session archives");
63
+ return serialize.call(this.#session, key);
64
+ }
65
+ to_encrypted_archive(key) {
66
+ return this.toEncryptedArchive(key);
67
+ }
68
+ toEncryptedArchiveAt(key, observedAtEpochSeconds) {
69
+ const serialize = this.#session.toEncryptedArchiveAt;
70
+ if (!serialize) throw new Error("Native anonymize binding does not support encrypted session archives");
71
+ return serialize.call(this.#session, key, observedAtEpochSeconds);
72
+ }
73
+ to_encrypted_archive_at(key, observedAtEpochSeconds) {
74
+ return this.toEncryptedArchiveAt(key, observedAtEpochSeconds);
75
+ }
76
+ inspect(observedAtEpochSeconds) {
77
+ const inspect = this.#session.inspectJson;
78
+ if (!inspect) throw new Error("Native anonymize binding does not support session lifecycle controls");
79
+ const metadata = JSON.parse(inspect.call(this.#session, observedAtEpochSeconds));
80
+ return {
81
+ sessionId: metadata.session_id,
82
+ createdAtEpochSeconds: metadata.created_at_epoch_seconds,
83
+ expiresAtEpochSeconds: metadata.expires_at_epoch_seconds,
84
+ mappingCount: metadata.mapping_count,
85
+ status: metadata.status
86
+ };
87
+ }
88
+ delete() {
89
+ const deleteSession = this.#session.deleteJson;
90
+ if (!deleteSession) throw new Error("Native anonymize binding does not support session lifecycle controls");
91
+ const summary = JSON.parse(deleteSession.call(this.#session));
92
+ return {
93
+ sessionId: summary.session_id,
94
+ deletedMappingCount: summary.deleted_mapping_count
95
+ };
96
+ }
97
+ redactStaticEntities(fullText, operators) {
98
+ const result = JSON.parse(this.redact_text_json(fullText, operators));
99
+ return fromCanonicalStaticRedactionResult(result);
100
+ }
101
+ redactText(fullText, operators) {
102
+ return this.redactStaticEntities(fullText, operators);
103
+ }
104
+ redact_text(fullText, operators) {
105
+ return this.redactText(fullText, operators);
106
+ }
107
+ redactTextJson(fullText, operators) {
108
+ return this.redact_text_json(fullText, operators);
109
+ }
110
+ redact_text_json(fullText, operators) {
111
+ return this.#session.redactStaticEntitiesJson(fullText, toBindingOperatorConfig(operators));
112
+ }
113
+ redactStaticEntitiesAt(options) {
114
+ const result = JSON.parse(this.redactTextJsonAt(options));
115
+ return fromCanonicalStaticRedactionResult(result);
116
+ }
117
+ redactTextAt(options) {
118
+ return this.redactStaticEntitiesAt(options);
119
+ }
120
+ redact_text_at(options) {
121
+ return this.redactTextAt(options);
122
+ }
123
+ redact_static_entities_at(options) {
124
+ return this.redactStaticEntitiesAt(options);
125
+ }
126
+ redactTextJsonAt({ fullText, observedAtEpochSeconds, operators }) {
127
+ const redact = this.#session.redactStaticEntitiesJsonAt;
128
+ if (!redact) throw new Error("Native anonymize binding does not support session lifecycle controls");
129
+ return redact.call(this.#session, fullText, observedAtEpochSeconds, toBindingOperatorConfig(operators));
130
+ }
131
+ redact_text_json_at(options) {
132
+ return this.redactTextJsonAt(options);
133
+ }
134
+ planTextBatchWithCallerDetections({ inputs, operators, observedAtEpochSeconds }) {
135
+ const plan = this.#session.planStaticEntitiesWithCallerDetections;
136
+ if (!plan) throw new Error("Native anonymize binding does not support transactional caller-detection session plans");
137
+ const bindingOperators = toBindingOperatorConfig(operators);
138
+ return new PreparedNativeSessionRedactionPlan(plan.call(this.#session, {
139
+ inputs: inputs.map(({ detections, fullText }) => ({
140
+ fullText,
141
+ requestJson: callerDetectionRequestJson(detections)
142
+ })),
143
+ ...bindingOperators === void 0 ? {} : { operators: bindingOperators },
144
+ ...observedAtEpochSeconds === void 0 ? {} : { observedAtEpochSeconds }
145
+ }));
146
+ }
147
+ };
148
+ var PreparedNativeSessionRedactionPlan = class {
149
+ blocks;
150
+ #plan;
151
+ constructor(plan) {
152
+ this.#plan = plan;
153
+ const blocks = JSON.parse(plan.resultJson());
154
+ this.blocks = blocks.map(({ caller_entity_count, entity_count, replacements }) => ({
155
+ replacements,
156
+ entityCount: entity_count,
157
+ callerEntityCount: caller_entity_count
158
+ }));
159
+ }
160
+ commit() {
161
+ this.#plan.commit();
162
+ }
163
+ };
3
164
  var PreparedNativeAnonymizer = class {
4
165
  #prepared;
5
166
  constructor(prepared) {
@@ -28,6 +189,43 @@ var PreparedNativeAnonymizer = class {
28
189
  warm_lazy_regex_diagnostics_json() {
29
190
  return this.warmLazyRegexDiagnosticsJson();
30
191
  }
192
+ createRedactionSession(sessionId) {
193
+ const create = this.#prepared.createRedactionSession;
194
+ if (!create) throw new Error("Native anonymize binding does not support redaction sessions");
195
+ return new PreparedNativeRedactionSession(create.call(this.#prepared, sessionId));
196
+ }
197
+ create_redaction_session(sessionId) {
198
+ return this.createRedactionSession(sessionId);
199
+ }
200
+ createRedactionSessionWithLifecycle({ sessionId, createdAtEpochSeconds, expiresAtEpochSeconds }) {
201
+ const create = this.#prepared.createRedactionSessionWithLifecycle;
202
+ if (!create) throw new Error("Native anonymize binding does not support session lifecycle controls");
203
+ return new PreparedNativeRedactionSession(create.call(this.#prepared, sessionId, createdAtEpochSeconds, expiresAtEpochSeconds));
204
+ }
205
+ create_redaction_session_with_lifecycle(options) {
206
+ return this.createRedactionSessionWithLifecycle(options);
207
+ }
208
+ restoreRedactionSession(plaintextJson) {
209
+ const restore = this.#prepared.restoreRedactionSession;
210
+ if (!restore) throw new Error("Native anonymize binding does not support redaction sessions");
211
+ return new PreparedNativeRedactionSession(restore.call(this.#prepared, plaintextJson));
212
+ }
213
+ restore_redaction_session(plaintextJson) {
214
+ return this.restoreRedactionSession(plaintextJson);
215
+ }
216
+ restoreEncryptedRedactionSession({ archive, key, expectedSessionId, observedAtEpochSeconds }) {
217
+ const restore = this.#prepared.restoreEncryptedRedactionSession;
218
+ if (!restore) throw new Error("Native anonymize binding does not support encrypted session archives");
219
+ return new PreparedNativeRedactionSession(restore.call(this.#prepared, {
220
+ archive,
221
+ key,
222
+ expectedSessionId,
223
+ ...observedAtEpochSeconds === void 0 ? {} : { observedAtEpochSeconds }
224
+ }));
225
+ }
226
+ restore_encrypted_redaction_session(options) {
227
+ return this.restoreEncryptedRedactionSession(options);
228
+ }
31
229
  redactStaticEntities(fullText, operators) {
32
230
  return toNativeStaticRedactionResult(this.#prepared.redactStaticEntities(fullText, toBindingOperatorConfig(operators)));
33
231
  }
@@ -39,6 +237,32 @@ var PreparedNativeAnonymizer = class {
39
237
  if (this.#prepared.redactStaticEntitiesJson) return this.#prepared.redactStaticEntitiesJson(fullText, bindingOperators);
40
238
  return JSON.stringify(toBindingStaticRedactionResult(toNativeStaticRedactionResult(this.#prepared.redactStaticEntities(fullText, bindingOperators))));
41
239
  }
240
+ redactStaticEntitiesWithCallerDetections(fullText, options) {
241
+ if (!this.#prepared.redactStaticEntitiesWithCallerDetectionsJson) throw new Error("Native anonymize binding does not support caller detections");
242
+ const requestJson = callerDetectionRequestJson(options.detections);
243
+ const operators = toBindingOperatorConfig(options.operators);
244
+ const result = JSON.parse(this.#prepared.redactStaticEntitiesWithCallerDetectionsJson(fullText, {
245
+ requestJson,
246
+ ...operators ? { operators } : {}
247
+ }));
248
+ return fromCanonicalStaticRedactionResult(result);
249
+ }
250
+ redact_text_with_caller_detections(fullText, options) {
251
+ return this.redactStaticEntitiesWithCallerDetections(fullText, options);
252
+ }
253
+ redactStaticEntitiesWithCallerDetectionsDiagnosticsJson(fullText, options) {
254
+ const redact = this.#prepared.redactStaticEntitiesWithCallerDetectionsDiagnosticsJson;
255
+ if (!redact) return null;
256
+ const requestJson = callerDetectionRequestJson(options.detections);
257
+ const operators = toBindingOperatorConfig(options.operators);
258
+ return redact.call(this.#prepared, fullText, {
259
+ requestJson,
260
+ ...operators ? { operators } : {}
261
+ });
262
+ }
263
+ redact_static_entities_with_caller_detections_diagnostics_json(fullText, options) {
264
+ return this.redactStaticEntitiesWithCallerDetectionsDiagnosticsJson(fullText, options);
265
+ }
42
266
  redactTextJson(fullText, operators) {
43
267
  return this.redact_text_json(fullText, operators);
44
268
  }
@@ -94,6 +318,30 @@ var PreparedNativePipeline = class {
94
318
  warm_lazy_regex_diagnostics_json() {
95
319
  return this.warmLazyRegexDiagnosticsJson();
96
320
  }
321
+ createRedactionSession(sessionId) {
322
+ return this.#anonymizer.createRedactionSession(sessionId);
323
+ }
324
+ create_redaction_session(sessionId) {
325
+ return this.createRedactionSession(sessionId);
326
+ }
327
+ createRedactionSessionWithLifecycle(options) {
328
+ return this.#anonymizer.createRedactionSessionWithLifecycle(options);
329
+ }
330
+ create_redaction_session_with_lifecycle(options) {
331
+ return this.createRedactionSessionWithLifecycle(options);
332
+ }
333
+ restoreRedactionSession(plaintextJson) {
334
+ return this.#anonymizer.restoreRedactionSession(plaintextJson);
335
+ }
336
+ restore_redaction_session(plaintextJson) {
337
+ return this.restoreRedactionSession(plaintextJson);
338
+ }
339
+ restoreEncryptedRedactionSession(options) {
340
+ return this.#anonymizer.restoreEncryptedRedactionSession(options);
341
+ }
342
+ restore_encrypted_redaction_session(options) {
343
+ return this.restoreEncryptedRedactionSession(options);
344
+ }
97
345
  redactText(fullText, operators) {
98
346
  return this.#anonymizer.redactStaticEntities(fullText, operators);
99
347
  }
@@ -103,6 +351,18 @@ var PreparedNativePipeline = class {
103
351
  redact_text_json(fullText, operators) {
104
352
  return this.#anonymizer.redact_text_json(fullText, operators);
105
353
  }
354
+ redactTextWithCallerDetections(fullText, options) {
355
+ return this.#anonymizer.redactStaticEntitiesWithCallerDetections(fullText, options);
356
+ }
357
+ redact_text_with_caller_detections(fullText, options) {
358
+ return this.redactTextWithCallerDetections(fullText, options);
359
+ }
360
+ redactTextWithCallerDetectionsDiagnosticsJson(fullText, options) {
361
+ return this.#anonymizer.redactStaticEntitiesWithCallerDetectionsDiagnosticsJson(fullText, options);
362
+ }
363
+ redact_text_with_caller_detections_diagnostics_json(fullText, options) {
364
+ return this.redactTextWithCallerDetectionsDiagnosticsJson(fullText, options);
365
+ }
106
366
  redactTextJson(fullText, operators) {
107
367
  return this.redact_text_json(fullText, operators);
108
368
  }
@@ -177,6 +437,20 @@ const toNativeStaticRedactionResult = (result) => ({
177
437
  resolvedEntities: result.resolvedEntities.map(toNativePipelineEntity),
178
438
  redaction: toNativeRedactionResult(result.redaction)
179
439
  });
440
+ const fromCanonicalStaticRedactionResult = (result) => ({
441
+ resolvedEntities: result.resolved_entities.map(({ source_detail, provider_id, detection_id, ...entity }) => ({
442
+ ...entity,
443
+ ...source_detail ? { sourceDetail: source_detail } : {},
444
+ ...provider_id ? { providerId: provider_id } : {},
445
+ ...detection_id ? { detectionId: detection_id } : {}
446
+ })),
447
+ redaction: {
448
+ redactedText: result.redaction.redacted_text,
449
+ redactionMap: toRedactionMap(result.redaction.redaction_map),
450
+ operatorMap: toOperatorMap(result.redaction.operator_map),
451
+ entityCount: result.redaction.entity_count
452
+ }
453
+ });
180
454
  const toBindingStaticRedactionResult = (result) => ({
181
455
  resolved_entities: result.resolvedEntities.map(toBindingPipelineEntity),
182
456
  redaction: {
@@ -199,11 +473,15 @@ const toNativePipelineEntity = (entity) => ({
199
473
  text: entity.text,
200
474
  score: entity.score,
201
475
  source: entity.source,
202
- ...entity.sourceDetail ? { sourceDetail: entity.sourceDetail } : {}
476
+ ...entity.sourceDetail ? { sourceDetail: entity.sourceDetail } : {},
477
+ ...entity.providerId ? { providerId: entity.providerId } : {},
478
+ ...entity.detectionId ? { detectionId: entity.detectionId } : {}
203
479
  });
204
- const toBindingPipelineEntity = ({ sourceDetail, ...entity }) => ({
480
+ const toBindingPipelineEntity = ({ sourceDetail, providerId, detectionId, ...entity }) => ({
205
481
  ...entity,
206
- source_detail: sourceDetail ?? null
482
+ source_detail: sourceDetail ?? null,
483
+ provider_id: providerId ?? null,
484
+ detection_id: detectionId ?? null
207
485
  });
208
486
  const toNativeRedactionResult = (result) => ({
209
487
  redactedText: result.redactedText,
@@ -471,8 +749,11 @@ const encoder = new TextEncoder();
471
749
  * the separate bundle preferentially, and keeping the (large) dictionaries out
472
750
  * of the config JSON avoids serializing them twice.
473
751
  */
474
- const toAssembleInputs = ({ dictionaries, ...config }, gazetteerEntries) => ({
475
- pipelineConfigJson: encoder.encode(JSON.stringify(config)),
752
+ const toAssembleInputs = ({ dictionaries, enableNer = false, ...config }, gazetteerEntries) => ({
753
+ pipelineConfigJson: encoder.encode(JSON.stringify({
754
+ ...config,
755
+ enableNer
756
+ })),
476
757
  dictionariesJson: dictionaries === void 0 ? void 0 : encoder.encode(JSON.stringify(dictionaries)),
477
758
  gazetteerJson: gazetteerEntries.length === 0 ? void 0 : encoder.encode(JSON.stringify(gazetteerEntries))
478
759
  });
@@ -584,10 +865,12 @@ const getBinding = () => {
584
865
  return bindingPromise;
585
866
  };
586
867
  const loadWasmBinding = async () => {
587
- return toNativeAnonymizeBinding(await import(
868
+ const glueModule = isNodeRuntime() ? NODE_GLUE_MODULE : BROWSER_GLUE_MODULE;
869
+ const loaded = await import(
588
870
  /* @vite-ignore */
589
- assetUrl(isNodeRuntime() ? NODE_GLUE_MODULE : BROWSER_GLUE_MODULE).href
590
- ));
871
+ assetUrl(glueModule).href
872
+ );
873
+ return toNativeAnonymizeBinding(loaded);
591
874
  };
592
875
  const isNodeRuntime = () => {
593
876
  const globals = globalThis;
@@ -747,6 +1030,6 @@ const normalizeLanguage = (language) => {
747
1030
  return normalized;
748
1031
  };
749
1032
  //#endregion
750
- export { DEFAULT_ENTITY_LABELS, DETECTION_SOURCES, DETECTOR_PRIORITY, OPERATOR_TYPES, PreparedAnonymizer, PreparedNativeAnonymizer, PreparedNativePipeline, PreparedSearch, assertNativeBindingVersion, assertNativePipelineSupported, createNativeAnonymizerFromConfig, createNativeAnonymizerFromPackage, createNativePipelineFromConfig, createNativePipelineFromPackage, createPipelineContext, deanonymise, defaultPackageUrl, diagnostics_json, diagnostics_stream_json, encodeNativeSearchConfig, encodeNativeSearchConfigInput, exportRedactionKey, getBinding, getDefaultPipeline, getNativeBindingVersion, getNativePipelineCompatibility, loadDefaultPipeline, loadPipeline, load_prepared_package, native_package_version, normalize_for_search, prepareNativePipelineConfig, prepareNativePipelinePackage, prepareNativeSearchPackage, prepare_search_package, redactDefaultText, redactDefaultTextJson, redact_text, redact_text_json, redact_text_stream_json, summary_diagnostics_json };
1033
+ export { CALLER_DETECTION_CONTRACT_VERSION, CAPABILITY_MANIFEST, CAPABILITY_MANIFEST_SCHEMA_VERSION, CAPABILITY_RUNTIMES, DEFAULT_ENTITY_LABELS, DETECTION_SOURCES, DETECTOR_PRIORITY, ENTITY_CAPABILITIES, ENTITY_LABELS, ENTITY_SELECTIONS, OPERATOR_TYPES, PreparedAnonymizer, PreparedNativeAnonymizer, PreparedNativePipeline, PreparedNativeRedactionSession, PreparedNativeSessionRedactionPlan, PreparedSearch, assertNativeBindingVersion, assertNativePipelineSupported, createNativeAnonymizerFromConfig, createNativeAnonymizerFromPackage, createNativePipelineFromConfig, createNativePipelineFromPackage, createPipelineContext, deanonymise, defaultPackageUrl, diagnostics_json, diagnostics_stream_json, encodeNativeSearchConfig, encodeNativeSearchConfigInput, exportRedactionKey, getBinding, getDefaultPipeline, getNativeBindingVersion, getNativePipelineCompatibility, loadDefaultPipeline, loadPipeline, load_prepared_package, native_package_version, normalize_for_search, prepareNativePipelineConfig, prepareNativePipelinePackage, prepareNativeSearchPackage, prepare_search_package, redactDefaultText, redactDefaultTextJson, redact_text, redact_text_json, redact_text_stream_json, summary_diagnostics_json };
751
1034
 
752
1035
  //# sourceMappingURL=wasm.mjs.map