Skip to content
This repository has been archived by the owner on Feb 5, 2022. It is now read-only.

Failed error check in download callback #18

Closed
confuser opened this issue Nov 17, 2014 · 1 comment
Closed

Failed error check in download callback #18

confuser opened this issue Nov 17, 2014 · 1 comment

Comments

@confuser
Copy link

https://github.com/lepture/node-scp2/blob/master/lib/client.js#L281

Error is never checked, and instead assumes sftp will always be defined. In cases where an error has occurred, a TypeError: Cannot call method 'createReadStream' of undefined is thrown

@KSoto
Copy link
Contributor

KSoto commented May 20, 2015

Yes! I'm having the same problem :/

I use "scp" and have a block that says something like "if scp returns an err, print it and callback", but the function keeps going instead of exiting and is causing node to crash.

If I give it a hostname that works, this code works great! But if the path doesn't exist, it fails with:

TypeError: Cannot read property 'createReadStream' of undefined
    at .../app/node_modules/scp2/lib/client.js:282:31

I'll insert more code:

        scp.scp({
            host: 'somehost',
            username: 'myusername',
            password: 'mypassword',
            path: 'somepath'
        }, './', function(err) {
            console.log(err);
            callback(err); //the code doesn't stop here, though!
        });

I was able to fix this by changing this code:

Client.prototype.download = function(src, dest, callback) {
  var self = this;

  self.sftp(function(err,sftp){
    var sftp_readStream = sftp.createReadStream(src);
    sftp_readStream.on('error', function(err){
      callback(err);
    });
    sftp_readStream.pipe(fs.createWriteStream(dest))
    .on('close',function(){
      self.emit('read', src);
      callback(null);
    })
    .on('error', function(err){
      callback(err);
    });
  });
};

to this:

Client.prototype.download = function(src, dest, callback) {
  var self = this;

  self.sftp(function(err,sftp){
  if(!err) //<---
  { //<---
    var sftp_readStream = sftp.createReadStream(src);
    sftp_readStream.on('error', function(err){
      callback(err);
    });
    sftp_readStream.pipe(fs.createWriteStream(dest))
    .on('close',function(){
      self.emit('read', src);
      callback(null);
    })
    .on('error', function(err){
      callback(err);
    });
  }else //<---
  { //<---
        callback(err); //<---
  } //<---
  });
};

I'll fork the repo and do a pull request

KSoto added a commit to KSoto/node-scp2 that referenced this issue May 20, 2015
Fixing this issue: spmjs#18

If there is an error in the download function, it would break when trying to use "sftp". This fixes that.
lepture pushed a commit that referenced this issue Jun 1, 2015
Fixing this issue: #18

If there is an error in the download function, it would break when trying to use "sftp". This fixes that.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants