Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit 6906c93

Browse files
committed
feat(webdriver-manager): use proxy for webdriver-manager
1 parent 7d90880 commit 6906c93

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

bin/webdriver-manager

+28-19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
var fs = require('fs');
44
var os = require('os');
5+
var url = require('url');
56
var request = require('request');
67
var http = require('http');
78
var path = require('path');
@@ -76,7 +77,6 @@ var binaries = {
7677
}
7778
};
7879

79-
8080
var cli = optimist.
8181
usage('Usage: webdriver-manager <command>\n' +
8282
'Commands:\n' +
@@ -85,7 +85,8 @@ var cli = optimist.
8585
' status: list the current available drivers').
8686
describe('out_dir', 'Location to output/expect ').
8787
default('out_dir', SELENIUM_DIR).
88-
describe('seleniumPort', 'Optional port for the selenium standalone server');
88+
describe('seleniumPort', 'Optional port for the selenium standalone server').
89+
describe('proxy', 'proxy to use for the install or update command');
8990

9091
for (bin in binaries) {
9192
cli.describe(bin, 'install or update ' + binaries[bin].name).
@@ -105,6 +106,17 @@ if (!fs.existsSync(argv.out_dir) || !fs.statSync(argv.out_dir).isDirectory()) {
105106
fs.mkdirSync(argv.out_dir);
106107
}
107108

109+
var resolveProxy = function(fileUrl) {
110+
var protocol = url.parse(fileUrl).protocol;
111+
if (argv.proxy) {
112+
return argv.proxy;
113+
} else if (protocol === 'https:') {
114+
return process.env.HTTPS_PROXY || process.env.HTTP_PROXY;
115+
} else if (protocol === 'http:') {
116+
return process.env.HTTP_PROXY;
117+
}
118+
};
119+
108120
/**
109121
* Function to download file using HTTP.get.
110122
* TODO: look into using something instead of request here, to avoid the
@@ -114,23 +126,20 @@ var httpGetFile = function(fileUrl, fileName, outputDir, callback) {
114126
console.log('downloading ' + fileUrl + '...');
115127
var filePath = path.join(outputDir, fileName);
116128
var file = fs.createWriteStream(filePath);
117-
var req = request(fileUrl);
118-
req.on('response', function(res) {
119-
if (res.statusCode !== 200) {
120-
throw new Error('Got code ' + res.statusCode + ' from ' + fileUrl);
121-
}
122-
});
123-
req.on('data', function(data) {
124-
file.write(data);
125-
});
126-
req.on('end', function() {
127-
file.end(function() {
128-
console.log(fileName + ' downloaded to ' + filePath);
129-
if (callback) {
130-
callback(filePath);
131-
}
132-
});
133-
});
129+
130+
var options = {
131+
url: fileUrl,
132+
proxy: resolveProxy(fileUrl)
133+
}
134+
request(options, function (error, response) {
135+
if (response.statusCode !== 200) {
136+
throw new Error('Got code ' + response.statusCode + ' from ' + fileUrl);
137+
}
138+
console.log(fileName + ' downloaded to ' + filePath);
139+
if (callback) {
140+
callback(filePath);
141+
}
142+
}).pipe(file);
134143
};
135144

136145
/**

0 commit comments

Comments
 (0)