2023-02-15 19:41:22 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2023 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-03-17 09:36:22 +00:00
|
|
|
import fs from 'fs';
|
2023-02-15 19:41:22 +00:00
|
|
|
import path from 'path';
|
|
|
|
|
2023-03-16 07:16:44 +00:00
|
|
|
import {Browser, BrowserPlatform} from './browser-data/browser-data.js';
|
2023-02-15 23:09:31 +00:00
|
|
|
|
2023-04-21 09:34:03 +00:00
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
export type InstalledBrowser = {
|
|
|
|
path: string;
|
|
|
|
browser: Browser;
|
|
|
|
buildId: string;
|
|
|
|
platform: BrowserPlatform;
|
|
|
|
};
|
|
|
|
|
2023-02-15 19:41:22 +00:00
|
|
|
/**
|
|
|
|
* The cache used by Puppeteer relies on the following structure:
|
|
|
|
*
|
|
|
|
* - rootDir
|
|
|
|
* -- <browser1> | browserRoot(browser1)
|
2023-02-21 16:15:49 +00:00
|
|
|
* ---- <platform>-<buildId> | installationDir()
|
|
|
|
* ------ the browser-platform-buildId
|
2023-02-15 19:41:22 +00:00
|
|
|
* ------ specific structure.
|
|
|
|
* -- <browser2> | browserRoot(browser2)
|
2023-02-21 16:15:49 +00:00
|
|
|
* ---- <platform>-<buildId> | installationDir()
|
|
|
|
* ------ the browser-platform-buildId
|
2023-02-15 19:41:22 +00:00
|
|
|
* ------ specific structure.
|
|
|
|
* @internal
|
|
|
|
*/
|
2023-03-16 13:05:31 +00:00
|
|
|
export class Cache {
|
2023-02-15 19:41:22 +00:00
|
|
|
#rootDir: string;
|
|
|
|
|
|
|
|
constructor(rootDir: string) {
|
|
|
|
this.#rootDir = rootDir;
|
|
|
|
}
|
|
|
|
|
|
|
|
browserRoot(browser: Browser): string {
|
2023-03-28 08:30:25 +00:00
|
|
|
// Chromium is a special case for backward compatibility: we install it in
|
2023-03-29 10:37:49 +00:00
|
|
|
// the Chrome folder so that Puppeteer can find it.
|
2023-03-28 08:30:25 +00:00
|
|
|
return path.join(
|
|
|
|
this.#rootDir,
|
|
|
|
browser === Browser.CHROMIUM ? Browser.CHROME : browser
|
|
|
|
);
|
2023-02-15 19:41:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
installationDir(
|
|
|
|
browser: Browser,
|
|
|
|
platform: BrowserPlatform,
|
2023-02-21 16:15:49 +00:00
|
|
|
buildId: string
|
2023-02-15 19:41:22 +00:00
|
|
|
): string {
|
2023-02-21 16:15:49 +00:00
|
|
|
return path.join(this.browserRoot(browser), `${platform}-${buildId}`);
|
2023-02-15 19:41:22 +00:00
|
|
|
}
|
2023-03-16 13:05:31 +00:00
|
|
|
|
|
|
|
clear(): void {
|
2023-03-17 09:36:22 +00:00
|
|
|
fs.rmSync(this.#rootDir, {
|
|
|
|
force: true,
|
|
|
|
recursive: true,
|
2023-03-24 09:45:28 +00:00
|
|
|
maxRetries: 10,
|
2023-04-06 09:14:58 +00:00
|
|
|
retryDelay: 500,
|
2023-03-17 09:36:22 +00:00
|
|
|
});
|
2023-03-16 13:05:31 +00:00
|
|
|
}
|
2023-04-21 09:34:03 +00:00
|
|
|
|
|
|
|
getInstalledBrowsers(): InstalledBrowser[] {
|
|
|
|
if (!fs.existsSync(this.#rootDir)) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
const types = fs.readdirSync(this.#rootDir);
|
|
|
|
const browsers = types.filter((t): t is Browser => {
|
|
|
|
return (Object.values(Browser) as string[]).includes(t);
|
|
|
|
});
|
|
|
|
return browsers.flatMap(browser => {
|
|
|
|
const files = fs.readdirSync(this.browserRoot(browser));
|
|
|
|
return files
|
|
|
|
.map(file => {
|
|
|
|
const result = parseFolderPath(
|
|
|
|
path.join(this.browserRoot(browser), file)
|
|
|
|
);
|
|
|
|
if (!result) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
path: path.join(this.browserRoot(browser), file),
|
|
|
|
browser,
|
|
|
|
platform: result.platform,
|
|
|
|
buildId: result.buildId,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
.filter((item): item is InstalledBrowser => {
|
|
|
|
return item !== null;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function parseFolderPath(
|
|
|
|
folderPath: string
|
|
|
|
): {platform: string; buildId: string} | undefined {
|
|
|
|
const name = path.basename(folderPath);
|
|
|
|
const splits = name.split('-');
|
|
|
|
if (splits.length !== 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const [platform, buildId] = splits;
|
|
|
|
if (!buildId || !platform) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return {platform, buildId};
|
2023-02-15 19:41:22 +00:00
|
|
|
}
|