-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Use proxy when opening Android browsers (closes #25) * Avoid simultaneous API requests
- Loading branch information
1 parent
aaad6ed
commit 5b72e49
Showing
5 changed files
with
166 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import Promise from 'pinkie'; | ||
import request from 'request-promise'; | ||
import delay from './utils/delay'; | ||
|
||
|
||
const BUILD_ID = process.env['BROWSERSTACK_BUILD_ID']; | ||
const PROJECT_NAME = process.env['BROWSERSTACK_PROJECT_NAME']; | ||
|
||
const AUTH_FAILED_ERROR = 'Authentication failed. Please assign the correct username and access key ' + | ||
'to the BROWSERSTACK_USERNAME and BROWSERSTACK_ACCESS_KEY environment variables.'; | ||
|
||
const API_REQUEST_DELAY = 100; | ||
|
||
let apiRequestPromise = Promise.resolve(null); | ||
|
||
|
||
export default function (apiPath, params) { | ||
if (!process.env['BROWSERSTACK_USERNAME'] || !process.env['BROWSERSTACK_ACCESS_KEY']) | ||
throw new Error(AUTH_FAILED_ERROR); | ||
|
||
var url = apiPath.url; | ||
|
||
var opts = { | ||
auth: { | ||
user: process.env['BROWSERSTACK_USERNAME'], | ||
pass: process.env['BROWSERSTACK_ACCESS_KEY'], | ||
}, | ||
|
||
qs: Object.assign({}, | ||
BUILD_ID && { build: BUILD_ID }, | ||
PROJECT_NAME && { project: PROJECT_NAME }, | ||
params | ||
), | ||
|
||
method: apiPath.method || 'GET', | ||
json: !apiPath.binaryStream | ||
}; | ||
|
||
if (apiPath.binaryStream) | ||
opts.encoding = null; | ||
|
||
const currentRequestPromise = apiRequestPromise | ||
.then(() => request(url, opts)) | ||
.catch(error => { | ||
if (error.statusCode === 401) | ||
throw new Error(AUTH_FAILED_ERROR); | ||
|
||
throw error; | ||
}); | ||
|
||
apiRequestPromise = currentRequestPromise.then(() => delay(API_REQUEST_DELAY)); | ||
|
||
return currentRequestPromise; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import http from 'http'; | ||
import { parse as parseUrl } from 'url'; | ||
import Promise from 'pinkie'; | ||
|
||
|
||
module.exports = class BrowserProxy { | ||
constructor (targetHost, targetPort, { proxyPort, responseDelay } = {}) { | ||
this.targetHost = targetHost; | ||
this.targetPort = targetPort; | ||
this.proxyPort = proxyPort; | ||
this.responseDelay = responseDelay || 0; | ||
|
||
this.server = http.createServer((...args) => this._onBrowserRequest(...args)); | ||
|
||
this.server.on('connection', socket => socket.unref()); | ||
} | ||
|
||
_onBrowserRequest (req, res) { | ||
setTimeout(() => { | ||
const parsedRequestUrl = parseUrl(req.url); | ||
const destinationUrl = 'http://' + this.targetHost + ':' + this.targetPort + parsedRequestUrl.path; | ||
|
||
res.statusCode = 302; | ||
|
||
res.setHeader('location', destinationUrl); | ||
res.end(); | ||
}, this.responseDelay); | ||
} | ||
|
||
async init () { | ||
return new Promise((resolve, reject) => { | ||
this.server.listen(this.proxyPort, err => { | ||
if (err) | ||
reject(err); | ||
else { | ||
this.proxyPort = this.server.address().port; | ||
|
||
resolve(); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
dispose () { | ||
this.server.close(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import Promise from 'pinkie'; | ||
|
||
|
||
export default function (ms) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
} |