-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathLocalBinary.js
257 lines (224 loc) · 7.78 KB
/
LocalBinary.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
var https = require('https'),
url = require('url'),
fs = require('fs'),
path = require('path'),
os = require('os'),
childProcess = require('child_process'),
zlib = require('zlib'),
HttpsProxyAgent = require('https-proxy-agent'),
version = require('../package.json').version,
LocalError = require('./LocalError');
const packageName = 'browserstack-local-nodejs';
function LocalBinary(){
this.hostOS = process.platform;
this.is64bits = process.arch == 'x64';
this.getDownloadPath = function () {
let sourceURL = 'https://www.browserstack.com/local-testing/downloads/binaries/';
if(this.hostOS.match(/darwin|mac os/i)){
return sourceURL + 'BrowserStackLocal-darwin-x64';
} else if(this.hostOS.match(/mswin|msys|mingw|cygwin|bccwin|wince|emc|win32/i)) {
this.windows = true;
return sourceURL + 'BrowserStackLocal.exe';
} else {
if(this.is64bits) {
if(this.isAlpine())
return sourceURL + 'BrowserStackLocal-alpine';
else
return sourceURL + 'BrowserStackLocal-linux-x64';
} else {
return sourceURL + 'BrowserStackLocal-linux-ia32';
}
}
};
this.isAlpine = function() {
try {
return childProcess.execSync('grep -w "NAME" /etc/os-release').includes('Alpine');
} catch(e) {
return false;
}
};
this.httpPath = this.getDownloadPath();
this.retryBinaryDownload = function(conf, destParentDir, callback, retries, binaryPath) {
var that = this;
if(retries > 0) {
console.log('Retrying Download. Retries left', retries);
fs.stat(binaryPath, function(err) {
if(err == null) {
fs.unlinkSync(binaryPath);
}
if(!callback) {
return that.downloadSync(conf, destParentDir, retries - 1);
}
that.download(conf, destParentDir, callback, retries - 1);
});
} else {
console.error('Number of retries to download exceeded.');
}
};
this.downloadSync = function(conf, destParentDir, retries) {
console.log('Downloading in sync');
var that = this;
if(!this.checkPath(destParentDir))
fs.mkdirSync(destParentDir);
var destBinaryName = (this.windows) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal';
var binaryPath = path.join(destParentDir, destBinaryName);
let cmd, opts;
cmd = 'node';
opts = [path.join(__dirname, 'download.js'), binaryPath, this.httpPath];
if(conf.proxyHost && conf.proxyPort) {
opts.push(conf.proxyHost, conf.proxyPort);
if (conf.useCaCertificate) {
opts.push(conf.useCaCertificate);
}
} else if (conf.useCaCertificate) {
opts.push(undefined, undefined, conf.useCaCertificate);
}
try{
const userAgent = [packageName, version].join('/');
const env = Object.assign({ 'USER_AGENT': userAgent }, process.env);
const obj = childProcess.spawnSync(cmd, opts, { env: env });
let output;
if(obj.stdout.length > 0) {
if(fs.existsSync(binaryPath)){
fs.chmodSync(binaryPath, '0755');
return binaryPath;
}else{
console.log('failed to download');
return that.retryBinaryDownload(conf, destParentDir, null, retries, binaryPath);
}
} else if(obj.stderr.length > 0) {
output = Buffer.from(JSON.parse(JSON.stringify(obj.stderr)).data).toString();
console.error(output);
return that.retryBinaryDownload(conf, destParentDir, null, retries, binaryPath);
}
} catch(err) {
console.error('Download failed with error', err);
return that.retryBinaryDownload(conf, destParentDir, null, retries, binaryPath);
}
};
this.download = function(conf, destParentDir, callback, retries){
var that = this;
if(!this.checkPath(destParentDir))
fs.mkdirSync(destParentDir);
var destBinaryName = (this.windows) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal';
var binaryPath = path.join(destParentDir, destBinaryName);
var fileStream = fs.createWriteStream(binaryPath);
var options = url.parse(this.httpPath);
if(conf.proxyHost && conf.proxyPort) {
options.agent = new HttpsProxyAgent({
host: conf.proxyHost,
port: conf.proxyPort
});
}
if (conf.useCaCertificate) {
try {
options.ca = fs.readFileSync(conf.useCaCertificate);
} catch(err) {
console.log('failed to read cert file', err);
}
}
options.headers = Object.assign({}, options.headers, {
'accept-encoding': 'gzip, *',
'user-agent': [packageName, version].join('/'),
});
https.get(options, function (response) {
const contentEncoding = response.headers['content-encoding'];
if (typeof contentEncoding === 'string' && contentEncoding.match(/gzip/i)) {
if (process.env.BROWSERSTACK_LOCAL_DEBUG_GZIP) {
console.info('Using gzip in ' + options.headers['user-agent']);
}
response.pipe(zlib.createGunzip()).pipe(fileStream);
} else {
response.pipe(fileStream);
}
response.on('error', function(err) {
console.error('Got Error in binary download response', err);
that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath);
});
fileStream.on('error', function (err) {
console.error('Got Error while downloading binary file', err);
that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath);
});
fileStream.on('close', function () {
fs.chmod(binaryPath, '0755', function() {
callback(binaryPath);
});
});
}).on('error', function(err) {
console.error('Got Error in binary downloading request', err);
that.retryBinaryDownload(conf, destParentDir, callback, retries, binaryPath);
});
};
this.binaryPath = function(conf, callback){
var destParentDir = this.getAvailableDirs();
var destBinaryName = (this.windows) ? 'BrowserStackLocal.exe' : 'BrowserStackLocal';
var binaryPath = path.join(destParentDir, destBinaryName);
if(this.checkPath(binaryPath, fs.X_OK)){
if(!callback) {
return binaryPath;
}
callback(binaryPath);
} else {
if(!callback) {
return this.downloadSync(conf, destParentDir, 5);
}
this.download(conf, destParentDir, callback, 5);
}
};
this.checkPath = function(path, mode){
mode = mode || (fs.R_OK | fs.W_OK);
try {
fs.accessSync(path, mode);
return true;
} catch(e){
if(typeof fs.accessSync !== 'undefined') return false;
// node v0.10
try {
fs.statSync(path);
return true;
} catch (e){
return false;
}
}
};
this.getAvailableDirs = function(){
for(var i=0; i < this.orderedPaths.length; i++){
var path = this.orderedPaths[i];
if(this.makePath(path))
return path;
}
throw new LocalError('Error trying to download BrowserStack Local binary');
};
this.makePath = function(path){
try {
if(!this.checkPath(path)){
fs.mkdirSync(path);
}
return true;
} catch(e){
return false;
}
};
this.homedir = function() {
if(typeof os.homedir === 'function') return os.homedir();
var env = process.env;
var home = env.HOME;
var user = env.LOGNAME || env.USER || env.LNAME || env.USERNAME;
if (process.platform === 'win32') {
return env.USERPROFILE || env.HOMEDRIVE + env.HOMEPATH || home || null;
}
if (process.platform === 'darwin') {
return home || (user ? '/Users/' + user : null);
}
if (process.platform === 'linux') {
return home || (process.getuid() === 0 ? '/root' : (user ? '/home/' + user : null));
}
return home || null;
};
this.orderedPaths = [
path.join(this.homedir(), '.browserstack'),
process.cwd(),
os.tmpdir()
];
}
module.exports = LocalBinary;