|
const cacheName = "llama-cpp-wasm-cache";
|
|
|
|
export async function loadBinaryResource(url, callback) {
|
|
let cache = null, window = self;
|
|
|
|
|
|
if (typeof window === "undefined") {
|
|
console.debug("`window` is not defined");
|
|
} else if (window && window.caches) {
|
|
cache = await window.caches.open(cacheName);
|
|
const cachedResponse = await cache.match(url);
|
|
|
|
if (cachedResponse) {
|
|
const data = await cachedResponse.arrayBuffer();
|
|
const byteArray = new Uint8Array(data);
|
|
callback(byteArray);
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const req = new XMLHttpRequest();
|
|
req.open("GET", url, true);
|
|
req.responseType = "arraybuffer";
|
|
|
|
req.onload = async (_) => {
|
|
const arrayBuffer = req.response;
|
|
|
|
if (arrayBuffer) {
|
|
const byteArray = new Uint8Array(arrayBuffer);
|
|
|
|
if (cache) {
|
|
await cache.put(url, new Response(arrayBuffer))
|
|
};
|
|
|
|
callback(byteArray);
|
|
}
|
|
};
|
|
|
|
req.send(null);
|
|
}
|
|
|