|
| 1 | +import { storageSettings } from "../index.js"; |
| 2 | +import { SessionBase, StorageKeys, type SessionManager } from "../types.js"; |
| 3 | +import { splitString } from "../utils.js"; |
| 4 | + |
| 5 | +/** |
| 6 | + * Provides a memory based session manager implementation for the browser. |
| 7 | + * @class MemoryStorage |
| 8 | + */ |
| 9 | +export class MemoryStorage<V extends string = StorageKeys> |
| 10 | + extends SessionBase<V> |
| 11 | + implements SessionManager<V> |
| 12 | +{ |
| 13 | + private memCache: Record<string, unknown> = {}; |
| 14 | + |
| 15 | + /** |
| 16 | + * Clears all items from session store. |
| 17 | + * @returns {void} |
| 18 | +.04 |
| 19 | + */ |
| 20 | + async destroySession(): Promise<void> { |
| 21 | + this.memCache = {}; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Sets the provided key-value store to the memory cache. |
| 26 | + * @param {string} itemKey |
| 27 | + * @param {unknown} itemValue |
| 28 | + * @returns {void} |
| 29 | + */ |
| 30 | + async setSessionItem( |
| 31 | + itemKey: V | StorageKeys, |
| 32 | + itemValue: unknown, |
| 33 | + ): Promise<void> { |
| 34 | + // clear items first |
| 35 | + await this.removeSessionItem(itemKey); |
| 36 | + |
| 37 | + if (typeof itemValue === "string") { |
| 38 | + splitString(itemValue, storageSettings.maxLength).forEach( |
| 39 | + (splitValue, index) => { |
| 40 | + this.memCache[`${storageSettings.keyPrefix}${itemKey}${index}`] = |
| 41 | + splitValue; |
| 42 | + }, |
| 43 | + ); |
| 44 | + return; |
| 45 | + } |
| 46 | + this.memCache[`${storageSettings.keyPrefix}${String(itemKey)}0`] = |
| 47 | + itemValue; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Gets the item for the provided key from the memory cache. |
| 52 | + * @param {string} itemKey |
| 53 | + * @returns {unknown | null} |
| 54 | + */ |
| 55 | + async getSessionItem(itemKey: V | StorageKeys): Promise<unknown | null> { |
| 56 | + if ( |
| 57 | + this.memCache[`${storageSettings.keyPrefix}${String(itemKey)}0`] === |
| 58 | + undefined |
| 59 | + ) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + let itemValue = ""; |
| 64 | + let index = 0; |
| 65 | + let key = `${storageSettings.keyPrefix}${String(itemKey)}${index}`; |
| 66 | + while (this.memCache[key] !== undefined) { |
| 67 | + itemValue += this.memCache[key]; |
| 68 | + index++; |
| 69 | + key = `${storageSettings.keyPrefix}${String(itemKey)}${index}`; |
| 70 | + } |
| 71 | + |
| 72 | + return itemValue; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Removes the item for the provided key from the memory cache. |
| 77 | + * @param {string} itemKey |
| 78 | + * @returns {void} |
| 79 | + */ |
| 80 | + async removeSessionItem(itemKey: V | StorageKeys): Promise<void> { |
| 81 | + // Remove all items with the key prefix |
| 82 | + for (const key in this.memCache) { |
| 83 | + if (key.startsWith(`${storageSettings.keyPrefix}${String(itemKey)}`)) { |
| 84 | + delete this.memCache[key]; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments