blob: f3ba68686d740df944aaf93ceab8523ff9edd8e8 [file] [log] [blame]
Randolf Jungbcb3bc82023-06-26 16:30:141/**
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 */
16var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
17 if (kind === "m") throw new TypeError("Private method is not writable");
18 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
19 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
20 return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
21};
22var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
23 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
24 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
25 return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
26};
27var _Cache_rootDir;
28import fs from 'fs';
29import path from 'path';
30import { Browser } from './browser-data/browser-data.js';
31/**
32 * The cache used by Puppeteer relies on the following structure:
33 *
34 * - rootDir
35 * -- <browser1> | browserRoot(browser1)
36 * ---- <platform>-<buildId> | installationDir()
37 * ------ the browser-platform-buildId
38 * ------ specific structure.
39 * -- <browser2> | browserRoot(browser2)
40 * ---- <platform>-<buildId> | installationDir()
41 * ------ the browser-platform-buildId
42 * ------ specific structure.
43 * @internal
44 */
45export class Cache {
46 constructor(rootDir) {
47 _Cache_rootDir.set(this, void 0);
48 __classPrivateFieldSet(this, _Cache_rootDir, rootDir, "f");
49 }
50 browserRoot(browser) {
51 return path.join(__classPrivateFieldGet(this, _Cache_rootDir, "f"), browser);
52 }
53 installationDir(browser, platform, buildId) {
54 return path.join(this.browserRoot(browser), `${platform}-${buildId}`);
55 }
56 clear() {
57 fs.rmSync(__classPrivateFieldGet(this, _Cache_rootDir, "f"), {
58 force: true,
59 recursive: true,
60 maxRetries: 10,
61 retryDelay: 500,
62 });
63 }
64 uninstall(browser, platform, buildId) {
65 fs.rmSync(this.installationDir(browser, platform, buildId), {
66 force: true,
67 recursive: true,
68 maxRetries: 10,
69 retryDelay: 500,
70 });
71 }
72 getInstalledBrowsers() {
73 if (!fs.existsSync(__classPrivateFieldGet(this, _Cache_rootDir, "f"))) {
74 return [];
75 }
76 const types = fs.readdirSync(__classPrivateFieldGet(this, _Cache_rootDir, "f"));
77 const browsers = types.filter((t) => {
78 return Object.values(Browser).includes(t);
79 });
80 return browsers.flatMap(browser => {
81 const files = fs.readdirSync(this.browserRoot(browser));
82 return files
83 .map(file => {
84 const result = parseFolderPath(path.join(this.browserRoot(browser), file));
85 if (!result) {
86 return null;
87 }
88 return {
89 path: path.join(this.browserRoot(browser), file),
90 browser,
91 platform: result.platform,
92 buildId: result.buildId,
93 };
94 })
95 .filter((item) => {
96 return item !== null;
97 });
98 });
99 }
100}
101_Cache_rootDir = new WeakMap();
102function parseFolderPath(folderPath) {
103 const name = path.basename(folderPath);
104 const splits = name.split('-');
105 if (splits.length !== 2) {
106 return;
107 }
108 const [platform, buildId] = splits;
109 if (!buildId || !platform) {
110 return;
111 }
112 return { platform, buildId };
113}
114//# sourceMappingURL=Cache.js.map