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'; |
Alex Rudenko | f7ea7ab | 2023-10-24 09:40:51 | [diff] [blame^] | 17 | import os from 'os'; |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 18 | import path from 'path'; |
Alex Rudenko | f7ea7ab | 2023-10-24 09:40:51 | [diff] [blame^] | 19 | import { Browser, executablePathByBrowser, } from './browser-data/browser-data.js'; |
| 20 | import { detectBrowserPlatform } from './detectPlatform.js'; |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 21 | /** |
| 22 | * @public |
| 23 | */ |
| 24 | export class InstalledBrowser { |
| 25 | browser; |
| 26 | buildId; |
| 27 | platform; |
Alex Rudenko | f7ea7ab | 2023-10-24 09:40:51 | [diff] [blame^] | 28 | executablePath; |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 29 | #cache; |
| 30 | /** |
| 31 | * @internal |
| 32 | */ |
| 33 | constructor(cache, browser, buildId, platform) { |
| 34 | this.#cache = cache; |
| 35 | this.browser = browser; |
| 36 | this.buildId = buildId; |
| 37 | this.platform = platform; |
Alex Rudenko | f7ea7ab | 2023-10-24 09:40:51 | [diff] [blame^] | 38 | this.executablePath = cache.computeExecutablePath({ |
| 39 | browser, |
| 40 | buildId, |
| 41 | platform, |
| 42 | }); |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 43 | } |
| 44 | /** |
| 45 | * Path to the root of the installation folder. Use |
| 46 | * {@link computeExecutablePath} to get the path to the executable binary. |
| 47 | */ |
| 48 | get path() { |
| 49 | return this.#cache.installationDir(this.browser, this.platform, this.buildId); |
| 50 | } |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 51 | } |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 52 | /** |
| 53 | * The cache used by Puppeteer relies on the following structure: |
| 54 | * |
| 55 | * - rootDir |
| 56 | * -- <browser1> | browserRoot(browser1) |
| 57 | * ---- <platform>-<buildId> | installationDir() |
| 58 | * ------ the browser-platform-buildId |
| 59 | * ------ specific structure. |
| 60 | * -- <browser2> | browserRoot(browser2) |
| 61 | * ---- <platform>-<buildId> | installationDir() |
| 62 | * ------ the browser-platform-buildId |
| 63 | * ------ specific structure. |
| 64 | * @internal |
| 65 | */ |
| 66 | export class Cache { |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 67 | #rootDir; |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 68 | constructor(rootDir) { |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 69 | this.#rootDir = rootDir; |
| 70 | } |
| 71 | /** |
| 72 | * @internal |
| 73 | */ |
| 74 | get rootDir() { |
| 75 | return this.#rootDir; |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 76 | } |
| 77 | browserRoot(browser) { |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 78 | return path.join(this.#rootDir, browser); |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 79 | } |
| 80 | installationDir(browser, platform, buildId) { |
| 81 | return path.join(this.browserRoot(browser), `${platform}-${buildId}`); |
| 82 | } |
| 83 | clear() { |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 84 | fs.rmSync(this.#rootDir, { |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 85 | force: true, |
| 86 | recursive: true, |
| 87 | maxRetries: 10, |
| 88 | retryDelay: 500, |
| 89 | }); |
| 90 | } |
| 91 | uninstall(browser, platform, buildId) { |
| 92 | fs.rmSync(this.installationDir(browser, platform, buildId), { |
| 93 | force: true, |
| 94 | recursive: true, |
| 95 | maxRetries: 10, |
| 96 | retryDelay: 500, |
| 97 | }); |
| 98 | } |
| 99 | getInstalledBrowsers() { |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 100 | if (!fs.existsSync(this.#rootDir)) { |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 101 | return []; |
| 102 | } |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 103 | const types = fs.readdirSync(this.#rootDir); |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 104 | const browsers = types.filter((t) => { |
| 105 | return Object.values(Browser).includes(t); |
| 106 | }); |
| 107 | return browsers.flatMap(browser => { |
| 108 | const files = fs.readdirSync(this.browserRoot(browser)); |
| 109 | return files |
| 110 | .map(file => { |
| 111 | const result = parseFolderPath(path.join(this.browserRoot(browser), file)); |
| 112 | if (!result) { |
| 113 | return null; |
| 114 | } |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 115 | return new InstalledBrowser(this, browser, result.buildId, result.platform); |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 116 | }) |
| 117 | .filter((item) => { |
| 118 | return item !== null; |
| 119 | }); |
| 120 | }); |
| 121 | } |
Alex Rudenko | f7ea7ab | 2023-10-24 09:40:51 | [diff] [blame^] | 122 | computeExecutablePath(options) { |
| 123 | options.platform ??= detectBrowserPlatform(); |
| 124 | if (!options.platform) { |
| 125 | throw new Error(`Cannot download a binary for the provided platform: ${os.platform()} (${os.arch()})`); |
| 126 | } |
| 127 | const installationDir = this.installationDir(options.browser, options.platform, options.buildId); |
| 128 | return path.join(installationDir, executablePathByBrowser[options.browser](options.platform, options.buildId)); |
| 129 | } |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 130 | } |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 131 | function parseFolderPath(folderPath) { |
| 132 | const name = path.basename(folderPath); |
| 133 | const splits = name.split('-'); |
| 134 | if (splits.length !== 2) { |
| 135 | return; |
| 136 | } |
| 137 | const [platform, buildId] = splits; |
| 138 | if (!buildId || !platform) { |
| 139 | return; |
| 140 | } |
| 141 | return { platform, buildId }; |
| 142 | } |
| 143 | //# sourceMappingURL=Cache.js.map |