-
Notifications
You must be signed in to change notification settings - Fork 61
/
local.js
119 lines (96 loc) · 3.15 KB
/
local.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
var Log = require('./logger'),
logger = new Log(global.logLevel),
exec = require('child_process').execFile,
fs = require('fs'),
http = require('http'),
windows = ((process.platform.match(/win32/) || process.platform.match(/win64/)) !== null),
localBinary = __dirname + '/BrowserStackLocal' + (windows ? '.exe' : ''),
utils = require('./utils'),
config = require('./config');
var Tunnel = function Tunnel(key, port, uniqueIdentifier, callback) {
var that = {};
function tunnelLauncher() {
var tunnelOptions = getTunnelOptions(key, uniqueIdentifier);
if (typeof callback !== 'function') {
callback = function(){};
}
logger.debug('[%s] Launching tunnel', new Date());
var subProcess = exec(localBinary, tunnelOptions, function(error, stdout, stderr) {
logger.debug(stderr);
logger.debug(error);
if (stdout.indexOf('Error') >= 0 || error) {
logger.debug('[%s] Tunnel launching failed', new Date());
logger.debug(stdout);
process.exit('SIGINT');
}
});
var data = '';
var running = false;
var runMatcher = 'You can now access your local server(s)';
setTimeout(function() {
if (!running) {
utils.alertBrowserStack('Tunnel launch timeout', 'Stdout:\n' + data);
}
}, 30 * 1000);
subProcess.stdout.on('data', function(_data) {
if (running) {
return;
}
data += _data;
if (data.indexOf(runMatcher) >= 0) {
running = true;
logger.debug('[%s] Tunnel launched', new Date());
setTimeout(function(){
callback();
}, 2000);
}
});
that.process = subProcess;
}
function getTunnelOptions(key, uniqueIdentifier) {
var options = [key];
if (config.debug) {
options.push('-v');
}
if (!uniqueIdentifier) {
options.push('-force');
options.push('-onlyAutomate');
} else {
options.push('-localIdentifier');
options.push(uniqueIdentifier);
}
var proxy = config.proxy;
if (proxy) {
options.push('-proxyHost ' + proxy.host);
options.push('-proxyPort ' + proxy.port);
if (proxy.username && proxy.password) {
options.push('-proxyUser ' + proxy.username);
options.push('-proxyPass ' + proxy.password);
}
}
return options;
}
fs.exists(localBinary, function(exists) {
if (exists) {
tunnelLauncher();
return;
}
logger.debug('Downloading BrowserStack Local to "%s"', localBinary);
var file = fs.createWriteStream(localBinary);
http.get((windows ? 'http://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal.exe' : ('http://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-' + process.platform + '-' + process.arch)),
function(response) {
response.pipe(file);
response.on('end', function() {
fs.chmodSync(localBinary, 0700);
setTimeout(function() {
tunnelLauncher();
}, 100);
}).on('error', function(e) {
logger.info('Got error while downloading binary: ' + e.message);
process.exit('SIGINT');
});
});
});
return that;
};
exports.Tunnel = Tunnel;