diff --git a/lib/client.js b/lib/client.js index ef80890..b02a8e9 100644 --- a/lib/client.js +++ b/lib/client.js @@ -11,6 +11,7 @@ function Client(options) { this._options = options || {}; this.remote = {}; + this._isConnectionReady = false; } util.inherits(Client, EventEmitter); @@ -72,6 +73,8 @@ Client.prototype.sftp = function(callback) { ssh.sftp(function(err, sftp) { if (err) throw err; + + self._isConnectionReady = true; // save for reuse self.__sftp = sftp; callback(err, sftp); @@ -82,6 +85,11 @@ Client.prototype.sftp = function(callback) { callback(err); }); ssh.on('end', function() { + if (!self._isConnectionReady){ + var err = 'Connection ended before ready'; + self.emit('error', err); + callback(err); + } self.emit('end'); }); ssh.on('close', function() {