From 7fd079a54777d70fac500bdeb5dd988faa7ad15e Mon Sep 17 00:00:00 2001 From: Aaron Shafovaloff Date: Sun, 24 Nov 2024 19:42:38 -0700 Subject: [PATCH] refactor: IDBCacheInterface --- packages/idb-cache/src/index.ts | 46 +++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/packages/idb-cache/src/index.ts b/packages/idb-cache/src/index.ts index 4bd0aaa..5a4603e 100644 --- a/packages/idb-cache/src/index.ts +++ b/packages/idb-cache/src/index.ts @@ -28,16 +28,20 @@ import { IDBCacheError, } from "./errors"; -const DB_VERSION = 1; -const DEFAULT_CHUNK_SIZE = 25000; // recommendation: keep under 100KiB (cf. https://surma.dev/things/is-postmessage-slow/) -const DEFAULT_GC_TIME = 7 * 24 * 60 * 60 * 1000; -const DEFAULT_PBKDF2_ITERATIONS = 100000; -const CLEANUP_INTERVAL = 60 * 1000; -const DURATION_THRESHOLD = 200; +export interface AsyncStorage { + getItem: (key: string) => Promise; + setItem: (key: string, value: string) => Promise; + removeItem: (key: string) => Promise; + clear: () => Promise; +} -const isSubtleCryptoSupported = crypto?.subtle; +export interface IDBCacheInterface extends AsyncStorage { + destroy: (options?: { clearData?: boolean }) => Promise; + cleanup: () => Promise; + count: () => Promise; +} -interface IDBCacheConfig { +export interface IDBCacheConfig { /** * Sensitive identifier used for securely encrypting data. */ @@ -88,14 +92,16 @@ interface IDBCacheConfig { priority?: "normal" | "low"; } -export interface AsyncStorage { - getItem: (key: string) => Promise; - setItem: (key: string, value: string) => Promise; - removeItem: (key: string) => Promise; - clear: () => Promise; -} +const DB_VERSION = 1; +const DEFAULT_CHUNK_SIZE = 25000; // recommendation: keep under 100KiB (cf. https://surma.dev/things/is-postmessage-slow/) +const DEFAULT_GC_TIME = 7 * 24 * 60 * 60 * 1000; +const DEFAULT_PBKDF2_ITERATIONS = 100000; +const CLEANUP_INTERVAL = 60 * 1000; +const DURATION_THRESHOLD = 200; + +const isSubtleCryptoSupported = crypto?.subtle; -export class IDBCache implements AsyncStorage { +export class IDBCache implements IDBCacheInterface { dbReadyPromise: Promise>; private storeName: STORE; private worker: Worker | null = null; @@ -406,7 +412,7 @@ export class IDBCache implements AsyncStorage { * @throws {DatabaseError} If there is an issue accessing the database. * @throws {WorkerInitializationError} If the worker is not initialized properly. */ - async getItem(itemKey: string): Promise { + public async getItem(itemKey: string): Promise { try { const startTime = Date.now(); @@ -545,7 +551,7 @@ export class IDBCache implements AsyncStorage { * @throws {DatabaseError} If there is an issue accessing the database. * @throws {EncryptionError} If encryption fails. */ - async setItem(itemKey: string, value: string): Promise { + public async setItem(itemKey: string, value: string): Promise { try { const startTime = Date.now(); @@ -702,7 +708,7 @@ export class IDBCache implements AsyncStorage { * @param key - The key associated with the item to remove. * @throws {DatabaseError} If there is an issue accessing the database. */ - async removeItem(itemKey: string): Promise { + public async removeItem(itemKey: string): Promise { try { const db = await this.dbReadyPromise; const baseKey = await deterministicUUID(`${this.cacheKey}:${itemKey}`); @@ -740,7 +746,7 @@ export class IDBCache implements AsyncStorage { * @returns The total number of entries (chunks) in the cache. * @throws {DatabaseError} If there is an issue accessing the database. */ - async count(): Promise { + public async count(): Promise { try { const db = await this.dbReadyPromise; const transaction = db.transaction(this.storeName, "readonly"); @@ -768,7 +774,7 @@ export class IDBCache implements AsyncStorage { * Clears all items from the cache without affecting the worker or pending requests. * @throws {DatabaseError} If there is an issue accessing the database. */ - async clear(): Promise { + public async clear(): Promise { try { const db = await this.dbReadyPromise; const transaction = db.transaction(this.storeName, "readwrite");