Skip to content

Commit

Permalink
Emit progress event during file download
Browse files Browse the repository at this point in the history
This changes allows to monitor file transfer progress

related issues spmjs#43 spmjs#57
  • Loading branch information
dbaikov committed Feb 28, 2016
1 parent b27058d commit 8d18b7c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
37 changes: 25 additions & 12 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var async = require('async');
var EventEmitter = require('events').EventEmitter;
var Connection = require('ssh2');
var _ = require('lodash');

var progressStream = require('progress-stream');

function Client(options) {
this._options = options || {};
Expand Down Expand Up @@ -283,17 +283,30 @@ Client.prototype.download = function(src, dest, callback) {
return callback(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);
sftp.stat(src, function (err, stat) {
if (err) {
return callback(err);
}
var ps = progressStream({
length: stat.size,
time: 100
});
ps.on('progress', function (progress) {
self.emit('progress', progress);
});
var sftp_readStream = sftp.createReadStream(src);
sftp_readStream.on('error', function(err){
callback(err);
});
sftp_readStream.pipe(ps).pipe(fs.createWriteStream(dest))
.on('close',function(){
self.emit('read', src);
callback(null);
})
.on('error', function(err){
callback(err);
});
return self;
});
});
};
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
"ssh2"
],
"dependencies": {
"ssh2": "~0.4.10",
"glob": "~4.0.6",
"async": "~0.9.0",
"lodash": "~2.4.1"
"glob": "~4.0.6",
"lodash": "~2.4.1",
"progress-stream": "^1.2.0",
"ssh2": "~0.4.10"
},
"repository": {
"type": "git",
Expand Down

0 comments on commit 8d18b7c

Please sign in to comment.