Skip to content

Commit

Permalink
feat: conditional debug
Browse files Browse the repository at this point in the history
  • Loading branch information
Danil Kolesnikov committed Dec 14, 2019
1 parent d911b1c commit ba18ba1
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,7 @@ var Tunnel = inherit(EventEmitter, {
debug('running ssh: ssh', cmd.join(' '));

this._tunnel = childProcess.spawn('ssh', cmd);

var cleanup = function () {
_this._tunnel.stderr.removeAllListeners('data');
};

var onData = function (data) {
if (_this._activityWatcher) {
debug('data:', data.toString());
_this._activityWatcher.update();
}

if (/success/.test(data)) {
if (!_this._activityWatcher) {
cleanup();
}

return _this._resolveTunnel();
}

if (!_this.connected && /failed/.test(data)) {
return _this._rejectTunnel();
}
};

this._tunnel.stderr.on('data', onData);
this._tunnel.stderr.on('data', this._onData.bind(this));

this._tunnel.on('exit', function (code, signal) {
_this.emit('exit', code, signal);
Expand Down Expand Up @@ -127,6 +103,28 @@ var Tunnel = inherit(EventEmitter, {
});
},

_onData: function (data) {
if (debug.enabled) {
debug('data:', data.toString());
}

if (this._activityWatcher) {
this._activityWatcher.update();
}

if (/success/.test(data)) {
if (!this._activityWatcher) {
this._tunnel.stderr.removeAllListeners('data');
}

return this._resolveTunnel();
}

if (!this.connected && /failed/.test(data)) {
return this._rejectTunnel();
}
},

_resolveTunnel: function () {
console.info('INFO: Tunnel created to %s', this.proxyHost);

Expand Down

0 comments on commit ba18ba1

Please sign in to comment.