Guest blog post on Cross-Origin Storage in Transformers.js
I'm very excited to share a guest post I wrote for the Hugging Face π€ blog! The post is called πΒ Experimenting with the proposed Cross-Origin Storage API in Transformers.jsΒ π! This proposed new browser API, navigator.crossOriginStorage.requestFileHandle(hash), has the potential of revolutionizing the Web, a little bit at least. Learn more by reading the Explainer for the Cross-Origin Storage (COS) API.
And Transformers.js aren't alone with experimenting with this, they're joined by WebLLM (docs), wllama (code), Flutter (code), and Emscripten (docs).
In a nutshell, this is the usual flow of using the API:
const hash = {
algorithm: 'SHA-256',
value: '8f434346648f6b96df89dda901c5176b10a6d83961dd3c1ac88b59b2dc327aa4',
};
try {
const handle = await navigator.crossOriginStorage.requestFileHandle(hash);
// Cache hit! Get the file as a Blob and use it directly.
const fileBlob = await handle.getFile();
} catch {
// Cache miss. Download from network, then store for next time.
const fileBlob = await fetch('https://cdn.jsdelivr.net/.../ort-wasm-simd-threaded.asyncify.wasm')
.then(r => r.blob());
const handle = await navigator.crossOriginStorage.requestFileHandle(
hash,
{ create: true, origins: '*' },
);
const writableStream = await handle.createWritable();
await writableStream.write(fileBlob);
await writableStream.close();
}