Socket
Socket
Sign inDemoInstall

node-ssh

Package Overview
Dependencies
11
Maintainers
1
Versions
65
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 3.0.1 to 3.1.0

8

CHANGELOG.md

@@ -0,1 +1,9 @@

#### 3.2.0
- Implement `maxAtOnce` in `putFiles()`
#### 3.1.0
- Implement sftp-backed `mkdir`
#### 3.0.1

@@ -2,0 +10,0 @@

53

lib/helpers.js

@@ -47,4 +47,5 @@ 'use strict';

exports.exists = exists;
exports.mkdirSftp = mkdirSftp;
exports.normalizePutDirectoryConfig = normalizePutDirectoryConfig;
exports.exists = exists;
exports.generateCallback = generateCallback;

@@ -60,2 +61,6 @@

var _mkdirp = require('mkdirp');
var _mkdirp2 = _interopRequireDefault(_mkdirp);
var _sbPromisify = require('sb-promisify');

@@ -67,8 +72,42 @@

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
var CODE_REGEXP = /Error: (E[\S]+): /;
var readFile = (0, _sbPromisify2.default)(_fs2.default.readFile);
var promisedMkdirp = (0, _sbPromisify2.default)(_mkdirp2.default);
var stat = exports.stat = (0, _sbPromisify2.default)(_fs2.default.stat);
var readFile = (0, _sbPromisify2.default)(_fs2.default.readFile);
var readdir = exports.readdir = (0, _sbPromisify2.default)(_fs2.default.readdir);
function transformError(givenError) {
var code = CODE_REGEXP.exec(givenError);
if (code) {
// eslint-disable-next-line no-param-reassign
givenError.code = code[1];
}
return givenError;
}
function exists(filePath) {
return new Promise(function (resolve) {
_fs2.default.access(filePath, _fs2.default.R_OK, function (error) {
resolve(!error);
});
});
}
function mkdirSftp(path, sftp) {
return promisedMkdirp(path, {
fs: {
mkdir: function mkdir(dirPath, _, cb) {
sftp.mkdir(dirPath, function (givenError) {
cb(givenError ? transformError(givenError) : null);
});
},
stat: function stat(dirPath, cb) {
sftp.stat(dirPath, cb);
}
}
});
}
function normalizePutDirectoryConfig(givenConfig) {

@@ -96,10 +135,2 @@ var config = Object.assign({}, givenConfig);

function exists(filePath) {
return new Promise(function (resolve) {
_fs2.default.access(filePath, _fs2.default.R_OK, function (error) {
resolve(!error);
});
});
}
function generateCallback(resolve, reject) {

@@ -106,0 +137,0 @@ return function (error, result) {

'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

@@ -35,3 +35,3 @@ var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { return step("next", value); }, function (err) { return step("throw", err); }); } } return step("next"); }); }; }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

