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

overlookmotel / livepack / 7308750553

23 Dec 2023 02:16PM UTC coverage: 90.526% (-0.03%) from 90.551%
7308750553

push

github

overlookmotel
Use `+` to conform strings to numbers [refactor]

4691 of 5049 branches covered (0.0%)

Branch coverage included in aggregate %.

13 of 13 new or added lines in 10 files covered. (100.0%)

31 existing lines in 3 files now uncovered.

12470 of 13908 relevant lines covered (89.66%)

8709.63 hits per line

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

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

62✔
6
'use strict';
62✔
7

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

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

62✔
17
// Exports
62✔
18

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

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

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

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

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

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

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

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

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

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

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

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

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

62✔
110
/**
62✔
111
 * Save cache content to disc.
62✔
112
 * Fail silently if cannot write cache.
62✔
113
 * @returns {undefined}
62✔
114
 */
62✔
115
function saveCache() {
62✔
116
        if (isAwaitingSave) isAwaitingSave = false;
62✔
117
        if (!isDirty) return;
62✔
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;
×
UNCOV
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
}
62✔
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