sftp-promises
Advanced tools
Comparing version 1.2.4 to 1.3.0
82
index.js
@@ -428,4 +428,86 @@ var Client = require('ssh2').Client | ||
/** | ||
* stream file contents from remote file | ||
* | ||
* @parm {string} path - remote file path | ||
* @parm {writableStream} writableStream - writable stream to pipe read data to | ||
* @parm {ssh2.Client} [session] - existing ssh2 connection | ||
*/ | ||
SFTPClient.prototype.getStream = function getStream(path, writableStream, session) { | ||
var getStreamCmd = function (resolve, reject) { | ||
return function (err, sftp) { | ||
if (!writableStream.writable) { | ||
reject(new Error('Stream must be a writable stream')) | ||
return | ||
} | ||
if (err) { | ||
reject(err) | ||
return | ||
} | ||
sftp.stat(path, function (err, stat) { | ||
if (err) { | ||
reject(err) | ||
return | ||
} | ||
var bytes = stat.size | ||
if (bytes > 0) { | ||
bytes -= 1 | ||
} | ||
try { | ||
var stream = sftp.createReadStream(path, { start: 0, end: bytes}) | ||
} catch(err) { | ||
reject(err) | ||
return | ||
} | ||
stream.pipe(writableStream) | ||
stream.on('end', function() { | ||
resolve(true) | ||
}) | ||
stream.on('error', function (err) { | ||
reject(err) | ||
}) | ||
}) | ||
} | ||
} | ||
return this.sftpCmd(getStreamCmd, session) | ||
} | ||
/** | ||
* stream file contents from local file | ||
* | ||
* @parm {string} path - remote file path | ||
* @parm {writableStream} writableStream - writable stream to pipe read data to | ||
* @parm {ssh2.Client} [session] - existing ssh2 connection | ||
*/ | ||
SFTPClient.prototype.putStream = function putStream(path, readableStream, session) { | ||
var putStreamCmd = function (resolve, reject) { | ||
return function (err, sftp) { | ||
if (!readableStream.readable) { | ||
reject(new Error('Stream must be a readable stream')) | ||
return | ||
} | ||
if (err) { | ||
reject(err) | ||
return | ||
} | ||
try { | ||
var stream = sftp.createWriteStream(path) | ||
} catch (err) { | ||
reject(err) | ||
return | ||
} | ||
readableStream.pipe(stream) | ||
stream.on('finish', function () { | ||
resolve(true) | ||
}) | ||
stream.on('error', function (err) { | ||
reject(err) | ||
}) | ||
} | ||
} | ||
return this.sftpCmd(putStreamCmd, session) | ||
} | ||
// export client | ||
module.exports = SFTPClient | ||
{ | ||
"name": "sftp-promises", | ||
"version": "1.2.4", | ||
"version": "1.3.0", | ||
"description": "SFTP Promise wrapper for ssh2 SFTP commands", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -12,2 +12,5 @@ # sftp-promises | ||
### Streams | ||
The current streams implementation requires supplying either readable or writable streams and do not return promises with streams like the getBuffer method. This is to support non-presistent connections which would drop if a stream was returned on the promise resolve. | ||
# Usage | ||
@@ -53,9 +56,11 @@ _**One connection per call**_ | ||
**sftp.rm(\<string>location, [ssh2.Connection]session)** returns a promise with a boolean, true if successful | ||
**sftp.mv(\<string>src, \<string>dest, [ssh2.Connection]session)** returns a promise with a boolean, true if successful | ||
**sftp.mv(\<string>src, \<string>dest, [ssh2.Connection]session)** returns a promise with a boolean, true if successful | ||
**sftp.mkdir(\<string>path, [ssh2.Connection]session)** returns a promise with a boolean, true if successful | ||
**sftp.rmdir(\<string>path, [ssh2.Connection]session)** returns a promise with a boolean, true if successful | ||
**sftp.getStream(\<string>path, <writableStream>writableStream, [ssh2.Connection]session)** returns a promise with a boolean, true if stream write completed | ||
**sftp.putStream(\<string>path, <writableStream>writableStream, [ssh2.Connection]session)** returns a promise with a boolean, true is stream write completed | ||
# Planned Features/Updates | ||
* Streaming implementation for get and put | ||
* mkdir and rmdir calls | ||
* have test rely on ssh2.server instead of local ssh server | ||
@@ -122,2 +122,30 @@ var fs = require('fs') | ||
describe('getStream(path, writableStream)', function() { | ||
it('getStream("/tmp/test.dat", writableStream) should be true', function() { | ||
var stream = fs.createWriteStream('/dev/null') | ||
return sftp.getStream('/tmp/test.dat', stream).should.eventually.be.true | ||
}) | ||
it('getStream("/tmp/test.dat", nonWritableStream) should reject', function () { | ||
return sftp.getStream('/tmp/test.dat', 'notastream').should.be.rejected | ||
}) | ||
it('getStream("/nonexistantfile", writableStream) should reject', function () { | ||
var stream = fs.createWriteStream('/dev/null') | ||
return sftp.getStream('/nonexistantfile', stream).should.be.rejected | ||
}) | ||
}) | ||
describe('putStream(path, readableStream)', function() { | ||
it('putStream("/tmp/test-stream.dat", readStream) should be true', function() { | ||
var stream = fs.createReadStream('test/fixtures/test.dat') | ||
return sftp.putStream('/tmp/test.dat', stream).should.eventually.be.true | ||
}) | ||
it('getStream("/tmp/test.dat", nonReadableStream) should reject', function () { | ||
return sftp.putStream('/tmp/test.dat', 'notastream').should.be.rejected | ||
}) | ||
it('getStream("/nonewritable/location", writableStream) should reject', function () { | ||
var stream = fs.createWriteStream('test/fixtures/test.dat') | ||
return sftp.getStream('/cantwritehere', stream).should.be.rejected | ||
}) | ||
}) | ||
describe('mv(source, dest)', function () { | ||
@@ -182,1 +210,3 @@ it('mv("/tmp/test.dat", "/tmp/test.mv.dat") should move a remote file', function () { | ||
}) | ||
Sorry, the diff of this file is not supported yet
723
65
28676