File size: 1,915 Bytes
c592d77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
0 && (module.exports = {
    getOrInitDiskLRU: null,
    resetDiskLRU: null
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    getOrInitDiskLRU: function() {
        return getOrInitDiskLRU;
    },
    resetDiskLRU: function() {
        return resetDiskLRU;
    }
});
const _fs = require("fs");
const _lrucache = require("./lru-cache");
/**
 * Module-level LRU singleton for disk cache eviction.
 * Initialized once on first `set()`, shared across all consumers.
 * Once resolved, the promise stays resolved — subsequent calls just await the cached result.
 */ let _diskLRUPromise = null;
async function getOrInitDiskLRU(cacheDir, maxDiskSize, readEntries, evictEntry) {
    if (!_diskLRUPromise) {
        _diskLRUPromise = (async ()=>{
            let maxSize = maxDiskSize;
            if (typeof maxSize === 'undefined') {
                // Ensure cacheDir exists before checking disk space
                await _fs.promises.mkdir(cacheDir, {
                    recursive: true
                });
                // Since config was not provided, default to 50% of available disk space
                const { bavail, bsize } = await _fs.promises.statfs(cacheDir);
                maxSize = Math.floor(bavail * bsize / 2);
            }
            const lru = new _lrucache.LRUCache(maxSize, (size)=>size, (cacheKey)=>evictEntry(cacheDir, cacheKey));
            const entries = await readEntries(cacheDir);
            for (const entry of entries){
                lru.set(entry.key, entry.size);
            }
            return lru;
        })();
    }
    return _diskLRUPromise;
}
function resetDiskLRU() {
    _diskLRUPromise = null;
}

//# sourceMappingURL=disk-lru-cache.external.js.map