Skip to content

Commit

Permalink
Merge pull request #10 from browserstack/daemon_mode
Browse files Browse the repository at this point in the history
added daemon mode, bumped up version
  • Loading branch information
nidhimj22 committed May 10, 2016
2 parents 1108f74 + 211d80b commit eea92ef
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 47 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ bin/
node_modules/
npm-debug.log
local.log
browserstack.err
99 changes: 60 additions & 39 deletions lib/Local.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var childProcess = require('child_process'),
fs = require('fs'),
path = require('path'),
running = require('is-running'),
LocalBinary = require('./LocalBinary'),
Expand All @@ -10,6 +9,7 @@ function Local(){
this.pid = undefined;
this.key = process.env.BROWSERSTACK_ACCESS_KEY;
this.logfile = path.join(process.cwd(), 'local.log');
this.opcode = 'start';
this.exitCallback;
this.userArgs = [];

Expand All @@ -27,54 +27,75 @@ function Local(){
that.binaryPath = binaryPath;
childProcess.exec('echo "" > ' + that.logfile);

that.tunnel = childProcess.spawn(binaryPath, that.getBinaryArgs());
that.tunnel.on('exit', function(){
that.tunnel = undefined;
if(that.exitCallback) that.exitCallback();
});

that.stdout = fs.openSync(that.logfile, 'r');
var chunkSize = 512,
buffer = new Buffer(81920),
bytesRead = 0,
error = undefined;

while(true){
var bytes = fs.readSync(that.stdout, buffer, bytesRead, chunkSize, bytesRead);
if(bytes == 0) continue;

var buffRead = buffer.slice(bytesRead, bytesRead+bytes);
bytesRead += bytes;
that.opcode = 'start';
that.tunnel = childProcess.execFile(that.binaryPath, that.getBinaryArgs(), function(error, stdout, stderr){
if(error) callback(new LocalError(error.toString()));

var data = buffRead.toString();
var data = {};
if(stdout)
data = JSON.parse(stdout);
else if(stderr)
data = JSON.parse(stderr);
else
callback(new LocalError('No output received'));

if(data.match(that.errorRegex)){
fs.closeSync(that.stdout);
error = data.match(that.errorRegex)[0].trim();
break;
}

if(data.match(that.doneRegex)){
fs.closeSync(that.stdout);
break;
if(data['state'] != 'connected'){
callback(new LocalError(data['message']));
} else {
that.pid = data['pid'];
callback();
}
}
});

if(error) throw new LocalError(error);
callback();
// that.tunnel = childProcess.spawn(binaryPath, that.getBinaryArgs());
// that.tunnel.on('exit', function(){
// that.tunnel = undefined;
// if(that.exitCallback) that.exitCallback();
// });

// that.stdout = fs.openSync(that.logfile, 'r');
// var chunkSize = 512,
// buffer = new Buffer(81920),
// bytesRead = 0,
// error = undefined;

// while(true){
// var bytes = fs.readSync(that.stdout, buffer, bytesRead, chunkSize, bytesRead);
// if(bytes == 0) continue;

// var buffRead = buffer.slice(bytesRead, bytesRead+bytes);
// bytesRead += bytes;

// var data = buffRead.toString();

// if(data.match(that.errorRegex)){
// fs.closeSync(that.stdout);
// error = data.match(that.errorRegex)[0].trim();
// break;
// }

// if(data.match(that.doneRegex)){
// fs.closeSync(that.stdout);
// break;
// }
// }

// if(error) throw new LocalError(error);
// callback();
});
};

this.isRunning = function(){
return this.tunnel && running(this.tunnel.pid);
return this.pid && running(this.pid);
};

this.stop = function (callback) {
if (this.tunnel) {
if(callback) this.exitCallback = callback;
this.tunnel.kill();
}
else if(callback) callback();
if(!this.pid) return callback();
this.opcode = 'stop';
this.tunnel = childProcess.execFile(this.binaryPath, this.getBinaryArgs(), function(error){
if(error) callback(new LocalError(error.toString()));
callback();
});
};

this.addArgs = function(options){
Expand Down Expand Up @@ -185,7 +206,7 @@ function Local(){
};

this.getBinaryArgs = function(){
var args = ['-logFile', this.logfile];
var args = ['-d', this.opcode, '-logFile', this.logfile];
if(this.folderFlag)
args.push(this.folderFlag);
args.push(this.key);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "browserstack-local",
"version": "0.1.0",
"version": "0.2.0",
"description": "Nodejs bindings for BrowserStack Local",
"engine": "^0.10.44",
"main": "index.js",
Expand Down
12 changes: 5 additions & 7 deletions test/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,15 @@ describe('Local', function () {

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

bsLocal_2.start({ 'key': process.env.BROWSERSTACK_ACCESS_KEY, 'logfile': tempLogPath }, function(error){
expect(error.toString().trim()).to.equal('LocalError: Either another browserstack local client is running on your machine or some server is listening on port 45691');
fs.unlinkSync(tempLogPath);
done();
}
});
});
});

Expand Down

0 comments on commit eea92ef

Please sign in to comment.