Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2023 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 16 | import fs from 'fs'; |
| 17 | import path from 'path'; |
| 18 | import { Browser } from './browser-data/browser-data.js'; |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 19 | import { computeExecutablePath } from './launch.js'; |
| 20 | /** |
| 21 | * @public |
| 22 | */ |
| 23 | export class InstalledBrowser { |
| 24 | browser; |
| 25 | buildId; |
| 26 | platform; |
| 27 | #cache; |
| 28 | /** |
| 29 | * @internal |
| 30 | */ |
| 31 | constructor(cache, browser, buildId, platform) { |
| 32 | this.#cache = cache; |
| 33 | this.browser = browser; |
| 34 | this.buildId = buildId; |
| 35 | this.platform = platform; |
| 36 | } |
| 37 | /** |
| 38 | * Path to the root of the installation folder. Use |
| 39 | * {@link computeExecutablePath} to get the path to the executable binary. |
| 40 | */ |
| 41 | get path() { |
| 42 | return this.#cache.installationDir(this.browser, this.platform, this.buildId); |
| 43 | } |
| 44 | get executablePath() { |
| 45 | return computeExecutablePath({ |
| 46 | cacheDir: this.#cache.rootDir, |
| 47 | platform: this.platform, |
| 48 | browser: this.browser, |
| 49 | buildId: this.buildId, |
| 50 | }); |
| 51 | } |
| 52 | } |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 53 | /** |
| 54 | * The cache used by Puppeteer relies on the following structure: |
| 55 | * |
| 56 | * - rootDir |
| 57 | * -- <browser1> | browserRoot(browser1) |
| 58 | * ---- <platform>-<buildId> | installationDir() |
| 59 | * ------ the browser-platform-buildId |
| 60 | * ------ specific structure. |
| 61 | * -- <browser2> | browserRoot(browser2) |
| 62 | * ---- <platform>-<buildId> | installationDir() |
| 63 | * ------ the browser-platform-buildId |
| 64 | * ------ specific structure. |
| 65 | * @internal |
| 66 | */ |
| 67 | export class Cache { |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 68 | #rootDir; |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 69 | constructor(rootDir) { |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 70 | this.#rootDir = rootDir; |
| 71 | } |
| 72 | /** |
| 73 | * @internal |
| 74 | */ |
| 75 | get rootDir() { |
| 76 | return this.#rootDir; |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 77 | } |
| 78 | browserRoot(browser) { |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 79 | return path.join(this.#rootDir, browser); |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 80 | } |
| 81 | installationDir(browser, platform, buildId) { |
| 82 | return path.join(this.browserRoot(browser), `${platform}-${buildId}`); |
| 83 | } |
| 84 | clear() { |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 85 | fs.rmSync(this.#rootDir, { |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 86 | force: true, |
| 87 | recursive: true, |
| 88 | maxRetries: 10, |
| 89 | retryDelay: 500, |
| 90 | }); |
| 91 | } |
| 92 | uninstall(browser, platform, buildId) { |
| 93 | fs.rmSync(this.installationDir(browser, platform, buildId), { |
| 94 | force: true, |
| 95 | recursive: true, |
| 96 | maxRetries: 10, |
| 97 | retryDelay: 500, |
| 98 | }); |
| 99 | } |
| 100 | getInstalledBrowsers() { |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 101 | if (!fs.existsSync(this.#rootDir)) { |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 102 | return []; |
| 103 | } |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 104 | const types = fs.readdirSync(this.#rootDir); |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 105 | const browsers = types.filter((t) => { |
| 106 | return Object.values(Browser).includes(t); |
| 107 | }); |
| 108 | return browsers.flatMap(browser => { |
| 109 | const files = fs.readdirSync(this.browserRoot(browser)); |
| 110 | return files |
| 111 | .map(file => { |
| 112 | const result = parseFolderPath(path.join(this.browserRoot(browser), file)); |
| 113 | if (!result) { |
| 114 | return null; |
| 115 | } |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 116 | return new InstalledBrowser(this, browser, result.buildId, result.platform); |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 117 | }) |
| 118 | .filter((item) => { |
| 119 | return item !== null; |
| 120 | }); |
| 121 | }); |
| 122 | } |
| 123 | } |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 124 | function parseFolderPath(folderPath) { |
| 125 | const name = path.basename(folderPath); |
| 126 | const splits = name.split('-'); |
| 127 | if (splits.length !== 2) { |
| 128 | return; |
| 129 | } |
| 130 | const [platform, buildId] = splits; |
| 131 | if (!buildId || !platform) { |
| 132 | return; |
| 133 | } |
| 134 | return { platform, buildId }; |
| 135 | } |
| 136 | //# sourceMappingURL=Cache.js.map |