Cache.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * @license
  3. * Copyright 2023 Google Inc.
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. import fs from 'fs';
  7. import os from 'os';
  8. import path from 'path';
  9. import debug from 'debug';
  10. import { Browser, executablePathByBrowser, getVersionComparator, } from './browser-data/browser-data.js';
  11. import { detectBrowserPlatform } from './detectPlatform.js';
  12. const debugCache = debug('puppeteer:browsers:cache');
  13. /**
  14. * @public
  15. */
  16. export class InstalledBrowser {
  17. browser;
  18. buildId;
  19. platform;
  20. executablePath;
  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;
  30. this.executablePath = cache.computeExecutablePath({
  31. browser,
  32. buildId,
  33. platform,
  34. });
  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. }
  43. readMetadata() {
  44. return this.#cache.readMetadata(this.browser);
  45. }
  46. writeMetadata(metadata) {
  47. this.#cache.writeMetadata(this.browser, metadata);
  48. }
  49. }
  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 {
  65. #rootDir;
  66. constructor(rootDir) {
  67. this.#rootDir = rootDir;
  68. }
  69. /**
  70. * @internal
  71. */
  72. get rootDir() {
  73. return this.#rootDir;
  74. }
  75. browserRoot(browser) {
  76. return path.join(this.#rootDir, browser);
  77. }
  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. }
  107. installationDir(browser, platform, buildId) {
  108. return path.join(this.browserRoot(browser), `${platform}-${buildId}`);
  109. }
  110. clear() {
  111. fs.rmSync(this.#rootDir, {
  112. force: true,
  113. recursive: true,
  114. maxRetries: 10,
  115. retryDelay: 500,
  116. });
  117. }
  118. uninstall(browser, platform, buildId) {
  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. }
  125. fs.rmSync(this.installationDir(browser, platform, buildId), {
  126. force: true,
  127. recursive: true,
  128. maxRetries: 10,
  129. retryDelay: 500,
  130. });
  131. }
  132. getInstalledBrowsers() {
  133. if (!fs.existsSync(this.#rootDir)) {
  134. return [];
  135. }
  136. const types = fs.readdirSync(this.#rootDir);
  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. }
  148. return new InstalledBrowser(this, browser, result.buildId, result.platform);
  149. })
  150. .filter((item) => {
  151. return item !== null;
  152. });
  153. });
  154. }
  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. }
  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. }
  167. const installationDir = this.installationDir(options.browser, options.platform, options.buildId);
  168. return path.join(installationDir, executablePathByBrowser[options.browser](options.platform, options.buildId));
  169. }
  170. }
  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