syntropylog
Version:
An instance manager with observability for Node.js applications
177 lines (176 loc) • 5.06 kB
TypeScript
/**
* FILE: src/masking/MaskingEngine.ts
* DESCRIPTION: Ultra-fast data masking engine using JSON flattening strategy.
*
* This engine flattens complex nested objects into linear key-value pairs,
* applies masking rules, and then reconstructs the original structure.
* This approach provides extreme processing speed for any object depth.
*/
/**
* @enum MaskingStrategy
* @description Different masking strategies for various data types.
*/
export declare enum MaskingStrategy {
CREDIT_CARD = "credit_card",
SSN = "ssn",
EMAIL = "email",
PHONE = "phone",
PASSWORD = "password",
TOKEN = "token",
CUSTOM = "custom"
}
/**
* @interface MaskingRule
* @description Configuration for a masking rule.
*/
export interface MaskingRule {
/** Regex pattern to match field names */
pattern: string | RegExp;
/** Masking strategy to apply */
strategy: MaskingStrategy;
/** Custom masking function (for CUSTOM strategy) */
customMask?: (value: string) => string;
/** Whether to preserve original length */
preserveLength?: boolean;
/** Character to use for masking */
maskChar?: string;
/** Compiled regex pattern for performance */
_compiledPattern?: RegExp;
}
/**
* @interface MaskingEngineOptions
* @description Options for configuring the MaskingEngine.
*/
export interface MaskingEngineOptions {
/** Array of masking rules */
rules?: MaskingRule[];
/** Default mask character */
maskChar?: string;
/** Whether to preserve original length by default */
preserveLength?: boolean;
/** Enable default rules for common data types */
enableDefaultRules?: boolean;
}
/**
* @class MaskingEngine
* Ultra-fast data masking engine using JSON flattening strategy.
*
* Instead of processing nested objects recursively, we flatten them to a linear
* structure for extreme processing speed. This approach provides O(n) performance
* regardless of object depth or complexity.
*/
export declare class MaskingEngine {
/** @private Array of masking rules */
private rules;
/** @private Default mask character */
private readonly maskChar;
/** @private Whether to preserve original length by default */
private readonly preserveLength;
/** @private Whether the engine is initialized */
private initialized;
/** @private Secure regex tester with timeout */
private readonly regexTest;
constructor(options?: MaskingEngineOptions);
/**
* Adds default masking rules for common data types.
* @private
*/
private addDefaultRules;
/**
* Adds a custom masking rule.
* @param rule - The masking rule to add
*/
addRule(rule: MaskingRule): void;
/**
* Processes a metadata object and applies the configured masking rules.
* Uses JSON flattening strategy for extreme performance.
* @param meta - The metadata object to process
* @returns A new object with the masked data
*/
process(meta: Record<string, unknown>): Record<string, unknown>;
/**
* Applies masking rules to data recursively.
* @param data - Data to mask
* @returns Masked data
* @private
*/
private applyMaskingRules;
/**
* Applies specific masking strategy to a value.
* @param value - Value to mask
* @param rule - Masking rule to apply
* @returns Masked value
* @private
*/
private applyStrategy;
/**
* Masks credit card number.
* @param value - Credit card number
* @param rule - Masking rule
* @returns Masked credit card
* @private
*/
private maskCreditCard;
/**
* Masks SSN.
* @param value - SSN
* @param rule - Masking rule
* @returns Masked SSN
* @private
*/
private maskSSN;
/**
* Masks email address.
* @param value - Email address
* @param rule - Masking rule
* @returns Masked email
* @private
*/
private maskEmail;
/**
* Masks phone number.
* @param value - Phone number
* @param rule - Masking rule
* @returns Masked phone number
* @private
*/
private maskPhone;
/**
* Masks password.
* @param value - Password
* @param rule - Masking rule
* @returns Masked password
* @private
*/
private maskPassword;
/**
* Masks token.
* @param value - Token
* @param rule - Masking rule
* @returns Masked token
* @private
*/
private maskToken;
/**
* Default masking strategy.
* @param value - Value to mask
* @param rule - Masking rule
* @returns Masked value
* @private
*/
private maskDefault;
/**
* Gets masking engine statistics.
* @returns Dictionary with masking statistics
*/
getStats(): Record<string, any>;
/**
* Checks if the masking engine is initialized.
* @returns True if initialized
*/
isInitialized(): boolean;
/**
* Shutdown the masking engine.
*/
shutdown(): void;
}