summaryrefslogtreecommitdiffstats
path: root/remote/test/puppeteer/packages/puppeteer/src/node/install.ts
blob: 76bad868b8ed8236ec4bf72b0db0bbdf0b744846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
 * @license
 * Copyright 2020 Google Inc.
 * SPDX-License-Identifier: Apache-2.0
 */

import {
  install,
  Browser,
  resolveBuildId,
  makeProgressCallback,
  detectBrowserPlatform,
} from '@puppeteer/browsers';
import type {Product} from 'puppeteer-core';
import {PUPPETEER_REVISIONS} from 'puppeteer-core/internal/revisions.js';

import {getConfiguration} from '../getConfiguration.js';

/**
 * @internal
 */
const supportedProducts = {
  chrome: 'Chrome',
  firefox: 'Firefox Nightly',
} as const;

/**
 * @internal
 */
export async function downloadBrowser(): Promise<void> {
  overrideProxy();

  const configuration = getConfiguration();
  if (configuration.skipDownload) {
    logPolitely('**INFO** Skipping browser download as instructed.');
    return;
  }

  const downloadBaseUrl = configuration.downloadBaseUrl;

  const platform = detectBrowserPlatform();
  if (!platform) {
    throw new Error('The current platform is not supported.');
  }

  const product = configuration.defaultProduct!;
  const browser = productToBrowser(product);

  const unresolvedBuildId =
    configuration.browserRevision || PUPPETEER_REVISIONS[product] || 'latest';
  const unresolvedShellBuildId =
    configuration.browserRevision ||
    PUPPETEER_REVISIONS['chrome-headless-shell'] ||
    'latest';

  // TODO: deprecate downloadPath in favour of cacheDirectory.
  const cacheDir = configuration.downloadPath ?? configuration.cacheDirectory!;

  try {
    const installationJobs = [];

    if (configuration.skipChromeDownload) {
      logPolitely('**INFO** Skipping Chrome download as instructed.');
    } else {
      const buildId = await resolveBuildId(
        browser,
        platform,
        unresolvedBuildId
      );
      installationJobs.push(
        install({
          browser,
          cacheDir,
          platform,
          buildId,
          downloadProgressCallback: makeProgressCallback(browser, buildId),
          baseUrl: downloadBaseUrl,
        })
          .then(result => {
            logPolitely(
              `${supportedProducts[product]} (${result.buildId}) downloaded to ${result.path}`
            );
          })
          .catch(error => {
            throw new Error(
              `ERROR: Failed to set up ${supportedProducts[product]} v${buildId}! Set "PUPPETEER_SKIP_DOWNLOAD" env variable to skip download.`,
              {
                cause: error,
              }
            );
          })
      );
    }

    if (browser === Browser.CHROME) {
      if (configuration.skipChromeHeadlessShellDownload) {
        logPolitely('**INFO** Skipping Chrome download as instructed.');
      } else {
        const shellBuildId = await resolveBuildId(
          browser,
          platform,
          unresolvedShellBuildId
        );

        installationJobs.push(
          install({
            browser: Browser.CHROMEHEADLESSSHELL,
            cacheDir,
            platform,
            buildId: shellBuildId,
            downloadProgressCallback: makeProgressCallback(
              browser,
              shellBuildId
            ),
            baseUrl: downloadBaseUrl,
          })
            .then(result => {
              logPolitely(
                `${Browser.CHROMEHEADLESSSHELL} (${result.buildId}) downloaded to ${result.path}`
              );
            })
            .catch(error => {
              throw new Error(
                `ERROR: Failed to set up ${Browser.CHROMEHEADLESSSHELL} v${shellBuildId}! Set "PUPPETEER_SKIP_DOWNLOAD" env variable to skip download.`,
                {
                  cause: error,
                }
              );
            })
        );
      }
    }

    await Promise.all(installationJobs);
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
}

function productToBrowser(product?: Product) {
  switch (product) {
    case 'chrome':
      return Browser.CHROME;
    case 'firefox':
      return Browser.FIREFOX;
  }
  return Browser.CHROME;
}

/**
 * @internal
 */
function logPolitely(toBeLogged: unknown): void {
  const logLevel = process.env['npm_config_loglevel'] || '';
  const logLevelDisplay = ['silent', 'error', 'warn'].indexOf(logLevel) > -1;

  // eslint-disable-next-line no-console
  if (!logLevelDisplay) {
    console.log(toBeLogged);
  }
}

/**
 * @internal
 */
function overrideProxy() {
  // Override current environment proxy settings with npm configuration, if any.
  const NPM_HTTPS_PROXY =
    process.env['npm_config_https_proxy'] || process.env['npm_config_proxy'];
  const NPM_HTTP_PROXY =
    process.env['npm_config_http_proxy'] || process.env['npm_config_proxy'];
  const NPM_NO_PROXY = process.env['npm_config_no_proxy'];

  if (NPM_HTTPS_PROXY) {
    process.env['HTTPS_PROXY'] = NPM_HTTPS_PROXY;
  }
  if (NPM_HTTP_PROXY) {
    process.env['HTTP_PROXY'] = NPM_HTTP_PROXY;
  }
  if (NPM_NO_PROXY) {
    process.env['NO_PROXY'] = NPM_NO_PROXY;
  }
}