Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 1 | /** |
Alex Rudenko | af11b7c | 2024-02-06 10:44:42 | [diff] [blame] | 2 | * @license |
| 3 | * Copyright 2023 Google Inc. |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 5 | */ |
Nikolay Vitkov | 95ea045 | 2025-04-30 15:52:34 | [diff] [blame^] | 6 | import fs from 'node:fs'; |
| 7 | import os from 'node:os'; |
| 8 | import path from 'node:path'; |
Alex Rudenko | d04dd45 | 2024-02-27 08:25:35 | [diff] [blame] | 9 | import debug from 'debug'; |
| 10 | import { Browser, executablePathByBrowser, getVersionComparator, } from './browser-data/browser-data.js'; |
Alex Rudenko | f7ea7ab | 2023-10-24 09:40:51 | [diff] [blame] | 11 | import { detectBrowserPlatform } from './detectPlatform.js'; |
Alex Rudenko | d04dd45 | 2024-02-27 08:25:35 | [diff] [blame] | 12 | const debugCache = debug('puppeteer:browsers:cache'); |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 13 | /** |
| 14 | * @public |
| 15 | */ |
| 16 | export class InstalledBrowser { |
| 17 | browser; |
| 18 | buildId; |
| 19 | platform; |
Alex Rudenko | f7ea7ab | 2023-10-24 09:40:51 | [diff] [blame] | 20 | executablePath; |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 21 | #cache; |
| 22 | /** |
| 23 | * @internal |
| 24 | */ |
| 25 | constructor(cache, browser, buildId, platform) { |
| 26 | this.#cache = cache; |
| 27 | this.browser = browser; |
| 28 | this.buildId = buildId; |
| 29 | this.platform = platform; |
Alex Rudenko | f7ea7ab | 2023-10-24 09:40:51 | [diff] [blame] | 30 | this.executablePath = cache.computeExecutablePath({ |
| 31 | browser, |
| 32 | buildId, |
| 33 | platform, |
| 34 | }); |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 35 | } |
| 36 | /** |
| 37 | * Path to the root of the installation folder. Use |
| 38 | * {@link computeExecutablePath} to get the path to the executable binary. |
| 39 | */ |
| 40 | get path() { |
| 41 | return this.#cache.installationDir(this.browser, this.platform, this.buildId); |
| 42 | } |
Alex Rudenko | d04dd45 | 2024-02-27 08:25:35 | [diff] [blame] | 43 | readMetadata() { |
| 44 | return this.#cache.readMetadata(this.browser); |
| 45 | } |
| 46 | writeMetadata(metadata) { |
| 47 | this.#cache.writeMetadata(this.browser, metadata); |
| 48 | } |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 49 | } |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 50 | /** |
| 51 | * The cache used by Puppeteer relies on the following structure: |
| 52 | * |
| 53 | * - rootDir |
| 54 | * -- <browser1> | browserRoot(browser1) |
| 55 | * ---- <platform>-<buildId> | installationDir() |
| 56 | * ------ the browser-platform-buildId |
| 57 | * ------ specific structure. |
| 58 | * -- <browser2> | browserRoot(browser2) |
| 59 | * ---- <platform>-<buildId> | installationDir() |
| 60 | * ------ the browser-platform-buildId |
| 61 | * ------ specific structure. |
| 62 | * @internal |
| 63 | */ |
| 64 | export class Cache { |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 65 | #rootDir; |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 66 | constructor(rootDir) { |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 67 | this.#rootDir = rootDir; |
| 68 | } |
| 69 | /** |
| 70 | * @internal |
| 71 | */ |
| 72 | get rootDir() { |
| 73 | return this.#rootDir; |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 74 | } |
| 75 | browserRoot(browser) { |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 76 | return path.join(this.#rootDir, browser); |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 77 | } |
Alex Rudenko | d04dd45 | 2024-02-27 08:25:35 | [diff] [blame] | 78 | metadataFile(browser) { |
| 79 | return path.join(this.browserRoot(browser), '.metadata'); |
| 80 | } |
| 81 | readMetadata(browser) { |
| 82 | const metatadaPath = this.metadataFile(browser); |
| 83 | if (!fs.existsSync(metatadaPath)) { |
| 84 | return { aliases: {} }; |
| 85 | } |
| 86 | // TODO: add type-safe parsing. |
| 87 | const data = JSON.parse(fs.readFileSync(metatadaPath, 'utf8')); |
| 88 | if (typeof data !== 'object') { |
| 89 | throw new Error('.metadata is not an object'); |
| 90 | } |
| 91 | return data; |
| 92 | } |
| 93 | writeMetadata(browser, metadata) { |
| 94 | const metatadaPath = this.metadataFile(browser); |
| 95 | fs.mkdirSync(path.dirname(metatadaPath), { recursive: true }); |
| 96 | fs.writeFileSync(metatadaPath, JSON.stringify(metadata, null, 2)); |
| 97 | } |
| 98 | resolveAlias(browser, alias) { |
| 99 | const metadata = this.readMetadata(browser); |
| 100 | if (alias === 'latest') { |
| 101 | return Object.values(metadata.aliases || {}) |
| 102 | .sort(getVersionComparator(browser)) |
| 103 | .at(-1); |
| 104 | } |
| 105 | return metadata.aliases[alias]; |
| 106 | } |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 107 | installationDir(browser, platform, buildId) { |
| 108 | return path.join(this.browserRoot(browser), `${platform}-${buildId}`); |
| 109 | } |
| 110 | clear() { |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 111 | fs.rmSync(this.#rootDir, { |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 112 | force: true, |
| 113 | recursive: true, |
| 114 | maxRetries: 10, |
| 115 | retryDelay: 500, |
| 116 | }); |
| 117 | } |
| 118 | uninstall(browser, platform, buildId) { |
Alex Rudenko | d04dd45 | 2024-02-27 08:25:35 | [diff] [blame] | 119 | const metadata = this.readMetadata(browser); |
| 120 | for (const alias of Object.keys(metadata.aliases)) { |
| 121 | if (metadata.aliases[alias] === buildId) { |
| 122 | delete metadata.aliases[alias]; |
| 123 | } |
| 124 | } |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 125 | fs.rmSync(this.installationDir(browser, platform, buildId), { |
| 126 | force: true, |
| 127 | recursive: true, |
| 128 | maxRetries: 10, |
| 129 | retryDelay: 500, |
| 130 | }); |
| 131 | } |
| 132 | getInstalledBrowsers() { |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 133 | if (!fs.existsSync(this.#rootDir)) { |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 134 | return []; |
| 135 | } |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 136 | const types = fs.readdirSync(this.#rootDir); |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 137 | const browsers = types.filter((t) => { |
| 138 | return Object.values(Browser).includes(t); |
| 139 | }); |
| 140 | return browsers.flatMap(browser => { |
| 141 | const files = fs.readdirSync(this.browserRoot(browser)); |
| 142 | return files |
| 143 | .map(file => { |
| 144 | const result = parseFolderPath(path.join(this.browserRoot(browser), file)); |
| 145 | if (!result) { |
| 146 | return null; |
| 147 | } |
Randolf Jung | 3e52631 | 2023-08-08 06:20:39 | [diff] [blame] | 148 | return new InstalledBrowser(this, browser, result.buildId, result.platform); |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 149 | }) |
| 150 | .filter((item) => { |
| 151 | return item !== null; |
| 152 | }); |
| 153 | }); |
| 154 | } |
Alex Rudenko | f7ea7ab | 2023-10-24 09:40:51 | [diff] [blame] | 155 | computeExecutablePath(options) { |
| 156 | options.platform ??= detectBrowserPlatform(); |
| 157 | if (!options.platform) { |
| 158 | throw new Error(`Cannot download a binary for the provided platform: ${os.platform()} (${os.arch()})`); |
| 159 | } |
Alex Rudenko | d04dd45 | 2024-02-27 08:25:35 | [diff] [blame] | 160 | try { |
| 161 | options.buildId = |
| 162 | this.resolveAlias(options.browser, options.buildId) ?? options.buildId; |
| 163 | } |
| 164 | catch { |
| 165 | debugCache('could not read .metadata file for the browser'); |
| 166 | } |
Alex Rudenko | f7ea7ab | 2023-10-24 09:40:51 | [diff] [blame] | 167 | const installationDir = this.installationDir(options.browser, options.platform, options.buildId); |
| 168 | return path.join(installationDir, executablePathByBrowser[options.browser](options.platform, options.buildId)); |
| 169 | } |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 170 | } |
Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 171 | function parseFolderPath(folderPath) { |
| 172 | const name = path.basename(folderPath); |
| 173 | const splits = name.split('-'); |
| 174 | if (splits.length !== 2) { |
| 175 | return; |
| 176 | } |
| 177 | const [platform, buildId] = splits; |
| 178 | if (!buildId || !platform) { |
| 179 | return; |
| 180 | } |
| 181 | return { platform, buildId }; |
| 182 | } |
| 183 | //# sourceMappingURL=Cache.js.map |