{"id":30060948,"url":"https://github.com/instructure/idb-cache","last_synced_at":"2025-08-08T01:51:52.145Z","repository":{"id":262678218,"uuid":"885038258","full_name":"instructure/idb-cache","owner":"instructure","description":"IndexedDB-based caching library with encryption and chunked storage, designed for performance and security. Implements AsyncStorage interface.","archived":false,"fork":false,"pushed_at":"2025-05-06T12:35:05.000Z","size":781,"stargazers_count":114,"open_issues_count":7,"forks_count":3,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-08-01T10:06:12.096Z","etag":null,"topics":["indexeddb","query","tanstack"],"latest_commit_sha":null,"homepage":"https://instructure.github.io/idb-cache/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/instructure.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-11-07T20:58:49.000Z","updated_at":"2025-07-18T23:20:24.000Z","dependencies_parsed_at":null,"dependency_job_id":"f684b3b9-f046-4955-97c4-9de466f385a3","html_url":"https://github.com/instructure/idb-cache","commit_stats":null,"previous_names":["instructure/idb-cache"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/instructure/idb-cache","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/instructure%2Fidb-cache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/instructure%2Fidb-cache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/instructure%2Fidb-cache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/instructure%2Fidb-cache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/instructure","download_url":"https://codeload.github.com/instructure/idb-cache/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/instructure%2Fidb-cache/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269351896,"owners_count":24402677,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-07T02:00:09.698Z","response_time":73,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["indexeddb","query","tanstack"],"created_at":"2025-08-08T01:51:49.218Z","updated_at":"2025-08-08T01:51:52.135Z","avatar_url":"https://github.com/instructure.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# idb-cache\n\nIndexedDB-based caching library with encryption and chunked storage, designed for performance and security. Implements AsyncStorage interface.\n\n```typescript\ninterface AsyncStorage {\n  getItem: (key: string) =\u003e Promise\u003cstring | null\u003e;\n  setItem: (key: string, value: string) =\u003e Promise\u003cunknown\u003e;\n  removeItem: (key: string) =\u003e Promise\u003cvoid\u003e;\n  clear: () =\u003e Promise\u003cvoid\u003e;\n}\n```\n\n## Why encrypt data in IndexedDB?\n\nEncryption keeps data in IndexedDB private, even when a browser profile is shared on the same device. Only one with access to the `cacheKey` can decrypt the data.\n\n## Installation\n\n```bash\nnpm install @instructure/idb-cache\n```\n\n## Usage\n\n```typescript\nimport { IDBCache } from '@instructure/idb-cache';\n\n// Initialize the cache\nconst cache = new IDBCache({\n  cacheKey: 'your-secure-key',\n  cacheBuster: 'unique-cache-buster',\n  // chunkSize?: number;\n  // cleanupInterval?: number;\n  // dbName?: string;\n  // debug?: boolean,\n  // gcTime?: number;\n  // maxTotalChunks?: number\n  // pbkdf2Iterations?: number;\n  // priority?: \"normal\" | \"low\"\n});\n\n// Store an item\nawait cache.setItem('key', 'value');\n\n// Retrieve an item\nconst token = await cache.getItem('key');\nconsole.log(token); // Outputs: 'value'\n\n// Remove an item\nawait cache.removeItem('key');\n\n// Count stored chunks\nconst totalChunks = await cache.count();\n\n// Removes expired items, busted items, and limits chunks\n// Runs at interval\nawait cache.cleanup();\n\n// Clears all items from cache\nawait cache.clear();\n\n// Destroy the cache instance\nawait cache.destroy();\n```\n\n## Features\n\n- **Web Worker**: Offloads encryption and decryption tasks to prevent blocking the main thread.\n- **Chunking**: Efficiently handles large data by splitting it into chunks.\n- **Encryption**: Secures data using AES-GCM with PBKDF2 key derivation.\n- **Garbage collection**: Expires and cleans up outdated cache entries.\n- **Task processing**: Uses parallelism and queue to mitigate crypto/CPU overload.\n\n## Usage with TanStack Query\n\nIntegrate idb-cache as an AsyncStorage persister for TanStack Query.\n\n```typescript\nimport { QueryClient } from '@tanstack/query-core';\nimport { experimental_createPersister as createPersister } from '@tanstack/query-persist-client-core';\nimport { IDBCache } from '@instructure/idb-cache';\n\nconst idbCache = new IDBCache({\n  cacheKey: 'user_cache_key',\n});\n\nconst persister = createPersister({\n  storage: idbCache,\n  maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days\n});\n\nexport const queryClient = new QueryClient({\n  defaultOptions: {\n    queries: {\n      staleTime: 1000 * 60 * 60, // 1 hour\n      gcTime: 1000 * 60 * 60 * 24 * 7, // 7 days\n      persister,\n    },\n  },\n});\n\n\n```\n\n## Data flow\n\n### setItem\n\n```mermaid\nflowchart TD\n subgraph Browser[\"Browser\"]\n        IDBCache[[\"IDBCache\"]]\n        Application([\"Client app\"])\n        WebWorker{{\"Web Worker\"}}\n        IndexedDB[(\"IndexedDB\")]\n  end\n    Application -- \".setItem()\" --\u003e IDBCache\n    IDBCache -. Unencrypted chunks .-\u003e WebWorker\n    WebWorker -. Encrypted chunks .-\u003e IDBCache\n    IDBCache -. Encrypted chunks .-\u003e IndexedDB\n```\n\n### getItem\n\n```mermaid\nflowchart TD\n subgraph Browser[\"Browser\"]\n    Application([Client app]) --\u003e |\".getItem()\"| IDBCache[[IDBCache]]\n    IDBCache --\u003e |Get chunks| IndexedDB[(IndexedDB)]\n    IndexedDB -.-\u003e |Encrypted chunks| IDBCache\n    IDBCache -.-\u003e |Encrypted chunks| WebWorker{{Web Worker}}\n    WebWorker -.-\u003e |Decrypted chunks| IDBCache\n    IDBCache --\u003e |\"Item\"| Application\n  end\n```\n\n## Technologies used\n\n- [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API)\n- [Web Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API)\n  - [SharedWorker](https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker)\n  - [MessageChannel](https://developer.mozilla.org/en-US/docs/Web/API/MessageChannel)\n  - [MessagePort](https://developer.mozilla.org/en-US/docs/Web/API/MessagePort)\n  - [Transferable objects](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Transferable_objects)\n- [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API)\n  - [SubtleCrypto](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto)\n  - [AES-GCM](https://developer.mozilla.org/en-US/docs/Web/API/AesGcmParams)\n  - [PBKDF2](https://developer.mozilla.org/en-US/docs/Web/API/Pbkdf2Params)\n- [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)\n\n## Related reading\n\n- [Is postMessage slow?](https://surma.dev/things/is-postmessage-slow/)\n- [Measure performance with the RAIL model](https://web.dev/articles/rail)\n- [LocalStorage vs. IndexedDB vs. Cookies vs. OPFS vs. WASM-SQLite](https://rxdb.info/articles/localstorage-indexeddb-cookies-opfs-sqlite-wasm.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finstructure%2Fidb-cache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finstructure%2Fidb-cache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finstructure%2Fidb-cache/lists"}