Skip to content

Commit ef2a652

Browse files
authored
feat: support atomic browser installation - attempt 2 (#3008)
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
1 parent a75835e commit ef2a652

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

src/install/browserPaths.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ export function browserDirectory(browsersPath: string, browser: BrowserDescripto
118118
return path.join(browsersPath, `${browser.name}-${browser.revision}`);
119119
}
120120

121+
export function markerFilePath(browsersPath: string, browser: BrowserDescriptor): string {
122+
return path.join(browserDirectory(browsersPath, browser), 'INSTALLATION_COMPLETE');
123+
}
124+
121125
export function isBrowserDirectory(browserPath: string): boolean {
122126
const baseName = path.basename(browserPath);
123127
return baseName.startsWith('chromium-') || baseName.startsWith('firefox-') || baseName.startsWith('webkit-');

src/install/installer.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import * as browserFetcher from './browserFetcher';
2626
const fsMkdirAsync = util.promisify(fs.mkdir.bind(fs));
2727
const fsReaddirAsync = util.promisify(fs.readdir.bind(fs));
2828
const fsReadFileAsync = util.promisify(fs.readFile.bind(fs));
29+
const fsExistsAsync = (filePath: string) => fsReadFileAsync(filePath).then(() => true).catch(e => false);
2930
const fsUnlinkAsync = util.promisify(fs.unlink.bind(fs));
3031
const fsWriteFileAsync = util.promisify(fs.writeFile.bind(fs));
3132
const removeFolderAsync = util.promisify(removeFolder);
@@ -44,15 +45,24 @@ export async function installBrowsersWithProgressBar(packagePath: string) {
4445
}
4546

4647
async function validateCache(packagePath: string, browsersPath: string, linksDir: string) {
47-
// 1. Collect unused downloads and package descriptors.
48-
const allBrowsers: browserPaths.BrowserDescriptor[] = [];
48+
// 1. Collect used downloads and package descriptors.
49+
const usedBrowserPaths: Set<string> = new Set();
4950
for (const fileName of await fsReaddirAsync(linksDir)) {
5051
const linkPath = path.join(linksDir, fileName);
5152
let linkTarget = '';
5253
try {
5354
linkTarget = (await fsReadFileAsync(linkPath)).toString();
5455
const browsers = JSON.parse((await fsReadFileAsync(path.join(linkTarget, 'browsers.json'))).toString())['browsers'];
55-
allBrowsers.push(...browsers);
56+
for (const browser of browsers) {
57+
const usedBrowserPath = browserPaths.browserDirectory(browsersPath, browser);
58+
const browserRevision = parseInt(browser.revision, 10);
59+
// Old browser installations don't have marker file.
60+
const shouldHaveMarkerFile = (browser.name === 'chromium' && browserRevision >= 786218) ||
61+
(browser.name === 'firefox' && browserRevision >= 1128) ||
62+
(browser.name === 'webkit' && browserRevision >= 1307);
63+
if (!shouldHaveMarkerFile || (await fsExistsAsync(browserPaths.markerFilePath(browsersPath, browser))))
64+
usedBrowserPaths.add(usedBrowserPath);
65+
}
5666
} catch (e) {
5767
if (linkTarget)
5868
logPolitely('Failed to process descriptor at ' + linkTarget);
@@ -64,8 +74,8 @@ async function validateCache(packagePath: string, browsersPath: string, linksDir
6474
let downloadedBrowsers = (await fsReaddirAsync(browsersPath)).map(file => path.join(browsersPath, file));
6575
downloadedBrowsers = downloadedBrowsers.filter(file => browserPaths.isBrowserDirectory(file));
6676
const directories = new Set<string>(downloadedBrowsers);
67-
for (const browser of allBrowsers)
68-
directories.delete(browserPaths.browserDirectory(browsersPath, browser));
77+
for (const browserPath of usedBrowserPaths)
78+
directories.delete(browserPath);
6979
for (const directory of directories) {
7080
logPolitely('Removing unused browser at ' + directory);
7181
await removeFolderAsync(directory).catch(e => {});
@@ -76,6 +86,7 @@ async function validateCache(packagePath: string, browsersPath: string, linksDir
7686
for (const browser of myBrowsers) {
7787
const browserPath = browserPaths.browserDirectory(browsersPath, browser);
7888
await browserFetcher.downloadBrowserWithProgressBar(browserPath, browser);
89+
await fsWriteFileAsync(browserPaths.markerFilePath(browsersPath, browser), '');
7990
}
8091
}
8192

0 commit comments

Comments
 (0)