spmjs / node-scp2

[MAINTAINER WANTED] A pure javascript scp program based on ssh2.
384 stars 96 forks source link

Bad destination of .scp throws error "handle is not a Buffer" #54

Open donbarthel opened 8 years ago

donbarthel commented 8 years ago

If the second parameter of .scp, the destination, is invalid it throws an undescriptive (IMHO) error message "handle is not a Buffer".

Example:

client.scp('file.txt', 'admin:password@example.com:/home/blah/', function(err) {...}) 

...where "/home/blah" doesn't exist.

In client.js at the end of Client.prototype.write() is:

sftp.open(destination, 'w', attrs, function(err, handle) {
  _write(handle);
});

I'm interpreting that the code at this point has already decided that the destination isn't absolute so it assumes that its relative, prepends a path, but after calling sftp.open() again it doesn't check the return value of err before calling _write().

The solution could be to check the value of err and act accordingly:

sftp.open(destination, 'w', attrs, function(err, handle) {
  if (err)
    throw new Error('Destination is invalid');
  _write(handle);
});
sjkmuninj commented 5 years ago

It does not work for me too.