blob: c04e0310dd1dc7b4cddbbb93d327eeebcc522d0c [file] [log] [blame]
Randolf Jungbcb3bc82023-06-26 16:30:141"use strict";
2/**
3 * Copyright 2017 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 */
17var __importDefault = (this && this.__importDefault) || function (mod) {
18 return (mod && mod.__esModule) ? mod : { "default": mod };
19};
20Object.defineProperty(exports, "__esModule", { value: true });
21exports.canDownload = exports.getInstalledBrowsers = exports.uninstall = exports.install = void 0;
22const assert_1 = __importDefault(require("assert"));
23const fs_1 = require("fs");
24const promises_1 = require("fs/promises");
25const os_1 = __importDefault(require("os"));
26const path_1 = __importDefault(require("path"));
27const browser_data_js_1 = require("./browser-data/browser-data.js");
28const Cache_js_1 = require("./Cache.js");
29const debug_js_1 = require("./debug.js");
30const detectPlatform_js_1 = require("./detectPlatform.js");
31const fileUtil_js_1 = require("./fileUtil.js");
32const httpUtil_js_1 = require("./httpUtil.js");
33const debugInstall = (0, debug_js_1.debug)('puppeteer:browsers:install');
34const times = new Map();
35function debugTime(label) {
36 times.set(label, process.hrtime());
37}
38function debugTimeEnd(label) {
39 const end = process.hrtime();
40 const start = times.get(label);
41 if (!start) {
42 return;
43 }
44 const duration = end[0] * 1000 + end[1] / 1e6 - (start[0] * 1000 + start[1] / 1e6); // calculate duration in milliseconds
45 debugInstall(`Duration for ${label}: ${duration}ms`);
46}
47/**
48 * @public
49 */
50async function install(options) {
51 options.platform ??= (0, detectPlatform_js_1.detectBrowserPlatform)();
52 options.unpack ??= true;
53 if (!options.platform) {
54 throw new Error(`Cannot download a binary for the provided platform: ${os_1.default.platform()} (${os_1.default.arch()})`);
55 }
56 const url = getDownloadUrl(options.browser, options.platform, options.buildId, options.baseUrl);
57 const fileName = url.toString().split('/').pop();
58 (0, assert_1.default)(fileName, `A malformed download URL was found: ${url}.`);
59 const structure = new Cache_js_1.Cache(options.cacheDir);
60 const browserRoot = structure.browserRoot(options.browser);
61 const archivePath = path_1.default.join(browserRoot, fileName);
62 if (!(0, fs_1.existsSync)(browserRoot)) {
63 await (0, promises_1.mkdir)(browserRoot, { recursive: true });
64 }
65 if (!options.unpack) {
66 if ((0, fs_1.existsSync)(archivePath)) {
67 return {
68 path: archivePath,
69 browser: options.browser,
70 platform: options.platform,
71 buildId: options.buildId,
72 };
73 }
74 debugInstall(`Downloading binary from ${url}`);
75 debugTime('download');
76 await (0, httpUtil_js_1.downloadFile)(url, archivePath, options.downloadProgressCallback);
77 debugTimeEnd('download');
78 return {
79 path: archivePath,
80 browser: options.browser,
81 platform: options.platform,
82 buildId: options.buildId,
83 };
84 }
85 const outputPath = structure.installationDir(options.browser, options.platform, options.buildId);
86 if ((0, fs_1.existsSync)(outputPath)) {
87 return {
88 path: outputPath,
89 browser: options.browser,
90 platform: options.platform,
91 buildId: options.buildId,
92 };
93 }
94 try {
95 debugInstall(`Downloading binary from ${url}`);
96 try {
97 debugTime('download');
98 await (0, httpUtil_js_1.downloadFile)(url, archivePath, options.downloadProgressCallback);
99 }
100 finally {
101 debugTimeEnd('download');
102 }
103 debugInstall(`Installing ${archivePath} to ${outputPath}`);
104 try {
105 debugTime('extract');
106 await (0, fileUtil_js_1.unpackArchive)(archivePath, outputPath);
107 }
108 finally {
109 debugTimeEnd('extract');
110 }
111 }
112 finally {
113 if ((0, fs_1.existsSync)(archivePath)) {
114 await (0, promises_1.unlink)(archivePath);
115 }
116 }
117 return {
118 path: outputPath,
119 browser: options.browser,
120 platform: options.platform,
121 buildId: options.buildId,
122 };
123}
124exports.install = install;
125/**
126 *
127 * @public
128 */
129async function uninstall(options) {
130 options.platform ??= (0, detectPlatform_js_1.detectBrowserPlatform)();
131 if (!options.platform) {
132 throw new Error(`Cannot detect the browser platform for: ${os_1.default.platform()} (${os_1.default.arch()})`);
133 }
134 new Cache_js_1.Cache(options.cacheDir).uninstall(options.browser, options.platform, options.buildId);
135}
136exports.uninstall = uninstall;
137/**
138 * Returns metadata about browsers installed in the cache directory.
139 *
140 * @public
141 */
142async function getInstalledBrowsers(options) {
143 return new Cache_js_1.Cache(options.cacheDir).getInstalledBrowsers();
144}
145exports.getInstalledBrowsers = getInstalledBrowsers;
146/**
147 * @public
148 */
149async function canDownload(options) {
150 options.platform ??= (0, detectPlatform_js_1.detectBrowserPlatform)();
151 if (!options.platform) {
152 throw new Error(`Cannot download a binary for the provided platform: ${os_1.default.platform()} (${os_1.default.arch()})`);
153 }
154 return await (0, httpUtil_js_1.headHttpRequest)(getDownloadUrl(options.browser, options.platform, options.buildId, options.baseUrl));
155}
156exports.canDownload = canDownload;
157function getDownloadUrl(browser, platform, buildId, baseUrl) {
158 return new URL(browser_data_js_1.downloadUrls[browser](platform, buildId, baseUrl));
159}
160//# sourceMappingURL=install.js.map