Randolf Jung | bcb3bc8 | 2023-06-26 16:30:14 | [diff] [blame] | 1 | "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 | */ |
| 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.canDownload = exports.getInstalledBrowsers = exports.uninstall = exports.install = void 0; |
| 22 | const assert_1 = __importDefault(require("assert")); |
| 23 | const fs_1 = require("fs"); |
| 24 | const promises_1 = require("fs/promises"); |
| 25 | const os_1 = __importDefault(require("os")); |
| 26 | const path_1 = __importDefault(require("path")); |
| 27 | const browser_data_js_1 = require("./browser-data/browser-data.js"); |
| 28 | const Cache_js_1 = require("./Cache.js"); |
| 29 | const debug_js_1 = require("./debug.js"); |
| 30 | const detectPlatform_js_1 = require("./detectPlatform.js"); |
| 31 | const fileUtil_js_1 = require("./fileUtil.js"); |
| 32 | const httpUtil_js_1 = require("./httpUtil.js"); |
| 33 | const debugInstall = (0, debug_js_1.debug)('puppeteer:browsers:install'); |
| 34 | const times = new Map(); |
| 35 | function debugTime(label) { |
| 36 | times.set(label, process.hrtime()); |
| 37 | } |
| 38 | function 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 | */ |
| 50 | async 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 | } |
| 124 | exports.install = install; |
| 125 | /** |
| 126 | * |
| 127 | * @public |
| 128 | */ |
| 129 | async 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 | } |
| 136 | exports.uninstall = uninstall; |
| 137 | /** |
| 138 | * Returns metadata about browsers installed in the cache directory. |
| 139 | * |
| 140 | * @public |
| 141 | */ |
| 142 | async function getInstalledBrowsers(options) { |
| 143 | return new Cache_js_1.Cache(options.cacheDir).getInstalledBrowsers(); |
| 144 | } |
| 145 | exports.getInstalledBrowsers = getInstalledBrowsers; |
| 146 | /** |
| 147 | * @public |
| 148 | */ |
| 149 | async 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 | } |
| 156 | exports.canDownload = canDownload; |
| 157 | function getDownloadUrl(browser, platform, buildId, baseUrl) { |
| 158 | return new URL(browser_data_js_1.downloadUrls[browser](platform, buildId, baseUrl)); |
| 159 | } |
| 160 | //# sourceMappingURL=install.js.map |