• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

overlookmotel / livepack / 7308909869

23 Dec 2023 02:53PM UTC coverage: 90.423% (-0.03%) from 90.448%
7308909869

push

github

overlookmotel
TODO

4807 of 5168 branches covered (0.0%)

Branch coverage included in aggregate %.

12821 of 14327 relevant lines covered (89.49%)

9003.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.24
/lib/register/cache.js
1
/* --------------------
64✔
2
 * livepack module
64✔
3
 * `register` cache
64✔
4
 * ------------------*/
64✔
5

64✔
6
'use strict';
64✔
7

64✔
8
// Modules
64✔
9
const pathJoin = require('path').join,
64✔
10
        {readFileSync, writeFileSync, mkdirSync} = require('fs'),
64✔
11
        os = require('os'),
64✔
12
        findCacheDir = require('find-cache-dir');
64✔
13

64✔
14
// Imports
64✔
15
const livepackVersion = require('../../package.json').version;
64✔
16

64✔
17
// Exports
64✔
18

64✔
19
module.exports = {openCache, closeCache};
64✔
20

64✔
21
const cacheDirPath = findCacheDir({name: 'livepack'})
64✔
22
        || pathJoin(os.homedir() || os.tmpdir(), '.livepack');
64!
23
const cachePath = pathJoin(cacheDirPath, `register-${livepackVersion}.json`);
64✔
24

64✔
25
let cache, cacheContent,
64✔
26
        isDirty = false,
64✔
27
        isAwaitingSave = false;
64✔
28

64✔
29
/**
64✔
30
 * Open cache or get existing cache.
64✔
31
 * @returns {Object} - Cache object
64✔
32
 */
64✔
33
function openCache() {
64✔
34
        if (!cache) createCache();
64✔
35
        return cache;
64✔
36
}
64✔
37

64✔
38
/**
64✔
39
 * Close cache.
64✔
40
 * Write cache to disc and delete from memory.
64✔
41
 * @returns {undefined}
64✔
42
 */
64✔
43
function closeCache() {
64✔
44
        if (!cache) return;
64!
45

64✔
46
        saveCache();
64✔
47
        cache = undefined;
64✔
48
        cacheContent = undefined;
64✔
49
}
64✔
50

64✔
51
/**
64✔
52
 * Load cache from disc and create cache object.
64✔
53
 * @returns {undefined}
64✔
54
 */
64✔
55
function createCache() {
64✔
56
        // Load cache content from disc
64✔
57
        cacheContent = loadCache();
64✔
58

64✔
59
        // If failed to load, create new cache
64✔
60
        if (!cacheContent) {
64✔
61
                cacheContent = {};
2✔
62
                isDirty = true;
2✔
63
                saveCacheOnNextTick();
2✔
64
        }
2✔
65

64✔
66
        // Create cache object
64✔
67
        cache = {
64✔
68
                getKey(filename, esm, jsx) {
64✔
69
                        return JSON.stringify({filename, esm, jsx});
×
70
                },
64✔
71

64✔
72
                get(key, lastMod) {
64✔
73
                        const cached = cacheContent[key];
×
74
                        if (cached && cached.lastMod === lastMod) return cached;
×
75
                        return undefined;
×
76
                },
64✔
77

64✔
78
                save(key, lastMod, code, map) {
64✔
79
                        cacheContent[key] = {lastMod, code, map};
×
80
                        isDirty = true;
×
81
                        saveCacheOnNextTick();
×
82
                }
×
83
        };
64✔
84
}
64✔
85

64✔
86
/**
64✔
87
 * Load cache from disc.
64✔
88
 * Fail silently if cannot read cache.
64✔
89
 * @returns {Object} - Cache content object
64✔
90
 */
64✔
91
function loadCache() {
64✔
92
        try {
64✔
93
                const buff = readFileSync(cachePath);
64✔
94
                return JSON.parse(buff);
64✔
95
        } catch {
64✔
96
                return undefined;
2✔
97
        }
2✔
98
}
64✔
99

64✔
100
/**
64✔
101
 * Save cache content to disc on next tick.
64✔
102
 * @returns {undefined}
64✔
103
 */
64✔
104
function saveCacheOnNextTick() {
2✔
105
        if (isAwaitingSave) return;
2!
106
        isAwaitingSave = true;
2✔
107
        process.nextTick(saveCache);
2✔
108
}
2✔
109

64✔
110
/**
64✔
111
 * Save cache content to disc.
64✔
112
 * Fail silently if cannot write cache.
64✔
113
 * @returns {undefined}
64✔
114
 */
64✔
115
function saveCache() {
64✔
116
        if (isAwaitingSave) isAwaitingSave = false;
64✔
117
        if (!isDirty) return;
64✔
118

2✔
119
        isDirty = false;
2✔
120

2✔
121
        let contentStr;
2✔
122
        try {
2✔
123
                contentStr = JSON.stringify(cacheContent);
2✔
124
        } catch {
2!
125
                return;
×
126
        }
×
127

2✔
128
        try {
2✔
129
                writeFileSync(cachePath, contentStr);
2✔
130
        } catch {
2✔
131
                try {
2✔
132
                        mkdirSync(cacheDirPath, {recursive: true});
2✔
133
                        writeFileSync(cachePath, contentStr);
2✔
134
                } catch {} // eslint-disable-line no-empty
2!
135
        }
2✔
136
}
64✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc