/** * JSON Partitioning Module * * This module provides functionality to split JSON objects into two parts: * 1. A "plain" object with sensitive fields replaced by UUID references * 2. An "encrypted map" containing the original sensitive values keyed by UUIDs * * This is useful for scenarios where you want to separate sensitive data from * the main data structure for encryption, storage, or transmission purposes. * * @example * ```typescript * import { partitionJson, mergeJson } from './partition'; * * const originalData = { * name: 'John Doe', * email: 'john@example.com', * password_enc: 'secret123', * profile: { * ssn_enc: '123-45-6789', * public_info: 'This is public' * } * }; * * // Partition based on fields ending with '_enc' * const { plain, encryptedMap } = partitionJson( * originalData, * (path, value) => path.endsWith('_enc') * ); * * console.log(plain); * // { * // name: 'John Doe', * // email: 'john@example.com', * // password_enc: { __enc_ref: 'uuid-1' }, * // profile: { * // ssn_enc: { __enc_ref: 'uuid-2' }, * // public_info: 'This is public' * // } * // } * * console.log(encryptedMap); * // { * // 'uuid-1': 'secret123', * // 'uuid-2': '123-45-6789' * // } * * // Reconstruct the original object * const reconstructed = mergeJson(plain, encryptedMap); * // reconstructed === originalData (deep equality) * ``` */ /** * Result of partitioning a JSON object */ export interface PartitionResult { /** The original object with sensitive fields replaced by UUID references */ plain: any; /** Map of UUIDs to original sensitive values */ encryptedMap: Record; } /** * Predicate function to determine if a field should be encrypted * @param path - The dot-notation path to the current field (e.g., 'user.profile.ssn_enc') * @param value - The value at this path * @returns true if this field should be encrypted, false otherwise */ export type PartitionPredicate = (path: string, value: any) => boolean; /** * Recursively partitions a JSON object into plain and encrypted parts * * @param obj - The object to partition * @param predicate - Function to determine which fields should be encrypted * @returns Object containing the plain data and encrypted map */ export declare function partitionJson(obj: any, predicate: PartitionPredicate): PartitionResult; /** * Merges a plain object with its encrypted map to reconstruct the original object * * @param plain - The plain object containing UUID references * @param encryptedMap - Map of UUIDs to encrypted values * @returns The reconstructed original object * @throws Error if a referenced UUID is not found in the encrypted map */ export declare function mergeJson(plain: any, encryptedMap: Record): any; /** * Utility function to create common partition predicates */ export declare const PartitionPredicates: { /** * Predicate that matches field names ending with a specific suffix * @param suffix - The suffix to match (e.g., '_enc', '_secret') * @returns Predicate function */ fieldEndsWith: (suffix: string) => PartitionPredicate; /** * Predicate that matches field names matching a regex pattern * @param pattern - Regular expression to match field names * @returns Predicate function */ fieldMatches: (pattern: RegExp) => PartitionPredicate; /** * Predicate that matches specific field paths * @param paths - Array of dot-notation paths to encrypt * @returns Predicate function */ specificPaths: (paths: string[]) => PartitionPredicate; /** * Predicate that matches values of specific types containing sensitive patterns * @param patterns - Array of regex patterns to match against string values * @returns Predicate function */ valueMatches: (patterns: RegExp[]) => PartitionPredicate; }; /** * Utility function to validate partition result integrity * @param original - Original object before partitioning * @param result - Result from partitionJson * @returns true if partition is valid and reversible */ export declare function validatePartition(original: any, result: PartitionResult): boolean; /** * Utility function to get statistics about a partition operation * @param result - Result from partitionJson * @returns Statistics about the partition */ export declare function getPartitionStats(result: PartitionResult): { totalEncryptedFields: number; encryptedFieldSizes: Record; totalEncryptedBytes: number; }; //# sourceMappingURL=partition.d.ts.map