Cache.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. "use strict";
  2. /**
  3. * Copyright 2023 Google Inc. All rights reserved.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. var __importDefault = (this && this.__importDefault) || function (mod) {
  18. return (mod && mod.__esModule) ? mod : { "default": mod };
  19. };
  20. Object.defineProperty(exports, "__esModule", { value: true });
  21. exports.Cache = void 0;
  22. const fs_1 = __importDefault(require("fs"));
  23. const path_1 = __importDefault(require("path"));
  24. const browser_data_js_1 = require("./browser-data/browser-data.js");
  25. /**
  26. * The cache used by Puppeteer relies on the following structure:
  27. *
  28. * - rootDir
  29. * -- <browser1> | browserRoot(browser1)
  30. * ---- <platform>-<buildId> | installationDir()
  31. * ------ the browser-platform-buildId
  32. * ------ specific structure.
  33. * -- <browser2> | browserRoot(browser2)
  34. * ---- <platform>-<buildId> | installationDir()
  35. * ------ the browser-platform-buildId
  36. * ------ specific structure.
  37. * @internal
  38. */
  39. class Cache {
  40. #rootDir;
  41. constructor(rootDir) {
  42. this.#rootDir = rootDir;
  43. }
  44. browserRoot(browser) {
  45. return path_1.default.join(this.#rootDir, browser);
  46. }
  47. installationDir(browser, platform, buildId) {
  48. return path_1.default.join(this.browserRoot(browser), `${platform}-${buildId}`);
  49. }
  50. clear() {
  51. fs_1.default.rmSync(this.#rootDir, {
  52. force: true,
  53. recursive: true,
  54. maxRetries: 10,
  55. retryDelay: 500,
  56. });
  57. }
  58. uninstall(browser, platform, buildId) {
  59. fs_1.default.rmSync(this.installationDir(browser, platform, buildId), {
  60. force: true,
  61. recursive: true,
  62. maxRetries: 10,
  63. retryDelay: 500,
  64. });
  65. }
  66. getInstalledBrowsers() {
  67. if (!fs_1.default.existsSync(this.#rootDir)) {
  68. return [];
  69. }
  70. const types = fs_1.default.readdirSync(this.#rootDir);
  71. const browsers = types.filter((t) => {
  72. return Object.values(browser_data_js_1.Browser).includes(t);
  73. });
  74. return browsers.flatMap(browser => {
  75. const files = fs_1.default.readdirSync(this.browserRoot(browser));
  76. return files
  77. .map(file => {
  78. const result = parseFolderPath(path_1.default.join(this.browserRoot(browser), file));
  79. if (!result) {
  80. return null;
  81. }
  82. return {
  83. path: path_1.default.join(this.browserRoot(browser), file),
  84. browser,
  85. platform: result.platform,
  86. buildId: result.buildId,
  87. };
  88. })
  89. .filter((item) => {
  90. return item !== null;
  91. });
  92. });
  93. }
  94. }
  95. exports.Cache = Cache;
  96. function parseFolderPath(folderPath) {
  97. const name = path_1.default.basename(folderPath);
  98. const splits = name.split('-');
  99. if (splits.length !== 2) {
  100. return;
  101. }
  102. const [platform, buildId] = splits;
  103. if (!buildId || !platform) {
  104. return;
  105. }
  106. return { platform, buildId };
  107. }
  108. //# sourceMappingURL=Cache.js.map