Skip to content
This repository has been archived by the owner on Jul 28, 2023. It is now read-only.

Commit

Permalink
feat: establish the ssh tunnel automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtajina committed Aug 7, 2013
1 parent 20f10ec commit 75cf642
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module.exports = function(config) {
### Global options
- `username` your BS username (email), you can also use `BROWSER_STACK_USERNAME` env variable.
- `accessKey` your BS access key (password), you can also use `BROWSER_STACK_ACCESS_KEY` env variable.
- `startTunnel` do you wanna establish the BrowserStack tunnel ? (defaults to `true`)


### Per browser options
Expand Down
70 changes: 61 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,49 @@
var q = require('q');
var api = require('browserstack');
var BrowserStackTunnel = require('browserstacktunnel-wrapper');


var createBrowserStackTunnel = function(logger, config, emitter) {
var log = logger.create('launcher.browserstack');
var bsConfig = config.browserStack;

if (bsConfig.startTunnel === false) {
return q();
}

log.debug('Establishing the tunnel on %s:%s', config.hostname, config.port);

var deferred = q.defer();
var tunnel = new BrowserStackTunnel({
key: process.env.BROWSER_STACK_ACCESS_KEY || bsConfig.accessKey,
hosts: [{
name: config.hostname,
port: config.port,
sslFlag: 0
}]
});

tunnel.start(function(error) {
if (error) {
log.error('Can not establish the tunnel.');
deferred.reject(error);
} else {
log.debug('Tunnel established.')
deferred.resolve();
}
});

emitter.on('exit', function(done) {
log.debug('Shutting down the tunnel.');
tunnel.stop(function(error) {
done();
});
});

return deferred.promise;
};



var createBrowserStackClient = function(/* config.browserStack */ config) {
var env = process.env;
Expand All @@ -20,7 +65,9 @@ var formatError = function(error) {
};


var BrowserStackBrowser = function(id, emitter, /* browserStackClient */ client, args, logger) {
var BrowserStackBrowser = function(id, emitter, args, logger,
/* browserStackTunnel */ tunnel, /* browserStackClient */ client) {

var self = this;
var workerId = null;
var captured = false;
Expand All @@ -40,14 +87,18 @@ var BrowserStackBrowser = function(id, emitter, /* browserStackClient */ client,
url: url + '?id=' + id
};

client.createWorker(settings, function(error, worker) {
if (error) {
log.error('Can not start %s\n %s', browserName, formatError(error));
return emitter.emit('browser_process_failure', self);
}

log.debug('Browser %s started with id %s', browserName, worker.id);
workerId = worker.id;
tunnel.then(function() {
client.createWorker(settings, function(error, worker) {
if (error) {
log.error('Can not start %s\n %s', browserName, formatError(error));
return emitter.emit('browser_process_failure', self);
}

log.debug('Browser %s started with id %s', browserName, worker.id);
workerId = worker.id;
});
}, function() {
emitter.emit('browser_process_failure', self);
});
};

Expand Down Expand Up @@ -77,6 +128,7 @@ var BrowserStackBrowser = function(id, emitter, /* browserStackClient */ client,

// PUBLISH DI MODULE
module.exports = {
'browserStackTunnel': ['factory', createBrowserStackTunnel],
'browserStackClient': ['factory', createBrowserStackClient],
'launcher:BrowserStack': ['type', BrowserStackBrowser]
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
],
"author": "Vojta Jina <vojta.jina@gmail.com>",
"dependencies": {
"browserstack": "~0.2.0"
"browserstack": "~0.2.0",
"browserstacktunnel-wrapper": "~1.0.1",
"q": "~0.9.6"
},
"peerDependencies": {
"karma": ">=0.9"
Expand Down

0 comments on commit 75cf642

Please sign in to comment.