Cache.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. */
  16. import fs from 'fs';
  17. import path from 'path';
  18. import { Browser } from './browser-data/browser-data.js';
  19. /**
  20. * The cache used by Puppeteer relies on the following structure:
  21. *
  22. * - rootDir
  23. * -- <browser1> | browserRoot(browser1)
  24. * ---- <platform>-<buildId> | installationDir()
  25. * ------ the browser-platform-buildId
  26. * ------ specific structure.
  27. * -- <browser2> | browserRoot(browser2)
  28. * ---- <platform>-<buildId> | installationDir()
  29. * ------ the browser-platform-buildId
  30. * ------ specific structure.
  31. * @internal
  32. */
  33. export class Cache {
  34. #rootDir;
  35. constructor(rootDir) {
  36. this.#rootDir = rootDir;
  37. }
  38. browserRoot(browser) {
  39. return path.join(this.#rootDir, browser);
  40. }
  41. installationDir(browser, platform, buildId) {
  42. return path.join(this.browserRoot(browser), `${platform}-${buildId}`);
  43. }
  44. clear() {
  45. fs.rmSync(this.#rootDir, {
  46. force: true,
  47. recursive: true,
  48. maxRetries: 10,
  49. retryDelay: 500,
  50. });
  51. }
  52. uninstall(browser, platform, buildId) {
  53. fs.rmSync(this.installationDir(browser, platform, buildId), {
  54. force: true,
  55. recursive: true,
  56. maxRetries: 10,
  57. retryDelay: 500,
  58. });
  59. }
  60. getInstalledBrowsers() {
  61. if (!fs.existsSync(this.#rootDir)) {
  62. return [];
  63. }
  64. const types = fs.readdirSync(this.#rootDir);
  65. const browsers = types.filter((t) => {
  66. return Object.values(Browser).includes(t);
  67. });
  68. return browsers.flatMap(browser => {
  69. const files = fs.readdirSync(this.browserRoot(browser));
  70. return files
  71. .map(file => {
  72. const result = parseFolderPath(path.join(this.browserRoot(browser), file));
  73. if (!result) {
  74. return null;
  75. }
  76. return {
  77. path: path.join(this.browserRoot(browser), file),
  78. browser,
  79. platform: result.platform,
  80. buildId: result.buildId,
  81. };
  82. })
  83. .filter((item) => {
  84. return item !== null;
  85. });
  86. });
  87. }
  88. }
  89. function parseFolderPath(folderPath) {
  90. const name = path.basename(folderPath);
  91. const splits = name.split('-');
  92. if (splits.length !== 2) {
  93. return;
  94. }
  95. const [platform, buildId] = splits;
  96. if (!buildId || !platform) {
  97. return;
  98. }
  99. return { platform, buildId };
  100. }
  101. //# sourceMappingURL=Cache.js.map