Full face stack that runs entirely in the browser. Detection, 576-point 3D mesh, recognition, anti-spoof, smile — all WebAssembly, zero server. Apache 2.0.
C avx-512avx2biometricsc99computer-visioncpu-inferencedeep-learningedge-aiembedded-aiface-embeddingface-recognitionface-verificationlow-latencyonnx-runtimeproduction-readysimdzero-dependencies
/*
* cache.js — IndexedDB weight caching for FaceX WASM.
* First load: fetch from network, store in IndexedDB.
* Subsequent loads: read from IndexedDB (instant).
*/
const CACHE_DB = 'facex-cache';
const CACHE_STORE = 'weights';
const CACHE_VERSION = 1;
function openCacheDB() {
return new Promise((resolve, reject) => {
const req = indexedDB.open(CACHE_DB, CACHE_VERSION);
req.onupgradeneeded = () => {
req.result.createObjectStore(CACHE_STORE);
};
req.onsuccess = () => resolve(req.result);
req.onerror = () => reject(req.error);
});
}
async function getCached(key) {
const db = await openCacheDB();
try {
return await new Promise((resolve, reject) => {
const tx = db.transaction(CACHE_STORE, 'readonly');
const req = tx.objectStore(CACHE_STORE).get(key);
req.onsuccess = () => resolve(req.result || null);
req.onerror = () => reject(req.error);
tx.onabort = () => reject(tx.error);
});
} finally {
db.close();
}
}
async function putCached(key, data) {
const db = await openCacheDB();
try {
await new Promise((resolve, reject) => {
const tx = db.transaction(CACHE_STORE, 'readwrite');
tx.oncomplete = () => resolve();
tx.onabort = tx.onerror = () => reject(tx.error);
tx.objectStore(CACHE_STORE).put(data, key);
});
} finally {
db.close();
}
}
/**
* Load weights with IndexedDB caching.
* @param {string} url - URL to fetch weights from
* @param {string} key - Cache key (e.g. 'det_weights' or 'emb_weights')
* @param {function} onProgress - callback(loaded, total) for progress
* @returns {Promise<Uint8Array>}
*/
async function loadWeightsCached(url, key, onProgress) {
// Try cache first
const absolute = new URL(url, globalThis.document?.baseURI || globalThis.location.href).href;
key = [key, absolute];
const cached = await getCached(key).catch(() => null);
if (cached) {
if (onProgress) onProgress(cached.byteLength, cached.byteLength);
return new Uint8Array(cached);
}
// Fetch from network with progress
const response = await fetch(absolute);
if (!response.ok) throw new Error('Weight request failed with status ' + response.status);
if (!onProgress || !response.body) {
const data = await response.arrayBuffer();
await putCached(key, data).catch(() => {});
onProgress?.(data.byteLength, data.byteLength);
return new Uint8Array(data);
}
const total = parseInt(response.headers.get('content-length')) || 0;
const reader = response.body.getReader();
const chunks = [];
let loaded = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
loaded += value.length;
if (onProgress) onProgress(loaded, total);
}
// Combine chunks
const data = new Uint8Array(loaded);
let offset = 0;
for (const chunk of chunks) {
data.set(chunk, offset);
offset += chunk.length;
}
// Store in cache
await putCached(key, data.buffer).catch(() => {});
return data;
}
if (typeof module !== 'undefined' && module.exports) module.exports = {loadWeightsCached};