Skip to content

Commit

Permalink
Support 30x redirect download for binaries url
Browse files Browse the repository at this point in the history
PR #89.
  • Loading branch information
fengmk2 authored and giggio committed Feb 13, 2017
1 parent 55869c6 commit 0cc943d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -32,19 +32,19 @@ To use a mirror of the ChromeDriver binaries use npm config property `chromedriv
Default is `http://chromedriver.storage.googleapis.com`.

```shell
npm install chromedriver --chromedriver_cdnurl=http://npm.taobao.org/mirrors/chromedriver
npm install chromedriver --chromedriver_cdnurl=https://npm.taobao.org/mirrors/chromedriver
```

Or add property into your [`.npmrc`](https://docs.npmjs.com/files/npmrc) file.

```
chromedriver_cdnurl=http://npm.taobao.org/mirrors/chromedriver
chromedriver_cdnurl=https://npm.taobao.org/mirrors/chromedriver
```

Another option is to use PATH variable `CHROMEDRIVER_CDNURL`.

```shell
CHROMEDRIVER_CDNURL=http://npm.taobao.org/mirrors/chromedriver npm install chromedriver
CHROMEDRIVER_CDNURL=https://npm.taobao.org/mirrors/chromedriver npm install chromedriver
```

### Custom binaries file
Expand Down
22 changes: 18 additions & 4 deletions install.js
Expand Up @@ -167,8 +167,7 @@ function getRequestOptions(downloadPath) {

function getLatestVersion(requestOptions) {
var deferred = kew.defer();
var protocol = requestOptions.protocol === 'https:' ? https : http;
var client = protocol.get(requestOptions, function (response) {
var client = get(requestOptions, function (response) {
var body = '';
if (response.statusCode === 200) {
response.addListener('data', function (data) {
Expand Down Expand Up @@ -197,8 +196,7 @@ function requestBinary(requestOptions, filePath) {
var notifiedCount = 0;
var outFile = fs.openSync(filePath, 'w');

var protocol = requestOptions.protocol === 'https:' ? https : http;
var client = protocol.get(requestOptions, function (response) {
var client = get(requestOptions, function (response) {
var status = response.statusCode;
console.log('Receiving...');

Expand Down Expand Up @@ -228,6 +226,22 @@ function requestBinary(requestOptions, filePath) {
}


function get(requestOptions, callback, redirects) {
redirects = redirects || 0;
var protocol = requestOptions.protocol === 'https:' ? https : http;
var client = protocol.get(requestOptions, function (response) {
var status = response.statusCode;
if ((status === 302 || status === 301 || status === 307) && redirects < 5) {
console.log('Redirect to %s', response.headers.location);
redirects++;
return get(getRequestOptions(response.headers.location), callback, redirects);
}
callback(response);
});
return client;
}


function extractDownload(filePath, tmpPath) {
var deferred = kew.defer();

Expand Down

0 comments on commit 0cc943d

Please sign in to comment.