This repository was archived by the owner on May 27, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathlocal.js
More file actions
118 lines (102 loc) · 3.37 KB
/
Copy pathlocal.js
File metadata and controls
118 lines (102 loc) · 3.37 KB
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
var Log = require('./logger'),
logger = new Log(global.logLevel),
exec = require('child_process').exec,
fs = require('fs'),
http = require('http'),
windows = ((process.platform.match(/win32/) || process.platform.match(/win64/)) !== null),
localBinary = __dirname + (windows ? '/BrowserStackTunnel.exe' : '/BrowserStackLocal'),
utils = require('./utils'),
config = require('./config');
var Tunnel = function Tunnel(key, port, uniqueIdentifier, callback, err) {
var that = {};
function tunnelLauncher() {
var tunnelCommand = localBinary + ' ';
if (config.debug) tunnelCommand += ' -v ';
tunnelCommand += key + ' ';
tunnelCommand += 'localhost' + ',';
tunnelCommand += port.toString() + ',';
tunnelCommand += '0';
tunnelCommand += (typeof uniqueIdentifier === 'undefined') ? ' -force -onlyAutomate' : ' -tunnelIdentifier ' + uniqueIdentifier;
tunnelCommand += checkAndAddProxy();
if (typeof callback !== 'function') {
callback = function(){};
}
logger.debug("[%s] Launching tunnel", new Date());
var subProcess = exec(tunnelCommand, 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.kill(process.pid, '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());
callback();
}
});
that.process = subProcess;
}
function getBinaryAddress() {
binaryURL = "http://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal";
if(windows) {
binaryURL += ".exe";
} else {
binaryURL += "-" + process.platform + "-" + process.arch;
}
return binaryURL;
}
function checkAndAddProxy() {
var proxy = config.proxy;
if(typeof proxy == 'undefined') {
return "";
}
var proxyCommand = "";
proxyCommand += " -proxyHost " + proxy.host;
proxyCommand += " -proxyPort " + proxy.port;
if(typeof proxy.username !== 'undefined'){
proxyCommand += " -proxyUser " + proxy.username;
proxyCommand += " -proxyPass " + proxy.password;
}
return proxyCommand;
}
fs.exists(localBinary, function(exists) {
if (exists) {
tunnelLauncher();
return;
}
logger.debug('Downloading BrowserStack Local to `%s` from `%s`', localBinary, getBinaryAddress());
var file = fs.createWriteStream(localBinary);
var request = http.get(
getBinaryAddress(),
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.kill(process.pid, 'SIGINT');
});
});
});
return that;
};
exports.Tunnel = Tunnel;