{"version":3,"file":"AsyncMemoryStorage.js","sources":["../../src/cache/AsyncMemoryStorage.ts"],"sourcesContent":["/*\n * Copyright (c) Microsoft Corporation. All rights reserved.\n * Licensed under the MIT License.\n */\n\nimport { Logger } from \"@azure/msal-common\";\nimport { BrowserAuthError, BrowserAuthErrorMessage } from \"../error/BrowserAuthError\";\nimport { DatabaseStorage } from \"./DatabaseStorage\";\nimport { IAsyncStorage } from \"./IAsyncMemoryStorage\";\nimport { MemoryStorage } from \"./MemoryStorage\";\n\n/**\n * This class allows MSAL to store artifacts asynchronously using the DatabaseStorage IndexedDB wrapper,\n * backed up with the more volatile MemoryStorage object for cases in which IndexedDB may be unavailable.\n */\nexport class AsyncMemoryStorage implements IAsyncStorage {\n private inMemoryCache: MemoryStorage;\n private indexedDBCache: DatabaseStorage;\n private logger: Logger;\n private storeName: string;\n\n constructor(logger: Logger, storeName: string) {\n this.inMemoryCache = new MemoryStorage();\n this.indexedDBCache = new DatabaseStorage();\n this.logger = logger;\n this.storeName = storeName;\n }\n\n private handleDatabaseAccessError(error: unknown): void {\n if (error instanceof BrowserAuthError && error.errorCode === BrowserAuthErrorMessage.databaseUnavailable.code) {\n this.logger.error(\"Could not access persistent storage. This may be caused by browser privacy features which block persistent storage in third-party contexts.\");\n } else {\n throw error;\n }\n }\n /**\n * Get the item matching the given key. Tries in-memory cache first, then in the asynchronous\n * storage object if item isn't found in-memory.\n * @param key \n */\n async getItem(key: string): Promise {\n const item = this.inMemoryCache.getItem(key);\n if(!item) {\n try {\n this.logger.verbose(\"Queried item not found in in-memory cache, now querying persistent storage.\");\n return await this.indexedDBCache.getItem(key);\n } catch (e) {\n this.handleDatabaseAccessError(e);\n }\n }\n return item;\n }\n\n /**\n * Sets the item in the in-memory cache and then tries to set it in the asynchronous\n * storage object with the given key.\n * @param key \n * @param value \n */\n async setItem(key: string, value: T): Promise {\n this.inMemoryCache.setItem(key, value);\n try {\n await this.indexedDBCache.setItem(key, value);\n } catch (e) {\n this.handleDatabaseAccessError(e);\n }\n }\n\n /**\n * Removes the item matching the key from the in-memory cache, then tries to remove it from the asynchronous storage object.\n * @param key \n */\n async removeItem(key: string): Promise {\n this.inMemoryCache.removeItem(key);\n try {\n await this.indexedDBCache.removeItem(key);\n } catch (e) {\n this.handleDatabaseAccessError(e);\n }\n }\n\n /**\n * Get all the keys from the in-memory cache as an iterable array of strings. If no keys are found, query the keys in the \n * asynchronous storage object.\n */\n async getKeys(): Promise {\n const cacheKeys = this.inMemoryCache.getKeys();\n if (cacheKeys.length === 0) {\n try {\n this.logger.verbose(\"In-memory cache is empty, now querying persistent storage.\");\n return await this.indexedDBCache.getKeys();\n } catch (e) {\n this.handleDatabaseAccessError(e);\n }\n }\n return cacheKeys;\n }\n\n /**\n * Returns true or false if the given key is present in the cache.\n * @param key \n */\n async containsKey(key: string): Promise {\n const containsKey = this.inMemoryCache.containsKey(key);\n if(!containsKey) {\n try {\n this.logger.verbose(\"Key not found in in-memory cache, now querying persistent storage.\");\n return await this.indexedDBCache.containsKey(key);\n } catch (e) {\n this.handleDatabaseAccessError(e);\n }\n }\n return containsKey;\n }\n\n /**\n * Clears in-memory Map\n */\n clearInMemory(): void {\n // InMemory cache is a Map instance, clear is straightforward\n this.logger.verbose(`Deleting in-memory keystore ${this.storeName}`);\n this.inMemoryCache.clear();\n this.logger.verbose(`In-memory keystore ${this.storeName} deleted`);\n }\n\n /**\n * Tries to delete the IndexedDB database\n * @returns\n */\n async clearPersistent(): Promise {\n try {\n this.logger.verbose(\"Deleting persistent keystore\");\n const dbDeleted = await this.indexedDBCache.deleteDatabase();\n if (dbDeleted) {\n this.logger.verbose(\"Persistent keystore deleted\");\n }\n \n return dbDeleted;\n } catch (e) {\n this.handleDatabaseAccessError(e);\n return false;\n }\n }\n}\n"],"names":[],"mappings":";;;;;;;AAAA;;;;AAWA;;;;;IAUI,4BAAY,MAAc,EAAE,SAAiB;QACzC,IAAI,CAAC,aAAa,GAAG,IAAI,aAAa,EAAK,CAAC;QAC5C,IA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