Skip to content

Add Local.startSync method #128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions lib/Local.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,52 @@ function Local(){
this.errorRegex = /\*\*\* Error: [^\r\n]*/i;
this.doneRegex = /Press Ctrl-C to exit/i;

this.startSync = function(options) {
this.userArgs = [];
var that = this;
this.addArgs(options);

if(typeof options['onlyCommand'] !== 'undefined')
return;

const binaryPath = this.getBinaryPath();
that.binaryPath = binaryPath;
childProcess.exec('echo "" > ' + that.logfile);
that.opcode = 'start';
if(!this.binaryPath){
return new LocalError('Couldn\'t find binary file');
}
try{
const obj = childProcess.spawnSync(that.binaryPath, that.getBinaryArgs());
this.tunnel = {pid: obj.pid};
var data = {};
if(obj.stdout.length > 0)
data = JSON.parse(obj.stdout);
else if(obj.stderr.length > 0)
data = JSON.parse(obj.stderr);
else
return new LocalError('No output received');
if(data['state'] != 'connected'){
return new LocalError(data['message']['message']);
} else {
that.pid = data['pid'];
that.isProcessRunning = true;
return;
}
}catch(error){
console.error('Error while trying to execute binary', error);
if(that.retriesLeft > 0) {
console.log('Retrying Binary Download. Retries Left', that.retriesLeft);
that.retriesLeft -= 1;
fs.unlinkSync(that.binaryPath);
delete(that.binaryPath);
return that.startSync(options);
} else {
return new LocalError(error.toString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@07souravkunda This should throw an error instead of returning it ~
@francisf

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ack

}
}
};

this.start = function(options, callback){
this.userArgs = [];
var that = this;
Expand Down Expand Up @@ -199,9 +245,15 @@ function Local(){
conf.proxyHost = this.proxyHost;
conf.proxyPort = this.proxyPort;
}
if(!callback) {
return this.binary.binaryPath(conf);
}
this.binary.binaryPath(conf, callback);
} else {
console.log('BINARY PATH IS DEFINED');
if(!callback) {
return this.binaryPath;
}
callback(this.binaryPath);
}
};
Expand Down
49 changes: 49 additions & 0 deletions lib/LocalBinary.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ function LocalBinary(){

this.httpPath = this.getDownloadPath();



this.retryBinaryDownload = function(conf, destParentDir, callback, retries, binaryPath) {
var that = this;
if(retries > 0) {
Expand All @@ -47,13 +49,54 @@ function LocalBinary(){
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);
}

try{
const obj = childProcess.spawnSync(cmd, opts);
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))
Expand Down Expand Up @@ -97,8 +140,14 @@ function LocalBinary(){
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);
}
};
Expand Down
31 changes: 31 additions & 0 deletions lib/download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const https = require('https'),
fs = require('fs'),
HttpsProxyAgent = require('https-proxy-agent'),
url = require('url');

const binaryPath = process.argv[2],httpPath = process.argv[3], proxyHost = process.argv[4], proxyPort = process.argv[5];

var fileStream = fs.createWriteStream(binaryPath);

var options = url.parse(httpPath);
if(proxyHost && proxyPort) {
options.agent = new HttpsProxyAgent({
host: proxyHost,
port: proxyPort
});
}

https.get(options, function (response) {
response.pipe(fileStream);
response.on('error', function(err) {
console.error('Got Error in binary download response', err);
});
fileStream.on('error', function (err) {
console.error('Got Error while downloading binary file', err);
});
fileStream.on('close', function () {
console.log('Done');
});
}).on('error', function(err) {
console.error('Got Error in binary downloading request', err);
});
43 changes: 42 additions & 1 deletion test/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,43 @@ describe('Local', function () {
this.timeout(60000);
bsLocal.stop(done);
});

});

describe('Start sync', () => {
var bsLocal;
beforeEach(function () {
bsLocal = new browserstack.Local();
});

it('should have pid when running', function () {
this.timeout(60000);
bsLocal.startSync({ 'key': process.env.BROWSERSTACK_ACCESS_KEY});
expect(bsLocal.tunnel.pid).to.not.equal(0);
});

it('should return is running properly', function () {
this.timeout(60000);
expect(bsLocal.isRunning()).to.not.equal(true);
bsLocal.startSync({ 'key': process.env.BROWSERSTACK_ACCESS_KEY});
expect(bsLocal.isRunning()).to.equal(true);
});

it('should throw error on running multiple binary', function () {
this.timeout(60000);
bsLocal.startSync({ 'key': process.env.BROWSERSTACK_ACCESS_KEY });
bsLocal_2 = new browserstack.Local();
var tempLogPath = path.join(process.cwd(), 'log2.log');
const error = bsLocal_2.startSync({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, 'logfile': tempLogPath });
expect(error.toString().trim()).to.equal('LocalError: Either another browserstack local client is running on your machine or some server is listening on port 45690');
fs.unlinkSync(tempLogPath);
});

afterEach(function (done) {
this.timeout(60000);
bsLocal.stop(done);
});
})

describe('LocalBinary', function () {
describe('Retries', function() {
var unlinkTmp,
Expand Down Expand Up @@ -414,5 +448,12 @@ describe('LocalBinary', function () {
done();
});
});

it('should download binaries in sync', function () {
this.timeout(MAX_TIMEOUT);
var conf = {};
const result = binary.downloadSync(conf, tempDownloadPath);
expect(fs.existsSync(result)).to.equal(true);
});
});
});