openredaction 1.0.10 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1809 @@
1
+ //#region src/types.d.ts
2
+ /**
3
+ * Core types for OpenRedaction
4
+ */
5
+ /**
6
+ * PII pattern definition with validation
7
+ */
8
+ interface PIIPattern {
9
+ /** Pattern type identifier (e.g., "EMAIL", "PHONE_UK_MOBILE") */
10
+ type: string;
11
+ /** Regular expression for matching */
12
+ regex: RegExp;
13
+ /** Priority for detection order (higher = checked first) */
14
+ priority: number;
15
+ /** Optional validator function for false positive reduction */
16
+ validator?: (match: string, context: string) => boolean;
17
+ /** Placeholder template (e.g., "[EMAIL_{n}]") */
18
+ placeholder: string;
19
+ /** Optional description */
20
+ description?: string;
21
+ /** Severity level */
22
+ severity?: 'critical' | 'high' | 'medium' | 'low';
23
+ }
24
+ /**
25
+ * Detected PII instance
26
+ */
27
+ interface PIIDetection {
28
+ /** Type of PII detected */
29
+ type: string;
30
+ /** Original detected value */
31
+ value: string;
32
+ /** Placeholder used for redaction */
33
+ placeholder: string;
34
+ /** Position in text [start, end] */
35
+ position: [number, number];
36
+ /** Severity level */
37
+ severity: 'critical' | 'high' | 'medium' | 'low';
38
+ /** Confidence score (0-1) based on context analysis */
39
+ confidence?: number;
40
+ }
41
+ /**
42
+ * Detection result
43
+ */
44
+ interface DetectionResult {
45
+ /** Original text */
46
+ original: string;
47
+ /** Redacted text */
48
+ redacted: string;
49
+ /** Array of detections */
50
+ detections: PIIDetection[];
51
+ /** Map of placeholders to original values for restoration */
52
+ redactionMap: Record<string, string>;
53
+ /** Statistics */
54
+ stats?: {
55
+ /** Processing time in milliseconds */processingTime?: number; /** Total PII count */
56
+ piiCount: number;
57
+ };
58
+ }
59
+ /**
60
+ * Redaction mode - controls how PII is replaced
61
+ */
62
+ type RedactionMode = 'placeholder' | 'mask-middle' | 'mask-all' | 'format-preserving' | 'token-replace';
63
+ /**
64
+ * Configuration options for OpenRedaction
65
+ */
66
+ type PresetName = 'gdpr' | 'hipaa' | 'ccpa' | 'healthcare' | 'healthcare-provider' | 'healthcare-research' | 'finance' | 'financial-services' | 'education' | 'transport-logistics' | 'transportation' | 'logistics' | 'pci-dss' | 'soc2';
67
+ interface OpenRedactionOptions {
68
+ /** Include name detection (default: true) */
69
+ includeNames?: boolean;
70
+ /** Include address detection (default: true) */
71
+ includeAddresses?: boolean;
72
+ /** Include phone detection (default: true) */
73
+ includePhones?: boolean;
74
+ /** Include email detection (default: true) */
75
+ includeEmails?: boolean;
76
+ /** Pattern categories to include (e.g., ['personal', 'financial']) */
77
+ categories?: string[];
78
+ /** Whitelist specific patterns only */
79
+ patterns?: string[];
80
+ /** Add custom patterns */
81
+ customPatterns?: PIIPattern[];
82
+ /** Whitelist of terms to ignore (e.g., company names) */
83
+ whitelist?: string[];
84
+ /** Enable deterministic placeholders (default: true) */
85
+ deterministic?: boolean;
86
+ /** Redaction mode (default: 'placeholder') */
87
+ redactionMode?: RedactionMode;
88
+ /** Compliance preset */
89
+ preset?: PresetName;
90
+ /** Enable context-aware detection (default: true) */
91
+ enableContextAnalysis?: boolean;
92
+ /** Minimum confidence threshold for detections (0-1, default: 0.5) */
93
+ confidenceThreshold?: number;
94
+ /** Enable false positive filtering (default: false, experimental) */
95
+ enableFalsePositiveFilter?: boolean;
96
+ /** False positive confidence threshold (0-1, default: 0.7) */
97
+ falsePositiveThreshold?: number;
98
+ /** Enable multi-pass detection for better accuracy (default: false, experimental) */
99
+ enableMultiPass?: boolean;
100
+ /** Number of detection passes (2-5, default: 3) */
101
+ multiPassCount?: number;
102
+ /** Enable result caching for repeated inputs (default: false) */
103
+ enableCache?: boolean;
104
+ /** Maximum cache size (default: 100) */
105
+ cacheSize?: number;
106
+ /** Enable debug logging (default: false) */
107
+ debug?: boolean;
108
+ /** Enable audit logging (default: false) */
109
+ enableAuditLog?: boolean;
110
+ /** Audit logger instance (optional, default: in-memory logger) */
111
+ auditLogger?: IAuditLogger;
112
+ /** User context for audit logs */
113
+ auditUser?: string;
114
+ /** Session ID for audit logs */
115
+ auditSessionId?: string;
116
+ /** Additional metadata for audit logs */
117
+ auditMetadata?: Record<string, unknown>;
118
+ /** Enable metrics collection (default: false) */
119
+ enableMetrics?: boolean;
120
+ /** Metrics collector instance (optional, default: in-memory collector) */
121
+ metricsCollector?: IMetricsCollector;
122
+ /** Enable RBAC (Role-Based Access Control) (default: false) */
123
+ enableRBAC?: boolean;
124
+ /** RBAC manager instance (optional, default: admin role) */
125
+ rbacManager?: IRBACManager;
126
+ /** Predefined role name (admin, analyst, operator, viewer) */
127
+ role?: RoleName;
128
+ }
129
+ /**
130
+ * Audit log entry for tracking redaction operations
131
+ */
132
+ interface AuditLogEntry {
133
+ /** Unique identifier for this audit entry */
134
+ id: string;
135
+ /** Timestamp of the operation (ISO 8601) */
136
+ timestamp: string;
137
+ /** Operation type */
138
+ operation: 'redact' | 'detect' | 'restore';
139
+ /** Number of PII items found/processed */
140
+ piiCount: number;
141
+ /** Types of PII detected (e.g., ["EMAIL", "SSN", "PHONE"]) */
142
+ piiTypes: string[];
143
+ /** Text length processed */
144
+ textLength: number;
145
+ /** Processing time in milliseconds */
146
+ processingTimeMs: number;
147
+ /** Redaction mode used */
148
+ redactionMode?: RedactionMode;
149
+ /** Success status */
150
+ success: boolean;
151
+ /** Error message if operation failed */
152
+ error?: string;
153
+ /** Optional user context */
154
+ user?: string;
155
+ /** Optional session/request identifier */
156
+ sessionId?: string;
157
+ /** Optional metadata */
158
+ metadata?: Record<string, unknown>;
159
+ }
160
+ /**
161
+ * Audit logger interface
162
+ */
163
+ interface IAuditLogger {
164
+ /** Log an audit entry */
165
+ log(entry: Omit<AuditLogEntry, 'id' | 'timestamp'>): void;
166
+ /** Get all audit logs */
167
+ getLogs(): AuditLogEntry[];
168
+ /** Get audit logs filtered by operation type */
169
+ getLogsByOperation(operation: AuditLogEntry['operation']): AuditLogEntry[];
170
+ /** Get audit logs filtered by date range */
171
+ getLogsByDateRange(startDate: Date, endDate: Date): AuditLogEntry[];
172
+ /** Export audit logs as JSON */
173
+ exportAsJson(): string;
174
+ /** Export audit logs as CSV */
175
+ exportAsCsv(): string;
176
+ /** Clear all audit logs */
177
+ clear(): void;
178
+ /** Get audit statistics */
179
+ getStats(): AuditStats;
180
+ }
181
+ /**
182
+ * Audit statistics
183
+ */
184
+ interface AuditStats {
185
+ /** Total number of operations */
186
+ totalOperations: number;
187
+ /** Total PII items detected */
188
+ totalPiiDetected: number;
189
+ /** Average processing time in milliseconds */
190
+ averageProcessingTime: number;
191
+ /** Most common PII types */
192
+ topPiiTypes: Array<{
193
+ type: string;
194
+ count: number;
195
+ }>;
196
+ /** Operations by type */
197
+ operationsByType: Record<string, number>;
198
+ /** Success rate (0-1) */
199
+ successRate: number;
200
+ }
201
+ /**
202
+ * Metrics for monitoring redaction operations
203
+ */
204
+ interface RedactionMetrics {
205
+ /** Total number of redaction operations */
206
+ totalRedactions: number;
207
+ /** Total number of PII items detected */
208
+ totalPiiDetected: number;
209
+ /** Total processing time in milliseconds */
210
+ totalProcessingTime: number;
211
+ /** Average processing time in milliseconds */
212
+ averageProcessingTime: number;
213
+ /** Total text length processed (characters) */
214
+ totalTextLength: number;
215
+ /** PII detection counts by type */
216
+ piiByType: Record<string, number>;
217
+ /** Operation counts by redaction mode */
218
+ byRedactionMode: Record<string, number>;
219
+ /** Error count */
220
+ totalErrors: number;
221
+ /** Timestamp of last update */
222
+ lastUpdated: string;
223
+ }
224
+ /**
225
+ * Metrics exporter interface
226
+ */
227
+ interface IMetricsExporter {
228
+ /** Export metrics in Prometheus format */
229
+ exportPrometheus(metrics: RedactionMetrics, prefix?: string): string;
230
+ /** Export metrics in StatsD format */
231
+ exportStatsD(metrics: RedactionMetrics, prefix?: string): string[];
232
+ /** Get current metrics snapshot */
233
+ getMetrics(): RedactionMetrics;
234
+ /** Reset all metrics */
235
+ reset(): void;
236
+ }
237
+ /**
238
+ * Metrics collector interface
239
+ */
240
+ interface IMetricsCollector {
241
+ /** Record a redaction operation */
242
+ recordRedaction(result: DetectionResult, processingTimeMs: number, redactionMode: RedactionMode): void;
243
+ /** Record an error */
244
+ recordError(): void;
245
+ /** Get metrics exporter */
246
+ getExporter(): IMetricsExporter;
247
+ }
248
+ /**
249
+ * RBAC Permission - granular access control
250
+ */
251
+ type Permission = 'pattern:read' | 'pattern:write' | 'pattern:delete' | 'detection:detect' | 'detection:redact' | 'detection:restore' | 'audit:read' | 'audit:export' | 'audit:delete' | 'metrics:read' | 'metrics:export' | 'metrics:reset' | 'config:read' | 'config:write';
252
+ /**
253
+ * RBAC Role - collection of permissions
254
+ */
255
+ interface Role {
256
+ /** Role identifier */
257
+ name: string;
258
+ /** Role description */
259
+ description?: string;
260
+ /** Permissions granted to this role */
261
+ permissions: Permission[];
262
+ }
263
+ /**
264
+ * Predefined role names
265
+ */
266
+ type RoleName = 'admin' | 'analyst' | 'operator' | 'viewer' | 'custom';
267
+ /**
268
+ * RBAC manager interface for access control
269
+ */
270
+ interface IRBACManager {
271
+ /** Check if user has specific permission */
272
+ hasPermission(permission: Permission): boolean;
273
+ /** Check if user has all specified permissions */
274
+ hasAllPermissions(permissions: Permission[]): boolean;
275
+ /** Check if user has any of the specified permissions */
276
+ hasAnyPermission(permissions: Permission[]): boolean;
277
+ /** Get current role */
278
+ getRole(): Role;
279
+ /** Set role */
280
+ setRole(role: Role): void;
281
+ /** Get all permissions for current role */
282
+ getPermissions(): Permission[];
283
+ /** Filter patterns based on read permissions */
284
+ filterPatterns(patterns: PIIPattern[]): PIIPattern[];
285
+ }
286
+ //#endregion
287
+ //#region src/learning/LocalLearningStore.d.ts
288
+ interface WhitelistEntry {
289
+ pattern: string;
290
+ confidence: number;
291
+ occurrences: number;
292
+ firstSeen: number;
293
+ lastSeen: number;
294
+ contexts: string[];
295
+ }
296
+ interface PatternAdjustment {
297
+ type: string;
298
+ issue: string;
299
+ suggestion: string;
300
+ confidence: number;
301
+ examples: string[];
302
+ occurrences: number;
303
+ }
304
+ interface LearningStats {
305
+ totalDetections: number;
306
+ falsePositives: number;
307
+ falseNegatives: number;
308
+ accuracy: number;
309
+ lastUpdated: number;
310
+ }
311
+ interface LearningData {
312
+ version: string;
313
+ whitelist: WhitelistEntry[];
314
+ patternAdjustments: PatternAdjustment[];
315
+ stats: LearningStats;
316
+ }
317
+ declare class LocalLearningStore {
318
+ private filePath;
319
+ private data;
320
+ private autoSave;
321
+ private confidenceThreshold;
322
+ constructor(filePath?: string, options?: {
323
+ autoSave?: boolean;
324
+ confidenceThreshold?: number;
325
+ });
326
+ /**
327
+ * Load learning data from file
328
+ */
329
+ private load;
330
+ /**
331
+ * Save learning data to file
332
+ */
333
+ private save;
334
+ /**
335
+ * Record a false positive detection
336
+ */
337
+ recordFalsePositive(text: string, _type: string, context: string): void;
338
+ /**
339
+ * Record a false negative (missed detection)
340
+ */
341
+ recordFalseNegative(text: string, type: string, _context: string): void;
342
+ /**
343
+ * Record a correct detection
344
+ */
345
+ recordCorrectDetection(): void;
346
+ /**
347
+ * Update accuracy calculation
348
+ */
349
+ private updateAccuracy;
350
+ /**
351
+ * Get whitelist entries above confidence threshold
352
+ */
353
+ getWhitelist(): string[];
354
+ /**
355
+ * Get all whitelist entries with metadata
356
+ */
357
+ getWhitelistEntries(): WhitelistEntry[];
358
+ /**
359
+ * Get pattern adjustments above confidence threshold
360
+ */
361
+ getPatternAdjustments(): PatternAdjustment[];
362
+ /**
363
+ * Get all pattern adjustments
364
+ */
365
+ getAllPatternAdjustments(): PatternAdjustment[];
366
+ /**
367
+ * Get learning statistics
368
+ */
369
+ getStats(): LearningStats;
370
+ /**
371
+ * Get confidence score for a specific pattern
372
+ */
373
+ getConfidence(pattern: string): number;
374
+ /**
375
+ * Get occurrences count for a specific pattern
376
+ */
377
+ getOccurrences(pattern: string): number;
378
+ /**
379
+ * Manually add pattern to whitelist
380
+ */
381
+ addToWhitelist(pattern: string, confidence?: number): void;
382
+ /**
383
+ * Remove pattern from whitelist
384
+ */
385
+ removeFromWhitelist(pattern: string): void;
386
+ /**
387
+ * Clear all learning data
388
+ */
389
+ clear(): void;
390
+ /**
391
+ * Export learning data (for sharing)
392
+ */
393
+ export(options?: {
394
+ includeContexts?: boolean;
395
+ minConfidence?: number;
396
+ }): LearningData;
397
+ /**
398
+ * Import learning data (merge with existing)
399
+ */
400
+ import(data: LearningData, merge?: boolean): void;
401
+ /**
402
+ * Manually save data
403
+ */
404
+ flush(): void;
405
+ }
406
+ //#endregion
407
+ //#region src/optimizer/PriorityOptimizer.d.ts
408
+ interface PatternStats {
409
+ type: string;
410
+ totalDetections: number;
411
+ falsePositives: number;
412
+ falseNegatives: number;
413
+ accuracy: number;
414
+ priority: number;
415
+ adjustedPriority: number;
416
+ }
417
+ interface OptimizerOptions {
418
+ learningWeight: number;
419
+ minSampleSize: number;
420
+ maxPriorityAdjustment: number;
421
+ }
422
+ /**
423
+ * Priority Optimizer - Dynamically adjusts pattern priorities based on learning data
424
+ */
425
+ declare class PriorityOptimizer {
426
+ private learningStore;
427
+ private options;
428
+ constructor(learningStore: LocalLearningStore, options?: Partial<OptimizerOptions>);
429
+ /**
430
+ * Optimize pattern priorities based on learning data
431
+ */
432
+ optimizePatterns(patterns: PIIPattern[]): PIIPattern[];
433
+ /**
434
+ * Get pattern statistics with learning data
435
+ */
436
+ getPatternStats(patterns: PIIPattern[]): PatternStats[];
437
+ /**
438
+ * Infer pattern type from a whitelisted value
439
+ * This is a heuristic - in production we'd track this explicitly
440
+ */
441
+ private inferPatternType;
442
+ /**
443
+ * Reset all priority adjustments
444
+ */
445
+ resetPriorities(patterns: PIIPattern[]): PIIPattern[];
446
+ /**
447
+ * Get optimizer configuration
448
+ */
449
+ getOptions(): OptimizerOptions;
450
+ /**
451
+ * Update optimizer configuration
452
+ */
453
+ setOptions(options: Partial<OptimizerOptions>): void;
454
+ }
455
+ //#endregion
456
+ //#region src/document/types.d.ts
457
+ /**
458
+ * Supported document formats
459
+ */
460
+ type DocumentFormat = 'pdf' | 'docx' | 'txt' | 'image' | 'json' | 'csv' | 'xlsx';
461
+ /**
462
+ * OCR language codes (Tesseract format)
463
+ */
464
+ type OCRLanguage = 'eng' | 'spa' | 'fra' | 'deu' | 'por' | 'ita' | 'rus' | 'chi_sim' | 'chi_tra' | 'jpn' | 'kor';
465
+ /**
466
+ * OCR options
467
+ */
468
+ interface OCROptions {
469
+ /** OCR language (default: 'eng' for English) */
470
+ language?: OCRLanguage | OCRLanguage[];
471
+ /** OCR engine mode (0-3, default: 3 for best accuracy) */
472
+ oem?: 0 | 1 | 2 | 3;
473
+ /** Page segmentation mode (0-13, default: 3 for automatic) */
474
+ psm?: number;
475
+ }
476
+ /**
477
+ * Document processing options
478
+ */
479
+ interface DocumentOptions {
480
+ /** Document format (auto-detected if not specified) */
481
+ format?: DocumentFormat;
482
+ /** Extract text from specific pages (PDF only, 1-indexed) */
483
+ pages?: number[];
484
+ /** Password for encrypted PDFs */
485
+ password?: string;
486
+ /** Maximum document size in bytes (default: 50MB) */
487
+ maxSize?: number;
488
+ /** Enable OCR for image-based content (default: false) */
489
+ enableOCR?: boolean;
490
+ /** OCR configuration options */
491
+ ocrOptions?: OCROptions;
492
+ }
493
+ /**
494
+ * Document processing result
495
+ */
496
+ interface DocumentResult {
497
+ /** Extracted text from document */
498
+ text: string;
499
+ /** Document metadata */
500
+ metadata: DocumentMetadata;
501
+ /** Detection result with PII findings */
502
+ detection: DetectionResult;
503
+ /** Original file size in bytes */
504
+ fileSize: number;
505
+ /** Text extraction time in milliseconds */
506
+ extractionTime: number;
507
+ }
508
+ /**
509
+ * Document metadata
510
+ */
511
+ interface DocumentMetadata {
512
+ /** Document format */
513
+ format: DocumentFormat;
514
+ /** Number of pages (if applicable) */
515
+ pages?: number;
516
+ /** Document title */
517
+ title?: string;
518
+ /** Document author */
519
+ author?: string;
520
+ /** Creation date */
521
+ creationDate?: Date;
522
+ /** Last modified date */
523
+ modifiedDate?: Date;
524
+ /** OCR confidence (0-100) if OCR was used */
525
+ ocrConfidence?: number;
526
+ /** Whether OCR was used for extraction */
527
+ usedOCR?: boolean;
528
+ /** Additional custom metadata */
529
+ custom?: Record<string, unknown>;
530
+ }
531
+ //#endregion
532
+ //#region src/context/ContextRules.d.ts
533
+ /**
534
+ * Proximity rule for context-based confidence adjustment
535
+ */
536
+ interface ProximityRule {
537
+ /** Pattern type this rule applies to (e.g., 'EMAIL', 'PHONE', 'SSN') */
538
+ patternType: string | string[];
539
+ /** Keywords to look for near the match */
540
+ keywords: string[];
541
+ /** Maximum word distance from match (default: 10) */
542
+ proximityWindow?: number;
543
+ /** Confidence boost if keyword found (0-1) */
544
+ confidenceBoost?: number;
545
+ /** Confidence penalty if keyword found (0-1) */
546
+ confidencePenalty?: number;
547
+ /** Whether match must come AFTER keyword (default: both directions) */
548
+ keywordBefore?: boolean;
549
+ /** Whether match must come BEFORE keyword (default: both directions) */
550
+ keywordAfter?: boolean;
551
+ /** Rule description */
552
+ description?: string;
553
+ }
554
+ /**
555
+ * Domain-specific vocabulary for context detection
556
+ */
557
+ interface DomainVocabulary {
558
+ /** Domain name */
559
+ domain: 'medical' | 'legal' | 'financial' | 'technical' | 'hr' | 'custom';
560
+ /** Domain-specific terms */
561
+ terms: string[];
562
+ /** Pattern types to boost in this domain */
563
+ boostPatterns?: string[];
564
+ /** Confidence boost amount (default: 0.15) */
565
+ boostAmount?: number;
566
+ }
567
+ /**
568
+ * Contextual rules configuration
569
+ */
570
+ interface ContextRulesConfig {
571
+ /** Proximity rules */
572
+ proximityRules?: ProximityRule[];
573
+ /** Domain vocabularies */
574
+ domainVocabularies?: DomainVocabulary[];
575
+ /** Enable default rules (default: true) */
576
+ useDefaultRules?: boolean;
577
+ }
578
+ //#endregion
579
+ //#region src/context/ContextAnalyzer.d.ts
580
+ /**
581
+ * Context Analysis for PII Detection
582
+ * Provides NLP-lite features to reduce false positives
583
+ */
584
+ interface ContextAnalysis {
585
+ /** 5 words before detection */
586
+ beforeWords: string[];
587
+ /** 5 words after detection */
588
+ afterWords: string[];
589
+ /** Full sentence containing detection */
590
+ sentence: string;
591
+ /** Inferred document type */
592
+ documentType: 'email' | 'document' | 'code' | 'chat' | 'unknown';
593
+ /** Confidence that this is actual PII (0-1) */
594
+ confidence: number;
595
+ }
596
+ //#endregion
597
+ //#region src/explain/ExplainAPI.d.ts
598
+ /**
599
+ * Pattern match result for explain
600
+ */
601
+ interface PatternMatchResult {
602
+ /** Pattern that was tested */
603
+ pattern: PIIPattern;
604
+ /** Whether the pattern matched */
605
+ matched: boolean;
606
+ /** Matched value (if matched) */
607
+ matchedValue?: string;
608
+ /** Position of match (if matched) */
609
+ position?: [number, number];
610
+ /** Why it didn't match or was filtered */
611
+ reason?: string;
612
+ /** Validator result (if validator exists) */
613
+ validatorPassed?: boolean;
614
+ /** Context analysis (if enabled) */
615
+ contextAnalysis?: ContextAnalysis;
616
+ /** False positive check (if enabled) */
617
+ falsePositiveCheck?: {
618
+ isFalsePositive: boolean;
619
+ confidence: number;
620
+ reason?: string;
621
+ };
622
+ }
623
+ /**
624
+ * Explanation for a specific text
625
+ */
626
+ interface TextExplanation {
627
+ /** Original text */
628
+ text: string;
629
+ /** All pattern match results */
630
+ patternResults: PatternMatchResult[];
631
+ /** Patterns that matched */
632
+ matchedPatterns: PatternMatchResult[];
633
+ /** Patterns that didn't match */
634
+ unmatchedPatterns: PatternMatchResult[];
635
+ /** Patterns that matched but were filtered */
636
+ filteredPatterns: PatternMatchResult[];
637
+ /** Final detections */
638
+ detections: PIIDetection[];
639
+ /** Summary statistics */
640
+ summary: {
641
+ totalPatternsChecked: number;
642
+ patternsMatched: number;
643
+ patternsFiltered: number;
644
+ finalDetections: number;
645
+ };
646
+ }
647
+ /**
648
+ * Explain API for debugging
649
+ */
650
+ declare class ExplainAPI {
651
+ private detector;
652
+ private patterns;
653
+ private options;
654
+ constructor(detector: OpenRedaction);
655
+ /**
656
+ * Explain why text was or wasn't detected as PII
657
+ */
658
+ explain(text: string): Promise<TextExplanation>;
659
+ /**
660
+ * Explain a specific detection
661
+ */
662
+ explainDetection(detection: PIIDetection, text: string): Promise<{
663
+ detection: PIIDetection;
664
+ pattern?: PIIPattern;
665
+ contextAnalysis?: ContextAnalysis;
666
+ reasoning: string[];
667
+ suggestions: string[];
668
+ }>;
669
+ /**
670
+ * Suggest why text wasn't detected
671
+ */
672
+ suggestWhy(text: string, expectedType: string): Promise<{
673
+ text: string;
674
+ expectedType: string;
675
+ suggestions: string[];
676
+ similarPatterns: PIIPattern[];
677
+ }>;
678
+ /**
679
+ * Get debugging information for entire detection process
680
+ */
681
+ debug(text: string): Promise<{
682
+ text: string;
683
+ textLength: number;
684
+ enabledFeatures: string[];
685
+ patternCount: number;
686
+ explanation: TextExplanation;
687
+ performance: {
688
+ estimatedTime: string;
689
+ };
690
+ }>;
691
+ }
692
+ //#endregion
693
+ //#region src/reports/ReportGenerator.d.ts
694
+ /**
695
+ * Report format options
696
+ */
697
+ type ReportFormat = 'html' | 'markdown';
698
+ /**
699
+ * Report type options
700
+ */
701
+ type ReportType = 'summary' | 'detailed' | 'compliance';
702
+ /**
703
+ * Report generation options
704
+ */
705
+ interface ReportOptions {
706
+ /** Report format */
707
+ format: ReportFormat;
708
+ /** Report type */
709
+ type?: ReportType;
710
+ /** Report title */
711
+ title?: string;
712
+ /** Include original text (default: false for privacy) */
713
+ includeOriginalText?: boolean;
714
+ /** Include redacted text (default: true) */
715
+ includeRedactedText?: boolean;
716
+ /** Include detection details (default: true) */
717
+ includeDetectionDetails?: boolean;
718
+ /** Include statistics (default: true) */
719
+ includeStatistics?: boolean;
720
+ /** Include explanation (requires ExplainAPI, default: false) */
721
+ includeExplanation?: boolean;
722
+ /** Company/project name for compliance reports */
723
+ organizationName?: string;
724
+ /** Additional metadata */
725
+ metadata?: Record<string, string>;
726
+ }
727
+ //#endregion
728
+ //#region src/detector.d.ts
729
+ declare class OpenRedaction {
730
+ private patterns;
731
+ private compiledPatterns;
732
+ private options;
733
+ private multiPassConfig?;
734
+ private resultCache?;
735
+ private valueToPlaceholder;
736
+ private placeholderCounter;
737
+ private learningStore?;
738
+ private priorityOptimizer?;
739
+ private enableLearning;
740
+ private auditLogger?;
741
+ private auditUser?;
742
+ private auditSessionId?;
743
+ private auditMetadata?;
744
+ private metricsCollector?;
745
+ private rbacManager?;
746
+ private nerDetector?;
747
+ private contextRulesEngine?;
748
+ private severityClassifier;
749
+ constructor(options?: OpenRedactionOptions & {
750
+ configPath?: string;
751
+ enableLearning?: boolean;
752
+ learningStorePath?: string;
753
+ enablePriorityOptimization?: boolean;
754
+ optimizerOptions?: Partial<OptimizerOptions>;
755
+ enableNER?: boolean;
756
+ enableContextRules?: boolean;
757
+ contextRulesConfig?: ContextRulesConfig;
758
+ maxInputSize?: number;
759
+ regexTimeout?: number;
760
+ });
761
+ /**
762
+ * Create OpenRedaction instance from config file
763
+ */
764
+ static fromConfig(configPath?: string): Promise<OpenRedaction>;
765
+ /**
766
+ * Build the list of patterns based on options
767
+ * Supports three filtering modes (in order of priority):
768
+ * 1. Specific pattern types (patterns option)
769
+ * 2. Pattern categories (categories option) - NEW!
770
+ * 3. All patterns with type-specific filters (includeNames, etc.)
771
+ */
772
+ private buildPatternList;
773
+ /**
774
+ * Validate all patterns to prevent malicious regex injection
775
+ * ONLY validates custom patterns - built-in patterns are already vetted
776
+ * Timeout protection in safeExec() is the primary defense against ReDoS
777
+ */
778
+ private validatePatterns;
779
+ /**
780
+ * Pre-compile all regex patterns for performance
781
+ * Avoids creating new RegExp objects on every detect() call
782
+ */
783
+ private precompilePatterns;
784
+ /**
785
+ * Process patterns and detect PII
786
+ * Used by both single-pass and multi-pass detection
787
+ */
788
+ private processPatterns;
789
+ /**
790
+ * Detect PII in text
791
+ * Async API for detection pipeline (NER, multi-pass, etc.)
792
+ */
793
+ detect(text: string): Promise<DetectionResult>;
794
+ /**
795
+ * Restore redacted text using redaction map
796
+ */
797
+ restore(redactedText: string, redactionMap: Record<string, string>): string;
798
+ /**
799
+ * Generate placeholder for a detected value
800
+ */
801
+ private generatePlaceholder;
802
+ /**
803
+ * Check if a range overlaps with existing detections
804
+ */
805
+ private overlapsWithExisting;
806
+ /**
807
+ * Escape special regex characters
808
+ */
809
+ private escapeRegex;
810
+ /**
811
+ * Get the list of active patterns
812
+ */
813
+ getPatterns(): PIIPattern[];
814
+ /**
815
+ * Get severity-based scan results
816
+ */
817
+ scan(text: string): Promise<{
818
+ high: PIIDetection[];
819
+ medium: PIIDetection[];
820
+ low: PIIDetection[];
821
+ total: number;
822
+ }>;
823
+ /**
824
+ * Record a false positive (incorrectly detected as PII)
825
+ */
826
+ recordFalsePositive(detection: PIIDetection, context?: string): void;
827
+ /**
828
+ * Record a false negative (missed PII that should have been detected)
829
+ */
830
+ recordFalseNegative(text: string, expectedType: string, context?: string): void;
831
+ /**
832
+ * Record a correct detection (for accuracy tracking)
833
+ */
834
+ recordCorrectDetection(): void;
835
+ /**
836
+ * Get learning statistics
837
+ */
838
+ getLearningStats(): LearningStats | null;
839
+ /**
840
+ * Get learned whitelist entries
841
+ */
842
+ getLearnedWhitelist(): WhitelistEntry[];
843
+ /**
844
+ * Get pattern adjustment suggestions
845
+ */
846
+ getPatternAdjustments(): PatternAdjustment[];
847
+ /**
848
+ * Export learned patterns for sharing
849
+ */
850
+ exportLearnings(options?: {
851
+ includeContexts?: boolean;
852
+ minConfidence?: number;
853
+ }): LearningData | null;
854
+ /**
855
+ * Import learned patterns from another source
856
+ */
857
+ importLearnings(data: any, merge?: boolean): void;
858
+ /**
859
+ * Manually add a term to the whitelist
860
+ */
861
+ addToWhitelist(pattern: string, confidence?: number): void;
862
+ /**
863
+ * Remove a term from the whitelist
864
+ */
865
+ removeFromWhitelist(pattern: string): void;
866
+ /**
867
+ * Get the learning store instance
868
+ */
869
+ getLearningStore(): LocalLearningStore | undefined;
870
+ /**
871
+ * Get the priority optimizer instance
872
+ */
873
+ getPriorityOptimizer(): PriorityOptimizer | undefined;
874
+ /**
875
+ * Optimize pattern priorities based on learning data
876
+ * Call this to re-optimize priorities after accumulating new learning data
877
+ */
878
+ optimizePriorities(): void;
879
+ /**
880
+ * Get pattern statistics with learning data
881
+ */
882
+ getPatternStats(): PatternStats[] | null;
883
+ /**
884
+ * Clear the result cache (if caching is enabled)
885
+ */
886
+ clearCache(): void;
887
+ /**
888
+ * Get cache statistics
889
+ */
890
+ getCacheStats(): {
891
+ size: number;
892
+ maxSize: number;
893
+ enabled: boolean;
894
+ };
895
+ /**
896
+ * Get the audit logger instance (if audit logging is enabled)
897
+ */
898
+ getAuditLogger(): IAuditLogger | undefined;
899
+ /**
900
+ * Get the metrics collector instance (if metrics collection is enabled)
901
+ */
902
+ getMetricsCollector(): IMetricsCollector | undefined;
903
+ /**
904
+ * Get the RBAC manager instance (if RBAC is enabled)
905
+ */
906
+ getRBACManager(): IRBACManager | undefined;
907
+ /**
908
+ * Create an explain API for debugging detections
909
+ */
910
+ explain(): ExplainAPI;
911
+ /**
912
+ * Generate a report from detection results
913
+ */
914
+ generateReport(result: DetectionResult, options: ReportOptions): string;
915
+ /**
916
+ * Export current configuration
917
+ */
918
+ exportConfig(metadata?: {
919
+ description?: string;
920
+ author?: string;
921
+ tags?: string[];
922
+ }): string;
923
+ /**
924
+ * Run health check
925
+ */
926
+ healthCheck(options?: {
927
+ testDetection?: boolean;
928
+ checkPerformance?: boolean;
929
+ performanceThreshold?: number;
930
+ memoryThreshold?: number;
931
+ }): Promise<any>;
932
+ /**
933
+ * Quick health check (minimal overhead)
934
+ */
935
+ quickHealthCheck(): Promise<{
936
+ status: 'healthy' | 'unhealthy';
937
+ message: string;
938
+ }>;
939
+ /**
940
+ * Detect PII in a document (PDF, DOCX, TXT)
941
+ * Requires optional peer dependencies:
942
+ * - pdf-parse for PDF support
943
+ * - mammoth for DOCX support
944
+ */
945
+ detectDocument(buffer: Buffer, options?: DocumentOptions): Promise<DocumentResult>;
946
+ /**
947
+ * Detect PII in a document file from filesystem
948
+ * Convenience method that reads file and calls detectDocument
949
+ */
950
+ detectDocumentFile(filePath: string, options?: DocumentOptions): Promise<DocumentResult>;
951
+ /**
952
+ * Batch detect PII in multiple texts using worker threads (parallel)
953
+ * Significantly faster for processing many texts
954
+ */
955
+ static detectBatch(texts: string[], options?: OpenRedactionOptions & {
956
+ numWorkers?: number;
957
+ }): Promise<DetectionResult[]>;
958
+ /**
959
+ * Batch process multiple documents using worker threads (parallel)
960
+ * Efficient for processing many documents at once
961
+ */
962
+ static detectDocumentsBatch(buffers: Buffer[], options?: DocumentOptions & {
963
+ numWorkers?: number;
964
+ }): Promise<DocumentResult[]>;
965
+ }
966
+ //#endregion
967
+ //#region src/tenancy/TenantManager.d.ts
968
+ /**
969
+ * Tenant configuration
970
+ */
971
+ interface TenantConfig {
972
+ /** Tenant unique identifier */
973
+ tenantId: string;
974
+ /** Tenant display name */
975
+ name: string;
976
+ /** Tenant-specific OpenRedaction options */
977
+ options?: OpenRedactionOptions;
978
+ /** Tenant-specific custom patterns */
979
+ customPatterns?: PIIPattern[];
980
+ /** Tenant-specific whitelist */
981
+ whitelist?: string[];
982
+ /** Tenant quota limits */
983
+ quotas?: TenantQuotas;
984
+ /** Tenant API key (for authentication) */
985
+ apiKey?: string;
986
+ /** Tenant metadata */
987
+ metadata?: Record<string, unknown>;
988
+ /** Tenant status */
989
+ status: 'active' | 'suspended' | 'trial';
990
+ /** Trial expiry date (for trial tenants) */
991
+ trialExpiresAt?: Date;
992
+ /** Created timestamp */
993
+ createdAt: Date;
994
+ /** Last updated timestamp */
995
+ updatedAt: Date;
996
+ }
997
+ /**
998
+ * Tenant quota limits
999
+ */
1000
+ interface TenantQuotas {
1001
+ /** Maximum requests per month (undefined = unlimited) */
1002
+ maxRequestsPerMonth?: number;
1003
+ /** Maximum text length per request (characters) */
1004
+ maxTextLength?: number;
1005
+ /** Maximum patterns allowed */
1006
+ maxPatterns?: number;
1007
+ /** Maximum audit logs to retain */
1008
+ maxAuditLogs?: number;
1009
+ /** Rate limit: requests per minute */
1010
+ rateLimit?: number;
1011
+ }
1012
+ /**
1013
+ * Tenant usage statistics
1014
+ */
1015
+ interface TenantUsage {
1016
+ /** Tenant ID */
1017
+ tenantId: string;
1018
+ /** Total requests this month */
1019
+ requestsThisMonth: number;
1020
+ /** Total text processed this month (characters) */
1021
+ textProcessedThisMonth: number;
1022
+ /** Total PII detected this month */
1023
+ piiDetectedThisMonth: number;
1024
+ /** Last request timestamp */
1025
+ lastRequestAt?: Date;
1026
+ /** Monthly usage reset date */
1027
+ monthlyResetDate: Date;
1028
+ }
1029
+ /**
1030
+ * Multi-tenant manager for SaaS deployments
1031
+ */
1032
+ declare class TenantManager {
1033
+ private tenants;
1034
+ private usage;
1035
+ private detectors;
1036
+ private rateLimitTracking;
1037
+ private auditLoggers;
1038
+ private metricsCollectors;
1039
+ /**
1040
+ * Register a new tenant
1041
+ */
1042
+ registerTenant(config: Omit<TenantConfig, 'createdAt' | 'updatedAt'>): TenantConfig;
1043
+ /**
1044
+ * Update tenant configuration
1045
+ */
1046
+ updateTenant(tenantId: string, updates: Partial<Omit<TenantConfig, 'tenantId' | 'createdAt'>>): TenantConfig;
1047
+ /**
1048
+ * Get tenant configuration
1049
+ */
1050
+ getTenantConfig(tenantId: string): TenantConfig;
1051
+ /**
1052
+ * Get or create tenant-specific detector instance
1053
+ */
1054
+ getDetector(tenantId: string): OpenRedaction;
1055
+ /**
1056
+ * Perform detection with tenant isolation and quota checks
1057
+ */
1058
+ detect(tenantId: string, text: string): Promise<any>;
1059
+ /**
1060
+ * Validate tenant status (active, trial expiry)
1061
+ */
1062
+ private validateTenantStatus;
1063
+ /**
1064
+ * Check tenant quotas before processing
1065
+ */
1066
+ private checkQuotas;
1067
+ /**
1068
+ * Track request for usage and rate limiting
1069
+ */
1070
+ private trackRequest;
1071
+ /**
1072
+ * Get number of requests in last minute
1073
+ */
1074
+ private getRequestsInLastMinute;
1075
+ /**
1076
+ * Reset monthly usage statistics
1077
+ */
1078
+ private resetMonthlyUsage;
1079
+ /**
1080
+ * Get next monthly reset date (1st of next month)
1081
+ */
1082
+ private getNextMonthlyResetDate;
1083
+ /**
1084
+ * Get tenant usage statistics
1085
+ */
1086
+ getTenantUsage(tenantId: string): TenantUsage;
1087
+ /**
1088
+ * Get all tenants
1089
+ */
1090
+ getAllTenants(): TenantConfig[];
1091
+ /**
1092
+ * Get tenants by status
1093
+ */
1094
+ getTenantsByStatus(status: TenantConfig['status']): TenantConfig[];
1095
+ /**
1096
+ * Authenticate tenant by API key
1097
+ */
1098
+ authenticateByApiKey(apiKey: string): TenantConfig | null;
1099
+ /**
1100
+ * Suspend a tenant
1101
+ */
1102
+ suspendTenant(tenantId: string): void;
1103
+ /**
1104
+ * Activate a tenant
1105
+ */
1106
+ activateTenant(tenantId: string): void;
1107
+ /**
1108
+ * Delete a tenant and all associated data
1109
+ */
1110
+ deleteTenant(tenantId: string): void;
1111
+ /**
1112
+ * Set tenant-specific audit logger
1113
+ */
1114
+ setAuditLogger(tenantId: string, logger: IAuditLogger): void;
1115
+ /**
1116
+ * Get tenant-specific audit logger
1117
+ */
1118
+ getAuditLogger(tenantId: string): IAuditLogger | undefined;
1119
+ /**
1120
+ * Set tenant-specific metrics collector
1121
+ */
1122
+ setMetricsCollector(tenantId: string, collector: IMetricsCollector): void;
1123
+ /**
1124
+ * Get tenant-specific metrics collector
1125
+ */
1126
+ getMetricsCollector(tenantId: string): IMetricsCollector | undefined;
1127
+ /**
1128
+ * Get aggregate statistics across all tenants
1129
+ */
1130
+ getAggregateStats(): {
1131
+ totalTenants: number;
1132
+ activeTenants: number;
1133
+ trialTenants: number;
1134
+ suspendedTenants: number;
1135
+ totalRequestsThisMonth: number;
1136
+ totalTextProcessedThisMonth: number;
1137
+ totalPiiDetectedThisMonth: number;
1138
+ };
1139
+ /**
1140
+ * Validate tenant exists
1141
+ */
1142
+ private validateTenantExists;
1143
+ /**
1144
+ * Export tenant configuration as JSON
1145
+ */
1146
+ exportTenantConfig(tenantId: string): string;
1147
+ /**
1148
+ * Import tenant configuration from JSON
1149
+ */
1150
+ importTenantConfig(json: string): TenantConfig;
1151
+ }
1152
+ //#endregion
1153
+ //#region src/webhooks/WebhookManager.d.ts
1154
+ /**
1155
+ * Webhook event types
1156
+ */
1157
+ type WebhookEventType = 'pii.detected.high_risk' | 'pii.detected.bulk' | 'pii.processing.failed' | 'pii.processing.slow' | 'quota.exceeded' | 'tenant.suspended' | 'audit.tamper_detected' | 'custom';
1158
+ /**
1159
+ * Webhook event payload
1160
+ */
1161
+ interface WebhookEvent {
1162
+ /** Event unique ID */
1163
+ id: string;
1164
+ /** Event type */
1165
+ type: WebhookEventType;
1166
+ /** Event timestamp (ISO 8601) */
1167
+ timestamp: string;
1168
+ /** Event severity */
1169
+ severity: 'critical' | 'high' | 'medium' | 'low' | 'info';
1170
+ /** Event data */
1171
+ data: Record<string, unknown>;
1172
+ /** Source identifier (e.g., tenant ID) */
1173
+ source?: string;
1174
+ /** Custom metadata */
1175
+ metadata?: Record<string, unknown>;
1176
+ }
1177
+ /**
1178
+ * Webhook configuration
1179
+ */
1180
+ interface WebhookConfig {
1181
+ /** Webhook unique ID */
1182
+ id: string;
1183
+ /** Webhook URL to POST events to */
1184
+ url: string;
1185
+ /** Event types to subscribe to (empty = all events) */
1186
+ events?: WebhookEventType[];
1187
+ /** Minimum severity to trigger webhook */
1188
+ minSeverity?: 'critical' | 'high' | 'medium' | 'low' | 'info';
1189
+ /** Custom headers to include in requests */
1190
+ headers?: Record<string, string>;
1191
+ /** Secret for HMAC signature (optional but recommended) */
1192
+ secret?: string;
1193
+ /** Enable webhook (default: true) */
1194
+ enabled?: boolean;
1195
+ /** Retry configuration */
1196
+ retry?: {
1197
+ /** Maximum retry attempts (default: 3) */maxAttempts?: number; /** Initial delay in ms (default: 1000) */
1198
+ initialDelay?: number; /** Maximum delay in ms (default: 60000) */
1199
+ maxDelay?: number; /** Backoff multiplier (default: 2) */
1200
+ backoffMultiplier?: number;
1201
+ };
1202
+ /** Timeout in ms (default: 5000) */
1203
+ timeout?: number;
1204
+ /** Tenant ID (for multi-tenant setups) */
1205
+ tenantId?: string;
1206
+ }
1207
+ /**
1208
+ * Webhook delivery status
1209
+ */
1210
+ type WebhookDeliveryStatus = 'pending' | 'success' | 'failed' | 'circuit_open';
1211
+ /**
1212
+ * Webhook delivery record
1213
+ */
1214
+ interface WebhookDelivery {
1215
+ /** Delivery ID */
1216
+ id: string;
1217
+ /** Webhook ID */
1218
+ webhookId: string;
1219
+ /** Event that was delivered */
1220
+ event: WebhookEvent;
1221
+ /** Delivery status */
1222
+ status: WebhookDeliveryStatus;
1223
+ /** HTTP status code */
1224
+ statusCode?: number;
1225
+ /** Delivery timestamp */
1226
+ timestamp: Date;
1227
+ /** Attempt number */
1228
+ attempt: number;
1229
+ /** Error message if failed */
1230
+ error?: string;
1231
+ /** Response body */
1232
+ responseBody?: string;
1233
+ /** Delivery duration in ms */
1234
+ durationMs?: number;
1235
+ }
1236
+ /**
1237
+ * Webhook statistics
1238
+ */
1239
+ interface WebhookStats {
1240
+ /** Webhook ID */
1241
+ webhookId: string;
1242
+ /** Total deliveries */
1243
+ totalDeliveries: number;
1244
+ /** Successful deliveries */
1245
+ successfulDeliveries: number;
1246
+ /** Failed deliveries */
1247
+ failedDeliveries: number;
1248
+ /** Average delivery time in ms */
1249
+ avgDeliveryTimeMs: number;
1250
+ /** Last delivery time */
1251
+ lastDeliveryTime?: Date;
1252
+ /** Circuit breaker state */
1253
+ circuitState: 'closed' | 'open' | 'half_open';
1254
+ }
1255
+ /**
1256
+ * Webhook Manager
1257
+ * Manages webhook subscriptions, delivery, retries, and circuit breaking
1258
+ */
1259
+ declare class WebhookManager {
1260
+ private webhooks;
1261
+ private deliveryHistory;
1262
+ private circuitBreakers;
1263
+ private pendingRetries;
1264
+ private maxHistorySize;
1265
+ private readonly FAILURE_THRESHOLD;
1266
+ private readonly RESET_TIMEOUT_MS;
1267
+ constructor(options?: {
1268
+ maxHistorySize?: number;
1269
+ });
1270
+ /**
1271
+ * Register a webhook
1272
+ */
1273
+ registerWebhook(config: WebhookConfig): void;
1274
+ /**
1275
+ * Update webhook configuration
1276
+ */
1277
+ updateWebhook(id: string, updates: Partial<Omit<WebhookConfig, 'id'>>): void;
1278
+ /**
1279
+ * Delete webhook
1280
+ */
1281
+ deleteWebhook(id: string): void;
1282
+ /**
1283
+ * Get webhook configuration
1284
+ */
1285
+ getWebhook(id: string): WebhookConfig | undefined;
1286
+ /**
1287
+ * Get all webhooks
1288
+ */
1289
+ getAllWebhooks(): WebhookConfig[];
1290
+ /**
1291
+ * Emit an event to all subscribed webhooks
1292
+ */
1293
+ emitEvent(event: Omit<WebhookEvent, 'id' | 'timestamp'>): Promise<void>;
1294
+ /**
1295
+ * Emit high-risk PII detection event
1296
+ */
1297
+ emitHighRiskPII(result: DetectionResult, tenantId?: string): Promise<void>;
1298
+ /**
1299
+ * Emit bulk PII detection event
1300
+ */
1301
+ emitBulkPII(result: DetectionResult, threshold?: number, tenantId?: string): Promise<void>;
1302
+ /**
1303
+ * Emit processing error event
1304
+ */
1305
+ emitProcessingError(error: Error, tenantId?: string): Promise<void>;
1306
+ /**
1307
+ * Emit slow processing event
1308
+ */
1309
+ emitSlowProcessing(durationMs: number, threshold?: number, tenantId?: string): Promise<void>;
1310
+ /**
1311
+ * Find webhooks that match the event
1312
+ */
1313
+ private findMatchingWebhooks;
1314
+ /**
1315
+ * Deliver webhook with retry logic
1316
+ */
1317
+ private deliverWebhook;
1318
+ /**
1319
+ * Make HTTP request to webhook URL
1320
+ */
1321
+ private makeHttpRequest;
1322
+ /**
1323
+ * Calculate HMAC signature for webhook verification
1324
+ */
1325
+ private calculateHmacSignature;
1326
+ /**
1327
+ * Calculate retry delay with exponential backoff
1328
+ */
1329
+ private calculateRetryDelay;
1330
+ /**
1331
+ * Record delivery in history
1332
+ */
1333
+ private recordDelivery;
1334
+ /**
1335
+ * Get delivery history for a webhook
1336
+ */
1337
+ getDeliveryHistory(webhookId: string, limit?: number): WebhookDelivery[];
1338
+ /**
1339
+ * Get webhook statistics
1340
+ */
1341
+ getWebhookStats(webhookId: string): WebhookStats;
1342
+ /**
1343
+ * Get aggregate statistics for all webhooks
1344
+ */
1345
+ getAggregateStats(): {
1346
+ totalWebhooks: number;
1347
+ enabledWebhooks: number;
1348
+ totalDeliveries: number;
1349
+ successfulDeliveries: number;
1350
+ failedDeliveries: number;
1351
+ avgDeliveryTimeMs: number;
1352
+ };
1353
+ /**
1354
+ * Clear delivery history
1355
+ */
1356
+ clearHistory(): void;
1357
+ /**
1358
+ * Generate unique ID
1359
+ */
1360
+ private generateId;
1361
+ }
1362
+ //#endregion
1363
+ //#region src/audit/PersistentAuditLogger.d.ts
1364
+ /**
1365
+ * Supported database backends
1366
+ */
1367
+ type AuditBackend = 'sqlite' | 'postgresql' | 'mongodb' | 's3' | 'file';
1368
+ /**
1369
+ * Database connection configuration
1370
+ */
1371
+ interface AuditDatabaseConfig {
1372
+ /** Backend type */
1373
+ backend: AuditBackend;
1374
+ /** Connection string (for PostgreSQL/MongoDB) */
1375
+ connectionString?: string;
1376
+ /** Database file path (for SQLite/file backend) */
1377
+ filePath?: string;
1378
+ /** S3 bucket configuration */
1379
+ s3Config?: {
1380
+ bucket: string;
1381
+ region: string;
1382
+ accessKeyId?: string;
1383
+ secretAccessKey?: string;
1384
+ prefix?: string;
1385
+ };
1386
+ /** Table/collection name (default: 'audit_logs') */
1387
+ tableName?: string;
1388
+ /** Enable compression (default: false) */
1389
+ enableCompression?: boolean;
1390
+ /** Batch size for bulk inserts (default: 100) */
1391
+ batchSize?: number;
1392
+ }
1393
+ /**
1394
+ * Retention policy configuration
1395
+ */
1396
+ interface RetentionPolicy {
1397
+ /** Maximum age of logs in days (default: 90) */
1398
+ maxAgeDays?: number;
1399
+ /** Maximum number of logs to keep (default: unlimited) */
1400
+ maxLogs?: number;
1401
+ /** Enable automatic cleanup (default: false) */
1402
+ autoCleanup?: boolean;
1403
+ /** Cleanup interval in hours (default: 24) */
1404
+ cleanupIntervalHours?: number;
1405
+ }
1406
+ /**
1407
+ * Persistent audit logger options
1408
+ */
1409
+ interface PersistentAuditLoggerOptions {
1410
+ /** Database configuration */
1411
+ database: AuditDatabaseConfig;
1412
+ /** Retention policy */
1413
+ retention?: RetentionPolicy;
1414
+ /** Enable cryptographic hashing for tamper detection (default: true) */
1415
+ enableHashing?: boolean;
1416
+ /** Hash algorithm (default: 'sha256') */
1417
+ hashAlgorithm?: 'sha256' | 'sha512';
1418
+ /** Enable write-ahead logging for crash recovery (default: true) */
1419
+ enableWAL?: boolean;
1420
+ /** Secret key for HMAC hashing (optional, recommended for production) */
1421
+ secretKey?: string;
1422
+ }
1423
+ /**
1424
+ * Audit log entry with cryptographic hash
1425
+ */
1426
+ interface HashedAuditLogEntry extends AuditLogEntry {
1427
+ /** Cryptographic hash of this entry */
1428
+ hash: string;
1429
+ /** Hash of previous entry for chain verification */
1430
+ previousHash?: string;
1431
+ /** Sequence number in the log chain */
1432
+ sequence: number;
1433
+ }
1434
+ /**
1435
+ * Audit query filter
1436
+ */
1437
+ interface AuditQueryFilter {
1438
+ /** Filter by operation type */
1439
+ operation?: AuditLogEntry['operation'];
1440
+ /** Filter by user */
1441
+ user?: string;
1442
+ /** Filter by session ID */
1443
+ sessionId?: string;
1444
+ /** Filter by date range (start) */
1445
+ startDate?: Date;
1446
+ /** Filter by date range (end) */
1447
+ endDate?: Date;
1448
+ /** Filter by success status */
1449
+ success?: boolean;
1450
+ /** Limit results */
1451
+ limit?: number;
1452
+ /** Offset for pagination */
1453
+ offset?: number;
1454
+ /** Sort order */
1455
+ sort?: 'asc' | 'desc';
1456
+ }
1457
+ /**
1458
+ * Persistent Audit Logger with cryptographic chain verification
1459
+ */
1460
+ declare class PersistentAuditLogger implements IAuditLogger {
1461
+ private adapter;
1462
+ private options;
1463
+ private batchBuffer;
1464
+ private lastHash;
1465
+ private sequence;
1466
+ private cleanupTimer?;
1467
+ private initialized;
1468
+ constructor(options: PersistentAuditLoggerOptions);
1469
+ /**
1470
+ * Initialize the logger (must be called before use)
1471
+ */
1472
+ initialize(): Promise<void>;
1473
+ /**
1474
+ * Log an audit entry
1475
+ */
1476
+ log(entry: Omit<AuditLogEntry, 'id' | 'timestamp'>): void;
1477
+ /**
1478
+ * Get all audit logs
1479
+ */
1480
+ getLogs(): AuditLogEntry[];
1481
+ /**
1482
+ * Query logs with filters (async)
1483
+ */
1484
+ queryLogs(filter?: AuditQueryFilter): Promise<HashedAuditLogEntry[]>;
1485
+ /**
1486
+ * Get logs by operation type
1487
+ */
1488
+ getLogsByOperation(_operation: AuditLogEntry['operation']): AuditLogEntry[];
1489
+ /**
1490
+ * Get logs by date range
1491
+ */
1492
+ getLogsByDateRange(_startDate: Date, _endDate: Date): AuditLogEntry[];
1493
+ /**
1494
+ * Export logs as JSON
1495
+ */
1496
+ exportAsJson(): string;
1497
+ /**
1498
+ * Export logs as JSON (async)
1499
+ */
1500
+ exportAsJsonAsync(filter?: AuditQueryFilter): Promise<string>;
1501
+ /**
1502
+ * Export logs as CSV
1503
+ */
1504
+ exportAsCsv(): string;
1505
+ /**
1506
+ * Export logs as CSV (async)
1507
+ */
1508
+ exportAsCsvAsync(filter?: AuditQueryFilter): Promise<string>;
1509
+ /**
1510
+ * Clear all audit logs (dangerous!)
1511
+ */
1512
+ clear(): void;
1513
+ /**
1514
+ * Delete logs older than specified date
1515
+ */
1516
+ deleteOlderThan(date: Date): Promise<number>;
1517
+ /**
1518
+ * Get audit statistics
1519
+ */
1520
+ getStats(): AuditStats;
1521
+ /**
1522
+ * Get audit statistics (async)
1523
+ */
1524
+ getStatsAsync(filter?: AuditQueryFilter): Promise<AuditStats>;
1525
+ /**
1526
+ * Verify log chain integrity
1527
+ */
1528
+ verifyChainIntegrity(startSequence?: number, endSequence?: number): Promise<{
1529
+ valid: boolean;
1530
+ brokenAt?: number;
1531
+ message: string;
1532
+ }>;
1533
+ /**
1534
+ * Flush batch buffer to database
1535
+ */
1536
+ flushBatch(): Promise<void>;
1537
+ /**
1538
+ * Close the logger and flush any pending logs
1539
+ */
1540
+ close(): Promise<void>;
1541
+ /**
1542
+ * Create hashed entry with chain linking
1543
+ */
1544
+ private createHashedEntry;
1545
+ /**
1546
+ * Calculate cryptographic hash of entry
1547
+ */
1548
+ private calculateHash;
1549
+ /**
1550
+ * Generate unique ID
1551
+ */
1552
+ private generateId;
1553
+ /**
1554
+ * Create database adapter based on backend
1555
+ */
1556
+ private createAdapter;
1557
+ /**
1558
+ * Start automatic cleanup schedule
1559
+ */
1560
+ private startCleanupSchedule;
1561
+ /**
1562
+ * Run cleanup based on retention policy
1563
+ */
1564
+ private runCleanup;
1565
+ }
1566
+ //#endregion
1567
+ //#region src/api/APIServer.d.ts
1568
+ /**
1569
+ * API Server configuration
1570
+ */
1571
+ interface APIServerConfig {
1572
+ /** Server port (default: 3000) */
1573
+ port?: number;
1574
+ /** Server host (default: '0.0.0.0') */
1575
+ host?: string;
1576
+ /** Enable CORS (default: true) */
1577
+ enableCors?: boolean;
1578
+ /** CORS origin (default: '*') */
1579
+ corsOrigin?: string | string[];
1580
+ /** API key for authentication (optional) */
1581
+ apiKey?: string;
1582
+ /** Enable rate limiting (default: true) */
1583
+ enableRateLimit?: boolean;
1584
+ /** Rate limit: requests per minute (default: 60) */
1585
+ rateLimit?: number;
1586
+ /** Request body size limit (default: '10mb') */
1587
+ bodyLimit?: string;
1588
+ /** Enable request logging (default: true) */
1589
+ enableLogging?: boolean;
1590
+ /** Tenant manager (for multi-tenant mode) */
1591
+ tenantManager?: TenantManager;
1592
+ /** Webhook manager */
1593
+ webhookManager?: WebhookManager;
1594
+ /** Persistent audit logger */
1595
+ auditLogger?: PersistentAuditLogger;
1596
+ /** Default OpenRedaction options (for non-tenant mode) */
1597
+ defaultOptions?: OpenRedactionOptions;
1598
+ }
1599
+ /**
1600
+ * API request with authentication
1601
+ */
1602
+ interface APIRequest {
1603
+ /** HTTP method (e.g. GET, POST) */
1604
+ method: string;
1605
+ /** URL pathname (e.g. /api/detect) */
1606
+ pathname: string;
1607
+ /** Request body */
1608
+ body: any;
1609
+ /** Headers */
1610
+ headers: Record<string, string | string[] | undefined>;
1611
+ /** Query parameters */
1612
+ query: Record<string, string | string[] | undefined>;
1613
+ /** Path parameters */
1614
+ params: Record<string, string>;
1615
+ /** Authenticated tenant ID (if multi-tenant) */
1616
+ tenantId?: string;
1617
+ /** Client IP address */
1618
+ ip?: string;
1619
+ }
1620
+ /**
1621
+ * API response
1622
+ */
1623
+ interface APIResponse {
1624
+ /** Status code */
1625
+ status: number;
1626
+ /** Response body */
1627
+ body: any;
1628
+ /** Headers */
1629
+ headers?: Record<string, string>;
1630
+ }
1631
+ /**
1632
+ * REST API Server
1633
+ * Lightweight HTTP server for OpenRedaction with Express-like interface
1634
+ */
1635
+ declare class APIServer {
1636
+ private server?;
1637
+ private config;
1638
+ private detector?;
1639
+ private isRunning;
1640
+ private rateLimitTracking;
1641
+ constructor(config?: APIServerConfig);
1642
+ /**
1643
+ * Start the API server
1644
+ */
1645
+ start(): Promise<void>;
1646
+ /**
1647
+ * Stop the server
1648
+ */
1649
+ stop(): Promise<void>;
1650
+ /**
1651
+ * Handle incoming HTTP requests
1652
+ */
1653
+ private handleRequest;
1654
+ /**
1655
+ * Parse HTTP request
1656
+ */
1657
+ private parseRequest;
1658
+ /**
1659
+ * Parse body size limit (e.g. "10mb", "512kb") to bytes
1660
+ */
1661
+ private parseBodyLimitBytes;
1662
+ /**
1663
+ * Parse request body
1664
+ */
1665
+ private parseBody;
1666
+ /**
1667
+ * Route request to appropriate handler
1668
+ */
1669
+ private routeRequest;
1670
+ /**
1671
+ * Handle POST /api/detect
1672
+ */
1673
+ private handleDetect;
1674
+ /**
1675
+ * Handle POST /api/redact
1676
+ */
1677
+ private handleRedact;
1678
+ /**
1679
+ * Handle POST /api/restore
1680
+ */
1681
+ private handleRestore;
1682
+ /**
1683
+ * Handle GET /api/audit/logs
1684
+ */
1685
+ private handleAuditLogs;
1686
+ /**
1687
+ * Handle GET /api/audit/stats
1688
+ */
1689
+ private handleAuditStats;
1690
+ /**
1691
+ * Handle GET /api/metrics
1692
+ */
1693
+ private handleMetrics;
1694
+ /**
1695
+ * Handle GET /api/patterns
1696
+ */
1697
+ private handleGetPatterns;
1698
+ /**
1699
+ * Handle GET /api/health
1700
+ */
1701
+ private handleHealth;
1702
+ /**
1703
+ * Handle GET /api/docs
1704
+ */
1705
+ private handleDocs;
1706
+ /**
1707
+ * Handle GET /
1708
+ */
1709
+ private handleRoot;
1710
+ /**
1711
+ * Send HTTP response
1712
+ */
1713
+ private sendResponse;
1714
+ /**
1715
+ * Check rate limit
1716
+ */
1717
+ private checkRateLimit;
1718
+ }
1719
+ /**
1720
+ * Create an API server instance
1721
+ */
1722
+ declare function createAPIServer(config?: APIServerConfig): APIServer;
1723
+ //#endregion
1724
+ //#region src/metrics/PrometheusServer.d.ts
1725
+ /**
1726
+ * Prometheus server options
1727
+ */
1728
+ interface PrometheusServerOptions {
1729
+ /** Port to listen on (default: 9090) */
1730
+ port?: number;
1731
+ /** Host to bind to (default: '0.0.0.0') */
1732
+ host?: string;
1733
+ /** Metrics path (default: '/metrics') */
1734
+ metricsPath?: string;
1735
+ /** Metrics prefix (default: 'openredaction') */
1736
+ prefix?: string;
1737
+ /** Health check path (default: '/health') */
1738
+ healthPath?: string;
1739
+ /** Enable CORS (default: false) */
1740
+ enableCors?: boolean;
1741
+ /** Basic auth username (optional) */
1742
+ username?: string;
1743
+ /** Basic auth password (optional) */
1744
+ password?: string;
1745
+ }
1746
+ /**
1747
+ * Prometheus metrics HTTP server
1748
+ * Provides a lightweight HTTP server for exposing metrics to Prometheus
1749
+ */
1750
+ declare class PrometheusServer {
1751
+ private server?;
1752
+ private metricsCollector;
1753
+ private options;
1754
+ private isRunning;
1755
+ private requestCount;
1756
+ private lastScrapeTime?;
1757
+ constructor(metricsCollector: IMetricsCollector, options?: PrometheusServerOptions);
1758
+ /**
1759
+ * Start the Prometheus metrics server
1760
+ */
1761
+ start(): Promise<void>;
1762
+ /**
1763
+ * Stop the server
1764
+ */
1765
+ stop(): Promise<void>;
1766
+ /**
1767
+ * Handle incoming HTTP requests
1768
+ */
1769
+ private handleRequest;
1770
+ /**
1771
+ * Handle /metrics endpoint
1772
+ */
1773
+ private handleMetrics;
1774
+ /**
1775
+ * Handle /health endpoint
1776
+ */
1777
+ private handleHealth;
1778
+ /**
1779
+ * Handle / root endpoint
1780
+ */
1781
+ private handleRoot;
1782
+ /**
1783
+ * Validate basic authentication
1784
+ */
1785
+ private validateAuth;
1786
+ /**
1787
+ * Get server-specific metrics in Prometheus format
1788
+ */
1789
+ private getServerMetrics;
1790
+ /**
1791
+ * Get server statistics
1792
+ */
1793
+ getStats(): {
1794
+ isRunning: boolean;
1795
+ requestCount: number;
1796
+ lastScrapeTime?: Date;
1797
+ uptime: number;
1798
+ host: string;
1799
+ port: number;
1800
+ metricsPath: string;
1801
+ };
1802
+ }
1803
+ /**
1804
+ * Create a Prometheus server instance
1805
+ */
1806
+ declare function createPrometheusServer(metricsCollector: IMetricsCollector, options?: PrometheusServerOptions): PrometheusServer;
1807
+ //#endregion
1808
+ export { type APIRequest, type APIResponse, APIServer, type APIServerConfig, PrometheusServer, type PrometheusServerOptions, createAPIServer, createPrometheusServer };
1809
+ //# sourceMappingURL=server.d.mts.map