File size: 1,246 Bytes
3a76a4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const cacheName = "llama-cpp-wasm-cache";

export async function loadBinaryResource(url, callback) {
    let cache = null, window = self;

    // Try to find if the model data is cached in Web Worker memory.
    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;
        }
    }


    // Download model and store in cache
    const req = new XMLHttpRequest();
    req.open("GET", url, true);
    req.responseType = "arraybuffer";

    req.onload = async (_) => {
        const arrayBuffer = req.response; // Note: not req.responseText
        
        if (arrayBuffer) {
            const byteArray = new Uint8Array(arrayBuffer);
            
            if (cache) {
                await cache.put(url, new Response(arrayBuffer))
            };

            callback(byteArray);
        }
    };

    req.send(null);
}