File size: 10,374 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
"use strict";

const la = require('lazy-ass');
const is = require('check-more-types');
const os = require('os');
const url = require('url');
const path = require('path');
const debug = require('debug')('cypress:cli');
const request = require('@cypress/request');
const Promise = require('bluebird');
const requestProgress = require('request-progress');
const {
  stripIndent
} = require('common-tags');
const getProxyForUrl = require('proxy-from-env').getProxyForUrl;
const {
  throwFormErrorText,
  errors
} = require('../errors');
const fs = require('../fs');
const util = require('../util');
const defaultBaseUrl = 'https://download.cypress.io/';
const defaultMaxRedirects = 10;
const getProxyForUrlWithNpmConfig = url => {
  return getProxyForUrl(url) || process.env.npm_config_https_proxy || process.env.npm_config_proxy || null;
};
const getBaseUrl = () => {
  if (util.getEnv('CYPRESS_DOWNLOAD_MIRROR')) {
    let baseUrl = util.getEnv('CYPRESS_DOWNLOAD_MIRROR');
    if (!baseUrl.endsWith('/')) {
      baseUrl += '/';
    }
    return baseUrl;
  }
  return defaultBaseUrl;
};
const getCA = () => {
  return new Promise(resolve => {
    if (process.env.npm_config_cafile) {
      fs.readFile(process.env.npm_config_cafile, 'utf8').then(cafileContent => {
        resolve(cafileContent);
      }).catch(() => {
        resolve();
      });
    } else if (process.env.npm_config_ca) {
      resolve(process.env.npm_config_ca);
    } else {
      resolve();
    }
  });
};
const prepend = (arch, urlPath, version) => {
  const endpoint = url.resolve(getBaseUrl(), urlPath);
  const platform = os.platform();
  const pathTemplate = util.getEnv('CYPRESS_DOWNLOAD_PATH_TEMPLATE', true);
  return pathTemplate ? pathTemplate.replace(/\\?\$\{endpoint\}/g, endpoint).replace(/\\?\$\{platform\}/g, platform).replace(/\\?\$\{arch\}/g, arch).replace(/\\?\$\{version\}/g, version) : `${endpoint}?platform=${platform}&arch=${arch}`;
};
const getUrl = (arch, version) => {
  if (is.url(version)) {
    debug('version is already an url', version);
    return version;
  }
  const urlPath = version ? `desktop/${version}` : 'desktop';
  return prepend(arch, urlPath, version);
};
const statusMessage = err => {
  return err.statusCode ? [err.statusCode, err.statusMessage].join(' - ') : err.toString();
};
const prettyDownloadErr = (err, url) => {
  const msg = stripIndent`
    URL: ${url}
    ${statusMessage(err)}
  `;
  debug(msg);
  return throwFormErrorText(errors.failedDownload)(msg);
};

/**
 * Checks checksum and file size for the given file. Allows both
 * values or just one of them to be checked.
 */
const verifyDownloadedFile = (filename, expectedSize, expectedChecksum) => {
  if (expectedSize && expectedChecksum) {
    debug('verifying checksum and file size');
    return Promise.join(util.getFileChecksum(filename), util.getFileSize(filename), (checksum, filesize) => {
      if (checksum === expectedChecksum && filesize === expectedSize) {
        debug('downloaded file has the expected checksum and size ✅');
        return;
      }
      debug('raising error: checksum or file size mismatch');
      const text = stripIndent`
          Corrupted download

          Expected downloaded file to have checksum: ${expectedChecksum}
          Computed checksum: ${checksum}

          Expected downloaded file to have size: ${expectedSize}
          Computed size: ${filesize}
        `;
      debug(text);
      throw new Error(text);
    });
  }
  if (expectedChecksum) {
    debug('only checking expected file checksum %d', expectedChecksum);
    return util.getFileChecksum(filename).then(checksum => {
      if (checksum === expectedChecksum) {
        debug('downloaded file has the expected checksum ✅');
        return;
      }
      debug('raising error: file checksum mismatch');
      const text = stripIndent`
        Corrupted download

        Expected downloaded file to have checksum: ${expectedChecksum}
        Computed checksum: ${checksum}
      `;
      throw new Error(text);
    });
  }
  if (expectedSize) {
    // maybe we don't have a checksum, but at least CDN returns content length
    // which we can check against the file size
    debug('only checking expected file size %d', expectedSize);
    return util.getFileSize(filename).then(filesize => {
      if (filesize === expectedSize) {
        debug('downloaded file has the expected size ✅');
        return;
      }
      debug('raising error: file size mismatch');
      const text = stripIndent`
          Corrupted download

          Expected downloaded file to have size: ${expectedSize}
          Computed size: ${filesize}
        `;
      throw new Error(text);
    });
  }
  debug('downloaded file lacks checksum or size to verify');
  return Promise.resolve();
};

