From 700e505e0bec69ed122ad67e7d1a6131157d33b0 Mon Sep 17 00:00:00 2001 From: samueleaton Date: Fri, 13 May 2016 18:13:25 -0600 Subject: [PATCH] adds info object to main object --- index.js | 49 +++++++++++++++++++++++++++++++++++-------------- package.json | 2 +- src/index.js | 48 ++++++++++++++++++++++++++++++++++++------------ 3 files changed, 72 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index 8ae77c2..6a20de2 100644 --- a/index.js +++ b/index.js @@ -92,25 +92,40 @@ var AutoSSH = function (_EventEmitter) { }); } + /* + */ + + }, { + key: 'getConnectionInfo', + value: function getConnectionInfo() { + var _this3 = this; + + var infoObj = { + kill: function kill() { + return _this3.kill; + }, + pid: null, + host: this.host || null, + username: this.username || null, + remotePort: parseInt(this.remotePort), + localPort: parseInt(this.localPort), + execString: this.execString || null + }; + + if (this.currentProcess) infoObj.pid = this.currentProcess.pid; + if (!infoObj.localPort) infoObj.localPort = null; + if (!infoObj.remotePort) infoObj.remotePort = null; + + return infoObj; + } + /* fired when connection established */ }, { key: 'emitConnect', value: function emitConnect() { - var _this3 = this; - - this.emit('connect', { - kill: function kill() { - return _this3.kill; - }, - pid: this.currentProcess.pid, - host: this.host, - username: this.username, - remotePort: this.remotePort, - localPort: this.localPort, - execString: this.execString - }); + this.emit('connect', this.getConnectionInfo()); } /* fired when timeout error occurs @@ -334,9 +349,15 @@ module.exports = function (conf) { } }; + Object.defineProperty(autosshInterface, 'info', { + get: function get() { + return autossh.getConnectionInfo(); + } + }); + Object.defineProperty(autosshInterface, 'pid', { get: function get() { - return autossh.currentProcess.pid; + if (autossh.currentProcess) return autossh.currentProcess.pid;else return null; } }); diff --git a/package.json b/package.json index 09d0021..0aec975 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "Persistent SSH tunnels", "main": "index.js", "scripts": { - "lint": "eslint -c .eslintrc.json src/*.js -o .eslint.log && echo '' > .eslint.log ; cat .eslint.log ", + "lint": "eslint -c .eslintrc.json --max-warnings 2 src/*.js -o .eslint.log && echo '' > .eslint.log ; cat .eslint.log ", "pretranspile": "npm run -s lint", "transpile": "babel src -d ." }, diff --git a/src/index.js b/src/index.js index 82b84b2..7a949f8 100644 --- a/src/index.js +++ b/src/index.js @@ -64,20 +64,35 @@ class AutoSSH extends EventEmitter { }); } + /* + */ + getConnectionInfo() { + const infoObj = { + kill: () => this.kill, + pid: null, + host: this.host || null, + username: this.username || null, + remotePort: parseInt(this.remotePort), + localPort: parseInt(this.localPort), + execString: this.execString || null + }; + + if (this.currentProcess) + infoObj.pid = this.currentProcess.pid; + if (!infoObj.localPort) + infoObj.localPort = null; + if (!infoObj.remotePort) + infoObj.remotePort = null; + + return infoObj; + } + /* fired when connection established */ emitConnect() { - this.emit('connect', { - kill: () => this.kill, - pid: this.currentProcess.pid, - host: this.host, - username: this.username, - remotePort: this.remotePort, - localPort: this.localPort, - execString: this.execString - }); + this.emit('connect', this.getConnectionInfo()); } - + /* fired when timeout error occurs */ emitTimeout() { @@ -96,7 +111,7 @@ class AutoSSH extends EventEmitter { */ pollConnection() { if (this.killed) - return; + return; if (this.maxPollCount && this.pollCount >= this.maxPollCount) { this.emit('error', 'Max poll count reached. Aborting...'); @@ -277,8 +292,17 @@ module.exports = function(conf) { } }; + Object.defineProperty(autosshInterface, 'info', { + get: () => autossh.getConnectionInfo() + }); + Object.defineProperty(autosshInterface, 'pid', { - get: () => autossh.currentProcess.pid + get: () => { + if (autossh.currentProcess) + return autossh.currentProcess.pid; + else + return null; + } }); return autosshInterface;