ssh2-connect
Advanced tools
Comparing version 2.0.5 to 3.0.0
# Changelog | ||
## Version 3.0.0 | ||
Breaking changes: | ||
* src: promise based API | ||
Project Management | ||
* package: ignore lock files | ||
* package: latest dependencies | ||
* doc: new connect.js sample | ||
* wait: new option, default to 2000 | ||
* package: update author url | ||
* license: use MIT | ||
## Version 2.0.5 | ||
@@ -5,0 +18,0 @@ |
158
lib/index.js
@@ -1,29 +0,32 @@ | ||
// Generated by CoffeeScript 2.3.2 | ||
// Generated by CoffeeScript 2.5.1 | ||
// # Connect | ||
// This module provide a callback based api to open an ssh2 connection. | ||
// This module provide a promise based api to open an ssh2 connection. | ||
// For example, the original ssh2 code... | ||
// ```coffee | ||
// ssh2 = require 'ssh2' | ||
// connection = new ssh2() | ||
// connection.on 'error', (err) -> | ||
// ```js | ||
// const ssh2 = require('ssh2') | ||
// const connection = new ssh2() | ||
// connection.on('error', (err) => { | ||
// // not ready at all | ||
// connection.end() | ||
// # not ready at all | ||
// connection.on 'ready', -> | ||
// # ready to go | ||
// connection.connect options | ||
// }) | ||
// connection.on('ready', () => { | ||
// // ready to go | ||
// }) | ||
// connection.connect(options) | ||
// ``` | ||
// ...is simplified to: | ||
// ...is now simplified to: | ||
// ```coffee | ||
// connect = require 'ssh2-exec/lib/connect' | ||
// connect options, (err, ssh) -> | ||
// # this is faster to write | ||
// ```js | ||
// const connect = require('ssh2-exec') | ||
// const ssh = await connect(options, (err, ssh) -> | ||
// // this is more comprehensive | ||
// ) | ||
// ``` | ||
var camelize, fs, ssh2; | ||
fs = require('fs'); | ||
fs = require('fs').promises; | ||
@@ -51,67 +54,72 @@ ssh2 = require('ssh2'); | ||
// interprated the same. | ||
module.exports = function(options, callback) { | ||
var connect, match, privateKeyPath, retry, st; | ||
if (options instanceof ssh2) { | ||
return callback(null, options); | ||
} | ||
options = camelize(options); | ||
if (options.username == null) { | ||
options.username = process.env['USER'] || require('child_process').execSync("whoami", { | ||
encoding: 'utf8', | ||
timeout: 1000 | ||
}).trim(); | ||
} | ||
if (options.username == null) { | ||
options.username = 'root'; // We've seed 'USER' not inside env inside the docker centos6 container. | ||
} | ||
if (options.retry == null) { | ||
options.retry = 1; | ||
} | ||
if (!options.password && !options.privateKey) { | ||
if (options.privateKeyPath == null) { | ||
options.privateKeyPath = '~/.ssh/id_rsa'; | ||
module.exports = function(options) { | ||
return new Promise(async function(resolve, reject) { | ||
var connect, e, match, retry; | ||
if (options instanceof ssh2) { | ||
return resolve(options); | ||
} | ||
if (options.privateKeyPath && (match = /~(\/.*)/.exec(options.privateKeyPath))) { | ||
options.privateKeyPath = process.env.HOME + match[1]; | ||
options = camelize(options); | ||
if (options.username == null) { | ||
options.username = process.env['USER'] || require('child_process').execSync("whoami", { | ||
encoding: 'utf8', | ||
timeout: 1000 | ||
}).trim(); | ||
} | ||
} else { | ||
options.privateKeyPath = null; | ||
} | ||
privateKeyPath = function() { | ||
if (!options.privateKeyPath) { | ||
return connect(); | ||
if (options.username == null) { | ||
options.username = 'root'; // We've seed 'USER' not inside env inside the docker centos6 container. | ||
} | ||
return fs.readFile(options.privateKeyPath, 'ascii', function(err, privateKey) { | ||
options.privateKey = privateKey; | ||
return connect(); | ||
}); | ||
}; | ||
retry = options.retry; | ||
st = Date.now(); | ||
connect = function() { | ||
var connection, succeed; | ||
if (retry !== true && retry > 0) { | ||
retry--; | ||
if (options.retry == null) { | ||
options.retry = 1; | ||
} | ||
succeed = false; | ||
connection = new ssh2(); | ||
connection.on('error', function(err) { | ||
connection.end(); | ||
// Event "error" is thrown after a "ready" if the connection is lost | ||
if (succeed) { | ||
return; | ||
if (options.wait == null) { | ||
options.wait = 500; | ||
} | ||
if (!options.password && !options.privateKey) { | ||
if (options.privateKeyPath == null) { | ||
options.privateKeyPath = '~/.ssh/id_rsa'; | ||
} | ||
if (retry === true || retry > 0) { | ||
return setTimeout(connect, 2000); | ||
} else { | ||
return callback(err); | ||
if (options.privateKeyPath && (match = /~(\/.*)/.exec(options.privateKeyPath))) { | ||
options.privateKeyPath = process.env.HOME + match[1]; | ||
} | ||
}); | ||
connection.on('ready', function() { | ||
succeed = true; | ||
return callback(null, connection); | ||
}); | ||
return connection.connect(options); | ||
}; | ||
return privateKeyPath(); | ||
} else { | ||
options.privateKeyPath = null; | ||
} | ||
try { | ||
// Extract private key from file | ||
if (options.privateKeyPath) { | ||
options.privateKey = (await fs.readFile(options.privateKeyPath, 'ascii')); | ||
} | ||
} catch (error) { | ||
e = error; | ||
return reject(e); | ||
} | ||
// Connection attempts | ||
retry = options.retry; | ||
connect = function() { | ||
var connection, succeed; | ||
if (retry !== true && retry > 0) { | ||
retry--; | ||
} | ||
succeed = false; | ||
connection = new ssh2(); | ||
connection.on('error', function(err) { | ||
connection.end(); | ||
// Event "error" is thrown after a "ready" if the connection is lost | ||
if (succeed) { | ||
return; | ||
} | ||
if (retry === true || retry > 0) { | ||
return setTimeout(connect, options.wait); | ||
} else { | ||
return reject(err); | ||
} | ||
}); | ||
connection.on('ready', function() { | ||
succeed = true; | ||
return resolve(connection); | ||
}); | ||
return connection.connect(options); | ||
}; | ||
return connect(); | ||
}); | ||
}; | ||
@@ -118,0 +126,0 @@ |
@@ -1,28 +0,21 @@ | ||
Copyright (c) 2014, SARL Adaltas. | ||
All rights reserved. | ||
The MIT License (MIT) | ||
Redistribution and use of this software in source and binary forms, with or | ||
without modification, are permitted provided that the following conditions | ||
are met: | ||
Copyright (c) 2010 Adaltas | ||
- Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
- Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
- Neither the name of SARL Adaltas nor the names of its contributors may be | ||
used to endorse or promote products derived from this software without | ||
specific prior written permission of SARL Adaltas. | ||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
{ | ||
"name": "ssh2-connect", | ||
"description": "Callback-based api behind ssh2 to open an SSH connection", | ||
"version": "2.0.5", | ||
"author": "David Worms <david@adaltas.com>", | ||
"version": "3.0.0", | ||
"author": "David Worms <david@adaltas.com> (https://www.adaltas.com)", | ||
"coffeelintConfig": { | ||
"indentation": { | ||
"level": "error", | ||
"value": 2 | ||
}, | ||
"line_endings": { | ||
"level": "error", | ||
"value": "unix" | ||
}, | ||
"max_line_length": { | ||
"level": "ignore" | ||
}, | ||
"no_backticks": { | ||
"level": "ignore" | ||
}, | ||
"no_nested_string_interpolation": { | ||
"level": "ignore" | ||
} | ||
}, | ||
"contributors": [], | ||
"dependencies": { | ||
"ssh2": "~0.8.2" | ||
"ssh2": "~0.8.9" | ||
}, | ||
"devDependencies": { | ||
"coffeescript": "^2.3.2", | ||
"mocha": "~5.2.0", | ||
"coffeescript": "^2.5.1", | ||
"mocha": "~7.1.1", | ||
"should": "~13.2.3" | ||
@@ -29,3 +48,3 @@ }, | ||
], | ||
"license": "BSD-3-Clause", | ||
"license": "MIT", | ||
"main": "./lib/index", | ||
@@ -44,6 +63,6 @@ "optionalDependencies": {}, | ||
"major": "npm version major -m 'Bump to version %s'", | ||
"coffee": "coffee -b -o lib src", | ||
"pretest": "coffee -b -o lib src", | ||
"test": "mocha test/*.coffee" | ||
"build": "coffee -b -o lib src", | ||
"pretest": "npm run build", | ||
"test": "mocha 'test/**/*.coffee'" | ||
} | ||
} |
@@ -19,6 +19,8 @@ [![Build Status](https://secure.travis-ci.org/adaltas/node-ssh2-connect.png)][travis] | ||
connection.on('error', function(err){ | ||
// not ready at all | ||
// Handle the connection error | ||
connection.end() | ||
}) | ||
connection.on('ready', function(){ | ||
// ready to go | ||
// Work with the connection | ||
connection.end() | ||
}) | ||
@@ -36,9 +38,15 @@ connection.connect({ | ||
const connect = require('ssh2-connect') | ||
connect({ | ||
host: 'localhost', | ||
username: 'milou', | ||
password: 'wafwaf' | ||
}, function(err, ssh){ | ||
// this is easier to write | ||
}) | ||
(async () => { | ||
try{ | ||
const ssh = await connect({ | ||
host: 'localhost', | ||
username: 'david', | ||
private_key_path: '~/.ssh/id_rsa' | ||
}) | ||
// Work with the connection | ||
ssh.end() | ||
}catch (err){ | ||
// Handle the connection error | ||
} | ||
})() | ||
``` | ||
@@ -59,3 +67,3 @@ | ||
- `wait` | ||
Time to wait in milliseconds between each retry, default to "500". | ||
Time to wait in milliseconds between each retry, default to "2000". | ||
@@ -116,2 +124,3 @@ Note, the "privateKeyPath" option is provided as a conveniency to prepare the | ||
To run the tests: | ||
```bash | ||
@@ -121,11 +130,10 @@ npm test | ||
The tests run against the CoffeeScript source files. | ||
To generate the JavaScript files: | ||
To generate the JavaScript files: | ||
```bash | ||
make build | ||
npm run build | ||
``` | ||
The test suite is run online with [Travis][travis] against Node.js version 0.9, | ||
0.10 and 0.11. | ||
The test suite is run online with [Travis][travis] against several Node.js | ||
version. | ||
@@ -136,7 +144,7 @@ ## Contributors | ||
This package is developed by [Adaltas](http://www.adaltas.com). | ||
This package is developed by [Adaltas](https://www.adaltas.com). | ||
[travis]: http://travis-ci.org/adaltas/node-ssh2-connect | ||
[ssh2]: https://github.com/mscdex/ssh2 | ||
[ssh2-connect]: https://github.com/mscdex/ssh2 | ||
[ssh2-connect]: https://github.com/adaltas/node-ssh2-connect | ||
[license]: https://github.com/adaltas/node-ssh2-connect/blob/master/LICENSE.md |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11223
6
144
145
5
Updatedssh2@~0.8.9