// downloads from given url
// return an object with
// {filename: ..., downloaded: true}
const downloadFromUrl = ({
  url,
  downloadDestination,
  progress,
  ca,
  version,
  redirectTTL = defaultMaxRedirects
}) => {
  if (redirectTTL <= 0) {
    return Promise.reject(new Error(stripIndent`
          Failed downloading the Cypress binary.
          There were too many redirects. The default allowance is ${defaultMaxRedirects}.
          Maybe you got stuck in a redirect loop?
        `));
  }
  return new Promise((resolve, reject) => {
    const proxy = getProxyForUrlWithNpmConfig(url);
    debug('Downloading package', {
      url,
      proxy,
      downloadDestination
    });
    if (ca) {
      debug('using custom CA details from npm config');
    }
    const reqOptions = {
      uri: url,
      ...(proxy ? {
        proxy
      } : {}),
      ...(ca ? {
        agentOptions: {
          ca
        }
      } : {}),
      method: 'GET',
      followRedirect: false
    };
    const req = request(reqOptions);

    // closure
    let started = null;
    let expectedSize;
    let expectedChecksum;
    requestProgress(req, {
      throttle: progress.throttle
    }).on('response', response => {
      // we have computed checksum and filesize during test runner binary build
      // and have set it on the S3 object as user meta data, available via
      // these custom headers "x-amz-meta-..."
      // see https://github.com/cypress-io/cypress/pull/4092
      expectedSize = response.headers['x-amz-meta-size'] || response.headers['content-length'];
      expectedChecksum = response.headers['x-amz-meta-checksum'];
      if (expectedChecksum) {
        debug('expected checksum %s', expectedChecksum);
      }
      if (expectedSize) {
        // convert from string (all Amazon custom headers are strings)
        expectedSize = Number(expectedSize);
        debug('expected file size %d', expectedSize);
      }

      // start counting now once we've gotten
      // response headers
      started = new Date();
      if (/^3/.test(response.statusCode)) {
        const redirectVersion = response.headers['x-version'];
        const redirectUrl = response.headers.location;
        debug('redirect version:', redirectVersion);
        debug('redirect url:', redirectUrl);
        downloadFromUrl({
          url: redirectUrl,
          progress,
          ca,
          downloadDestination,
          version: redirectVersion,
          redirectTTL: redirectTTL - 1
        }).then(resolve).catch(reject);

        // if our status code does not start with 200
      } else if (!/^2/.test(response.statusCode)) {
        debug('response code %d', response.statusCode);
        const err = new Error(stripIndent`
          Failed downloading the Cypress binary.
          Response code: ${response.statusCode}
          Response message: ${response.statusMessage}
        `);
        reject(err);
        // status codes here are all 2xx
      } else {
        // We only enable this pipe connection when we know we've got a successful return
        // and handle the completion with verify and resolve
        // there was a possible race condition between end of request and close of writeStream
        // that is made ordered with this Promise.all
        Promise.all([new Promise(r => {
          return response.pipe(fs.createWriteStream(downloadDestination).on('close', r));
        }), new Promise(r => response.on('end', r))]).then(() => {
          debug('downloading finished');
          verifyDownloadedFile(downloadDestination, expectedSize, expectedChecksum).then(() => debug('verified')).then(() => resolve(version)).catch(reject);
        });
      }
    }).on('error', e => {
      if (e.code === 'ECONNRESET') return; // sometimes proxies give ECONNRESET but we don't care

      reject(e);
    }).on('progress', state => {
      // total time we've elapsed
      // starting on our first progress notification
      const elapsed = new Date() - started;

      // request-progress sends a value between 0 and 1
      const percentage = util.convertPercentToPercentage(state.percent);
      const eta = util.calculateEta(percentage, elapsed);

      // send up our percent and seconds remaining
      progress.onProgress(percentage, util.secsRemaining(eta));
    });
  });
};

/**
 * Download Cypress.zip from external versionUrl to local file.
 * @param [string] version Could be "3.3.0" or full URL
 * @param [string] downloadDestination Local filename to save as
 */
const start = async opts => {
  let {
    version,
    downloadDestination,
    progress,
    redirectTTL
  } = opts;
  if (!downloadDestination) {
    la(is.unemptyString(downloadDestination), 'missing download dir', opts);
  }
  if (!progress) {
    progress = {
      onProgress: () => {
        return {};
      }
    };
  }
  const arch = await util.getRealArch();
  const versionUrl = getUrl(arch, version);
  progress.throttle = 100;
  debug('needed Cypress version: %s', version);
  debug('source url %s', versionUrl);
  debug(`downloading cypress.zip to "${downloadDestination}"`);

  // ensure download dir exists
  return fs.ensureDirAsync(path.dirname(downloadDestination)).then(() => {
    return getCA();
  }).then(ca => {
    return downloadFromUrl({
      url: versionUrl,
      downloadDestination,
      progress,
      ca,
      version,
      ...(redirectTTL ? {
        redirectTTL
      } : {})
    });
  }).catch(err => {
    return prettyDownloadErr(err, versionUrl);
  });
};
module.exports = {
  start,
  getUrl,
  getProxyForUrlWithNpmConfig,
  getCA
};