From 589a6599a1799581b062aa911de8b30028e719a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Br=C3=A5then?= Date: Sat, 29 Apr 2017 14:20:23 +0200 Subject: [PATCH 1/2] Emit transfer event on download. --- lib/client.js | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/client.js b/lib/client.js index 4af629d..3eca945 100644 --- a/lib/client.js +++ b/lib/client.js @@ -292,18 +292,27 @@ 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); - self.close(); - callback(null); - }) - .on('error', function(err){ - callback(err); + sftp.open(src, 'r', function(err, fd) { + sftp.fstat(fd, function(err, stats) { + var bufferSize = stats.size; + + var sftp_readStream = sftp.createReadStream(src); + sftp_readStream.on('data', function(data) { + bytesDownloaded += data.length; + self.emit('transfer', data, bytesDownloaded, bufferSize); + }) + 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); + }); + }); }); }); }; From 032a6184a90e7b6c01c8754c6a68e10392813342 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Br=C3=A5then?= Date: Sat, 29 Apr 2017 14:28:00 +0200 Subject: [PATCH 2/2] Adds example of downloading file from server with progress bar in README.md --- README.md | 36 ++++++++++++++++++++++++++++++++++++ lib/client.js | 1 + 2 files changed, 37 insertions(+) diff --git a/README.md b/README.md index 319c0d4..6996fdb 100644 --- a/README.md +++ b/README.md @@ -191,6 +191,42 @@ The high level client is an instance of `Client`, but it contains the high level download a server file to local. +### Example with progress bar (download from server) + +You can easily combine this module with the `progress` npm package, to get live progress data in your commandline interface. Example below: + +```javascript +var scp2 = require('scp2'); +var ProgressBar = require('progress'); + +var client = new scp2.Client(); +var bar; + +client.on('transfer', function(buf, downloaded, total) { + if (!bar) { + bar = new ProgressBar(' downloading [:bar] :rate/bps :percent :etas', { + complete: '=', + incomplete: ' ', + width: 20, + total: total + }); + } + bar.tick(buf.length); +}); + +scp2.scp({ + host: ..., + username: ..., + password: ..., + path: '/path/to/src/file' +}, '/path/to/dest/file', client, function(err) { + if (err) { + console.log(err); + } + + // Do stuff with the downloaded file +}); +``` ## Events diff --git a/lib/client.js b/lib/client.js index 3eca945..47a2966 100644 --- a/lib/client.js +++ b/lib/client.js @@ -295,6 +295,7 @@ Client.prototype.download = function(src, dest, callback) { sftp.open(src, 'r', function(err, fd) { sftp.fstat(fd, function(err, stats) { var bufferSize = stats.size; + var bytesDownloaded = 0; var sftp_readStream = sftp.createReadStream(src); sftp_readStream.on('data', function(data) {