Skip to content

Commit

Permalink
feat: support atomic browser installation - attempt 2 (#3008)
Browse files Browse the repository at this point in the history
Currently, Ctrl-C while extracting browser might yield users in
a bad place.

This patch adds a marker file inside browser directory to make
sure that browser extraction completed.

Note: this was already attempted in #2489, but was eventually
reverted in #2534.

References #2660
  • Loading branch information
aslushnikov authored Jul 17, 2020
1 parent a75835e commit ef2a652
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/install/browserPaths.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ export function browserDirectory(browsersPath: string, browser: BrowserDescripto
return path.join(browsersPath, `${browser.name}-${browser.revision}`);
}

export function markerFilePath(browsersPath: string, browser: BrowserDescriptor): string {
return path.join(browserDirectory(browsersPath, browser), 'INSTALLATION_COMPLETE');
}

export function isBrowserDirectory(browserPath: string): boolean {
const baseName = path.basename(browserPath);
return baseName.startsWith('chromium-') || baseName.startsWith('firefox-') || baseName.startsWith('webkit-');
Expand Down
21 changes: 16 additions & 5 deletions src/install/installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import * as browserFetcher from './browserFetcher';
const fsMkdirAsync = util.promisify(fs.mkdir.bind(fs));
const fsReaddirAsync = util.promisify(fs.readdir.bind(fs));
const fsReadFileAsync = util.promisify(fs.readFile.bind(fs));
const fsExistsAsync = (filePath: string) => fsReadFileAsync(filePath).then(() => true).catch(e => false);
const fsUnlinkAsync = util.promisify(fs.unlink.bind(fs));
const fsWriteFileAsync = util.promisify(fs.writeFile.bind(fs));
const removeFolderAsync = util.promisify(removeFolder);
Expand All @@ -44,15 +45,24 @@ export async function installBrowsersWithProgressBar(packagePath: string) {
}

async function validateCache(packagePath: string, browsersPath: string, linksDir: string) {
// 1. Collect unused downloads and package descriptors.
const allBrowsers: browserPaths.BrowserDescriptor[] = [];
// 1. Collect used downloads and package descriptors.
const usedBrowserPaths: Set<string> = new Set();
for (const fileName of await fsReaddirAsync(linksDir)) {
const linkPath = path.join(linksDir, fileName);
let linkTarget = '';
try {
linkTarget = (await fsReadFileAsync(linkPath)).toString();
const browsers = JSON.parse((await fsReadFileAsync(path.join(linkTarget, 'browsers.json'))).toString())['browsers'];
allBrowsers.push(...browsers);
for (const browser of browsers) {
const usedBrowserPath = browserPaths.browserDirectory(browsersPath, browser);
const browserRevision = parseInt(browser.revision, 10);
// Old browser installations don't have marker file.
const shouldHaveMarkerFile = (browser.name === 'chromium' && browserRevision >= 786218) ||
(browser.name === 'firefox' && browserRevision >= 1128) ||
(browser.name === 'webkit' && browserRevision >= 1307);
if (!shouldHaveMarkerFile || (await fsExistsAsync(browserPaths.markerFilePath(browsersPath, browser))))
usedBrowserPaths.add(usedBrowserPath);
}
} catch (e) {
if (linkTarget)
logPolitely('Failed to process descriptor at ' + linkTarget);
Expand All @@ -64,8 +74,8 @@ async function validateCache(packagePath: string, browsersPath: string, linksDir
let downloadedBrowsers = (await fsReaddirAsync(browsersPath)).map(file => path.join(browsersPath, file));
downloadedBrowsers = downloadedBrowsers.filter(file => browserPaths.isBrowserDirectory(file));
const directories = new Set<string>(downloadedBrowsers);
for (const browser of allBrowsers)
directories.delete(browserPaths.browserDirectory(browsersPath, browser));
for (const browserPath of usedBrowserPaths)
directories.delete(browserPath);
for (const directory of directories) {
logPolitely('Removing unused browser at ' + directory);
await removeFolderAsync(directory).catch(e => {});
Expand All @@ -76,6 +86,7 @@ async function validateCache(packagePath: string, browsersPath: string, linksDir
for (const browser of myBrowsers) {
const browserPath = browserPaths.browserDirectory(browsersPath, browser);
await browserFetcher.downloadBrowserWithProgressBar(browserPath, browser);
await fsWriteFileAsync(browserPaths.markerFilePath(browsersPath, browser), '');
}
}

Expand Down

0 comments on commit ef2a652

Please sign in to comment.