tiny-crypto-suite
Version:
Tiny tools, big crypto — seamless encryption and certificate handling for modern web and Node apps.
89 lines • 4.46 kB
text/typescript
export default TinyCryptoParser;
/**
* TinyCryptoParser provides serialization and deserialization utilities for complex JavaScript data types.
*
* This class enables conversion of various JavaScript values (such as `Map`, `Set`, `Buffer`, `Date`, `BigInt`, `Symbol`, `HTMLElement`, and others)
* into JSON-compatible formats, allowing safe encryption, transmission, or storage.
* It also supports deserialization, restoring the original JavaScript objects from the serialized data.
*
* Unsupported types like `WeakMap`, `WeakSet`, `Promise`, and `Function` will throw explicit errors when an attempt is made to serialize them.
*
* It includes built-in validation to ensure the integrity of types during deserialization, providing stronger consistency guarantees
* when handling critical data transformations.
*
* @class
*/
declare class TinyCryptoParser {
/**
* Add a new value type and its converter function.
* @param {string} typeName
* @param {(data: any) => any} getFunction
* @param {(data: any) => SerializedData} convertFunction
* @param {(data: any) => any} [serializeDeep]
* @param {(data: any) => any} [deserializeDeep]
*/
addValueType(typeName: string, getFunction: (data: any) => any, convertFunction: (data: any) => {
__type: string;
value?: any;
}, serializeDeep?: (data: any) => any, deserializeDeep?: (data: any) => any): void;
/**
* Serializes a given data value into a JSON-compatible format based on its type.
* This method converts various JavaScript data types into their serialized representation
* that can be encrypted or stored. If the data type is unsupported, an error is thrown.
*
* @param {any} data - The data to be serialized.
* @returns {string} The serialized data in JSON format.
* @throws {Error} If the data type is unsupported for serialization.
*/
serialize(data: any): string;
/**
* Deserializes a string back into its original JavaScript object based on the serialized type information.
* This method checks the serialized type and converts the string back to its original JavaScript object
* (such as a `Date`, `Buffer`, `RegExp`, etc.). If the type is unknown or unsupported, it returns the raw value.
*
* @param {string} text - The serialized data to be deserialized.
* @param {string|null} [expectedType=null] - Optionally specify the expected type of the decrypted data. If provided, the method will validate the type of the deserialized value.
* @returns {DeserializedData} An object containing the deserialized value and its type.
* @throws {Error} If deserialization fails due to an invalid or unknown type.
*/
deserialize(text: string, expectedType?: string | null): {
/**
* - The deserialized value, which can be any JavaScript type.
*/
value: any;
/**
* - The type of the deserialized data (e.g., 'object', 'array', 'string', etc.).
*/
type: string;
};
/**
* Recursively serializes a given data value into a JSON-compatible format.
* If the data is an object or array, it will traverse each entry and serialize them individually.
*
* @param {any} data - The data to be deeply serialized.
* @returns {string} The deeply serialized data in JSON format.
* @throws {Error} If the data type is unsupported for serialization.
*/
serializeDeep(data: any): string;
/**
* Recursively deserializes a string back into its original value format.
* If the data is an object or array, it will traverse each entry and deserialize them individually.
*
* @param {string} text - The serialized data to be deeply deserialized.
* @param {string|null} [expectedType=null] - Optionally specify the expected type of the decrypted data.
* @returns {DeserializedData} An object containing the deserialized value and its type.
* @throws {Error} If deserialization fails due to an invalid or unknown type.
*/
deserializeDeep(text: string, expectedType?: string | null): {
/**
* - The deserialized value, which can be any JavaScript type.
*/
value: any;
/**
* - The type of the deserialized data (e.g., 'object', 'array', 'string', etc.).
*/
type: string;
};
#private;
}
//# sourceMappingURL=TinyCryptoParser.d.mts.map