GitHub - P0u4a/opfs-cache: Cache large responses on the web with the Origin Private File System

1 min read Original article ↗

Origin Private File System (OPFS) Cache

Web Cache API compliant implementation using the Origin Private File System (OPFS) to store responses. Useful for caching large responses such as Hugging Face models or any other large file that exceeds the typical caching quota's of the browser. Also works in Web Workers.

Installation

npm install @p0u4a/opfs-cache

Usage in Web Workers

The OPFS Cache can also be run inside a web worker. It automatically detects if the current execution is happening in a web worker and uses the more efficient synchronous read/write file system APIs instead.

Quick Start

Put a response into the cache

const blob = await response.blob();
await cache.put(new Request("https://example.com/path/to/file"), new Response(blob));

Try to match a cached response

const response = await cache.match(new Request("https://example.com/path/to/file"));

List all cached entries

const entries = await cache.keys();

Delete a cached entry

await cache.delete(new Request("https://example.com/path/to/file"));

Use as a Custom Cache For Hugging Face Transformers.js

import { OPFSCache } from "@p0u4a/opfs-cache";
import { env } from "@huggingface/transformers";

const opfsModelCache = new OPFSCache("transformers-cache");

env.useBrowserCache = false;
env.useCustomCache = true;
env.customCache = opfsModelCache;