@@ -109,10 +109,26 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var _ref3 = _asyncToGenerator(function* (path) {
var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'sftp';
var givenSftp = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
(0, _assert2.default)(this.connection, 'Not connected to server');
var output = yield this.exec('mkdir', ['-p', path]);
if (output.stdout) {
throw new Error(output.stdout);
(0, _assert2.default)(type === 'exec' || type === 'sftp', 'Type should either be sftp or exec');
if (type === 'exec') {
var output = yield this.exec('mkdir', ['-p', path]);
if (output.stdout) {
throw new Error(output.stdout);
}
} else {
(0, _assert2.default)(!givenSftp || (typeof givenSftp === 'undefined' ? 'undefined' : _typeof(givenSftp)) === 'object', 'sftp must be an object');
var sftp = givenSftp || (yield this.requestSFTP());
try {
yield Helpers.mkdirSftp(path, sftp);
} finally {
if (!givenSftp) {
sftp.end();
}
}
}
});
function mkdir(_x) {
function mkdir(_x, _x2, _x3) {
return _ref3.apply(this, arguments);

@@ -127,4 +143,4 @@ }

var _ref4 = _asyncToGenerator(function* (command) {
var parameters = arguments.length <= 1 || arguments[1] === undefined ? [] : arguments[1];
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var parameters = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};

@@ -149,3 +165,3 @@ (0, _assert2.default)(this.connection, 'Not connected to server');

function exec(_x2, _x3, _x4) {
function exec(_x6, _x7, _x8) {
return _ref4.apply(this, arguments);

@@ -160,3 +176,3 @@ }

var _ref5 = _asyncToGenerator(function* (givenCommand) {
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

@@ -194,3 +210,3 @@ var command = givenCommand;

function execCommand(_x7, _x8) {
function execCommand(_x11, _x12) {
return _ref5.apply(this, arguments);

@@ -205,3 +221,3 @@ }

var _ref6 = _asyncToGenerator(function* (localFile, remoteFile) {
var givenSftp = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
var givenSftp = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;

@@ -225,3 +241,3 @@ (0, _assert2.default)(this.connection, 'Not connected to server');

function getFile(_x10, _x11, _x12) {
function getFile(_x14, _x15, _x16) {
return _ref6.apply(this, arguments);

@@ -236,3 +252,3 @@ }

var _ref7 = _asyncToGenerator(function* (localFile, remoteFile) {
var givenSftp = arguments.length <= 2 || arguments[2] === undefined ? null : arguments[2];
var givenSftp = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;

@@ -271,3 +287,3 @@ (0, _assert2.default)(this.connection, 'Not connected to server');

function putFile(_x14, _x15, _x16) {
function putFile(_x18, _x19, _x20) {
return _ref7.apply(this, arguments);

@@ -284,3 +300,4 @@ }

var givenSftp = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
var givenSftp = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
var maxAtOnce = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 5;

@@ -290,2 +307,3 @@ (0, _assert2.default)(this.connection, 'Not connected to server');

(0, _assert2.default)(Array.isArray(files), 'files must be an array');
(0, _assert2.default)(typeof maxAtOnce === 'number' && Number.isFinite(maxAtOnce), 'maxAtOnce must be a valid number');

@@ -300,7 +318,16 @@ for (var i = 0, length = files.length; i < length; ++i) {

var sftp = givenSftp || (yield this.requestSFTP());
var promises = files.map(function (file) {
return _this2.putFile(file.local, file.remote, sftp);
});
var transferred = [];
try {
yield Promise.all(promises);
for (var _i = 0, _length = Math.ceil(files.length / maxAtOnce); _i < _length; _i++) {
var index = _i * maxAtOnce;
var chunk = files.slice(index, index + maxAtOnce);
yield Promise.all(chunk.map(function (file) {
return _this2.putFile(file.local, file.remote, sftp);
}));
transferred = transferred.concat(chunk);
}
} catch (error) {
error.transferred = transferred;
throw error;
} finally {

@@ -313,3 +340,3 @@ if (!sftp) {

function putFiles(_x18, _x19) {
function putFiles(_x22, _x23, _x24) {
return _ref8.apply(this, arguments);

@@ -326,4 +353,4 @@ }

var givenConfig = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2];
var givenSftp = arguments.length <= 3 || arguments[3] === undefined ? null : arguments[3];
var givenConfig = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var givenSftp = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;

@@ -350,3 +377,3 @@ (0, _assert2.default)(this.connection, 'Not connected to server');

if (!directoriesCreated.has(remoteFileDirectory)) {
yield _this3.mkdir(remoteFileDirectory);
yield _this3.mkdir(remoteFileDirectory, 'sftp', sftp);
directoriesCreated.add(remoteFileDirectory);

@@ -364,3 +391,3 @@ }

return function (_x27) {
return function (_x33) {
return _ref10.apply(this, arguments);

@@ -384,3 +411,3 @@ };

function putDirectory(_x21, _x22, _x23, _x24) {
function putDirectory(_x27, _x28, _x29, _x30) {
return _ref9.apply(this, arguments);

@@ -387,0 +414,0 @@ }

{
"name": "node-ssh",
"version": "3.0.1",
"version": "3.1.0",
"description": "SS2 with Promises",

@@ -27,2 +27,3 @@ "main": "lib/index.js",

"dependencies": {
"mkdirp": "^0.5.1",
"sb-promisify": "^2.0.1",

@@ -37,3 +38,3 @@ "sb-scandir": "^1.0.0",

"eslint-config-steelbrain": "^1.0.4",
"flow-bin": "^0.31.1",
"flow-bin": "^0.32.0",
"jasmine-fix": "^1.0.1",

@@ -40,0 +41,0 @@ "pty.js": "^0.3.1",

@@ -9,4 +9,5 @@ Node-SSH - SSH2 with Promises

```js
var path, node_ssh, ssh
var path, node_ssh, ssh, fs
fs = require('fs')
path = require('path')

@@ -20,5 +21,15 @@ node_ssh = require('node-ssh')

privateKey: '/home/steel/.ssh/id_rsa'
}).then(function() {
})
/*
Or
ssh.connect({
host: 'localhost',
username: 'steel',
privateKey: fs.readFileSync('/home/steel/.ssh/id_rsa')
})
if you want to use the raw string as private key
*/
.then(function() {
// Local, Remote
ssh.putFile('/home/steel/Lab/LocalSource', '/home/steel/Lab/RemoteTarget').then(function() {
ssh.putFile('/home/steel/Lab/localPath', '/home/steel/Lab/remotePath').then(function() {
console.log("The File thing is done")

@@ -30,3 +41,3 @@ }, function(error) {

// Array<Shape('local' => string, 'remote' => string)>
ssh.putFiles([{ local: '/home/steel/Lab/LocalSource', remote: '/home/steel/Lab/RemoteTarget' }]).then(function() {
ssh.putFiles([{ local: '/home/steel/Lab/localPath', remote: '/home/steel/Lab/remotePath' }]).then(function() {
console.log("The File thing is done")

@@ -38,3 +49,3 @@ }, function(error) {

// Local, Remote
ssh.getFile('/home/steel/Lab/RemoteSource', '/home/steel/Lab/LocalTarget').then(function(Contents) {
ssh.getFile('/home/steel/Lab/localPath', '/home/steel/Lab/remotePath').then(function(Contents) {
console.log("The File's contents were successfully downloaded")

@@ -68,3 +79,3 @@ }, function(error) {

// Command
ssh.execCommand('hh_client --json', { cwd:'/var/www', stream: 'both' }).then(function(result) {
ssh.execCommand('hh_client --json', { cwd:'/var/www' }).then(function(result) {
console.log('STDOUT: ' + result.stdout)

@@ -74,3 +85,3 @@ console.log('STDERR: ' + result.stderr)

// Command with escaped params
ssh.exec('hh_client', ['--json'], { cwd: '/var/www' }).then(function(result) {
ssh.exec('hh_client', ['--json'], { cwd: '/var/www', stream: 'stdout' }).then(function(result) {
console.log('STDOUT: ' + result)

@@ -93,3 +104,3 @@ })

getFile(localFile: string, remoteFile: string, sftp: ?Object = null): Promise<void>
putFiles(files: Array<{ local: string, remote: string }>, sftp: ?Object = null): Promise<void>
putFiles(files: Array<{ local: string, remote: string }>, sftp: ?Object = null, maxAtOnce: number = 5): Promise<void>
putDirectory(localDirectory: string, remoteDirectory: string, options: ?{ recursive: boolean, tick(localPath, remotePath, error): any, validate(localPath): boolean } = null, sftp: ?Object = null): Promise<boolean>

@@ -96,0 +107,0 @@ dispose(): void

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc