1
0

DataStoreSerializer.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * @module lib/DataStoreSerializer
  3. * 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)
  4. */
  5. import { computeHash } from "./crypto.js";
  6. import { getUnsafeWindow } from "./dom.js";
  7. import { ChecksumMismatchError } from "./errors.js";
  8. import type { DataStore } from "./DataStore.js";
  9. export type DataStoreSerializerOptions = {
  10. /** Whether to add a checksum to the exported data */
  11. addChecksum?: boolean;
  12. /** 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) */
  13. ensureIntegrity?: boolean;
  14. };
  15. /** Serialized data of a DataStore instance */
  16. export type SerializedDataStore = {
  17. /** The ID of the DataStore instance */
  18. id: string;
  19. /** The serialized data */
  20. data: string;
  21. /** The format version of the data */
  22. formatVersion: number;
  23. /** Whether the data is encoded */
  24. encoded: boolean;
  25. /** The checksum of the data - key is not present when `addChecksum` is `false` */
  26. checksum?: string;
  27. };
  28. /** Result of {@linkcode DataStoreSerializer.loadStoresData()} */
  29. export type LoadStoresDataResult = {
  30. /** The ID of the DataStore instance */
  31. id: string;
  32. /** The in-memory data object */
  33. data: object;
  34. }
  35. /**
  36. * Allows for easy serialization and deserialization of multiple DataStore instances.
  37. *
  38. * 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.
  39. * Remember that you can call `super.methodName()` in the subclass to access the original method.
  40. *
  41. * - ⚠️ Needs to run in a secure context (HTTPS) due to the use of the SubtleCrypto API if checksumming is enabled.
  42. */
  43. export class DataStoreSerializer {
  44. protected stores: DataStore[];
  45. protected options: Required<DataStoreSerializerOptions>;
  46. constructor(stores: DataStore[], options: DataStoreSerializerOptions = {}) {
  47. if(!getUnsafeWindow().crypto || !getUnsafeWindow().crypto.subtle)
  48. throw new Error("DataStoreSerializer has to run in a secure context (HTTPS)!");
  49. this.stores = stores;
  50. this.options = {
  51. addChecksum: true,
  52. ensureIntegrity: true,
  53. ...options,
  54. };
  55. }
  56. /** Calculates the checksum of a string */
  57. protected async calcChecksum(input: string): Promise<string> {
  58. return computeHash(input, "SHA-256");
  59. }
  60. /**
  61. * Serializes only a subset of the data stores into a string.
  62. * @param stores An array of store IDs or functions that take a store ID and return a boolean
  63. * @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method
  64. * @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects
  65. */
  66. public async serializePartial(stores: string[] | ((id: string) => boolean), useEncoding?: boolean, stringified?: true): Promise<string>;
  67. /**
  68. * Serializes only a subset of the data stores into a string.
  69. * @param stores An array of store IDs or functions that take a store ID and return a boolean
  70. * @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method
  71. * @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects
  72. */
  73. public async serializePartial(stores: string[] | ((id: string) => boolean), useEncoding?: boolean, stringified?: false): Promise<SerializedDataStore[]>;
  74. /**
  75. * Serializes only a subset of the data stores into a string.
  76. * @param stores An array of store IDs or functions that take a store ID and return a boolean
  77. * @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method
  78. * @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects
  79. */
  80. public async serializePartial(stores: string[] | ((id: string) => boolean), useEncoding?: boolean, stringified?: boolean): Promise<string | SerializedDataStore[]>;
  81. /**
  82. * Serializes only a subset of the data stores into a string.
  83. * @param stores An array of store IDs or functions that take a store ID and return a boolean
  84. * @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method
  85. * @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects
  86. */
  87. public async serializePartial(stores: string[] | ((id: string) => boolean), useEncoding = true, stringified = true): Promise<string | SerializedDataStore[]> {
  88. const serData: SerializedDataStore[] = [];
  89. for(const storeInst of this.stores.filter(s => typeof stores === "function" ? stores(s.id) : stores.includes(s.id))) {
  90. const data = useEncoding && storeInst.encodingEnabled()
  91. ? await storeInst.encodeData(JSON.stringify(storeInst.getData()))
  92. : JSON.stringify(storeInst.getData());
  93. serData.push({
  94. id: storeInst.id,
  95. data,
  96. formatVersion: storeInst.formatVersion,
  97. encoded: useEncoding && storeInst.encodingEnabled(),
  98. checksum: this.options.addChecksum
  99. ? await this.calcChecksum(data)
  100. : undefined,
  101. });
  102. }
  103. return stringified ? JSON.stringify(serData) : serData;
  104. }
  105. /**
  106. * Serializes the data stores into a string.
  107. * @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method
  108. * @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects
  109. */
  110. public async serialize(useEncoding?: boolean, stringified?: true): Promise<string>;
  111. /**
  112. * Serializes the data stores into a string.
  113. * @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method
  114. * @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects
  115. */
  116. public async serialize(useEncoding?: boolean, stringified?: false): Promise<SerializedDataStore[]>;
  117. /**
  118. * Serializes the data stores into a string.
  119. * @param useEncoding Whether to encode the data using each DataStore's `encodeData()` method
  120. * @param stringified Whether to return the result as a string or as an array of `SerializedDataStore` objects
  121. */
  122. public async serialize(useEncoding = true, stringified = true): Promise<string | SerializedDataStore[]> {
  123. return this.serializePartial(this.stores.map(s => s.id), useEncoding, stringified);
  124. }
  125. /**
  126. * Deserializes the data exported via {@linkcode serialize()} and imports only a subset into the DataStore instances.
  127. * Also triggers the migration process if the data format has changed.
  128. */
  129. public async deserializePartial(stores: string[] | ((id: string) => boolean), data: string | SerializedDataStore[]): Promise<void> {
  130. const deserStores: SerializedDataStore[] = typeof data === "string" ? JSON.parse(data) : data;
  131. if(!Array.isArray(deserStores) || !deserStores.every(DataStoreSerializer.isSerializedDataStore))
  132. throw new TypeError("Invalid serialized data format! Expected an array of SerializedDataStore objects.");
  133. for(const storeData of deserStores.filter(s => typeof stores === "function" ? stores(s.id) : stores.includes(s.id))) {
  134. const storeInst = this.stores.find(s => s.id === storeData.id);
  135. if(!storeInst)
  136. throw new Error(`DataStore instance with ID "${storeData.id}" not found! Make sure to provide it in the DataStoreSerializer constructor.`);
  137. if(this.options.ensureIntegrity && typeof storeData.checksum === "string") {
  138. const checksum = await this.calcChecksum(storeData.data);
  139. if(checksum !== storeData.checksum)
  140. throw new ChecksumMismatchError(`Checksum mismatch for DataStore with ID "${storeData.id}"!\nExpected: ${storeData.checksum}\nHas: ${checksum}`);
  141. }
  142. const decodedData = storeData.encoded && storeInst.encodingEnabled()
  143. ? await storeInst.decodeData(storeData.data)
  144. : storeData.data;
  145. if(storeData.formatVersion && !isNaN(Number(storeData.formatVersion)) && Number(storeData.formatVersion) < storeInst.formatVersion)
  146. await storeInst.runMigrations(JSON.parse(decodedData), Number(storeData.formatVersion), false);
  147. else
  148. await storeInst.setData(JSON.parse(decodedData));
  149. }
  150. }
  151. /**
  152. * Deserializes the data exported via {@linkcode serialize()} and imports the data into all matching DataStore instances.
  153. * Also triggers the migration process if the data format has changed.
  154. */
  155. public async deserialize(data: string | SerializedDataStore[]): Promise<void> {
  156. return this.deserializePartial(this.stores.map(s => s.id), data);
  157. }
  158. /**
  159. * Loads the persistent data of the DataStore instances into the in-memory cache.
  160. * Also triggers the migration process if the data format has changed.
  161. * @returns Returns a PromiseSettledResult array with the results of each DataStore instance in the format `{ id: string, data: object }`
  162. */
  163. public async loadStoresData(): Promise<PromiseSettledResult<LoadStoresDataResult>[]> {
  164. return Promise.allSettled(this.stores.map(
  165. async store => ({
  166. id: store.id,
  167. data: await store.loadData(),
  168. })
  169. ));
  170. }
  171. /** Resets the persistent data of the DataStore instances to their default values. */
  172. public async resetStoresData(): Promise<PromiseSettledResult<void>[]> {
  173. return Promise.allSettled(this.stores.map(store => store.saveDefaultData()));
  174. }
  175. /**
  176. * Deletes the persistent data of the DataStore instances.
  177. * Leaves the in-memory data untouched.
  178. */
  179. public async deleteStoresData(): Promise<PromiseSettledResult<void>[]> {
  180. return Promise.allSettled(this.stores.map(store => store.deleteData()));
  181. }
  182. /** Checks if a given value is a SerializedDataStore object */
  183. public static isSerializedDataStore(obj: unknown): obj is SerializedDataStore {
  184. return typeof obj === "object" && obj !== null && "id" in obj && "data" in obj && "formatVersion" in obj && "encoded" in obj;
  185. }
  186. }