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

Add support to 'scp' for Buffers #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ client.scp('file.txt', {
}, function(err) {})
```

Copy a [`Buffer`](https://nodejs.org/api/buffer.html) to the server:

```js
var buffer = new Buffer('Hello World\n');

client.scp(buffer, 'admin:[email protected]:port:/home/admin/', function(err) {
});
```

Copy a file to the server and rename it:

```js
Expand Down
16 changes: 16 additions & 0 deletions lib/scp.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ function cp2remote(client, src, dest, callback) {
}
}

function cpBufferToRemote(client, buffer, dest, callback) {
var remote = client.parse(dest);

client.write({
destination: remote.path,
content: buffer
}, function(err) {
client.on('close', function closeHandler() {
callback(err);
client.removeListener('close', closeHandler);
});
client.close();
});
}

function cp2local(client, src, dest, callback) {
var remote = client.parse(src);
Expand Down Expand Up @@ -100,6 +114,8 @@ exports.scp = function(src, dest, client, callback) {
var parsed = client.parse(src);
if (parsed.host && parsed.path) {
cp2local(client, parsed, dest, callback);
} else if (Buffer.isBuffer(src)) {
cpBufferToRemote(client, src, dest, callback);
} else {
cp2remote(client, src, dest, callback);
}
Expand Down
12 changes: 12 additions & 0 deletions test/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ describe('Client', function() {
});


it('returns non-string objects ', function() {
var stringBuffer = new Buffer('1234'),
arrayBuffer = new Buffer([ '1', '2', '3', '4' ]),
obj = { username: 'admin', host: 'example.com' };

expect(client.parse(false)).to.equal(false);
expect(client.parse(42)).to.equal(42);
expect(client.parse(stringBuffer)).to.equal(stringBuffer);
expect(client.parse(arrayBuffer)).to.equal(arrayBuffer);
expect(client.parse(obj)).to.equal(obj);
});

});

describe('when calling from windows', function() {
Expand Down