-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Caching the downloaded archive (#30)
* Implement filesystem caching of downloaded binary Closes #24 * Fix failure to install multiple times on Mac * Cater for different OSs sharing node_modules Closes #25 * Update proposed filesystem caching after feedback * Turn caching on by default, using default filesystem locations * `CHROMIUM_CACHE` now overrides default cache location * Add flag `CHROMIUM_CACHE_SKIP` to disable caching * Many little refactors as suggested * Updated readme accordingly * Delete temporary files when the process exits * Refactor setEnvVar test util method * Ask wise and all-knowing friend for cache location * Rename CHROMIUM_CACHE to NODE_CHROMIUM_CACHE * Update readme * Rename cacheDir variable now cachedir is in scope dtolstyi would never let me get away with that :) * Rename all env vars CHROMIUM_* > NODE_CHROMIUM_* * More resilient progress bar and rename env var
- Loading branch information
1 parent
78f0289
commit f4bd3e8
Showing
12 changed files
with
342 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.