-
Notifications
You must be signed in to change notification settings - Fork 33
/
cache.js
57 lines (51 loc) · 1.82 KB
/
cache.js
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';
const path = require('path');
const fs = require('fs');
const cachedir = require('cachedir');
const config = require('./config');
/**
* Retrieve a Chromium archive from filesystem cache.
* @param {string} revision The Chromium revision to retrieve.
* @returns {string} The path to the cached Chromium archive. Falsy if not found.
*/
function get(revision) {
const cachePath = buildCachePath(revision);
if (fs.existsSync(cachePath)) {
return cachePath;
}
return '';
}
/**
* Store a Chromium archive in filesystem cache for future use.
* Has no effect if the user has not configured a cache location.
* @param {string} revision The Chromium revision in the archive file.
* @param {string} filePath The path to the Chromium archive file to store in cache.
*/
function put(revision, filePath) {
const cachePath = buildCachePath(revision);
if (cachePath && filePath) {
try {
fs.mkdirSync(path.dirname(cachePath), {recursive: true});
fs.copyFileSync(filePath, cachePath);
} catch (error) {
// Don't die on cache fail
console.error('Could not cache file', cachePath, error);
}
}
}
/**
* Get the unique cache path for this revision, on this platform and architecture.
* @param {string} revision The revision of this Chromium binary, essentially a unique cache key.
* @returns {string} The cache path, or falsy if caching is not enabled.
*/
function buildCachePath(revision) {
if (!revision || config.getEnvVar('NODE_CHROMIUM_CACHE_DISABLE').toLowerCase() === 'true') {
return '';
}
const cachePath = config.getEnvVar('NODE_CHROMIUM_CACHE_PATH') || cachedir('node-chromium');
return path.join(cachePath, `chromium-${revision}-${process.platform}-${process.arch}.zip`);
}
module.exports = {
get,
put
};