browserFetcher.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.downloadBrowserWithProgressBar = downloadBrowserWithProgressBar;
  6. exports.logPolitely = logPolitely;
  7. var _fs = _interopRequireDefault(require("fs"));
  8. var _os = _interopRequireDefault(require("os"));
  9. var _path = _interopRequireDefault(require("path"));
  10. var _child_process = _interopRequireDefault(require("child_process"));
  11. var _fileUtils = require("../../utils/fileUtils");
  12. var _debugLogger = require("../../common/debugLogger");
  13. var _manualPromise = require("../../utils/manualPromise");
  14. var _utilsBundle = require("../../utilsBundle");
  15. var _ = require(".");
  16. var _userAgent = require("../../utils/userAgent");
  17. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  18. /**
  19. * Copyright 2017 Google Inc. All rights reserved.
  20. * Modifications copyright (c) Microsoft Corporation.
  21. *
  22. * Licensed under the Apache License, Version 2.0 (the "License");
  23. * you may not use this file except in compliance with the License.
  24. * You may obtain a copy of the License at
  25. *
  26. * http://www.apache.org/licenses/LICENSE-2.0
  27. *
  28. * Unless required by applicable law or agreed to in writing, software
  29. * distributed under the License is distributed on an "AS IS" BASIS,
  30. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  31. * See the License for the specific language governing permissions and
  32. * limitations under the License.
  33. */
  34. async function downloadBrowserWithProgressBar(title, browserDirectory, executablePath, downloadURLs, downloadFileName, downloadConnectionTimeout) {
  35. if (await (0, _fileUtils.existsAsync)((0, _.browserDirectoryToMarkerFilePath)(browserDirectory))) {
  36. // Already downloaded.
  37. _debugLogger.debugLogger.log('install', `${title} is already downloaded.`);
  38. return false;
  39. }
  40. const zipPath = _path.default.join(_os.default.tmpdir(), downloadFileName);
  41. try {
  42. const retryCount = 3;
  43. for (let attempt = 1; attempt <= retryCount; ++attempt) {
  44. _debugLogger.debugLogger.log('install', `downloading ${title} - attempt #${attempt}`);
  45. const url = downloadURLs[(attempt - 1) % downloadURLs.length];
  46. logPolitely(`Downloading ${title}` + _utilsBundle.colors.dim(` from ${url}`));
  47. const {
  48. error
  49. } = await downloadBrowserWithProgressBarOutOfProcess(title, browserDirectory, url, zipPath, executablePath, downloadConnectionTimeout);
  50. if (!error) {
  51. _debugLogger.debugLogger.log('install', `SUCCESS installing ${title}`);
  52. break;
  53. }
  54. if (await (0, _fileUtils.existsAsync)(zipPath)) await _fs.default.promises.unlink(zipPath);
  55. if (await (0, _fileUtils.existsAsync)(browserDirectory)) await _fs.default.promises.rmdir(browserDirectory, {
  56. recursive: true
  57. });
  58. const errorMessage = (error === null || error === void 0 ? void 0 : error.message) || '';
  59. _debugLogger.debugLogger.log('install', `attempt #${attempt} - ERROR: ${errorMessage}`);
  60. if (attempt >= retryCount) throw error;
  61. }
  62. } catch (e) {
  63. _debugLogger.debugLogger.log('install', `FAILED installation ${title} with error: ${e}`);
  64. process.exitCode = 1;
  65. throw e;
  66. } finally {
  67. if (await (0, _fileUtils.existsAsync)(zipPath)) await _fs.default.promises.unlink(zipPath);
  68. }
  69. logPolitely(`${title} downloaded to ${browserDirectory}`);
  70. return true;
  71. }
  72. /**
  73. * Node.js has a bug where the process can exit with 0 code even though there was an uncaught exception.
  74. * Thats why we execute it in a separate process and check manually if the destination file exists.
  75. * https://github.com/microsoft/playwright/issues/17394
  76. */
  77. function downloadBrowserWithProgressBarOutOfProcess(title, browserDirectory, url, zipPath, executablePath, connectionTimeout) {
  78. const cp = _child_process.default.fork(_path.default.join(__dirname, 'oopDownloadBrowserMain.js'));
  79. const promise = new _manualPromise.ManualPromise();
  80. const progress = getDownloadProgress();
  81. cp.on('message', message => {
  82. if ((message === null || message === void 0 ? void 0 : message.method) === 'log') _debugLogger.debugLogger.log('install', message.params.message);
  83. if ((message === null || message === void 0 ? void 0 : message.method) === 'progress') progress(message.params.done, message.params.total);
  84. });
  85. cp.on('exit', code => {
  86. if (code !== 0) {
  87. promise.resolve({
  88. error: new Error(`Download failure, code=${code}`)
  89. });
  90. return;
  91. }
  92. if (!_fs.default.existsSync((0, _.browserDirectoryToMarkerFilePath)(browserDirectory))) promise.resolve({
  93. error: new Error(`Download failure, ${(0, _.browserDirectoryToMarkerFilePath)(browserDirectory)} does not exist`)
  94. });else promise.resolve({
  95. error: null
  96. });
  97. });
  98. cp.on('error', error => {
  99. promise.resolve({
  100. error
  101. });
  102. });
  103. _debugLogger.debugLogger.log('install', `running download:`);
  104. _debugLogger.debugLogger.log('install', `-- from url: ${url}`);
  105. _debugLogger.debugLogger.log('install', `-- to location: ${zipPath}`);
  106. const downloadParams = {
  107. title,
  108. browserDirectory,
  109. url,
  110. zipPath,
  111. executablePath,
  112. connectionTimeout,
  113. userAgent: (0, _userAgent.getUserAgent)()
  114. };
  115. cp.send({
  116. method: 'download',
  117. params: downloadParams
  118. });
  119. return promise;
  120. }
  121. function logPolitely(toBeLogged) {
  122. const logLevel = process.env.npm_config_loglevel;
  123. const logLevelDisplay = ['silent', 'error', 'warn'].indexOf(logLevel || '') > -1;
  124. if (!logLevelDisplay) console.log(toBeLogged); // eslint-disable-line no-console
  125. }
  126. function getDownloadProgress() {
  127. if (process.stdout.isTTY) return getAnimatedDownloadProgress();
  128. return getBasicDownloadProgress();
  129. }
  130. function getAnimatedDownloadProgress() {
  131. let progressBar;
  132. let lastDownloadedBytes = 0;
  133. return (downloadedBytes, totalBytes) => {
  134. if (!progressBar) {
  135. progressBar = new _utilsBundle.progress(`${toMegabytes(totalBytes)} [:bar] :percent :etas`, {
  136. complete: '=',
  137. incomplete: ' ',
  138. width: 20,
  139. total: totalBytes
  140. });
  141. }
  142. const delta = downloadedBytes - lastDownloadedBytes;
  143. lastDownloadedBytes = downloadedBytes;
  144. progressBar.tick(delta);
  145. };
  146. }
  147. function getBasicDownloadProgress() {
  148. const totalRows = 10;
  149. const stepWidth = 8;
  150. let lastRow = -1;
  151. return (downloadedBytes, totalBytes) => {
  152. const percentage = downloadedBytes / totalBytes;
  153. const row = Math.floor(totalRows * percentage);
  154. if (row > lastRow) {
  155. lastRow = row;
  156. const percentageString = String(percentage * 100 | 0).padStart(3);
  157. // eslint-disable-next-line no-console
  158. console.log(`|${'■'.repeat(row * stepWidth)}${' '.repeat((totalRows - row) * stepWidth)}| ${percentageString}% of ${toMegabytes(totalBytes)}`);
  159. }
  160. };
  161. }
  162. function toMegabytes(bytes) {
  163. const mb = bytes / 1024 / 1024;
  164. return `${Math.round(mb * 10) / 10} MiB`;
  165. }