/** * @module DataStoreSerializer * This module contains the DataStoreSerializer class, which allows you to import and export serialized DataStore data - [see the documentation for more info](https://github.com/Sv443-Network/UserUtils/blob/main/docs.md#datastoreserializer) */ import type { DataStore, DataStoreData } from "./DataStore.ts"; /** Options for the DataStoreSerializer class */ export type DataStoreSerializerOptions = { /** Whether to add a checksum to the exported data. Defaults to `true` */ addChecksum?: boolean; /** Whether to ensure the integrity of the data when importing it by throwing an error (doesn't throw when the checksum property doesn't exist). Defaults to `true` */ ensureIntegrity?: boolean; /** If provided, all stores with an ID in the value's array will be remapped to the key's ID when deserialization is called. If they don't match a DataStore instance's ID, nothing will happen. */ remapIds?: Record; /** * Controls the type of the `data` property on {@linkcode SerializedDataStore} objects. * When this is set to `false`, `data` will be the raw data object instead of a string, regardless of whether {@linkcode DataStoreSerializer.serialize()} or {@linkcode DataStoreSerializer.serializePartial()} are called with their `stringify` / `stringified` parameter set to `true` or `false`. */ stringifyData?: boolean; }; /** Meta object and serialized data of a DataStore instance */ export type SerializedDataStore = { /** The ID of the DataStore instance */ id: string; /** The serialized data */ data: TData; /** The format version of the data */ formatVersion: number; /** Whether the data is encoded */ encoded: boolean; /** The checksum of the data - key is not present when `addChecksum` is `false` */ checksum?: string; }; /** Result of {@linkcode DataStoreSerializer.loadStoresData()} */ export type LoadStoresDataResult = { /** The ID of the DataStore instance */ id: string; /** The in-memory data object */ data: object; }; /** Filter for selecting data stores */ export type StoreFilter = string[] | ((id: string) => boolean); /** * Allows for easy serialization and deserialization of multiple {@linkcode DataStore} instances. * Offers methods to only serialize or deserialize a subset of the stores, and to ensure the integrity of the data by adding checksums. * * All methods are at least `protected`, so you can easily extend this class and overwrite them to use a different storage method or to add additional functionality. * Remember that you can call `super.methodName()` in the subclass to access the original method. * * - ⚠️ Needs to run in a secure context (HTTPS) due to the use of the SubtleCrypto API if checksumming is enabled. */ export declare class DataStoreSerializer { protected stores: DataStore[]; protected options: Required; constructor(stores: DataStore[], options?: DataStoreSerializerOptions); /** * Calculates the checksum of a string or {@linkcode DataStoreData} object. Uses {@linkcode computeHash()} with SHA-256 and digests as a hex string by default. * Override this in a subclass if a custom checksum method is needed. */ protected calcChecksum(input: string | DataStoreData): Promise; /** * Serializes only a subset of the data stores into a string. * @param stores An array of store IDs or functions that take a store ID and return a boolean * @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method * @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects */ serializePartial(stores: StoreFilter, useEncoding?: boolean, stringified?: true): Promise; /** * Serializes only a subset of the data stores into a string. * @param stores An array of store IDs or functions that take a store ID and return a boolean * @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method * @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects */ serializePartial(stores: StoreFilter, useEncoding?: boolean, stringified?: false): Promise[]>; /** * Serializes only a subset of the data stores into a string. * @param stores An array of store IDs or functions that take a store ID and return a boolean * @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method * @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects */ serializePartial(stores: StoreFilter, useEncoding?: boolean, stringified?: boolean): Promise[]>; /** * Serializes the data stores into a string. * @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method * @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects */ serialize(useEncoding?: boolean, stringified?: true): Promise; /** * Serializes the data stores into a string. * @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method * @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects */ serialize(useEncoding?: boolean, stringified?: false): Promise[]>; /** * Deserializes the data exported via {@linkcode serialize()} and imports only a subset into the DataStore instances. * Also triggers the migration process if the data format has changed. */ deserializePartial(stores: StoreFilter, data: string | SerializedDataStore[]): Promise; /** * Deserializes the data exported via {@linkcode serialize()} and imports the data into all matching DataStore instances. * Also triggers the migration process if the data format has changed. */ deserialize(data: string | SerializedDataStore[]): Promise; /** * Loads the persistent data of the DataStore instances into the in-memory cache. * Also triggers the migration process if the data format has changed. * @param stores An array of store IDs or a function that takes the store IDs and returns a boolean - if omitted, all stores will be loaded * @returns Returns a PromiseSettledResult array with the results of each DataStore instance in the format `{ id: string, data: object }` */ loadStoresData(stores?: StoreFilter): Promise[]>; /** * Resets the persistent and in-memory data of the DataStore instances to their default values. * @param stores An array of store IDs or a function that takes the store IDs and returns a boolean - if omitted, all stores will be affected */ resetStoresData(stores?: StoreFilter): Promise[]>; /** * Deletes the persistent data of the DataStore instances. * Leaves the in-memory data untouched. * @param stores An array of store IDs or a function that takes the store IDs and returns a boolean - if omitted, all stores will be affected */ deleteStoresData(stores?: StoreFilter): Promise[]>; /** Checks if a given value is an array of SerializedDataStore objects */ static isSerializedDataStoreObjArray(obj: unknown): obj is SerializedDataStore[]; /** Checks if a given value is a SerializedDataStore object */ static isSerializedDataStoreObj(obj: unknown): obj is SerializedDataStore; /** Returns the DataStore instances whose IDs match the provided array or function */ protected getStoresFiltered(stores?: StoreFilter): DataStore[]; }