gx
chenyc
2025-06-12 7b72ac13a83764a662159d4a49b7fffb90476ecb
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
/**
 * @license
 * Copyright 2019 Google LLC. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * =============================================================================
 */
 
const fs = require('fs');
const http = require('http');
const https = require('https');
const HttpsProxyAgent = require('https-proxy-agent');
const path = require('os').platform() === 'win32' ? require('path') :
                                                    require('path').win32;
const ProgressBar = require('progress');
const tar = require('tar');
const url = require('url');
const util = require('util');
const zip = require('adm-zip');
 
const unlink = util.promisify(fs.unlink);
 
/**
 * Downloads and unpacks a given tarball or zip file at a given path.
 * @param {string} uri The path of the compressed file to download and extract.
 * @param {string} destPath The destination path for the compressed content.
 * @param {Function} callback Handler for when downloading and extraction is
 *     complete.
 */
async function downloadAndUnpackResource(uri, destPath, callback) {
  const httpClient = uri.startsWith('https') ? https : http;
 
  // If HTTPS_PROXY, https_proxy, HTTP_PROXY, or http_proxy is set
  const proxy = process.env['HTTPS_PROXY'] || process.env['https_proxy'] ||
      process.env['HTTP_PROXY'] || process.env['http_proxy'] || '';
 
  // Using object destructuring to construct the options object for the
  // http request.  the '...url.parse(targetUri)' part fills in the host,
  // path, protocol, etc from the targetUri and then we set the agent to the
  // default agent which is overridden a few lines down if there is a proxy
  const options = {...url.parse(uri), agent: httpClient.globalAgent};
 
  if (proxy !== '') {
    options.agent = new HttpsProxyAgent(proxy);
  }
 
  const request = httpClient.get(options, response => {
    const bar = new ProgressBar('[:bar] :rate/bps :percent :etas', {
      complete: '=',
      incomplete: ' ',
      width: 30,
      total: parseInt(response.headers['content-length'], 10)
    });
 
    if (uri.endsWith('.zip')) {
      // Save zip file to disk, extract, and delete the downloaded zip file.
      const tempFileName = path.join(__dirname, '_tmp.zip');
      const outputFile = fs.createWriteStream(tempFileName);
 
      response.on('data', chunk => bar.tick(chunk.length))
          .pipe(outputFile)
          .on('close', async () => {
            const zipFile = new zip(tempFileName);
            zipFile.extractAllTo(destPath, true /* overwrite */);
 
            await unlink(tempFileName);
 
            if (callback !== undefined) {
              callback();
            }
          });
    } else if (uri.endsWith('.tar.gz')) {
      response.on('data', chunk => bar.tick(chunk.length))
          .pipe(tar.x({C: destPath, strict: true}))
          .on('close', () => {
            if (callback !== undefined) {
              callback();
            }
          });
    } else {
      throw new Error(`Unsupported packed resource: ${uri}`);
    }
  });
  request.end();
}
 
module.exports = {downloadAndUnpackResource};